Coverage Report

Created: 2025-11-16 06:46

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/mbedtls/library/ssl_tls13_client.c
Line
Count
Source
1
/*
2
 *  TLS 1.3 client-side functions
3
 *
4
 *  Copyright The Mbed TLS Contributors
5
 *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6
 */
7
8
#include "common.h"
9
10
#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
11
12
#include <string.h>
13
14
#include "debug_internal.h"
15
#include "mbedtls/error.h"
16
#include "mbedtls/platform.h"
17
18
#include "ssl_misc.h"
19
#include "ssl_client.h"
20
#include "ssl_tls13_keys.h"
21
#include "ssl_debug_helpers.h"
22
#include "mbedtls/psa_util.h"
23
24
#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
25
/* Define a local translating function to save code size by not using too many
26
 * arguments in each translating place. */
27
static int local_err_translation(psa_status_t status)
28
0
{
29
0
    return psa_status_to_mbedtls(status, psa_to_ssl_errors,
30
0
                                 ARRAY_LENGTH(psa_to_ssl_errors),
31
0
                                 psa_generic_status_to_mbedtls);
32
0
}
33
0
#define PSA_TO_MBEDTLS_ERR(status) local_err_translation(status)
34
#endif
35
36
/* Write extensions */
37
38
/*
39
 * ssl_tls13_write_supported_versions_ext():
40
 *
41
 * struct {
42
 *      ProtocolVersion versions<2..254>;
43
 * } SupportedVersions;
44
 */
45
MBEDTLS_CHECK_RETURN_CRITICAL
46
static int ssl_tls13_write_supported_versions_ext(mbedtls_ssl_context *ssl,
47
                                                  unsigned char *buf,
48
                                                  unsigned char *end,
49
                                                  size_t *out_len)
50
0
{
51
0
    unsigned char *p = buf;
52
0
    unsigned char versions_len = (ssl->handshake->min_tls_version <=
53
0
                                  MBEDTLS_SSL_VERSION_TLS1_2) ? 4 : 2;
54
55
0
    *out_len = 0;
56
57
0
    MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, adding supported versions extension"));
58
59
    /* Check if we have space to write the extension:
60
     * - extension_type         (2 bytes)
61
     * - extension_data_length  (2 bytes)
62
     * - versions_length        (1 byte )
63
     * - versions               (2 or 4 bytes)
64
     */
65
0
    MBEDTLS_SSL_CHK_BUF_PTR(p, end, 5 + versions_len);
66
67
0
    MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, p, 0);
68
0
    MBEDTLS_PUT_UINT16_BE(versions_len + 1, p, 2);
69
0
    p += 4;
70
71
    /* Length of versions */
72
0
    *p++ = versions_len;
73
74
    /* Write values of supported versions.
75
     * They are defined by the configuration.
76
     * Currently, we advertise only TLS 1.3 or both TLS 1.3 and TLS 1.2.
77
     */
78
0
    mbedtls_ssl_write_version(p, MBEDTLS_SSL_TRANSPORT_STREAM,
79
0
                              MBEDTLS_SSL_VERSION_TLS1_3);
80
0
    MBEDTLS_SSL_DEBUG_MSG(3, ("supported version: [3:4]"));
81
82
83
0
    if (ssl->handshake->min_tls_version <= MBEDTLS_SSL_VERSION_TLS1_2) {
84
0
        mbedtls_ssl_write_version(p + 2, MBEDTLS_SSL_TRANSPORT_STREAM,
85
0
                                  MBEDTLS_SSL_VERSION_TLS1_2);
86
0
        MBEDTLS_SSL_DEBUG_MSG(3, ("supported version: [3:3]"));
87
0
    }
88
89
0
    *out_len = 5 + versions_len;
90
91
0
    mbedtls_ssl_tls13_set_hs_sent_ext_mask(
92
0
        ssl, MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS);
93
94
0
    return 0;
95
0
}
96
97
MBEDTLS_CHECK_RETURN_CRITICAL
98
static int ssl_tls13_parse_supported_versions_ext(mbedtls_ssl_context *ssl,
99
                                                  const unsigned char *buf,
100
                                                  const unsigned char *end)
101
0
{
102
0
    ((void) ssl);
103
104
0
    MBEDTLS_SSL_CHK_BUF_READ_PTR(buf, end, 2);
105
0
    if (mbedtls_ssl_read_version(buf, ssl->conf->transport) !=
106
0
        MBEDTLS_SSL_VERSION_TLS1_3) {
107
0
        MBEDTLS_SSL_DEBUG_MSG(1, ("unexpected version"));
108
109
0
        MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
110
0
                                     MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
111
0
        return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
112
0
    }
113
114
0
    if (&buf[2] != end) {
115
0
        MBEDTLS_SSL_DEBUG_MSG(
116
0
            1, ("supported_versions ext data length incorrect"));
117
0
        MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
118
0
                                     MBEDTLS_ERR_SSL_DECODE_ERROR);
119
0
        return MBEDTLS_ERR_SSL_DECODE_ERROR;
120
0
    }
121
122
0
    return 0;
123
0
}
124
125
#if defined(MBEDTLS_SSL_ALPN)
126
MBEDTLS_CHECK_RETURN_CRITICAL
127
static int ssl_tls13_parse_alpn_ext(mbedtls_ssl_context *ssl,
128
                                    const unsigned char *buf, size_t len)
129
0
{
130
0
    const unsigned char *p = buf;
131
0
    const unsigned char *end = buf + len;
132
0
    size_t protocol_name_list_len, protocol_name_len;
133
0
    const unsigned char *protocol_name_list_end;
134
135
    /* If we didn't send it, the server shouldn't send it */
136
0
    if (ssl->conf->alpn_list == NULL) {
137
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
138
0
    }
139
140
    /*
141
     * opaque ProtocolName<1..2^8-1>;
142
     *
143
     * struct {
144
     *     ProtocolName protocol_name_list<2..2^16-1>
145
     * } ProtocolNameList;
146
     *
147
     * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
148
     */
149
150
0
    MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
151
0
    protocol_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
152
0
    p += 2;
153
154
0
    MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, protocol_name_list_len);
155
0
    protocol_name_list_end = p + protocol_name_list_len;
156
157
0
    MBEDTLS_SSL_CHK_BUF_READ_PTR(p, protocol_name_list_end, 1);
158
0
    protocol_name_len = *p++;
159
160
    /* Check that the server chosen protocol was in our list and save it */
161
0
    MBEDTLS_SSL_CHK_BUF_READ_PTR(p, protocol_name_list_end, protocol_name_len);
162
0
    for (const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++) {
163
0
        if (protocol_name_len == strlen(*alpn) &&
164
0
            memcmp(p, *alpn, protocol_name_len) == 0) {
165
0
            ssl->alpn_chosen = *alpn;
166
0
            return 0;
167
0
        }
168
0
    }
169
170
0
    return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
171
0
}
172
#endif /* MBEDTLS_SSL_ALPN */
173
174
MBEDTLS_CHECK_RETURN_CRITICAL
175
static int ssl_tls13_reset_key_share(mbedtls_ssl_context *ssl)
176
0
{
177
0
    uint16_t group_id = ssl->handshake->offered_group_id;
178
179
0
    if (group_id == 0) {
180
0
        return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
181
0
    }
182
183
0
#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
184
0
    if (mbedtls_ssl_tls13_named_group_is_ecdhe(group_id) ||
185
0
        mbedtls_ssl_tls13_named_group_is_ffdh(group_id)) {
186
0
        int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
187
0
        psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
188
189
        /* Destroy generated private key. */
190
0
        status = psa_destroy_key(ssl->handshake->xxdh_psa_privkey);
191
0
        if (status != PSA_SUCCESS) {
192
0
            ret = PSA_TO_MBEDTLS_ERR(status);
193
0
            MBEDTLS_SSL_DEBUG_RET(1, "psa_destroy_key", ret);
194
0
            return ret;
195
0
        }
196
197
0
        ssl->handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
198
0
        return 0;
199
0
    } else
200
0
#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
201
0
    if (0 /* other KEMs? */) {
202
        /* Do something */
203
0
    }
204
205
0
    return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
206
0
}
207
208
/*
209
 * Functions for writing key_share extension.
210
 */
211
#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
212
MBEDTLS_CHECK_RETURN_CRITICAL
213
static int ssl_tls13_get_default_group_id(mbedtls_ssl_context *ssl,
214
                                          uint16_t *group_id)
215
0
{
216
0
    int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
217
218
219
0
#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
220
0
    const uint16_t *group_list = mbedtls_ssl_get_groups(ssl);
221
    /* Pick first available ECDHE group compatible with TLS 1.3 */
222
0
    if (group_list == NULL) {
223
0
        return MBEDTLS_ERR_SSL_BAD_CONFIG;
224
0
    }
225
226
0
    for (; *group_list != 0; group_list++) {
227
0
#if defined(PSA_WANT_ALG_ECDH)
228
0
        if ((mbedtls_ssl_get_psa_curve_info_from_tls_id(
229
0
                 *group_list, NULL, NULL) == PSA_SUCCESS) &&
230
0
            mbedtls_ssl_tls13_named_group_is_ecdhe(*group_list)) {
231
0
            *group_id = *group_list;
232
0
            return 0;
233
0
        }
234
0
#endif
235
0
#if defined(PSA_WANT_ALG_FFDH)
236
0
        if (mbedtls_ssl_tls13_named_group_is_ffdh(*group_list)) {
237
0
            *group_id = *group_list;
238
0
            return 0;
239
0
        }
240
0
#endif
241
0
    }
242
#else
243
    ((void) ssl);
244
    ((void) group_id);
245
#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH */
246
247
0
    return ret;
248
0
}
249
250
/*
251
 * ssl_tls13_write_key_share_ext
252
 *
253
 * Structure of key_share extension in ClientHello:
254
 *
255
 *  struct {
256
 *          NamedGroup group;
257
 *          opaque key_exchange<1..2^16-1>;
258
 *      } KeyShareEntry;
259
 *  struct {
260
 *          KeyShareEntry client_shares<0..2^16-1>;
261
 *      } KeyShareClientHello;
262
 */
263
MBEDTLS_CHECK_RETURN_CRITICAL
264
static int ssl_tls13_write_key_share_ext(mbedtls_ssl_context *ssl,
265
                                         unsigned char *buf,
266
                                         unsigned char *end,
267
                                         size_t *out_len)
268
0
{
269
0
    unsigned char *p = buf;
270
0
    unsigned char *client_shares; /* Start of client_shares */
271
0
    size_t client_shares_len;     /* Length of client_shares */
272
0
    uint16_t group_id;
273
0
    int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
274
275
0
    *out_len = 0;
276
277
    /* Check if we have space for header and length fields:
278
     * - extension_type         (2 bytes)
279
     * - extension_data_length  (2 bytes)
280
     * - client_shares_length   (2 bytes)
281
     */
282
0
    MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
283
0
    p += 6;
284
285
0
    MBEDTLS_SSL_DEBUG_MSG(3, ("client hello: adding key share extension"));
286
287
    /* HRR could already have requested something else. */
288
0
    group_id = ssl->handshake->offered_group_id;
289
0
    if (!mbedtls_ssl_tls13_named_group_is_ecdhe(group_id) &&
290
0
        !mbedtls_ssl_tls13_named_group_is_ffdh(group_id)) {
291
0
        MBEDTLS_SSL_PROC_CHK(ssl_tls13_get_default_group_id(ssl,
292
0
                                                            &group_id));
293
0
    }
294
295
    /*
296
     * Dispatch to type-specific key generation function.
297
     *
298
     * So far, we're only supporting ECDHE. With the introduction
299
     * of PQC KEMs, we'll want to have multiple branches, one per
300
     * type of KEM, and dispatch to the corresponding crypto. And
301
     * only one key share entry is allowed.
302
     */
303
0
    client_shares = p;
304
0
#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
305
0
    if (mbedtls_ssl_tls13_named_group_is_ecdhe(group_id) ||
306
0
        mbedtls_ssl_tls13_named_group_is_ffdh(group_id)) {
307
        /* Pointer to group */
308
0
        unsigned char *group = p;
309
        /* Length of key_exchange */
310
0
        size_t key_exchange_len = 0;
311
312
        /* Check there is space for header of KeyShareEntry
313
         * - group                  (2 bytes)
314
         * - key_exchange_length    (2 bytes)
315
         */
316
0
        MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4);
317
0
        p += 4;
318
0
        ret = mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange(
319
0
            ssl, group_id, p, end, &key_exchange_len);
320
0
        p += key_exchange_len;
321
0
        if (ret != 0) {
322
0
            MBEDTLS_SSL_DEBUG_MSG(1, ("client hello: failed generating xxdh key exchange"));
323
0
            return ret;
324
0
        }
325
326
        /* Write group */
327
0
        MBEDTLS_PUT_UINT16_BE(group_id, group, 0);
328
        /* Write key_exchange_length */
329
0
        MBEDTLS_PUT_UINT16_BE(key_exchange_len, group, 2);
330
0
    } else
331
0
#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH */
332
0
    if (0 /* other KEMs? */) {
333
        /* Do something */
334
0
    } else {
335
0
        return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
336
0
    }
337
338
    /* Length of client_shares */
339
0
    client_shares_len = p - client_shares;
340
0
    if (client_shares_len == 0) {
341
0
        MBEDTLS_SSL_DEBUG_MSG(1, ("No key share defined."));
342
0
        return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
343
0
    }
344
    /* Write extension_type */
345
0
    MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0);
346
    /* Write extension_data_length */
347
0
    MBEDTLS_PUT_UINT16_BE(client_shares_len + 2, buf, 2);
348
    /* Write client_shares_length */
349
0
    MBEDTLS_PUT_UINT16_BE(client_shares_len, buf, 4);
350
351
    /* Update offered_group_id field */
352
0
    ssl->handshake->offered_group_id = group_id;
353
354
    /* Output the total length of key_share extension. */
355
0
    *out_len = p - buf;
356
357
0
    MBEDTLS_SSL_DEBUG_BUF(
358
0
        3, "client hello, key_share extension", buf, *out_len);
359
360
0
    mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
361
362
0
cleanup:
363
364
0
    return ret;
365
0
}
366
#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
367
368
/*
369
 * ssl_tls13_parse_hrr_key_share_ext()
370
 *      Parse key_share extension in Hello Retry Request
371
 *
372
 * struct {
373
 *        NamedGroup selected_group;
374
 * } KeyShareHelloRetryRequest;
375
 */
376
MBEDTLS_CHECK_RETURN_CRITICAL
377
static int ssl_tls13_parse_hrr_key_share_ext(mbedtls_ssl_context *ssl,
378
                                             const unsigned char *buf,
379
                                             const unsigned char *end)
