blob: 544e50e67531fa3c0c06c21cd650c8c4cc3c90ea [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
Dave Rodgman7ff79652023-11-03 12:04:52 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker5121ce52009-01-03 21:22:43 +00006 */
7
Gilles Peskinedb09ef62020-06-03 01:43:33 +02008#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000011
SimonBd5800b72016-04-26 07:43:27 +010012#include "mbedtls/platform.h"
SimonBd5800b72016-04-26 07:43:27 +010013
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000014#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020015#include "mbedtls/ssl_internal.h"
Janos Follath73c616b2019-12-18 15:07:04 +000016#include "mbedtls/debug.h"
17#include "mbedtls/error.h"
Andres Amaya Garcia84914062018-04-24 08:40:46 -050018#include "mbedtls/platform_util.h"
Gabor Mezeic0ae1cf2021-10-20 12:09:35 +020019#include "constant_time_internal.h"
Gabor Mezeie24dea82021-10-19 12:22:25 +020020#include "mbedtls/constant_time.h"
Rich Evans00ab4702015-02-06 13:43:58 +000021
22#include <string.h>
23
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020024#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000025#include "mbedtls/ecp.h"
Paul Bakker41c83d32013-03-20 14:39:14 +010026#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_HAVE_TIME)
Simon Butcherb5b6af22016-07-13 14:46:18 +010029#include "mbedtls/platform_time.h"
Paul Bakkerfa9b1002013-07-03 15:31:03 +020030#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010033int mbedtls_ssl_set_client_transport_id(mbedtls_ssl_context *ssl,
34 const unsigned char *info,
35 size_t ilen)
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020036{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010037 if (ssl->conf->endpoint != MBEDTLS_SSL_IS_SERVER) {
38 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
39 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020040
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010041 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020042
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010043 if ((ssl->cli_id = mbedtls_calloc(1, ilen)) == NULL) {
44 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
45 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020046
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010047 memcpy(ssl->cli_id, info, ilen);
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020048 ssl->cli_id_len = ilen;
49
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010050 return 0;
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020051}
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +020052
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010053void mbedtls_ssl_conf_dtls_cookies(mbedtls_ssl_config *conf,
54 mbedtls_ssl_cookie_write_t *f_cookie_write,
55 mbedtls_ssl_cookie_check_t *f_cookie_check,
56 void *p_cookie)
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +020057{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +020058 conf->f_cookie_write = f_cookie_write;
59 conf->f_cookie_check = f_cookie_check;
60 conf->p_cookie = p_cookie;
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +020061}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +020065MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010066static int ssl_parse_servername_ext(mbedtls_ssl_context *ssl,
67 const unsigned char *buf,
68 size_t len)
Paul Bakker5701cdc2012-09-27 21:49:42 +000069{
Janos Follath865b3eb2019-12-16 11:46:15 +000070 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5701cdc2012-09-27 21:49:42 +000071 size_t servername_list_size, hostname_len;
Paul Bakker23f36802012-09-28 14:15:14 +000072 const unsigned char *p;
Paul Bakker5701cdc2012-09-27 21:49:42 +000073
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010074 MBEDTLS_SSL_DEBUG_MSG(3, ("parse ServerName extension"));
Manuel Pégourié-Gonnard96ea2f22014-02-25 12:26:29 +010075
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010076 if (len < 2) {
77 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
78 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
79 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
80 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Philippe Antoine747fd532018-05-30 09:13:21 +020081 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010082 servername_list_size = ((buf[0] << 8) | (buf[1]));
83 if (servername_list_size + 2 != len) {
84 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
85 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
86 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
87 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Paul Bakker5701cdc2012-09-27 21:49:42 +000088 }
89
90 p = buf + 2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010091 while (servername_list_size > 2) {
92 hostname_len = ((p[1] << 8) | p[2]);
93 if (hostname_len + 3 > servername_list_size) {
94 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
95 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
96 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
97 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Paul Bakker5701cdc2012-09-27 21:49:42 +000098 }
99
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100100 if (p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME) {
101 ret = ssl->conf->f_sni(ssl->conf->p_sni,
102 ssl, p + 3, hostname_len);
103 if (ret != 0) {
104 MBEDTLS_SSL_DEBUG_RET(1, "ssl_sni_wrapper", ret);
105 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
106 MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME);
107 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Paul Bakker5701cdc2012-09-27 21:49:42 +0000108 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100109 return 0;
Paul Bakker5701cdc2012-09-27 21:49:42 +0000110 }
111
112 servername_list_size -= hostname_len + 3;
Paul Bakker23f36802012-09-28 14:15:14 +0000113 p += hostname_len + 3;
114 }
115
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100116 if (servername_list_size != 0) {
117 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
118 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
119 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
120 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Paul Bakker5701cdc2012-09-27 21:49:42 +0000121 }
122
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100123 return 0;
Paul Bakker5701cdc2012-09-27 21:49:42 +0000124}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +0000126
Gilles Peskineeccd8882020-03-10 12:19:08 +0100127#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200128MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100129static int ssl_conf_has_psk_or_cb(mbedtls_ssl_config const *conf)
Hanno Becker845b9462018-10-26 12:07:29 +0100130{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100131 if (conf->f_psk != NULL) {
132 return 1;
133 }
Hanno Becker845b9462018-10-26 12:07:29 +0100134
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100135 if (conf->psk_identity_len == 0 || conf->psk_identity == NULL) {
136 return 0;
137 }
Hanno Becker845b9462018-10-26 12:07:29 +0100138
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100139 if (conf->psk != NULL && conf->psk_len != 0) {
140 return 1;
141 }
Hanno Becker845b9462018-10-26 12:07:29 +0100142
143#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100144 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
145 return 1;
146 }
Hanno Becker845b9462018-10-26 12:07:29 +0100147#endif /* MBEDTLS_USE_PSA_CRYPTO */
148
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100149 return 0;
Hanno Becker845b9462018-10-26 12:07:29 +0100150}
151
152#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200153MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100154static int ssl_use_opaque_psk(mbedtls_ssl_context const *ssl)
Hanno Becker845b9462018-10-26 12:07:29 +0100155{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100156 if (ssl->conf->f_psk != NULL) {
Hanno Becker845b9462018-10-26 12:07:29 +0100157 /* If we've used a callback to select the PSK,
158 * the static configuration is irrelevant. */
159
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100160 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
161 return 1;
162 }
Hanno Becker845b9462018-10-26 12:07:29 +0100163
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100164 return 0;
Hanno Becker845b9462018-10-26 12:07:29 +0100165 }
166
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100167 if (!mbedtls_svc_key_id_is_null(ssl->conf->psk_opaque)) {
168 return 1;
169 }
Hanno Becker845b9462018-10-26 12:07:29 +0100170
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100171 return 0;
Hanno Becker845b9462018-10-26 12:07:29 +0100172}
173#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskineeccd8882020-03-10 12:19:08 +0100174#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Hanno Becker845b9462018-10-26 12:07:29 +0100175
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200176MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100177static int ssl_parse_renegotiation_info(mbedtls_ssl_context *ssl,
178 const unsigned char *buf,
179 size_t len)
Paul Bakker48916f92012-09-16 19:57:18 +0000180{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100182 if (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE) {
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100183 /* Check verify-data in constant-time. The length OTOH is no secret */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100184 if (len != 1 + ssl->verify_data_len ||
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100185 buf[0] != ssl->verify_data_len ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100186 mbedtls_ct_memcmp(buf + 1, ssl->peer_verify_data,
187 ssl->verify_data_len) != 0) {
188 MBEDTLS_SSL_DEBUG_MSG(1, ("non-matching renegotiation info"));
189 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
190 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
191 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100192 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100193 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +0000195 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100196 if (len != 1 || buf[0] != 0x0) {
197 MBEDTLS_SSL_DEBUG_MSG(1, ("non-zero length renegotiation info"));
198 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
199 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
200 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Paul Bakker48916f92012-09-16 19:57:18 +0000201 }
202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +0000204 }
Paul Bakker48916f92012-09-16 19:57:18 +0000205
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100206 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +0000207}
208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +0100210 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +0100211
212/*
213 * Status of the implementation of signature-algorithms extension:
214 *
215 * Currently, we are only considering the signature-algorithm extension
216 * to pick a ciphersuite which allows us to send the ServerKeyExchange
217 * message with a signature-hash combination that the user allows.
218 *
219 * We do *not* check whether all certificates in our certificate
220 * chain are signed with an allowed signature-hash pair.
221 * This needs to be done at a later stage.
222 *
223 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200224MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100225static int ssl_parse_signature_algorithms_ext(mbedtls_ssl_context *ssl,
226 const unsigned char *buf,
227 size_t len)
Paul Bakker23f36802012-09-28 14:15:14 +0000228{
229 size_t sig_alg_list_size;
Hanno Becker7e5437a2017-04-28 17:15:26 +0100230
Paul Bakker23f36802012-09-28 14:15:14 +0000231 const unsigned char *p;
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200232 const unsigned char *end = buf + len;
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200233
Hanno Becker7e5437a2017-04-28 17:15:26 +0100234 mbedtls_md_type_t md_cur;
235 mbedtls_pk_type_t sig_cur;
Paul Bakker23f36802012-09-28 14:15:14 +0000236
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100237 if (len < 2) {
238 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
239 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
240 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
241 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Philippe Antoine747fd532018-05-30 09:13:21 +0200242 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100243 sig_alg_list_size = ((buf[0] << 8) | (buf[1]));
244 if (sig_alg_list_size + 2 != len ||
245 sig_alg_list_size % 2 != 0) {
246 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
247 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
248 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
249 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Paul Bakker23f36802012-09-28 14:15:14 +0000250 }
251
Hanno Becker7e5437a2017-04-28 17:15:26 +0100252 /* Currently we only guarantee signing the ServerKeyExchange message according
253 * to the constraints specified in this extension (see above), so it suffices
254 * to remember only one suitable hash for each possible signature algorithm.
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200255 *
Hanno Becker7e5437a2017-04-28 17:15:26 +0100256 * This will change when we also consider certificate signatures,
257 * in which case we will need to remember the whole signature-hash
258 * pair list from the extension.
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200259 */
Hanno Becker7e5437a2017-04-28 17:15:26 +0100260
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100261 for (p = buf + 2; p < end; p += 2) {
Hanno Becker7e5437a2017-04-28 17:15:26 +0100262 /* Silently ignore unknown signature or hash algorithms. */
263
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100264 if ((sig_cur = mbedtls_ssl_pk_alg_from_sig(p[1])) == MBEDTLS_PK_NONE) {
265 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello v3, signature_algorithm ext"
266 " unknown sig alg encoding %d", p[1]));
Hanno Becker7e5437a2017-04-28 17:15:26 +0100267 continue;
268 }
269
270 /* Check if we support the hash the user proposes */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100271 md_cur = mbedtls_ssl_md_alg_from_hash(p[0]);
272 if (md_cur == MBEDTLS_MD_NONE) {
273 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello v3, signature_algorithm ext:"
274 " unknown hash alg encoding %d", p[0]));
Hanno Becker7e5437a2017-04-28 17:15:26 +0100275 continue;
276 }
277
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100278 if (mbedtls_ssl_check_sig_hash(ssl, md_cur) == 0) {
279 mbedtls_ssl_sig_hash_set_add(&ssl->handshake->hash_algs, sig_cur, md_cur);
280 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello v3, signature_algorithm ext:"
281 " match sig %u and hash %u",
282 (unsigned) sig_cur, (unsigned) md_cur));
283 } else {
284 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello v3, signature_algorithm ext: "
285 "hash alg %u not supported", (unsigned) md_cur));
Paul Bakker23f36802012-09-28 14:15:14 +0000286 }
Paul Bakker23f36802012-09-28 14:15:14 +0000287 }
288
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100289 return 0;
Paul Bakker23f36802012-09-28 14:15:14 +0000290}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Gilles Peskineeccd8882020-03-10 12:19:08 +0100292 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Paul Bakker23f36802012-09-28 14:15:14 +0000293
Robert Cragie136884c2015-10-02 13:34:31 +0100294#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +0100295 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200296MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100297static int ssl_parse_supported_elliptic_curves(mbedtls_ssl_context *ssl,
298 const unsigned char *buf,
299 size_t len)
Paul Bakker41c83d32013-03-20 14:39:14 +0100300{
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200301 size_t list_size, our_size;
Paul Bakker41c83d32013-03-20 14:39:14 +0100302 const unsigned char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303 const mbedtls_ecp_curve_info *curve_info, **curves;
Paul Bakker41c83d32013-03-20 14:39:14 +0100304
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100305 if (len < 2) {
306 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
307 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
308 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
309 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Philippe Antoine747fd532018-05-30 09:13:21 +0200310 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100311 list_size = ((buf[0] << 8) | (buf[1]));
312 if (list_size + 2 != len ||
313 list_size % 2 != 0) {
314 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
315 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
316 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
317 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Paul Bakker41c83d32013-03-20 14:39:14 +0100318 }
319
Manuel Pégourié-Gonnard43c3b282014-10-17 12:42:11 +0200320 /* Should never happen unless client duplicates the extension */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100321 if (ssl->handshake->curves != NULL) {
322 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
323 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
324 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
325 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Manuel Pégourié-Gonnard43c3b282014-10-17 12:42:11 +0200326 }
327
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +0100328 /* Don't allow our peer to make us allocate too much memory,
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200329 * and leave room for a final 0 */
330 our_size = list_size / 2 + 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100331 if (our_size > MBEDTLS_ECP_DP_MAX) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332 our_size = MBEDTLS_ECP_DP_MAX;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100333 }
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200334
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100335 if ((curves = mbedtls_calloc(our_size, sizeof(*curves))) == NULL) {
336 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
337 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
338 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200339 }
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200340
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200341 ssl->handshake->curves = curves;
342
Paul Bakker41c83d32013-03-20 14:39:14 +0100343 p = buf + 2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100344 while (list_size > 0 && our_size > 1) {
345 curve_info = mbedtls_ecp_curve_info_from_tls_id((p[0] << 8) | p[1]);
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200346
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100347 if (curve_info != NULL) {
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200348 *curves++ = curve_info;
349 our_size--;
Paul Bakker41c83d32013-03-20 14:39:14 +0100350 }
351
352 list_size -= 2;
353 p += 2;
354 }
355
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100356 return 0;
Paul Bakker41c83d32013-03-20 14:39:14 +0100357}
358
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200359MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100360static int ssl_parse_supported_point_formats(mbedtls_ssl_context *ssl,
361 const unsigned char *buf,
362 size_t len)
Paul Bakker41c83d32013-03-20 14:39:14 +0100363{
364 size_t list_size;
365 const unsigned char *p;
366
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100367 if (len == 0 || (size_t) (buf[0] + 1) != len) {
368 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
369 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
370 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
371 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Paul Bakker41c83d32013-03-20 14:39:14 +0100372 }
Philippe Antoine747fd532018-05-30 09:13:21 +0200373 list_size = buf[0];
Paul Bakker41c83d32013-03-20 14:39:14 +0100374
Manuel Pégourié-Gonnardc1b46d02015-09-16 11:18:32 +0200375 p = buf + 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100376 while (list_size > 0) {
377 if (p[0] == MBEDTLS_ECP_PF_UNCOMPRESSED ||
378 p[0] == MBEDTLS_ECP_PF_COMPRESSED) {
Robert Cragie136884c2015-10-02 13:34:31 +0100379#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200380 ssl->handshake->ecdh_ctx.point_format = p[0];
Robert Cragie136884c2015-10-02 13:34:31 +0100381#endif
Robert Cragieae8535d2015-10-06 17:11:18 +0100382#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Robert Cragie136884c2015-10-02 13:34:31 +0100383 ssl->handshake->ecjpake_ctx.point_format = p[0];
384#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100385 MBEDTLS_SSL_DEBUG_MSG(4, ("point format selected: %d", p[0]));
386 return 0;
Paul Bakker41c83d32013-03-20 14:39:14 +0100387 }
388
389 list_size--;
390 p++;
391 }
392
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100393 return 0;
Paul Bakker41c83d32013-03-20 14:39:14 +0100394}
Robert Cragieae8535d2015-10-06 17:11:18 +0100395#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
396 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +0100397
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +0200398#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200399MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100400static int ssl_parse_ecjpake_kkpp(mbedtls_ssl_context *ssl,
401 const unsigned char *buf,
402 size_t len)
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +0200403{
Janos Follath865b3eb2019-12-16 11:46:15 +0000404 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +0200405
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100406 if (mbedtls_ecjpake_check(&ssl->handshake->ecjpake_ctx) != 0) {
407 MBEDTLS_SSL_DEBUG_MSG(3, ("skip ecjpake kkpp extension"));
408 return 0;
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +0200409 }
410
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100411 if ((ret = mbedtls_ecjpake_read_round_one(&ssl->handshake->ecjpake_ctx,
412 buf, len)) != 0) {
413 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_read_round_one", ret);
414 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
415 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
416 return ret;
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +0200417 }
418
419 /* Only mark the extension as OK when we're sure it is */
420 ssl->handshake->cli_exts |= MBEDTLS_TLS_EXT_ECJPAKE_KKPP_OK;
421
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100422 return 0;
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +0200423}
424#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200427MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100428static int ssl_parse_max_fragment_length_ext(mbedtls_ssl_context *ssl,
429 const unsigned char *buf,
430 size_t len)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200431{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100432 if (len != 1 || buf[0] >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID) {
433 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
434 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
435 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
436 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200437 }
438
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200439 ssl->session_negotiate->mfl_code = buf[0];
440
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100441 return 0;
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200442}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200444
Hanno Beckera0e20d02019-05-15 14:03:01 +0100445#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200446MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100447static int ssl_parse_cid_ext(mbedtls_ssl_context *ssl,
448 const unsigned char *buf,
449 size_t len)
Hanno Becker89dcc882019-04-26 13:56:39 +0100450{
451 size_t peer_cid_len;
452
453 /* CID extension only makes sense in DTLS */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100454 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
455 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
456 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
457 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
458 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Hanno Becker89dcc882019-04-26 13:56:39 +0100459 }
460
461 /*
Hanno Beckerebcc9132019-05-15 10:26:32 +0100462 * Quoting draft-ietf-tls-dtls-connection-id-05
463 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05
Hanno Becker89dcc882019-04-26 13:56:39 +0100464 *
465 * struct {
466 * opaque cid<0..2^8-1>;
467 * } ConnectionId;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100468 */
Hanno Becker89dcc882019-04-26 13:56:39 +0100469
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100470 if (len < 1) {
471 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
472 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
473 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
474 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Hanno Becker89dcc882019-04-26 13:56:39 +0100475 }
476
477 peer_cid_len = *buf++;
478 len--;
479
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100480 if (len != peer_cid_len) {
481 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
482 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
483 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
484 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Hanno Becker89dcc882019-04-26 13:56:39 +0100485 }
486
487 /* Ignore CID if the user has disabled its use. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100488 if (ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED) {
Hanno Becker89dcc882019-04-26 13:56:39 +0100489 /* Leave ssl->handshake->cid_in_use in its default
490 * value of MBEDTLS_SSL_CID_DISABLED. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100491 MBEDTLS_SSL_DEBUG_MSG(3, ("Client sent CID extension, but CID disabled"));
492 return 0;
Hanno Becker89dcc882019-04-26 13:56:39 +0100493 }
494
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100495 if (peer_cid_len > MBEDTLS_SSL_CID_OUT_LEN_MAX) {
496 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
497 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
498 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
499 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Hanno Becker89dcc882019-04-26 13:56:39 +0100500 }
501
Hanno Becker08556bf2019-05-03 12:43:44 +0100502 ssl->handshake->cid_in_use = MBEDTLS_SSL_CID_ENABLED;
Hanno Becker89dcc882019-04-26 13:56:39 +0100503 ssl->handshake->peer_cid_len = (uint8_t) peer_cid_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100504 memcpy(ssl->handshake->peer_cid, buf, peer_cid_len);
Hanno Becker89dcc882019-04-26 13:56:39 +0100505
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100506 MBEDTLS_SSL_DEBUG_MSG(3, ("Use of CID extension negotiated"));
507 MBEDTLS_SSL_DEBUG_BUF(3, "Client CID", buf, peer_cid_len);
Hanno Becker89dcc882019-04-26 13:56:39 +0100508
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100509 return 0;
Hanno Becker89dcc882019-04-26 13:56:39 +0100510}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100511#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker89dcc882019-04-26 13:56:39 +0100512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200514MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100515static int ssl_parse_truncated_hmac_ext(mbedtls_ssl_context *ssl,
516 const unsigned char *buf,
517 size_t len)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200518{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100519 if (len != 0) {
520 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
521 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
522 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
523 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200524 }
525
526 ((void) buf);
527
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100528 if (ssl->conf->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529 ssl->session_negotiate->trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100530 }
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200531
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100532 return 0;
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200533}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200537MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100538static int ssl_parse_encrypt_then_mac_ext(mbedtls_ssl_context *ssl,
539 const unsigned char *buf,
540 size_t len)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100541{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100542 if (len != 0) {
543 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
544 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
545 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
546 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100547 }
548
549 ((void) buf);
550
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100551 if (ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
552 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553 ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100554 }
555
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100556 return 0;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100557}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200561MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100562static int ssl_parse_extended_ms_ext(mbedtls_ssl_context *ssl,
563 const unsigned char *buf,
564 size_t len)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200565{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100566 if (len != 0) {
567 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
568 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
569 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
570 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200571 }
572
573 ((void) buf);
574
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100575 if (ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED &&
576 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577 ssl->handshake->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200578 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200579
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100580 return 0;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200581}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200585MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100586static int ssl_parse_session_ticket_ext(mbedtls_ssl_context *ssl,
587 unsigned char *buf,
588 size_t len)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200589{
Janos Follath865b3eb2019-12-16 11:46:15 +0000590 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200591 mbedtls_ssl_session session;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200592
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100593 mbedtls_ssl_session_init(&session);
Manuel Pégourié-Gonnardbae389b2015-06-24 10:45:58 +0200594
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100595 if (ssl->conf->f_ticket_parse == NULL ||
596 ssl->conf->f_ticket_write == NULL) {
597 return 0;
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200598 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200599
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200600 /* Remember the client asked us to send a new ticket */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200601 ssl->handshake->new_session_ticket = 1;
602
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100603 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket length: %" MBEDTLS_PRINTF_SIZET, len));
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200604
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100605 if (len == 0) {
606 return 0;
607 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100610 if (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE) {
611 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket rejected: renegotiating"));
612 return 0;
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200613 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200615
616 /*
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200617 * Failures are ok: just ignore the ticket and proceed.
618 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100619 if ((ret = ssl->conf->f_ticket_parse(ssl->conf->p_ticket, &session,
620 buf, len)) != 0) {
621 mbedtls_ssl_session_free(&session);
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200622
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100623 if (ret == MBEDTLS_ERR_SSL_INVALID_MAC) {
624 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket is not authentic"));
625 } else if (ret == MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED) {
626 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket is expired"));
627 } else {
628 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_ticket_parse", ret);
629 }
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200630
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100631 return 0;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200632 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200633
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200634 /*
635 * Keep the session ID sent by the client, since we MUST send it back to
636 * inform them we're accepting the ticket (RFC 5077 section 3.4)
637 */
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +0200638 session.id_len = ssl->session_negotiate->id_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100639 memcpy(&session.id, ssl->session_negotiate->id, session.id_len);
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200640
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100641 mbedtls_ssl_session_free(ssl->session_negotiate);
642 memcpy(ssl->session_negotiate, &session, sizeof(mbedtls_ssl_session));
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200643
644 /* Zeroize instead of free as we copied the content */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100645 mbedtls_platform_zeroize(&session, sizeof(mbedtls_ssl_session));
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200646
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100647 MBEDTLS_SSL_DEBUG_MSG(3, ("session successfully restored from ticket"));
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200648
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200649 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200650
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200651 /* Don't send a new ticket after all, this one is OK */
652 ssl->handshake->new_session_ticket = 0;
653
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100654 return 0;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200655}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200659MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100660static int ssl_parse_alpn_ext(mbedtls_ssl_context *ssl,
661 const unsigned char *buf, size_t len)
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200662{
Paul Bakker14b16c62014-05-28 11:33:54 +0200663 size_t list_len, cur_len, ours_len;
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200664 const unsigned char *theirs, *start, *end;
665 const char **ours;
666
667 /* If ALPN not configured, just ignore the extension */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100668 if (ssl->conf->alpn_list == NULL) {
669 return 0;
670 }
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200671
672 /*
673 * opaque ProtocolName<1..2^8-1>;
674 *
675 * struct {
676 * ProtocolName protocol_name_list<2..2^16-1>
677 * } ProtocolNameList;
678 */
679
680 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100681 if (len < 4) {
682 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
683 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
684 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200685 }
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200686
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100687 list_len = (buf[0] << 8) | buf[1];
688 if (list_len != len - 2) {
689 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
690 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
691 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200692 }
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200693
694 /*
Manuel Pégourié-Gonnard239987f2018-01-09 10:43:43 +0100695 * Validate peer's list (lengths)
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200696 */
697 start = buf + 2;
698 end = buf + len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100699 for (theirs = start; theirs != end; theirs += cur_len) {
Manuel Pégourié-Gonnard239987f2018-01-09 10:43:43 +0100700 cur_len = *theirs++;
701
702 /* Current identifier must fit in list */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100703 if (cur_len > (size_t) (end - theirs)) {
704 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
705 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
706 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Manuel Pégourié-Gonnard239987f2018-01-09 10:43:43 +0100707 }
708
709 /* Empty strings MUST NOT be included */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100710 if (cur_len == 0) {
711 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
712 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
713 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Manuel Pégourié-Gonnard239987f2018-01-09 10:43:43 +0100714 }
715 }
716
717 /*
718 * Use our order of preference
719 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100720 for (ours = ssl->conf->alpn_list; *ours != NULL; ours++) {
721 ours_len = strlen(*ours);
722 for (theirs = start; theirs != end; theirs += cur_len) {
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200723 cur_len = *theirs++;
724
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100725 if (cur_len == ours_len &&
726 memcmp(theirs, *ours, cur_len) == 0) {
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200727 ssl->alpn_chosen = *ours;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100728 return 0;
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200729 }
730 }
731 }
732
733 /* If we get there, no match was found */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100734 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
735 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL);
736 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200737}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200738#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200739
Johan Pascalb62bb512015-12-03 21:56:45 +0100740#if defined(MBEDTLS_SSL_DTLS_SRTP)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200741MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100742static int ssl_parse_use_srtp_ext(mbedtls_ssl_context *ssl,
743 const unsigned char *buf,
744 size_t len)
Johan Pascalb62bb512015-12-03 21:56:45 +0100745{
Johan Pascal43f94902020-09-22 12:25:52 +0200746 mbedtls_ssl_srtp_profile client_protection = MBEDTLS_TLS_SRTP_UNSET;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100747 size_t i, j;
Johan Pascalf6417ec2020-09-22 15:15:19 +0200748 size_t profile_length;
749 uint16_t mki_length;
Ron Eldor313d7b52018-12-10 14:56:21 +0200750 /*! 2 bytes for profile length and 1 byte for mki len */
751 const size_t size_of_lengths = 3;
Johan Pascalb62bb512015-12-03 21:56:45 +0100752
753 /* If use_srtp is not configured, just ignore the extension */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100754 if ((ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) ||
755 (ssl->conf->dtls_srtp_profile_list == NULL) ||
756 (ssl->conf->dtls_srtp_profile_list_len == 0)) {
757 return 0;
Johan Pascal85269572020-08-25 10:01:54 +0200758 }
Johan Pascalb62bb512015-12-03 21:56:45 +0100759
760 /* RFC5764 section 4.1.1
761 * uint8 SRTPProtectionProfile[2];
762 *
763 * struct {
764 * SRTPProtectionProfiles SRTPProtectionProfiles;
765 * opaque srtp_mki<0..255>;
766 * } UseSRTPData;
767
768 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
Johan Pascalb62bb512015-12-03 21:56:45 +0100769 */
770
Ron Eldoref72faf2018-07-12 11:54:20 +0300771 /*
772 * Min length is 5: at least one protection profile(2 bytes)
773 * and length(2 bytes) + srtp_mki length(1 byte)
Johan Pascal042d4562020-08-25 12:14:02 +0200774 * Check here that we have at least 2 bytes of protection profiles length
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200775 * and one of srtp_mki length
Ron Eldoref72faf2018-07-12 11:54:20 +0300776 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100777 if (len < size_of_lengths) {
778 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
779 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
780 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Ron Eldor313d7b52018-12-10 14:56:21 +0200781 }
Johan Pascalb62bb512015-12-03 21:56:45 +0100782
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100783 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor591f1622018-01-22 12:30:04 +0200784
Ron Eldoref72faf2018-07-12 11:54:20 +0300785 /* first 2 bytes are protection profile length(in bytes) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100786 profile_length = (buf[0] << 8) | buf[1];
Johan Pascal042d4562020-08-25 12:14:02 +0200787 buf += 2;
Ron Eldor591f1622018-01-22 12:30:04 +0200788
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200789 /* The profile length cannot be bigger than input buffer size - lengths fields */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100790 if (profile_length > len - size_of_lengths ||
791 profile_length % 2 != 0) { /* profiles are 2 bytes long, so the length must be even */
792 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
793 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
794 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Ron Eldor313d7b52018-12-10 14:56:21 +0200795 }
Ron Eldoref72faf2018-07-12 11:54:20 +0300796 /*
797 * parse the extension list values are defined in
798 * http://www.iana.org/assignments/srtp-protection/srtp-protection.xhtml
799 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100800 for (j = 0; j < profile_length; j += 2) {
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200801 uint16_t protection_profile_value = buf[j] << 8 | buf[j + 1];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100802 client_protection = mbedtls_ssl_check_srtp_profile_value(protection_profile_value);
Johan Pascalb62bb512015-12-03 21:56:45 +0100803
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100804 if (client_protection != MBEDTLS_TLS_SRTP_UNSET) {
805 MBEDTLS_SSL_DEBUG_MSG(3, ("found srtp profile: %s",
806 mbedtls_ssl_get_srtp_profile_as_string(
807 client_protection)));
808 } else {
Johan Pascal85269572020-08-25 10:01:54 +0200809 continue;
810 }
Ron Eldor591f1622018-01-22 12:30:04 +0200811 /* check if suggested profile is in our list */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100812 for (i = 0; i < ssl->conf->dtls_srtp_profile_list_len; i++) {
813 if (client_protection == ssl->conf->dtls_srtp_profile_list[i]) {
Ron Eldor3adb9922017-12-21 10:15:08 +0200814 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = ssl->conf->dtls_srtp_profile_list[i];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100815 MBEDTLS_SSL_DEBUG_MSG(3, ("selected srtp profile: %s",
816 mbedtls_ssl_get_srtp_profile_as_string(
817 client_protection)));
Ron Eldor591f1622018-01-22 12:30:04 +0200818 break;
Johan Pascalb62bb512015-12-03 21:56:45 +0100819 }
820 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100821 if (ssl->dtls_srtp_info.chosen_dtls_srtp_profile != MBEDTLS_TLS_SRTP_UNSET) {
Ron Eldor591f1622018-01-22 12:30:04 +0200822 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100823 }
Ron Eldor591f1622018-01-22 12:30:04 +0200824 }
Johan Pascal042d4562020-08-25 12:14:02 +0200825 buf += profile_length; /* buf points to the mki length */
826 mki_length = *buf;
827 buf++;
Ron Eldor591f1622018-01-22 12:30:04 +0200828
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100829 if (mki_length > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH ||
830 mki_length + profile_length + size_of_lengths != len) {
831 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
832 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
833 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Johan Pascal042d4562020-08-25 12:14:02 +0200834 }
835
836 /* Parse the mki only if present and mki is supported locally */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100837 if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED &&
838 mki_length > 0) {
Johan Pascal042d4562020-08-25 12:14:02 +0200839 ssl->dtls_srtp_info.mki_len = mki_length;
840
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100841 memcpy(ssl->dtls_srtp_info.mki_value, buf, mki_length);
Ron Eldorb4655392018-07-05 18:25:39 +0300842
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100843 MBEDTLS_SSL_DEBUG_BUF(3, "using mki", ssl->dtls_srtp_info.mki_value,
844 ssl->dtls_srtp_info.mki_len);
Johan Pascalb62bb512015-12-03 21:56:45 +0100845 }
846
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100847 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +0100848}
849#endif /* MBEDTLS_SSL_DTLS_SRTP */
850
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100851/*
852 * Auxiliary functions for ServerHello parsing and related actions
853 */
854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100856/*
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100857 * Return 0 if the given key uses one of the acceptable curves, -1 otherwise
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100858 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200859#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200860MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100861static int ssl_check_key_curve(mbedtls_pk_context *pk,
862 const mbedtls_ecp_curve_info **curves)
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100863{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200864 const mbedtls_ecp_curve_info **crv = curves;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100865 mbedtls_ecp_group_id grp_id = mbedtls_pk_ec(*pk)->grp.id;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100866
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100867 while (*crv != NULL) {
868 if ((*crv)->grp_id == grp_id) {
869 return 0;
870 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100871 crv++;
872 }
873
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100874 return -1;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100875}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200876#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100877
878/*
879 * Try picking a certificate for this ciphersuite,
880 * return 0 on success and -1 on failure.
881 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200882MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100883static int ssl_pick_cert(mbedtls_ssl_context *ssl,
884 const mbedtls_ssl_ciphersuite_t *ciphersuite_info)
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100885{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200886 mbedtls_ssl_key_cert *cur, *list, *fallback = NULL;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +0100887 mbedtls_pk_type_t pk_alg =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100888 mbedtls_ssl_get_ciphersuite_sig_pk_alg(ciphersuite_info);
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200889 uint32_t flags;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200891#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100892 if (ssl->handshake->sni_key_cert != NULL) {
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100893 list = ssl->handshake->sni_key_cert;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100894 } else
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100895#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100896 list = ssl->conf->key_cert;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100897
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100898 if (pk_alg == MBEDTLS_PK_NONE) {
899 return 0;
Manuel Pégourié-Gonnarde540b492015-07-07 12:44:38 +0200900 }
901
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100902 MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite requires certificate"));
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000903
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100904 if (list == NULL) {
905 MBEDTLS_SSL_DEBUG_MSG(3, ("server has no certificate"));
906 return -1;
907 }
908
909 for (cur = list; cur != NULL; cur = cur->next) {
910 flags = 0;
911 MBEDTLS_SSL_DEBUG_CRT(3, "candidate certificate chain, certificate",
912 cur->cert);
913
914 if (!mbedtls_pk_can_do(&cur->cert->pk, pk_alg)) {
915 MBEDTLS_SSL_DEBUG_MSG(3, ("certificate mismatch: key type"));
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100916 continue;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000917 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100918
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200919 /*
920 * This avoids sending the client a cert it'll reject based on
921 * keyUsage or other extensions.
922 *
923 * It also allows the user to provision different certificates for
924 * different uses based on keyUsage, eg if they want to avoid signing
925 * and decrypting with the same RSA key.
926 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100927 if (mbedtls_ssl_check_cert_usage(cur->cert, ciphersuite_info,
928 MBEDTLS_SSL_IS_SERVER, &flags) != 0) {
929 MBEDTLS_SSL_DEBUG_MSG(3, ("certificate mismatch: "
930 "(extended) key usage extension"));
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200931 continue;
932 }
933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200934#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100935 if (pk_alg == MBEDTLS_PK_ECDSA &&
936 ssl_check_key_curve(&cur->cert->pk, ssl->handshake->curves) != 0) {
937 MBEDTLS_SSL_DEBUG_MSG(3, ("certificate mismatch: elliptic curve"));
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100938 continue;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000939 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100940#endif
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100941
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100942 /*
943 * Try to select a SHA-1 certificate for pre-1.2 clients, but still
944 * present them a SHA-higher cert rather than failing if it's the only
945 * one we got that satisfies the other conditions.
946 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100947 if (ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 &&
948 cur->cert->sig_md != MBEDTLS_MD_SHA1) {
949 if (fallback == NULL) {
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100950 fallback = cur;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100951 }
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000952 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100953 MBEDTLS_SSL_DEBUG_MSG(3, ("certificate not preferred: "
954 "sha-2 with pre-TLS 1.2 client"));
955 continue;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000956 }
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100957 }
958
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100959 /* If we get there, we got a winner */
960 break;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100961 }
962
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100963 if (cur == NULL) {
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000964 cur = fallback;
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100965 }
966
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100967 /* Do not update ssl->handshake->key_cert unless there is a match */
968 if (cur != NULL) {
969 ssl->handshake->key_cert = cur;
970 MBEDTLS_SSL_DEBUG_CRT(3, "selected certificate chain, certificate",
971 ssl->handshake->key_cert->cert);
972 return 0;
973 }
974
975 return -1;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100976}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100978
979/*
980 * Check if a given ciphersuite is suitable for use with our config/keys/etc
981 * Sets ciphersuite_info only if the suite matches.
982 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200983MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100984static int ssl_ciphersuite_match(mbedtls_ssl_context *ssl, int suite_id,
985 const mbedtls_ssl_ciphersuite_t **ciphersuite_info)
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100986{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200987 const mbedtls_ssl_ciphersuite_t *suite_info;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100988
Hanno Becker7e5437a2017-04-28 17:15:26 +0100989#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +0100990 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +0100991 mbedtls_pk_type_t sig_type;
992#endif
993
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100994 suite_info = mbedtls_ssl_ciphersuite_from_id(suite_id);
995 if (suite_info == NULL) {
996 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
997 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100998 }
999
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001000 MBEDTLS_SSL_DEBUG_MSG(3, ("trying ciphersuite: %#04x (%s)",
1001 (unsigned int) suite_id, suite_info->name));
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001002
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001003 if (suite_info->min_minor_ver > ssl->minor_ver ||
1004 suite_info->max_minor_ver < ssl->minor_ver) {
1005 MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite mismatch: version"));
1006 return 0;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001007 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001009#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001010 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
1011 (suite_info->flags & MBEDTLS_CIPHERSUITE_NODTLS)) {
1012 return 0;
1013 }
Manuel Pégourié-Gonnardd6664512014-02-06 13:26:57 +01001014#endif
1015
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02001016#if defined(MBEDTLS_ARC4_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001017 if (ssl->conf->arc4_disabled == MBEDTLS_SSL_ARC4_DISABLED &&
1018 suite_info->cipher == MBEDTLS_CIPHER_ARC4_128) {
1019 MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite mismatch: rc4"));
1020 return 0;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001021 }
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02001022#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001023
Manuel Pégourié-Gonnarde511b4e2015-09-16 14:11:09 +02001024#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001025 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
1026 (ssl->handshake->cli_exts & MBEDTLS_TLS_EXT_ECJPAKE_KKPP_OK) == 0) {
1027 MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite mismatch: ecjpake "
1028 "not configured or ext missing"));
1029 return 0;
Manuel Pégourié-Gonnarde511b4e2015-09-16 14:11:09 +02001030 }
1031#endif
1032
1033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001035 if (mbedtls_ssl_ciphersuite_uses_ec(suite_info) &&
1036 (ssl->handshake->curves == NULL ||
1037 ssl->handshake->curves[0] == NULL)) {
1038 MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite mismatch: "
1039 "no common elliptic curve"));
1040 return 0;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001041 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001042#endif
1043
Gilles Peskineeccd8882020-03-10 12:19:08 +01001044#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001045 /* If the ciphersuite requires a pre-shared key and we don't
1046 * have one, skip it now rather than failing later */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001047 if (mbedtls_ssl_ciphersuite_uses_psk(suite_info) &&
1048 ssl_conf_has_psk_or_cb(ssl->conf) == 0) {
1049 MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite mismatch: no pre-shared key"));
1050 return 0;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001051 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001052#endif
1053
Hanno Becker7e5437a2017-04-28 17:15:26 +01001054#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01001055 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01001056 /* If the ciphersuite requires signing, check whether
1057 * a suitable hash algorithm is present. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001058 if (ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3) {
1059 sig_type = mbedtls_ssl_get_ciphersuite_sig_alg(suite_info);
1060 if (sig_type != MBEDTLS_PK_NONE &&
1061 mbedtls_ssl_sig_hash_set_find(&ssl->handshake->hash_algs,
1062 sig_type) == MBEDTLS_MD_NONE) {
1063 MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite mismatch: no suitable hash algorithm "
1064 "for signature algorithm %u", (unsigned) sig_type));
1065 return 0;
Hanno Becker7e5437a2017-04-28 17:15:26 +01001066 }
1067 }
1068
1069#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Gilles Peskineeccd8882020-03-10 12:19:08 +01001070 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Hanno Becker7e5437a2017-04-28 17:15:26 +01001071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001073 /*
1074 * Final check: if ciphersuite requires us to have a
1075 * certificate/key of a particular type:
1076 * - select the appropriate certificate if we have one, or
1077 * - try the next ciphersuite if we don't
1078 * This must be done last since we modify the key_cert list.
1079 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001080 if (ssl_pick_cert(ssl, suite_info) != 0) {
1081 MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite mismatch: "
1082 "no suitable certificate"));
1083 return 0;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001084 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001085#endif
1086
1087 *ciphersuite_info = suite_info;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001088 return 0;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001089}
1090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001091#if defined(MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02001092MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001093static int ssl_parse_client_hello_v2(mbedtls_ssl_context *ssl)
Paul Bakker78a8c712013-03-06 17:01:52 +01001094{
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001095 int ret, got_common_suite;
Paul Bakker78a8c712013-03-06 17:01:52 +01001096 unsigned int i, j;
1097 size_t n;
1098 unsigned int ciph_len, sess_len, chal_len;
1099 unsigned char *buf, *p;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001100 const int *ciphersuites;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker78a8c712013-03-06 17:01:52 +01001102
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001103 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse client hello v2"));
Paul Bakker78a8c712013-03-06 17:01:52 +01001104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001105#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001106 if (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE) {
1107 MBEDTLS_SSL_DEBUG_MSG(1, ("client hello v2 illegal for renegotiation"));
1108 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1109 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
1110 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Paul Bakker78a8c712013-03-06 17:01:52 +01001111 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001112#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker78a8c712013-03-06 17:01:52 +01001113
1114 buf = ssl->in_hdr;
1115
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001116 MBEDTLS_SSL_DEBUG_BUF(4, "record header", buf, 5);
Paul Bakker78a8c712013-03-06 17:01:52 +01001117
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001118 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello v2, message type: %d",
1119 buf[2]));
1120 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello v2, message len.: %d",
1121 ((buf[0] & 0x7F) << 8) | buf[1]));
1122 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello v2, max. version: [%d:%d]",
1123 buf[3], buf[4]));
Paul Bakker78a8c712013-03-06 17:01:52 +01001124
1125 /*
1126 * SSLv2 Client Hello
1127 *
1128 * Record layer:
1129 * 0 . 1 message length
1130 *
1131 * SSL layer:
1132 * 2 . 2 message type
1133 * 3 . 4 protocol version
1134 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001135 if (buf[2] != MBEDTLS_SSL_HS_CLIENT_HELLO ||
1136 buf[3] != MBEDTLS_SSL_MAJOR_VERSION_3) {
1137 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1138 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Paul Bakker78a8c712013-03-06 17:01:52 +01001139 }
1140
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001141 n = ((buf[0] << 8) | buf[1]) & 0x7FFF;
Paul Bakker78a8c712013-03-06 17:01:52 +01001142
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001143 if (n < 17 || n > 512) {
1144 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1145 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Paul Bakker78a8c712013-03-06 17:01:52 +01001146 }
1147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001148 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001149 ssl->minor_ver = (buf[4] <= ssl->conf->max_minor_ver)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001150 ? buf[4] : ssl->conf->max_minor_ver;
Paul Bakker78a8c712013-03-06 17:01:52 +01001151
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001152 if (ssl->minor_ver < ssl->conf->min_minor_ver) {
1153 MBEDTLS_SSL_DEBUG_MSG(1, ("client only supports ssl smaller than minimum"
1154 " [%d:%d] < [%d:%d]",
1155 ssl->major_ver, ssl->minor_ver,
1156 ssl->conf->min_major_ver, ssl->conf->min_minor_ver));
Paul Bakker78a8c712013-03-06 17:01:52 +01001157
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001158 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1159 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION);
1160 return MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION;
Paul Bakker78a8c712013-03-06 17:01:52 +01001161 }
1162
Paul Bakker2fbefde2013-06-29 16:01:15 +02001163 ssl->handshake->max_major_ver = buf[3];
1164 ssl->handshake->max_minor_ver = buf[4];
Paul Bakker78a8c712013-03-06 17:01:52 +01001165
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001166 if ((ret = mbedtls_ssl_fetch_input(ssl, 2 + n)) != 0) {
1167 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_fetch_input", ret);
1168 return ret;
Paul Bakker78a8c712013-03-06 17:01:52 +01001169 }
1170
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001171 ssl->handshake->update_checksum(ssl, buf + 2, n);
Paul Bakker78a8c712013-03-06 17:01:52 +01001172
1173 buf = ssl->in_msg;
1174 n = ssl->in_left - 5;
1175
1176 /*
1177 * 0 . 1 ciphersuitelist length
1178 * 2 . 3 session id length
1179 * 4 . 5 challenge length
1180 * 6 . .. ciphersuitelist
1181 * .. . .. session id
1182 * .. . .. challenge
1183 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001184 MBEDTLS_SSL_DEBUG_BUF(4, "record contents", buf, n);
Paul Bakker78a8c712013-03-06 17:01:52 +01001185
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001186 ciph_len = (buf[0] << 8) | buf[1];
1187 sess_len = (buf[2] << 8) | buf[3];
1188 chal_len = (buf[4] << 8) | buf[5];
Paul Bakker78a8c712013-03-06 17:01:52 +01001189
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001190 MBEDTLS_SSL_DEBUG_MSG(3, ("ciph_len: %u, sess_len: %u, chal_len: %u",
1191 ciph_len, sess_len, chal_len));
Paul Bakker78a8c712013-03-06 17:01:52 +01001192
1193 /*
1194 * Make sure each parameter length is valid
1195 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001196 if (ciph_len < 3 || (ciph_len % 3) != 0) {
1197 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1198 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Paul Bakker78a8c712013-03-06 17:01:52 +01001199 }
1200
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001201 if (sess_len > 32) {
1202 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1203 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Paul Bakker78a8c712013-03-06 17:01:52 +01001204 }
1205
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001206 if (chal_len < 8 || chal_len > 32) {
1207 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1208 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Paul Bakker78a8c712013-03-06 17:01:52 +01001209 }
1210
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001211 if (n != 6 + ciph_len + sess_len + chal_len) {
1212 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1213 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Paul Bakker78a8c712013-03-06 17:01:52 +01001214 }
1215
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001216 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, ciphersuitelist",
1217 buf + 6, ciph_len);
1218 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, session id",
1219 buf + 6 + ciph_len, sess_len);
1220 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, challenge",
1221 buf + 6 + ciph_len + sess_len, chal_len);
Paul Bakker78a8c712013-03-06 17:01:52 +01001222
1223 p = buf + 6 + ciph_len;
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001224 ssl->session_negotiate->id_len = sess_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001225 memset(ssl->session_negotiate->id, 0,
1226 sizeof(ssl->session_negotiate->id));
1227 memcpy(ssl->session_negotiate->id, p, ssl->session_negotiate->id_len);
Paul Bakker78a8c712013-03-06 17:01:52 +01001228
1229 p += sess_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001230 memset(ssl->handshake->randbytes, 0, 64);
1231 memcpy(ssl->handshake->randbytes + 32 - chal_len, p, chal_len);
Paul Bakker78a8c712013-03-06 17:01:52 +01001232
1233 /*
1234 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1235 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001236 for (i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3) {
1237 if (p[0] == 0 && p[1] == 0 && p[2] == MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO) {
1238 MBEDTLS_SSL_DEBUG_MSG(3, ("received TLS_EMPTY_RENEGOTIATION_INFO "));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001239#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001240 if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
1241 MBEDTLS_SSL_DEBUG_MSG(1, ("received RENEGOTIATION SCSV "
1242 "during renegotiation"));
Paul Bakker78a8c712013-03-06 17:01:52 +01001243
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001244 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1245 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
1246 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Paul Bakker78a8c712013-03-06 17:01:52 +01001247 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001248#endif /* MBEDTLS_SSL_RENEGOTIATION */
1249 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
Paul Bakker78a8c712013-03-06 17:01:52 +01001250 break;
1251 }
1252 }
1253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001254#if defined(MBEDTLS_SSL_FALLBACK_SCSV)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001255 for (i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3) {
1256 if (p[0] == 0 &&
1257 MBEDTLS_GET_UINT16_BE(p, 1) != MBEDTLS_SSL_FALLBACK_SCSV_VALUE) {
1258 MBEDTLS_SSL_DEBUG_MSG(3, ("received FALLBACK_SCSV"));
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001259
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001260 if (ssl->minor_ver < ssl->conf->max_minor_ver) {
1261 MBEDTLS_SSL_DEBUG_MSG(1, ("inapropriate fallback"));
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001262
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001263 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1264 MBEDTLS_SSL_ALERT_MSG_INAPROPRIATE_FALLBACK);
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001265
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001266 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001267 }
1268
1269 break;
1270 }
1271 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001272#endif /* MBEDTLS_SSL_FALLBACK_SCSV */
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001273
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001274 got_common_suite = 0;
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001275 ciphersuites = ssl->conf->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001276 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001277#if defined(MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001278 for (j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3) {
1279 for (i = 0; ciphersuites[i] != 0; i++) {
1280 if (p[0] != 0 ||
1281 MBEDTLS_GET_UINT16_BE(p, 1) != ciphersuites[i]) {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001282 continue;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001283 }
Paul Bakker59c28a22013-06-29 15:33:42 +02001284
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001285 got_common_suite = 1;
1286
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001287 if ((ret = ssl_ciphersuite_match(ssl, ciphersuites[i],
1288 &ciphersuite_info)) != 0) {
1289 return ret;
1290 }
Paul Bakker59c28a22013-06-29 15:33:42 +02001291
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001292 if (ciphersuite_info != NULL) {
Paul Bakker78a8c712013-03-06 17:01:52 +01001293 goto have_ciphersuite_v2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001294 }
Paul Bakker78a8c712013-03-06 17:01:52 +01001295 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001296 }
Gilles Peskine9f540922022-12-08 21:50:34 +01001297#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001298 for (i = 0; ciphersuites[i] != 0; i++) {
1299 for (j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3) {
1300 if (p[0] != 0 ||
1301 MBEDTLS_GET_UINT16_BE(p, 1) != ciphersuites[i]) {
Gilles Peskine9f540922022-12-08 21:50:34 +01001302 continue;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001303 }
Gilles Peskine9f540922022-12-08 21:50:34 +01001304
1305 got_common_suite = 1;
1306
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001307 if ((ret = ssl_ciphersuite_match(ssl, ciphersuites[i],
1308 &ciphersuite_info)) != 0) {
1309 return ret;
1310 }
Gilles Peskine9f540922022-12-08 21:50:34 +01001311
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001312 if (ciphersuite_info != NULL) {
Gilles Peskine9f540922022-12-08 21:50:34 +01001313 goto have_ciphersuite_v2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001314 }
Gilles Peskine9f540922022-12-08 21:50:34 +01001315 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001316 }
Gilles Peskine9f540922022-12-08 21:50:34 +01001317#endif
Paul Bakker78a8c712013-03-06 17:01:52 +01001318
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001319 if (got_common_suite) {
1320 MBEDTLS_SSL_DEBUG_MSG(1, ("got ciphersuites in common, "
1321 "but none of them usable"));
1322 return MBEDTLS_ERR_SSL_NO_USABLE_CIPHERSUITE;
1323 } else {
1324 MBEDTLS_SSL_DEBUG_MSG(1, ("got no ciphersuites in common"));
1325 return MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN;
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001326 }
Paul Bakker78a8c712013-03-06 17:01:52 +01001327
1328have_ciphersuite_v2:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001329 MBEDTLS_SSL_DEBUG_MSG(2, ("selected ciphersuite: %s", ciphersuite_info->name));
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001330
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001331 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Hanno Beckere694c3e2017-12-27 21:34:08 +00001332 ssl->handshake->ciphersuite_info = ciphersuite_info;
Paul Bakker78a8c712013-03-06 17:01:52 +01001333
1334 /*
1335 * SSLv2 Client Hello relevant renegotiation security checks
1336 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001337 if (ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
1338 ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE) {
1339 MBEDTLS_SSL_DEBUG_MSG(1, ("legacy renegotiation, breaking off handshake"));
1340 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1341 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
1342 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Paul Bakker78a8c712013-03-06 17:01:52 +01001343 }
1344
1345 ssl->in_left = 0;
1346 ssl->state++;
1347
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001348 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse client hello v2"));
Paul Bakker78a8c712013-03-06 17:01:52 +01001349
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001350 return 0;
Paul Bakker78a8c712013-03-06 17:01:52 +01001351}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001352#endif /* MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */
Paul Bakker78a8c712013-03-06 17:01:52 +01001353
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001354/* This function doesn't alert on errors that happen early during
1355 ClientHello parsing because they might indicate that the client is
1356 not talking SSL/TLS at all and would not understand our alert. */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02001357MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001358static int ssl_parse_client_hello(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00001359{
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001360 int ret, got_common_suite;
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001361 size_t i, j;
1362 size_t ciph_offset, comp_offset, ext_offset;
1363 size_t msg_len, ciph_len, sess_len, comp_len, ext_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001364#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001365 size_t cookie_offset, cookie_len;
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001366#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001367 unsigned char *buf, *p, *ext;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001368#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001369 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001370#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001371 int handshake_failure = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001372 const int *ciphersuites;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001373 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001374 int major, minor;
Paul Bakker5121ce52009-01-03 21:22:43 +00001375
Hanno Becker7e5437a2017-04-28 17:15:26 +01001376 /* If there is no signature-algorithm extension present,
1377 * we need to fall back to the default values for allowed
1378 * signature-hash pairs. */
1379#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01001380 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01001381 int sig_hash_alg_ext_present = 0;
1382#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Gilles Peskineeccd8882020-03-10 12:19:08 +01001383 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Hanno Becker7e5437a2017-04-28 17:15:26 +01001384
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001385 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse client hello"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001386
David Horstmann74ace592022-10-25 17:26:58 +01001387 int renegotiating = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001388#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001389read_record_header:
1390#endif
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001391 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001392 * If renegotiating, then the input was read with mbedtls_ssl_read_record(),
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001393 * otherwise read it ourselves manually in order to support SSLv2
1394 * ClientHello, which doesn't use the same record layer format.
1395 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001396#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001397 if (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE) {
David Horstmann74ace592022-10-25 17:26:58 +01001398 renegotiating = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001399 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001400#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001401 if (!renegotiating) {
1402 if ((ret = mbedtls_ssl_fetch_input(ssl, 5)) != 0) {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001403 /* No alert on a read error. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001404 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_fetch_input", ret);
1405 return ret;
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +00001406 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001407 }
1408
1409 buf = ssl->in_hdr;
1410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001411#if defined(MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
David Horstmann74ace592022-10-25 17:26:58 +01001412 int is_dtls = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001413#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001414 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
David Horstmann74ace592022-10-25 17:26:58 +01001415 is_dtls = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001416 }
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02001417#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001418 if (!is_dtls) {
1419 if ((buf[0] & 0x80) != 0) {
1420 return ssl_parse_client_hello_v2(ssl);
1421 }
1422 }
Paul Bakker78a8c712013-03-06 17:01:52 +01001423#endif
1424
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001425 MBEDTLS_SSL_DEBUG_BUF(4, "record header", buf, mbedtls_ssl_in_hdr_len(ssl));
Paul Bakkerec636f32012-09-09 19:17:02 +00001426
Paul Bakkerec636f32012-09-09 19:17:02 +00001427 /*
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001428 * SSLv3/TLS Client Hello
Paul Bakkerec636f32012-09-09 19:17:02 +00001429 *
1430 * Record layer:
1431 * 0 . 0 message type
1432 * 1 . 2 protocol version
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001433 * 3 . 11 DTLS: epoch + record sequence number
Paul Bakkerec636f32012-09-09 19:17:02 +00001434 * 3 . 4 message length
1435 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001436 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello v3, message type: %d",
1437 buf[0]));
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001438
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001439 if (buf[0] != MBEDTLS_SSL_MSG_HANDSHAKE) {
1440 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1441 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001442 }
1443
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001444 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello v3, message len.: %d",
1445 (ssl->in_len[0] << 8) | ssl->in_len[1]));
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001446
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001447 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello v3, protocol version: [%d:%d]",
1448 buf[1], buf[2]));
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001449
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001450 mbedtls_ssl_read_version(&major, &minor, ssl->conf->transport, buf + 1);
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001451
1452 /* According to RFC 5246 Appendix E.1, the version here is typically
1453 * "{03,00}, the lowest version number supported by the client, [or] the
1454 * value of ClientHello.client_version", so the only meaningful check here
1455 * is the major version shouldn't be less than 3 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001456 if (major < MBEDTLS_SSL_MAJOR_VERSION_3) {
1457 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1458 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Paul Bakkerec636f32012-09-09 19:17:02 +00001459 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001460
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001461 /* For DTLS if this is the initial handshake, remember the client sequence
1462 * number to use it in our next message (RFC 6347 4.2.1) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001463#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001464 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001465#if defined(MBEDTLS_SSL_RENEGOTIATION)
1466 && ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE
Manuel Pégourié-Gonnard3a173f42015-01-22 13:30:33 +00001467#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001468 ) {
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001469 /* Epoch should be 0 for initial handshakes */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001470 if (ssl->in_ctr[0] != 0 || ssl->in_ctr[1] != 0) {
1471 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1472 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001473 }
1474
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001475 memcpy(ssl->cur_out_ctr + 2, ssl->in_ctr + 2, 6);
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001477#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001478 if (mbedtls_ssl_dtls_replay_check(ssl) != 0) {
1479 MBEDTLS_SSL_DEBUG_MSG(1, ("replayed record, discarding"));
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001480 ssl->next_record_offset = 0;
1481 ssl->in_left = 0;
1482 goto read_record_header;
1483 }
1484
1485 /* No MAC to check yet, so we can update right now */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001486 mbedtls_ssl_dtls_replay_update(ssl);
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001487#endif
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001488 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001489#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001490
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001491 msg_len = (ssl->in_len[0] << 8) | ssl->in_len[1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001493#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001494 if (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001495 /* Set by mbedtls_ssl_read_record() */
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00001496 msg_len = ssl->in_hslen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001497 } else
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001498#endif
Paul Bakkerec636f32012-09-09 19:17:02 +00001499 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001500 if (msg_len > MBEDTLS_SSL_IN_CONTENT_LEN) {
1501 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1502 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001503 }
Paul Bakkerec636f32012-09-09 19:17:02 +00001504
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001505 if ((ret = mbedtls_ssl_fetch_input(ssl,
1506 mbedtls_ssl_in_hdr_len(ssl) + msg_len)) != 0) {
1507 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_fetch_input", ret);
1508 return ret;
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001509 }
Manuel Pégourié-Gonnard30d16eb2014-08-19 17:43:50 +02001510
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001511 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001512#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001513 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1514 ssl->next_record_offset = msg_len + mbedtls_ssl_in_hdr_len(ssl);
1515 } else
Manuel Pégourié-Gonnard30d16eb2014-08-19 17:43:50 +02001516#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001517 ssl->in_left = 0;
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001518 }
Paul Bakkerec636f32012-09-09 19:17:02 +00001519
1520 buf = ssl->in_msg;
Paul Bakkerec636f32012-09-09 19:17:02 +00001521
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001522 MBEDTLS_SSL_DEBUG_BUF(4, "record contents", buf, msg_len);
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001523
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001524 ssl->handshake->update_checksum(ssl, buf, msg_len);
Paul Bakkerec636f32012-09-09 19:17:02 +00001525
1526 /*
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001527 * Handshake layer:
1528 * 0 . 0 handshake type
1529 * 1 . 3 handshake length
Shaun Case0e7791f2021-12-20 21:14:10 -08001530 * 4 . 5 DTLS only: message sequence number
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001531 * 6 . 8 DTLS only: fragment offset
1532 * 9 . 11 DTLS only: fragment length
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001533 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001534 if (msg_len < mbedtls_ssl_hs_hdr_len(ssl)) {
1535 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1536 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001537 }
1538
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001539 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello v3, handshake type: %d", buf[0]));
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001540
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001541 if (buf[0] != MBEDTLS_SSL_HS_CLIENT_HELLO) {
1542 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1543 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001544 }
1545
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001546 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello v3, handshake len.: %d",
1547 (buf[1] << 16) | (buf[2] << 8) | buf[3]));
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001548
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001549 if (buf[1] != 0) {
1550 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message: %u != 0",
1551 (unsigned) buf[1]));
1552 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Andrzej Kurek4353d3d2022-06-08 11:53:59 -04001553 }
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001554 /* We don't support fragmentation of ClientHello (yet?) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001555 if (msg_len != mbedtls_ssl_hs_hdr_len(ssl) + ((buf[2] << 8) | buf[3])) {
1556 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message: %u != %u + %u",
1557 (unsigned) msg_len,
1558 (unsigned) mbedtls_ssl_hs_hdr_len(ssl),
1559 (unsigned) (buf[2] << 8) | buf[3]));
1560 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001561 }
1562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001563#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001564 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001565 /*
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00001566 * Copy the client's handshake message_seq on initial handshakes,
1567 * check sequence number on renego.
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001568 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001569#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001570 if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001571 /* This couldn't be done in ssl_prepare_handshake_record() */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001572 unsigned int cli_msg_seq = (ssl->in_msg[4] << 8) |
1573 ssl->in_msg[5];
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001574
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001575 if (cli_msg_seq != ssl->handshake->in_msg_seq) {
1576 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message_seq: "
1577 "%u (expected %u)", cli_msg_seq,
1578 ssl->handshake->in_msg_seq));
1579 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001580 }
1581
1582 ssl->handshake->in_msg_seq++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001583 } else
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00001584#endif
1585 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001586 unsigned int cli_msg_seq = (ssl->in_msg[4] << 8) |
1587 ssl->in_msg[5];
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00001588 ssl->handshake->out_msg_seq = cli_msg_seq;
1589 ssl->handshake->in_msg_seq = cli_msg_seq + 1;
1590 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001591
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001592 /*
1593 * For now we don't support fragmentation, so make sure
1594 * fragment_offset == 0 and fragment_length == length
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001595 */
Gilles Peskinef333dfa2022-02-15 23:53:36 +01001596 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001597 4, ("fragment_offset=%u fragment_length=%u length=%u",
1598 (unsigned) (ssl->in_msg[6] << 16 | ssl->in_msg[7] << 8 | ssl->in_msg[8]),
1599 (unsigned) (ssl->in_msg[9] << 16 | ssl->in_msg[10] << 8 | ssl->in_msg[11]),
1600 (unsigned) (ssl->in_msg[1] << 16 | ssl->in_msg[2] << 8 | ssl->in_msg[3])));
1601 if (ssl->in_msg[6] != 0 || ssl->in_msg[7] != 0 || ssl->in_msg[8] != 0 ||
1602 memcmp(ssl->in_msg + 1, ssl->in_msg + 9, 3) != 0) {
1603 MBEDTLS_SSL_DEBUG_MSG(1, ("ClientHello fragmentation not supported"));
1604 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001605 }
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001606 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001607#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001608
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001609 buf += mbedtls_ssl_hs_hdr_len(ssl);
1610 msg_len -= mbedtls_ssl_hs_hdr_len(ssl);
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001611
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001612 /*
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001613 * ClientHello layer:
1614 * 0 . 1 protocol version
1615 * 2 . 33 random bytes (starting with 4 bytes of Unix time)
1616 * 34 . 35 session id length (1 byte)
1617 * 35 . 34+x session id
1618 * 35+x . 35+x DTLS only: cookie length (1 byte)
1619 * 36+x . .. DTLS only: cookie
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001620 * .. . .. ciphersuite list length (2 bytes)
1621 * .. . .. ciphersuite list
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001622 * .. . .. compression alg. list length (1 byte)
1623 * .. . .. compression alg. list
1624 * .. . .. extensions length (2 bytes, optional)
1625 * .. . .. extensions (optional)
Paul Bakkerec636f32012-09-09 19:17:02 +00001626 */
Paul Bakkerec636f32012-09-09 19:17:02 +00001627
1628 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01001629 * Minimal length (with everything empty and extensions omitted) is
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001630 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
1631 * read at least up to session id length without worrying.
Paul Bakkerec636f32012-09-09 19:17:02 +00001632 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001633 if (msg_len < 38) {
1634 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1635 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001636 }
1637
1638 /*
1639 * Check and save the protocol version
1640 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001641 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, version", buf, 2);
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001642
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001643 mbedtls_ssl_read_version(&ssl->major_ver, &ssl->minor_ver,
1644 ssl->conf->transport, buf);
Paul Bakkerec636f32012-09-09 19:17:02 +00001645
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001646 ssl->handshake->max_major_ver = ssl->major_ver;
1647 ssl->handshake->max_minor_ver = ssl->minor_ver;
1648
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001649 if (ssl->major_ver < ssl->conf->min_major_ver ||
1650 ssl->minor_ver < ssl->conf->min_minor_ver) {
1651 MBEDTLS_SSL_DEBUG_MSG(1, ("client only supports ssl smaller than minimum"
1652 " [%d:%d] < [%d:%d]",
1653 ssl->major_ver, ssl->minor_ver,
1654 ssl->conf->min_major_ver, ssl->conf->min_minor_ver));
1655 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1656 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION);
1657 return MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001658 }
1659
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001660 if (ssl->major_ver > ssl->conf->max_major_ver) {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001661 ssl->major_ver = ssl->conf->max_major_ver;
1662 ssl->minor_ver = ssl->conf->max_minor_ver;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001663 } else if (ssl->minor_ver > ssl->conf->max_minor_ver) {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001664 ssl->minor_ver = ssl->conf->max_minor_ver;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001665 }
Paul Bakkerec636f32012-09-09 19:17:02 +00001666
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001667 /*
1668 * Save client random (inc. Unix time)
1669 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001670 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, random bytes", buf + 2, 32);
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001671
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001672 memcpy(ssl->handshake->randbytes, buf + 2, 32);
Paul Bakkerec636f32012-09-09 19:17:02 +00001673
1674 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001675 * Check the session ID length and save session ID
Paul Bakkerec636f32012-09-09 19:17:02 +00001676 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001677 sess_len = buf[34];
Paul Bakkerec636f32012-09-09 19:17:02 +00001678
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001679 if (sess_len > sizeof(ssl->session_negotiate->id) ||
1680 sess_len + 34 + 2 > msg_len) { /* 2 for cipherlist length field */
1681 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1682 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1683 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1684 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Paul Bakkerec636f32012-09-09 19:17:02 +00001685 }
1686
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001687 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, session id", buf + 35, sess_len);
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001688
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001689 ssl->session_negotiate->id_len = sess_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001690 memset(ssl->session_negotiate->id, 0,
1691 sizeof(ssl->session_negotiate->id));
1692 memcpy(ssl->session_negotiate->id, buf + 35,
1693 ssl->session_negotiate->id_len);
Paul Bakkerec636f32012-09-09 19:17:02 +00001694
1695 /*
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001696 * Check the cookie length and content
1697 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001698#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001699 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001700 cookie_offset = 35 + sess_len;
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001701 cookie_len = buf[cookie_offset];
1702
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001703 if (cookie_offset + 1 + cookie_len + 2 > msg_len) {
1704 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1705 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1706 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION);
1707 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001708 }
1709
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001710 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, cookie",
1711 buf + cookie_offset + 1, cookie_len);
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001713#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001714 if (ssl->conf->f_cookie_check != NULL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001715#if defined(MBEDTLS_SSL_RENEGOTIATION)
1716 && ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00001717#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001718 ) {
1719 if (ssl->conf->f_cookie_check(ssl->conf->p_cookie,
1720 buf + cookie_offset + 1, cookie_len,
1721 ssl->cli_id, ssl->cli_id_len) != 0) {
1722 MBEDTLS_SSL_DEBUG_MSG(2, ("cookie verification failed"));
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001723 ssl->handshake->verify_cookie_len = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001724 } else {
1725 MBEDTLS_SSL_DEBUG_MSG(2, ("cookie verification passed"));
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001726 ssl->handshake->verify_cookie_len = 0;
1727 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001728 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001729#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001730 {
1731 /* We know we didn't send a cookie, so it should be empty */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001732 if (cookie_len != 0) {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001733 /* This may be an attacker's probe, so don't send an alert */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001734 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1735 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001736 }
1737
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001738 MBEDTLS_SSL_DEBUG_MSG(2, ("cookie verification skipped"));
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001739 }
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001740
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001741 /*
1742 * Check the ciphersuitelist length (will be parsed later)
1743 */
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001744 ciph_offset = cookie_offset + 1 + cookie_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001745 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001746#endif /* MBEDTLS_SSL_PROTO_DTLS */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001747 ciph_offset = 35 + sess_len;
Paul Bakkerec636f32012-09-09 19:17:02 +00001748
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001749 ciph_len = (buf[ciph_offset + 0] << 8)
1750 | (buf[ciph_offset + 1]);
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001751
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001752 if (ciph_len < 2 ||
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001753 ciph_len + 2 + ciph_offset + 1 > msg_len || /* 1 for comp. alg. len */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001754 (ciph_len % 2) != 0) {
1755 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1756 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1757 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1758 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Paul Bakkerec636f32012-09-09 19:17:02 +00001759 }
1760
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001761 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, ciphersuitelist",
1762 buf + ciph_offset + 2, ciph_len);
Paul Bakkerec636f32012-09-09 19:17:02 +00001763
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001764 /*
1765 * Check the compression algorithms length and pick one
1766 */
1767 comp_offset = ciph_offset + 2 + ciph_len;
1768
1769 comp_len = buf[comp_offset];
1770
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001771 if (comp_len < 1 ||
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001772 comp_len > 16 ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001773 comp_len + comp_offset + 1 > msg_len) {
1774 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1775 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1776 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1777 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Paul Bakkerec636f32012-09-09 19:17:02 +00001778 }
1779
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001780 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, compression",
1781 buf + comp_offset + 1, comp_len);
Paul Bakker48916f92012-09-16 19:57:18 +00001782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001783 ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_NULL;
1784#if defined(MBEDTLS_ZLIB_SUPPORT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001785 for (i = 0; i < comp_len; ++i) {
1786 if (buf[comp_offset + 1 + i] == MBEDTLS_SSL_COMPRESS_DEFLATE) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001787 ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_DEFLATE;
Paul Bakkerec636f32012-09-09 19:17:02 +00001788 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00001789 }
1790 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001791#endif
1792
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001793 /* See comments in ssl_write_client_hello() */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001794#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001795 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001796 ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001797 }
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001798#endif
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02001799
Janos Follathc6dab2b2016-05-23 14:27:02 +01001800 /* Do not parse the extensions if the protocol is SSLv3 */
1801#if defined(MBEDTLS_SSL_PROTO_SSL3)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001802 if ((ssl->major_ver != 3) || (ssl->minor_ver != 0)) {
Janos Follathc6dab2b2016-05-23 14:27:02 +01001803#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001804 /*
1805 * Check the extension length
1806 */
1807 ext_offset = comp_offset + 1 + comp_len;
1808 if (msg_len > ext_offset) {
1809 if (msg_len < ext_offset + 2) {
1810 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1811 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1812 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1813 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001814 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001815
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001816 ext_len = (buf[ext_offset + 0] << 8)
1817 | (buf[ext_offset + 1]);
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001818
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001819 if (msg_len != ext_offset + 2 + ext_len) {
1820 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1821 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1822 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1823 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
1824 }
1825 } else {
1826 ext_len = 0;
1827 }
Paul Bakker48916f92012-09-16 19:57:18 +00001828
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001829 ext = buf + ext_offset + 2;
1830 MBEDTLS_SSL_DEBUG_BUF(3, "client hello extensions", ext, ext_len);
1831
1832 while (ext_len != 0) {
1833 unsigned int ext_id;
1834 unsigned int ext_size;
1835 if (ext_len < 4) {
1836 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1837 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1838 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1839 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
1840 }
1841 ext_id = ((ext[0] << 8) | (ext[1]));
1842 ext_size = ((ext[2] << 8) | (ext[3]));
1843
1844 if (ext_size + 4 > ext_len) {
1845 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1846 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1847 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1848 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
1849 }
1850 switch (ext_id) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001851#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Simon Butcher584a5472016-05-23 16:24:52 +01001852 case MBEDTLS_TLS_EXT_SERVERNAME:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001853 MBEDTLS_SSL_DEBUG_MSG(3, ("found ServerName extension"));
1854 if (ssl->conf->f_sni == NULL) {
Simon Butcher584a5472016-05-23 16:24:52 +01001855 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001856 }
Paul Bakker5701cdc2012-09-27 21:49:42 +00001857
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001858 ret = ssl_parse_servername_ext(ssl, ext + 4, ext_size);
1859 if (ret != 0) {
1860 return ret;
1861 }
Simon Butcher584a5472016-05-23 16:24:52 +01001862 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001863#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00001864
Simon Butcher584a5472016-05-23 16:24:52 +01001865 case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001866 MBEDTLS_SSL_DEBUG_MSG(3, ("found renegotiation extension"));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001867#if defined(MBEDTLS_SSL_RENEGOTIATION)
Simon Butcher584a5472016-05-23 16:24:52 +01001868 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001869#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001870
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001871 ret = ssl_parse_renegotiation_info(ssl, ext + 4, ext_size);
1872 if (ret != 0) {
1873 return ret;
1874 }
Simon Butcher584a5472016-05-23 16:24:52 +01001875 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001877#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001878 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Simon Butcher584a5472016-05-23 16:24:52 +01001879 case MBEDTLS_TLS_EXT_SIG_ALG:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001880 MBEDTLS_SSL_DEBUG_MSG(3, ("found signature_algorithms extension"));
Ron Eldor73a38172017-10-03 15:58:26 +03001881
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001882 ret = ssl_parse_signature_algorithms_ext(ssl, ext + 4, ext_size);
1883 if (ret != 0) {
1884 return ret;
1885 }
Hanno Becker7e5437a2017-04-28 17:15:26 +01001886
1887 sig_hash_alg_ext_present = 1;
Simon Butcher584a5472016-05-23 16:24:52 +01001888 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001889#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Gilles Peskineeccd8882020-03-10 12:19:08 +01001890 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Paul Bakker48916f92012-09-16 19:57:18 +00001891
Robert Cragie136884c2015-10-02 13:34:31 +01001892#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001893 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Simon Butcher584a5472016-05-23 16:24:52 +01001894 case MBEDTLS_TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001895 MBEDTLS_SSL_DEBUG_MSG(3, ("found supported elliptic curves extension"));
Paul Bakker41c83d32013-03-20 14:39:14 +01001896
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001897 ret = ssl_parse_supported_elliptic_curves(ssl, ext + 4, ext_size);
1898 if (ret != 0) {
1899 return ret;
1900 }
Simon Butcher584a5472016-05-23 16:24:52 +01001901 break;
Paul Bakker41c83d32013-03-20 14:39:14 +01001902
Simon Butcher584a5472016-05-23 16:24:52 +01001903 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001904 MBEDTLS_SSL_DEBUG_MSG(3, ("found supported point formats extension"));
Simon Butcher584a5472016-05-23 16:24:52 +01001905 ssl->handshake->cli_exts |= MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT;
Paul Bakker41c83d32013-03-20 14:39:14 +01001906
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001907 ret = ssl_parse_supported_point_formats(ssl, ext + 4, ext_size);
1908 if (ret != 0) {
1909 return ret;
1910 }
Simon Butcher584a5472016-05-23 16:24:52 +01001911 break;
Robert Cragieae8535d2015-10-06 17:11:18 +01001912#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
1913 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01001914
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001915#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Simon Butcher584a5472016-05-23 16:24:52 +01001916 case MBEDTLS_TLS_EXT_ECJPAKE_KKPP:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001917 MBEDTLS_SSL_DEBUG_MSG(3, ("found ecjpake kkpp extension"));
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001918
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001919 ret = ssl_parse_ecjpake_kkpp(ssl, ext + 4, ext_size);
1920 if (ret != 0) {
1921 return ret;
1922 }
Simon Butcher584a5472016-05-23 16:24:52 +01001923 break;
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001924#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
1925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001926#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Simon Butcher584a5472016-05-23 16:24:52 +01001927 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001928 MBEDTLS_SSL_DEBUG_MSG(3, ("found max fragment length extension"));
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001929
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001930 ret = ssl_parse_max_fragment_length_ext(ssl, ext + 4, ext_size);
1931 if (ret != 0) {
1932 return ret;
1933 }
Simon Butcher584a5472016-05-23 16:24:52 +01001934 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001935#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001937#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Simon Butcher584a5472016-05-23 16:24:52 +01001938 case MBEDTLS_TLS_EXT_TRUNCATED_HMAC:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001939 MBEDTLS_SSL_DEBUG_MSG(3, ("found truncated hmac extension"));
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001940
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001941 ret = ssl_parse_truncated_hmac_ext(ssl, ext + 4, ext_size);
1942 if (ret != 0) {
1943 return ret;
1944 }
Simon Butcher584a5472016-05-23 16:24:52 +01001945 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001946#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001947
Hanno Beckera0e20d02019-05-15 14:03:01 +01001948#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89dcc882019-04-26 13:56:39 +01001949 case MBEDTLS_TLS_EXT_CID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001950 MBEDTLS_SSL_DEBUG_MSG(3, ("found CID extension"));
Hanno Becker89dcc882019-04-26 13:56:39 +01001951
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001952 ret = ssl_parse_cid_ext(ssl, ext + 4, ext_size);
1953 if (ret != 0) {
1954 return ret;
1955 }
Hanno Becker89dcc882019-04-26 13:56:39 +01001956 break;
1957#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
1958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001959#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Simon Butcher584a5472016-05-23 16:24:52 +01001960 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001961 MBEDTLS_SSL_DEBUG_MSG(3, ("found encrypt then mac extension"));
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001962
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001963 ret = ssl_parse_encrypt_then_mac_ext(ssl, ext + 4, ext_size);
1964 if (ret != 0) {
1965 return ret;
1966 }
Simon Butcher584a5472016-05-23 16:24:52 +01001967 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001968#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001970#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Simon Butcher584a5472016-05-23 16:24:52 +01001971 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001972 MBEDTLS_SSL_DEBUG_MSG(3, ("found extended master secret extension"));
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001973
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001974 ret = ssl_parse_extended_ms_ext(ssl, ext + 4, ext_size);
1975 if (ret != 0) {
1976 return ret;
1977 }
Simon Butcher584a5472016-05-23 16:24:52 +01001978 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001979#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001981#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Simon Butcher584a5472016-05-23 16:24:52 +01001982 case MBEDTLS_TLS_EXT_SESSION_TICKET:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001983 MBEDTLS_SSL_DEBUG_MSG(3, ("found session ticket extension"));
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001984
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001985 ret = ssl_parse_session_ticket_ext(ssl, ext + 4, ext_size);
1986 if (ret != 0) {
1987 return ret;
1988 }
Simon Butcher584a5472016-05-23 16:24:52 +01001989 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001990#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001992#if defined(MBEDTLS_SSL_ALPN)
Simon Butcher584a5472016-05-23 16:24:52 +01001993 case MBEDTLS_TLS_EXT_ALPN:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001994 MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001995
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001996 ret = ssl_parse_alpn_ext(ssl, ext + 4, ext_size);
1997 if (ret != 0) {
1998 return ret;
1999 }
Simon Butcher584a5472016-05-23 16:24:52 +01002000 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002001#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002002
Johan Pascalb62bb512015-12-03 21:56:45 +01002003#if defined(MBEDTLS_SSL_DTLS_SRTP)
2004 case MBEDTLS_TLS_EXT_USE_SRTP:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002005 MBEDTLS_SSL_DEBUG_MSG(3, ("found use_srtp extension"));
Johan Pascald576fdb2020-09-22 10:39:53 +02002006
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002007 ret = ssl_parse_use_srtp_ext(ssl, ext + 4, ext_size);
2008 if (ret != 0) {
2009 return ret;
2010 }
Johan Pascalb62bb512015-12-03 21:56:45 +01002011 break;
2012#endif /* MBEDTLS_SSL_DTLS_SRTP */
2013
Simon Butcher584a5472016-05-23 16:24:52 +01002014 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002015 MBEDTLS_SSL_DEBUG_MSG(3, ("unknown extension found: %u (ignoring)",
2016 ext_id));
Paul Bakker48916f92012-09-16 19:57:18 +00002017 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002018
2019 ext_len -= 4 + ext_size;
2020 ext += 4 + ext_size;
Janos Follathc6dab2b2016-05-23 14:27:02 +01002021 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002022#if defined(MBEDTLS_SSL_PROTO_SSL3)
2023}
Janos Follathc6dab2b2016-05-23 14:27:02 +01002024#endif
2025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002026#if defined(MBEDTLS_SSL_FALLBACK_SCSV)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002027 for (i = 0, p = buf + ciph_offset + 2; i < ciph_len; i += 2, p += 2) {
2028 if (MBEDTLS_GET_UINT16_BE(p, 0) == MBEDTLS_SSL_FALLBACK_SCSV_VALUE) {
2029 MBEDTLS_SSL_DEBUG_MSG(2, ("received FALLBACK_SCSV"));
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002030
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002031 if (ssl->minor_ver < ssl->conf->max_minor_ver) {
2032 MBEDTLS_SSL_DEBUG_MSG(1, ("inapropriate fallback"));
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002033
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002034 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2035 MBEDTLS_SSL_ALERT_MSG_INAPROPRIATE_FALLBACK);
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002036
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002037 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002038 }
2039
2040 break;
2041 }
2042 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002043#endif /* MBEDTLS_SSL_FALLBACK_SCSV */
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002044
Hanno Becker7e5437a2017-04-28 17:15:26 +01002045#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01002046 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01002047
2048 /*
2049 * Try to fall back to default hash SHA1 if the client
2050 * hasn't provided any preferred signature-hash combinations.
2051 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002052 if (sig_hash_alg_ext_present == 0) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01002053 mbedtls_md_type_t md_default = MBEDTLS_MD_SHA1;
2054
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002055 if (mbedtls_ssl_check_sig_hash(ssl, md_default) != 0) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01002056 md_default = MBEDTLS_MD_NONE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002057 }
Hanno Becker7e5437a2017-04-28 17:15:26 +01002058
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002059 mbedtls_ssl_sig_hash_set_const_hash(&ssl->handshake->hash_algs, md_default);
Hanno Becker7e5437a2017-04-28 17:15:26 +01002060 }
2061
2062#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Gilles Peskineeccd8882020-03-10 12:19:08 +01002063 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Hanno Becker7e5437a2017-04-28 17:15:26 +01002064
Paul Bakker48916f92012-09-16 19:57:18 +00002065 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002066 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
2067 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002068 for (i = 0, p = buf + ciph_offset + 2; i < ciph_len; i += 2, p += 2) {
2069 if (p[0] == 0 && p[1] == MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO) {
2070 MBEDTLS_SSL_DEBUG_MSG(3, ("received TLS_EMPTY_RENEGOTIATION_INFO "));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002071#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002072 if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
2073 MBEDTLS_SSL_DEBUG_MSG(1, ("received RENEGOTIATION SCSV "
2074 "during renegotiation"));
2075 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2076 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
2077 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002078 }
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00002079#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002080 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002081 break;
2082 }
2083 }
2084
2085 /*
Paul Bakker48916f92012-09-16 19:57:18 +00002086 * Renegotiation security checks
2087 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002088 if (ssl->secure_renegotiation != MBEDTLS_SSL_SECURE_RENEGOTIATION &&
2089 ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE) {
2090 MBEDTLS_SSL_DEBUG_MSG(1, ("legacy renegotiation, breaking off handshake"));
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002091 handshake_failure = 1;
2092 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002093#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002094 else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002095 ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002096 renegotiation_info_seen == 0) {
2097 MBEDTLS_SSL_DEBUG_MSG(1, ("renegotiation_info extension missing (secure)"));
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002098 handshake_failure = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002099 } else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
2100 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
2101 ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION) {
2102 MBEDTLS_SSL_DEBUG_MSG(1, ("legacy renegotiation not allowed"));
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002103 handshake_failure = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002104 } else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
2105 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
2106 renegotiation_info_seen == 1) {
2107 MBEDTLS_SSL_DEBUG_MSG(1, ("renegotiation_info extension present (legacy)"));
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002108 handshake_failure = 1;
2109 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002110#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002111
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002112 if (handshake_failure == 1) {
2113 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2114 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
2115 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Paul Bakker48916f92012-09-16 19:57:18 +00002116 }
Paul Bakker380da532012-04-18 16:10:25 +00002117
Paul Bakker41c83d32013-03-20 14:39:14 +01002118 /*
2119 * Search for a matching ciphersuite
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02002120 * (At the end because we need information from the EC-based extensions
2121 * and certificate from the SNI callback triggered by the SNI extension.)
Paul Bakker41c83d32013-03-20 14:39:14 +01002122 */
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002123 got_common_suite = 0;
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002124 ciphersuites = ssl->conf->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard59b81d72013-11-30 17:46:04 +01002125 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002126#if defined(MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002127 for (j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2) {
2128 for (i = 0; ciphersuites[i] != 0; i++) {
2129 if (MBEDTLS_GET_UINT16_BE(p, 0) != ciphersuites[i]) {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01002130 continue;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002131 }
Paul Bakker41c83d32013-03-20 14:39:14 +01002132
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002133 got_common_suite = 1;
2134
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002135 if ((ret = ssl_ciphersuite_match(ssl, ciphersuites[i],
2136 &ciphersuite_info)) != 0) {
2137 return ret;
2138 }
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01002139
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002140 if (ciphersuite_info != NULL) {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01002141 goto have_ciphersuite;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002142 }
Paul Bakker41c83d32013-03-20 14:39:14 +01002143 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002144 }
Gilles Peskine9f540922022-12-08 21:50:34 +01002145#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002146 for (i = 0; ciphersuites[i] != 0; i++) {
2147 for (j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2) {
2148 if (MBEDTLS_GET_UINT16_BE(p, 0) != ciphersuites[i]) {
Gilles Peskine9f540922022-12-08 21:50:34 +01002149 continue;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002150 }
Gilles Peskine9f540922022-12-08 21:50:34 +01002151
2152 got_common_suite = 1;
2153
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002154 if ((ret = ssl_ciphersuite_match(ssl, ciphersuites[i],
2155 &ciphersuite_info)) != 0) {
2156 return ret;
2157 }
Gilles Peskine9f540922022-12-08 21:50:34 +01002158
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002159 if (ciphersuite_info != NULL) {
Gilles Peskine9f540922022-12-08 21:50:34 +01002160 goto have_ciphersuite;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002161 }
Gilles Peskine9f540922022-12-08 21:50:34 +01002162 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002163 }
Gilles Peskine9f540922022-12-08 21:50:34 +01002164#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002165
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002166 if (got_common_suite) {
2167 MBEDTLS_SSL_DEBUG_MSG(1, ("got ciphersuites in common, "
2168 "but none of them usable"));
2169 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2170 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
2171 return MBEDTLS_ERR_SSL_NO_USABLE_CIPHERSUITE;
2172 } else {
2173 MBEDTLS_SSL_DEBUG_MSG(1, ("got no ciphersuites in common"));
2174 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2175 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
2176 return MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN;
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002177 }
Paul Bakker41c83d32013-03-20 14:39:14 +01002178
2179have_ciphersuite:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002180 MBEDTLS_SSL_DEBUG_MSG(2, ("selected ciphersuite: %s", ciphersuite_info->name));
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00002181
Paul Bakker8f4ddae2013-04-15 15:09:54 +02002182 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Hanno Beckere694c3e2017-12-27 21:34:08 +00002183 ssl->handshake->ciphersuite_info = ciphersuite_info;
Paul Bakker41c83d32013-03-20 14:39:14 +01002184
Paul Bakker5121ce52009-01-03 21:22:43 +00002185 ssl->state++;
2186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002187#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002188 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
2189 mbedtls_ssl_recv_flight_completed(ssl);
2190 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002191#endif
2192
Hanno Becker7e5437a2017-04-28 17:15:26 +01002193 /* Debugging-only output for testsuite */
2194#if defined(MBEDTLS_DEBUG_C) && \
2195 defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01002196 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002197 if (ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3) {
2198 mbedtls_pk_type_t sig_alg = mbedtls_ssl_get_ciphersuite_sig_alg(ciphersuite_info);
2199 if (sig_alg != MBEDTLS_PK_NONE) {
2200 mbedtls_md_type_t md_alg = mbedtls_ssl_sig_hash_set_find(&ssl->handshake->hash_algs,
2201 sig_alg);
2202 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello v3, signature_algorithm ext: %d",
2203 mbedtls_ssl_hash_from_md_alg(md_alg)));
2204 } else {
2205 MBEDTLS_SSL_DEBUG_MSG(3, ("no hash algorithm for signature algorithm "
2206 "%u - should not happen", (unsigned) sig_alg));
Hanno Becker7e5437a2017-04-28 17:15:26 +01002207 }
2208 }
2209#endif
2210
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002211 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse client hello"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002212
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002213 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002214}
2215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002216#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002217static void ssl_write_truncated_hmac_ext(mbedtls_ssl_context *ssl,
2218 unsigned char *buf,
2219 size_t *olen)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002220{
2221 unsigned char *p = buf;
2222
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002223 if (ssl->session_negotiate->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_DISABLED) {
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002224 *olen = 0;
2225 return;
2226 }
2227
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002228 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding truncated hmac extension"));
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002229
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002230 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_TRUNCATED_HMAC, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01002231 p += 2;
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002232
2233 *p++ = 0x00;
2234 *p++ = 0x00;
2235
2236 *olen = 4;
2237}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002238#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002239
Hanno Beckera0e20d02019-05-15 14:03:01 +01002240#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002241static void ssl_write_cid_ext(mbedtls_ssl_context *ssl,
2242 unsigned char *buf,
2243 size_t *olen)
Hanno Becker51de2d32019-04-26 15:46:55 +01002244{
2245 unsigned char *p = buf;
2246 size_t ext_len;
2247 const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
2248
2249 *olen = 0;
2250
2251 /* Skip writing the extension if we don't want to use it or if
2252 * the client hasn't offered it. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002253 if (ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_DISABLED) {
Hanno Becker51de2d32019-04-26 15:46:55 +01002254 return;
2255 }
2256
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002257 /* ssl->own_cid_len is at most MBEDTLS_SSL_CID_IN_LEN_MAX
2258 * which is at most 255, so the increment cannot overflow. */
2259 if (end < p || (size_t) (end - p) < (unsigned) (ssl->own_cid_len + 5)) {
2260 MBEDTLS_SSL_DEBUG_MSG(1, ("buffer too small"));
2261 return;
2262 }
2263
2264 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding CID extension"));
Hanno Becker51de2d32019-04-26 15:46:55 +01002265
2266 /*
Hanno Beckerebcc9132019-05-15 10:26:32 +01002267 * Quoting draft-ietf-tls-dtls-connection-id-05
2268 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05
Hanno Becker51de2d32019-04-26 15:46:55 +01002269 *
2270 * struct {
2271 * opaque cid<0..2^8-1>;
2272 * } ConnectionId;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002273 */
2274 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_CID, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01002275 p += 2;
Hanno Becker51de2d32019-04-26 15:46:55 +01002276 ext_len = (size_t) ssl->own_cid_len + 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002277 MBEDTLS_PUT_UINT16_BE(ext_len, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01002278 p += 2;
Hanno Becker51de2d32019-04-26 15:46:55 +01002279
2280 *p++ = (uint8_t) ssl->own_cid_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002281 memcpy(p, ssl->own_cid, ssl->own_cid_len);
Hanno Becker51de2d32019-04-26 15:46:55 +01002282
2283 *olen = ssl->own_cid_len + 5;
2284}
Hanno Beckera0e20d02019-05-15 14:03:01 +01002285#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker51de2d32019-04-26 15:46:55 +01002286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002287#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002288static void ssl_write_encrypt_then_mac_ext(mbedtls_ssl_context *ssl,
2289 unsigned char *buf,
2290 size_t *olen)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002291{
2292 unsigned char *p = buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002293 const mbedtls_ssl_ciphersuite_t *suite = NULL;
2294 const mbedtls_cipher_info_t *cipher = NULL;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002295
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002296 if (ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0) {
Ronald Cronf1ed5952022-03-24 14:15:28 +01002297 ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_DISABLED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002298 }
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002299
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01002300 /*
2301 * RFC 7366: "If a server receives an encrypt-then-MAC request extension
2302 * from a client and then selects a stream or Authenticated Encryption
2303 * with Associated Data (AEAD) ciphersuite, it MUST NOT send an
2304 * encrypt-then-MAC response extension back to the client."
2305 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002306 if ((suite = mbedtls_ssl_ciphersuite_from_id(
2307 ssl->session_negotiate->ciphersuite)) == NULL ||
2308 (cipher = mbedtls_cipher_info_from_type(suite->cipher)) == NULL ||
2309 cipher->mode != MBEDTLS_MODE_CBC) {
Ronald Cronf1ed5952022-03-24 14:15:28 +01002310 ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_DISABLED;
2311 }
2312
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002313 if (ssl->session_negotiate->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED) {
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01002314 *olen = 0;
2315 return;
2316 }
2317
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002318 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding encrypt then mac extension"));
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002319
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002320 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01002321 p += 2;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002322
2323 *p++ = 0x00;
2324 *p++ = 0x00;
2325
2326 *olen = 4;
2327}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002328#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002330#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002331static void ssl_write_extended_ms_ext(mbedtls_ssl_context *ssl,
2332 unsigned char *buf,
2333 size_t *olen)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002334{
2335 unsigned char *p = buf;
2336
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002337 if (ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED ||
2338 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0) {
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002339 *olen = 0;
2340 return;
2341 }
2342
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002343 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding extended master secret "
2344 "extension"));
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002345
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002346 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01002347 p += 2;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002348
2349 *p++ = 0x00;
2350 *p++ = 0x00;
2351
2352 *olen = 4;
2353}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002354#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002356#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002357static void ssl_write_session_ticket_ext(mbedtls_ssl_context *ssl,
2358 unsigned char *buf,
2359 size_t *olen)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002360{
2361 unsigned char *p = buf;
2362
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002363 if (ssl->handshake->new_session_ticket == 0) {
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002364 *olen = 0;
2365 return;
2366 }
2367
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002368 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding session ticket extension"));
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002369
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002370 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SESSION_TICKET, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01002371 p += 2;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002372
2373 *p++ = 0x00;
2374 *p++ = 0x00;
2375
2376 *olen = 4;
2377}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002378#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002379
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002380static void ssl_write_renegotiation_ext(mbedtls_ssl_context *ssl,
2381 unsigned char *buf,
2382 size_t *olen)
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002383{
2384 unsigned char *p = buf;
2385
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002386 if (ssl->secure_renegotiation != MBEDTLS_SSL_SECURE_RENEGOTIATION) {
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002387 *olen = 0;
2388 return;
2389 }
2390
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002391 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, secure renegotiation extension"));
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002392
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002393 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_RENEGOTIATION_INFO, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01002394 p += 2;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002396#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002397 if (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE) {
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002398 *p++ = 0x00;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002399 *p++ = (ssl->verify_data_len * 2 + 1) & 0xFF;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002400 *p++ = ssl->verify_data_len * 2 & 0xFF;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002401
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002402 memcpy(p, ssl->peer_verify_data, ssl->verify_data_len);
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002403 p += ssl->verify_data_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002404 memcpy(p, ssl->own_verify_data, ssl->verify_data_len);
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002405 p += ssl->verify_data_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002406 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002407#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002408 {
2409 *p++ = 0x00;
2410 *p++ = 0x01;
2411 *p++ = 0x00;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002412 }
Manuel Pégourié-Gonnard19389752015-06-23 13:46:44 +02002413
2414 *olen = p - buf;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002415}
2416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002417#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002418static void ssl_write_max_fragment_length_ext(mbedtls_ssl_context *ssl,
2419 unsigned char *buf,
2420 size_t *olen)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002421{
2422 unsigned char *p = buf;
2423
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002424 if (ssl->session_negotiate->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE) {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002425 *olen = 0;
2426 return;
2427 }
2428
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002429 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, max_fragment_length extension"));
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002430
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002431 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01002432 p += 2;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002433
2434 *p++ = 0x00;
2435 *p++ = 1;
2436
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02002437 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002438
2439 *olen = 5;
2440}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002441#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002442
Manuel Pégourié-Gonnardf4721792015-09-15 10:53:51 +02002443#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02002444 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002445static void ssl_write_supported_point_formats_ext(mbedtls_ssl_context *ssl,
2446 unsigned char *buf,
2447 size_t *olen)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002448{
2449 unsigned char *p = buf;
2450 ((void) ssl);
2451
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002452 if ((ssl->handshake->cli_exts &
2453 MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT) == 0) {
Paul Bakker677377f2013-10-28 12:54:26 +01002454 *olen = 0;
2455 return;
2456 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002457
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002458 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, supported_point_formats extension"));
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002459
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002460 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01002461 p += 2;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002462
2463 *p++ = 0x00;
2464 *p++ = 2;
2465
2466 *p++ = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002467 *p++ = MBEDTLS_ECP_PF_UNCOMPRESSED;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002468
2469 *olen = 6;
2470}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02002471#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002472
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002473#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002474static void ssl_write_ecjpake_kkpp_ext(mbedtls_ssl_context *ssl,
2475 unsigned char *buf,
2476 size_t *olen)
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002477{
Janos Follath865b3eb2019-12-16 11:46:15 +00002478 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002479 unsigned char *p = buf;
Angus Grattond8213d02016-05-25 20:56:48 +10002480 const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002481 size_t kkpp_len;
2482
2483 *olen = 0;
2484
2485 /* Skip costly computation if not needed */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002486 if (ssl->handshake->ciphersuite_info->key_exchange !=
2487 MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002488 return;
2489 }
2490
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002491 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, ecjpake kkpp extension"));
2492
2493 if (end - p < 4) {
2494 MBEDTLS_SSL_DEBUG_MSG(1, ("buffer too small"));
2495 return;
2496 }
2497
2498 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ECJPAKE_KKPP, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01002499 p += 2;
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002500
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002501 ret = mbedtls_ecjpake_write_round_one(&ssl->handshake->ecjpake_ctx,
2502 p + 2, end - p - 2, &kkpp_len,
2503 ssl->conf->f_rng, ssl->conf->p_rng);
2504 if (ret != 0) {
2505 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_write_round_one", ret);
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002506 return;
2507 }
2508
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002509 MBEDTLS_PUT_UINT16_BE(kkpp_len, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01002510 p += 2;
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002511
2512 *olen = kkpp_len + 4;
2513}
2514#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2515
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002516#if defined(MBEDTLS_SSL_ALPN)
2517static void ssl_write_alpn_ext(mbedtls_ssl_context *ssl,
2518 unsigned char *buf, size_t *olen)
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002519{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002520 if (ssl->alpn_chosen == NULL) {
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002521 *olen = 0;
2522 return;
2523 }
2524
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002525 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding alpn extension"));
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002526
2527 /*
2528 * 0 . 1 ext identifier
2529 * 2 . 3 ext length
2530 * 4 . 5 protocol list length
2531 * 6 . 6 protocol name length
2532 * 7 . 7+n protocol name
2533 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002534 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ALPN, buf, 0);
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002535
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002536 *olen = 7 + strlen(ssl->alpn_chosen);
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002537
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002538 MBEDTLS_PUT_UINT16_BE(*olen - 4, buf, 2);
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002539
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002540 MBEDTLS_PUT_UINT16_BE(*olen - 6, buf, 4);
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002541
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002542 buf[6] = MBEDTLS_BYTE_0(*olen - 7);
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002543
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002544 memcpy(buf + 7, ssl->alpn_chosen, *olen - 7);
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002545}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002546#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002547
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002548#if defined(MBEDTLS_SSL_DTLS_SRTP) && defined(MBEDTLS_SSL_PROTO_DTLS)
2549static void ssl_write_use_srtp_ext(mbedtls_ssl_context *ssl,
2550 unsigned char *buf,
2551 size_t *olen)
Johan Pascalb62bb512015-12-03 21:56:45 +01002552{
Ron Eldor75870ec2018-12-06 17:31:55 +02002553 size_t mki_len = 0, ext_len = 0;
Ron Eldor089c9fe2018-12-06 17:12:49 +02002554 uint16_t profile_value = 0;
Johan Pascal8f70fba2020-09-02 10:32:06 +02002555 const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
2556
2557 *olen = 0;
Ron Eldor591f1622018-01-22 12:30:04 +02002558
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002559 if ((ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) ||
2560 (ssl->dtls_srtp_info.chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET)) {
Johan Pascalb62bb512015-12-03 21:56:45 +01002561 return;
2562 }
2563
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002564 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding use_srtp extension"));
Johan Pascalb62bb512015-12-03 21:56:45 +01002565
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002566 if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED) {
Ron Eldor591f1622018-01-22 12:30:04 +02002567 mki_len = ssl->dtls_srtp_info.mki_len;
2568 }
2569
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002570 /* The extension total size is 9 bytes :
2571 * - 2 bytes for the extension tag
2572 * - 2 bytes for the total size
2573 * - 2 bytes for the protection profile length
2574 * - 2 bytes for the protection profile
2575 * - 1 byte for the mki length
2576 * + the actual mki length
2577 * Check we have enough room in the output buffer */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002578 if ((size_t) (end - buf) < mki_len + 9) {
2579 MBEDTLS_SSL_DEBUG_MSG(1, ("buffer too small"));
Johan Pascal8f70fba2020-09-02 10:32:06 +02002580 return;
2581 }
2582
Johan Pascalb62bb512015-12-03 21:56:45 +01002583 /* extension */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002584 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_USE_SRTP, buf, 0);
Ron Eldoref72faf2018-07-12 11:54:20 +03002585 /*
2586 * total length 5 and mki value: only one profile(2 bytes)
2587 * and length(2 bytes) and srtp_mki )
2588 */
Ron Eldor591f1622018-01-22 12:30:04 +02002589 ext_len = 5 + mki_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002590 MBEDTLS_PUT_UINT16_BE(ext_len, buf, 2);
Johan Pascalb62bb512015-12-03 21:56:45 +01002591
2592 /* protection profile length: 2 */
2593 buf[4] = 0x00;
2594 buf[5] = 0x02;
Johan Pascal43f94902020-09-22 12:25:52 +02002595 profile_value = mbedtls_ssl_check_srtp_profile_value(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002596 ssl->dtls_srtp_info.chosen_dtls_srtp_profile);
2597 if (profile_value != MBEDTLS_TLS_SRTP_UNSET) {
2598 MBEDTLS_PUT_UINT16_BE(profile_value, buf, 6);
2599 } else {
2600 MBEDTLS_SSL_DEBUG_MSG(1, ("use_srtp extension invalid profile"));
Ron Eldor089c9fe2018-12-06 17:12:49 +02002601 return;
Johan Pascalb62bb512015-12-03 21:56:45 +01002602 }
2603
Ron Eldor591f1622018-01-22 12:30:04 +02002604 buf[8] = mki_len & 0xFF;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002605 memcpy(&buf[9], ssl->dtls_srtp_info.mki_value, mki_len);
Johan Pascalb62bb512015-12-03 21:56:45 +01002606
Ron Eldor591f1622018-01-22 12:30:04 +02002607 *olen = 9 + mki_len;
Johan Pascalb62bb512015-12-03 21:56:45 +01002608}
2609#endif /* MBEDTLS_SSL_DTLS_SRTP */
2610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002611#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002612MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002613static int ssl_write_hello_verify_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002614{
Janos Follath865b3eb2019-12-16 11:46:15 +00002615 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002616 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002617 unsigned char *cookie_len_byte;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002618
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002619 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello verify request"));
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002620
2621 /*
2622 * struct {
2623 * ProtocolVersion server_version;
2624 * opaque cookie<0..2^8-1>;
2625 * } HelloVerifyRequest;
2626 */
2627
Manuel Pégourié-Gonnardb35fe562014-08-09 17:00:46 +02002628 /* The RFC is not clear on this point, but sending the actual negotiated
2629 * version looks like the most interoperable thing to do. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002630 mbedtls_ssl_write_version(ssl->major_ver, ssl->minor_ver,
2631 ssl->conf->transport, p);
2632 MBEDTLS_SSL_DEBUG_BUF(3, "server version", p, 2);
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002633 p += 2;
2634
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02002635 /* If we get here, f_cookie_check is not null */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002636 if (ssl->conf->f_cookie_write == NULL) {
2637 MBEDTLS_SSL_DEBUG_MSG(1, ("inconsistent cookie callbacks"));
2638 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02002639 }
2640
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002641 /* Skip length byte until we know the length */
2642 cookie_len_byte = p++;
2643
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002644 if ((ret = ssl->conf->f_cookie_write(ssl->conf->p_cookie,
2645 &p, ssl->out_buf + MBEDTLS_SSL_OUT_BUFFER_LEN,
2646 ssl->cli_id, ssl->cli_id_len)) != 0) {
2647 MBEDTLS_SSL_DEBUG_RET(1, "f_cookie_write", ret);
2648 return ret;
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002649 }
2650
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002651 *cookie_len_byte = (unsigned char) (p - (cookie_len_byte + 1));
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002652
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002653 MBEDTLS_SSL_DEBUG_BUF(3, "cookie sent", cookie_len_byte + 1, *cookie_len_byte);
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002654
2655 ssl->out_msglen = p - ssl->out_msg;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002656 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
2657 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002659 ssl->state = MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002660
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002661 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
2662 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
2663 return ret;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002664 }
2665
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002666#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002667 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2668 (ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
2669 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flight_transmit", ret);
2670 return ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002671 }
Hanno Beckerbc2498a2018-08-28 10:13:29 +01002672#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002673
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002674 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello verify request"));
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002675
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002676 return 0;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002677}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002678#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002679
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002680static void ssl_handle_id_based_session_resumption(mbedtls_ssl_context *ssl)
Hanno Becker83e36712021-04-15 08:19:40 +01002681{
2682 int ret;
2683 mbedtls_ssl_session session_tmp;
2684 mbedtls_ssl_session * const session = ssl->session_negotiate;
2685
2686 /* Resume is 0 by default, see ssl_handshake_init().
2687 * It may be already set to 1 by ssl_parse_session_ticket_ext(). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002688 if (ssl->handshake->resume == 1) {
Hanno Becker83e36712021-04-15 08:19:40 +01002689 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002690 }
2691 if (session->id_len == 0) {
Hanno Becker83e36712021-04-15 08:19:40 +01002692 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002693 }
2694 if (ssl->conf->f_get_cache == NULL) {
Hanno Becker83e36712021-04-15 08:19:40 +01002695 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002696 }
Hanno Becker83e36712021-04-15 08:19:40 +01002697#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002698 if (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE) {
Hanno Becker83e36712021-04-15 08:19:40 +01002699 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002700 }
Hanno Becker83e36712021-04-15 08:19:40 +01002701#endif
2702
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002703 mbedtls_ssl_session_init(&session_tmp);
Hanno Becker83e36712021-04-15 08:19:40 +01002704
2705 session_tmp.id_len = session->id_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002706 memcpy(session_tmp.id, session->id, session->id_len);
Hanno Becker83e36712021-04-15 08:19:40 +01002707
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002708 ret = ssl->conf->f_get_cache(ssl->conf->p_cache,
2709 &session_tmp);
2710 if (ret != 0) {
Hanno Becker83e36712021-04-15 08:19:40 +01002711 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002712 }
Hanno Becker83e36712021-04-15 08:19:40 +01002713
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002714 if (session->ciphersuite != session_tmp.ciphersuite ||
2715 session->compression != session_tmp.compression) {
Hanno Becker83e36712021-04-15 08:19:40 +01002716 /* Mismatch between cached and negotiated session */
2717 goto exit;
2718 }
2719
2720 /* Move semantics */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002721 mbedtls_ssl_session_free(session);
Hanno Becker83e36712021-04-15 08:19:40 +01002722 *session = session_tmp;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002723 memset(&session_tmp, 0, sizeof(session_tmp));
Hanno Becker83e36712021-04-15 08:19:40 +01002724
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002725 MBEDTLS_SSL_DEBUG_MSG(3, ("session successfully restored from cache"));
Hanno Becker83e36712021-04-15 08:19:40 +01002726 ssl->handshake->resume = 1;
2727
2728exit:
2729
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002730 mbedtls_ssl_session_free(&session_tmp);
Hanno Becker83e36712021-04-15 08:19:40 +01002731}
2732
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002733MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002734static int ssl_write_server_hello(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002735{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002736#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +01002737 mbedtls_time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002738#endif
Janos Follath865b3eb2019-12-16 11:46:15 +00002739 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002740 size_t olen, ext_len = 0, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002741 unsigned char *buf, *p;
2742
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002743 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write server hello"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002745#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002746 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2747 ssl->handshake->verify_cookie_len != 0) {
2748 MBEDTLS_SSL_DEBUG_MSG(2, ("client hello was not authenticated"));
2749 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server hello"));
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002750
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002751 return ssl_write_hello_verify_request(ssl);
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002752 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002753#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002754
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002755 if (ssl->conf->f_rng == NULL) {
2756 MBEDTLS_SSL_DEBUG_MSG(1, ("no RNG provided"));
2757 return MBEDTLS_ERR_SSL_NO_RNG;
Paul Bakkera9a028e2013-11-21 17:31:06 +01002758 }
2759
Paul Bakker5121ce52009-01-03 21:22:43 +00002760 /*
2761 * 0 . 0 handshake type
2762 * 1 . 3 handshake length
2763 * 4 . 5 protocol version
2764 * 6 . 9 UNIX time()
2765 * 10 . 37 random bytes
2766 */
2767 buf = ssl->out_msg;
2768 p = buf + 4;
2769
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002770 mbedtls_ssl_write_version(ssl->major_ver, ssl->minor_ver,
2771 ssl->conf->transport, p);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002772 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00002773
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002774 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, chosen version: [%d:%d]",
2775 buf[4], buf[5]));
Paul Bakker5121ce52009-01-03 21:22:43 +00002776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002777#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002778 t = mbedtls_time(NULL);
2779 MBEDTLS_PUT_UINT32_BE(t, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01002780 p += 4;
Paul Bakker5121ce52009-01-03 21:22:43 +00002781
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002782 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, current time: %" MBEDTLS_PRINTF_LONGLONG,
2783 (long long) t));
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002784#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002785 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, p, 4)) != 0) {
2786 return ret;
2787 }
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002788
2789 p += 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002790#endif /* MBEDTLS_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00002791
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002792 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, p, 28)) != 0) {
2793 return ret;
2794 }
Paul Bakkera3d195c2011-11-27 21:07:34 +00002795
2796 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00002797
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002798 memcpy(ssl->handshake->randbytes + 32, buf + 6, 32);
Paul Bakker5121ce52009-01-03 21:22:43 +00002799
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002800 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes", buf + 6, 32);
Paul Bakker5121ce52009-01-03 21:22:43 +00002801
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002802 ssl_handle_id_based_session_resumption(ssl);
Paul Bakker5121ce52009-01-03 21:22:43 +00002803
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002804 if (ssl->handshake->resume == 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002805 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002806 * New session, create a new session id,
2807 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00002808 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002809 ssl->state++;
2810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002811#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002812 ssl->session_negotiate->start = mbedtls_time(NULL);
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002813#endif
2814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002815#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002816 if (ssl->handshake->new_session_ticket != 0) {
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002817 ssl->session_negotiate->id_len = n = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002818 memset(ssl->session_negotiate->id, 0, 32);
2819 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002820#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002821 {
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002822 ssl->session_negotiate->id_len = n = 32;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002823 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, ssl->session_negotiate->id,
2824 n)) != 0) {
2825 return ret;
2826 }
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002827 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002828 } else {
Paul Bakker5121ce52009-01-03 21:22:43 +00002829 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002830 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00002831 */
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002832 n = ssl->session_negotiate->id_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002833 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002834
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002835 if ((ret = mbedtls_ssl_derive_keys(ssl)) != 0) {
2836 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret);
2837 return ret;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002838 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002839 }
2840
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002841 /*
2842 * 38 . 38 session id length
2843 * 39 . 38+n session id
2844 * 39+n . 40+n chosen ciphersuite
2845 * 41+n . 41+n chosen compression alg.
2846 * 42+n . 43+n extensions length
2847 * 44+n . 43+n+m extensions
2848 */
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002849 *p++ = (unsigned char) ssl->session_negotiate->id_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002850 memcpy(p, ssl->session_negotiate->id, ssl->session_negotiate->id_len);
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002851 p += ssl->session_negotiate->id_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002852
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002853 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, session id len.: %" MBEDTLS_PRINTF_SIZET, n));
2854 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, session id", buf + 39, n);
2855 MBEDTLS_SSL_DEBUG_MSG(3, ("%s session has been resumed",
2856 ssl->handshake->resume ? "a" : "no"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002857
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002858 MBEDTLS_PUT_UINT16_BE(ssl->session_negotiate->ciphersuite, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01002859 p += 2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002860 *p++ = MBEDTLS_BYTE_0(ssl->session_negotiate->compression);
Paul Bakker5121ce52009-01-03 21:22:43 +00002861
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002862 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, chosen ciphersuite: %s",
2863 mbedtls_ssl_get_ciphersuite_name(ssl->session_negotiate->ciphersuite)));
2864 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, compress alg.: 0x%02X",
2865 (unsigned int) ssl->session_negotiate->compression));
Paul Bakker48916f92012-09-16 19:57:18 +00002866
Janos Follathc6dab2b2016-05-23 14:27:02 +01002867 /* Do not write the extensions if the protocol is SSLv3 */
2868#if defined(MBEDTLS_SSL_PROTO_SSL3)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002869 if ((ssl->major_ver != 3) || (ssl->minor_ver != 0)) {
Janos Follathc6dab2b2016-05-23 14:27:02 +01002870#endif
2871
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002872 /*
2873 * First write extensions, then the total length
2874 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002875 ssl_write_renegotiation_ext(ssl, p + 2 + ext_len, &olen);
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002876 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00002877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002878#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002879 ssl_write_max_fragment_length_ext(ssl, p + 2 + ext_len, &olen);
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002880 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02002881#endif
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002883#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002884 ssl_write_truncated_hmac_ext(ssl, p + 2 + ext_len, &olen);
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002885 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +02002886#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002887
Hanno Beckera0e20d02019-05-15 14:03:01 +01002888#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002889 ssl_write_cid_ext(ssl, p + 2 + ext_len, &olen);
Hanno Becker51de2d32019-04-26 15:46:55 +01002890 ext_len += olen;
2891#endif
2892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002893#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002894 ssl_write_encrypt_then_mac_ext(ssl, p + 2 + ext_len, &olen);
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002895 ext_len += olen;
2896#endif
2897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002898#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002899 ssl_write_extended_ms_ext(ssl, p + 2 + ext_len, &olen);
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002900 ext_len += olen;
2901#endif
2902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002903#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002904 ssl_write_session_ticket_ext(ssl, p + 2 + ext_len, &olen);
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002905 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02002906#endif
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002907
Manuel Pégourié-Gonnardf4721792015-09-15 10:53:51 +02002908#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +01002909 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002910 if (mbedtls_ssl_ciphersuite_uses_ec(
2911 mbedtls_ssl_ciphersuite_from_id(ssl->session_negotiate->ciphersuite))) {
2912 ssl_write_supported_point_formats_ext(ssl, p + 2 + ext_len, &olen);
Ron Eldor755bb6a2018-02-14 19:30:48 +02002913 ext_len += olen;
2914 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002915#endif
2916
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002917#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002918 ssl_write_ecjpake_kkpp_ext(ssl, p + 2 + ext_len, &olen);
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002919 ext_len += olen;
2920#endif
2921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002922#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002923 ssl_write_alpn_ext(ssl, p + 2 + ext_len, &olen);
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002924 ext_len += olen;
2925#endif
2926
Johan Pascalb62bb512015-12-03 21:56:45 +01002927#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002928 ssl_write_use_srtp_ext(ssl, p + 2 + ext_len, &olen);
Johan Pascalc3ccd982020-10-28 17:18:18 +01002929 ext_len += olen;
Johan Pascalb62bb512015-12-03 21:56:45 +01002930#endif
2931
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002932 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, total extension length: %" MBEDTLS_PRINTF_SIZET,
2933 ext_len));
Paul Bakker48916f92012-09-16 19:57:18 +00002934
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002935 if (ext_len > 0) {
2936 MBEDTLS_PUT_UINT16_BE(ext_len, p, 0);
Joe Subbiani24647c52021-08-20 15:56:22 +01002937 p += 2 + ext_len;
Paul Bakkera7036632014-04-30 10:15:38 +02002938 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002939
Janos Follathc6dab2b2016-05-23 14:27:02 +01002940#if defined(MBEDTLS_SSL_PROTO_SSL3)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002941}
Janos Follathc6dab2b2016-05-23 14:27:02 +01002942#endif
2943
Paul Bakker5121ce52009-01-03 21:22:43 +00002944 ssl->out_msglen = p - buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002945 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
2946 ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00002947
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002948 ret = mbedtls_ssl_write_handshake_msg(ssl);
Paul Bakker5121ce52009-01-03 21:22:43 +00002949
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002950 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server hello"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002951
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002952 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002953}
2954
Gilles Peskineeccd8882020-03-10 12:19:08 +01002955#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002956MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002957static int ssl_write_certificate_request(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002958{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002959 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002960 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002961
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002962 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate request"));
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002963
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002964 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
2965 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate request"));
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002966 ssl->state++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002967 return 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002968 }
2969
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002970 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2971 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002972}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002973#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002974MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002975static int ssl_write_certificate_request(mbedtls_ssl_context *ssl)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002976{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002977 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002978 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002979 ssl->handshake->ciphersuite_info;
irwirc9bc3002020-04-01 13:46:36 +03002980 uint16_t dn_size, total_dn_size; /* excluding length bytes */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002981 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00002982 unsigned char *buf, *p;
Angus Grattond8213d02016-05-25 20:56:48 +10002983 const unsigned char * const end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002984 const mbedtls_x509_crt *crt;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02002985 int authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00002986
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002987 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate request"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002988
2989 ssl->state++;
2990
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02002991#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002992 if (ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET) {
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02002993 authmode = ssl->handshake->sni_authmode;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002994 } else
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02002995#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002996 authmode = ssl->conf->authmode;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02002997
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002998 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info) ||
2999 authmode == MBEDTLS_SSL_VERIFY_NONE) {
3000 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate request"));
3001 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003002 }
3003
3004 /*
3005 * 0 . 0 handshake type
3006 * 1 . 3 handshake length
3007 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01003008 * 5 .. m-1 cert types
3009 * m .. m+1 sig alg length (TLS 1.2 only)
Paul Bakker9af723c2014-05-01 13:03:14 +02003010 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00003011 * n .. n+1 length of all DNs
3012 * n+2 .. n+3 length of DN 1
3013 * n+4 .. ... Distinguished Name #1
3014 * ... .. ... length of DN 2, etc.
3015 */
3016 buf = ssl->out_msg;
3017 p = buf + 4;
3018
3019 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003020 * Supported certificate types
3021 *
3022 * ClientCertificateType certificate_types<1..2^8-1>;
3023 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00003024 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003025 ct_len = 0;
Paul Bakker926af752012-11-23 13:38:07 +01003026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003027#if defined(MBEDTLS_RSA_C)
3028 p[1 + ct_len++] = MBEDTLS_SSL_CERT_TYPE_RSA_SIGN;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003029#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003030#if defined(MBEDTLS_ECDSA_C)
3031 p[1 + ct_len++] = MBEDTLS_SSL_CERT_TYPE_ECDSA_SIGN;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003032#endif
3033
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003034 p[0] = (unsigned char) ct_len++;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003035 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01003036
Paul Bakker577e0062013-08-28 11:57:20 +02003037 sa_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003038#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01003039 /*
3040 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01003041 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003042 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
3043 *
3044 * struct {
3045 * HashAlgorithm hash;
3046 * SignatureAlgorithm signature;
3047 * } SignatureAndHashAlgorithm;
3048 *
3049 * enum { (255) } HashAlgorithm;
3050 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01003051 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003052 if (ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3) {
Simon Butcher99000142016-10-13 17:21:01 +01003053 const int *cur;
Paul Bakkerf7abd422013-04-16 13:15:56 +02003054
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003055 /*
3056 * Supported signature algorithms
3057 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003058 for (cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++) {
3059 unsigned char hash = mbedtls_ssl_hash_from_md_alg(*cur);
Simon Butcher99000142016-10-13 17:21:01 +01003060
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003061 if (MBEDTLS_SSL_HASH_NONE == hash || mbedtls_ssl_set_calc_verify_md(ssl, hash)) {
Simon Butcher99000142016-10-13 17:21:01 +01003062 continue;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003063 }
Simon Butcher99000142016-10-13 17:21:01 +01003064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003065#if defined(MBEDTLS_RSA_C)
Simon Butcher99000142016-10-13 17:21:01 +01003066 p[2 + sa_len++] = hash;
3067 p[2 + sa_len++] = MBEDTLS_SSL_SIG_RSA;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003068#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003069#if defined(MBEDTLS_ECDSA_C)
Simon Butcher99000142016-10-13 17:21:01 +01003070 p[2 + sa_len++] = hash;
3071 p[2 + sa_len++] = MBEDTLS_SSL_SIG_ECDSA;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003072#endif
Simon Butcher99000142016-10-13 17:21:01 +01003073 }
Paul Bakker926af752012-11-23 13:38:07 +01003074
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003075 MBEDTLS_PUT_UINT16_BE(sa_len, p, 0);
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003076 sa_len += 2;
3077 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01003078 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003079#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003080
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003081 /*
3082 * DistinguishedName certificate_authorities<0..2^16-1>;
3083 * opaque DistinguishedName<1..2^16-1>;
3084 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003085 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00003086
Paul Bakkerbc3d9842012-11-26 16:12:02 +01003087 total_dn_size = 0;
Janos Follath088ce432017-04-10 12:42:31 +01003088
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003089 if (ssl->conf->cert_req_ca_list == MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED) {
Hanno Becker8bf74f32019-03-27 11:01:30 +00003090 /* NOTE: If trusted certificates are provisioned
3091 * via a CA callback (configured through
3092 * `mbedtls_ssl_conf_ca_cb()`, then the
3093 * CertificateRequest is currently left empty. */
3094
Janos Follath088ce432017-04-10 12:42:31 +01003095#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003096 if (ssl->handshake->sni_ca_chain != NULL) {
Janos Follath088ce432017-04-10 12:42:31 +01003097 crt = ssl->handshake->sni_ca_chain;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003098 } else
Janos Follath088ce432017-04-10 12:42:31 +01003099#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003100 crt = ssl->conf->ca_chain;
Manuel Pégourié-Gonnardbc1babb2015-10-02 11:16:47 +02003101
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003102 while (crt != NULL && crt->version != 0) {
irwirc9bc3002020-04-01 13:46:36 +03003103 /* It follows from RFC 5280 A.1 that this length
3104 * can be represented in at most 11 bits. */
3105 dn_size = (uint16_t) crt->subject_raw.len;
Janos Follath088ce432017-04-10 12:42:31 +01003106
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003107 if (end < p || (size_t) (end - p) < 2 + (size_t) dn_size) {
3108 MBEDTLS_SSL_DEBUG_MSG(1, ("skipping CAs: buffer too short"));
Janos Follath088ce432017-04-10 12:42:31 +01003109 break;
3110 }
3111
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003112 MBEDTLS_PUT_UINT16_BE(dn_size, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01003113 p += 2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003114 memcpy(p, crt->subject_raw.p, dn_size);
Janos Follath088ce432017-04-10 12:42:31 +01003115 p += dn_size;
3116
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003117 MBEDTLS_SSL_DEBUG_BUF(3, "requested DN", p - dn_size, dn_size);
Janos Follath088ce432017-04-10 12:42:31 +01003118
3119 total_dn_size += 2 + dn_size;
3120 crt = crt->next;
Manuel Pégourié-Gonnardbc1babb2015-10-02 11:16:47 +02003121 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003122 }
3123
Paul Bakker926af752012-11-23 13:38:07 +01003124 ssl->out_msglen = p - buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003125 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3126 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_REQUEST;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003127 MBEDTLS_PUT_UINT16_BE(total_dn_size, ssl->out_msg, 4 + ct_len + sa_len);
Paul Bakker5121ce52009-01-03 21:22:43 +00003128
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003129 ret = mbedtls_ssl_write_handshake_msg(ssl);
Paul Bakker5121ce52009-01-03 21:22:43 +00003130
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003131 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate request"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003132
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003133 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003134}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003135#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003137#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
3138 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003139MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003140static int ssl_get_ecdh_params_from_cert(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003141{
Janos Follath865b3eb2019-12-16 11:46:15 +00003142 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003143 mbedtls_pk_context *own_key = mbedtls_ssl_own_key(ssl);
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003144
Manuel Pégourié-Gonnardb9c7ea42022-06-14 09:25:17 +02003145 /* Check if the key is a transparent ECDH key.
3146 * This also ensures that it is safe to call mbedtls_pk_ec(). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003147 if (mbedtls_pk_get_type(own_key) != MBEDTLS_PK_ECKEY &&
3148 mbedtls_pk_get_type(own_key) != MBEDTLS_PK_ECKEY_DH) {
3149 MBEDTLS_SSL_DEBUG_MSG(1, ("server key not ECDH capable"));
3150 return MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH;
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003151 }
3152
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003153 if ((ret = mbedtls_ecdh_get_params(&ssl->handshake->ecdh_ctx,
3154 mbedtls_pk_ec(*own_key),
3155 MBEDTLS_ECDH_OURS)) != 0) {
3156 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ecdh_get_params"), ret);
3157 return ret;
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003158 }
3159
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003160 return 0;
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003161}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003162#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
3163 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003164
Gilles Peskineeccd8882020-03-10 12:19:08 +01003165#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) && \
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003166 defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003167MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003168static int ssl_resume_server_key_exchange(mbedtls_ssl_context *ssl,
3169 size_t *signature_len)
Paul Bakker41c83d32013-03-20 14:39:14 +01003170{
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02003171 /* Append the signature to ssl->out_msg, leaving 2 bytes for the
3172 * signature length which will be added in ssl_write_server_key_exchange
3173 * after the call to ssl_prepare_server_key_exchange.
3174 * ssl_write_server_key_exchange also takes care of incrementing
3175 * ssl->out_msglen. */
3176 unsigned char *sig_start = ssl->out_msg + ssl->out_msglen + 2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003177 size_t sig_max_len = (ssl->out_buf + MBEDTLS_SSL_OUT_CONTENT_LEN
3178 - sig_start);
3179 int ret = ssl->conf->f_async_resume(ssl,
3180 sig_start, signature_len, sig_max_len);
3181 if (ret != MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS) {
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003182 ssl->handshake->async_in_progress = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003183 mbedtls_ssl_set_async_operation_data(ssl, NULL);
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003184 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003185 MBEDTLS_SSL_DEBUG_RET(2, "ssl_resume_server_key_exchange", ret);
3186 return ret;
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003187}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003188#endif /* defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) &&
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003189 defined(MBEDTLS_SSL_ASYNC_PRIVATE) */
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003190
Gilles Peskined3eb0612018-01-08 17:07:44 +01003191/* Prepare the ServerKeyExchange message, up to and including
Gilles Peskine168dae82018-04-25 23:35:42 +02003192 * calculating the signature if any, but excluding formatting the
3193 * signature and sending the message. */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003194MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003195static int ssl_prepare_server_key_exchange(mbedtls_ssl_context *ssl,
3196 size_t *signature_len)
Paul Bakker5690efc2011-05-26 13:16:06 +00003197{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003198 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003199 ssl->handshake->ciphersuite_info;
3200
Gilles Peskineeccd8882020-03-10 12:19:08 +01003201#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PFS_ENABLED)
3202#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskine3ce9b902018-01-06 01:34:21 +01003203 unsigned char *dig_signed = NULL;
Gilles Peskineeccd8882020-03-10 12:19:08 +01003204#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
3205#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PFS_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01003206
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003207 (void) ciphersuite_info; /* unused in some configurations */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003208#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskine22e695f2018-04-26 00:22:50 +02003209 (void) signature_len;
Gilles Peskineeccd8882020-03-10 12:19:08 +01003210#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01003211
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003212 ssl->out_msglen = 4; /* header (type:1, length:3) to be written later */
Paul Bakker5121ce52009-01-03 21:22:43 +00003213
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003214 /*
3215 *
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003216 * Part 1: Provide key exchange parameters for chosen ciphersuite.
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003217 *
3218 */
3219
3220 /*
3221 * - ECJPAKE key exchanges
3222 */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003223#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003224 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
Janos Follath865b3eb2019-12-16 11:46:15 +00003225 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher600c5e62018-06-14 08:58:59 +01003226 size_t len = 0;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003227
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003228 ret = mbedtls_ecjpake_write_round_two(
3229 &ssl->handshake->ecjpake_ctx,
3230 ssl->out_msg + ssl->out_msglen,
Angus Grattond8213d02016-05-25 20:56:48 +10003231 MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen, &len,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003232 ssl->conf->f_rng, ssl->conf->p_rng);
3233 if (ret != 0) {
3234 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_write_round_two", ret);
3235 return ret;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003236 }
3237
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003238 ssl->out_msglen += len;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003239 }
3240#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
3241
Hanno Becker1aa267c2017-04-28 17:08:27 +01003242 /*
3243 * For (EC)DHE key exchanges with PSK, parameters are prefixed by support
3244 * identity hint (RFC 4279, Sec. 3). Until someone needs this feature,
3245 * we use empty support identity hints here.
3246 **/
3247#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003248 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003249 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
3250 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003251 ssl->out_msg[ssl->out_msglen++] = 0x00;
3252 ssl->out_msg[ssl->out_msglen++] = 0x00;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003253 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003254#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED ||
3255 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003256
Hanno Becker7e5437a2017-04-28 17:15:26 +01003257 /*
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003258 * - DHE key exchanges
Hanno Becker1aa267c2017-04-28 17:08:27 +01003259 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003260#if defined(MBEDTLS_KEY_EXCHANGE_SOME_DHE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003261 if (mbedtls_ssl_ciphersuite_uses_dhe(ciphersuite_info)) {
Janos Follath865b3eb2019-12-16 11:46:15 +00003262 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher600c5e62018-06-14 08:58:59 +01003263 size_t len = 0;
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003264
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003265 if (ssl->conf->dhm_P.p == NULL || ssl->conf->dhm_G.p == NULL) {
3266 MBEDTLS_SSL_DEBUG_MSG(1, ("no DH parameters set"));
3267 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01003268 }
3269
Paul Bakker41c83d32013-03-20 14:39:14 +01003270 /*
3271 * Ephemeral DH parameters:
3272 *
3273 * struct {
3274 * opaque dh_p<1..2^16-1>;
3275 * opaque dh_g<1..2^16-1>;
3276 * opaque dh_Ys<1..2^16-1>;
3277 * } ServerDHParams;
3278 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003279 if ((ret = mbedtls_dhm_set_group(&ssl->handshake->dhm_ctx,
3280 &ssl->conf->dhm_P,
3281 &ssl->conf->dhm_G)) != 0) {
3282 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_set_group", ret);
3283 return ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01003284 }
Paul Bakker48916f92012-09-16 19:57:18 +00003285
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003286 if ((ret = mbedtls_dhm_make_params(
3287 &ssl->handshake->dhm_ctx,
3288 (int) mbedtls_mpi_size(&ssl->handshake->dhm_ctx.P),
3289 ssl->out_msg + ssl->out_msglen, &len,
3290 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
3291 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_make_params", ret);
3292 return ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01003293 }
3294
Gilles Peskineeccd8882020-03-10 12:19:08 +01003295#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003296 dig_signed = ssl->out_msg + ssl->out_msglen;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003297#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003298
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003299 ssl->out_msglen += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003300
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003301 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: X ", &ssl->handshake->dhm_ctx.X);
3302 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: P ", &ssl->handshake->dhm_ctx.P);
3303 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: G ", &ssl->handshake->dhm_ctx.G);
3304 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: GX", &ssl->handshake->dhm_ctx.GX);
Paul Bakker41c83d32013-03-20 14:39:14 +01003305 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003306#endif /* MBEDTLS_KEY_EXCHANGE_SOME_DHE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01003307
Hanno Becker1aa267c2017-04-28 17:08:27 +01003308 /*
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003309 * - ECDHE key exchanges
Hanno Becker1aa267c2017-04-28 17:08:27 +01003310 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003311#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003312 if (mbedtls_ssl_ciphersuite_uses_ecdhe(ciphersuite_info)) {
Paul Bakker41c83d32013-03-20 14:39:14 +01003313 /*
3314 * Ephemeral ECDH parameters:
3315 *
3316 * struct {
3317 * ECParameters curve_params;
3318 * ECPoint public;
3319 * } ServerECDHParams;
3320 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003321 const mbedtls_ecp_curve_info **curve = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003322 const mbedtls_ecp_group_id *gid;
Janos Follath865b3eb2019-12-16 11:46:15 +00003323 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher600c5e62018-06-14 08:58:59 +01003324 size_t len = 0;
Gergely Budai987bfb52014-01-19 21:48:42 +01003325
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01003326 /* Match our preference list against the offered curves */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003327 for (gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++) {
3328 for (curve = ssl->handshake->curves; *curve != NULL; curve++) {
3329 if ((*curve)->grp_id == *gid) {
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01003330 goto curve_matching_done;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003331 }
3332 }
3333 }
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01003334
3335curve_matching_done:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003336 if (curve == NULL || *curve == NULL) {
3337 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching curve for ECDHE"));
3338 return MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN;
Gergely Budai987bfb52014-01-19 21:48:42 +01003339 }
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01003340
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003341 MBEDTLS_SSL_DEBUG_MSG(2, ("ECDHE curve: %s", (*curve)->name));
Gergely Budai987bfb52014-01-19 21:48:42 +01003342
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003343 if ((ret = mbedtls_ecdh_setup(&ssl->handshake->ecdh_ctx,
3344 (*curve)->grp_id)) != 0) {
3345 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecp_group_load", ret);
3346 return ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01003347 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003348
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003349 if ((ret = mbedtls_ecdh_make_params(
3350 &ssl->handshake->ecdh_ctx, &len,
3351 ssl->out_msg + ssl->out_msglen,
3352 MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen,
3353 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
3354 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_make_params", ret);
3355 return ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01003356 }
3357
Gilles Peskineeccd8882020-03-10 12:19:08 +01003358#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003359 dig_signed = ssl->out_msg + ssl->out_msglen;
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003360#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003361
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003362 ssl->out_msglen += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003363
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003364 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
3365 MBEDTLS_DEBUG_ECDH_Q);
Paul Bakker41c83d32013-03-20 14:39:14 +01003366 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003367#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003368
Hanno Becker1aa267c2017-04-28 17:08:27 +01003369 /*
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003370 *
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003371 * Part 2: For key exchanges involving the server signing the
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003372 * exchange parameters, compute and add the signature here.
3373 *
Hanno Becker1aa267c2017-04-28 17:08:27 +01003374 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003375#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003376 if (mbedtls_ssl_ciphersuite_uses_server_signature(ciphersuite_info)) {
Gilles Peskine1004c192018-01-08 16:59:14 +01003377 size_t dig_signed_len = ssl->out_msg + ssl->out_msglen - dig_signed;
Gilles Peskineca1d7422018-04-24 11:53:22 +02003378 size_t hashlen = 0;
Ronald Cron3a95d2b2021-10-18 09:47:58 +02003379#if defined(MBEDTLS_USE_PSA_CRYPTO)
3380 unsigned char hash[PSA_HASH_MAX_SIZE];
3381#else
Gilles Peskinee1efdf92018-01-05 21:18:37 +01003382 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Ronald Cron3a95d2b2021-10-18 09:47:58 +02003383#endif
Janos Follath865b3eb2019-12-16 11:46:15 +00003384 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23f36802012-09-28 14:15:14 +00003385
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003386 /*
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003387 * 2.1: Choose hash algorithm:
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01003388 * A: For TLS 1.2, obey signature-hash-algorithm extension
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003389 * to choose appropriate hash.
3390 * B: For SSL3, TLS1.0, TLS1.1 and ECDHE_ECDSA, use SHA1
3391 * (RFC 4492, Sec. 5.4)
3392 * C: Otherwise, use MD5 + SHA1 (RFC 4346, Sec. 7.4.3)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003393 */
Hanno Becker7e5437a2017-04-28 17:15:26 +01003394
3395 mbedtls_md_type_t md_alg;
3396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003397#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003398 mbedtls_pk_type_t sig_alg =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003399 mbedtls_ssl_get_ciphersuite_sig_pk_alg(ciphersuite_info);
3400 if (ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3) {
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003401 /* A: For TLS 1.2, obey signature-hash-algorithm extension
3402 * (RFC 5246, Sec. 7.4.1.4.1). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003403 if (sig_alg == MBEDTLS_PK_NONE ||
3404 (md_alg = mbedtls_ssl_sig_hash_set_find(&ssl->handshake->hash_algs,
3405 sig_alg)) == MBEDTLS_MD_NONE) {
3406 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01003407 /* (... because we choose a cipher suite
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003408 * only if there is a matching hash.) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003409 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003410 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003411 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003412#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3413#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003414 defined(MBEDTLS_SSL_PROTO_TLS1_1)
3415 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA) {
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003416 /* B: Default hash SHA1 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003417 md_alg = MBEDTLS_MD_SHA1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003418 } else
Hanno Becker1aa267c2017-04-28 17:08:27 +01003419#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
3420 MBEDTLS_SSL_PROTO_TLS1_1 */
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003421 {
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003422 /* C: MD5 + SHA1 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003423 md_alg = MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003424 }
3425
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003426 MBEDTLS_SSL_DEBUG_MSG(3, ("pick hash algorithm %u for signing", (unsigned) md_alg));
Hanno Becker7e5437a2017-04-28 17:15:26 +01003427
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003428 /*
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003429 * 2.2: Compute the hash to be signed
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003430 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003431#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003432 defined(MBEDTLS_SSL_PROTO_TLS1_1)
3433 if (md_alg == MBEDTLS_MD_NONE) {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003434 hashlen = 36;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003435 ret = mbedtls_ssl_get_key_exchange_md_ssl_tls(ssl, hash,
3436 dig_signed,
3437 dig_signed_len);
3438 if (ret != 0) {
3439 return ret;
3440 }
3441 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003442#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
3443 MBEDTLS_SSL_PROTO_TLS1_1 */
3444#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003445 defined(MBEDTLS_SSL_PROTO_TLS1_2)
3446 if (md_alg != MBEDTLS_MD_NONE) {
3447 ret = mbedtls_ssl_get_key_exchange_md_tls1_2(ssl, hash, &hashlen,
3448 dig_signed,
3449 dig_signed_len,
3450 md_alg);
3451 if (ret != 0) {
3452 return ret;
3453 }
3454 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003455#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3456 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02003457 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003458 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3459 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker577e0062013-08-28 11:57:20 +02003460 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02003461
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003462 MBEDTLS_SSL_DEBUG_BUF(3, "parameters hash", hash, hashlen);
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003463
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003464 /*
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003465 * 2.3: Compute and add the signature
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003466 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003467#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003468 if (ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3) {
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003469 /*
3470 * For TLS 1.2, we need to specify signature and hash algorithm
Hanno Becker7e5437a2017-04-28 17:15:26 +01003471 * explicitly through a prefix to the signature.
3472 *
3473 * struct {
3474 * HashAlgorithm hash;
3475 * SignatureAlgorithm signature;
3476 * } SignatureAndHashAlgorithm;
3477 *
3478 * struct {
3479 * SignatureAndHashAlgorithm algorithm;
3480 * opaque signature<0..2^16-1>;
3481 * } DigitallySigned;
3482 *
3483 */
3484
Gilles Peskine1004c192018-01-08 16:59:14 +01003485 ssl->out_msg[ssl->out_msglen++] =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003486 mbedtls_ssl_hash_from_md_alg(md_alg);
Gilles Peskine1004c192018-01-08 16:59:14 +01003487 ssl->out_msg[ssl->out_msglen++] =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003488 mbedtls_ssl_sig_from_pk_alg(sig_alg);
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003489 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003490#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003491
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003492#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003493 if (ssl->conf->f_async_sign_start != NULL) {
3494 ret = ssl->conf->f_async_sign_start(ssl,
3495 mbedtls_ssl_own_cert(ssl),
3496 md_alg, hash, hashlen);
3497 switch (ret) {
3498 case MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH:
3499 /* act as if f_async_sign was null */
3500 break;
3501 case 0:
3502 ssl->handshake->async_in_progress = 1;
3503 return ssl_resume_server_key_exchange(ssl, signature_len);
3504 case MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS:
3505 ssl->handshake->async_in_progress = 1;
3506 return MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS;
3507 default:
3508 MBEDTLS_SSL_DEBUG_RET(1, "f_async_sign_start", ret);
3509 return ret;
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003510 }
3511 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003512#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003513
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003514 if (mbedtls_ssl_own_key(ssl) == NULL) {
3515 MBEDTLS_SSL_DEBUG_MSG(1, ("got no private key"));
3516 return MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED;
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003517 }
3518
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02003519 /* Append the signature to ssl->out_msg, leaving 2 bytes for the
3520 * signature length which will be added in ssl_write_server_key_exchange
3521 * after the call to ssl_prepare_server_key_exchange.
3522 * ssl_write_server_key_exchange also takes care of incrementing
3523 * ssl->out_msglen. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003524 if ((ret = mbedtls_pk_sign(mbedtls_ssl_own_key(ssl),
3525 md_alg, hash, hashlen,
3526 ssl->out_msg + ssl->out_msglen + 2,
3527 signature_len,
3528 ssl->conf->f_rng,
3529 ssl->conf->p_rng)) != 0) {
3530 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_sign", ret);
3531 return ret;
Paul Bakker23f36802012-09-28 14:15:14 +00003532 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00003533 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003534#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003535
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003536 return 0;
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003537}
Paul Bakker1ef83d62012-04-11 12:09:53 +00003538
Gilles Peskined3eb0612018-01-08 17:07:44 +01003539/* Prepare the ServerKeyExchange message and send it. For ciphersuites
Gilles Peskine168dae82018-04-25 23:35:42 +02003540 * that do not include a ServerKeyExchange message, do nothing. Either
3541 * way, if successful, move on to the next step in the SSL state
3542 * machine. */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003543MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003544static int ssl_write_server_key_exchange(mbedtls_ssl_context *ssl)
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003545{
Janos Follath865b3eb2019-12-16 11:46:15 +00003546 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003547 size_t signature_len = 0;
Gilles Peskineeccd8882020-03-10 12:19:08 +01003548#if defined(MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED)
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003549 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003550 ssl->handshake->ciphersuite_info;
Gilles Peskineeccd8882020-03-10 12:19:08 +01003551#endif /* MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003552
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003553 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write server key exchange"));
Gilles Peskined3eb0612018-01-08 17:07:44 +01003554
Gilles Peskineeccd8882020-03-10 12:19:08 +01003555#if defined(MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED)
Gilles Peskined3eb0612018-01-08 17:07:44 +01003556 /* Extract static ECDH parameters and abort if ServerKeyExchange
3557 * is not needed. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003558 if (mbedtls_ssl_ciphersuite_no_pfs(ciphersuite_info)) {
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003559 /* For suites involving ECDH, extract DH parameters
3560 * from certificate at this point. */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003561#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003562 if (mbedtls_ssl_ciphersuite_uses_ecdh(ciphersuite_info)) {
3563 ret = ssl_get_ecdh_params_from_cert(ssl);
3564 if (ret != 0) {
3565 MBEDTLS_SSL_DEBUG_RET(1, "ssl_get_ecdh_params_from_cert", ret);
3566 return ret;
Manuel Pégourié-Gonnard5b3f24f2022-06-10 09:34:20 +02003567 }
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003568 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003569#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDH_ENABLED */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003570
3571 /* Key exchanges not involving ephemeral keys don't use
3572 * ServerKeyExchange, so end here. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003573 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write server key exchange"));
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003574 ssl->state++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003575 return 0;
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003576 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003577#endif /* MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003578
Gilles Peskineeccd8882020-03-10 12:19:08 +01003579#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) && \
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003580 defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskined3eb0612018-01-08 17:07:44 +01003581 /* If we have already prepared the message and there is an ongoing
Gilles Peskine168dae82018-04-25 23:35:42 +02003582 * signature operation, resume signing. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003583 if (ssl->handshake->async_in_progress != 0) {
3584 MBEDTLS_SSL_DEBUG_MSG(2, ("resuming signature operation"));
3585 ret = ssl_resume_server_key_exchange(ssl, &signature_len);
3586 } else
Gilles Peskineeccd8882020-03-10 12:19:08 +01003587#endif /* defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) &&
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003588 defined(MBEDTLS_SSL_ASYNC_PRIVATE) */
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003589 {
3590 /* ServerKeyExchange is needed. Prepare the message. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003591 ret = ssl_prepare_server_key_exchange(ssl, &signature_len);
Gilles Peskined3eb0612018-01-08 17:07:44 +01003592 }
3593
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003594 if (ret != 0) {
Gilles Peskinead28bf02018-04-26 00:19:16 +02003595 /* If we're starting to write a new message, set ssl->out_msglen
3596 * to 0. But if we're resuming after an asynchronous message,
3597 * out_msglen is the amount of data written so far and mst be
3598 * preserved. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003599 if (ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS) {
3600 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server key exchange (pending)"));
3601 } else {
Gilles Peskined3eb0612018-01-08 17:07:44 +01003602 ssl->out_msglen = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003603 }
3604 return ret;
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003605 }
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003606
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003607 /* If there is a signature, write its length.
Gilles Peskine168dae82018-04-25 23:35:42 +02003608 * ssl_prepare_server_key_exchange already wrote the signature
3609 * itself at its proper place in the output buffer. */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003610#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003611 if (signature_len != 0) {
3612 ssl->out_msg[ssl->out_msglen++] = MBEDTLS_BYTE_1(signature_len);
3613 ssl->out_msg[ssl->out_msglen++] = MBEDTLS_BYTE_0(signature_len);
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003614
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003615 MBEDTLS_SSL_DEBUG_BUF(3, "my signature",
3616 ssl->out_msg + ssl->out_msglen,
3617 signature_len);
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003618
3619 /* Skip over the already-written signature */
3620 ssl->out_msglen += signature_len;
3621 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003622#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003623
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003624 /* Add header and send. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003625 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3626 ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003627
3628 ssl->state++;
3629
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003630 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
3631 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
3632 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003633 }
3634
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003635 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server key exchange"));
3636 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003637}
3638
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003639MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003640static int ssl_write_server_hello_done(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003641{
Janos Follath865b3eb2019-12-16 11:46:15 +00003642 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003643
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003644 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write server hello done"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003645
3646 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003647 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3648 ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_HELLO_DONE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003649
3650 ssl->state++;
3651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003652#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003653 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
3654 mbedtls_ssl_send_flight_completed(ssl);
3655 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003656#endif
3657
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003658 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
3659 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
3660 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003661 }
3662
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003663#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003664 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3665 (ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
3666 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flight_transmit", ret);
3667 return ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003668 }
Hanno Beckerbc2498a2018-08-28 10:13:29 +01003669#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003670
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003671 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server hello done"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003672
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003673 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003674}
3675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003676#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
3677 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003678MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003679static int ssl_parse_client_dh_public(mbedtls_ssl_context *ssl, unsigned char **p,
3680 const unsigned char *end)
Paul Bakker70df2fb2013-04-17 17:19:09 +02003681{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003682 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003683 size_t n;
3684
3685 /*
3686 * Receive G^Y mod P, premaster = (G^Y)^X mod P
3687 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003688 if (*p + 2 > end) {
3689 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange message"));
3690 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003691 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02003692
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003693 n = ((*p)[0] << 8) | (*p)[1];
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003694 *p += 2;
3695
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003696 if (*p + n > end) {
3697 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange message"));
3698 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003699 }
3700
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003701 if ((ret = mbedtls_dhm_read_public(&ssl->handshake->dhm_ctx, *p, n)) != 0) {
3702 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_read_public", ret);
3703 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003704 }
3705
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003706 *p += n;
3707
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003708 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: GY", &ssl->handshake->dhm_ctx.GY);
Paul Bakker70df2fb2013-04-17 17:19:09 +02003709
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003710 return ret;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003711}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003712#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
3713 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003715#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
3716 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003717
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003718#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003719MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003720static int ssl_resume_decrypt_pms(mbedtls_ssl_context *ssl,
3721 unsigned char *peer_pms,
3722 size_t *peer_pmslen,
3723 size_t peer_pmssize)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003724{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003725 int ret = ssl->conf->f_async_resume(ssl,
3726 peer_pms, peer_pmslen, peer_pmssize);
3727 if (ret != MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS) {
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003728 ssl->handshake->async_in_progress = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003729 mbedtls_ssl_set_async_operation_data(ssl, NULL);
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003730 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003731 MBEDTLS_SSL_DEBUG_RET(2, "ssl_decrypt_encrypted_pms", ret);
3732 return ret;
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003733}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003734#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003735
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003736MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003737static int ssl_decrypt_encrypted_pms(mbedtls_ssl_context *ssl,
3738 const unsigned char *p,
3739 const unsigned char *end,
3740 unsigned char *peer_pms,
3741 size_t *peer_pmslen,
3742 size_t peer_pmssize)
Paul Bakker70df2fb2013-04-17 17:19:09 +02003743{
Janos Follath865b3eb2019-12-16 11:46:15 +00003744 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Leonid Rozenboim81e74232022-08-08 15:43:44 -07003745
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003746 mbedtls_x509_crt *own_cert = mbedtls_ssl_own_cert(ssl);
3747 if (own_cert == NULL) {
3748 MBEDTLS_SSL_DEBUG_MSG(1, ("got no local certificate"));
3749 return MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Leonid Rozenboim81e74232022-08-08 15:43:44 -07003750 }
3751 mbedtls_pk_context *public_key = &own_cert->pk;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003752 mbedtls_pk_context *private_key = mbedtls_ssl_own_key(ssl);
3753 size_t len = mbedtls_pk_get_len(public_key);
Paul Bakker70df2fb2013-04-17 17:19:09 +02003754
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003755#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003756 /* If we have already started decoding the message and there is an ongoing
Gilles Peskine168dae82018-04-25 23:35:42 +02003757 * decryption operation, resume signing. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003758 if (ssl->handshake->async_in_progress != 0) {
3759 MBEDTLS_SSL_DEBUG_MSG(2, ("resuming decryption operation"));
3760 return ssl_resume_decrypt_pms(ssl,
3761 peer_pms, peer_pmslen, peer_pmssize);
Paul Bakker70df2fb2013-04-17 17:19:09 +02003762 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003763#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003764
3765 /*
Gilles Peskine422ccab2018-01-11 18:29:01 +01003766 * Prepare to decrypt the premaster using own private RSA key
Paul Bakker70df2fb2013-04-17 17:19:09 +02003767 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003768#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3769 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003770 if (ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0) {
3771 if (p + 2 > end) {
3772 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange message"));
3773 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE;
Philippe Antoine747fd532018-05-30 09:13:21 +02003774 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003775 if (*p++ != MBEDTLS_BYTE_1(len) ||
3776 *p++ != MBEDTLS_BYTE_0(len)) {
3777 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange message"));
3778 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003779 }
3780 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003781#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02003782
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003783 if (p + len != end) {
3784 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange message"));
3785 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003786 }
3787
Gilles Peskine422ccab2018-01-11 18:29:01 +01003788 /*
3789 * Decrypt the premaster secret
3790 */
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003791#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003792 if (ssl->conf->f_async_decrypt_start != NULL) {
3793 ret = ssl->conf->f_async_decrypt_start(ssl,
3794 mbedtls_ssl_own_cert(ssl),
3795 p, len);
3796 switch (ret) {
3797 case MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH:
3798 /* act as if f_async_decrypt_start was null */
3799 break;
3800 case 0:
3801 ssl->handshake->async_in_progress = 1;
3802 return ssl_resume_decrypt_pms(ssl,
3803 peer_pms,
3804 peer_pmslen,
3805 peer_pmssize);
3806 case MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS:
3807 ssl->handshake->async_in_progress = 1;
3808 return MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS;
3809 default:
3810 MBEDTLS_SSL_DEBUG_RET(1, "f_async_decrypt_start", ret);
3811 return ret;
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003812 }
3813 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003814#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003815
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003816 if (!mbedtls_pk_can_do(private_key, MBEDTLS_PK_RSA)) {
3817 MBEDTLS_SSL_DEBUG_MSG(1, ("got no RSA private key"));
3818 return MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED;
Gilles Peskine422ccab2018-01-11 18:29:01 +01003819 }
3820
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003821 ret = mbedtls_pk_decrypt(private_key, p, len,
3822 peer_pms, peer_pmslen, peer_pmssize,
3823 ssl->conf->f_rng, ssl->conf->p_rng);
3824 return ret;
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003825}
3826
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003827MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003828static int ssl_parse_encrypted_pms(mbedtls_ssl_context *ssl,
3829 const unsigned char *p,
3830 const unsigned char *end,
3831 size_t pms_offset)
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003832{
Janos Follath865b3eb2019-12-16 11:46:15 +00003833 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003834 unsigned char *pms = ssl->handshake->premaster + pms_offset;
3835 unsigned char ver[2];
3836 unsigned char fake_pms[48], peer_pms[48];
3837 unsigned char mask;
3838 size_t i, peer_pmslen;
3839 unsigned int diff;
3840
Gilles Peskine0a8352b2018-06-13 18:16:41 +02003841 /* In case of a failure in decryption, the decryption may write less than
3842 * 2 bytes of output, but we always read the first two bytes. It doesn't
3843 * matter in the end because diff will be nonzero in that case due to
André Maroneze79533292020-11-12 09:37:42 +01003844 * ret being nonzero, and we only care whether diff is 0.
3845 * But do initialize peer_pms and peer_pmslen for robustness anyway. This
3846 * also makes memory analyzers happy (don't access uninitialized memory,
3847 * even if it's an unsigned char). */
Gilles Peskine0a8352b2018-06-13 18:16:41 +02003848 peer_pms[0] = peer_pms[1] = ~0;
André Maroneze79533292020-11-12 09:37:42 +01003849 peer_pmslen = 0;
Gilles Peskine0a8352b2018-06-13 18:16:41 +02003850
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003851 ret = ssl_decrypt_encrypted_pms(ssl, p, end,
3852 peer_pms,
3853 &peer_pmslen,
3854 sizeof(peer_pms));
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003855
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003856#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003857 if (ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS) {
3858 return ret;
3859 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003860#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003861
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003862 mbedtls_ssl_write_version(ssl->handshake->max_major_ver,
3863 ssl->handshake->max_minor_ver,
3864 ssl->conf->transport, ver);
Gilles Peskine2e333372018-04-24 13:22:10 +02003865
3866 /* Avoid data-dependent branches while checking for invalid
3867 * padding, to protect against timing-based Bleichenbacher-type
3868 * attacks. */
3869 diff = (unsigned int) ret;
3870 diff |= peer_pmslen ^ 48;
3871 diff |= peer_pms[0] ^ ver[0];
3872 diff |= peer_pms[1] ^ ver[1];
3873
3874 /* mask = diff ? 0xff : 0x00 using bit operations to avoid branches */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003875 mask = mbedtls_ct_uint_mask(diff);
Manuel Pégourié-Gonnardb9c93d02015-06-23 13:53:15 +02003876
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003877 /*
3878 * Protection against Bleichenbacher's attack: invalid PKCS#1 v1.5 padding
3879 * must not cause the connection to end immediately; instead, send a
3880 * bad_record_mac later in the handshake.
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003881 * To protect against timing-based variants of the attack, we must
3882 * not have any branch that depends on whether the decryption was
3883 * successful. In particular, always generate the fake premaster secret,
3884 * regardless of whether it will ultimately influence the output or not.
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003885 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003886 ret = ssl->conf->f_rng(ssl->conf->p_rng, fake_pms, sizeof(fake_pms));
3887 if (ret != 0) {
Gilles Peskinee1416382018-04-26 10:23:21 +02003888 /* It's ok to abort on an RNG failure, since this does not reveal
3889 * anything about the RSA decryption. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003890 return ret;
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003891 }
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003892
Manuel Pégourié-Gonnard331ba572015-04-20 12:33:57 +01003893#if defined(MBEDTLS_SSL_DEBUG_ALL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003894 if (diff != 0) {
3895 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange message"));
3896 }
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003897#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02003898
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003899 if (sizeof(ssl->handshake->premaster) < pms_offset ||
3900 sizeof(ssl->handshake->premaster) - pms_offset < 48) {
3901 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3902 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003903 }
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003904 ssl->handshake->pmslen = 48;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003905
Gilles Peskine422ccab2018-01-11 18:29:01 +01003906 /* Set pms to either the true or the fake PMS, without
3907 * data-dependent branches. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003908 for (i = 0; i < ssl->handshake->pmslen; i++) {
3909 pms[i] = (mask & fake_pms[i]) | ((~mask) & peer_pms[i]);
3910 }
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003911
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003912 return 0;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003913}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003914#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED ||
3915 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003916
Gilles Peskineeccd8882020-03-10 12:19:08 +01003917#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003918MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003919static int ssl_parse_client_psk_identity(mbedtls_ssl_context *ssl, unsigned char **p,
3920 const unsigned char *end)
Paul Bakkerfbb17802013-04-17 19:10:21 +02003921{
Paul Bakker6db455e2013-09-18 17:29:31 +02003922 int ret = 0;
irwir6527bd62019-09-21 18:51:25 +03003923 uint16_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003924
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003925 if (ssl_conf_has_psk_or_cb(ssl->conf) == 0) {
3926 MBEDTLS_SSL_DEBUG_MSG(1, ("got no pre-shared key"));
3927 return MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003928 }
3929
3930 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003931 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02003932 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003933 if (end - *p < 2) {
3934 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange message"));
3935 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003936 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02003937
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003938 n = ((*p)[0] << 8) | (*p)[1];
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003939 *p += 2;
3940
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003941 if (n == 0 || n > end - *p) {
3942 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange message"));
3943 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003944 }
3945
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003946 if (ssl->conf->f_psk != NULL) {
3947 if (ssl->conf->f_psk(ssl->conf->p_psk, ssl, *p, n) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003948 ret = MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003949 }
3950 } else {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003951 /* Identity is not a big secret since clients send it in the clear,
3952 * but treat it carefully anyway, just in case */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003953 if (n != ssl->conf->psk_identity_len ||
3954 mbedtls_ct_memcmp(ssl->conf->psk_identity, *p, n) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003955 ret = MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
Paul Bakker6db455e2013-09-18 17:29:31 +02003956 }
3957 }
3958
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003959 if (ret == MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY) {
3960 MBEDTLS_SSL_DEBUG_BUF(3, "Unknown PSK identity", *p, n);
3961 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3962 MBEDTLS_SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY);
3963 return MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003964 }
3965
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003966 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003967
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003968 return 0;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003969}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003970#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02003971
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003972MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003973static int ssl_parse_client_key_exchange(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003974{
Janos Follath865b3eb2019-12-16 11:46:15 +00003975 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003976 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00003977 unsigned char *p, *end;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003978
Hanno Beckere694c3e2017-12-27 21:34:08 +00003979 ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003980
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003981 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse client key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003982
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003983#if defined(MBEDTLS_SSL_ASYNC_PRIVATE) && \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003984 (defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
3985 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED))
3986 if ((ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
3987 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA) &&
3988 (ssl->handshake->async_in_progress != 0)) {
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003989 /* We've already read a record and there is an asynchronous
3990 * operation in progress to decrypt it. So skip reading the
Gilles Peskine168dae82018-04-25 23:35:42 +02003991 * record. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003992 MBEDTLS_SSL_DEBUG_MSG(3, ("will resume decryption of previously-read record"));
3993 } else
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003994#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003995 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
3996 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
3997 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003998 }
3999
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004000 p = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00004001 end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnardf8995832014-09-10 08:25:12 +00004002
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004003 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
4004 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange message"));
4005 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004006 }
4007
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004008 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE) {
4009 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange message"));
4010 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004011 }
4012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004013#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004014 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA) {
4015 if ((ret = ssl_parse_client_dh_public(ssl, &p, end)) != 0) {
4016 MBEDTLS_SSL_DEBUG_RET(1, ("ssl_parse_client_dh_public"), ret);
4017 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004018 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004019
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004020 if (p != end) {
4021 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange"));
4022 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01004023 }
4024
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004025 if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
4026 ssl->handshake->premaster,
4027 MBEDTLS_PREMASTER_SIZE,
4028 &ssl->handshake->pmslen,
4029 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
4030 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
4031 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004032 }
4033
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004034 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
4035 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004036#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
4037#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
4038 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
4039 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
4040 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004041 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004042 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
4043 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004044 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA) {
4045 if ((ret = mbedtls_ecdh_read_public(&ssl->handshake->ecdh_ctx,
4046 p, end - p)) != 0) {
4047 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_read_public", ret);
4048 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP;
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02004049 }
4050
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004051 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
4052 MBEDTLS_DEBUG_ECDH_QP);
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02004053
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004054 if ((ret = mbedtls_ecdh_calc_secret(&ssl->handshake->ecdh_ctx,
4055 &ssl->handshake->pmslen,
4056 ssl->handshake->premaster,
4057 MBEDTLS_MPI_MAX_SIZE,
4058 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
4059 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret);
4060 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004061 }
4062
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004063 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
4064 MBEDTLS_DEBUG_ECDH_Z);
4065 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004066#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
4067 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
4068 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
4069 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
4070#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004071 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK) {
4072 if ((ret = ssl_parse_client_psk_identity(ssl, &p, end)) != 0) {
4073 MBEDTLS_SSL_DEBUG_RET(1, ("ssl_parse_client_psk_identity"), ret);
4074 return ret;
Paul Bakkerfbb17802013-04-17 19:10:21 +02004075 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004076
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004077 if (p != end) {
4078 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange"));
4079 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01004080 }
4081
Hanno Becker845b9462018-10-26 12:07:29 +01004082#if defined(MBEDTLS_USE_PSA_CRYPTO)
Shaun Case0e7791f2021-12-20 21:14:10 -08004083 /* For opaque PSKs, we perform the PSK-to-MS derivation automatically
Hanno Becker845b9462018-10-26 12:07:29 +01004084 * and skip the intermediate PMS. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004085 if (ssl_use_opaque_psk(ssl) == 1) {
4086 MBEDTLS_SSL_DEBUG_MSG(1, ("skip PMS generation for opaque PSK"));
4087 } else
Hanno Becker845b9462018-10-26 12:07:29 +01004088#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004089 if ((ret = mbedtls_ssl_psk_derive_premaster(ssl,
4090 ciphersuite_info->key_exchange)) != 0) {
4091 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_psk_derive_premaster", ret);
4092 return ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02004093 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004094 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004095#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
4096#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004097 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
Gilles Peskineb74a1c72018-04-24 13:09:22 +02004098#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004099 if (ssl->handshake->async_in_progress != 0) {
Gilles Peskine2c6078e2018-01-12 13:46:43 +01004100 /* There is an asynchronous operation in progress to
4101 * decrypt the encrypted premaster secret, so skip
4102 * directly to resuming this operation. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004103 MBEDTLS_SSL_DEBUG_MSG(3, ("PSK identity already parsed"));
Gilles Peskine2c6078e2018-01-12 13:46:43 +01004104 /* Update p to skip the PSK identity. ssl_parse_encrypted_pms
4105 * won't actually use it, but maintain p anyway for robustness. */
4106 p += ssl->conf->psk_identity_len + 2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004107 } else
Gilles Peskineb74a1c72018-04-24 13:09:22 +02004108#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004109 if ((ret = ssl_parse_client_psk_identity(ssl, &p, end)) != 0) {
4110 MBEDTLS_SSL_DEBUG_RET(1, ("ssl_parse_client_psk_identity"), ret);
4111 return ret;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02004112 }
4113
Hanno Becker845b9462018-10-26 12:07:29 +01004114#if defined(MBEDTLS_USE_PSA_CRYPTO)
4115 /* Opaque PSKs are currently only supported for PSK-only. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004116 if (ssl_use_opaque_psk(ssl) == 1) {
4117 MBEDTLS_SSL_DEBUG_MSG(1, ("opaque PSK not supported with RSA-PSK"));
4118 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardd80d8a42022-06-14 10:53:15 +02004119 }
Hanno Becker845b9462018-10-26 12:07:29 +01004120#endif
4121
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004122 if ((ret = ssl_parse_encrypted_pms(ssl, p, end, 2)) != 0) {
4123 MBEDTLS_SSL_DEBUG_RET(1, ("ssl_parse_encrypted_pms"), ret);
4124 return ret;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02004125 }
4126
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004127 if ((ret = mbedtls_ssl_psk_derive_premaster(ssl,
4128 ciphersuite_info->key_exchange)) != 0) {
4129 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_psk_derive_premaster", ret);
4130 return ret;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02004131 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004132 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004133#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
4134#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004135 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
4136 if ((ret = ssl_parse_client_psk_identity(ssl, &p, end)) != 0) {
4137 MBEDTLS_SSL_DEBUG_RET(1, ("ssl_parse_client_psk_identity"), ret);
4138 return ret;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004139 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004140 if ((ret = ssl_parse_client_dh_public(ssl, &p, end)) != 0) {
4141 MBEDTLS_SSL_DEBUG_RET(1, ("ssl_parse_client_dh_public"), ret);
4142 return ret;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004143 }
4144
Hanno Becker845b9462018-10-26 12:07:29 +01004145#if defined(MBEDTLS_USE_PSA_CRYPTO)
4146 /* Opaque PSKs are currently only supported for PSK-only. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004147 if (ssl_use_opaque_psk(ssl) == 1) {
4148 MBEDTLS_SSL_DEBUG_MSG(1, ("opaque PSK not supported with DHE-PSK"));
4149 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardd80d8a42022-06-14 10:53:15 +02004150 }
Hanno Becker845b9462018-10-26 12:07:29 +01004151#endif
4152
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004153 if (p != end) {
4154 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange"));
4155 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01004156 }
4157
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004158 if ((ret = mbedtls_ssl_psk_derive_premaster(ssl,
4159 ciphersuite_info->key_exchange)) != 0) {
4160 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_psk_derive_premaster", ret);
4161 return ret;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004162 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004163 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004164#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
4165#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004166 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
4167 if ((ret = ssl_parse_client_psk_identity(ssl, &p, end)) != 0) {
4168 MBEDTLS_SSL_DEBUG_RET(1, ("ssl_parse_client_psk_identity"), ret);
4169 return ret;
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004170 }
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02004171
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004172 if ((ret = mbedtls_ecdh_read_public(&ssl->handshake->ecdh_ctx,
4173 p, end - p)) != 0) {
4174 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_read_public", ret);
4175 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP;
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004176 }
4177
Hanno Becker845b9462018-10-26 12:07:29 +01004178#if defined(MBEDTLS_USE_PSA_CRYPTO)
4179 /* Opaque PSKs are currently only supported for PSK-only. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004180 if (ssl_use_opaque_psk(ssl) == 1) {
4181 MBEDTLS_SSL_DEBUG_MSG(1, ("opaque PSK not supported with ECDHE-PSK"));
4182 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardd80d8a42022-06-14 10:53:15 +02004183 }
Hanno Becker845b9462018-10-26 12:07:29 +01004184#endif
4185
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004186 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
4187 MBEDTLS_DEBUG_ECDH_QP);
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02004188
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004189 if ((ret = mbedtls_ssl_psk_derive_premaster(ssl,
4190 ciphersuite_info->key_exchange)) != 0) {
4191 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_psk_derive_premaster", ret);
4192 return ret;
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004193 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004194 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004195#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
4196#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004197 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA) {
4198 if ((ret = ssl_parse_encrypted_pms(ssl, p, end, 0)) != 0) {
4199 MBEDTLS_SSL_DEBUG_RET(1, ("ssl_parse_parse_encrypted_pms_secret"), ret);
4200 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004201 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004202 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004203#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02004204#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004205 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
4206 ret = mbedtls_ecjpake_read_round_two(&ssl->handshake->ecjpake_ctx,
4207 p, end - p);
4208 if (ret != 0) {
4209 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_read_round_two", ret);
4210 return MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02004211 }
4212
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004213 ret = mbedtls_ecjpake_derive_secret(&ssl->handshake->ecjpake_ctx,
4214 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
4215 ssl->conf->f_rng, ssl->conf->p_rng);
4216 if (ret != 0) {
4217 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_derive_secret", ret);
4218 return ret;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02004219 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004220 } else
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02004221#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004222 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004223 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
4224 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004225 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004226
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004227 if ((ret = mbedtls_ssl_derive_keys(ssl)) != 0) {
4228 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret);
4229 return ret;
Paul Bakkerff60ee62010-03-16 21:09:09 +00004230 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004231
Paul Bakker5121ce52009-01-03 21:22:43 +00004232 ssl->state++;
4233
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004234 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse client key exchange"));
Paul Bakker5121ce52009-01-03 21:22:43 +00004235
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004236 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00004237}
4238
Gilles Peskineeccd8882020-03-10 12:19:08 +01004239#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02004240MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004241static int ssl_parse_certificate_verify(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00004242{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01004243 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00004244 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00004245
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004246 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate verify"));
Paul Bakker5121ce52009-01-03 21:22:43 +00004247
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004248 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
4249 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate verify"));
Paul Bakkered27a042013-04-18 22:46:23 +02004250 ssl->state++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004251 return 0;
Paul Bakkered27a042013-04-18 22:46:23 +02004252 }
4253
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004254 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
4255 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004256}
Gilles Peskineeccd8882020-03-10 12:19:08 +01004257#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02004258MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004259static int ssl_parse_certificate_verify(mbedtls_ssl_context *ssl)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004260{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004261 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004262 size_t i, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004263 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02004264 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004265 size_t hashlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004266#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4267 mbedtls_pk_type_t pk_alg;
Paul Bakker577e0062013-08-28 11:57:20 +02004268#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004269 mbedtls_md_type_t md_alg;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01004270 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00004271 ssl->handshake->ciphersuite_info;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004272 mbedtls_pk_context *peer_pk;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004273
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004274 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate verify"));
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004275
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004276 if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
4277 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate verify"));
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004278 ssl->state++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004279 return 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004280 }
4281
Hanno Becker2a831a42019-02-07 13:17:25 +00004282#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004283 if (ssl->session_negotiate->peer_cert == NULL) {
4284 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate verify"));
Hanno Becker2a831a42019-02-07 13:17:25 +00004285 ssl->state++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004286 return 0;
Hanno Becker2a831a42019-02-07 13:17:25 +00004287 }
4288#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004289 if (ssl->session_negotiate->peer_cert_digest == NULL) {
4290 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate verify"));
Hanno Becker2a831a42019-02-07 13:17:25 +00004291 ssl->state++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004292 return 0;
Hanno Becker2a831a42019-02-07 13:17:25 +00004293 }
4294#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4295
Simon Butcher99000142016-10-13 17:21:01 +01004296 /* Read the message without adding it to the checksum */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004297 ret = mbedtls_ssl_read_record(ssl, 0 /* no checksum update */);
4298 if (0 != ret) {
4299 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_read_record"), ret);
4300 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004301 }
4302
4303 ssl->state++;
4304
Simon Butcher99000142016-10-13 17:21:01 +01004305 /* Process the message contents */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004306 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ||
4307 ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE_VERIFY) {
4308 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate verify message"));
4309 return MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00004310 }
4311
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004312 i = mbedtls_ssl_hs_hdr_len(ssl);
Paul Bakker5121ce52009-01-03 21:22:43 +00004313
Hanno Beckera1ab9be2019-02-06 18:31:04 +00004314#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
4315 peer_pk = &ssl->handshake->peer_pubkey;
4316#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004317 if (ssl->session_negotiate->peer_cert == NULL) {
Hanno Beckera1ab9be2019-02-06 18:31:04 +00004318 /* Should never happen */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004319 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Beckera1ab9be2019-02-06 18:31:04 +00004320 }
4321 peer_pk = &ssl->session_negotiate->peer_cert->pk;
4322#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4323
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004324 /*
4325 * struct {
4326 * SignatureAndHashAlgorithm algorithm; -- TLS 1.2 only
4327 * opaque signature<0..2^16-1>;
4328 * } DigitallySigned;
4329 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004330#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
4331 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004332 if (ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004333 md_alg = MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004334 hashlen = 36;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02004335
4336 /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004337 if (mbedtls_pk_can_do(peer_pk, MBEDTLS_PK_ECDSA)) {
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02004338 hash_start += 16;
4339 hashlen -= 16;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004340 md_alg = MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02004341 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004342 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004343#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 ||
4344 MBEDTLS_SSL_PROTO_TLS1_1 */
4345#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004346 if (ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3) {
4347 if (i + 2 > ssl->in_hslen) {
4348 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate verify message"));
4349 return MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY;
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00004350 }
4351
Paul Bakker5121ce52009-01-03 21:22:43 +00004352 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004353 * Hash
Paul Bakker5121ce52009-01-03 21:22:43 +00004354 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004355 md_alg = mbedtls_ssl_md_alg_from_hash(ssl->in_msg[i]);
Simon Butcher99000142016-10-13 17:21:01 +01004356
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004357 if (md_alg == MBEDTLS_MD_NONE || mbedtls_ssl_set_calc_verify_md(ssl, ssl->in_msg[i])) {
4358 MBEDTLS_SSL_DEBUG_MSG(1, ("peer not adhering to requested sig_alg"
4359 " for verify message"));
4360 return MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00004361 }
4362
Simon Butcher99000142016-10-13 17:21:01 +01004363#if !defined(MBEDTLS_MD_SHA1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004364 if (MBEDTLS_MD_SHA1 == md_alg) {
Simon Butcher99000142016-10-13 17:21:01 +01004365 hash_start += 16;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004366 }
Simon Butcher99000142016-10-13 17:21:01 +01004367#endif
Paul Bakker926af752012-11-23 13:38:07 +01004368
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02004369 /* Info from md_alg will be used instead */
4370 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004371
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004372 i++;
4373
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004374 /*
4375 * Signature
4376 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004377 if ((pk_alg = mbedtls_ssl_pk_alg_from_sig(ssl->in_msg[i]))
4378 == MBEDTLS_PK_NONE) {
4379 MBEDTLS_SSL_DEBUG_MSG(1, ("peer not adhering to requested sig_alg"
4380 " for verify message"));
4381 return MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004382 }
4383
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004384 /*
4385 * Check the certificate's key type matches the signature alg
4386 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004387 if (!mbedtls_pk_can_do(peer_pk, pk_alg)) {
4388 MBEDTLS_SSL_DEBUG_MSG(1, ("sig_alg doesn't match cert key"));
4389 return MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004390 }
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004391
4392 i++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004393 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004394#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02004395 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004396 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
4397 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02004398 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02004399
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004400 if (i + 2 > ssl->in_hslen) {
4401 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate verify message"));
4402 return MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY;
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00004403 }
4404
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004405 sig_len = (ssl->in_msg[i] << 8) | ssl->in_msg[i+1];
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004406 i += 2;
Paul Bakker926af752012-11-23 13:38:07 +01004407
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004408 if (i + sig_len != ssl->in_hslen) {
4409 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate verify message"));
4410 return MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00004411 }
4412
Simon Butcher99000142016-10-13 17:21:01 +01004413 /* Calculate hash and verify signature */
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02004414 {
4415 size_t dummy_hlen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004416 ssl->handshake->calc_verify(ssl, hash, &dummy_hlen);
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02004417 }
Simon Butcher99000142016-10-13 17:21:01 +01004418
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004419 if ((ret = mbedtls_pk_verify(peer_pk,
4420 md_alg, hash_start, hashlen,
4421 ssl->in_msg + i, sig_len)) != 0) {
4422 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_verify", ret);
4423 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004424 }
4425
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004426 mbedtls_ssl_update_handshake_status(ssl);
Simon Butcher99000142016-10-13 17:21:01 +01004427
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004428 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate verify"));
Paul Bakker5121ce52009-01-03 21:22:43 +00004429
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004430 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004431}
Gilles Peskineeccd8882020-03-10 12:19:08 +01004432#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00004433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004434#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02004435MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004436static int ssl_write_new_session_ticket(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004437{
Janos Follath865b3eb2019-12-16 11:46:15 +00004438 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02004439 size_t tlen;
Manuel Pégourié-Gonnardb0394be2015-05-19 11:40:30 +02004440 uint32_t lifetime;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004441
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004442 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write new session ticket"));
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004444 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4445 ssl->out_msg[0] = MBEDTLS_SSL_HS_NEW_SESSION_TICKET;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004446
4447 /*
4448 * struct {
4449 * uint32 ticket_lifetime_hint;
4450 * opaque ticket<0..2^16-1>;
4451 * } NewSessionTicket;
4452 *
4453 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
4454 * 8 . 9 ticket_len (n)
4455 * 10 . 9+n ticket content
4456 */
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02004457
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004458 if ((ret = ssl->conf->f_ticket_write(ssl->conf->p_ticket,
4459 ssl->session_negotiate,
4460 ssl->out_msg + 10,
4461 ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN,
4462 &tlen, &lifetime)) != 0) {
4463 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_ticket_write", ret);
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02004464 tlen = 0;
4465 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004466
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004467 MBEDTLS_PUT_UINT32_BE(lifetime, ssl->out_msg, 4);
4468 MBEDTLS_PUT_UINT16_BE(tlen, ssl->out_msg, 8);
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02004469 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004470
Manuel Pégourié-Gonnard145dfcb2014-02-26 14:23:33 +01004471 /*
4472 * Morally equivalent to updating ssl->state, but NewSessionTicket and
4473 * ChangeCipherSpec share the same state.
4474 */
4475 ssl->handshake->new_session_ticket = 0;
4476
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004477 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
4478 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
4479 return ret;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004480 }
4481
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004482 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write new session ticket"));
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004483
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004484 return 0;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004485}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004486#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004487
Paul Bakker5121ce52009-01-03 21:22:43 +00004488/*
Paul Bakker1961b702013-01-25 14:49:24 +01004489 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00004490 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004491int mbedtls_ssl_handshake_server_step(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00004492{
4493 int ret = 0;
4494
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004495 if (ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL) {
4496 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4497 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004498
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004499 MBEDTLS_SSL_DEBUG_MSG(2, ("server state: %d", ssl->state));
Paul Bakker1961b702013-01-25 14:49:24 +01004500
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004501 if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) {
4502 return ret;
4503 }
Paul Bakker1961b702013-01-25 14:49:24 +01004504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004505#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004506 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4507 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING) {
4508 if ((ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
4509 return ret;
4510 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004511 }
Hanno Beckerbc2498a2018-08-28 10:13:29 +01004512#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004513
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004514 switch (ssl->state) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004515 case MBEDTLS_SSL_HELLO_REQUEST:
4516 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00004517 break;
4518
Paul Bakker1961b702013-01-25 14:49:24 +01004519 /*
4520 * <== ClientHello
4521 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004522 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004523 ret = ssl_parse_client_hello(ssl);
Paul Bakker5121ce52009-01-03 21:22:43 +00004524 break;
Paul Bakker1961b702013-01-25 14:49:24 +01004525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004526#if defined(MBEDTLS_SSL_PROTO_DTLS)
4527 case MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004528 return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED;
Manuel Pégourié-Gonnard579950c2014-09-29 17:47:33 +02004529#endif
4530
Paul Bakker1961b702013-01-25 14:49:24 +01004531 /*
4532 * ==> ServerHello
4533 * Certificate
4534 * ( ServerKeyExchange )
4535 * ( CertificateRequest )
4536 * ServerHelloDone
4537 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004538 case MBEDTLS_SSL_SERVER_HELLO:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004539 ret = ssl_write_server_hello(ssl);
Paul Bakker1961b702013-01-25 14:49:24 +01004540 break;
4541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004542 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004543 ret = mbedtls_ssl_write_certificate(ssl);
Paul Bakker1961b702013-01-25 14:49:24 +01004544 break;
4545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004546 case MBEDTLS_SSL_SERVER_KEY_EXCHANGE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004547 ret = ssl_write_server_key_exchange(ssl);
Paul Bakker1961b702013-01-25 14:49:24 +01004548 break;
4549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004550 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004551 ret = ssl_write_certificate_request(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_HELLO_DONE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004555 ret = ssl_write_server_hello_done(ssl);
Paul Bakker1961b702013-01-25 14:49:24 +01004556 break;
4557
4558 /*
4559 * <== ( Certificate/Alert )
4560 * ClientKeyExchange
4561 * ( CertificateVerify )
4562 * ChangeCipherSpec
4563 * Finished
4564 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004565 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004566 ret = mbedtls_ssl_parse_certificate(ssl);
Paul Bakker1961b702013-01-25 14:49:24 +01004567 break;
4568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004569 case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004570 ret = ssl_parse_client_key_exchange(ssl);
Paul Bakker1961b702013-01-25 14:49:24 +01004571 break;
4572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004573 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004574 ret = ssl_parse_certificate_verify(ssl);
Paul Bakker1961b702013-01-25 14:49:24 +01004575 break;
4576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004577 case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004578 ret = mbedtls_ssl_parse_change_cipher_spec(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_FINISHED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004582 ret = mbedtls_ssl_parse_finished(ssl);
Paul Bakker1961b702013-01-25 14:49:24 +01004583 break;
4584
4585 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004586 * ==> ( NewSessionTicket )
4587 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01004588 * Finished
4589 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004590 case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC:
4591#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004592 if (ssl->handshake->new_session_ticket != 0) {
4593 ret = ssl_write_new_session_ticket(ssl);
4594 } else
Paul Bakkera503a632013-08-14 13:48:06 +02004595#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004596 ret = mbedtls_ssl_write_change_cipher_spec(ssl);
Paul Bakker1961b702013-01-25 14:49:24 +01004597 break;
4598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004599 case MBEDTLS_SSL_SERVER_FINISHED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004600 ret = mbedtls_ssl_write_finished(ssl);
Paul Bakker1961b702013-01-25 14:49:24 +01004601 break;
4602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004603 case MBEDTLS_SSL_FLUSH_BUFFERS:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004604 MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004605 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Paul Bakker1961b702013-01-25 14:49:24 +01004606 break;
4607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004608 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004609 mbedtls_ssl_handshake_wrapup(ssl);
Paul Bakker1961b702013-01-25 14:49:24 +01004610 break;
4611
4612 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004613 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
4614 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Paul Bakker5121ce52009-01-03 21:22:43 +00004615 }
4616
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004617 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004618}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004619#endif /* MBEDTLS_SSL_SRV_C */