380
0
{
381
0
#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
382
0
    const unsigned char *p = buf;
383
0
    int selected_group;
384
0
    int found = 0;
385
386
0
    const uint16_t *group_list = mbedtls_ssl_get_groups(ssl);
387
0
    if (group_list == NULL) {
388
0
        return MBEDTLS_ERR_SSL_BAD_CONFIG;
389
0
    }
390
391
0
    MBEDTLS_SSL_DEBUG_BUF(3, "key_share extension", p, end - buf);
392
393
    /* Read selected_group */
394
0
    MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
395
0
    selected_group = MBEDTLS_GET_UINT16_BE(p, 0);
396
0
    MBEDTLS_SSL_DEBUG_MSG(3, ("selected_group ( %d )", selected_group));
397
398
    /* Upon receipt of this extension in a HelloRetryRequest, the client
399
     * MUST first verify that the selected_group field corresponds to a
400
     * group which was provided in the "supported_groups" extension in the
401
     * original ClientHello.
402
     * The supported_group was based on the info in ssl->conf->group_list.
403
     *
404
     * If the server provided a key share that was not sent in the ClientHello
405
     * then the client MUST abort the handshake with an "illegal_parameter" alert.
406
     */
407
0
    for (; *group_list != 0; group_list++) {
408
0
#if defined(PSA_WANT_ALG_ECDH)
409
0
        if (mbedtls_ssl_tls13_named_group_is_ecdhe(*group_list)) {
410
0
            if ((mbedtls_ssl_get_psa_curve_info_from_tls_id(
411
0
                     *group_list, NULL, NULL) == PSA_ERROR_NOT_SUPPORTED) ||
412
0
                *group_list != selected_group) {
413
0
                found = 1;
414
0
                break;
415
0
            }
416
0
        }
417
0
#endif /* PSA_WANT_ALG_ECDH */
418
0
#if defined(PSA_WANT_ALG_FFDH)
419
0
        if (mbedtls_ssl_tls13_named_group_is_ffdh(*group_list)) {
420
0
            found = 1;
421
0
            break;
422
0
        }
423
0
#endif /* PSA_WANT_ALG_FFDH */
424
0
    }
425
426
    /* Client MUST verify that the selected_group field does not
427
     * correspond to a group which was provided in the "key_share"
428
     * extension in the original ClientHello. If the server sent an
429
     * HRR message with a key share already provided in the
430
     * ClientHello then the client MUST abort the handshake with
431
     * an "illegal_parameter" alert.
432
     */
433
0
    if (found == 0 || selected_group == ssl->handshake->offered_group_id) {
434
0
        MBEDTLS_SSL_DEBUG_MSG(1, ("Invalid key share in HRR"));
435
0
        MBEDTLS_SSL_PEND_FATAL_ALERT(
436
0
            MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
437
0
            MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
438
0
        return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
439
0
    }
440
441
    /* Remember server's preference for next ClientHello */
442
0
    ssl->handshake->offered_group_id = selected_group;
443
444
0
    return 0;
445
#else /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH */
446
    (void) ssl;
447
    (void) buf;
448
    (void) end;
449
    return MBEDTLS_ERR_SSL_BAD_CONFIG;
450
#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH */
451
0
}
452
453
/*
454
 * ssl_tls13_parse_key_share_ext()
455
 *      Parse key_share extension in Server Hello
456
 *
457
 * struct {
458
 *        KeyShareEntry server_share;
459
 * } KeyShareServerHello;
460
 * struct {
461
 *        NamedGroup group;
462
 *        opaque key_exchange<1..2^16-1>;
463
 * } KeyShareEntry;
464
 */
465
MBEDTLS_CHECK_RETURN_CRITICAL
466
static int ssl_tls13_parse_key_share_ext(mbedtls_ssl_context *ssl,
467
                                         const unsigned char *buf,
468
                                         const unsigned char *end)
469
0
{
470
0
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
471
0
    const unsigned char *p = buf;
472
0
    uint16_t group, offered_group;
473
474
    /* ...
475
     * NamedGroup group; (2 bytes)
476
     * ...
477
     */
478
0
    MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
479
0
    group = MBEDTLS_GET_UINT16_BE(p, 0);
480
0
    p += 2;
481
482
    /* Check that the chosen group matches the one we offered. */
483
0
    offered_group = ssl->handshake->offered_group_id;
484
0
    if (offered_group != group) {
485
0
        MBEDTLS_SSL_DEBUG_MSG(
486
0
            1, ("Invalid server key share, our group %u, their group %u",
487
0
                (unsigned) offered_group, (unsigned) group));
488
0
        MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
489
0
                                     MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
490
0
        return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
491
0
    }
492
493
0
#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
494
0
    if (mbedtls_ssl_tls13_named_group_is_ecdhe(group) ||
495
0
        mbedtls_ssl_tls13_named_group_is_ffdh(group)) {
496
0
        MBEDTLS_SSL_DEBUG_MSG(2,
497
0
                              ("DHE group name: %s", mbedtls_ssl_named_group_to_str(group)));
498
0
        ret = mbedtls_ssl_tls13_read_public_xxdhe_share(ssl, p, end - p);
499
0
        if (ret != 0) {
500
0
            return ret;
501
0
        }
502
0
    } else
503
0
#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
504
0
    if (0 /* other KEMs? */) {
505
        /* Do something */
506
0
    } else {
507
0
        return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
508
0
    }
509
510
0
    return ret;
511
0
}
512
513
/*
514
 * ssl_tls13_parse_cookie_ext()
515
 *      Parse cookie extension in Hello Retry Request
516
 *
517
 * struct {
518
 *        opaque cookie<1..2^16-1>;
519
 * } Cookie;
520
 *
521
 * When sending a HelloRetryRequest, the server MAY provide a "cookie"
522
 * extension to the client (this is an exception to the usual rule that
523
 * the only extensions that may be sent are those that appear in the
524
 * ClientHello).  When sending the new ClientHello, the client MUST copy
525
 * the contents of the extension received in the HelloRetryRequest into
526
 * a "cookie" extension in the new ClientHello.  Clients MUST NOT use
527
 * cookies in their initial ClientHello in subsequent connections.
528
 */
529
MBEDTLS_CHECK_RETURN_CRITICAL
530
static int ssl_tls13_parse_cookie_ext(mbedtls_ssl_context *ssl,
531
                                      const unsigned char *buf,
532
                                      const unsigned char *end)
533
0
{
534
0
    uint16_t cookie_len;
535
0
    const unsigned char *p = buf;
536
0
    mbedtls_ssl_handshake_params *handshake = ssl->handshake;
537
538
    /* Retrieve length field of cookie */
539
0
    MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
540
0
    cookie_len = MBEDTLS_GET_UINT16_BE(p, 0);
541
0
    p += 2;
542
543
0
    MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, cookie_len);
544
0
    MBEDTLS_SSL_DEBUG_BUF(3, "cookie extension", p, cookie_len);
545
546
0
    mbedtls_free(handshake->cookie);
547
0
    handshake->cookie_len = 0;
548
0
    handshake->cookie = mbedtls_calloc(1, cookie_len);
549
0
    if (handshake->cookie == NULL) {
550
0
        MBEDTLS_SSL_DEBUG_MSG(1,
551
0
                              ("alloc failed ( %ud bytes )",
552
0
                               cookie_len));
553
0
        return MBEDTLS_ERR_SSL_ALLOC_FAILED;
554
0
    }
555
556
0
    memcpy(handshake->cookie, p, cookie_len);
557
0
    handshake->cookie_len = cookie_len;
558
559
0
    return 0;
560
0
}
561
562
MBEDTLS_CHECK_RETURN_CRITICAL
563
static int ssl_tls13_write_cookie_ext(mbedtls_ssl_context *ssl,
564
                                      unsigned char *buf,
565
                                      unsigned char *end,
566
                                      size_t *out_len)
567
0
{
568
0
    unsigned char *p = buf;
569
0
    *out_len = 0;
570
0
    mbedtls_ssl_handshake_params *handshake = ssl->handshake;
571
572
0
    if (handshake->cookie == NULL) {
573
0
        MBEDTLS_SSL_DEBUG_MSG(3, ("no cookie to send; skip extension"));
574
0
        return 0;
575
0
    }
576
577
0
    MBEDTLS_SSL_DEBUG_BUF(3, "client hello, cookie",
578
0
                          handshake->cookie,
579
0
                          handshake->cookie_len);
580
581
0
    MBEDTLS_SSL_CHK_BUF_PTR(p, end, handshake->cookie_len + 6);
582
583
0
    MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, adding cookie extension"));
584
585
0
    MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_COOKIE, p, 0);
586
0
    MBEDTLS_PUT_UINT16_BE(handshake->cookie_len + 2, p, 2);
587
0
    MBEDTLS_PUT_UINT16_BE(handshake->cookie_len, p, 4);
588
0
    p += 6;
589
590
    /* Cookie */
591
0
    memcpy(p, handshake->cookie, handshake->cookie_len);
592
593
0
    *out_len = handshake->cookie_len + 6;
594
595
0
    mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_COOKIE);
596
597
0
    return 0;
598
0
}
599
600
#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
601
/*
602
 * ssl_tls13_write_psk_key_exchange_modes_ext() structure:
603
 *
604
 * enum { psk_ke( 0 ), psk_dhe_ke( 1 ), ( 255 ) } PskKeyExchangeMode;
605
 *
606
 * struct {
607
 *     PskKeyExchangeMode ke_modes<1..255>;
608
 * } PskKeyExchangeModes;
609
 */
610
MBEDTLS_CHECK_RETURN_CRITICAL
611
static int ssl_tls13_write_psk_key_exchange_modes_ext(mbedtls_ssl_context *ssl,
612
                                                      unsigned char *buf,
613
                                                      unsigned char *end,
614
                                                      size_t *out_len)
615
0
{
616
0
    unsigned char *p = buf;
617
0
    int ke_modes_len = 0;
618
619
0
    ((void) ke_modes_len);
620
0
    *out_len = 0;
621
622
    /* Skip writing extension if no PSK key exchange mode
623
     * is enabled in the config.
624
     */
625
0
    if (!mbedtls_ssl_conf_tls13_is_some_psk_enabled(ssl)) {
626
0
        MBEDTLS_SSL_DEBUG_MSG(3, ("skip psk_key_exchange_modes extension"));
627
0
        return 0;
628
0
    }
629
630
    /* Require 7 bytes of data, otherwise fail,
631
     * even if extension might be shorter.
632
     */
633
0
    MBEDTLS_SSL_CHK_BUF_PTR(p, end, 7);
634
0
    MBEDTLS_SSL_DEBUG_MSG(
635
0
        3, ("client hello, adding psk_key_exchange_modes extension"));
636
637
0
    MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES, p, 0);
638
639
    /* Skip extension length (2 bytes) and
640
     * ke_modes length (1 byte) for now.
641
     */
642
0
    p += 5;
643
644
0
    if (mbedtls_ssl_conf_tls13_is_psk_ephemeral_enabled(ssl)) {
645
0
        *p++ = MBEDTLS_SSL_TLS1_3_PSK_MODE_ECDHE;
646
0
        ke_modes_len++;
647
648
0
        MBEDTLS_SSL_DEBUG_MSG(4, ("Adding PSK-ECDHE key exchange mode"));
649
0
    }
650
651
0
    if (mbedtls_ssl_conf_tls13_is_psk_enabled(ssl)) {
652
0
        *p++ = MBEDTLS_SSL_TLS1_3_PSK_MODE_PURE;
653
0
        ke_modes_len++;
654
655
0
        MBEDTLS_SSL_DEBUG_MSG(4, ("Adding pure PSK key exchange mode"));
656
0
    }
657
658
    /* Now write the extension and ke_modes length */
659
0
    MBEDTLS_PUT_UINT16_BE(ke_modes_len + 1, buf, 2);
660
0
    buf[4] = ke_modes_len;
661
662
0
    *out_len = p - buf;
663
664
0
    mbedtls_ssl_tls13_set_hs_sent_ext_mask(
665
0
        ssl, MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES);
666
667
0
    return 0;
668
0
}
669
670
#if defined(MBEDTLS_SSL_SESSION_TICKETS)
671
static psa_algorithm_t ssl_tls13_get_ciphersuite_hash_alg(int ciphersuite)
672
0
{
673
0
    const mbedtls_ssl_ciphersuite_t *ciphersuite_info = NULL;
674
0
    ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(ciphersuite);
675
676
0
    if (ciphersuite_info != NULL) {
677
0
        return mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
678
0
    }
679
680
0
    return PSA_ALG_NONE;
681
0
}
682
683
static int ssl_tls13_has_configured_ticket(mbedtls_ssl_context *ssl)
684
0
{
685
0
    mbedtls_ssl_session *session = ssl->session_negotiate;
686
0
    return ssl->handshake->resume &&
687
0
           session != NULL && session->ticket != NULL &&
688
0
           mbedtls_ssl_conf_tls13_is_kex_mode_enabled(
689
0
        ssl, mbedtls_ssl_tls13_session_get_ticket_flags(
690
0
            session, MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL));
691
0
}
692
693
#if defined(MBEDTLS_SSL_EARLY_DATA)
694
static int ssl_tls13_early_data_has_valid_ticket(mbedtls_ssl_context *ssl)
695
0
{
696
0
    mbedtls_ssl_session *session = ssl->session_negotiate;
697
0
    return ssl->handshake->resume &&
698
0
           session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
699
0
           mbedtls_ssl_tls13_session_ticket_allow_early_data(session) &&
700
0
           mbedtls_ssl_tls13_cipher_suite_is_offered(ssl, session->ciphersuite);
701
0
}
702
#endif
703
704
MBEDTLS_CHECK_RETURN_CRITICAL
705
static int ssl_tls13_ticket_get_identity(mbedtls_ssl_context *ssl,
706
                                         psa_algorithm_t *hash_alg,
707
                                         const unsigned char **identity,
708
                                         size_t *identity_len)
709
0
{
710
0
    mbedtls_ssl_session *session = ssl->session_negotiate;
711
712
0
    if (!ssl_tls13_has_configured_ticket(ssl)) {
713
0
        return -1;
714
0
    }
715
716
0
    *hash_alg = ssl_tls13_get_ciphersuite_hash_alg(session->ciphersuite);
717
0
    *identity = session->ticket;
718
0
    *identity_len = session->ticket_len;
719
0
    return 0;
720
0
}
721
722
MBEDTLS_CHECK_RETURN_CRITICAL
723
static int ssl_tls13_ticket_get_psk(mbedtls_ssl_context *ssl,
724
                                    psa_algorithm_t *hash_alg,
725
                                    const unsigned char **psk,
726
                                    size_t *psk_len)
727
0
{
728
729
0
    mbedtls_ssl_session *session = ssl->session_negotiate;
730
731
0
    if (!ssl_tls13_has_configured_ticket(ssl)) {
732
0
        return -1;
733
0
    }
734
735
0
    *hash_alg = ssl_tls13_get_ciphersuite_hash_alg(session->ciphersuite);
736
0
    *psk = session->resumption_key;
737
0
    *psk_len = session->resumption_key_len;
738
739
0
    return 0;
740
0
}
741
#endif /* MBEDTLS_SSL_SESSION_TICKETS */
742
743
MBEDTLS_CHECK_RETURN_CRITICAL
744
static int ssl_tls13_psk_get_identity(mbedtls_ssl_context *ssl,
745
                                      psa_algorithm_t *hash_alg,
746
                                      const unsigned char **identity,
747
                                      size_t *identity_len)
748
0
{
749
750
0
    if (!mbedtls_ssl_conf_has_static_psk(ssl->conf)) {
751
0
        return -1;
752
0
    }
753
754
0
    *hash_alg = PSA_ALG_SHA_256;
755
0
    *identity = ssl->conf->psk_identity;
756
0
    *identity_len = ssl->conf->psk_identity_len;
757
0
    return 0;
758
0
}
759
760
MBEDTLS_CHECK_RETURN_CRITICAL
761
static int ssl_tls13_psk_get_psk(mbedtls_ssl_context *ssl,
762
                                 psa_algorithm_t *hash_alg,
763
                                 const unsigned char **psk,
764
                                 size_t *psk_len)
765
0
{
766
767
0
    if (!mbedtls_ssl_conf_has_static_psk(ssl->conf)) {
768
0
        return -1;
769
0
    }
770
771
0
    *hash_alg = PSA_ALG_SHA_256;
772
0
    *psk = ssl->conf->psk;
773
0
    *psk_len = ssl->conf->psk_len;
774
0
    return 0;
775
0
}
776
777
static int ssl_tls13_get_configured_psk_count(mbedtls_ssl_context *ssl)
778
0
{
779
0
    int configured_psk_count = 0;
780
0
#if defined(MBEDTLS_SSL_SESSION_TICKETS)
781
0
    if (ssl_tls13_has_configured_ticket(ssl)) {
782
0
        MBEDTLS_SSL_DEBUG_MSG(3, ("Ticket is configured"));
783
0
        configured_psk_count++;
784
0
    }
785
0
#endif
786
0
    if (mbedtls_ssl_conf_has_static_psk(ssl->conf)) {
787
0
        MBEDTLS_SSL_DEBUG_MSG(3, ("PSK is configured"));
788
0
        configured_psk_count++;
789
0
    }
790
0
    return configured_psk_count;
791
0
}
792
793
MBEDTLS_CHECK_RETURN_CRITICAL
794
static int ssl_tls13_write_identity(mbedtls_ssl_context *ssl,
795
                                    unsigned char *buf,
796
                                    unsigned char *end,
797
                                    const unsigned char *identity,
798
                                    size_t identity_len,
799
                                    uint32_t obfuscated_ticket_age,
800
                                    size_t *out_len)
801
0
{
802
0
    ((void) ssl);
803
0
    *out_len = 0;
804
805
    /*
806
     * - identity_len           (2 bytes)
807
     * - identity               (psk_identity_len bytes)
808
     * - obfuscated_ticket_age  (4 bytes)
809
     */
810
0
    MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6 + identity_len);
811
812
0
    MBEDTLS_PUT_UINT16_BE(identity_len, buf, 0);
813
0
    memcpy(buf + 2, identity, identity_len);
814
0
    MBEDTLS_PUT_UINT32_BE(obfuscated_ticket_age, buf, 2 + identity_len);
815
816
0
    MBEDTLS_SSL_DEBUG_BUF(4, "write identity", buf, 6 + identity_len);
817
818
0
    *out_len = 6 + identity_len;
819
820
0
    return 0;
821
0
}
822
823
MBEDTLS_CHECK_RETURN_CRITICAL
824
static int ssl_tls13_write_binder(mbedtls_ssl_context *ssl,
825
                                  unsigned char *buf,
826
                                  unsigned char *end,
827
                                  int psk_type,
828
                                  psa_algorithm_t hash_alg,
829
                                  const unsigned char *psk,
830
                                  size_t psk_len,
831
                                  size_t *out_len)
832
0
{
833
0
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
834
0
    unsigned char binder_len;
835
0
    unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
836
0
    size_t transcript_len = 0;
837
838
0
    *out_len = 0;
839
840
0
    binder_len = PSA_HASH_LENGTH(hash_alg);
841
842
    /*
843
     * - binder_len           (1 bytes)
844
     * - binder               (binder_len bytes)
845
     */
846
0
    MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 1 + binder_len);
847
848
0
    buf[0] = binder_len;
849
850
    /* Get current state of handshake transcript. */
851
0
    ret = mbedtls_ssl_get_handshake_transcript(
852
0
        ssl, mbedtls_md_type_from_psa_alg(hash_alg),
853
0
        transcript, sizeof(transcript), &transcript_len);
854
0
    if (ret != 0) {
855
0
        return ret;
856
0
    }
857
858
0
    ret = mbedtls_ssl_tls13_create_psk_binder(ssl, hash_alg,
859
0
                                              psk, psk_len, psk_type,
860
0
                                              transcript, buf + 1);
861
0
    if (ret != 0) {
862
0
        MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_create_psk_binder", ret);
863
0
        return ret;
864
0
    }
865
0
    MBEDTLS_SSL_DEBUG_BUF(4, "write binder", buf, 1 + binder_len);
866
867
0
    *out_len = 1 + binder_len;
868
869
0
    return 0;
870
0
}
871
872
/*
873
 * mbedtls_ssl_tls13_write_identities_of_pre_shared_key_ext() structure:
874
 *
875
 * struct {
876
 *   opaque identity<1..2^16-1>;
877
 *   uint32 obfuscated_ticket_age;
878
 * } PskIdentity;
879
 *
880
 * opaque PskBinderEntry<32..255>;
881
 *
882
 * struct {
883
 *   PskIdentity identities<7..2^16-1>;
884
 *   PskBinderEntry binders<33..2^16-1>;
885
 * } OfferedPsks;
886
 *
887
 * struct {
888
 *   select (Handshake.msg_type) {
889
 *      case client_hello: OfferedPsks;
890
 *      ...
891
 *   };
892
 * } PreSharedKeyExtension;
893
 *
894
 */
895
int mbedtls_ssl_tls13_write_identities_of_pre_shared_key_ext(
896
    mbedtls_ssl_context *ssl, unsigned char *buf, unsigned char *end,
897
    size_t *out_len, size_t *binders_len)
898
0
{
899
0
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
900
0
    int configured_psk_count = 0;
901
0
    unsigned char *p = buf;
902
0
    psa_algorithm_t hash_alg = PSA_ALG_NONE;
903
0
    const unsigned char *identity;
904
0
    size_t identity_len;
905
0
    size_t l_binders_len = 0;
906
0
    size_t output_len;
907
908
0
    *out_len = 0;
909
0
    *binders_len = 0;
910
911
    /* Check if we have any PSKs to offer. If no, skip pre_shared_key */
912
0
    configured_psk_count = ssl_tls13_get_configured_psk_count(ssl);
913
0
    if (configured_psk_count == 0) {
914
0
        MBEDTLS_SSL_DEBUG_MSG(3, ("skip pre_shared_key extensions"));
915
0
        return 0;
916
0
    }
917
918
0
    MBEDTLS_SSL_DEBUG_MSG(4, ("Pre-configured PSK number = %d",
919
0
                              configured_psk_count));
920
921
    /* Check if we have space to write the extension, binders included.
922
     * - extension_type         (2 bytes)
923
     * - extension_data_len     (2 bytes)
924
     * - identities_len         (2 bytes)
925
     */
926
0
    MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
927
0
    p += 6;
928
929
0
#if defined(MBEDTLS_SSL_SESSION_TICKETS)
930
0
    if (ssl_tls13_ticket_get_identity(
931
0
            ssl, &hash_alg, &identity, &identity_len) == 0) {
932
0
#if defined(MBEDTLS_HAVE_TIME)
933
0
        mbedtls_ms_time_t now = mbedtls_ms_time();
934
0
        mbedtls_ssl_session *session = ssl->session_negotiate;
935
        /* The ticket age has been checked to be smaller than the
936
         * `ticket_lifetime` in ssl_prepare_client_hello() which is smaller than
937
         * 7 days (enforced in ssl_tls13_parse_new_session_ticket()) . Thus the
938
         * cast to `uint32_t` of the ticket age is safe. */
939
0
        uint32_t obfuscated_ticket_age =
940
0
            (uint32_t) (now - session->ticket_reception_time);
941
0
        obfuscated_ticket_age += session->ticket_age_add;
942
943
0
        ret = ssl_tls13_write_identity(ssl, p, end,
944
0
                                       identity, identity_len,
945
0
                                       obfuscated_ticket_age,
946
0
                                       &output_len);
947
#else
948
        ret = ssl_tls13_write_identity(ssl, p, end, identity, identity_len,
949
                                       0, &output_len);
950
#endif /* MBEDTLS_HAVE_TIME */
951
0
        if (ret != 0) {
952
0
            return ret;
953
0
        }
954
955
0
        p += output_len;
956
0
        l_binders_len += 1 + PSA_HASH_LENGTH(hash_alg);
957
0
    }
958
0
#endif /* MBEDTLS_SSL_SESSION_TICKETS */
959
960
0
    if (ssl_tls13_psk_get_identity(
961
0
            ssl, &hash_alg, &identity, &identity_len) == 0) {
962
963
0
        ret = ssl_tls13_write_identity(ssl, p, end, identity, identity_len, 0,
964
0
                                       &output_len);
965
0
        if (ret != 0) {
966
0
            return ret;
967
0
        }
968
969
0
        p += output_len;
970
0
        l_binders_len += 1 + PSA_HASH_LENGTH(hash_alg);
971
0
    }
972
973
0
    MBEDTLS_SSL_DEBUG_MSG(3,
974
0
                          ("client hello, adding pre_shared_key extension, "
975
0
                           "omitting PSK binder list"));
976
977
    /* Take into account the two bytes for the length of the binders. */
978
0
    l_binders_len += 2;
979
    /* Check if there is enough space for binders */
980
0
    MBEDTLS_SSL_CHK_BUF_PTR(p, end, l_binders_len);
981
982
    /*
983
     * - extension_type         (2 bytes)
984
     * - extension_data_len     (2 bytes)
985
     * - identities_len         (2 bytes)
986
     */
987
0
    MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_PRE_SHARED_KEY, buf, 0);
988
0
    MBEDTLS_PUT_UINT16_BE(p - buf - 4 + l_binders_len, buf, 2);
989
0
    MBEDTLS_PUT_UINT16_BE(p - buf - 6, buf, 4);
990
991
0
    *out_len = (p - buf) + l_binders_len;
992
0
    *binders_len = l_binders_len;
993
994
0
    MBEDTLS_SSL_DEBUG_BUF(3, "pre_shared_key identities", buf, p - buf);
995
996
0
    return 0;
997
0
}
998
999
int mbedtls_ssl_tls13_write_binders_of_pre_shared_key_ext(
1000
    mbedtls_ssl_context *ssl, unsigned char *buf, unsigned char *end)
1001
0
{
1002
0
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1003
0
    unsigned char *p = buf;
1004
0
    psa_algorithm_t hash_alg = PSA_ALG_NONE;
1005
0
    const unsigned char *psk;
1006
0
    size_t psk_len;
1007
0
    size_t output_len;
1008
1009
    /* Check if we have space to write binders_len.
1010
     * - binders_len         (2 bytes)
1011
     */
1012
0
    MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
1013
0
    p += 2;
1014
1015
0
#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1016
0
    if (ssl_tls13_ticket_get_psk(ssl, &hash_alg, &psk, &psk_len) == 0) {
1017
1018
0
        ret = ssl_tls13_write_binder(ssl, p, end,
1019
0
                                     MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION,
1020
0
                                     hash_alg, psk, psk_len,
1021
0
                                     &output_len);
1022
0
        if (ret != 0) {
1023
0
            return ret;
1024
0
        }
1025
0
        p += output_len;
1026
0
    }
1027
0
#endif /* MBEDTLS_SSL_SESSION_TICKETS */
1028
1029
0
    if (ssl_tls13_psk_get_psk(ssl, &hash_alg, &psk, &psk_len) == 0) {
1030
1031
0
        ret = ssl_tls13_write_binder(ssl, p, end,
1032
0
                                     MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL,
1033
0
                                     hash_alg, psk, psk_len,
1034
0
                                     &output_len);
1035
0
        if (ret != 0) {
1036
0
            return ret;
1037
0
        }
1038
0
        p += output_len;
1039
0
    }
1040
1041
0
    MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, adding PSK binder list."));
1042
1043
    /*
1044
     * - binders_len         (2 bytes)
1045
     */
1046
0
    MBEDTLS_PUT_UINT16_BE(p - buf - 2, buf, 0);
1047
1048
0
    MBEDTLS_SSL_DEBUG_BUF(3, "pre_shared_key binders", buf, p - buf);
1049
1050
0
    mbedtls_ssl_tls13_set_hs_sent_ext_mask(
1051
0
        ssl, MBEDTLS_TLS_EXT_PRE_SHARED_KEY);
1052
1053
0
    return 0;
1054
0
}
1055
1056
/*
1057
 * struct {
1058
 *   opaque identity<1..2^16-1>;
1059
 *   uint32 obfuscated_ticket_age;
1060
 * } PskIdentity;
1061
 *
1062
 * opaque PskBinderEntry<32..255>;
1063
 *
1064
 * struct {
1065
 *
1066
 *   select (Handshake.msg_type) {
1067
 *         ...
1068
 *         case server_hello: uint16 selected_identity;
1069
 *   };
1070
 *
1071
 * } PreSharedKeyExtension;
1072
 *
1073
 */
1074
MBEDTLS_CHECK_RETURN_CRITICAL
1075
static int ssl_tls13_parse_server_pre_shared_key_ext(mbedtls_ssl_context *ssl,
1076
                                                     const unsigned char *buf,
1077
                                                     const unsigned char *end)
1078
0
{
1079
0
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1080
0
    int selected_identity;
1081
0
    const unsigned char *psk;
1082
0
    size_t psk_len;
1083
0
    psa_algorithm_t hash_alg;
1084
1085
0
    MBEDTLS_SSL_CHK_BUF_READ_PTR(buf, end, 2);
1086
0
    selected_identity = MBEDTLS_GET_UINT16_BE(buf, 0);
1087
0
    ssl->handshake->selected_identity = (uint16_t) selected_identity;
1088
1089
0
    MBEDTLS_SSL_DEBUG_MSG(3, ("selected_identity = %d", selected_identity));
1090
1091
0
    if (selected_identity >= ssl_tls13_get_configured_psk_count(ssl)) {
1092
0
        MBEDTLS_SSL_DEBUG_MSG(1, ("Invalid PSK identity."));
1093
1094
0
        MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1095
0
                                     MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1096
0
        return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1097
0
    }
1098
1099
0
#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1100
0
    if (selected_identity == 0 && ssl_tls13_has_configured_ticket(ssl)) {
1101
0
        ret = ssl_tls13_ticket_get_psk(ssl, &hash_alg, &psk, &psk_len);
1102
0
    } else
1103
0
#endif
1104
0
    if (mbedtls_ssl_conf_has_static_psk(ssl->conf)) {
1105
0
        ret = ssl_tls13_psk_get_psk(ssl, &hash_alg, &psk, &psk_len);
1106
0
    } else {
1107
0
        MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1108
0
        return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1109
0
    }
1110
0
    if (ret != 0) {
1111
0
        return ret;
1112
0
    }
1113
1114
0
    if (mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ssl->handshake->ciphersuite_info->mac)
1115
0
        != hash_alg) {
1116
0
        MBEDTLS_SSL_DEBUG_MSG(
1117
0
            1, ("Invalid ciphersuite for external psk."));
1118
1119
0
        MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1120
0
                                     MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1121
0
        return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1122
0
    }
1123
1124
0
    ret = mbedtls_ssl_set_hs_psk(ssl, psk, psk_len);
1125
0
    if (ret != 0) {
1126
0
        MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_set_hs_psk", ret);
1127
0
        return ret;
1128
0
    }
1129
1130
0
    return 0;
1131
0
}
1132
#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
1133
1134
int mbedtls_ssl_tls13_write_client_hello_exts(mbedtls_ssl_context *ssl,
1135
                                              unsigned char *buf,
1136
                                              unsigned char *end,
1137
                                              size_t *out_len)
1138
24
{
1139
24
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1140
24
    unsigned char *p = buf;
1141
24
    size_t ext_len;
1142
1143
24
    *out_len = 0;
1144
1145
24
    ret = mbedtls_ssl_tls13_crypto_init(ssl);
1146
24
    if (ret != 0) {
1147
24
        return ret;
1148
24
    }
1149
1150
    /* Write supported_versions extension
1151
     *
1152
     * Supported Versions Extension is mandatory with TLS 1.3.
1153
     */
1154
0
    ret = ssl_tls13_write_supported_versions_ext(ssl, p, end, &ext_len);
1155
0
    if (ret != 0) {
1156
0
        return ret;
1157
0
    }
1158
0
    p += ext_len;
1159
1160
    /* Echo the cookie if the server provided one in its preceding
1161
     * HelloRetryRequest message.
1162
     */
1163
0
    ret = ssl_tls13_write_cookie_ext(ssl, p, end, &ext_len);
1164
0
    if (ret != 0) {
1165
0
        return ret;
1166
0
    }
1167
0
    p += ext_len;
1168
1169
0
#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
1170
0
    ret = mbedtls_ssl_tls13_write_record_size_limit_ext(
1171
0
        ssl, p, end, &ext_len);
1172
0
    if (ret != 0) {
1173
0
        return ret;
1174
0
    }
1175
0
    p += ext_len;
1176
0
#endif
1177
1178
0
#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
1179
0
    if (mbedtls_ssl_conf_tls13_is_some_ephemeral_enabled(ssl)) {
1180
0
        ret = ssl_tls13_write_key_share_ext(ssl, p, end, &ext_len);
1181
0
        if (ret != 0) {
1182
0
            return ret;
1183
0
        }
1184
0
        p += ext_len;
1185
0
    }
1186
0
#endif
1187
1188
0
#if defined(MBEDTLS_SSL_EARLY_DATA)
1189
    /* In the first ClientHello, write the early data indication extension if
1190
     * necessary and update the early data state.
1191
     * If an HRR has been received and thus we are currently writing the
1192
     * second ClientHello, the second ClientHello must not contain an early
1193
     * data extension and the early data state must stay as it is:
1194
     * MBEDTLS_SSL_EARLY_DATA_STATE_NO_IND_SENT or
1195
     * MBEDTLS_SSL_EARLY_DATA_STATE_REJECTED.
1196
     */
1197
0
    if (!ssl->handshake->hello_retry_request_flag) {
1198
0
        if (mbedtls_ssl_conf_tls13_is_some_psk_enabled(ssl) &&
1199
0
            ssl_tls13_early_data_has_valid_ticket(ssl) &&
1200
0
            ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_ENABLED) {
1201
0
            ret = mbedtls_ssl_tls13_write_early_data_ext(
1202
0
                ssl, 0, p, end, &ext_len);
1203
0
            if (ret != 0) {
1204
0
                return ret;
1205
0
            }
1206
0
            p += ext_len;
1207
1208
0
            ssl->early_data_state = MBEDTLS_SSL_EARLY_DATA_STATE_IND_SENT;
1209
0
        } else {
1210
0
            ssl->early_data_state = MBEDTLS_SSL_EARLY_DATA_STATE_NO_IND_SENT;
1211
0
        }
1212
0
    }
1213
0
#endif /* MBEDTLS_SSL_EARLY_DATA */
1214
1215
0
#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
1216
    /* For PSK-based key exchange we need the pre_shared_key extension
1217
     * and the psk_key_exchange_modes extension.
1218
     *
1219
     * The pre_shared_key extension MUST be the last extension in the
1220
     * ClientHello. Servers MUST check that it is the last extension and
1221
     * otherwise fail the handshake with an "illegal_parameter" alert.
1222
     *
1223
     * Add the psk_key_exchange_modes extension.
1224
     */
1225
0
    ret = ssl_tls13_write_psk_key_exchange_modes_ext(ssl, p, end, &ext_len);
1226
0
    if (ret != 0) {
1227
0
        return ret;
1228
0
    }
1229
0
    p += ext_len;
1230
0
#endif
1231
1232
0
    *out_len = p - buf;
1233
1234
0
    return 0;
1235
0
}
1236
1237
int mbedtls_ssl_tls13_finalize_client_hello(mbedtls_ssl_context *ssl)
1238
0
{
1239
0
    ((void) ssl);
1240
1241
0
#if defined(MBEDTLS_SSL_EARLY_DATA)
1242
0
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1243
0
    psa_algorithm_t hash_alg = PSA_ALG_NONE;
1244
0
    const unsigned char *psk;
1245
0
    size_t psk_len;
1246
0
    const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
1247
1248
0
    if (ssl->early_data_state == MBEDTLS_SSL_EARLY_DATA_STATE_IND_SENT) {
1249
0
        MBEDTLS_SSL_DEBUG_MSG(
1250
0
            1, ("Set hs psk for early data when writing the first psk"));
1251
1252
0
        ret = ssl_tls13_ticket_get_psk(ssl, &hash_alg, &psk, &psk_len);
1253
0
        if (ret != 0) {
1254
0
            MBEDTLS_SSL_DEBUG_RET(
1255
0
                1, "ssl_tls13_ticket_get_psk", ret);
1256
0
            return ret;
1257
0
        }
1258
1259
0
        ret = mbedtls_ssl_set_hs_psk(ssl, psk, psk_len);
1260
0
        if (ret  != 0) {
1261
0
            MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_set_hs_psk", ret);
1262
0
            return ret;
1263
0
        }
1264
1265
        /*
1266
         * Early data are going to be encrypted using the ciphersuite
1267
         * associated with the pre-shared key used for the handshake.
1268
         * Note that if the server rejects early data, the handshake
1269
         * based on the pre-shared key may complete successfully
1270
         * with a selected ciphersuite different from the ciphersuite
1271
         * associated with the pre-shared key. Only the hashes of the
1272
         * two ciphersuites have to be the same. In that case, the
1273
         * encrypted handshake data and application data are
1274
         * encrypted using a different ciphersuite than the one used for
1275
         * the rejected early data.
1276
         */
1277
0
        ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(
1278
0
            ssl->session_negotiate->ciphersuite);
1279
0
        ssl->handshake->ciphersuite_info = ciphersuite_info;
1280
1281
        /* Enable psk and psk_ephemeral to make stage early happy */
1282
0
        ssl->handshake->key_exchange_mode =
1283
0
            MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL;
1284
1285
        /* Start the TLS 1.3 key schedule:
1286
         *     Set the PSK and derive early secret.
1287
         */
1288
0
        ret = mbedtls_ssl_tls13_key_schedule_stage_early(ssl);
1289
0
        if (ret != 0) {
1290
0
            MBEDTLS_SSL_DEBUG_RET(
1291
0
                1, "mbedtls_ssl_tls13_key_schedule_stage_early", ret);
1292
0
            return ret;
1293
0
        }
1294
1295
        /* Derive early data key material */
1296
0
        ret = mbedtls_ssl_tls13_compute_early_transform(ssl);
1297
0
        if (ret != 0) {
1298
0
            MBEDTLS_SSL_DEBUG_RET(
1299
0
                1, "mbedtls_ssl_tls13_compute_early_transform", ret);
1300
0
            return ret;
1301
0
        }
1302
1303
0
#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1304
0
        mbedtls_ssl_handshake_set_state(
1305
0
            ssl, MBEDTLS_SSL_CLIENT_CCS_AFTER_CLIENT_HELLO);
1306
#else
1307
        MBEDTLS_SSL_DEBUG_MSG(
1308
            1, ("Switch to early data keys for outbound traffic"));
1309
        mbedtls_ssl_set_outbound_transform(
1310
            ssl, ssl->handshake->transform_earlydata);
1311
        ssl->early_data_state = MBEDTLS_SSL_EARLY_DATA_STATE_CAN_WRITE;
1312
#endif
1313
0
    }
1314
0
#endif /* MBEDTLS_SSL_EARLY_DATA */
1315
0
    return 0;
1316
0
}
1317
/*
1318
 * Functions for parsing and processing Server Hello
1319
 */
1320
1321
/**
1322
 * \brief Detect if the ServerHello contains a supported_versions extension
1323
 *        or not.
1324
 *
1325
 * \param[in] ssl  SSL context
1326
 * \param[in] buf  Buffer containing the ServerHello message
1327
 * \param[in] end  End of the buffer containing the ServerHello message
1328
 *
1329
 * \return 0 if the ServerHello does not contain a supported_versions extension
1330
 * \return 1 if the ServerHello contains a supported_versions extension
1331
 * \return A negative value if an error occurred while parsing the ServerHello.
1332
 */
1333
MBEDTLS_CHECK_RETURN_CRITICAL
1334
static int ssl_tls13_is_supported_versions_ext_present(
1335
    mbedtls_ssl_context *ssl,
1336
    const unsigned char *buf,
1337
    const unsigned char *end)
1338
0
{
1339
0
    const unsigned char *p = buf;
1340
0
    size_t legacy_session_id_echo_len;
1341
0
    const unsigned char *supported_versions_data;
1342
0
    const unsigned char *supported_versions_data_end;
1343
1344
    /*
1345
     * Check there is enough data to access the legacy_session_id_echo vector
1346
     * length:
1347
     * - legacy_version                 2 bytes
1348
     * - random                         MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes
1349
     * - legacy_session_id_echo length  1 byte
1350
     */
1351
0
    MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 3);
1352
0
    p += MBEDTLS_SERVER_HELLO_RANDOM_LEN + 2;
1353
0
    legacy_session_id_echo_len = *p;
1354
1355
    /*
1356
     * Jump to the extensions, jumping over:
1357
     * - legacy_session_id_echo     (legacy_session_id_echo_len + 1) bytes
1358
     * - cipher_suite               2 bytes
1359
     * - legacy_compression_method  1 byte
1360
     */
1361
0
    MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, legacy_session_id_echo_len + 4);
1362
0
    p += legacy_session_id_echo_len + 4;
1363
1364
0
    return mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts(
1365
0
        ssl, p, end,
1366
0
        &supported_versions_data, &supported_versions_data_end);
1367
0
}
1368
1369
/* Returns a negative value on failure, and otherwise
1370
 * - 1 if the last eight bytes of the ServerHello random bytes indicate that
1371
 *     the server is TLS 1.3 capable but negotiating TLS 1.2 or below.
1372
 * - 0 otherwise
1373
 */
1374
MBEDTLS_CHECK_RETURN_CRITICAL
1375
static int ssl_tls13_is_downgrade_negotiation(mbedtls_ssl_context *ssl,
1376
                                              const unsigned char *buf,
1377
                                              const unsigned char *end)
1378
0
{
1379
    /* First seven bytes of the magic downgrade strings, see RFC 8446 4.1.3 */
1380
0
    static const unsigned char magic_downgrade_string[] =
1381
0
    { 0x44, 0x4F, 0x57, 0x4E, 0x47, 0x52, 0x44 };
1382
0
    const unsigned char *last_eight_bytes_of_random;
1383
0
    unsigned char last_byte_of_random;
1384
1385
0
    MBEDTLS_SSL_CHK_BUF_READ_PTR(buf, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 2);
1386
0
    last_eight_bytes_of_random = buf + 2 + MBEDTLS_SERVER_HELLO_RANDOM_LEN - 8;
1387
1388
0
    if (memcmp(last_eight_bytes_of_random,
1389
0
               magic_downgrade_string,
1390
0
               sizeof(magic_downgrade_string)) == 0) {
1391
0
        last_byte_of_random = last_eight_bytes_of_random[7];
1392
0
        return last_byte_of_random == 0 ||
1393
0
               last_byte_of_random == 1;
1394
0
    }
1395
1396
0
    return 0;
1397
0
}
1398
1399
/* Returns a negative value on failure, and otherwise
1400
 * - SSL_SERVER_HELLO or
1401
 * - SSL_SERVER_HELLO_HRR
1402
 * to indicate which message is expected and to be parsed next.
1403
 */
1404
0
#define SSL_SERVER_HELLO 0
1405
0
#define SSL_SERVER_HELLO_HRR 1
1406
MBEDTLS_CHECK_RETURN_CRITICAL
1407
static int ssl_server_hello_is_hrr(mbedtls_ssl_context *ssl,
1408
                                   const unsigned char *buf,
1409
                                   const unsigned char *end)
1410
0
{
1411
1412
    /* Check whether this message is a HelloRetryRequest ( HRR ) message.
1413
     *
1414
     * Server Hello and HRR are only distinguished by Random set to the
1415
     * special value of the SHA-256 of "HelloRetryRequest".
1416
     *
1417
     * struct {
1418
     *    ProtocolVersion legacy_version = 0x0303;
1419
     *    Random random;
1420
     *    opaque legacy_session_id_echo<0..32>;
1421
     *    CipherSuite cipher_suite;
1422
     *    uint8 legacy_compression_method = 0;
1423
     *    Extension extensions<6..2^16-1>;
1424
     * } ServerHello;
1425
     *
1426
     */
1427
0
    MBEDTLS_SSL_CHK_BUF_READ_PTR(
1428
0
        buf, end, 2 + sizeof(mbedtls_ssl_tls13_hello_retry_request_magic));
1429
1430
0
    if (memcmp(buf + 2, mbedtls_ssl_tls13_hello_retry_request_magic,
1431
0
               sizeof(mbedtls_ssl_tls13_hello_retry_request_magic)) == 0) {
1432
0
        return SSL_SERVER_HELLO_HRR;
1433
0
    }
1434
1435
0
    return SSL_SERVER_HELLO;
1436
0
}
1437
1438
/*
1439
 * Returns a negative value on failure, and otherwise
1440
 * - SSL_SERVER_HELLO or
1441
 * - SSL_SERVER_HELLO_HRR or
1442
 * - SSL_SERVER_HELLO_TLS1_2
1443
 */
1444
0
#define SSL_SERVER_HELLO_TLS1_2 2
1445
MBEDTLS_CHECK_RETURN_CRITICAL
1446
static int ssl_tls13_preprocess_server_hello(mbedtls_ssl_context *ssl,
1447
                                             const unsigned char *buf,
1448
                                             const unsigned char *end)
1449
0
{
1450
0
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1451
0
    mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1452
1453
0
    MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_is_supported_versions_ext_present(
1454
0
                                 ssl, buf, end));
1455
1456
0
    if (ret == 0) {
1457
0
        MBEDTLS_SSL_PROC_CHK_NEG(
1458
0
            ssl_tls13_is_downgrade_negotiation(ssl, buf, end));
1459
1460
        /* If the server is negotiating TLS 1.2 or below and:
1461
         * . we did not propose TLS 1.2 or
1462
         * . the server responded it is TLS 1.3 capable but negotiating a lower
1463
         *   version of the protocol and thus we are under downgrade attack
1464
         * abort the handshake with an "illegal parameter" alert.
1465
         */
1466
0
        if (handshake->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 || ret) {
1467
0
            MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1468
0
                                         MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1469
0
            return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1470
0
        }
1471
1472
        /*
1473
         * Version 1.2 of the protocol has been negotiated, set the
1474
         * ssl->keep_current_message flag for the ServerHello to be kept and
1475
         * parsed as a TLS 1.2 ServerHello. We also change ssl->tls_version to
1476
         * MBEDTLS_SSL_VERSION_TLS1_2 thus from now on mbedtls_ssl_handshake_step()
1477
         * will dispatch to the TLS 1.2 state machine.
1478
         */
1479
0
        ssl->keep_current_message = 1;
1480
0
        ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
1481
0
        MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
1482
0
                                 ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
1483
0
                                 buf, (size_t) (end - buf)));
1484
1485
0
        if (mbedtls_ssl_conf_tls13_is_some_ephemeral_enabled(ssl)) {
1486
0
            ret = ssl_tls13_reset_key_share(ssl);
1487
0
            if (ret != 0) {
1488
0
                return ret;
1489
0
            }
1490
0
        }
1491
1492
0
        return SSL_SERVER_HELLO_TLS1_2;
1493
0
    }
1494
1495
0
    ssl->session_negotiate->tls_version = ssl->tls_version;
1496
0
    ssl->session_negotiate->endpoint = ssl->conf->endpoint;
1497
1498
0
    handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
1499
1500
0
    ret = ssl_server_hello_is_hrr(ssl, buf, end);
1501
0
    switch (ret) {
1502
0
        case SSL_SERVER_HELLO:
1503
0
            MBEDTLS_SSL_DEBUG_MSG(2, ("received ServerHello message"));
1504
0
            break;
1505
0
        case SSL_SERVER_HELLO_HRR:
1506
0
            MBEDTLS_SSL_DEBUG_MSG(2, ("received HelloRetryRequest message"));
1507
            /* If a client receives a second HelloRetryRequest in the same
1508
             * connection (i.e., where the ClientHello was itself in response
1509
             * to a HelloRetryRequest), it MUST abort the handshake with an
1510
             * "unexpected_message" alert.
1511
             */
1512
0
            if (handshake->hello_retry_request_flag) {
1513
0
                MBEDTLS_SSL_DEBUG_MSG(1, ("Multiple HRRs received"));
1514
0
                MBEDTLS_SSL_PEND_FATAL_ALERT(
1515
0
                    MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
1516
0
                    MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE);
1517
0
                return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
1518
0
            }
1519
            /*
1520
             * Clients must abort the handshake with an "illegal_parameter"
1521
             * alert if the HelloRetryRequest would not result in any change
1522
             * in the ClientHello.
1523
             * In a PSK only key exchange that what we expect.
1524
             */
1525
0
            if (!mbedtls_ssl_conf_tls13_is_some_ephemeral_enabled(ssl)) {
1526
0
                MBEDTLS_SSL_DEBUG_MSG(1,
1527
0
                                      ("Unexpected HRR in pure PSK key exchange."));
1528
0
                MBEDTLS_SSL_PEND_FATAL_ALERT(
1529
0
                    MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1530
0
                    MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1531
0
                return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1532
0
            }
1533
1534
0
            handshake->hello_retry_request_flag = 1;
1535
1536
0
            break;
1537
0
    }
1538
1539
0
cleanup:
1540
1541
0
    return ret;
1542
0
}
1543
1544
MBEDTLS_CHECK_RETURN_CRITICAL
1545
static int ssl_tls13_check_server_hello_session_id_echo(mbedtls_ssl_context *ssl,
1546
                                                        const unsigned char **buf,
1547
                                                        const unsigned char *end)
1548
0
{
1549
0
    const unsigned char *p = *buf;
1550
0
    size_t legacy_session_id_echo_len;
1551
1552
0
    MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
1553
0
    legacy_session_id_echo_len = *p++;
1554
1555
0
    MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, legacy_session_id_echo_len);
1556
1557
    /* legacy_session_id_echo */
1558
0
    if (ssl->session_negotiate->id_len != legacy_session_id_echo_len ||
1559
0
        memcmp(ssl->session_negotiate->id, p, legacy_session_id_echo_len) != 0) {
1560
0
        MBEDTLS_SSL_DEBUG_BUF(3, "Expected Session ID",
1561
0
                              ssl->session_negotiate->id,
1562
0
                              ssl->session_negotiate->id_len);
1563
0
        MBEDTLS_SSL_DEBUG_BUF(3, "Received Session ID", p,
1564
0
                              legacy_session_id_echo_len);
1565
1566
0
        MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1567
0
                                     MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1568
1569
0
        return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1570
0
    }
1571
1572
0
    p += legacy_session_id_echo_len;
1573
0
    *buf = p;
1574
1575
0
    MBEDTLS_SSL_DEBUG_BUF(3, "Session ID", ssl->session_negotiate->id,
1576
0
                          ssl->session_negotiate->id_len);
1577
0
    return 0;
1578
0
}
1579
1580
/* Parse ServerHello message and configure context
1581
 *
1582
 * struct {
1583
 *    ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1584
 *    Random random;
1585
 *    opaque legacy_session_id_echo<0..32>;
1586
 *    CipherSuite cipher_suite;
1587
 *    uint8 legacy_compression_method = 0;
1588
 *    Extension extensions<6..2^16-1>;
1589
 * } ServerHello;
1590
 */
1591
MBEDTLS_CHECK_RETURN_CRITICAL
1592
static int ssl_tls13_parse_server_hello(mbedtls_ssl_context *ssl,
1593
                                        const unsigned char *buf,
1594
                                        const unsigned char *end,
1595
                                        int is_hrr)
1596
0
{
1597
0
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1598
0
    const unsigned char *p = buf;
1599
0
    mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1600
0
    size_t extensions_len;
1601
0
    const unsigned char *extensions_end;
1602
0
    uint16_t cipher_suite;
1603
0
    const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
1604
0
    int fatal_alert = 0;
1605
0
    uint32_t allowed_extensions_mask;
1606
0
    int hs_msg_type = is_hrr ? MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST :
1607
0
                      MBEDTLS_SSL_HS_SERVER_HELLO;
1608
1609
    /*
1610
     * Check there is space for minimal fields
1611
     *
1612
     * - legacy_version             ( 2 bytes)
1613
     * - random                     (MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes)
1614
     * - legacy_session_id_echo     ( 1 byte ), minimum size
1615
     * - cipher_suite               ( 2 bytes)
1616
     * - legacy_compression_method  ( 1 byte )
1617
     */
1618
0
    MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 6);
1619
1620
0
    MBEDTLS_SSL_DEBUG_BUF(4, "server hello", p, end - p);
1621
0
    MBEDTLS_SSL_DEBUG_BUF(3, "server hello, version", p, 2);
1622
1623
    /* ...
1624
     * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1625
     * ...
1626
     * with ProtocolVersion defined as:
1627
     * uint16 ProtocolVersion;
1628
     */
1629
0
    if (mbedtls_ssl_read_version(p, ssl->conf->transport) !=
1630
0
        MBEDTLS_SSL_VERSION_TLS1_2) {
1631
0
        MBEDTLS_SSL_DEBUG_MSG(1, ("Unsupported version of TLS."));
1632
0
        MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1633
0
                                     MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
1634
0
        ret = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
1635
0
        goto cleanup;
1636
0
    }
1637
0
    p += 2;
1638
1639
    /* ...
1640
     * Random random;
1641
     * ...
1642
     * with Random defined as:
1643
     * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
1644
     */
1645
0
    if (!is_hrr) {
1646
0
        memcpy(&handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN], p,
1647
0
               MBEDTLS_SERVER_HELLO_RANDOM_LEN);
1648
0
        MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes",
1649
0
                              p, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
1650
0
    }
1651
0
    p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
1652
1653
    /* ...
1654
     * opaque legacy_session_id_echo<0..32>;
1655
     * ...
1656
     */
1657
0
    if (ssl_tls13_check_server_hello_session_id_echo(ssl, &p, end) != 0) {
1658
0
        fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
1659
0
        goto cleanup;
1660
0
    }
1661
1662
    /* ...
1663
     * CipherSuite cipher_suite;
1664
     * ...
1665
     * with CipherSuite defined as:
1666
     * uint8 CipherSuite[2];
1667
     */
1668
0
    MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
1669
0
    cipher_suite = MBEDTLS_GET_UINT16_BE(p, 0);
1670
0
    p += 2;
1671
1672
1673
0
    ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(cipher_suite);
1674
    /*
1675
     * Check whether this ciphersuite is valid and offered.
1676
     */
1677
0
    if ((mbedtls_ssl_validate_ciphersuite(ssl, ciphersuite_info,
1678
0
                                          ssl->tls_version,
1679
0
                                          ssl->tls_version) != 0) ||
1680
0
        !mbedtls_ssl_tls13_cipher_suite_is_offered(ssl, cipher_suite)) {
1681
0
        fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
1682
0
    }
1683
    /*
1684
     * If we received an HRR before and that the proposed selected
1685
     * ciphersuite in this server hello is not the same as the one
1686
     * proposed in the HRR, we abort the handshake and send an
1687
     * "illegal_parameter" alert.
1688
     */
1689
0
    else if ((!is_hrr) && handshake->hello_retry_request_flag &&
1690
0
             (cipher_suite != ssl->session_negotiate->ciphersuite)) {
1691
0
        fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
1692
0
    }
1693
1694
0
    if (fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER) {
1695
0
        MBEDTLS_SSL_DEBUG_MSG(1, ("invalid ciphersuite(%04x) parameter",
1696
0
                                  cipher_suite));
1697
0
        goto cleanup;
1698
0
    }
1699
1700
    /* Configure ciphersuites */
1701
0
    mbedtls_ssl_optimize_checksum(ssl, ciphersuite_info);
1702
1703
0
    handshake->ciphersuite_info = ciphersuite_info;
1704
0
    MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, chosen ciphersuite: ( %04x ) - %s",
1705
0
                              cipher_suite, ciphersuite_info->name));
1706
1707
0
#if defined(MBEDTLS_HAVE_TIME)
1708
0
    ssl->session_negotiate->start = mbedtls_time(NULL);
1709
0
#endif /* MBEDTLS_HAVE_TIME */
1710
1711
    /* ...
1712
     * uint8 legacy_compression_method = 0;
1713
     * ...
1714
     */
1715
0
    MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
1716
0
    if (p[0] != MBEDTLS_SSL_COMPRESS_NULL) {
1717
0
        MBEDTLS_SSL_DEBUG_MSG(1, ("bad legacy compression method"));
1718
0
        fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
1719
0
        goto cleanup;
1720
0
    }
1721
0
    p++;
1722
1723
    /* ...
1724
     * Extension extensions<6..2^16-1>;
1725
     * ...
1726
     * struct {
1727
     *      ExtensionType extension_type; (2 bytes)
1728
     *      opaque extension_data<0..2^16-1>;
1729
     * } Extension;
1730
     */
1731
0
    MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
1732
0
    extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
1733
0
    p += 2;
1734
1735
    /* Check extensions do not go beyond the buffer of data. */
1736
0
    MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
1737
0
    extensions_end = p + extensions_len;
1738
1739
0
    MBEDTLS_SSL_DEBUG_BUF(3, "server hello extensions", p, extensions_len);
1740
1741
0
    handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
1742
0
    allowed_extensions_mask = is_hrr ?
1743
0
                              MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_HRR :
1744
0
                              MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_SH;
1745
1746
0
    while (p < extensions_end) {
1747
0
        unsigned int extension_type;
1748
0
        size_t extension_data_len;
1749
0
        const unsigned char *extension_data_end;
1750
1751
0
        MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
1752
0
        extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
1753
0
        extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
1754
0
        p += 4;
1755
1756
0
        MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
1757
0
        extension_data_end = p + extension_data_len;
1758
1759
0
        ret = mbedtls_ssl_tls13_check_received_extension(
1760
0
            ssl, hs_msg_type, extension_type, allowed_extensions_mask);
1761
0
        if (ret != 0) {
1762
0
            return ret;
1763
0
        }
1764
1765
0
        switch (extension_type) {
1766
0
            case MBEDTLS_TLS_EXT_COOKIE:
1767
1768
0
                ret = ssl_tls13_parse_cookie_ext(ssl,
1769
0
                                                 p, extension_data_end);
1770
0
                if (ret != 0) {
1771
0
                    MBEDTLS_SSL_DEBUG_RET(1,
1772
0
                                          "ssl_tls13_parse_cookie_ext",
1773
0
                                          ret);
1774
0
                    goto cleanup;
1775
0
                }
1776
0
                break;
1777
1778
0
            case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
1779
0
                ret = ssl_tls13_parse_supported_versions_ext(ssl,
1780
0
                                                             p,
1781
0
                                                             extension_data_end);
1782
0
                if (ret != 0) {
1783
0
                    goto cleanup;
1784
0
                }
1785
0
                break;
1786
1787
0
#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
1788
0
            case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
1789
0
                MBEDTLS_SSL_DEBUG_MSG(3, ("found pre_shared_key extension"));
1790
1791
0
                if ((ret = ssl_tls13_parse_server_pre_shared_key_ext(
1792
0
                         ssl, p, extension_data_end)) != 0) {
1793
0
                    MBEDTLS_SSL_DEBUG_RET(
1794
0
                        1, ("ssl_tls13_parse_server_pre_shared_key_ext"), ret);
1795
0
                    return ret;
1796
0
                }
1797
0
                break;
1798
0
#endif
1799
1800
0
            case MBEDTLS_TLS_EXT_KEY_SHARE:
1801
0
                MBEDTLS_SSL_DEBUG_MSG(3, ("found key_shares extension"));
1802
0
                if (!mbedtls_ssl_conf_tls13_is_some_ephemeral_enabled(ssl)) {
1803
0
                    fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
1804
0
                    goto cleanup;
1805
0
                }
1806
1807
0
                if (is_hrr) {
1808
0
                    ret = ssl_tls13_parse_hrr_key_share_ext(ssl,
1809
0
                                                            p, extension_data_end);
1810
0
                } else {
1811
0
                    ret = ssl_tls13_parse_key_share_ext(ssl,
1812
0
                                                        p, extension_data_end);
1813
0
                }
1814
0
                if (ret != 0) {
1815
0
                    MBEDTLS_SSL_DEBUG_RET(1,
1816
0
                                          "ssl_tls13_parse_key_share_ext",
1817
0
                                          ret);
1818
0
                    goto cleanup;
1819
0
                }
1820
0
                break;
1821
1822
0
            default:
1823
0
                ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1824
0
                goto cleanup;
1825
0
        }
1826
1827
0
        p += extension_data_len;
1828
0
    }
1829
1830
0
    MBEDTLS_SSL_PRINT_EXTS(3, hs_msg_type, handshake->received_extensions);
1831
1832
0
cleanup:
1833
1834
0
    if (fatal_alert == MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT) {
1835
0
        MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1836
0
                                     MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION);
1837
0
        ret = MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
1838
0
    } else if (fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER) {
1839
0
        MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1840
0
                                     MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1841
0
        ret = MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1842
0
    }
1843
0
    return ret;
1844
0
}
1845
1846
#if defined(MBEDTLS_DEBUG_C)
1847
static const char *ssl_tls13_get_kex_mode_str(int mode)
1848
0
{
1849
0
    switch (mode) {
1850
0
        case MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK:
1851
0
            return "psk";
1852
0
        case MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL:
1853
0
            return "ephemeral";
1854
0
        case MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL:
1855
0
            return "psk_ephemeral";
1856
0
        default:
1857
0
            return "unknown mode";
1858
0
    }
1859
0
}
1860
#endif /* MBEDTLS_DEBUG_C */
1861
1862
MBEDTLS_CHECK_RETURN_CRITICAL
1863
static int ssl_tls13_postprocess_server_hello(mbedtls_ssl_context *ssl)
1864
0
{
1865
0
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1866
0
    mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1867
1868
    /* Determine the key exchange mode:
1869
     * 1) If both the pre_shared_key and key_share extensions were received
1870
     *    then the key exchange mode is PSK with EPHEMERAL.
1871
     * 2) If only the pre_shared_key extension was received then the key
1872
     *    exchange mode is PSK-only.
1873
     * 3) If only the key_share extension was received then the key
1874
     *    exchange mode is EPHEMERAL-only.
1875
     */
1876
0
    switch (handshake->received_extensions &
1877
0
            (MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
1878
0
             MBEDTLS_SSL_EXT_MASK(KEY_SHARE))) {
1879
        /* Only the pre_shared_key extension was received */
1880
0
        case MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY):
1881
0
            handshake->key_exchange_mode =
1882
0
                MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
1883
0
            break;
1884
1885
        /* Only the key_share extension was received */
1886
0
        case MBEDTLS_SSL_EXT_MASK(KEY_SHARE):
1887
0
            handshake->key_exchange_mode =
1888
0
                MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
1889
0
            break;
1890
1891
        /* Both the pre_shared_key and key_share extensions were received */
1892
0
        case (MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
1893
0
              MBEDTLS_SSL_EXT_MASK(KEY_SHARE)):
1894
0
            handshake->key_exchange_mode =
1895
0
                MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
1896
0
            break;
1897
1898
        /* Neither pre_shared_key nor key_share extension was received */
1899
0
        default:
1900
0
            MBEDTLS_SSL_DEBUG_MSG(1, ("Unknown key exchange."));
1901
0
            ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1902
0
            goto cleanup;
1903
0
    }
1904
1905
0
    if (!mbedtls_ssl_conf_tls13_is_kex_mode_enabled(
1906
0
            ssl, handshake->key_exchange_mode)) {
1907
0
        ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1908
0
        MBEDTLS_SSL_DEBUG_MSG(
1909
0
            2, ("Key exchange mode(%s) is not supported.",
1910
0
                ssl_tls13_get_kex_mode_str(handshake->key_exchange_mode)));
1911
0
        goto cleanup;
1912
0
    }
1913
1914
0
    MBEDTLS_SSL_DEBUG_MSG(
1915
0
        3, ("Selected key exchange mode: %s",
1916
0
            ssl_tls13_get_kex_mode_str(handshake->key_exchange_mode)));
1917
1918
    /* Start the TLS 1.3 key scheduling if not already done.
1919
     *
1920
     * If we proposed early data then we have already derived an
1921
     * early secret using the selected PSK and its associated hash.
1922
     * It means that if the negotiated key exchange mode is psk or
1923
     * psk_ephemeral, we have already correctly computed the
1924
     * early secret and thus we do not do it again. In all other
1925
     * cases we compute it here.
1926
     */
1927
0
#if defined(MBEDTLS_SSL_EARLY_DATA)
1928
0
    if (ssl->early_data_state == MBEDTLS_SSL_EARLY_DATA_STATE_NO_IND_SENT ||
1929
0
        handshake->key_exchange_mode ==
1930
0
        MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL)
1931
0
#endif
1932
0
    {
1933
0
        ret = mbedtls_ssl_tls13_key_schedule_stage_early(ssl);
1934
0
        if (ret != 0) {
1935
0
            MBEDTLS_SSL_DEBUG_RET(
1936
0
                1, "mbedtls_ssl_tls13_key_schedule_stage_early", ret);
1937
0
            goto cleanup;
1938
0
        }
1939
0
    }
1940
1941
0
    ret = mbedtls_ssl_tls13_compute_handshake_transform(ssl);
1942
0
    if (ret != 0) {
1943
0
        MBEDTLS_SSL_DEBUG_RET(1,
1944
0
                              "mbedtls_ssl_tls13_compute_handshake_transform",
1945
0
                              ret);
1946
0
        goto cleanup;
1947
0
    }
1948
1949
0
    mbedtls_ssl_set_inbound_transform(ssl, handshake->transform_handshake);
1950
0
    MBEDTLS_SSL_DEBUG_MSG(1, ("Switch to handshake keys for inbound traffic"));
1951
0
    ssl->session_in = ssl->session_negotiate;
1952
1953
0
cleanup:
1954
0
    if (ret != 0) {
1955
0
        MBEDTLS_SSL_PEND_FATAL_ALERT(
1956
0
            MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1957
0
            MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
1958
0
    }
1959
1960
0
    return ret;
1961
0
}
1962
1963
MBEDTLS_CHECK_RETURN_CRITICAL
1964
static int ssl_tls13_postprocess_hrr(mbedtls_ssl_context *ssl)
1965
0
{
1966
0
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1967
1968
0
    mbedtls_ssl_session_reset_msg_layer(ssl, 0);
1969
1970
    /*
1971
     * We are going to re-generate a shared secret corresponding to the group
1972
     * selected by the server, which is different from the group for which we
1973
     * generated a shared secret in the first client hello.
1974
     * Thus, reset the shared secret.
1975
     */
1976
0
    ret = ssl_tls13_reset_key_share(ssl);
1977
0
    if (ret != 0) {
1978
0
        return ret;
1979
0
    }
1980
1981
0
    ssl->session_negotiate->ciphersuite = ssl->handshake->ciphersuite_info->id;
1982
1983
0
#if defined(MBEDTLS_SSL_EARLY_DATA)
1984
0
    if (ssl->early_data_state != MBEDTLS_SSL_EARLY_DATA_STATE_NO_IND_SENT) {
1985
0
        ssl->early_data_state = MBEDTLS_SSL_EARLY_DATA_STATE_REJECTED;
1986
0
    }
1987
0
#endif
1988
1989
0
    return 0;
1990
0
}
1991
1992
/*
1993
 * Wait and parse ServerHello handshake message.
1994
 * Handler for MBEDTLS_SSL_SERVER_HELLO
1995
 */
1996
MBEDTLS_CHECK_RETURN_CRITICAL
1997
static int ssl_tls13_process_server_hello(mbedtls_ssl_context *ssl)
1998
0
{
1999
0
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2000
0
    unsigned char *buf = NULL;
2001
0
    size_t buf_len = 0;
2002
0
    int is_hrr = 0;
2003
2004
0
    MBEDTLS_SSL_DEBUG_MSG(2, ("=> %s", __func__));
2005
2006
0
    MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
2007
0
                             ssl, MBEDTLS_SSL_HS_SERVER_HELLO, &buf, &buf_len));
2008
2009
0
    ret = ssl_tls13_preprocess_server_hello(ssl, buf, buf + buf_len);
2010
0
    if (ret < 0) {
2011
0
        goto cleanup;
2012
0
    } else {
2013
0
        is_hrr = (ret == SSL_SERVER_HELLO_HRR);
2014
0
    }
2015
2016
0
    if (ret == SSL_SERVER_HELLO_TLS1_2) {
2017
0
        ret = 0;
2018
0
        goto cleanup;
2019
0
    }
2020
2021
0
    MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_server_hello(ssl, buf,
2022
0
                                                      buf + buf_len,
2023
0
                                                      is_hrr));
2024
0
    if (is_hrr) {
2025
0
        MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_reset_transcript_for_hrr(ssl));
2026
0
    }
2027
2028
0
    MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
2029
0
                             ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, buf_len));
2030
2031
0
    if (is_hrr) {
2032
0
        MBEDTLS_SSL_PROC_CHK(ssl_tls13_postprocess_hrr(ssl));
2033
0
#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2034
        /* If not offering early data, the client sends a dummy CCS record
2035
         * immediately before its second flight. This may either be before
2036
         * its second ClientHello or before its encrypted handshake flight.
2037
         */
2038
0
        mbedtls_ssl_handshake_set_state(
2039
0
            ssl, MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO);
2040
#else
2041
        mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
2042
#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
2043
0
    } else {
2044
0
        MBEDTLS_SSL_PROC_CHK(ssl_tls13_postprocess_server_hello(ssl));
2045
0
        mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
2046
0
    }
2047
2048
0
cleanup:
2049
0
    MBEDTLS_SSL_DEBUG_MSG(2, ("<= %s ( %s )", __func__,
2050
0
                              is_hrr ? "HelloRetryRequest" : "ServerHello"));
2051
0
    return ret;
2052
0
}
2053
2054
/*
2055
 *
2056
 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
2057
 *
2058
 * The EncryptedExtensions message contains any extensions which
2059
 * should be protected, i.e., any which are not needed to establish
2060
 * the cryptographic context.
2061
 */
2062
2063
/* Parse EncryptedExtensions message
2064
 * struct {
2065
 *     Extension extensions<0..2^16-1>;
2066
 * } EncryptedExtensions;
2067
 */
2068
MBEDTLS_CHECK_RETURN_CRITICAL
2069
static int ssl_tls13_parse_encrypted_extensions(mbedtls_ssl_context *ssl,
2070
                                                const unsigned char *buf,
2071
                                                const unsigned char *end)
2072
0
{
2073
0
    int ret = 0;
2074
0
    size_t extensions_len;
2075
0
    const unsigned char *p = buf;
2076
0
    const unsigned char *extensions_end;
2077
0
    mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2078
2079
0
    MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
2080
0
    extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
2081
0
    p += 2;
2082
2083
0
    MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
2084
0
    extensions_end = p + extensions_len;
2085
2086
0
    MBEDTLS_SSL_DEBUG_BUF(3, "encrypted extensions", p, extensions_len);
2087
2088
0
    handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
2089
2090
0
    while (p < extensions_end) {
2091
0
        unsigned int extension_type;
2092
0
        size_t extension_data_len;
2093
2094
        /*
2095
         * struct {
2096
         *     ExtensionType extension_type; (2 bytes)
2097
         *     opaque extension_data<0..2^16-1>;
2098
         * } Extension;
2099
         */
2100
0
        MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
2101
0
        extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
2102
0
        extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
2103
0
        p += 4;
2104
2105
0
        MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
2106
2107
0
        ret = mbedtls_ssl_tls13_check_received_extension(
2108
0
            ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, extension_type,
2109
0
            MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_EE);
2110
0
        if (ret != 0) {
2111
0
            return ret;
2112
0
        }
2113
2114
0
        switch (extension_type) {
2115
0
#if defined(MBEDTLS_SSL_ALPN)
2116
0
            case MBEDTLS_TLS_EXT_ALPN:
2117
0
                MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
2118
2119
0
                if ((ret = ssl_tls13_parse_alpn_ext(
2120
0
                         ssl, p, (size_t) extension_data_len)) != 0) {
2121
0
                    return ret;
2122
0
                }
2123
2124
0
                break;
2125
0
#endif /* MBEDTLS_SSL_ALPN */
2126
2127
0
#if defined(MBEDTLS_SSL_EARLY_DATA)
2128
0
            case MBEDTLS_TLS_EXT_EARLY_DATA:
2129
2130
0
                if (extension_data_len != 0) {
2131
                    /* The message must be empty. */
2132
0
                    MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
2133
0
                                                 MBEDTLS_ERR_SSL_DECODE_ERROR);
2134
0
                    return MBEDTLS_ERR_SSL_DECODE_ERROR;
2135
0
                }
2136
2137
0
                break;
2138
0
#endif /* MBEDTLS_SSL_EARLY_DATA */
2139
2140
0
#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
2141
0
            case MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT:
2142
0
                MBEDTLS_SSL_DEBUG_MSG(3, ("found record_size_limit extension"));
2143
2144
0
                ret = mbedtls_ssl_tls13_parse_record_size_limit_ext(
2145
0
                    ssl, p, p + extension_data_len);
2146
0
                if (ret != 0) {
2147
0
                    MBEDTLS_SSL_DEBUG_RET(
2148
0
                        1, ("mbedtls_ssl_tls13_parse_record_size_limit_ext"), ret);
2149
0
                    return ret;
2150
0
                }
2151
0
                break;
2152
0
#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
2153
2154
0
            default:
2155
0
                MBEDTLS_SSL_PRINT_EXT(
2156
0
                    3, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2157
0
                    extension_type, "( ignored )");
2158
0
                break;
2159
0
        }
2160
2161
0
        p += extension_data_len;
2162
0
    }
2163
2164
0
    if ((handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(RECORD_SIZE_LIMIT)) &&
2165
0
        (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(MAX_FRAGMENT_LENGTH))) {
2166
0
        MBEDTLS_SSL_DEBUG_MSG(3,
2167
0
                              (
2168
0
                                  "Record size limit extension cannot be used with max fragment length extension"));
2169
0
        MBEDTLS_SSL_PEND_FATAL_ALERT(
2170
0
            MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
2171
0
            MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
2172
0
        return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
2173
0
    }
2174
2175
0
    MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2176
0
                           handshake->received_extensions);
2177
2178
    /* Check that we consumed all the message. */
2179
0
    if (p != end) {
2180
0
        MBEDTLS_SSL_DEBUG_MSG(1, ("EncryptedExtension lengths misaligned"));
2181
0
        MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
2182
0
                                     MBEDTLS_ERR_SSL_DECODE_ERROR);
2183
0
        return MBEDTLS_ERR_SSL_DECODE_ERROR;
2184
0
    }
2185
2186
0
    return ret;
2187
0
}
2188
2189
MBEDTLS_CHECK_RETURN_CRITICAL
2190
static int ssl_tls13_process_encrypted_extensions(mbedtls_ssl_context *ssl)
2191
0
{
2192
0
    int ret;
2193
0
    unsigned char *buf;
2194
0
    size_t buf_len;
2195
0
    mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2196
2197
0
    MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse encrypted extensions"));
2198
2199
0
    MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
2200
0
                             ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2201
0
                             &buf, &buf_len));
2202
2203
    /* Process the message contents */
2204
0
    MBEDTLS_SSL_PROC_CHK(
2205
0
        ssl_tls13_parse_encrypted_extensions(ssl, buf, buf + buf_len));
2206
2207
0
#if defined(MBEDTLS_SSL_EARLY_DATA)
2208
0
    if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(EARLY_DATA)) {
2209
        /* RFC8446 4.2.11
2210
         * If the server supplies an "early_data" extension, the
2211
         * client MUST verify that the server's selected_identity
2212
         * is 0. If any other value is returned, the client MUST
2213
         * abort the handshake with an "illegal_parameter" alert.
2214
         *
2215
         * RFC 8446 4.2.10
2216
         * In order to accept early data, the server MUST have accepted a PSK
2217
         * cipher suite and selected the first key offered in the client's
2218
         * "pre_shared_key" extension. In addition, it MUST verify that the
2219
         * following values are the same as those associated with the
2220
         * selected PSK:
2221
         * - The TLS version number
2222
         * - The selected cipher suite
2223
         * - The selected ALPN [RFC7301] protocol, if any
2224
         *
2225
         * The server has sent an early data extension in its Encrypted
2226
         * Extension message thus accepted to receive early data. We
2227
         * check here that the additional constraints on the handshake
2228
         * parameters, when early data are exchanged, are met,
2229
         * namely:
2230
         * - a PSK has been selected for the handshake
2231
         * - the selected PSK for the handshake was the first one proposed
2232
         *   by the client.
2233
         * - the selected ciphersuite for the handshake is the ciphersuite
2234
         *   associated with the selected PSK.
2235
         */
2236
0
        if ((!mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) ||
2237
0
            handshake->selected_identity != 0 ||
2238
0
            handshake->ciphersuite_info->id !=
2239
0
            ssl->session_negotiate->ciphersuite) {
2240
2241
0
            MBEDTLS_SSL_PEND_FATAL_ALERT(
2242
0
                MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
2243
0
                MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
2244
0
            return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
2245
0
        }
2246
2247
0
        ssl->early_data_state = MBEDTLS_SSL_EARLY_DATA_STATE_ACCEPTED;
2248
0
    } else if (ssl->early_data_state !=
2249
0
               MBEDTLS_SSL_EARLY_DATA_STATE_NO_IND_SENT) {
2250
0
        ssl->early_data_state = MBEDTLS_SSL_EARLY_DATA_STATE_REJECTED;
2251
0
    }
2252
0
#endif
2253
2254
    /*
2255
     * In case the client has proposed a PSK associated with a ticket,
2256
     * `ssl->session_negotiate->ciphersuite` still contains at this point the
2257
     * identifier of the ciphersuite associated with the ticket. This is that
2258
     * way because, if an exchange of early data is agreed upon, we need
2259
     * it to check that the ciphersuite selected for the handshake is the
2260
     * ticket ciphersuite (see above). This information is not needed
2261
     * anymore thus we can now set it to the identifier of the ciphersuite
2262
     * used in this session under negotiation.
2263
     */
2264
0
    ssl->session_negotiate->ciphersuite = handshake->ciphersuite_info->id;
2265
2266
0
    MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
2267
0
                             ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2268
0
                             buf, buf_len));
2269
2270
0
#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
2271
0
    if (mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2272
0
        mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2273
0
    } else {
2274
0
        mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST);
2275
0
    }
2276
#else
2277
    ((void) ssl);
2278
    mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2279
#endif
2280
2281
0
cleanup:
2282
2283
0
    MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse encrypted extensions"));
2284
0
    return ret;
2285
2286
0
}
2287
2288
#if defined(MBEDTLS_SSL_EARLY_DATA)
2289
/*
2290
 * Handler for MBEDTLS_SSL_END_OF_EARLY_DATA
2291
 *
2292
 * RFC 8446 section 4.5
2293
 *
2294
 * struct {} EndOfEarlyData;
2295
 *
2296
 * If the server sent an "early_data" extension in EncryptedExtensions, the
2297
 * client MUST send an EndOfEarlyData message after receiving the server
2298
 * Finished. Otherwise, the client MUST NOT send an EndOfEarlyData message.
2299
 */
2300
2301
MBEDTLS_CHECK_RETURN_CRITICAL
2302
static int ssl_tls13_write_end_of_early_data(mbedtls_ssl_context *ssl)
2303
0
{
2304
0
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2305
0
    unsigned char *buf = NULL;
2306
0
    size_t buf_len;
2307
0
    MBEDTLS_SSL_DEBUG_MSG(2, ("=> write EndOfEarlyData"));
2308
2309
0
    MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2310
0
                             ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA,
2311
0
                             &buf, &buf_len));
2312
2313
0
    MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_hdr_to_checksum(
2314
0
                             ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA, 0));
2315
2316
0
    MBEDTLS_SSL_PROC_CHK(
2317
0
        mbedtls_ssl_finish_handshake_msg(ssl, buf_len, 0));
2318
2319
0
    mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE);
2320
2321
0
cleanup:
2322
2323
0
    MBEDTLS_SSL_DEBUG_MSG(2, ("<= write EndOfEarlyData"));
2324
0
    return ret;
2325
0
}
2326
2327
int mbedtls_ssl_get_early_data_status(mbedtls_ssl_context *ssl)
2328
0
{
2329
0
    if ((ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) ||
2330
0
        (!mbedtls_ssl_is_handshake_over(ssl))) {
2331
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2332
0
    }
2333
2334
0
    switch (ssl->early_data_state) {
2335
0
        case MBEDTLS_SSL_EARLY_DATA_STATE_NO_IND_SENT:
2336
0
            return MBEDTLS_SSL_EARLY_DATA_STATUS_NOT_INDICATED;
2337
0
            break;
2338
2339
0
        case MBEDTLS_SSL_EARLY_DATA_STATE_REJECTED:
2340
0
            return MBEDTLS_SSL_EARLY_DATA_STATUS_REJECTED;
2341
0
            break;
2342
2343
0
        case MBEDTLS_SSL_EARLY_DATA_STATE_SERVER_FINISHED_RECEIVED:
2344
0
            return MBEDTLS_SSL_EARLY_DATA_STATUS_ACCEPTED;
2345
0
            break;
2346
2347
0
        default:
2348
0
            return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2349
0
    }
2350
0
}
2351
#endif /* MBEDTLS_SSL_EARLY_DATA */
2352
2353
#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
2354
/*
2355
 * STATE HANDLING: CertificateRequest
2356
 *
2357
 */
2358
0
#define SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST 0
2359
0
#define SSL_CERTIFICATE_REQUEST_SKIP           1
2360
/* Coordination:
2361
 * Deals with the ambiguity of not knowing if a CertificateRequest
2362
 * will be sent. Returns a negative code on failure, or
2363
 * - SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST
2364
 * - SSL_CERTIFICATE_REQUEST_SKIP
2365
 * indicating if a Certificate Request is expected or not.
2366
 */
2367
MBEDTLS_CHECK_RETURN_CRITICAL
2368
static int ssl_tls13_certificate_request_coordinate(mbedtls_ssl_context *ssl)
2369
0
{
2370
0
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2371
2372
0
    if ((ret = mbedtls_ssl_read_record(ssl, 0)) != 0) {
2373
0
        MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2374
0
        return ret;
2375
0
    }
2376
0
    ssl->keep_current_message = 1;
2377
2378
0
    if ((ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE) &&
2379
0
        (ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST)) {
2380
0
        MBEDTLS_SSL_DEBUG_MSG(3, ("got a certificate request"));
2381
0
        return SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST;
2382
0
    }
2383
2384
0
    MBEDTLS_SSL_DEBUG_MSG(3, ("got no certificate request"));
2385
2386
0
    return SSL_CERTIFICATE_REQUEST_SKIP;
2387
0
}
2388
2389
/*
2390
 * ssl_tls13_parse_certificate_request()
2391
 *     Parse certificate request
2392
 * struct {
2393
 *   opaque certificate_request_context<0..2^8-1>;
2394
 *   Extension extensions<2..2^16-1>;
2395
 * } CertificateRequest;
2396
 */
2397
MBEDTLS_CHECK_RETURN_CRITICAL
2398
static int ssl_tls13_parse_certificate_request(mbedtls_ssl_context *ssl,
2399
                                               const unsigned char *buf,
2400
                                               const unsigned char *end)
2401
0
{
2402
0
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2403
0
    const unsigned char *p = buf;
2404
0
    size_t certificate_request_context_len = 0;
2405
0
    size_t extensions_len = 0;
2406
0
    const unsigned char *extensions_end;
2407
0
    mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2408
2409
    /* ...
2410
     * opaque certificate_request_context<0..2^8-1>
2411
     * ...
2412
     */
2413
0
    MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
2414
0
    certificate_request_context_len = (size_t) p[0];
2415
0
    p += 1;
2416
2417
0
    if (certificate_request_context_len > 0) {
2418
0
        MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, certificate_request_context_len);
2419
0
        MBEDTLS_SSL_DEBUG_BUF(3, "Certificate Request Context",
2420
0
                              p, certificate_request_context_len);
2421
2422
0
        handshake->certificate_request_context =
2423
0
            mbedtls_calloc(1, certificate_request_context_len);
2424
0
        if (handshake->certificate_request_context == NULL) {
2425
0
            MBEDTLS_SSL_DEBUG_MSG(1, ("buffer too small"));
2426
0
            return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2427
0
        }
2428
0
        memcpy(handshake->certificate_request_context, p,
2429
0
               certificate_request_context_len);
2430
0
        p += certificate_request_context_len;
2431
0
    }
2432
2433
    /* ...
2434
     * Extension extensions<2..2^16-1>;
2435
     * ...
2436
     */
2437
0
    MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
2438
0
    extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
2439
0
    p += 2;
2440
2441
0
    MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
2442
0
    extensions_end = p + extensions_len;
2443
2444
0
    handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
2445
2446
0
    while (p < extensions_end) {
2447
0
        unsigned int extension_type;
2448
0
        size_t extension_data_len;
2449
2450
0
        MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
2451
0
        extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
2452
0
        extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
2453
0
        p += 4;
2454
2455
0
        MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
2456
2457
0
        ret = mbedtls_ssl_tls13_check_received_extension(
2458
0
            ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, extension_type,
2459
0
            MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_CR);
2460
0
        if (ret != 0) {
2461
0
            return ret;
2462
0
        }
2463
2464
0
        switch (extension_type) {
2465
0
            case MBEDTLS_TLS_EXT_SIG_ALG:
2466
0
                MBEDTLS_SSL_DEBUG_MSG(3,
2467
0
                                      ("found signature algorithms extension"));
2468
0
                ret = mbedtls_ssl_parse_sig_alg_ext(ssl, p,
2469
0
                                                    p + extension_data_len);
2470
0
                if (ret != 0) {
2471
0
                    return ret;
2472
0
                }
2473
2474
0
                break;
2475
2476
0
            default:
2477
0
                MBEDTLS_SSL_PRINT_EXT(
2478
0
                    3, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2479
0
                    extension_type, "( ignored )");
2480
0
                break;
2481
0
        }
2482
2483
0
        p += extension_data_len;
2484
0
    }
2485
2486
0
    MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2487
0
                           handshake->received_extensions);
2488
2489
    /* Check that we consumed all the message. */
2490
0
    if (p != end) {
2491
0
        MBEDTLS_SSL_DEBUG_MSG(1,
2492
0
                              ("CertificateRequest misaligned"));
2493
0
        goto decode_error;
2494
0
    }
2495
2496
    /* RFC 8446 section 4.3.2
2497
     *
2498
     * The "signature_algorithms" extension MUST be specified
2499
     */
2500
0
    if ((handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(SIG_ALG)) == 0) {
2501
0
        MBEDTLS_SSL_DEBUG_MSG(3,
2502
0
                              ("no signature algorithms extension found"));
2503
0
        goto decode_error;
2504
0
    }
2505
2506
0
    ssl->handshake->client_auth = 1;
2507
0
    return 0;
2508
2509
0
decode_error:
2510
0
    MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
2511
0
                                 MBEDTLS_ERR_SSL_DECODE_ERROR);
2512
0
    return MBEDTLS_ERR_SSL_DECODE_ERROR;
2513
0
}
2514
2515
/*
2516
 * Handler for  MBEDTLS_SSL_CERTIFICATE_REQUEST
2517
 */
2518
MBEDTLS_CHECK_RETURN_CRITICAL
2519
static int ssl_tls13_process_certificate_request(mbedtls_ssl_context *ssl)
2520
0
{
2521
0
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2522
2523
0
    MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate request"));
2524
2525
0
    MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_certificate_request_coordinate(ssl));
2526
2527
0
    if (ret == SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST) {
2528
0
        unsigned char *buf;
2529
0
        size_t buf_len;
2530
2531
0
        MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
2532
0
                                 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2533
0
                                 &buf, &buf_len));
2534
2535
0
        MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_certificate_request(
2536
0
                                 ssl, buf, buf + buf_len));
2537
2538
0
        MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
2539
0
                                 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2540
0
                                 buf, buf_len));
2541
0
    } else if (ret == SSL_CERTIFICATE_REQUEST_SKIP) {
2542
0
        ret = 0;
2543
0
    } else {
2544
0
        MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2545
0
        ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2546
0
        goto cleanup;
2547
0
    }
2548
2549
0
    mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_CERTIFICATE);
2550
2551
0
cleanup:
2552
2553
0
    MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate request"));
2554
0
    return ret;
2555
0
}
2556
2557
/*
2558
 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
2559
 */
2560
MBEDTLS_CHECK_RETURN_CRITICAL
2561
static int ssl_tls13_process_server_certificate(mbedtls_ssl_context *ssl)
2562
0
{
2563
0
    int ret;
2564
2565
0
    ret = mbedtls_ssl_tls13_process_certificate(ssl);
2566
0
    if (ret != 0) {
2567
0
        return ret;
2568
0
    }
2569
2570
0
    mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY);
2571
0
    return 0;
2572
0
}
2573
2574
/*
2575
 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
2576
 */
2577
MBEDTLS_CHECK_RETURN_CRITICAL
2578
static int ssl_tls13_process_certificate_verify(mbedtls_ssl_context *ssl)
2579
0
{
2580
0
    int ret;
2581
2582
0
    ret = mbedtls_ssl_tls13_process_certificate_verify(ssl);
2583
0
    if (ret != 0) {
2584
0
        return ret;
2585
0
    }
2586
2587
0
    mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2588
0
    return 0;
2589
0
}
2590
#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
2591
2592
/*
2593
 * Handler for MBEDTLS_SSL_SERVER_FINISHED
2594
 */
2595
MBEDTLS_CHECK_RETURN_CRITICAL
2596
static int ssl_tls13_process_server_finished(mbedtls_ssl_context *ssl)
2597
0
{
2598
0
    int ret;
2599
2600
0
    ret = mbedtls_ssl_tls13_process_finished_message(ssl);
2601
0
    if (ret != 0) {
2602
0
        return ret;
2603
0
    }
2604
2605
0
    ret = mbedtls_ssl_tls13_compute_application_transform(ssl);
2606
0
    if (ret != 0) {
2607
0
        MBEDTLS_SSL_PEND_FATAL_ALERT(
2608
0
            MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2609
0
            MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2610
0
        return ret;
2611
0
    }
2612
2613
0
#if defined(MBEDTLS_SSL_EARLY_DATA)
2614
0
    if (ssl->early_data_state == MBEDTLS_SSL_EARLY_DATA_STATE_ACCEPTED) {
2615
0
        ssl->early_data_state = MBEDTLS_SSL_EARLY_DATA_STATE_SERVER_FINISHED_RECEIVED;
2616
0
        mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_END_OF_EARLY_DATA);
2617
0
    } else
2618
0
#endif /* MBEDTLS_SSL_EARLY_DATA */
2619
0
    {
2620
0
#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2621
0
        mbedtls_ssl_handshake_set_state(
2622
0
            ssl, MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED);
2623
#else
2624
        mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE);
2625
#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
2626
0
    }
2627
2628
0
    return 0;
2629
0
}
2630
2631
/*
2632
 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE
2633
 */
2634
MBEDTLS_CHECK_RETURN_CRITICAL
2635
static int ssl_tls13_write_client_certificate(mbedtls_ssl_context *ssl)
2636
0
{
2637
0
    int non_empty_certificate_msg = 0;
2638
2639
0
    MBEDTLS_SSL_DEBUG_MSG(1,
2640
0
                          ("Switch to handshake traffic keys for outbound traffic"));
2641
0
    mbedtls_ssl_set_outbound_transform(ssl, ssl->handshake->transform_handshake);
2642
2643
0
#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
2644
0
    if (ssl->handshake->client_auth) {
2645
0
        int ret = mbedtls_ssl_tls13_write_certificate(ssl);
2646
0
        if (ret != 0) {
2647
0
            return ret;
2648
0
        }
2649
2650
0
        if (mbedtls_ssl_own_cert(ssl) != NULL) {
2651
0
            non_empty_certificate_msg = 1;
2652
0
        }
2653
0
    } else {
2654
0
        MBEDTLS_SSL_DEBUG_MSG(2, ("skip write certificate"));
2655
0
    }
2656
0
#endif
2657
2658
0
    if (non_empty_certificate_msg) {
2659
0
        mbedtls_ssl_handshake_set_state(ssl,
2660
0
                                        MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY);
2661
0
    } else {
2662
0
        MBEDTLS_SSL_DEBUG_MSG(2, ("skip write certificate verify"));
2663
0
        mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_FINISHED);
2664
0
    }
2665
2666
0
    return 0;
2667
0
}
2668
2669
#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
2670
/*
2671
 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY
2672
 */
2673
MBEDTLS_CHECK_RETURN_CRITICAL
2674
static int ssl_tls13_write_client_certificate_verify(mbedtls_ssl_context *ssl)
2675
0
{
2676
0
    int ret = mbedtls_ssl_tls13_write_certificate_verify(ssl);
2677
2678
0
    if (ret == 0) {
2679
0
        mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_FINISHED);
2680
0
    }
2681
2682
0
    return ret;
2683
0
}
2684
#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
2685
2686
/*
2687
 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
2688
 */
2689
MBEDTLS_CHECK_RETURN_CRITICAL
2690
static int ssl_tls13_write_client_finished(mbedtls_ssl_context *ssl)
2691
0
{
2692
0
    int ret;
2693
2694
0
    ret = mbedtls_ssl_tls13_write_finished_message(ssl);
2695
0
    if (ret != 0) {
2696
0
        return ret;
2697
0
    }
2698
2699
0
    ret = mbedtls_ssl_tls13_compute_resumption_master_secret(ssl);
2700
0
    if (ret != 0) {
2701
0
        MBEDTLS_SSL_DEBUG_RET(
2702
0
            1, "mbedtls_ssl_tls13_compute_resumption_master_secret ", ret);
2703
0
        return ret;
2704
0
    }
2705
2706
0
    mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_FLUSH_BUFFERS);
2707
0
    return 0;
2708
0
}
2709
2710
/*
2711
 * Handler for MBEDTLS_SSL_FLUSH_BUFFERS
2712
 */
2713
MBEDTLS_CHECK_RETURN_CRITICAL
2714
static int ssl_tls13_flush_buffers(mbedtls_ssl_context *ssl)
2715
0
{
2716
0
    MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
2717
0
    mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP);
2718
0
    return 0;
2719
0
}
2720
2721
/*
2722
 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
2723
 */
2724
MBEDTLS_CHECK_RETURN_CRITICAL
2725
static int ssl_tls13_handshake_wrapup(mbedtls_ssl_context *ssl)
2726
0
{
2727
2728
0
    mbedtls_ssl_tls13_handshake_wrapup(ssl);
2729
2730
0
    mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
2731
0
    return 0;
2732
0
}
2733
2734
#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2735
2736
#if defined(MBEDTLS_SSL_EARLY_DATA)
2737
/* From RFC 8446 section 4.2.10
2738
 *
2739
 * struct {
2740
 *     select (Handshake.msg_type) {
2741
 *         case new_session_ticket:   uint32 max_early_data_size;
2742
 *         ...
2743
 *     };
2744
 * } EarlyDataIndication;
2745
 */
2746
MBEDTLS_CHECK_RETURN_CRITICAL
2747
static int ssl_tls13_parse_new_session_ticket_early_data_ext(
2748
    mbedtls_ssl_context *ssl,
2749
    const unsigned char *buf,
2750
    const unsigned char *end)
2751
0
{
2752
0
    mbedtls_ssl_session *session = ssl->session;
2753
2754
0
    MBEDTLS_SSL_CHK_BUF_READ_PTR(buf, end, 4);
2755
2756
0
    session->max_early_data_size = MBEDTLS_GET_UINT32_BE(buf, 0);
2757
0
    mbedtls_ssl_tls13_session_set_ticket_flags(
2758
0
        session, MBEDTLS_SSL_TLS1_3_TICKET_ALLOW_EARLY_DATA);
2759
0
    MBEDTLS_SSL_DEBUG_MSG(
2760
0
        3, ("received max_early_data_size: %u",
2761
0
            (unsigned int) session->max_early_data_size));
2762
2763
0
    return 0;
2764
0
}
2765
#endif /* MBEDTLS_SSL_EARLY_DATA */
2766
2767
MBEDTLS_CHECK_RETURN_CRITICAL
2768
static int ssl_tls13_parse_new_session_ticket_exts(mbedtls_ssl_context *ssl,
2769
                                                   const unsigned char *buf,
2770
                                                   const unsigned char *end)
2771
0
{
2772
0
    mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2773
0
    const unsigned char *p = buf;
2774
2775
2776
0
    handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
2777
2778
0
    while (p < end) {
2779
0
        unsigned int extension_type;
2780
0
        size_t extension_data_len;
2781
0
        int ret;
2782
2783
0
        MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 4);
2784
0
        extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
2785
0
        extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
2786
0
        p += 4;
2787
2788
0
        MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extension_data_len);
2789
2790
0
        ret = mbedtls_ssl_tls13_check_received_extension(
2791
0
            ssl, MBEDTLS_SSL_HS_NEW_SESSION_TICKET, extension_type,
2792
0
            MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_NST);
2793
0
        if (ret != 0) {
2794
0
            return ret;
2795
0
        }
2796
2797
0
        switch (extension_type) {
2798
0
#if defined(MBEDTLS_SSL_EARLY_DATA)
2799
0
            case MBEDTLS_TLS_EXT_EARLY_DATA:
2800
0
                ret = ssl_tls13_parse_new_session_ticket_early_data_ext(
2801
0
                    ssl, p, p + extension_data_len);
2802
0
                if (ret != 0) {
2803
0
                    MBEDTLS_SSL_DEBUG_RET(
2804
0
                        1, "ssl_tls13_parse_new_session_ticket_early_data_ext",
2805
0
                        ret);
2806
0
                }
2807
0
                break;
2808
0
#endif /* MBEDTLS_SSL_EARLY_DATA */
2809
2810
0
            default:
2811
0
                MBEDTLS_SSL_PRINT_EXT(
2812
0
                    3, MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
2813
0
                    extension_type, "( ignored )");
2814
0
                break;
2815
0
        }
2816
2817
0
        p +=  extension_data_len;
2818
0
    }
2819
2820
0
    MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
2821
0
                           handshake->received_extensions);
2822
2823
0
    return 0;
2824
0
}
2825
2826
/*
2827
 * From RFC8446, page 74
2828
 *
2829
 * struct {
2830
 *    uint32 ticket_lifetime;
2831
 *    uint32 ticket_age_add;
2832
 *    opaque ticket_nonce<0..255>;
2833
 *    opaque ticket<1..2^16-1>;
2834
 *    Extension extensions<0..2^16-2>;
2835
 * } NewSessionTicket;
2836
 *
2837
 */
2838
MBEDTLS_CHECK_RETURN_CRITICAL
2839
static int ssl_tls13_parse_new_session_ticket(mbedtls_ssl_context *ssl,
2840
                                              unsigned char *buf,
2841
                                              unsigned char *end,
2842
                                              unsigned char **ticket_nonce,
2843
                                              size_t *ticket_nonce_len)
2844
0
{
2845
0
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2846
0
    unsigned char *p = buf;
2847
0
    mbedtls_ssl_session *session = ssl->session;
2848
0
    size_t ticket_len;
2849
0
    unsigned char *ticket;
2850
0
    size_t extensions_len;
2851
2852
0
    *ticket_nonce = NULL;
2853
0
    *ticket_nonce_len = 0;
2854
    /*
2855
     *    ticket_lifetime   4 bytes
2856
     *    ticket_age_add    4 bytes
2857
     *    ticket_nonce_len  1 byte
2858
     */
2859
0
    MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 9);
2860
2861
0
    session->ticket_lifetime = MBEDTLS_GET_UINT32_BE(p, 0);
2862
0
    MBEDTLS_SSL_DEBUG_MSG(3,
2863
0
                          ("ticket_lifetime: %u",
2864
0
                           (unsigned int) session->ticket_lifetime));
2865
0
    if (session->ticket_lifetime >
2866
0
        MBEDTLS_SSL_TLS1_3_MAX_ALLOWED_TICKET_LIFETIME) {
2867
0
        MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_lifetime exceeds 7 days."));
2868
0
        return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
2869
0
    }
2870
2871
0
    session->ticket_age_add = MBEDTLS_GET_UINT32_BE(p, 4);
2872
0
    MBEDTLS_SSL_DEBUG_MSG(3,
2873
0
                          ("ticket_age_add: %u",
2874
0
                           (unsigned int) session->ticket_age_add));
2875
2876
0
    *ticket_nonce_len = p[8];
2877
0
    p += 9;
2878
2879
0
    MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, *ticket_nonce_len);
2880
0
    *ticket_nonce = p;
2881
0
    MBEDTLS_SSL_DEBUG_BUF(3, "ticket_nonce:", *ticket_nonce, *ticket_nonce_len);
2882
0
    p += *ticket_nonce_len;
2883
2884
    /* Ticket */
2885
0
    MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
2886
0
    ticket_len = MBEDTLS_GET_UINT16_BE(p, 0);
2887
0
    p += 2;
2888
0
    MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, ticket_len);
2889
0
    MBEDTLS_SSL_DEBUG_BUF(3, "received ticket", p, ticket_len);
2890
2891
    /* Check if we previously received a ticket already. */
2892
0
    if (session->ticket != NULL || session->ticket_len > 0) {
2893
0
        mbedtls_free(session->ticket);
2894
0
        session->ticket = NULL;
2895
0
        session->ticket_len = 0;
2896
0
    }
2897
2898
0
    if ((ticket = mbedtls_calloc(1, ticket_len)) == NULL) {
2899
0
        MBEDTLS_SSL_DEBUG_MSG(1, ("ticket alloc failed"));
2900
0
        return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2901
0
    }
2902
0
    memcpy(ticket, p, ticket_len);
2903
0
    p += ticket_len;
2904
0
    session->ticket = ticket;
2905
0
    session->ticket_len = ticket_len;
2906
2907
    /* Clear all flags in ticket_flags */
2908
0
    mbedtls_ssl_tls13_session_clear_ticket_flags(
2909
0
        session, MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK);
2910
2911
0
    MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
2912
0
    extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
2913
0
    p += 2;
2914
0
    MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
2915
2916
0
    MBEDTLS_SSL_DEBUG_BUF(3, "ticket extension", p, extensions_len);
2917
2918
0
    ret = ssl_tls13_parse_new_session_ticket_exts(ssl, p, p + extensions_len);
2919
0
    if (ret != 0) {
2920
0
        MBEDTLS_SSL_DEBUG_RET(1,
2921
0
                              "ssl_tls13_parse_new_session_ticket_exts",
2922
0
                              ret);
2923
0
        return ret;
2924
0
    }
2925
2926
0
    return 0;
2927
0
}
2928
2929
/* Non negative return values for ssl_tls13_postprocess_new_session_ticket().
2930
 * - POSTPROCESS_NEW_SESSION_TICKET_SIGNAL, all good, we have to signal the
2931
 *   application that a valid ticket has been received.
2932
 * - POSTPROCESS_NEW_SESSION_TICKET_DISCARD, no fatal error, we keep the
2933
 *   connection alive but we do not signal the ticket to the application.
2934
 */
2935
0
#define POSTPROCESS_NEW_SESSION_TICKET_SIGNAL 0
2936
0
#define POSTPROCESS_NEW_SESSION_TICKET_DISCARD 1
2937
MBEDTLS_CHECK_RETURN_CRITICAL
2938
static int ssl_tls13_postprocess_new_session_ticket(mbedtls_ssl_context *ssl,
2939
                                                    unsigned char *ticket_nonce,
2940
                                                    size_t ticket_nonce_len)
2941
0
{
2942
0
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2943
0
    mbedtls_ssl_session *session = ssl->session;
2944
0
    const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
2945
0
    psa_algorithm_t psa_hash_alg;
2946
0
    int hash_length;
2947
2948
0
    if (session->ticket_lifetime == 0) {
2949
0
        return POSTPROCESS_NEW_SESSION_TICKET_DISCARD;
2950
0
    }
2951
2952
0
#if defined(MBEDTLS_HAVE_TIME)
2953
    /* Store ticket creation time */
2954
0
    session->ticket_reception_time = mbedtls_ms_time();
2955
0
#endif
2956
2957
0
    ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(session->ciphersuite);
2958
0
    if (ciphersuite_info == NULL) {
2959
0
        MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2960
0
        return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2961
0
    }
2962
2963
0
    psa_hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
2964
0
    hash_length = PSA_HASH_LENGTH(psa_hash_alg);
2965
0
    if (hash_length == -1 ||
2966
0
        (size_t) hash_length > sizeof(session->resumption_key)) {
2967
0
        return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2968
0
    }
2969
2970
2971
0
    MBEDTLS_SSL_DEBUG_BUF(3, "resumption_master_secret",
2972
0
                          session->app_secrets.resumption_master_secret,
2973
0
                          hash_length);
2974
2975
    /* Compute resumption key
2976
     *
2977
     *  HKDF-Expand-Label( resumption_master_secret,
2978
     *                    "resumption", ticket_nonce, Hash.length )
2979
     */
2980
0
    ret = mbedtls_ssl_tls13_hkdf_expand_label(
2981
0
        psa_hash_alg,
2982
0
        session->app_secrets.resumption_master_secret,
2983
0
        hash_length,
2984
0
        MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(resumption),
2985
0
        ticket_nonce,
2986
0
        ticket_nonce_len,
2987
0
        session->resumption_key,
2988
0
        hash_length);
2989
2990
0
    if (ret != 0) {
2991
0
        MBEDTLS_SSL_DEBUG_RET(2,
2992
0
                              "Creating the ticket-resumed PSK failed",
2993
0
                              ret);
2994
0
        return ret;
2995
0
    }
2996
2997
0
    session->resumption_key_len = hash_length;
2998
2999
0
    MBEDTLS_SSL_DEBUG_BUF(3, "Ticket-resumed PSK",
3000
0
                          session->resumption_key,
3001
0
                          session->resumption_key_len);
3002
3003
    /* Set ticket_flags depends on the selected key exchange modes */
3004
0
    mbedtls_ssl_tls13_session_set_ticket_flags(
3005
0
        session, ssl->conf->tls13_kex_modes);
3006
0
    MBEDTLS_SSL_PRINT_TICKET_FLAGS(4, session->ticket_flags);
3007
3008
0
    return POSTPROCESS_NEW_SESSION_TICKET_SIGNAL;
3009
0
}
3010
3011
/*
3012
 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
3013
 */
3014
MBEDTLS_CHECK_RETURN_CRITICAL
3015
static int ssl_tls13_process_new_session_ticket(mbedtls_ssl_context *ssl)
3016
0
{
3017
0
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3018
0
    unsigned char *buf;
3019
0
    size_t buf_len;
3020
0
    unsigned char *ticket_nonce;
3021
0
    size_t ticket_nonce_len;
3022
3023
0
    MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse new session ticket"));
3024
3025
0
    MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
3026
0
                             ssl, MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
3027
0
                             &buf, &buf_len));
3028
3029
    /*
3030
     * We are about to update (maybe only partially) ticket data thus block
3031
     * any session export for the time being.
3032
     */
3033
0
    ssl->session->exported = 1;
3034
3035
0
    MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_new_session_ticket(
3036
0
                             ssl, buf, buf + buf_len,
3037
0
                             &ticket_nonce, &ticket_nonce_len));
3038
3039
0
    MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_postprocess_new_session_ticket(
3040
0
                                 ssl, ticket_nonce, ticket_nonce_len));
3041
3042
0
    switch (ret) {
3043
0
        case POSTPROCESS_NEW_SESSION_TICKET_SIGNAL:
3044
            /*
3045
             * All good, we have received a new valid ticket, session data can
3046
             * be exported now and we signal the ticket to the application.
3047
             */
3048
0
            ssl->session->exported = 0;
3049
0
            ret = MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET;
3050
0
            break;
3051
3052
0
        case POSTPROCESS_NEW_SESSION_TICKET_DISCARD:
3053
0
            ret = 0;
3054
0
            MBEDTLS_SSL_DEBUG_MSG(2, ("Discard new session ticket"));
3055
0
            break;
3056
3057
0
        default:
3058
0
            ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3059
0
    }
3060
3061
0
    mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
3062
3063
0
cleanup:
3064
3065
0
    MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse new session ticket"));
3066
0
    return ret;
3067
0
}
3068
#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3069
3070
int mbedtls_ssl_tls13_handshake_client_step(mbedtls_ssl_context *ssl)
3071
0
{
3072
0
    int ret = 0;
3073
3074
0
    switch (ssl->state) {
3075
0
        case MBEDTLS_SSL_HELLO_REQUEST:
3076
0
            mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
3077
0
            break;
3078
3079
0
        case MBEDTLS_SSL_CLIENT_HELLO:
3080
0
            ret = mbedtls_ssl_write_client_hello(ssl);
3081
0
            break;
3082
3083
0
        case MBEDTLS_SSL_SERVER_HELLO:
3084
0
            ret = ssl_tls13_process_server_hello(ssl);
3085
0
            break;
3086
3087
0
        case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
3088
0
            ret = ssl_tls13_process_encrypted_extensions(ssl);
3089
0
            break;
3090
3091
0
#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
3092
0
        case MBEDTLS_SSL_CERTIFICATE_REQUEST:
3093
0
            ret = ssl_tls13_process_certificate_request(ssl);
3094
0
            break;
3095
3096
0
        case MBEDTLS_SSL_SERVER_CERTIFICATE:
3097
0
            ret = ssl_tls13_process_server_certificate(ssl);
3098
0
            break;
3099
3100
0
        case MBEDTLS_SSL_CERTIFICATE_VERIFY:
3101
0
            ret = ssl_tls13_process_certificate_verify(ssl);
3102
0
            break;
3103
0
#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
3104
3105
0
        case MBEDTLS_SSL_SERVER_FINISHED:
3106
0
            ret = ssl_tls13_process_server_finished(ssl);
3107
0
            break;
3108
3109
0
#if defined(MBEDTLS_SSL_EARLY_DATA)
3110
0
        case MBEDTLS_SSL_END_OF_EARLY_DATA:
3111
0
            ret = ssl_tls13_write_end_of_early_data(ssl);
3112
0
            break;
3113
0
#endif
3114
3115
0
        case MBEDTLS_SSL_CLIENT_CERTIFICATE:
3116
0
            ret = ssl_tls13_write_client_certificate(ssl);
3117
0
            break;
3118
3119
0
#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
3120
0
        case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
3121
0
            ret = ssl_tls13_write_client_certificate_verify(ssl);
3122
0
            break;
3123
0
#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
3124
3125
0
        case MBEDTLS_SSL_CLIENT_FINISHED:
3126
0
            ret = ssl_tls13_write_client_finished(ssl);
3127
0
            break;
3128
3129
0
        case MBEDTLS_SSL_FLUSH_BUFFERS:
3130
0
            ret = ssl_tls13_flush_buffers(ssl);
3131
0
            break;
3132
3133
0
        case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
3134
0
            ret = ssl_tls13_handshake_wrapup(ssl);
3135
0
            break;
3136
3137
            /*
3138
             * Injection of dummy-CCS's for middlebox compatibility
3139
             */
3140
0
#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
3141
0
        case MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO:
3142
0
            ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3143
0
            if (ret != 0) {
3144
0
                break;
3145
0
            }
3146
0
            mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
3147
0
            break;
3148
3149
0
        case MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED:
3150
0
            ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3151
0
            if (ret != 0) {
3152
0
                break;
3153
0
            }
3154
0
            mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE);
3155
0
            break;
3156
3157
0
#if defined(MBEDTLS_SSL_EARLY_DATA)
3158
0
        case MBEDTLS_SSL_CLIENT_CCS_AFTER_CLIENT_HELLO:
3159
0
            ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3160
0
            if (ret == 0) {
3161
0
                mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_HELLO);
3162
3163
0
                MBEDTLS_SSL_DEBUG_MSG(
3164
0
                    1, ("Switch to early data keys for outbound traffic"));
3165
0
                mbedtls_ssl_set_outbound_transform(
3166
0
                    ssl, ssl->handshake->transform_earlydata);
3167
0
                ssl->early_data_state = MBEDTLS_SSL_EARLY_DATA_STATE_CAN_WRITE;
3168
0
            }
3169
0
            break;
3170
0
#endif /* MBEDTLS_SSL_EARLY_DATA */
3171
0
#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
3172
3173
0
#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3174
0
        case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET:
3175
0
            ret = ssl_tls13_process_new_session_ticket(ssl);
3176
0
            break;
3177
0
#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3178
3179
0
        default:
3180
0
            MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
3181
0
            return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3182
0
    }
3183
3184
0
    return ret;
3185
0
}
3186
3187
#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_3 */