Coverage Report

Created: 2026-09-14 07:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/mbedtls/library/ssl_tls.c
Line
Count
Source
1
/*
2
 *  TLS shared functions
3
 *
4
 *  Copyright The Mbed TLS Contributors
5
 *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6
 */
7
/*
8
 *  http://www.ietf.org/rfc/rfc2246.txt
9
 *  http://www.ietf.org/rfc/rfc4346.txt
10
 */
11
12
#include "common.h"
13
14
#if defined(MBEDTLS_SSL_TLS_C)
15
16
#include "mbedtls/platform.h"
17
18
#include "mbedtls/ssl.h"
19
#include "ssl_client.h"
20
#include "ssl_debug_helpers.h"
21
#include "ssl_misc.h"
22
#include "ssl_tls13_keys.h"
23
24
#include "debug_internal.h"
25
#include "mbedtls/error.h"
26
#include "mbedtls/platform_util.h"
27
#include "mbedtls/version.h"
28
#include "mbedtls/constant_time.h"
29
30
#include <string.h>
31
32
#if defined(MBEDTLS_USE_PSA_CRYPTO)
33
#include "mbedtls/psa_util.h"
34
#include "psa/crypto.h"
35
#endif
36
37
#if defined(MBEDTLS_X509_CRT_PARSE_C)
38
#include "mbedtls/oid.h"
39
#endif
40
41
#if defined(MBEDTLS_USE_PSA_CRYPTO)
42
/* Define local translating functions to save code size by not using too many
43
 * arguments in each translating place. */
44
static int local_err_translation(psa_status_t status)
45
0
{
46
0
    return psa_status_to_mbedtls(status, psa_to_ssl_errors,
47
0
                                 ARRAY_LENGTH(psa_to_ssl_errors),
48
0
                                 psa_generic_status_to_mbedtls);
49
0
}
50
7.88k
#define PSA_TO_MBEDTLS_ERR(status) local_err_translation(status)
51
#endif
52
53
#if defined(MBEDTLS_TEST_HOOKS)
54
static mbedtls_ssl_chk_buf_ptr_args chk_buf_ptr_fail_args;
55
56
void mbedtls_ssl_set_chk_buf_ptr_fail_args(
57
    const uint8_t *cur, const uint8_t *end, size_t need)
58
468
{
59
468
    chk_buf_ptr_fail_args.cur = cur;
60
468
    chk_buf_ptr_fail_args.end = end;
61
468
    chk_buf_ptr_fail_args.need = need;
62
468
}
63
64
void mbedtls_ssl_reset_chk_buf_ptr_fail_args(void)
65
0
{
66
0
    memset(&chk_buf_ptr_fail_args, 0, sizeof(chk_buf_ptr_fail_args));
67
0
}
68
69
int mbedtls_ssl_cmp_chk_buf_ptr_fail_args(mbedtls_ssl_chk_buf_ptr_args *args)
70
0
{
71
0
    return (chk_buf_ptr_fail_args.cur  != args->cur) ||
72
0
           (chk_buf_ptr_fail_args.end  != args->end) ||
73
0
           (chk_buf_ptr_fail_args.need != args->need);
74
0
}
75
#endif /* MBEDTLS_TEST_HOOKS */
76
77
#if defined(MBEDTLS_SSL_PROTO_DTLS)
78
79
#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
80
/* Top-level Connection ID API */
81
82
int mbedtls_ssl_conf_cid(mbedtls_ssl_config *conf,
83
                         size_t len,
84
                         int ignore_other_cid)
85
0
{
86
0
    if (len > MBEDTLS_SSL_CID_IN_LEN_MAX) {
87
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
88
0
    }
89
90
0
    if (ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
91
0
        ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE) {
92
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
93
0
    }
94
95
0
    conf->ignore_unexpected_cid = ignore_other_cid;
96
0
    conf->cid_len = len;
97
0
    return 0;
98
0
}
99
100
int mbedtls_ssl_set_cid(mbedtls_ssl_context *ssl,
101
                        int enable,
102
                        unsigned char const *own_cid,
103
                        size_t own_cid_len)
104
0
{
105
0
    if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
106
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
107
0
    }
108
109
0
    ssl->negotiate_cid = enable;
110
0
    if (enable == MBEDTLS_SSL_CID_DISABLED) {
111
0
        MBEDTLS_SSL_DEBUG_MSG(3, ("Disable use of CID extension."));
112
0
        return 0;
113
0
    }
114
0
    MBEDTLS_SSL_DEBUG_MSG(3, ("Enable use of CID extension."));
115
0
    MBEDTLS_SSL_DEBUG_BUF(3, "Own CID", own_cid, own_cid_len);
116
117
0
    if (own_cid_len != ssl->conf->cid_len) {
118
0
        MBEDTLS_SSL_DEBUG_MSG(3, ("CID length %u does not match CID length %u in config",
119
0
                                  (unsigned) own_cid_len,
120
0
                                  (unsigned) ssl->conf->cid_len));
121
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
122
0
    }
123
124
0
    memcpy(ssl->own_cid, own_cid, own_cid_len);
125
    /* Truncation is not an issue here because
126
     * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
127
0
    ssl->own_cid_len = (uint8_t) own_cid_len;
128
129
0
    return 0;
130
0
}
131
132
int mbedtls_ssl_get_own_cid(mbedtls_ssl_context *ssl,
133
                            int *enabled,
134
                            unsigned char own_cid[MBEDTLS_SSL_CID_IN_LEN_MAX],
135
                            size_t *own_cid_len)
136
0
{
137
0
    *enabled = MBEDTLS_SSL_CID_DISABLED;
138
139
0
    if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
140
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
141
0
    }
142
143
    /* We report MBEDTLS_SSL_CID_DISABLED in case the CID length is
144
     * zero as this is indistinguishable from not requesting to use
145
     * the CID extension. */
146
0
    if (ssl->own_cid_len == 0 || ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED) {
147
0
        return 0;
148
0
    }
149
150
0
    if (own_cid_len != NULL) {
151
0
        *own_cid_len = ssl->own_cid_len;
152
0
        if (own_cid != NULL) {
153
0
            memcpy(own_cid, ssl->own_cid, ssl->own_cid_len);
154
0
        }
155
0
    }
156
157
0
    *enabled = MBEDTLS_SSL_CID_ENABLED;
158
159
0
    return 0;
160
0
}
161
162
int mbedtls_ssl_get_peer_cid(mbedtls_ssl_context *ssl,
163
                             int *enabled,
164
                             unsigned char peer_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX],
165
                             size_t *peer_cid_len)
166
0
{
167
0
    *enabled = MBEDTLS_SSL_CID_DISABLED;
168
169
0
    if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
170
0
        mbedtls_ssl_is_handshake_over(ssl) == 0) {
171
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
172
0
    }
173
174
    /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
175
     * were used, but client and server requested the empty CID.
176
     * This is indistinguishable from not using the CID extension
177
     * in the first place. */
178
0
    if (ssl->transform_in->in_cid_len  == 0 &&
179
0
        ssl->transform_in->out_cid_len == 0) {
180
0
        return 0;
181
0
    }
182
183
0
    if (peer_cid_len != NULL) {
184
0
        *peer_cid_len = ssl->transform_in->out_cid_len;
185
0
        if (peer_cid != NULL) {
186
0
            memcpy(peer_cid, ssl->transform_in->out_cid,
187
0
                   ssl->transform_in->out_cid_len);
188
0
        }
189
0
    }
190
191
0
    *enabled = MBEDTLS_SSL_CID_ENABLED;
192
193
0
    return 0;
194
0
}
195
#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
196
197
#endif /* MBEDTLS_SSL_PROTO_DTLS */
198
199
#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
200
/*
201
 * Convert max_fragment_length codes to length.
202
 * RFC 6066 says:
203
 *    enum{
204
 *        2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
205
 *    } MaxFragmentLength;
206
 * and we add 0 -> extension unused
207
 */
208
static unsigned int ssl_mfl_code_to_length(int mfl)
209
256k
{
210
256k
    switch (mfl) {
211
256k
        case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
212
256k
            return MBEDTLS_TLS_EXT_ADV_CONTENT_LEN;
213
40
        case MBEDTLS_SSL_MAX_FRAG_LEN_512:
214
40
            return 512;
215
26
        case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
216
26
            return 1024;
217
22
        case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
218
22
            return 2048;
219
39
        case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
220
39
            return 4096;
221
0
        default:
222
0
            return MBEDTLS_TLS_EXT_ADV_CONTENT_LEN;
223
256k
    }
224
256k
}
225
#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
226
227
int mbedtls_ssl_session_copy(mbedtls_ssl_session *dst,
228
                             const mbedtls_ssl_session *src)
229
0
{
230
0
    mbedtls_ssl_session_free(dst);
231
0
    memcpy(dst, src, sizeof(mbedtls_ssl_session));
232
0
#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
233
0
    dst->ticket = NULL;
234
0
#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
235
0
    defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
236
0
    dst->hostname = NULL;
237
0
#endif
238
0
#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
239
240
0
#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_ALPN) && \
241
0
    defined(MBEDTLS_SSL_EARLY_DATA)
242
0
    dst->ticket_alpn = NULL;
243
0
#endif
244
245
0
#if defined(MBEDTLS_X509_CRT_PARSE_C)
246
247
0
#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
248
0
    if (src->peer_cert != NULL) {
249
0
        int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
250
251
0
        dst->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
252
0
        if (dst->peer_cert == NULL) {
253
0
            return MBEDTLS_ERR_SSL_ALLOC_FAILED;
254
0
        }
255
256
0
        mbedtls_x509_crt_init(dst->peer_cert);
257
258
0
        if ((ret = mbedtls_x509_crt_parse_der(dst->peer_cert, src->peer_cert->raw.p,
259
0
                                              src->peer_cert->raw.len)) != 0) {
260
0
            mbedtls_free(dst->peer_cert);
261
0
            dst->peer_cert = NULL;
262
0
            return ret;
263
0
        }
264
0
    }
265
#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
266
    if (src->peer_cert_digest != NULL) {
267
        dst->peer_cert_digest =
268
            mbedtls_calloc(1, src->peer_cert_digest_len);
269
        if (dst->peer_cert_digest == NULL) {
270
            return MBEDTLS_ERR_SSL_ALLOC_FAILED;
271
        }
272
273
        memcpy(dst->peer_cert_digest, src->peer_cert_digest,
274
               src->peer_cert_digest_len);
275
        dst->peer_cert_digest_type = src->peer_cert_digest_type;
276
        dst->peer_cert_digest_len = src->peer_cert_digest_len;
277
    }
278
#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
279
280
0
#endif /* MBEDTLS_X509_CRT_PARSE_C */
281
282
0
#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_ALPN) && \
283
0
    defined(MBEDTLS_SSL_EARLY_DATA)
284
0
    {
285
0
        int ret = mbedtls_ssl_session_set_ticket_alpn(dst, src->ticket_alpn);
286
0
        if (ret != 0) {
287
0
            return ret;
288
0
        }
289
0
    }
290
0
#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_ALPN && MBEDTLS_SSL_EARLY_DATA */
291
292
0
#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
293
0
    if (src->ticket != NULL) {
294
0
        dst->ticket = mbedtls_calloc(1, src->ticket_len);
295
0
        if (dst->ticket == NULL) {
296
0
            return MBEDTLS_ERR_SSL_ALLOC_FAILED;
297
0
        }
298
299
0
        memcpy(dst->ticket, src->ticket, src->ticket_len);
300
0
    }
301
302
0
#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
303
0
    defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
304
0
    if (src->endpoint == MBEDTLS_SSL_IS_CLIENT) {
305
0
        int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
306
0
        ret = mbedtls_ssl_session_set_hostname(dst, src->hostname);
307
0
        if (ret != 0) {
308
0
            return ret;
309
0
        }
310
0
    }
311
0
#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
312
          MBEDTLS_SSL_SERVER_NAME_INDICATION */
313
0
#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
314
315
0
    return 0;
316
0
}
317
318
#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
319
MBEDTLS_CHECK_RETURN_CRITICAL
320
static int resize_buffer(unsigned char **buffer, size_t len_new, size_t *len_old)
321
0
{
322
0
    unsigned char *resized_buffer = mbedtls_calloc(1, len_new);
323
0
    if (resized_buffer == NULL) {
324
0
        return MBEDTLS_ERR_SSL_ALLOC_FAILED;
325
0
    }
326
327
    /* We want to copy len_new bytes when downsizing the buffer, and
328
     * len_old bytes when upsizing, so we choose the smaller of two sizes,
329
     * to fit one buffer into another. Size checks, ensuring that no data is
330
     * lost, are done outside of this function. */
331
0
    memcpy(resized_buffer, *buffer,
332
0
           (len_new < *len_old) ? len_new : *len_old);
333
0
    mbedtls_zeroize_and_free(*buffer, *len_old);
334
335
0
    *buffer = resized_buffer;
336
0
    *len_old = len_new;
337
338
0
    return 0;
339
0
}
340
341
static void handle_buffer_resizing(mbedtls_ssl_context *ssl, int downsizing,
342
                                   size_t in_buf_new_len,
343
                                   size_t out_buf_new_len)
344
21.4k
{
345
21.4k
    int modified = 0;
346
21.4k
    size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0, hdr_in = 0;
347
21.4k
    size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0;
348
21.4k
    if (ssl->in_buf != NULL) {
349
12.5k
        written_in = ssl->in_msg - ssl->in_buf;
350
12.5k
        iv_offset_in = ssl->in_iv - ssl->in_buf;
351
12.5k
        len_offset_in = ssl->in_len - ssl->in_buf;
352
12.5k
        hdr_in = ssl->in_hdr - ssl->in_buf;
353
12.5k
        if (downsizing ?
354
1.77k
            ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len :
355
12.5k
            ssl->in_buf_len < in_buf_new_len) {
356
0
            if (resize_buffer(&ssl->in_buf, in_buf_new_len, &ssl->in_buf_len) != 0) {
357
0
                MBEDTLS_SSL_DEBUG_MSG(1, ("input buffer resizing failed - out of memory"));
358
0
            } else {
359
0
                MBEDTLS_SSL_DEBUG_MSG(2, ("Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET,
360
0
                                          in_buf_new_len));
361
0
                modified = 1;
362
0
            }
363
0
        }
364
12.5k
    }
365
366
21.4k
    if (ssl->out_buf != NULL) {
367
12.5k
        written_out = ssl->out_msg - ssl->out_buf;
368
12.5k
        iv_offset_out = ssl->out_iv - ssl->out_buf;
369
12.5k
        len_offset_out = ssl->out_len - ssl->out_buf;
370
12.5k
        if (downsizing ?
371
1.77k
            ssl->out_buf_len > out_buf_new_len && ssl->out_left < out_buf_new_len :
372
12.5k
            ssl->out_buf_len < out_buf_new_len) {
373
0
            if (resize_buffer(&ssl->out_buf, out_buf_new_len, &ssl->out_buf_len) != 0) {
374
0
                MBEDTLS_SSL_DEBUG_MSG(1, ("output buffer resizing failed - out of memory"));
375
0
            } else {
376
0
                MBEDTLS_SSL_DEBUG_MSG(2, ("Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET,
377
0
                                          out_buf_new_len));
378
0
                modified = 1;
379
0
            }
380
0
        }
381
12.5k
    }
382
21.4k
    if (modified) {
383
        /* Update pointers here to avoid doing it twice. */
384
0
        ssl->in_hdr = ssl->in_buf + hdr_in;
385
0
        mbedtls_ssl_update_in_pointers(ssl);
386
0
        mbedtls_ssl_reset_out_pointers(ssl);
387
388
        /* Fields below might not be properly updated with record
389
         * splitting or with CID, so they are manually updated here. */
390
0
        ssl->out_msg = ssl->out_buf + written_out;
391
0
        ssl->out_len = ssl->out_buf + len_offset_out;
392
0
        ssl->out_iv = ssl->out_buf + iv_offset_out;
393
394
0
        ssl->in_msg = ssl->in_buf + written_in;
395
0
        ssl->in_len = ssl->in_buf + len_offset_in;
396
0
        ssl->in_iv = ssl->in_buf + iv_offset_in;
397
0
    }
398
21.4k
}
399
#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
400
401
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
402
403
#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
404
typedef int (*tls_prf_fn)(const unsigned char *secret, size_t slen,
405
                          const char *label,
406
                          const unsigned char *random, size_t rlen,
407
                          unsigned char *dstbuf, size_t dlen);
408
409
static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id);
410
411
#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
412
413
/* Type for the TLS PRF */
414
typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
415
                          const unsigned char *, size_t,
416
                          unsigned char *, size_t);
417
418
MBEDTLS_CHECK_RETURN_CRITICAL
419
static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform,
420
                                        int ciphersuite,
421
                                        const unsigned char master[48],
422
#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
423
                                        int encrypt_then_mac,
424
#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
425
                                        ssl_tls_prf_t tls_prf,
426
                                        const unsigned char randbytes[64],
427
                                        mbedtls_ssl_protocol_version tls_version,
428
                                        unsigned endpoint,
429
                                        const mbedtls_ssl_context *ssl);
430
431
#if defined(MBEDTLS_MD_CAN_SHA256)
432
MBEDTLS_CHECK_RETURN_CRITICAL
433
static int tls_prf_sha256(const unsigned char *secret, size_t slen,
434
                          const char *label,
435
                          const unsigned char *random, size_t rlen,
436
                          unsigned char *dstbuf, size_t dlen);
437
static int ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *, unsigned char *, size_t *);
438
static int ssl_calc_finished_tls_sha256(mbedtls_ssl_context *, unsigned char *, int);
439
440
#endif /* MBEDTLS_MD_CAN_SHA256*/
441
442
#if defined(MBEDTLS_MD_CAN_SHA384)
443
MBEDTLS_CHECK_RETURN_CRITICAL
444
static int tls_prf_sha384(const unsigned char *secret, size_t slen,
445
                          const char *label,
446
                          const unsigned char *random, size_t rlen,
447
                          unsigned char *dstbuf, size_t dlen);
448
449
static int ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *, unsigned char *, size_t *);
450
static int ssl_calc_finished_tls_sha384(mbedtls_ssl_context *, unsigned char *, int);
451
#endif /* MBEDTLS_MD_CAN_SHA384*/
452
453
MBEDTLS_CHECK_RETURN_CRITICAL
454
static int ssl_tls12_session_load(mbedtls_ssl_session *session,
455
                                  const unsigned char *buf,
456
                                  size_t len);
457
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
458
459
static int ssl_update_checksum_start(mbedtls_ssl_context *, const unsigned char *, size_t);
460
461
#if defined(MBEDTLS_MD_CAN_SHA256)
462
static int ssl_update_checksum_sha256(mbedtls_ssl_context *, const unsigned char *, size_t);
463
#endif /* MBEDTLS_MD_CAN_SHA256*/
464
465
#if defined(MBEDTLS_MD_CAN_SHA384)
466
static int ssl_update_checksum_sha384(mbedtls_ssl_context *, const unsigned char *, size_t);
467
#endif /* MBEDTLS_MD_CAN_SHA384*/
468
469
int  mbedtls_ssl_tls_prf(const mbedtls_tls_prf_types prf,
470
                         const unsigned char *secret, size_t slen,
471
                         const char *label,
472
                         const unsigned char *random, size_t rlen,
473
                         unsigned char *dstbuf, size_t dlen)
474
0
{
475
0
    mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
476
477
0
    switch (prf) {
478
0
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
479
0
#if defined(MBEDTLS_MD_CAN_SHA384)
480
0
        case MBEDTLS_SSL_TLS_PRF_SHA384:
481
0
            tls_prf = tls_prf_sha384;
482
0
            break;
483
0
#endif /* MBEDTLS_MD_CAN_SHA384*/
484
0
#if defined(MBEDTLS_MD_CAN_SHA256)
485
0
        case MBEDTLS_SSL_TLS_PRF_SHA256:
486
0
            tls_prf = tls_prf_sha256;
487
0
            break;
488
0
#endif /* MBEDTLS_MD_CAN_SHA256*/
489
0
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
490
0
        default:
491
0
            return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
492
0
    }
493
494
0
    return tls_prf(secret, slen, label, random, rlen, dstbuf, dlen);
495
0
}
496
497
#if defined(MBEDTLS_X509_CRT_PARSE_C)
498
static void ssl_clear_peer_cert(mbedtls_ssl_session *session)
499
17.4k
{
500
17.4k
#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
501
17.4k
    if (session->peer_cert != NULL) {
502
2.02k
        mbedtls_x509_crt_free(session->peer_cert);
503
2.02k
        mbedtls_free(session->peer_cert);
504
2.02k
        session->peer_cert = NULL;
505
2.02k
    }
506
#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
507
    if (session->peer_cert_digest != NULL) {
508
        /* Zeroization is not necessary. */
509
        mbedtls_free(session->peer_cert_digest);
510
        session->peer_cert_digest      = NULL;
511
        session->peer_cert_digest_type = MBEDTLS_MD_NONE;
512
        session->peer_cert_digest_len  = 0;
513
    }
514
#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
515
17.4k
}
516
#endif /* MBEDTLS_X509_CRT_PARSE_C */
517
518
uint32_t mbedtls_ssl_get_extension_id(unsigned int extension_type)
519
185k
{
520
185k
    switch (extension_type) {
521
11.5k
        case MBEDTLS_TLS_EXT_SERVERNAME:
522
11.5k
            return MBEDTLS_SSL_EXT_ID_SERVERNAME;
523
524
5.78k
        case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
525
5.78k
            return MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH;
526
527
5.78k
        case MBEDTLS_TLS_EXT_STATUS_REQUEST:
528
5.78k
            return MBEDTLS_SSL_EXT_ID_STATUS_REQUEST;
529
530
11.5k
        case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
531
11.5k
            return MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS;
532
533
11.5k
        case MBEDTLS_TLS_EXT_SIG_ALG:
534
11.5k
            return MBEDTLS_SSL_EXT_ID_SIG_ALG;
535
536
5.78k
        case MBEDTLS_TLS_EXT_USE_SRTP:
537
5.78k
            return MBEDTLS_SSL_EXT_ID_USE_SRTP;
538
539
5.78k
        case MBEDTLS_TLS_EXT_HEARTBEAT:
540
5.78k
            return MBEDTLS_SSL_EXT_ID_HEARTBEAT;
541
542
5.80k
        case MBEDTLS_TLS_EXT_ALPN:
543
5.80k
            return MBEDTLS_SSL_EXT_ID_ALPN;
544
545
5.78k
        case MBEDTLS_TLS_EXT_SCT:
546
5.78k
            return MBEDTLS_SSL_EXT_ID_SCT;
547
548
5.78k
        case MBEDTLS_TLS_EXT_CLI_CERT_TYPE:
549
5.78k
            return MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE;
550
551
5.78k
        case MBEDTLS_TLS_EXT_SERV_CERT_TYPE:
552
5.78k
            return MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE;
553
554
5.78k
        case MBEDTLS_TLS_EXT_PADDING:
555
5.78k
            return MBEDTLS_SSL_EXT_ID_PADDING;
556
557
5.78k
        case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
558
5.78k
            return MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY;
559
560
5.78k
        case MBEDTLS_TLS_EXT_EARLY_DATA:
561
5.78k
            return MBEDTLS_SSL_EXT_ID_EARLY_DATA;
562
563
5.78k
        case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
564
5.78k
            return MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS;
565
566
5.78k
        case MBEDTLS_TLS_EXT_COOKIE:
567
5.78k
            return MBEDTLS_SSL_EXT_ID_COOKIE;
568
569
5.78k
        case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
570
5.78k
            return MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES;
571
572
5.78k
        case MBEDTLS_TLS_EXT_CERT_AUTH:
573
5.78k
            return MBEDTLS_SSL_EXT_ID_CERT_AUTH;
574
575
5.78k
        case MBEDTLS_TLS_EXT_OID_FILTERS:
576
5.78k
            return MBEDTLS_SSL_EXT_ID_OID_FILTERS;
577
578
5.78k
        case MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH:
579
5.78k
            return MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH;
580
581
5.78k
        case MBEDTLS_TLS_EXT_SIG_ALG_CERT:
582
5.78k
            return MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT;
583
584
5.78k
        case MBEDTLS_TLS_EXT_KEY_SHARE:
585
5.78k
            return MBEDTLS_SSL_EXT_ID_KEY_SHARE;
586
587
5.78k
        case MBEDTLS_TLS_EXT_TRUNCATED_HMAC:
588
5.78k
            return MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC;
589
590
5.78k
        case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
591
5.78k
            return MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS;
592
593
5.78k
        case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
594
5.78k
            return MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC;
595
596
5.78k
        case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
597
5.78k
            return MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET;
598
599
5.78k
        case MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT:
600
5.78k
            return MBEDTLS_SSL_EXT_ID_RECORD_SIZE_LIMIT;
601
602
5.78k
        case MBEDTLS_TLS_EXT_SESSION_TICKET:
603
5.78k
            return MBEDTLS_SSL_EXT_ID_SESSION_TICKET;
604
605
185k
    }
606
607
5.78k
    return MBEDTLS_SSL_EXT_ID_UNRECOGNIZED;
608
185k
}
609
610
uint32_t mbedtls_ssl_get_extension_mask(unsigned int extension_type)
611
17.3k
{
612
17.3k
    return 1 << mbedtls_ssl_get_extension_id(extension_type);
613
17.3k
}
614
615
#if defined(MBEDTLS_DEBUG_C)
616
static const char *extension_name_table[] = {
617
    [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = "unrecognized",
618
    [MBEDTLS_SSL_EXT_ID_SERVERNAME] = "server_name",
619
    [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = "max_fragment_length",
620
    [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = "status_request",
621
    [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = "supported_groups",
622
    [MBEDTLS_SSL_EXT_ID_SIG_ALG] = "signature_algorithms",
623
    [MBEDTLS_SSL_EXT_ID_USE_SRTP] = "use_srtp",
624
    [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = "heartbeat",
625
    [MBEDTLS_SSL_EXT_ID_ALPN] = "application_layer_protocol_negotiation",
626
    [MBEDTLS_SSL_EXT_ID_SCT] = "signed_certificate_timestamp",
627
    [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = "client_certificate_type",
628
    [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = "server_certificate_type",
629
    [MBEDTLS_SSL_EXT_ID_PADDING] = "padding",
630
    [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = "pre_shared_key",
631
    [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = "early_data",
632
    [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = "supported_versions",
633
    [MBEDTLS_SSL_EXT_ID_COOKIE] = "cookie",
634
    [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = "psk_key_exchange_modes",
635
    [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = "certificate_authorities",
636
    [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = "oid_filters",
637
    [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = "post_handshake_auth",
638
    [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = "signature_algorithms_cert",
639
    [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = "key_share",
640
    [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = "truncated_hmac",
641
    [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = "supported_point_formats",
642
    [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = "encrypt_then_mac",
643
    [MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = "extended_master_secret",
644
    [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = "session_ticket",
645
    [MBEDTLS_SSL_EXT_ID_RECORD_SIZE_LIMIT] = "record_size_limit"
646
};
647
648
static const unsigned int extension_type_table[] = {
649
    [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = 0xff,
650
    [MBEDTLS_SSL_EXT_ID_SERVERNAME] = MBEDTLS_TLS_EXT_SERVERNAME,
651
    [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH,
652
    [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = MBEDTLS_TLS_EXT_STATUS_REQUEST,
653
    [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = MBEDTLS_TLS_EXT_SUPPORTED_GROUPS,
654
    [MBEDTLS_SSL_EXT_ID_SIG_ALG] = MBEDTLS_TLS_EXT_SIG_ALG,
655
    [MBEDTLS_SSL_EXT_ID_USE_SRTP] = MBEDTLS_TLS_EXT_USE_SRTP,
656
    [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = MBEDTLS_TLS_EXT_HEARTBEAT,
657
    [MBEDTLS_SSL_EXT_ID_ALPN] = MBEDTLS_TLS_EXT_ALPN,
658
    [MBEDTLS_SSL_EXT_ID_SCT] = MBEDTLS_TLS_EXT_SCT,
659
    [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = MBEDTLS_TLS_EXT_CLI_CERT_TYPE,
660
    [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = MBEDTLS_TLS_EXT_SERV_CERT_TYPE,
661
    [MBEDTLS_SSL_EXT_ID_PADDING] = MBEDTLS_TLS_EXT_PADDING,
662
    [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = MBEDTLS_TLS_EXT_PRE_SHARED_KEY,
663
    [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = MBEDTLS_TLS_EXT_EARLY_DATA,
664
    [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS,
665
    [MBEDTLS_SSL_EXT_ID_COOKIE] = MBEDTLS_TLS_EXT_COOKIE,
666
    [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES,
667
    [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = MBEDTLS_TLS_EXT_CERT_AUTH,
668
    [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = MBEDTLS_TLS_EXT_OID_FILTERS,
669
    [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH,
670
    [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = MBEDTLS_TLS_EXT_SIG_ALG_CERT,
671
    [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = MBEDTLS_TLS_EXT_KEY_SHARE,
672
    [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = MBEDTLS_TLS_EXT_TRUNCATED_HMAC,
673
    [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS,
674
    [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC,
675
    [MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET,
676
    [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = MBEDTLS_TLS_EXT_SESSION_TICKET,
677
    [MBEDTLS_SSL_EXT_ID_RECORD_SIZE_LIMIT] = MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT
678
};
679
680
const char *mbedtls_ssl_get_extension_name(unsigned int extension_type)
681
167k
{
682
167k
    return extension_name_table[
683
167k
        mbedtls_ssl_get_extension_id(extension_type)];
684
167k
}
685
686
const char *mbedtls_ssl_get_hs_msg_name(int hs_msg_type)
687
168k
{
688
168k
    switch (hs_msg_type) {
689
167k
        case MBEDTLS_SSL_HS_CLIENT_HELLO:
690
167k
            return "ClientHello";
691
4
        case MBEDTLS_SSL_HS_SERVER_HELLO:
692
4
            return "ServerHello";
693
0
        case MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST:
694
0
            return "HelloRetryRequest";
695
2
        case MBEDTLS_SSL_HS_NEW_SESSION_TICKET:
696
2
            return "NewSessionTicket";
697
3
        case MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS:
698
3
            return "EncryptedExtensions";
699
330
        case MBEDTLS_SSL_HS_CERTIFICATE:
700
330
            return "Certificate";
701
1
        case MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE:
702
1
            return "ServerKeyExchange";
703
2
        case MBEDTLS_SSL_HS_CERTIFICATE_REQUEST:
704
2
            return "CertificateRequest";
705
2
        case MBEDTLS_SSL_HS_CERTIFICATE_VERIFY:
706
2
            return "CertificateVerify";
707
1
        case MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE:
708
1
            return "ClientKeyExchange";
709
1
        case MBEDTLS_SSL_HS_FINISHED:
710
1
            return "Finished";
711
168k
    }
712
351
    return "Unknown";
713
168k
}
714
715
void mbedtls_ssl_print_extension(const mbedtls_ssl_context *ssl,
716
                                 int level, const char *file, int line,
717
                                 int hs_msg_type, unsigned int extension_type,
718
                                 const char *extra_msg0, const char *extra_msg1)
719
167k
{
720
167k
    const char *extra_msg;
721
167k
    if (extra_msg0 && extra_msg1) {
722
0
        mbedtls_debug_print_msg(
723
0
            ssl, level, file, line,
724
0
            "%s: %s(%u) extension %s %s.",
725
0
            mbedtls_ssl_get_hs_msg_name(hs_msg_type),
726
0
            mbedtls_ssl_get_extension_name(extension_type),
727
0
            extension_type,
728
0
            extra_msg0, extra_msg1);
729
0
        return;
730
0
    }
731
732
167k
    extra_msg = extra_msg0 ? extra_msg0 : extra_msg1;
733
167k
    if (extra_msg) {
734
167k
        mbedtls_debug_print_msg(
735
167k
            ssl, level, file, line,
736
167k
            "%s: %s(%u) extension %s.", mbedtls_ssl_get_hs_msg_name(hs_msg_type),
737
167k
            mbedtls_ssl_get_extension_name(extension_type), extension_type,
738
167k
            extra_msg);
739
167k
        return;
740
167k
    }
741
742
0
    mbedtls_debug_print_msg(
743
0
        ssl, level, file, line,
744
0
        "%s: %s(%u) extension.", mbedtls_ssl_get_hs_msg_name(hs_msg_type),
745
0
        mbedtls_ssl_get_extension_name(extension_type), extension_type);
746
0
}
747
748
void mbedtls_ssl_print_extensions(const mbedtls_ssl_context *ssl,
749
                                  int level, const char *file, int line,
750
                                  int hs_msg_type, uint32_t extensions_mask,
751
                                  const char *extra)
752
5.78k
{
753
754
5.78k
    for (unsigned i = 0;
755
173k
         i < sizeof(extension_name_table) / sizeof(extension_name_table[0]);
756
167k
         i++) {
757
167k
        mbedtls_ssl_print_extension(
758
167k
            ssl, level, file, line, hs_msg_type, extension_type_table[i],
759
167k
            extensions_mask & (1 << i) ? "exists" : "does not exist", extra);
760
167k
    }
761
5.78k
}
762
763
#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
764
static const char *ticket_flag_name_table[] =
765
{
766
    [0] = "ALLOW_PSK_RESUMPTION",
767
    [2] = "ALLOW_PSK_EPHEMERAL_RESUMPTION",
768
    [3] = "ALLOW_EARLY_DATA",
769
};
770
771
void mbedtls_ssl_print_ticket_flags(const mbedtls_ssl_context *ssl,
772
                                    int level, const char *file, int line,
773
                                    unsigned int flags)
774
0
{
775
0
    size_t i;
776
777
0
    mbedtls_debug_print_msg(ssl, level, file, line,
778
0
                            "print ticket_flags (0x%02x)", flags);
779
780
0
    flags = flags & MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK;
781
782
0
    for (i = 0; i < ARRAY_LENGTH(ticket_flag_name_table); i++) {
783
0
        if ((flags & (1 << i))) {
784
0
            mbedtls_debug_print_msg(ssl, level, file, line, "- %s is set.",
785
0
                                    ticket_flag_name_table[i]);
786
0
        }
787
0
    }
788
0
}
789
#endif /* MBEDTLS_SSL_PROTO_TLS1_3 && MBEDTLS_SSL_SESSION_TICKETS */
790
791
#endif /* MBEDTLS_DEBUG_C */
792
793
void mbedtls_ssl_optimize_checksum(mbedtls_ssl_context *ssl,
794
                                   const mbedtls_ssl_ciphersuite_t *ciphersuite_info)
795
4.02k
{
796
4.02k
    ((void) ciphersuite_info);
797
798
4.02k
#if defined(MBEDTLS_MD_CAN_SHA384)
799
4.02k
    if (ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
800
1.89k
        ssl->handshake->update_checksum = ssl_update_checksum_sha384;
801
1.89k
    } else
802
2.12k
#endif
803
2.12k
#if defined(MBEDTLS_MD_CAN_SHA256)
804
2.12k
    if (ciphersuite_info->mac != MBEDTLS_MD_SHA384) {
805
2.12k
        ssl->handshake->update_checksum = ssl_update_checksum_sha256;
806
2.12k
    } else
807
0
#endif
808
0
    {
809
0
        MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
810
0
        return;
811
0
    }
812
4.02k
}
813
814
int mbedtls_ssl_add_hs_hdr_to_checksum(mbedtls_ssl_context *ssl,
815
                                       unsigned hs_type,
816
                                       size_t total_hs_len)
817
0
{
818
0
    unsigned char hs_hdr[4];
819
820
    /* Build HS header for checksum update. */
821
0
    hs_hdr[0] = MBEDTLS_BYTE_0(hs_type);
822
0
    hs_hdr[1] = MBEDTLS_BYTE_2(total_hs_len);
823
0
    hs_hdr[2] = MBEDTLS_BYTE_1(total_hs_len);
824
0
    hs_hdr[3] = MBEDTLS_BYTE_0(total_hs_len);
825
826
0
    return ssl->handshake->update_checksum(ssl, hs_hdr, sizeof(hs_hdr));
827
0
}
828
829
int mbedtls_ssl_add_hs_msg_to_checksum(mbedtls_ssl_context *ssl,
830
                                       unsigned hs_type,
831
                                       unsigned char const *msg,
832
                                       size_t msg_len)
833
0
{
834
0
    int ret;
835
0
    ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl, hs_type, msg_len);
836
0
    if (ret != 0) {
837
0
        return ret;
838
0
    }
839
0
    return ssl->handshake->update_checksum(ssl, msg, msg_len);
840
0
}
841
842
int mbedtls_ssl_reset_checksum(mbedtls_ssl_context *ssl)
843
22.4k
{
844
22.4k
#if defined(MBEDTLS_MD_CAN_SHA256) || \
845
22.4k
    defined(MBEDTLS_MD_CAN_SHA384)
846
#if defined(MBEDTLS_USE_PSA_CRYPTO)
847
    psa_status_t status;
848
#else
849
11.2k
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
850
#endif
851
#else /* SHA-256 or SHA-384 */
852
    ((void) ssl);
853
#endif /* SHA-256 or SHA-384 */
854
22.4k
#if defined(MBEDTLS_MD_CAN_SHA256)
855
#if defined(MBEDTLS_USE_PSA_CRYPTO)
856
    status = psa_hash_abort(&ssl->handshake->fin_sha256_psa);
857
11.2k
    if (status != PSA_SUCCESS) {
858
0
        return PSA_TO_MBEDTLS_ERR(status);
859
0
    }
860
11.2k
    status = psa_hash_setup(&ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256);
861
11.2k
    if (status != PSA_SUCCESS) {
862
0
        return PSA_TO_MBEDTLS_ERR(status);
863
0
    }
864
#else
865
    mbedtls_md_free(&ssl->handshake->fin_sha256);
866
    mbedtls_md_init(&ssl->handshake->fin_sha256);
867
    ret = mbedtls_md_setup(&ssl->handshake->fin_sha256,
868
                           mbedtls_md_info_from_type(MBEDTLS_MD_SHA256),
869
                           0);
870
11.2k
    if (ret != 0) {
871
0
        return ret;
872
0
    }
873
11.2k
    ret = mbedtls_md_starts(&ssl->handshake->fin_sha256);
874
11.2k
    if (ret != 0) {
875
0
        return ret;
876
0
    }
877
11.2k
#endif
878
11.2k
#endif
879
11.2k
#if defined(MBEDTLS_MD_CAN_SHA384)
880
#if defined(MBEDTLS_USE_PSA_CRYPTO)
881
11.2k
    status = psa_hash_abort(&ssl->handshake->fin_sha384_psa);
882
11.2k
    if (status != PSA_SUCCESS) {
883
0
        return PSA_TO_MBEDTLS_ERR(status);
884
0
    }
885
11.2k
    status = psa_hash_setup(&ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384);
886
11.2k
    if (status != PSA_SUCCESS) {
887
0
        return PSA_TO_MBEDTLS_ERR(status);
888
0
    }
889
#else
890
11.2k
    mbedtls_md_free(&ssl->handshake->fin_sha384);
891
11.2k
    mbedtls_md_init(&ssl->handshake->fin_sha384);
892
11.2k
    ret = mbedtls_md_setup(&ssl->handshake->fin_sha384,
893
11.2k
                           mbedtls_md_info_from_type(MBEDTLS_MD_SHA384), 0);
894
11.2k
    if (ret != 0) {
895
0
        return ret;
896
0
    }
897
11.2k
    ret = mbedtls_md_starts(&ssl->handshake->fin_sha384);
898
11.2k
    if (ret != 0) {
899
0
        return ret;
900
0
    }
901
11.2k
#endif
902
11.2k
#endif
903
22.4k
    return 0;
904
11.2k
}
mbedtls_ssl_reset_checksum
Line
Count
Source
843
11.2k
{
844
11.2k
#if defined(MBEDTLS_MD_CAN_SHA256) || \
845
11.2k
    defined(MBEDTLS_MD_CAN_SHA384)
846
#if defined(MBEDTLS_USE_PSA_CRYPTO)
847
    psa_status_t status;
848
#else
849
11.2k
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
850
11.2k
#endif
851
#else /* SHA-256 or SHA-384 */
852
    ((void) ssl);
853
#endif /* SHA-256 or SHA-384 */
854
11.2k
#if defined(MBEDTLS_MD_CAN_SHA256)
855
#if defined(MBEDTLS_USE_PSA_CRYPTO)
856
    status = psa_hash_abort(&ssl->handshake->fin_sha256_psa);
857
    if (status != PSA_SUCCESS) {
858
        return PSA_TO_MBEDTLS_ERR(status);
859
    }
860
    status = psa_hash_setup(&ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256);
861
    if (status != PSA_SUCCESS) {
862
        return PSA_TO_MBEDTLS_ERR(status);
863
    }
864
#else
865
11.2k
    mbedtls_md_free(&ssl->handshake->fin_sha256);
866
11.2k
    mbedtls_md_init(&ssl->handshake->fin_sha256);
867
11.2k
    ret = mbedtls_md_setup(&ssl->handshake->fin_sha256,
868
11.2k
                           mbedtls_md_info_from_type(MBEDTLS_MD_SHA256),
869
11.2k
                           0);
870
11.2k
    if (ret != 0) {
871
0
        return ret;
872
0
    }
873
11.2k
    ret = mbedtls_md_starts(&ssl->handshake->fin_sha256);
874
11.2k
    if (ret != 0) {
875
0
        return ret;
876
0
    }
877
11.2k
#endif
878
11.2k
#endif
879
11.2k
#if defined(MBEDTLS_MD_CAN_SHA384)
880
#if defined(MBEDTLS_USE_PSA_CRYPTO)
881
    status = psa_hash_abort(&ssl->handshake->fin_sha384_psa);
882
    if (status != PSA_SUCCESS) {
883
        return PSA_TO_MBEDTLS_ERR(status);
884
    }
885
    status = psa_hash_setup(&ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384);
886
    if (status != PSA_SUCCESS) {
887
        return PSA_TO_MBEDTLS_ERR(status);
888
    }
889
#else
890
11.2k
    mbedtls_md_free(&ssl->handshake->fin_sha384);
891
11.2k
    mbedtls_md_init(&ssl->handshake->fin_sha384);
892
11.2k
    ret = mbedtls_md_setup(&ssl->handshake->fin_sha384,
893
11.2k
                           mbedtls_md_info_from_type(MBEDTLS_MD_SHA384), 0);
894
11.2k
    if (ret != 0) {
895
0
        return ret;
896
0
    }
897
11.2k
    ret = mbedtls_md_starts(&ssl->handshake->fin_sha384);
898
11.2k
    if (ret != 0) {
899
0
        return ret;
900
0
    }
901
11.2k
#endif
902
11.2k
#endif
903
11.2k
    return 0;
904
11.2k
}
mbedtls_ssl_reset_checksum
Line
Count
Source
843
11.2k
{
844
11.2k
#if defined(MBEDTLS_MD_CAN_SHA256) || \
845
11.2k
    defined(MBEDTLS_MD_CAN_SHA384)
846
11.2k
#if defined(MBEDTLS_USE_PSA_CRYPTO)
847
11.2k
    psa_status_t status;
848
#else
849
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
850
#endif
851
#else /* SHA-256 or SHA-384 */
852
    ((void) ssl);
853
#endif /* SHA-256 or SHA-384 */
854
11.2k
#if defined(MBEDTLS_MD_CAN_SHA256)
855
11.2k
#if defined(MBEDTLS_USE_PSA_CRYPTO)
856
11.2k
    status = psa_hash_abort(&ssl->handshake->fin_sha256_psa);
857
11.2k
    if (status != PSA_SUCCESS) {
858
0
        return PSA_TO_MBEDTLS_ERR(status);
859
0
    }
860
11.2k
    status = psa_hash_setup(&ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256);
861
11.2k
    if (status != PSA_SUCCESS) {
862
0
        return PSA_TO_MBEDTLS_ERR(status);
863
0
    }
864
#else
865
    mbedtls_md_free(&ssl->handshake->fin_sha256);
866
    mbedtls_md_init(&ssl->handshake->fin_sha256);
867
    ret = mbedtls_md_setup(&ssl->handshake->fin_sha256,
868
                           mbedtls_md_info_from_type(MBEDTLS_MD_SHA256),
869
                           0);
870
    if (ret != 0) {
871
        return ret;
872
    }
873
    ret = mbedtls_md_starts(&ssl->handshake->fin_sha256);
874
    if (ret != 0) {
875
        return ret;
876
    }
877
#endif
878
11.2k
#endif
879
11.2k
#if defined(MBEDTLS_MD_CAN_SHA384)
880
11.2k
#if defined(MBEDTLS_USE_PSA_CRYPTO)
881
11.2k
    status = psa_hash_abort(&ssl->handshake->fin_sha384_psa);
882
11.2k
    if (status != PSA_SUCCESS) {
883
0
        return PSA_TO_MBEDTLS_ERR(status);
884
0
    }
885
11.2k
    status = psa_hash_setup(&ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384);
886
11.2k
    if (status != PSA_SUCCESS) {
887
0
        return PSA_TO_MBEDTLS_ERR(status);
888
0
    }
889
#else
890
    mbedtls_md_free(&ssl->handshake->fin_sha384);
891
    mbedtls_md_init(&ssl->handshake->fin_sha384);
892
    ret = mbedtls_md_setup(&ssl->handshake->fin_sha384,
893
                           mbedtls_md_info_from_type(MBEDTLS_MD_SHA384), 0);
894
    if (ret != 0) {
895
        return ret;
896
    }
897
    ret = mbedtls_md_starts(&ssl->handshake->fin_sha384);
898
    if (ret != 0) {
899
        return ret;
900
    }
901
#endif
902
11.2k
#endif
903
11.2k
    return 0;
904
11.2k
}
905
906
static int ssl_update_checksum_start(mbedtls_ssl_context *ssl,
907
                                     const unsigned char *buf, size_t len)
908
26.8k
{
909
26.8k
#if defined(MBEDTLS_MD_CAN_SHA256) || \
910
26.8k
    defined(MBEDTLS_MD_CAN_SHA384)
911
#if defined(MBEDTLS_USE_PSA_CRYPTO)
912
    psa_status_t status;
913
#else
914
13.4k
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
915
#endif
916
#else /* SHA-256 or SHA-384 */
917
    ((void) ssl);
918
    (void) buf;
919
    (void) len;
920
#endif /* SHA-256 or SHA-384 */
921
26.8k
#if defined(MBEDTLS_MD_CAN_SHA256)
922
#if defined(MBEDTLS_USE_PSA_CRYPTO)
923
    status = psa_hash_update(&ssl->handshake->fin_sha256_psa, buf, len);
924
13.4k
    if (status != PSA_SUCCESS) {
925
0
        return PSA_TO_MBEDTLS_ERR(status);
926
0
    }
927
#else
928
    ret = mbedtls_md_update(&ssl->handshake->fin_sha256, buf, len);
929
13.4k
    if (ret != 0) {
930
0
        return ret;
931
0
    }
932
13.4k
#endif
933
13.4k
#endif
934
13.4k
#if defined(MBEDTLS_MD_CAN_SHA384)
935
#if defined(MBEDTLS_USE_PSA_CRYPTO)
936
13.4k
    status = psa_hash_update(&ssl->handshake->fin_sha384_psa, buf, len);
937
13.4k
    if (status != PSA_SUCCESS) {
938
0
        return PSA_TO_MBEDTLS_ERR(status);
939
0
    }
940
#else
941
13.4k
    ret = mbedtls_md_update(&ssl->handshake->fin_sha384, buf, len);
942
13.4k
    if (ret != 0) {
943
0
        return ret;
944
0
    }
945
13.4k
#endif
946
13.4k
#endif
947
26.8k
    return 0;
948
13.4k
}
ssl_tls.c:ssl_update_checksum_start
Line
Count
Source
908
13.4k
{
909
13.4k
#if defined(MBEDTLS_MD_CAN_SHA256) || \
910
13.4k
    defined(MBEDTLS_MD_CAN_SHA384)
911
#if defined(MBEDTLS_USE_PSA_CRYPTO)
912
    psa_status_t status;
913
#else
914
13.4k
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
915
13.4k
#endif
916
#else /* SHA-256 or SHA-384 */
917
    ((void) ssl);
918
    (void) buf;
919
    (void) len;
920
#endif /* SHA-256 or SHA-384 */
921
13.4k
#if defined(MBEDTLS_MD_CAN_SHA256)
922
#if defined(MBEDTLS_USE_PSA_CRYPTO)
923
    status = psa_hash_update(&ssl->handshake->fin_sha256_psa, buf, len);
924
    if (status != PSA_SUCCESS) {
925
        return PSA_TO_MBEDTLS_ERR(status);
926
    }
927
#else
928
13.4k
    ret = mbedtls_md_update(&ssl->handshake->fin_sha256, buf, len);
929
13.4k
    if (ret != 0) {
930
0
        return ret;
931
0
    }
932
13.4k
#endif
933
13.4k
#endif
934
13.4k
#if defined(MBEDTLS_MD_CAN_SHA384)
935
#if defined(MBEDTLS_USE_PSA_CRYPTO)
936
    status = psa_hash_update(&ssl->handshake->fin_sha384_psa, buf, len);
937
    if (status != PSA_SUCCESS) {
938
        return PSA_TO_MBEDTLS_ERR(status);
939
    }
940
#else
941
13.4k
    ret = mbedtls_md_update(&ssl->handshake->fin_sha384, buf, len);
942
13.4k
    if (ret != 0) {
943
0
        return ret;
944
0
    }
945
13.4k
#endif
946
13.4k
#endif
947
13.4k
    return 0;
948
13.4k
}
ssl_tls.c:ssl_update_checksum_start
Line
Count
Source
908
13.4k
{
909
13.4k
#if defined(MBEDTLS_MD_CAN_SHA256) || \
910
13.4k
    defined(MBEDTLS_MD_CAN_SHA384)
911
13.4k
#if defined(MBEDTLS_USE_PSA_CRYPTO)
912
13.4k
    psa_status_t status;
913
#else
914
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
915
#endif
916
#else /* SHA-256 or SHA-384 */
917
    ((void) ssl);
918
    (void) buf;
919
    (void) len;
920
#endif /* SHA-256 or SHA-384 */
921
13.4k
#if defined(MBEDTLS_MD_CAN_SHA256)
922
13.4k
#if defined(MBEDTLS_USE_PSA_CRYPTO)
923
13.4k
    status = psa_hash_update(&ssl->handshake->fin_sha256_psa, buf, len);
924
13.4k
    if (status != PSA_SUCCESS) {
925
0
        return PSA_TO_MBEDTLS_ERR(status);
926
0
    }
927
#else
928
    ret = mbedtls_md_update(&ssl->handshake->fin_sha256, buf, len);
929
    if (ret != 0) {
930
        return ret;
931
    }
932
#endif
933
13.4k
#endif
934
13.4k
#if defined(MBEDTLS_MD_CAN_SHA384)
935
13.4k
#if defined(MBEDTLS_USE_PSA_CRYPTO)
936
13.4k
    status = psa_hash_update(&ssl->handshake->fin_sha384_psa, buf, len);
937
13.4k
    if (status != PSA_SUCCESS) {
938
0
        return PSA_TO_MBEDTLS_ERR(status);
939
0
    }
940
#else
941
    ret = mbedtls_md_update(&ssl->handshake->fin_sha384, buf, len);
942
    if (ret != 0) {
943
        return ret;
944
    }
945
#endif
946
13.4k
#endif
947
13.4k
    return 0;
948
13.4k
}
949
950
#if defined(MBEDTLS_MD_CAN_SHA256)
951
static int ssl_update_checksum_sha256(mbedtls_ssl_context *ssl,
952
                                      const unsigned char *buf, size_t len)
953
9.84k
{
954
#if defined(MBEDTLS_USE_PSA_CRYPTO)
955
4.92k
    return PSA_TO_MBEDTLS_ERR(psa_hash_update(
956
                                  &ssl->handshake->fin_sha256_psa, buf, len));
957
#else
958
    return mbedtls_md_update(&ssl->handshake->fin_sha256, buf, len);
959
#endif
960
9.84k
}
ssl_tls.c:ssl_update_checksum_sha256
Line
Count
Source
953
4.92k
{
954
#if defined(MBEDTLS_USE_PSA_CRYPTO)
955
    return PSA_TO_MBEDTLS_ERR(psa_hash_update(
956
                                  &ssl->handshake->fin_sha256_psa, buf, len));
957
#else
958
4.92k
    return mbedtls_md_update(&ssl->handshake->fin_sha256, buf, len);
959
4.92k
#endif
960
4.92k
}
ssl_tls.c:ssl_update_checksum_sha256
Line
Count
Source
953
4.92k
{
954
4.92k
#if defined(MBEDTLS_USE_PSA_CRYPTO)
955
4.92k
    return PSA_TO_MBEDTLS_ERR(psa_hash_update(
956
4.92k
                                  &ssl->handshake->fin_sha256_psa, buf, len));
957
#else
958
    return mbedtls_md_update(&ssl->handshake->fin_sha256, buf, len);
959
#endif
960
4.92k
}
961
#endif
962
963
#if defined(MBEDTLS_MD_CAN_SHA384)
964
static int ssl_update_checksum_sha384(mbedtls_ssl_context *ssl,
965
                                      const unsigned char *buf, size_t len)
966
5.92k
{
967
#if defined(MBEDTLS_USE_PSA_CRYPTO)
968
2.96k
    return PSA_TO_MBEDTLS_ERR(psa_hash_update(
969
                                  &ssl->handshake->fin_sha384_psa, buf, len));
970
#else
971
    return mbedtls_md_update(&ssl->handshake->fin_sha384, buf, len);
972
#endif
973
5.92k
}
ssl_tls.c:ssl_update_checksum_sha384
Line
Count
Source
966
2.96k
{
967
#if defined(MBEDTLS_USE_PSA_CRYPTO)
968
    return PSA_TO_MBEDTLS_ERR(psa_hash_update(
969
                                  &ssl->handshake->fin_sha384_psa, buf, len));
970
#else
971
2.96k
    return mbedtls_md_update(&ssl->handshake->fin_sha384, buf, len);
972
2.96k
#endif
973
2.96k
}
ssl_tls.c:ssl_update_checksum_sha384
Line
Count
Source
966
2.96k
{
967
2.96k
#if defined(MBEDTLS_USE_PSA_CRYPTO)
968
2.96k
    return PSA_TO_MBEDTLS_ERR(psa_hash_update(
969
2.96k
                                  &ssl->handshake->fin_sha384_psa, buf, len));
970
#else
971
    return mbedtls_md_update(&ssl->handshake->fin_sha384, buf, len);
972
#endif
973
2.96k
}
974
#endif
975
976
static void ssl_handshake_params_init(mbedtls_ssl_handshake_params *handshake)
977
21.4k
{
978
21.4k
    memset(handshake, 0, sizeof(mbedtls_ssl_handshake_params));
979
980
21.4k
#if defined(MBEDTLS_MD_CAN_SHA256)
981
#if defined(MBEDTLS_USE_PSA_CRYPTO)
982
    handshake->fin_sha256_psa = psa_hash_operation_init();
983
#else
984
    mbedtls_md_init(&handshake->fin_sha256);
985
#endif
986
21.4k
#endif
987
21.4k
#if defined(MBEDTLS_MD_CAN_SHA384)
988
#if defined(MBEDTLS_USE_PSA_CRYPTO)
989
    handshake->fin_sha384_psa = psa_hash_operation_init();
990
#else
991
    mbedtls_md_init(&handshake->fin_sha384);
992
#endif
993
21.4k
#endif
994
995
21.4k
    handshake->update_checksum = ssl_update_checksum_start;
996
997
21.4k
#if defined(MBEDTLS_DHM_C)
998
21.4k
    mbedtls_dhm_init(&handshake->dhm_ctx);
999
21.4k
#endif
1000
#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
1001
    defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED)
1002
    mbedtls_ecdh_init(&handshake->ecdh_ctx);
1003
#endif
1004
21.4k
#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1005
#if defined(MBEDTLS_USE_PSA_CRYPTO)
1006
    handshake->psa_pake_ctx = psa_pake_operation_init();
1007
10.7k
    handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
1008
#else
1009
    mbedtls_ecjpake_init(&handshake->ecjpake_ctx);
1010
#endif /* MBEDTLS_USE_PSA_CRYPTO */
1011
21.4k
#if defined(MBEDTLS_SSL_CLI_C)
1012
21.4k
    handshake->ecjpake_cache = NULL;
1013
21.4k
    handshake->ecjpake_cache_len = 0;
1014
21.4k
#endif
1015
21.4k
#endif
1016
1017
21.4k
#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
1018
21.4k
    mbedtls_x509_crt_restart_init(&handshake->ecrs_ctx);
1019
21.4k
#endif
1020
1021
21.4k
#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1022
21.4k
    handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
1023
21.4k
#endif
1024
1025
#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
1026
    !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1027
    mbedtls_pk_init(&handshake->peer_pubkey);
1028
#endif
1029
21.4k
}
ssl_tls.c:ssl_handshake_params_init
Line
Count
Source
977
10.7k
{
978
10.7k
    memset(handshake, 0, sizeof(mbedtls_ssl_handshake_params));
979
980
10.7k
#if defined(MBEDTLS_MD_CAN_SHA256)
981
#if defined(MBEDTLS_USE_PSA_CRYPTO)
982
    handshake->fin_sha256_psa = psa_hash_operation_init();
983
#else
984
10.7k
    mbedtls_md_init(&handshake->fin_sha256);
985
10.7k
#endif
986
10.7k
#endif
987
10.7k
#if defined(MBEDTLS_MD_CAN_SHA384)
988
#if defined(MBEDTLS_USE_PSA_CRYPTO)
989
    handshake->fin_sha384_psa = psa_hash_operation_init();
990
#else
991
10.7k
    mbedtls_md_init(&handshake->fin_sha384);
992
10.7k
#endif
993
10.7k
#endif
994
995
10.7k
    handshake->update_checksum = ssl_update_checksum_start;
996
997
10.7k
#if defined(MBEDTLS_DHM_C)
998
10.7k
    mbedtls_dhm_init(&handshake->dhm_ctx);
999
10.7k
#endif
1000
10.7k
#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
1001
10.7k
    defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED)
1002
10.7k
    mbedtls_ecdh_init(&handshake->ecdh_ctx);
1003
10.7k
#endif
1004
10.7k
#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1005
#if defined(MBEDTLS_USE_PSA_CRYPTO)
1006
    handshake->psa_pake_ctx = psa_pake_operation_init();
1007
    handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
1008
#else
1009
10.7k
    mbedtls_ecjpake_init(&handshake->ecjpake_ctx);
1010
10.7k
#endif /* MBEDTLS_USE_PSA_CRYPTO */
1011
10.7k
#if defined(MBEDTLS_SSL_CLI_C)
1012
10.7k
    handshake->ecjpake_cache = NULL;
1013
10.7k
    handshake->ecjpake_cache_len = 0;
1014
10.7k
#endif
1015
10.7k
#endif
1016
1017
10.7k
#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
1018
10.7k
    mbedtls_x509_crt_restart_init(&handshake->ecrs_ctx);
1019
10.7k
#endif
1020
1021
10.7k
#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1022
10.7k
    handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
1023
10.7k
#endif
1024
1025
#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
1026
    !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1027
    mbedtls_pk_init(&handshake->peer_pubkey);
1028
#endif
1029
10.7k
}
ssl_tls.c:ssl_handshake_params_init
Line
Count
Source
977
10.7k
{
978
10.7k
    memset(handshake, 0, sizeof(mbedtls_ssl_handshake_params));
979
980
10.7k
#if defined(MBEDTLS_MD_CAN_SHA256)
981
10.7k
#if defined(MBEDTLS_USE_PSA_CRYPTO)
982
10.7k
    handshake->fin_sha256_psa = psa_hash_operation_init();
983
#else
984
    mbedtls_md_init(&handshake->fin_sha256);
985
#endif
986
10.7k
#endif
987
10.7k
#if defined(MBEDTLS_MD_CAN_SHA384)
988
10.7k
#if defined(MBEDTLS_USE_PSA_CRYPTO)
989
10.7k
    handshake->fin_sha384_psa = psa_hash_operation_init();
990
#else
991
    mbedtls_md_init(&handshake->fin_sha384);
992
#endif
993
10.7k
#endif
994
995
10.7k
    handshake->update_checksum = ssl_update_checksum_start;
996
997
10.7k
#if defined(MBEDTLS_DHM_C)
998
10.7k
    mbedtls_dhm_init(&handshake->dhm_ctx);
999
10.7k
#endif
1000
#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
1001
    defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED)
1002
    mbedtls_ecdh_init(&handshake->ecdh_ctx);
1003
#endif
1004
10.7k
#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1005
10.7k
#if defined(MBEDTLS_USE_PSA_CRYPTO)
1006
10.7k
    handshake->psa_pake_ctx = psa_pake_operation_init();
1007
10.7k
    handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
1008
#else
1009
    mbedtls_ecjpake_init(&handshake->ecjpake_ctx);
1010
#endif /* MBEDTLS_USE_PSA_CRYPTO */
1011
10.7k
#if defined(MBEDTLS_SSL_CLI_C)
1012
10.7k
    handshake->ecjpake_cache = NULL;
1013
10.7k
    handshake->ecjpake_cache_len = 0;
1014
10.7k
#endif
1015
10.7k
#endif
1016
1017
10.7k
#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
1018
10.7k
    mbedtls_x509_crt_restart_init(&handshake->ecrs_ctx);
1019
10.7k
#endif
1020
1021
10.7k
#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1022
10.7k
    handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
1023
10.7k
#endif
1024
1025
#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
1026
    !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1027
    mbedtls_pk_init(&handshake->peer_pubkey);
1028
#endif
1029
10.7k
}
1030
1031
void mbedtls_ssl_transform_init(mbedtls_ssl_transform *transform)
1032
21.4k
{
1033
21.4k
    memset(transform, 0, sizeof(mbedtls_ssl_transform));
1034
1035
#if defined(MBEDTLS_USE_PSA_CRYPTO)
1036
10.7k
    transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT;
1037
10.7k
    transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT;
1038
#else
1039
    mbedtls_cipher_init(&transform->cipher_ctx_enc);
1040
    mbedtls_cipher_init(&transform->cipher_ctx_dec);
1041
#endif
1042
1043
21.4k
#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
1044
#if defined(MBEDTLS_USE_PSA_CRYPTO)
1045
10.7k
    transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
1046
10.7k
    transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
1047
#else
1048
    mbedtls_md_init(&transform->md_ctx_enc);
1049
    mbedtls_md_init(&transform->md_ctx_dec);
1050
#endif
1051
21.4k
#endif
1052
21.4k
}
mbedtls_ssl_transform_init
Line
Count
Source
1032
10.7k
{
1033
10.7k
    memset(transform, 0, sizeof(mbedtls_ssl_transform));
1034
1035
#if defined(MBEDTLS_USE_PSA_CRYPTO)
1036
    transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT;
1037
    transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT;
1038
#else
1039
10.7k
    mbedtls_cipher_init(&transform->cipher_ctx_enc);
1040
10.7k
    mbedtls_cipher_init(&transform->cipher_ctx_dec);
1041
10.7k
#endif
1042
1043
10.7k
#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
1044
#if defined(MBEDTLS_USE_PSA_CRYPTO)
1045
    transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
1046
    transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
1047
#else
1048
10.7k
    mbedtls_md_init(&transform->md_ctx_enc);
1049
10.7k
    mbedtls_md_init(&transform->md_ctx_dec);
1050
10.7k
#endif
1051
10.7k
#endif
1052
10.7k
}
mbedtls_ssl_transform_init
Line
Count
Source
1032
10.7k
{
1033
10.7k
    memset(transform, 0, sizeof(mbedtls_ssl_transform));
1034
1035
10.7k
#if defined(MBEDTLS_USE_PSA_CRYPTO)
1036
10.7k
    transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT;
1037
10.7k
    transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT;
1038
#else
1039
    mbedtls_cipher_init(&transform->cipher_ctx_enc);
1040
    mbedtls_cipher_init(&transform->cipher_ctx_dec);
1041
#endif
1042
1043
10.7k
#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
1044
10.7k
#if defined(MBEDTLS_USE_PSA_CRYPTO)
1045
10.7k
    transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
1046
10.7k
    transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
1047
#else
1048
    mbedtls_md_init(&transform->md_ctx_enc);
1049
    mbedtls_md_init(&transform->md_ctx_dec);
1050
#endif
1051
10.7k
#endif
1052
10.7k
}
1053
1054
void mbedtls_ssl_session_init(mbedtls_ssl_session *session)
1055
14.7k
{
1056
14.7k
    memset(session, 0, sizeof(mbedtls_ssl_session));
1057
    /* Set verify_result to -1u to indicate 'result not available'. */
1058
14.7k
    session->verify_result = 0xFFFFFFFF;
1059
14.7k
}
1060
1061
MBEDTLS_CHECK_RETURN_CRITICAL
1062
static int ssl_handshake_init(mbedtls_ssl_context *ssl)
1063
10.7k
{
1064
10.7k
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1065
1066
    /* Clear old handshake information if present */
1067
10.7k
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1068
10.7k
    if (ssl->transform_negotiate) {
1069
1.77k
        mbedtls_ssl_transform_free(ssl->transform_negotiate);
1070
1.77k
    }
1071
10.7k
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1072
10.7k
    if (ssl->session_negotiate) {
1073
1.77k
        mbedtls_ssl_session_free(ssl->session_negotiate);
1074
1.77k
    }
1075
10.7k
    if (ssl->handshake) {
1076
1.77k
        mbedtls_ssl_handshake_free(ssl);
1077
1.77k
    }
1078
1079
10.7k
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1080
    /*
1081
     * Either the pointers are now NULL or cleared properly and can be freed.
1082
     * Now allocate missing structures.
1083
     */
1084
10.7k
    if (ssl->transform_negotiate == NULL) {
1085
8.97k
        ssl->transform_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
1086
8.97k
    }
1087
10.7k
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1088
1089
10.7k
    if (ssl->session_negotiate == NULL) {
1090
8.97k
        ssl->session_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_session));
1091
8.97k
    }
1092
1093
10.7k
    if (ssl->handshake == NULL) {
1094
8.97k
        ssl->handshake = mbedtls_calloc(1, sizeof(mbedtls_ssl_handshake_params));
1095
8.97k
    }
1096
10.7k
#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1097
    /* If the buffers are too small - reallocate */
1098
1099
10.7k
    handle_buffer_resizing(ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
1100
10.7k
                           MBEDTLS_SSL_OUT_BUFFER_LEN);
1101
10.7k
#endif
1102
1103
    /* All pointers should exist and can be directly freed without issue */
1104
10.7k
    if (ssl->handshake           == NULL ||
1105
10.7k
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1106
10.7k
        ssl->transform_negotiate == NULL ||
1107
10.7k
#endif
1108
10.7k
        ssl->session_negotiate   == NULL) {
1109
0
        MBEDTLS_SSL_DEBUG_MSG(1, ("alloc() of ssl sub-contexts failed"));
1110
1111
0
        mbedtls_free(ssl->handshake);
1112
0
        ssl->handshake = NULL;
1113
1114
0
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1115
0
        mbedtls_free(ssl->transform_negotiate);
1116
0
        ssl->transform_negotiate = NULL;
1117
0
#endif
1118
1119
0
        mbedtls_free(ssl->session_negotiate);
1120
0
        ssl->session_negotiate = NULL;
1121
1122
0
        return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1123
0
    }
1124
1125
10.7k
#if defined(MBEDTLS_SSL_EARLY_DATA)
1126
10.7k
#if defined(MBEDTLS_SSL_CLI_C)
1127
10.7k
    ssl->early_data_state = MBEDTLS_SSL_EARLY_DATA_STATE_IDLE;
1128
10.7k
#endif
1129
10.7k
#if defined(MBEDTLS_SSL_SRV_C)
1130
10.7k
    ssl->discard_early_data_record = MBEDTLS_SSL_EARLY_DATA_NO_DISCARD;
1131
10.7k
#endif
1132
10.7k
    ssl->total_early_data_size = 0;
1133
10.7k
#endif /* MBEDTLS_SSL_EARLY_DATA */
1134
1135
    /* Initialize structures */
1136
10.7k
    mbedtls_ssl_session_init(ssl->session_negotiate);
1137
10.7k
    ssl_handshake_params_init(ssl->handshake);
1138
1139
10.7k
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1140
10.7k
    mbedtls_ssl_transform_init(ssl->transform_negotiate);
1141
10.7k
#endif
1142
1143
    /* Setup handshake checksums */
1144
10.7k
    ret = mbedtls_ssl_reset_checksum(ssl);
1145
10.7k
    if (ret != 0) {
1146
0
        MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_checksum", ret);
1147
0
        return ret;
1148
0
    }
1149
1150
10.7k
#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
1151
10.7k
    defined(MBEDTLS_SSL_SRV_C) && \
1152
10.7k
    defined(MBEDTLS_SSL_SESSION_TICKETS)
1153
10.7k
    ssl->handshake->new_session_tickets_count =
1154
10.7k
        ssl->conf->new_session_tickets_count;
1155
10.7k
#endif
1156
1157
10.7k
#if defined(MBEDTLS_SSL_PROTO_DTLS)
1158
10.7k
    if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1159
7.16k
        ssl->handshake->alt_transform_out = ssl->transform_out;
1160
1161
7.16k
        if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
1162
5.30k
            ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
1163
5.30k
        } else {
1164
1.86k
            ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
1165
1.86k
        }
1166
1167
7.16k
        mbedtls_ssl_set_timer(ssl, 0);
1168
7.16k
    }
1169
10.7k
#endif
1170
1171
/*
1172
 * curve_list is translated to IANA TLS group identifiers here because
1173
 * mbedtls_ssl_conf_curves returns void and so can't return
1174
 * any error codes.
1175
 */
1176
10.7k
#if defined(MBEDTLS_ECP_C)
1177
10.7k
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
1178
    /* Heap allocate and translate curve_list from internal to IANA group ids */
1179
10.7k
    if (ssl->conf->curve_list != NULL) {
1180
0
        size_t length;
1181
0
        const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list;
1182
1183
0
        for (length = 0;  (curve_list[length] != MBEDTLS_ECP_DP_NONE); length++) {
1184
0
        }
1185
1186
        /* Leave room for zero termination */
1187
0
        uint16_t *group_list = mbedtls_calloc(length + 1, sizeof(uint16_t));
1188
0
        if (group_list == NULL) {
1189
0
            return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1190
0
        }
1191
1192
0
        for (size_t i = 0; i < length; i++) {
1193
0
            uint16_t tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(
1194
0
                curve_list[i]);
1195
0
            if (tls_id == 0) {
1196
0
                mbedtls_free(group_list);
1197
0
                return MBEDTLS_ERR_SSL_BAD_CONFIG;
1198
0
            }
1199
0
            group_list[i] = tls_id;
1200
0
        }
1201
1202
0
        group_list[length] = 0;
1203
1204
0
        ssl->handshake->group_list = group_list;
1205
0
        ssl->handshake->group_list_heap_allocated = 1;
1206
10.7k
    } else {
1207
10.7k
        ssl->handshake->group_list = ssl->conf->group_list;
1208
10.7k
        ssl->handshake->group_list_heap_allocated = 0;
1209
10.7k
    }
1210
10.7k
#endif /* MBEDTLS_DEPRECATED_REMOVED */
1211
10.7k
#endif /* MBEDTLS_ECP_C */
1212
1213
10.7k
#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
1214
10.7k
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
1215
10.7k
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1216
    /* Heap allocate and translate sig_hashes from internal hash identifiers to
1217
       signature algorithms IANA identifiers.  */
1218
10.7k
    if (mbedtls_ssl_conf_is_tls12_only(ssl->conf) &&
1219
7.16k
        ssl->conf->sig_hashes != NULL) {
1220
0
        const int *md;
1221
0
        const int *sig_hashes = ssl->conf->sig_hashes;
1222
0
        size_t sig_algs_len = 0;
1223
0
        uint16_t *p;
1224
1225
0
        MBEDTLS_STATIC_ASSERT(MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN
1226
0
                              <= (SIZE_MAX - (2 * sizeof(uint16_t))),
1227
0
                              "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big");
1228
1229
0
        for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) {
1230
0
            if (mbedtls_ssl_hash_from_md_alg(*md) == MBEDTLS_SSL_HASH_NONE) {
1231
0
                continue;
1232
0
            }
1233
0
#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
1234
0
            sig_algs_len += sizeof(uint16_t);
1235
0
#endif
1236
1237
0
#if defined(MBEDTLS_RSA_C)
1238
0
            sig_algs_len += sizeof(uint16_t);
1239
0
#endif
1240
0
            if (sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN) {
1241
0
                return MBEDTLS_ERR_SSL_BAD_CONFIG;
1242
0
            }
1243
0
        }
1244
1245
0
        if (sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN) {
1246
0
            return MBEDTLS_ERR_SSL_BAD_CONFIG;
1247
0
        }
1248
1249
0
        ssl->handshake->sig_algs = mbedtls_calloc(1, sig_algs_len +
1250
0
                                                  sizeof(uint16_t));
1251
0
        if (ssl->handshake->sig_algs == NULL) {
1252
0
            return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1253
0
        }
1254
1255
0
        p = (uint16_t *) ssl->handshake->sig_algs;
1256
0
        for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) {
1257
0
            unsigned char hash = mbedtls_ssl_hash_from_md_alg(*md);
1258
0
            if (hash == MBEDTLS_SSL_HASH_NONE) {
1259
0
                continue;
1260
0
            }
1261
0
#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
1262
0
            *p = ((hash << 8) | MBEDTLS_SSL_SIG_ECDSA);
1263
0
            p++;
1264
0
#endif
1265
0
#if defined(MBEDTLS_RSA_C)
1266
0
            *p = ((hash << 8) | MBEDTLS_SSL_SIG_RSA);
1267
0
            p++;
1268
0
#endif
1269
0
        }
1270
0
        *p = MBEDTLS_TLS_SIG_NONE;
1271
0
        ssl->handshake->sig_algs_heap_allocated = 1;
1272
0
    } else
1273
10.7k
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1274
10.7k
    {
1275
10.7k
        ssl->handshake->sig_algs_heap_allocated = 0;
1276
10.7k
    }
1277
10.7k
#endif /* !MBEDTLS_DEPRECATED_REMOVED */
1278
10.7k
#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
1279
10.7k
    return 0;
1280
10.7k
}
1281
1282
#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
1283
/* Dummy cookie callbacks for defaults */
1284
MBEDTLS_CHECK_RETURN_CRITICAL
1285
static int ssl_cookie_write_dummy(void *ctx,
1286
                                  unsigned char **p, unsigned char *end,
1287
                                  const unsigned char *cli_id, size_t cli_id_len)
1288
0
{
1289
0
    ((void) ctx);
1290
0
    ((void) p);
1291
0
    ((void) end);
1292
0
    ((void) cli_id);
1293
0
    ((void) cli_id_len);
1294
1295
0
    return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
1296
0
}
1297
1298
MBEDTLS_CHECK_RETURN_CRITICAL
1299
static int ssl_cookie_check_dummy(void *ctx,
1300
                                  const unsigned char *cookie, size_t cookie_len,
1301
                                  const unsigned char *cli_id, size_t cli_id_len)
1302
0
{
1303
0
    ((void) ctx);
1304
0
    ((void) cookie);
1305
0
    ((void) cookie_len);
1306
0
    ((void) cli_id);
1307
0
    ((void) cli_id_len);
1308
1309
0
    return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
1310
0
}
1311
#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
1312
1313
/*
1314
 * Initialize an SSL context
1315
 */
1316
void mbedtls_ssl_init(mbedtls_ssl_context *ssl)
1317
9.01k
{
1318
9.01k
    memset(ssl, 0, sizeof(mbedtls_ssl_context));
1319
9.01k
}
1320
1321
MBEDTLS_CHECK_RETURN_CRITICAL
1322
static int ssl_conf_version_check(const mbedtls_ssl_context *ssl)
1323
8.97k
{
1324
8.97k
    const mbedtls_ssl_config *conf = ssl->conf;
1325
1326
8.97k
#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1327
8.97k
    if (mbedtls_ssl_conf_is_tls13_only(conf)) {
1328
0
        if (conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1329
0
            MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS 1.3 is not yet supported."));
1330
0
            return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
1331
0
        }
1332
1333
0
        MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls13 only."));
1334
0
        return 0;
1335
0
    }
1336
8.97k
#endif
1337
1338
8.97k
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1339
8.97k
    if (mbedtls_ssl_conf_is_tls12_only(conf)) {
1340
7.16k
        MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls12 only."));
1341
7.16k
        return 0;
1342
7.16k
    }
1343
1.80k
#endif
1344
1345
1.80k
#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
1346
1.80k
    if (mbedtls_ssl_conf_is_hybrid_tls12_tls13(conf)) {
1347
1.80k
        if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1348
0
            MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2"));
1349
0
            return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
1350
0
        }
1351
1352
1.80k
        MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is TLS 1.3 or TLS 1.2."));
1353
1.80k
        return 0;
1354
1.80k
    }
1355
0
#endif
1356
1357
0
    MBEDTLS_SSL_DEBUG_MSG(1, ("The SSL configuration is invalid."));
1358
0
    return MBEDTLS_ERR_SSL_BAD_CONFIG;
1359
1.80k
}
1360
1361
MBEDTLS_CHECK_RETURN_CRITICAL
1362
static int ssl_conf_check(const mbedtls_ssl_context *ssl)
1363
8.97k
{
1364
8.97k
    int ret;
1365
8.97k
    ret = ssl_conf_version_check(ssl);
1366
8.97k
    if (ret != 0) {
1367
0
        return ret;
1368
0
    }
1369
1370
8.97k
    if (ssl->conf->f_rng == NULL) {
1371
0
        MBEDTLS_SSL_DEBUG_MSG(1, ("no RNG provided"));
1372
0
        return MBEDTLS_ERR_SSL_NO_RNG;
1373
0
    }
1374
1375
    /* Space for further checks */
1376
1377
8.97k
    return 0;
1378
8.97k
}
1379
1380
/*
1381
 * Setup an SSL context
1382
 */
1383
1384
int mbedtls_ssl_setup(mbedtls_ssl_context *ssl,
1385
                      const mbedtls_ssl_config *conf)
1386
8.97k
{
1387
8.97k
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1388
8.97k
    size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1389
8.97k
    size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1390
1391
8.97k
    ssl->conf = conf;
1392
1393
8.97k
    if ((ret = ssl_conf_check(ssl)) != 0) {
1394
0
        return ret;
1395
0
    }
1396
8.97k
    ssl->tls_version = ssl->conf->max_tls_version;
1397
1398
    /*
1399
     * Prepare base structures
1400
     */
1401
1402
    /* Set to NULL in case of an error condition */
1403
8.97k
    ssl->out_buf = NULL;
1404
1405
8.97k
#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1406
8.97k
    ssl->in_buf_len = in_buf_len;
1407
8.97k
#endif
1408
8.97k
    ssl->in_buf = mbedtls_calloc(1, in_buf_len);
1409
8.97k
    if (ssl->in_buf == NULL) {
1410
0
        MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len));
1411
0
        ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1412
0
        goto error;
1413
0
    }
1414
1415
8.97k
#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1416
8.97k
    ssl->out_buf_len = out_buf_len;
1417
8.97k
#endif
1418
8.97k
    ssl->out_buf = mbedtls_calloc(1, out_buf_len);
1419
8.97k
    if (ssl->out_buf == NULL) {
1420
0
        MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len));
1421
0
        ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1422
0
        goto error;
1423
0
    }
1424
1425
8.97k
    mbedtls_ssl_reset_in_pointers(ssl);
1426
8.97k
    mbedtls_ssl_reset_out_pointers(ssl);
1427
1428
8.97k
#if defined(MBEDTLS_SSL_DTLS_SRTP)
1429
8.97k
    memset(&ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info));
1430
8.97k
#endif
1431
1432
8.97k
    if ((ret = ssl_handshake_init(ssl)) != 0) {
1433
0
        goto error;
1434
0
    }
1435
1436
8.97k
    return 0;
1437
1438
0
error:
1439
0
    mbedtls_free(ssl->in_buf);
1440
0
    mbedtls_free(ssl->out_buf);
1441
1442
0
    ssl->conf = NULL;
1443
1444
0
#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1445
0
    ssl->in_buf_len = 0;
1446
0
    ssl->out_buf_len = 0;
1447
0
#endif
1448
0
    ssl->in_buf = NULL;
1449
0
    ssl->out_buf = NULL;
1450
1451
0
    ssl->in_hdr = NULL;
1452
0
    ssl->in_ctr = NULL;
1453
0
    ssl->in_len = NULL;
1454
0
    ssl->in_iv = NULL;
1455
0
    ssl->in_msg = NULL;
1456
1457
0
    ssl->out_hdr = NULL;
1458
0
    ssl->out_ctr = NULL;
1459
0
    ssl->out_len = NULL;
1460
0
    ssl->out_iv = NULL;
1461
0
    ssl->out_msg = NULL;
1462
1463
0
    return ret;
1464
8.97k
}
1465
1466
/*
1467
 * Reset an initialized and used SSL context for re-use while retaining
1468
 * all application-set variables, function pointers and data.
1469
 *
1470
 * If partial is non-zero, keep data in the input buffer and client ID.
1471
 * (Use when a DTLS client reconnects from the same port.)
1472
 */
1473
void mbedtls_ssl_session_reset_msg_layer(mbedtls_ssl_context *ssl,
1474
                                         int partial)
1475
1.77k
{
1476
1.77k
#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1477
1.77k
    size_t in_buf_len = ssl->in_buf_len;
1478
1.77k
    size_t out_buf_len = ssl->out_buf_len;
1479
#else
1480
    size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1481
    size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1482
#endif
1483
1484
#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C)
1485
    partial = 0;
1486
#endif
1487
1488
    /* Cancel any possibly running timer */
1489
1.77k
    mbedtls_ssl_set_timer(ssl, 0);
1490
1491
1.77k
    mbedtls_ssl_reset_in_pointers(ssl);
1492
1.77k
    mbedtls_ssl_reset_out_pointers(ssl);
1493
1494
    /* Reset incoming message parsing */
1495
1.77k
    ssl->in_offt    = NULL;
1496
1.77k
    ssl->nb_zero    = 0;
1497
1.77k
    ssl->in_msgtype = 0;
1498
1.77k
    ssl->in_msglen  = 0;
1499
1.77k
    ssl->in_hslen   = 0;
1500
1.77k
    ssl->keep_current_message = 0;
1501
1.77k
    ssl->transform_in  = NULL;
1502
1503
    /* TLS: reset in_hsfraglen, which is part of message parsing.
1504
     * DTLS: on a client reconnect, don't reset badmac_seen. */
1505
1.77k
    if (!partial) {
1506
1.77k
        ssl->badmac_seen_or_in_hsfraglen = 0;
1507
1.77k
    }
1508
1509
1.77k
#if defined(MBEDTLS_SSL_PROTO_DTLS)
1510
1.77k
    ssl->next_record_offset = 0;
1511
1.77k
    ssl->in_epoch = 0;
1512
1.77k
#endif
1513
1514
    /* Keep current datagram if partial == 1 */
1515
1.77k
    if (partial == 0) {
1516
1.77k
        ssl->in_left = 0;
1517
1.77k
        memset(ssl->in_buf, 0, in_buf_len);
1518
1.77k
    }
1519
1520
1.77k
    ssl->send_alert = 0;
1521
1.77k
    ssl->alert_reason = 0;
1522
1.77k
    ssl->alert_type = 0;
1523
1524
    /* Reset outgoing message writing */
1525
1.77k
    ssl->out_msgtype = 0;
1526
1.77k
    ssl->out_msglen  = 0;
1527
1.77k
    ssl->out_left    = 0;
1528
1.77k
    memset(ssl->out_buf, 0, out_buf_len);
1529
1.77k
    memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
1530
1.77k
    ssl->transform_out = NULL;
1531
1532
1.77k
#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
1533
1.77k
    mbedtls_ssl_dtls_replay_reset(ssl);
1534
1.77k
#endif
1535
1536
1.77k
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1537
1.77k
    if (ssl->transform) {
1538
0
        mbedtls_ssl_transform_free(ssl->transform);
1539
0
        mbedtls_free(ssl->transform);
1540
0
        ssl->transform = NULL;
1541
0
    }
1542
1.77k
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1543
1544
1.77k
#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1545
1.77k
    mbedtls_ssl_transform_free(ssl->transform_application);
1546
1.77k
    mbedtls_free(ssl->transform_application);
1547
1.77k
    ssl->transform_application = NULL;
1548
1549
1.77k
    if (ssl->handshake != NULL) {
1550
1.77k
#if defined(MBEDTLS_SSL_EARLY_DATA)
1551
1.77k
        mbedtls_ssl_transform_free(ssl->handshake->transform_earlydata);
1552
1.77k
        mbedtls_free(ssl->handshake->transform_earlydata);
1553
1.77k
        ssl->handshake->transform_earlydata = NULL;
1554
1.77k
#endif
1555
1556
1.77k
        mbedtls_ssl_transform_free(ssl->handshake->transform_handshake);
1557
1.77k
        mbedtls_free(ssl->handshake->transform_handshake);
1558
1.77k
        ssl->handshake->transform_handshake = NULL;
1559
1.77k
    }
1560
1561
1.77k
#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1562
1.77k
}
1563
1564
int mbedtls_ssl_session_reset_int(mbedtls_ssl_context *ssl, int partial)
1565
1.77k
{
1566
1.77k
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1567
1568
1.77k
    mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HELLO_REQUEST);
1569
1.77k
    ssl->tls_version = ssl->conf->max_tls_version;
1570
1571
1.77k
    mbedtls_ssl_session_reset_msg_layer(ssl, partial);
1572
1573
    /* Reset renegotiation state */
1574
1.77k
#if defined(MBEDTLS_SSL_RENEGOTIATION)
1575
1.77k
    ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
1576
1.77k
    ssl->renego_records_seen = 0;
1577
1578
1.77k
    ssl->verify_data_len = 0;
1579
1.77k
    memset(ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
1580
1.77k
    memset(ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
1581
1.77k
#endif
1582
1.77k
    ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
1583
1584
1.77k
    ssl->session_in  = NULL;
1585
1.77k
    ssl->session_out = NULL;
1586
1.77k
    if (ssl->session) {
1587
0
        mbedtls_ssl_session_free(ssl->session);
1588
0
        mbedtls_free(ssl->session);
1589
0
        ssl->session = NULL;
1590
0
    }
1591
1592
1.77k
#if defined(MBEDTLS_SSL_ALPN)
1593
1.77k
    ssl->alpn_chosen = NULL;
1594
1.77k
#endif
1595
1596
1.77k
#if defined(MBEDTLS_SSL_DTLS_SRTP)
1597
1.77k
    memset(&ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info));
1598
1.77k
#endif /* MBEDTLS_SSL_DTLS_SRTP */
1599
1600
1.77k
#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
1601
1.77k
    int free_cli_id = 1;
1602
1.77k
#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
1603
1.77k
    free_cli_id = (partial == 0);
1604
1.77k
#endif
1605
1.77k
    if (free_cli_id) {
1606
1.77k
        mbedtls_free(ssl->cli_id);
1607
1.77k
        ssl->cli_id = NULL;
1608
1.77k
        ssl->cli_id_len = 0;
1609
1.77k
    }
1610
1.77k
#endif
1611
1612
1.77k
    if ((ret = ssl_handshake_init(ssl)) != 0) {
1613
0
        return ret;
1614
0
    }
1615
1616
1.77k
    return 0;
1617
1.77k
}
1618
1619
/*
1620
 * Reset an initialized and used SSL context for re-use while retaining
1621
 * all application-set variables, function pointers and data.
1622
 */
1623
int mbedtls_ssl_session_reset(mbedtls_ssl_context *ssl)
1624
1.77k
{
1625
1.77k
    return mbedtls_ssl_session_reset_int(ssl, 0);
1626
1.77k
}
1627
1628
/*
1629
 * SSL set accessors
1630
 */
1631
void mbedtls_ssl_conf_endpoint(mbedtls_ssl_config *conf, int endpoint)
1632
8.97k
{
1633
8.97k
    conf->endpoint   = endpoint;
1634
8.97k
}
1635
1636
void mbedtls_ssl_conf_transport(mbedtls_ssl_config *conf, int transport)
1637
8.97k
{
1638
8.97k
    conf->transport = transport;
1639
8.97k
}
1640
1641
#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
1642
void mbedtls_ssl_conf_dtls_anti_replay(mbedtls_ssl_config *conf, char mode)
1643
0
{
1644
0
    conf->anti_replay = mode;
1645
0
}
1646
#endif
1647
1648
void mbedtls_ssl_conf_dtls_badmac_limit(mbedtls_ssl_config *conf, unsigned limit)
1649
0
{
1650
0
    conf->badmac_limit = limit;
1651
0
}
1652
1653
#if defined(MBEDTLS_SSL_PROTO_DTLS)
1654
1655
void mbedtls_ssl_set_datagram_packing(mbedtls_ssl_context *ssl,
1656
                                      unsigned allow_packing)
1657
0
{
1658
0
    ssl->disable_datagram_packing = !allow_packing;
1659
0
}
1660
1661
void mbedtls_ssl_conf_handshake_timeout(mbedtls_ssl_config *conf,
1662
                                        uint32_t min, uint32_t max)
1663
0
{
1664
0
    conf->hs_timeout_min = min;
1665
0
    conf->hs_timeout_max = max;
1666
0
}
1667
#endif
1668
1669
void mbedtls_ssl_conf_authmode(mbedtls_ssl_config *conf, int authmode)
1670
5.33k
{
1671
5.33k
    conf->authmode   = authmode;
1672
5.33k
}
1673
1674
#if defined(MBEDTLS_X509_CRT_PARSE_C)
1675
void mbedtls_ssl_conf_verify(mbedtls_ssl_config *conf,
1676
                             int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1677
                             void *p_vrfy)
1678
0
{
1679
0
    conf->f_vrfy      = f_vrfy;
1680
0
    conf->p_vrfy      = p_vrfy;
1681
0
}
1682
#endif /* MBEDTLS_X509_CRT_PARSE_C */
1683
1684
void mbedtls_ssl_conf_rng(mbedtls_ssl_config *conf,
1685
                          int (*f_rng)(void *, unsigned char *, size_t),
1686
                          void *p_rng)
1687
8.97k
{
1688
8.97k
    conf->f_rng      = f_rng;
1689
8.97k
    conf->p_rng      = p_rng;
1690
8.97k
}
1691
1692
void mbedtls_ssl_conf_dbg(mbedtls_ssl_config *conf,
1693
                          void (*f_dbg)(void *, int, const char *, int, const char *),
1694
                          void  *p_dbg)
1695
0
{
1696
0
    conf->f_dbg      = f_dbg;
1697
0
    conf->p_dbg      = p_dbg;
1698
0
}
1699
1700
void mbedtls_ssl_set_bio(mbedtls_ssl_context *ssl,
1701
                         void *p_bio,
1702
                         mbedtls_ssl_send_t *f_send,
1703
                         mbedtls_ssl_recv_t *f_recv,
1704
                         mbedtls_ssl_recv_timeout_t *f_recv_timeout)
1705
8.97k
{
1706
8.97k
    ssl->p_bio          = p_bio;
1707
8.97k
    ssl->f_send         = f_send;
1708
8.97k
    ssl->f_recv         = f_recv;
1709
8.97k
    ssl->f_recv_timeout = f_recv_timeout;
1710
8.97k
}
1711
1712
#if defined(MBEDTLS_SSL_PROTO_DTLS)
1713
void mbedtls_ssl_set_mtu(mbedtls_ssl_context *ssl, uint16_t mtu)
1714
0
{
1715
0
    ssl->mtu = mtu;
1716
0
}
1717
#endif
1718
1719
void mbedtls_ssl_conf_read_timeout(mbedtls_ssl_config *conf, uint32_t timeout)
1720
0
{
1721
0
    conf->read_timeout   = timeout;
1722
0
}
1723
1724
void mbedtls_ssl_set_timer_cb(mbedtls_ssl_context *ssl,
1725
                              void *p_timer,
1726
                              mbedtls_ssl_set_timer_t *f_set_timer,
1727
                              mbedtls_ssl_get_timer_t *f_get_timer)
1728
7.16k
{
1729
7.16k
    ssl->p_timer        = p_timer;
1730
7.16k
    ssl->f_set_timer    = f_set_timer;
1731
7.16k
    ssl->f_get_timer    = f_get_timer;
1732
1733
    /* Make sure we start with no timer running */
1734
7.16k
    mbedtls_ssl_set_timer(ssl, 0);
1735
7.16k
}
1736
1737
#if defined(MBEDTLS_SSL_SRV_C)
1738
void mbedtls_ssl_conf_session_cache(mbedtls_ssl_config *conf,
1739
                                    void *p_cache,
1740
                                    mbedtls_ssl_cache_get_t *f_get_cache,
1741
                                    mbedtls_ssl_cache_set_t *f_set_cache)
1742
0
{
1743
0
    conf->p_cache = p_cache;
1744
0
    conf->f_get_cache = f_get_cache;
1745
0
    conf->f_set_cache = f_set_cache;
1746
0
}
1747
#endif /* MBEDTLS_SSL_SRV_C */
1748
1749
#if defined(MBEDTLS_SSL_CLI_C)
1750
int mbedtls_ssl_set_session(mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session)
1751
0
{
1752
0
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1753
1754
0
    if (ssl == NULL ||
1755
0
        session == NULL ||
1756
0
        ssl->session_negotiate == NULL ||
1757
0
        ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
1758
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
1759
0
    }
1760
1761
0
    if (ssl->handshake->resume == 1) {
1762
0
        return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
1763
0
    }
1764
1765
0
#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1766
0
    if (session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
1767
0
#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1768
0
        const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
1769
0
            mbedtls_ssl_ciphersuite_from_id(session->ciphersuite);
1770
1771
0
        if (mbedtls_ssl_validate_ciphersuite(
1772
0
                ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3,
1773
0
                MBEDTLS_SSL_VERSION_TLS1_3) != 0) {
1774
0
            MBEDTLS_SSL_DEBUG_MSG(4, ("%d is not a valid TLS 1.3 ciphersuite.",
1775
0
                                      session->ciphersuite));
1776
0
            return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
1777
0
        }
1778
#else
1779
        /*
1780
         * If session tickets are not enabled, it is not possible to resume a
1781
         * TLS 1.3 session, thus do not make any change to the SSL context in
1782
         * the first place.
1783
         */
1784
        return 0;
1785
#endif
1786
0
    }
1787
0
#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1788
1789
0
    if ((ret = mbedtls_ssl_session_copy(ssl->session_negotiate,
1790
0
                                        session)) != 0) {
1791
0
        return ret;
1792
0
    }
1793
1794
0
    ssl->handshake->resume = 1;
1795
1796
0
    return 0;
1797
0
}
1798
#endif /* MBEDTLS_SSL_CLI_C */
1799
1800
void mbedtls_ssl_conf_ciphersuites(mbedtls_ssl_config *conf,
1801
                                   const int *ciphersuites)
1802
0
{
1803
0
    conf->ciphersuite_list = ciphersuites;
1804
0
}
1805
1806
#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1807
void mbedtls_ssl_conf_tls13_key_exchange_modes(mbedtls_ssl_config *conf,
1808
                                               const int kex_modes)
1809
0
{
1810
0
    conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
1811
0
}
1812
1813
#if defined(MBEDTLS_SSL_EARLY_DATA)
1814
void mbedtls_ssl_conf_early_data(mbedtls_ssl_config *conf,
1815
                                 int early_data_enabled)
1816
8.97k
{
1817
8.97k
    conf->early_data_enabled = early_data_enabled;
1818
8.97k
}
1819
1820
#if defined(MBEDTLS_SSL_SRV_C)
1821
void mbedtls_ssl_conf_max_early_data_size(
1822
    mbedtls_ssl_config *conf, uint32_t max_early_data_size)
1823
8.97k
{
1824
8.97k
    conf->max_early_data_size = max_early_data_size;
1825
8.97k
}
1826
#endif /* MBEDTLS_SSL_SRV_C */
1827
1828
#endif /* MBEDTLS_SSL_EARLY_DATA */
1829
#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1830
1831
#if defined(MBEDTLS_X509_CRT_PARSE_C)
1832
void mbedtls_ssl_conf_cert_profile(mbedtls_ssl_config *conf,
1833
                                   const mbedtls_x509_crt_profile *profile)
1834
0
{
1835
0
    conf->cert_profile = profile;
1836
0
}
1837
1838
static void ssl_key_cert_free(mbedtls_ssl_key_cert *key_cert)
1839
19.7k
{
1840
19.7k
    mbedtls_ssl_key_cert *cur = key_cert, *next;
1841
1842
23.3k
    while (cur != NULL) {
1843
3.63k
        next = cur->next;
1844
3.63k
        mbedtls_free(cur);
1845
3.63k
        cur = next;
1846
3.63k
    }
1847
19.7k
}
1848
1849
/* Append a new keycert entry to a (possibly empty) list */
1850
MBEDTLS_CHECK_RETURN_CRITICAL
1851
static int ssl_append_key_cert(mbedtls_ssl_key_cert **head,
1852
                               mbedtls_x509_crt *cert,
1853
                               mbedtls_pk_context *key)
1854
3.63k
{
1855
3.63k
    mbedtls_ssl_key_cert *new_cert;
1856
1857
3.63k
    if (cert == NULL) {
1858
        /* Free list if cert is null */
1859
0
        ssl_key_cert_free(*head);
1860
0
        *head = NULL;
1861
0
        return 0;
1862
0
    }
1863
1864
3.63k
    new_cert = mbedtls_calloc(1, sizeof(mbedtls_ssl_key_cert));
1865
3.63k
    if (new_cert == NULL) {
1866
0
        return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1867
0
    }
1868
1869
3.63k
    new_cert->cert = cert;
1870
3.63k
    new_cert->key  = key;
1871
3.63k
    new_cert->next = NULL;
1872
1873
    /* Update head if the list was null, else add to the end */
1874
3.63k
    if (*head == NULL) {
1875
3.63k
        *head = new_cert;
1876
3.63k
    } else {
1877
0
        mbedtls_ssl_key_cert *cur = *head;
1878
0
        while (cur->next != NULL) {
1879
0
            cur = cur->next;
1880
0
        }
1881
0
        cur->next = new_cert;
1882
0
    }
1883
1884
3.63k
    return 0;
1885
3.63k
}
1886
1887
int mbedtls_ssl_conf_own_cert(mbedtls_ssl_config *conf,
1888
                              mbedtls_x509_crt *own_cert,
1889
                              mbedtls_pk_context *pk_key)
1890
3.63k
{
1891
3.63k
    return ssl_append_key_cert(&conf->key_cert, own_cert, pk_key);
1892
3.63k
}
1893
1894
void mbedtls_ssl_conf_ca_chain(mbedtls_ssl_config *conf,
1895
                               mbedtls_x509_crt *ca_chain,
1896
                               mbedtls_x509_crl *ca_crl)
1897
8.95k
{
1898
8.95k
    conf->ca_chain   = ca_chain;
1899
8.95k
    conf->ca_crl     = ca_crl;
1900
1901
8.95k
#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1902
    /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1903
     * cannot be used together. */
1904
8.95k
    conf->f_ca_cb = NULL;
1905
8.95k
    conf->p_ca_cb = NULL;
1906
8.95k
#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
1907
8.95k
}
1908
1909
#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1910
void mbedtls_ssl_conf_ca_cb(mbedtls_ssl_config *conf,
1911
                            mbedtls_x509_crt_ca_cb_t f_ca_cb,
1912
                            void *p_ca_cb)
1913
0
{
1914
0
    conf->f_ca_cb = f_ca_cb;
1915
0
    conf->p_ca_cb = p_ca_cb;
1916
1917
    /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1918
     * cannot be used together. */
1919
0
    conf->ca_chain   = NULL;
1920
0
    conf->ca_crl     = NULL;
1921
0
}
1922
#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
1923
#endif /* MBEDTLS_X509_CRT_PARSE_C */
1924
1925
#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1926
const unsigned char *mbedtls_ssl_get_hs_sni(mbedtls_ssl_context *ssl,
1927
                                            size_t *name_len)
1928
0
{
1929
0
    *name_len = ssl->handshake->sni_name_len;
1930
0
    return ssl->handshake->sni_name;
1931
0
}
1932
1933
int mbedtls_ssl_set_hs_own_cert(mbedtls_ssl_context *ssl,
1934
                                mbedtls_x509_crt *own_cert,
1935
                                mbedtls_pk_context *pk_key)
1936
0
{
1937
0
    return ssl_append_key_cert(&ssl->handshake->sni_key_cert,
1938
0
                               own_cert, pk_key);
1939
0
}
1940
1941
void mbedtls_ssl_set_hs_ca_chain(mbedtls_ssl_context *ssl,
1942
                                 mbedtls_x509_crt *ca_chain,
1943
                                 mbedtls_x509_crl *ca_crl)
1944
0
{
1945
0
    ssl->handshake->sni_ca_chain   = ca_chain;
1946
0
    ssl->handshake->sni_ca_crl     = ca_crl;
1947
0
}
1948
1949
#if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
1950
void mbedtls_ssl_set_hs_dn_hints(mbedtls_ssl_context *ssl,
1951
                                 const mbedtls_x509_crt *crt)
1952
0
{
1953
0
    ssl->handshake->dn_hints = crt;
1954
0
}
1955
#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
1956
1957
void mbedtls_ssl_set_hs_authmode(mbedtls_ssl_context *ssl,
1958
                                 int authmode)
1959
0
{
1960
0
    ssl->handshake->sni_authmode = authmode;
1961
0
}
1962
#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1963
1964
#if defined(MBEDTLS_X509_CRT_PARSE_C)
1965
void mbedtls_ssl_set_verify(mbedtls_ssl_context *ssl,
1966
                            int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1967
                            void *p_vrfy)
1968
0
{
1969
0
    ssl->f_vrfy = f_vrfy;
1970
0
    ssl->p_vrfy = p_vrfy;
1971
0
}
1972
#endif
1973
1974
#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1975
1976
#if defined(MBEDTLS_USE_PSA_CRYPTO)
1977
static const uint8_t jpake_server_id[] = { 's', 'e', 'r', 'v', 'e', 'r' };
1978
static const uint8_t jpake_client_id[] = { 'c', 'l', 'i', 'e', 'n', 't' };
1979
1980
static psa_status_t mbedtls_ssl_set_hs_ecjpake_password_common(
1981
    mbedtls_ssl_context *ssl,
1982
    mbedtls_svc_key_id_t pwd)
1983
0
{
1984
0
    psa_status_t status;
1985
0
    psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
1986
0
    const uint8_t *user = NULL;
1987
0
    size_t user_len = 0;
1988
0
    const uint8_t *peer = NULL;
1989
0
    size_t peer_len = 0;
1990
0
    psa_pake_cs_set_algorithm(&cipher_suite, PSA_ALG_JPAKE);
1991
0
    psa_pake_cs_set_primitive(&cipher_suite,
1992
0
                              PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC,
1993
0
                                                 PSA_ECC_FAMILY_SECP_R1,
1994
0
                                                 256));
1995
0
    psa_pake_cs_set_hash(&cipher_suite, PSA_ALG_SHA_256);
1996
1997
0
    status = psa_pake_setup(&ssl->handshake->psa_pake_ctx, &cipher_suite);
1998
0
    if (status != PSA_SUCCESS) {
1999
0
        return status;
2000
0
    }
2001
2002
0
    if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
2003
0
        user = jpake_server_id;
2004
0
        user_len = sizeof(jpake_server_id);
2005
0
        peer = jpake_client_id;
2006
0
        peer_len = sizeof(jpake_client_id);
2007
0
    } else {
2008
0
        user = jpake_client_id;
2009
0
        user_len = sizeof(jpake_client_id);
2010
0
        peer = jpake_server_id;
2011
0
        peer_len = sizeof(jpake_server_id);
2012
0
    }
2013
2014
0
    status = psa_pake_set_user(&ssl->handshake->psa_pake_ctx, user, user_len);
2015
0
    if (status != PSA_SUCCESS) {
2016
0
        return status;
2017
0
    }
2018
2019
0
    status = psa_pake_set_peer(&ssl->handshake->psa_pake_ctx, peer, peer_len);
2020
0
    if (status != PSA_SUCCESS) {
2021
0
        return status;
2022
0
    }
2023
2024
0
    status = psa_pake_set_password_key(&ssl->handshake->psa_pake_ctx, pwd);
2025
0
    if (status != PSA_SUCCESS) {
2026
0
        return status;
2027
0
    }
2028
2029
0
    ssl->handshake->psa_pake_ctx_is_ok = 1;
2030
2031
0
    return PSA_SUCCESS;
2032
0
}
2033
2034
int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl,
2035
                                        const unsigned char *pw,
2036
                                        size_t pw_len)
2037
0
{
2038
0
    psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2039
0
    psa_status_t status;
2040
2041
0
    if (ssl->handshake == NULL || ssl->conf == NULL) {
2042
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2043
0
    }
2044
2045
    /* Empty password is not valid  */
2046
0
    if ((pw == NULL) || (pw_len == 0)) {
2047
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2048
0
    }
2049
2050
0
    psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
2051
0
    psa_set_key_algorithm(&attributes, PSA_ALG_JPAKE);
2052
0
    psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
2053
2054
0
    status = psa_import_key(&attributes, pw, pw_len,
2055
0
                            &ssl->handshake->psa_pake_password);
2056
0
    if (status != PSA_SUCCESS) {
2057
0
        return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2058
0
    }
2059
2060
0
    status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl,
2061
0
                                                        ssl->handshake->psa_pake_password);
2062
0
    if (status != PSA_SUCCESS) {
2063
0
        psa_destroy_key(ssl->handshake->psa_pake_password);
2064
0
        psa_pake_abort(&ssl->handshake->psa_pake_ctx);
2065
0
        return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2066
0
    }
2067
2068
0
    return 0;
2069
0
}
2070
2071
int mbedtls_ssl_set_hs_ecjpake_password_opaque(mbedtls_ssl_context *ssl,
2072
                                               mbedtls_svc_key_id_t pwd)
2073
0
{
2074
0
    psa_status_t status;
2075
2076
0
    if (ssl->handshake == NULL || ssl->conf == NULL) {
2077
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2078
0
    }
2079
2080
0
    if (mbedtls_svc_key_id_is_null(pwd)) {
2081
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2082
0
    }
2083
2084
0
    status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl, pwd);
2085
0
    if (status != PSA_SUCCESS) {
2086
0
        psa_pake_abort(&ssl->handshake->psa_pake_ctx);
2087
0
        return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2088
0
    }
2089
2090
0
    return 0;
2091
0
}
2092
#else /* MBEDTLS_USE_PSA_CRYPTO */
2093
int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl,
2094
                                        const unsigned char *pw,
2095
                                        size_t pw_len)
2096
0
{
2097
0
    mbedtls_ecjpake_role role;
2098
2099
0
    if (ssl->handshake == NULL || ssl->conf == NULL) {
2100
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2101
0
    }
2102
2103
    /* Empty password is not valid  */
2104
0
    if ((pw == NULL) || (pw_len == 0)) {
2105
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2106
0
    }
2107
2108
0
    if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
2109
0
        role = MBEDTLS_ECJPAKE_SERVER;
2110
0
    } else {
2111
0
        role = MBEDTLS_ECJPAKE_CLIENT;
2112
0
    }
2113
2114
0
    return mbedtls_ecjpake_setup(&ssl->handshake->ecjpake_ctx,
2115
0
                                 role,
2116
0
                                 MBEDTLS_MD_SHA256,
2117
0
                                 MBEDTLS_ECP_DP_SECP256R1,
2118
0
                                 pw, pw_len);
2119
0
}
2120
#endif /* MBEDTLS_USE_PSA_CRYPTO */
2121
#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2122
2123
#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
2124
int mbedtls_ssl_conf_has_static_psk(mbedtls_ssl_config const *conf)
2125
430k
{
2126
430k
    if (conf->psk_identity     == NULL ||
2127
429k
        conf->psk_identity_len == 0) {
2128
429k
        return 0;
2129
429k
    }
2130
2131
#if defined(MBEDTLS_USE_PSA_CRYPTO)
2132
    if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
2133
        return 1;
2134
    }
2135
#endif /* MBEDTLS_USE_PSA_CRYPTO */
2136
2137
1.11k
    if (conf->psk != NULL && conf->psk_len != 0) {
2138
1.11k
        return 1;
2139
1.11k
    }
2140
2141
0
    return 0;
2142
1.11k
}
2143
2144
static void ssl_conf_remove_psk(mbedtls_ssl_config *conf)
2145
0
{
2146
    /* Remove reference to existing PSK, if any. */
2147
#if defined(MBEDTLS_USE_PSA_CRYPTO)
2148
0
    if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
2149
        /* The maintenance of the PSK key slot is the
2150
         * user's responsibility. */
2151
0
        conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
2152
0
    }
2153
#endif /* MBEDTLS_USE_PSA_CRYPTO */
2154
0
    if (conf->psk != NULL) {
2155
0
        mbedtls_zeroize_and_free(conf->psk, conf->psk_len);
2156
0
        conf->psk = NULL;
2157
0
        conf->psk_len = 0;
2158
0
    }
2159
2160
    /* Remove reference to PSK identity, if any. */
2161
0
    if (conf->psk_identity != NULL) {
2162
0
        mbedtls_free(conf->psk_identity);
2163
0
        conf->psk_identity = NULL;
2164
0
        conf->psk_identity_len = 0;
2165
0
    }
2166
0
}
Unexecuted instantiation: ssl_tls.c:ssl_conf_remove_psk
Unexecuted instantiation: ssl_tls.c:ssl_conf_remove_psk
2167
2168
/* This function assumes that PSK identity in the SSL config is unset.
2169
 * It checks that the provided identity is well-formed and attempts
2170
 * to make a copy of it in the SSL config.
2171
 * On failure, the PSK identity in the config remains unset. */
2172
MBEDTLS_CHECK_RETURN_CRITICAL
2173
static int ssl_conf_set_psk_identity(mbedtls_ssl_config *conf,
2174
                                     unsigned char const *psk_identity,
2175
                                     size_t psk_identity_len)
2176
783
{
2177
    /* Identity len will be encoded on two bytes */
2178
783
    if (psk_identity               == NULL ||
2179
783
        psk_identity_len           == 0    ||
2180
783
        (psk_identity_len >> 16) != 0    ||
2181
783
        psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN) {
2182
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2183
0
    }
2184
2185
783
    conf->psk_identity = mbedtls_calloc(1, psk_identity_len);
2186
783
    if (conf->psk_identity == NULL) {
2187
0
        return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2188
0
    }
2189
2190
783
    conf->psk_identity_len = psk_identity_len;
2191
783
    memcpy(conf->psk_identity, psk_identity, conf->psk_identity_len);
2192
2193
783
    return 0;
2194
783
}
2195
2196
int mbedtls_ssl_conf_psk(mbedtls_ssl_config *conf,
2197
                         const unsigned char *psk, size_t psk_len,
2198
                         const unsigned char *psk_identity, size_t psk_identity_len)
2199
783
{
2200
783
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2201
2202
    /* We currently only support one PSK, raw or opaque. */
2203
783
    if (mbedtls_ssl_conf_has_static_psk(conf)) {
2204
0
        return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
2205
0
    }
2206
2207
    /* Check and set raw PSK */
2208
783
    if (psk == NULL) {
2209
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2210
0
    }
2211
783
    if (psk_len == 0) {
2212
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2213
0
    }
2214
783
    if (psk_len > MBEDTLS_PSK_MAX_LEN) {
2215
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2216
0
    }
2217
2218
783
    if ((conf->psk = mbedtls_calloc(1, psk_len)) == NULL) {
2219
0
        return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2220
0
    }
2221
783
    conf->psk_len = psk_len;
2222
783
    memcpy(conf->psk, psk, conf->psk_len);
2223
2224
    /* Check and set PSK Identity */
2225
783
    ret = ssl_conf_set_psk_identity(conf, psk_identity, psk_identity_len);
2226
783
    if (ret != 0) {
2227
0
        ssl_conf_remove_psk(conf);
2228
0
    }
2229
2230
783
    return ret;
2231
783
}
2232
2233
static void ssl_remove_psk(mbedtls_ssl_context *ssl)
2234
0
{
2235
#if defined(MBEDTLS_USE_PSA_CRYPTO)
2236
0
    if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
2237
        /* The maintenance of the external PSK key slot is the
2238
         * user's responsibility. */
2239
0
        if (ssl->handshake->psk_opaque_is_internal) {
2240
0
            psa_destroy_key(ssl->handshake->psk_opaque);
2241
0
            ssl->handshake->psk_opaque_is_internal = 0;
2242
0
        }
2243
0
        ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
2244
0
    }
2245
#else
2246
0
    if (ssl->handshake->psk != NULL) {
2247
0
        mbedtls_zeroize_and_free(ssl->handshake->psk,
2248
0
                                 ssl->handshake->psk_len);
2249
0
        ssl->handshake->psk_len = 0;
2250
0
        ssl->handshake->psk = NULL;
2251
0
    }
2252
#endif /* MBEDTLS_USE_PSA_CRYPTO */
2253
0
}
Unexecuted instantiation: ssl_tls.c:ssl_remove_psk
Unexecuted instantiation: ssl_tls.c:ssl_remove_psk
2254
2255
int mbedtls_ssl_set_hs_psk(mbedtls_ssl_context *ssl,
2256
                           const unsigned char *psk, size_t psk_len)
2257
0
{
2258
#if defined(MBEDTLS_USE_PSA_CRYPTO)
2259
    psa_key_attributes_t key_attributes = psa_key_attributes_init();
2260
0
    psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2261
0
    psa_algorithm_t alg = PSA_ALG_NONE;
2262
0
    mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2263
#endif /* MBEDTLS_USE_PSA_CRYPTO */
2264
2265
0
    if (psk == NULL || ssl->handshake == NULL) {
2266
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2267
0
    }
2268
2269
0
    if (psk_len > MBEDTLS_PSK_MAX_LEN) {
2270
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2271
0
    }
2272
2273
0
    ssl_remove_psk(ssl);
2274
2275
#if defined(MBEDTLS_USE_PSA_CRYPTO)
2276
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2277
0
    if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
2278
0
        if (ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
2279
0
            alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
2280
0
        } else {
2281
0
            alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
2282
0
        }
2283
0
        psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
2284
0
    }
2285
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2286
2287
#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
2288
0
    if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
2289
0
        alg = PSA_ALG_HKDF_EXTRACT(PSA_ALG_ANY_HASH);
2290
0
        psa_set_key_usage_flags(&key_attributes,
2291
0
                                PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT);
2292
0
    }
2293
#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2294
2295
    psa_set_key_algorithm(&key_attributes, alg);
2296
0
    psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE);
2297
2298
    status = psa_import_key(&key_attributes, psk, psk_len, &key);
2299
0
    if (status != PSA_SUCCESS) {
2300
0
        return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2301
0
    }
2302
2303
    /* Allow calling psa_destroy_key() on psk remove */
2304
0
    ssl->handshake->psk_opaque_is_internal = 1;
2305
0
    return mbedtls_ssl_set_hs_psk_opaque(ssl, key);
2306
#else
2307
0
    if ((ssl->handshake->psk = mbedtls_calloc(1, psk_len)) == NULL) {
2308
0
        return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2309
0
    }
2310
2311
0
    ssl->handshake->psk_len = psk_len;
2312
0
    memcpy(ssl->handshake->psk, psk, ssl->handshake->psk_len);
2313
2314
0
    return 0;
2315
#endif /* MBEDTLS_USE_PSA_CRYPTO */
2316
0
}
Unexecuted instantiation: mbedtls_ssl_set_hs_psk
Unexecuted instantiation: mbedtls_ssl_set_hs_psk
2317
2318
#if defined(MBEDTLS_USE_PSA_CRYPTO)
2319
int mbedtls_ssl_conf_psk_opaque(mbedtls_ssl_config *conf,
2320
                                mbedtls_svc_key_id_t psk,
2321
                                const unsigned char *psk_identity,
2322
                                size_t psk_identity_len)
2323
0
{
2324
0
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2325
2326
    /* We currently only support one PSK, raw or opaque. */
2327
0
    if (mbedtls_ssl_conf_has_static_psk(conf)) {
2328
0
        return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
2329
0
    }
2330
2331
    /* Check and set opaque PSK */
2332
0
    if (mbedtls_svc_key_id_is_null(psk)) {
2333
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2334
0
    }
2335
0
    conf->psk_opaque = psk;
2336
2337
    /* Check and set PSK Identity */
2338
0
    ret = ssl_conf_set_psk_identity(conf, psk_identity,
2339
0
                                    psk_identity_len);
2340
0
    if (ret != 0) {
2341
0
        ssl_conf_remove_psk(conf);
2342
0
    }
2343
2344
0
    return ret;
2345
0
}
2346
2347
int mbedtls_ssl_set_hs_psk_opaque(mbedtls_ssl_context *ssl,
2348
                                  mbedtls_svc_key_id_t psk)
2349
0
{
2350
0
    if ((mbedtls_svc_key_id_is_null(psk)) ||
2351
0
        (ssl->handshake == NULL)) {
2352
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2353
0
    }
2354
2355
0
    ssl_remove_psk(ssl);
2356
0
    ssl->handshake->psk_opaque = psk;
2357
0
    return 0;
2358
0
}
2359
#endif /* MBEDTLS_USE_PSA_CRYPTO */
2360
2361
#if defined(MBEDTLS_SSL_SRV_C)
2362
void mbedtls_ssl_conf_psk_cb(mbedtls_ssl_config *conf,
2363
                             int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
2364
                                          size_t),
2365
                             void *p_psk)
2366
0
{
2367
0
    conf->f_psk = f_psk;
2368
0
    conf->p_psk = p_psk;
2369
0
}
2370
#endif /* MBEDTLS_SSL_SRV_C */
2371
2372
#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
2373
2374
#if defined(MBEDTLS_USE_PSA_CRYPTO)
2375
static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
2376
    psa_algorithm_t alg)
2377
0
{
2378
0
#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
2379
0
    if (alg == PSA_ALG_CBC_NO_PADDING) {
2380
0
        return MBEDTLS_SSL_MODE_CBC;
2381
0
    }
2382
0
#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
2383
0
    if (PSA_ALG_IS_AEAD(alg)) {
2384
0
        return MBEDTLS_SSL_MODE_AEAD;
2385
0
    }
2386
0
    return MBEDTLS_SSL_MODE_STREAM;
2387
0
}
2388
2389
#else /* MBEDTLS_USE_PSA_CRYPTO */
2390
2391
static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
2392
    mbedtls_cipher_mode_t mode)
2393
12.2k
{
2394
12.2k
#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
2395
12.2k
    if (mode == MBEDTLS_MODE_CBC) {
2396
7.57k
        return MBEDTLS_SSL_MODE_CBC;
2397
7.57k
    }
2398
4.71k
#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
2399
2400
4.71k
#if defined(MBEDTLS_GCM_C) || \
2401
4.71k
    defined(MBEDTLS_CCM_C) || \
2402
4.71k
    defined(MBEDTLS_CHACHAPOLY_C)
2403
4.71k
    if (mode == MBEDTLS_MODE_GCM ||
2404
2.88k
        mode == MBEDTLS_MODE_CCM ||
2405
2.70k
        mode == MBEDTLS_MODE_CHACHAPOLY) {
2406
2.70k
        return MBEDTLS_SSL_MODE_AEAD;
2407
2.70k
    }
2408
2.01k
#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
2409
2410
2.01k
    return MBEDTLS_SSL_MODE_STREAM;
2411
4.71k
}
2412
#endif /* MBEDTLS_USE_PSA_CRYPTO */
2413
2414
static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode(
2415
    mbedtls_ssl_mode_t base_mode,
2416
    int encrypt_then_mac)
2417
12.2k
{
2418
12.2k
#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
2419
12.2k
    if (encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
2420
2.49k
        base_mode == MBEDTLS_SSL_MODE_CBC) {
2421
1.94k
        return MBEDTLS_SSL_MODE_CBC_ETM;
2422
1.94k
    }
2423
#else
2424
    (void) encrypt_then_mac;
2425
#endif
2426
10.3k
    return base_mode;
2427
12.2k
}
2428
2429
mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
2430
    const mbedtls_ssl_transform *transform)
2431
11.0k
{
2432
11.0k
    mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode(
2433
#if defined(MBEDTLS_USE_PSA_CRYPTO)
2434
        transform->psa_alg
2435
#else
2436
11.0k
        mbedtls_cipher_get_cipher_mode(&transform->cipher_ctx_enc)
2437
11.0k
#endif
2438
11.0k
        );
2439
2440
11.0k
    int encrypt_then_mac = 0;
2441
11.0k
#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
2442
11.0k
    encrypt_then_mac = transform->encrypt_then_mac;
2443
11.0k
#endif
2444
11.0k
    return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac);
2445
11.0k
}
2446
2447
mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
2448
#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
2449
    int encrypt_then_mac,
2450
#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
2451
    const mbedtls_ssl_ciphersuite_t *suite)
2452
1.25k
{
2453
1.25k
    mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM;
2454
2455
#if defined(MBEDTLS_USE_PSA_CRYPTO)
2456
    psa_status_t status;
2457
    psa_algorithm_t alg;
2458
    psa_key_type_t type;
2459
    size_t size;
2460
    status = mbedtls_ssl_cipher_to_psa((mbedtls_cipher_type_t) suite->cipher,
2461
                                       0, &alg, &type, &size);
2462
    if (status == PSA_SUCCESS) {
2463
        base_mode = mbedtls_ssl_get_base_mode(alg);
2464
    }
2465
#else
2466
1.25k
    const mbedtls_cipher_info_t *cipher =
2467
1.25k
        mbedtls_cipher_info_from_type((mbedtls_cipher_type_t) suite->cipher);
2468
1.25k
    if (cipher != NULL) {
2469
1.25k
        base_mode =
2470
1.25k
            mbedtls_ssl_get_base_mode(
2471
1.25k
                mbedtls_cipher_info_get_mode(cipher));
2472
1.25k
    }
2473
1.25k
#endif /* MBEDTLS_USE_PSA_CRYPTO */
2474
2475
#if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
2476
    int encrypt_then_mac = 0;
2477
#endif
2478
1.25k
    return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac);
2479
1.25k
}
2480
2481
#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
2482
2483
psa_status_t mbedtls_ssl_cipher_to_psa(mbedtls_cipher_type_t mbedtls_cipher_type,
2484
                                       size_t taglen,
2485
                                       psa_algorithm_t *alg,
2486
                                       psa_key_type_t *key_type,
2487
                                       size_t *key_size)
2488
0
{
2489
#if !defined(MBEDTLS_SSL_HAVE_CCM)
2490
    (void) taglen;
2491
#endif
2492
0
    switch (mbedtls_cipher_type) {
2493
0
#if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_CBC)
2494
0
        case MBEDTLS_CIPHER_AES_128_CBC:
2495
0
            *alg = PSA_ALG_CBC_NO_PADDING;
2496
0
            *key_type = PSA_KEY_TYPE_AES;
2497
0
            *key_size = 128;
2498
0
            break;
2499
0
#endif
2500
0
#if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_CCM)
2501
0
        case MBEDTLS_CIPHER_AES_128_CCM:
2502
0
            *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
2503
0
            *key_type = PSA_KEY_TYPE_AES;
2504
0
            *key_size = 128;
2505
0
            break;
2506
0
#endif
2507
0
#if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_GCM)
2508
0
        case MBEDTLS_CIPHER_AES_128_GCM:
2509
0
            *alg = PSA_ALG_GCM;
2510
0
            *key_type = PSA_KEY_TYPE_AES;
2511
0
            *key_size = 128;
2512
0
            break;
2513
0
#endif
2514
0
#if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_CCM)
2515
0
        case MBEDTLS_CIPHER_AES_192_CCM:
2516
0
            *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
2517
0
            *key_type = PSA_KEY_TYPE_AES;
2518
0
            *key_size = 192;
2519
0
            break;
2520
0
#endif
2521
0
#if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_GCM)
2522
0
        case MBEDTLS_CIPHER_AES_192_GCM:
2523
0
            *alg = PSA_ALG_GCM;
2524
0
            *key_type = PSA_KEY_TYPE_AES;
2525
0
            *key_size = 192;
2526
0
            break;
2527
0
#endif
2528
0
#if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_CBC)
2529
0
        case MBEDTLS_CIPHER_AES_256_CBC:
2530
0
            *alg = PSA_ALG_CBC_NO_PADDING;
2531
0
            *key_type = PSA_KEY_TYPE_AES;
2532
0
            *key_size = 256;
2533
0
            break;
2534
0
#endif
2535
0
#if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_CCM)
2536
0
        case MBEDTLS_CIPHER_AES_256_CCM:
2537
0
            *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
2538
0
            *key_type = PSA_KEY_TYPE_AES;
2539
0
            *key_size = 256;
2540
0
            break;
2541
0
#endif
2542
0
#if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_GCM)
2543
0
        case MBEDTLS_CIPHER_AES_256_GCM:
2544
0
            *alg = PSA_ALG_GCM;
2545
0
            *key_type = PSA_KEY_TYPE_AES;
2546
0
            *key_size = 256;
2547
0
            break;
2548
0
#endif
2549
0
#if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_CBC)
2550
0
        case MBEDTLS_CIPHER_ARIA_128_CBC:
2551
0
            *alg = PSA_ALG_CBC_NO_PADDING;
2552
0
            *key_type = PSA_KEY_TYPE_ARIA;
2553
0
            *key_size = 128;
2554
0
            break;
2555
0
#endif
2556
0
#if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_CCM)
2557
0
        case MBEDTLS_CIPHER_ARIA_128_CCM:
2558
0
            *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
2559
0
            *key_type = PSA_KEY_TYPE_ARIA;
2560
0
            *key_size = 128;
2561
0
            break;
2562
0
#endif
2563
0
#if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_GCM)
2564
0
        case MBEDTLS_CIPHER_ARIA_128_GCM:
2565
0
            *alg = PSA_ALG_GCM;
2566
0
            *key_type = PSA_KEY_TYPE_ARIA;
2567
0
            *key_size = 128;
2568
0
            break;
2569
0
#endif
2570
0
#if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_CCM)
2571
0
        case MBEDTLS_CIPHER_ARIA_192_CCM:
2572
0
            *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
2573
0
            *key_type = PSA_KEY_TYPE_ARIA;
2574
0
            *key_size = 192;
2575
0
            break;
2576
0
#endif
2577
0
#if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_GCM)
2578
0
        case MBEDTLS_CIPHER_ARIA_192_GCM:
2579
0
            *alg = PSA_ALG_GCM;
2580
0
            *key_type = PSA_KEY_TYPE_ARIA;
2581
0
            *key_size = 192;
2582
0
            break;
2583
0
#endif
2584
0
#if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_CBC)
2585
0
        case MBEDTLS_CIPHER_ARIA_256_CBC:
2586
0
            *alg = PSA_ALG_CBC_NO_PADDING;
2587
0
            *key_type = PSA_KEY_TYPE_ARIA;
2588
0
            *key_size = 256;
2589
0
            break;
2590
0
#endif
2591
0
#if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_CCM)
2592
0
        case MBEDTLS_CIPHER_ARIA_256_CCM:
2593
0
            *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
2594
0
            *key_type = PSA_KEY_TYPE_ARIA;
2595
0
            *key_size = 256;
2596
0
            break;
2597
0
#endif
2598
0
#if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_GCM)
2599
0
        case MBEDTLS_CIPHER_ARIA_256_GCM:
2600
0
            *alg = PSA_ALG_GCM;
2601
0
            *key_type = PSA_KEY_TYPE_ARIA;
2602
0
            *key_size = 256;
2603
0
            break;
2604
0
#endif
2605
0
#if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_CBC)
2606
0
        case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2607
0
            *alg = PSA_ALG_CBC_NO_PADDING;
2608
0
            *key_type = PSA_KEY_TYPE_CAMELLIA;
2609
0
            *key_size = 128;
2610
0
            break;
2611
0
#endif
2612
0
#if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_CCM)
2613
0
        case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
2614
0
            *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
2615
0
            *key_type = PSA_KEY_TYPE_CAMELLIA;
2616
0
            *key_size = 128;
2617
0
            break;
2618
0
#endif
2619
0
#if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_GCM)
2620
0
        case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2621
0
            *alg = PSA_ALG_GCM;
2622
0
            *key_type = PSA_KEY_TYPE_CAMELLIA;
2623
0
            *key_size = 128;
2624
0
            break;
2625
0
#endif
2626
0
#if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_CCM)
2627
0
        case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
2628
0
            *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
2629
0
            *key_type = PSA_KEY_TYPE_CAMELLIA;
2630
0
            *key_size = 192;
2631
0
            break;
2632
0
#endif
2633
0
#if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_GCM)
2634
0
        case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2635
0
            *alg = PSA_ALG_GCM;
2636
0
            *key_type = PSA_KEY_TYPE_CAMELLIA;
2637
0
            *key_size = 192;
2638
0
            break;
2639
0
#endif
2640
0
#if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_CBC)
2641
0
        case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2642
0
            *alg = PSA_ALG_CBC_NO_PADDING;
2643
0
            *key_type = PSA_KEY_TYPE_CAMELLIA;
2644
0
            *key_size = 256;
2645
0
            break;
2646
0
#endif
2647
0
#if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_CCM)
2648
0
        case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
2649
0
            *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
2650
0
            *key_type = PSA_KEY_TYPE_CAMELLIA;
2651
0
            *key_size = 256;
2652
0
            break;
2653
0
#endif
2654
0
#if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_GCM)
2655
0
        case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2656
0
            *alg = PSA_ALG_GCM;
2657
0
            *key_type = PSA_KEY_TYPE_CAMELLIA;
2658
0
            *key_size = 256;
2659
0
            break;
2660
0
#endif
2661
0
#if defined(MBEDTLS_SSL_HAVE_CHACHAPOLY)
2662
0
        case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2663
0
            *alg = PSA_ALG_CHACHA20_POLY1305;
2664
0
            *key_type = PSA_KEY_TYPE_CHACHA20;
2665
0
            *key_size = 256;
2666
0
            break;
2667
0
#endif
2668
0
        case MBEDTLS_CIPHER_NULL:
2669
0
            *alg = MBEDTLS_SSL_NULL_CIPHER;
2670
0
            *key_type = 0;
2671
0
            *key_size = 0;
2672
0
            break;
2673
0
        default:
2674
0
            return PSA_ERROR_NOT_SUPPORTED;
2675
0
    }
2676
2677
0
    return PSA_SUCCESS;
2678
0
}
2679
#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
2680
2681
#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
2682
int mbedtls_ssl_conf_dh_param_bin(mbedtls_ssl_config *conf,
2683
                                  const unsigned char *dhm_P, size_t P_len,
2684
                                  const unsigned char *dhm_G, size_t G_len)
2685
3.63k
{
2686
3.63k
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2687
2688
3.63k
    mbedtls_mpi_free(&conf->dhm_P);
2689
3.63k
    mbedtls_mpi_free(&conf->dhm_G);
2690
2691
3.63k
    if ((ret = mbedtls_mpi_read_binary(&conf->dhm_P, dhm_P, P_len)) != 0 ||
2692
3.63k
        (ret = mbedtls_mpi_read_binary(&conf->dhm_G, dhm_G, G_len)) != 0) {
2693
0
        mbedtls_mpi_free(&conf->dhm_P);
2694
0
        mbedtls_mpi_free(&conf->dhm_G);
2695
0
        return ret;
2696
0
    }
2697
2698
3.63k
    return 0;
2699
3.63k
}
2700
2701
int mbedtls_ssl_conf_dh_param_ctx(mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx)
2702
0
{
2703
0
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2704
2705
0
    mbedtls_mpi_free(&conf->dhm_P);
2706
0
    mbedtls_mpi_free(&conf->dhm_G);
2707
2708
0
    if ((ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_P,
2709
0
                                     &conf->dhm_P)) != 0 ||
2710
0
        (ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_G,
2711
0
                                     &conf->dhm_G)) != 0) {
2712
0
        mbedtls_mpi_free(&conf->dhm_P);
2713
0
        mbedtls_mpi_free(&conf->dhm_G);
2714
0
        return ret;
2715
0
    }
2716
2717
0
    return 0;
2718
0
}
2719
#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
2720
2721
#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2722
/*
2723
 * Set the minimum length for Diffie-Hellman parameters
2724
 */
2725
void mbedtls_ssl_conf_dhm_min_bitlen(mbedtls_ssl_config *conf,
2726
                                     unsigned int bitlen)
2727
0
{
2728
0
    conf->dhm_min_bitlen = bitlen;
2729
0
}
2730
#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2731
2732
#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
2733
#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
2734
/*
2735
 * Set allowed/preferred hashes for handshake signatures
2736
 */
2737
void mbedtls_ssl_conf_sig_hashes(mbedtls_ssl_config *conf,
2738
                                 const int *hashes)
2739
0
{
2740
0
    conf->sig_hashes = hashes;
2741
0
}
2742
#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
2743
2744
/* Configure allowed signature algorithms for handshake */
2745
void mbedtls_ssl_conf_sig_algs(mbedtls_ssl_config *conf,
2746
                               const uint16_t *sig_algs)
2747
0
{
2748
0
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2749
0
    conf->sig_hashes = NULL;
2750
0
#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2751
0
    conf->sig_algs = sig_algs;
2752
0
}
2753
#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
2754
2755
#if defined(MBEDTLS_ECP_C)
2756
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2757
/*
2758
 * Set the allowed elliptic curves
2759
 *
2760
 * mbedtls_ssl_setup() takes the provided list
2761
 * and translates it to a list of IANA TLS group identifiers,
2762
 * stored in ssl->handshake->group_list.
2763
 *
2764
 */
2765
void mbedtls_ssl_conf_curves(mbedtls_ssl_config *conf,
2766
                             const mbedtls_ecp_group_id *curve_list)
2767
0
{
2768
0
    conf->curve_list = curve_list;
2769
0
    conf->group_list = NULL;
2770
0
}
2771
#endif /* MBEDTLS_DEPRECATED_REMOVED */
2772
#endif /* MBEDTLS_ECP_C */
2773
2774
/*
2775
 * Set the allowed groups
2776
 */
2777
void mbedtls_ssl_conf_groups(mbedtls_ssl_config *conf,
2778
                             const uint16_t *group_list)
2779
0
{
2780
0
#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
2781
0
    conf->curve_list = NULL;
2782
0
#endif
2783
0
    conf->group_list = group_list;
2784
0
}
2785
2786
#if defined(MBEDTLS_X509_CRT_PARSE_C)
2787
2788
/* A magic value for `ssl->hostname` indicating that
2789
 * mbedtls_ssl_set_hostname() has been called with `NULL`.
2790
 * If mbedtls_ssl_set_hostname() has never been called on `ssl`, then
2791
 * `ssl->hostname == NULL`. */
2792
static const char *const ssl_hostname_skip_cn_verification = "";
2793
2794
#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
2795
/** Whether mbedtls_ssl_set_hostname() has been called.
2796
 *
2797
 * \param[in]   ssl     SSL context
2798
 *
2799
 * \return \c 1 if mbedtls_ssl_set_hostname() has been called on \p ssl
2800
 *         (including `mbedtls_ssl_set_hostname(ssl, NULL)`),
2801
 *         otherwise \c 0.
2802
 */
2803
static int mbedtls_ssl_has_set_hostname_been_called(
2804
    const mbedtls_ssl_context *ssl)
2805
0
{
2806
0
    return ssl->hostname != NULL;
2807
0
}
2808
#endif
2809
2810
/*
2811
 * Exported for ssl_client.c when SNI is enabled.  When SNI is off the only
2812
 * in-file caller is get_hostname_for_verification() which is guarded by
2813
 * MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED, so compile the function out
2814
 * entirely when neither macro is defined.
2815
 */
2816
#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) || \
2817
    defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
2818
#if !defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2819
static
2820
#endif
2821
const char *mbedtls_ssl_get_hostname_pointer(const mbedtls_ssl_context *ssl)
2822
11.6k
{
2823
11.6k
    if (ssl->hostname == ssl_hostname_skip_cn_verification) {
2824
0
        return NULL;
2825
0
    }
2826
11.6k
    return ssl->hostname;
2827
11.6k
}
2828
#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION || MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
2829
2830
static void mbedtls_ssl_free_hostname(mbedtls_ssl_context *ssl)
2831
14.3k
{
2832
14.3k
    if (ssl->hostname != NULL &&
2833
5.32k
        ssl->hostname != ssl_hostname_skip_cn_verification) {
2834
5.32k
        mbedtls_zeroize_and_free(ssl->hostname, strlen(ssl->hostname));
2835
5.32k
    }
2836
14.3k
    ssl->hostname = NULL;
2837
14.3k
}
2838
2839
int mbedtls_ssl_set_hostname(mbedtls_ssl_context *ssl, const char *hostname)
2840
5.32k
{
2841
    /* Initialize to suppress unnecessary compiler warning */
2842
5.32k
    size_t hostname_len = 0;
2843
2844
    /* Check if new hostname is valid before
2845
     * making any change to current one */
2846
5.32k
    if (hostname != NULL) {
2847
5.32k
        hostname_len = strlen(hostname);
2848
2849
5.32k
        if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) {
2850
0
            return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2851
0
        }
2852
5.32k
    }
2853
2854
    /* Now it's clear that we will overwrite the old hostname,
2855
     * so we can free it safely */
2856
5.32k
    mbedtls_ssl_free_hostname(ssl);
2857
2858
5.32k
    if (hostname == NULL) {
2859
        /* Passing NULL as hostname clears the old one, but leaves a
2860
         * special marker to indicate that mbedtls_ssl_set_hostname()
2861
         * has been called. */
2862
        /* ssl->hostname should be const, but isn't. We won't actually
2863
         * write to the buffer, so it's ok to cast away the const. */
2864
0
        ssl->hostname = (char *) ssl_hostname_skip_cn_verification;
2865
5.32k
    } else {
2866
5.32k
        ssl->hostname = mbedtls_calloc(1, hostname_len + 1);
2867
5.32k
        if (ssl->hostname == NULL) {
2868
            /* mbedtls_ssl_set_hostname() has been called, but unsuccessfully.
2869
             * Leave ssl->hostname in the same state as if the function had
2870
             * not been called, i.e. a null pointer. */
2871
0
            return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2872
0
        }
2873
2874
5.32k
        memcpy(ssl->hostname, hostname, hostname_len);
2875
2876
5.32k
        ssl->hostname[hostname_len] = '\0';
2877
5.32k
    }
2878
2879
5.32k
    return 0;
2880
5.32k
}
2881
#endif /* MBEDTLS_X509_CRT_PARSE_C */
2882
2883
#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2884
void mbedtls_ssl_conf_sni(mbedtls_ssl_config *conf,
2885
                          int (*f_sni)(void *, mbedtls_ssl_context *,
2886
                                       const unsigned char *, size_t),
2887
                          void *p_sni)
2888
0
{
2889
0
    conf->f_sni = f_sni;
2890
0
    conf->p_sni = p_sni;
2891
0
}
2892
#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
2893
2894
#if defined(MBEDTLS_SSL_ALPN)
2895
int mbedtls_ssl_conf_alpn_protocols(mbedtls_ssl_config *conf, const char **protos)
2896
796
{
2897
796
    size_t cur_len, tot_len;
2898
796
    const char **p;
2899
2900
    /*
2901
     * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2902
     * MUST NOT be truncated."
2903
     * We check lengths now rather than later.
2904
     */
2905
796
    tot_len = 0;
2906
2.38k
    for (p = protos; *p != NULL; p++) {
2907
1.59k
        cur_len = strlen(*p);
2908
1.59k
        tot_len += cur_len;
2909
2910
1.59k
        if ((cur_len == 0) ||
2911
1.59k
            (cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN) ||
2912
1.59k
            (tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN)) {
2913
0
            return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2914
0
        }
2915
1.59k
    }
2916
2917
796
    conf->alpn_list = protos;
2918
2919
796
    return 0;
2920
796
}
2921
2922
const char *mbedtls_ssl_get_alpn_protocol(const mbedtls_ssl_context *ssl)
2923
0
{
2924
0
    return ssl->alpn_chosen;
2925
0
}
2926
#endif /* MBEDTLS_SSL_ALPN */
2927
2928
#if defined(MBEDTLS_SSL_DTLS_SRTP)
2929
void mbedtls_ssl_conf_srtp_mki_value_supported(mbedtls_ssl_config *conf,
2930
                                               int support_mki_value)
2931
0
{
2932
0
    conf->dtls_srtp_mki_support = support_mki_value;
2933
0
}
2934
2935
int mbedtls_ssl_dtls_srtp_set_mki_value(mbedtls_ssl_context *ssl,
2936
                                        unsigned char *mki_value,
2937
                                        uint16_t mki_len)
2938
0
{
2939
0
    if (mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH) {
2940
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2941
0
    }
2942
2943
0
    if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED) {
2944
0
        return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
2945
0
    }
2946
2947
0
    memcpy(ssl->dtls_srtp_info.mki_value, mki_value, mki_len);
2948
0
    ssl->dtls_srtp_info.mki_len = mki_len;
2949
0
    return 0;
2950
0
}
2951
2952
int mbedtls_ssl_conf_dtls_srtp_protection_profiles(mbedtls_ssl_config *conf,
2953
                                                   const mbedtls_ssl_srtp_profile *profiles)
2954
0
{
2955
0
    const mbedtls_ssl_srtp_profile *p;
2956
0
    size_t list_size = 0;
2957
2958
    /* check the profiles list: all entry must be valid,
2959
     * its size cannot be more than the total number of supported profiles, currently 4 */
2960
0
    for (p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
2961
0
         list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
2962
0
         p++) {
2963
0
        if (mbedtls_ssl_check_srtp_profile_value(*p) != MBEDTLS_TLS_SRTP_UNSET) {
2964
0
            list_size++;
2965
0
        } else {
2966
            /* unsupported value, stop parsing and set the size to an error value */
2967
0
            list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
2968
0
        }
2969
0
    }
2970
2971
0
    if (list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH) {
2972
0
        conf->dtls_srtp_profile_list = NULL;
2973
0
        conf->dtls_srtp_profile_list_len = 0;
2974
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2975
0
    }
2976
2977
0
    conf->dtls_srtp_profile_list = profiles;
2978
0
    conf->dtls_srtp_profile_list_len = list_size;
2979
2980
0
    return 0;
2981
0
}
2982
2983
void mbedtls_ssl_get_dtls_srtp_negotiation_result(const mbedtls_ssl_context *ssl,
2984
                                                  mbedtls_dtls_srtp_info *dtls_srtp_info)
2985
0
{
2986
0
    dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
2987
    /* do not copy the mki value if there is no chosen profile */
2988
0
    if (dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET) {
2989
0
        dtls_srtp_info->mki_len = 0;
2990
0
    } else {
2991
0
        dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
2992
0
        memcpy(dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
2993
0
               ssl->dtls_srtp_info.mki_len);
2994
0
    }
2995
0
}
2996
#endif /* MBEDTLS_SSL_DTLS_SRTP */
2997
2998
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2999
void mbedtls_ssl_conf_max_version(mbedtls_ssl_config *conf, int major, int minor)
3000
0
{
3001
0
    conf->max_tls_version = (mbedtls_ssl_protocol_version) ((major << 8) | minor);
3002
0
}
3003
3004
void mbedtls_ssl_conf_min_version(mbedtls_ssl_config *conf, int major, int minor)
3005
0
{
3006
0
    conf->min_tls_version = (mbedtls_ssl_protocol_version) ((major << 8) | minor);
3007
0
}
3008
#endif /* MBEDTLS_DEPRECATED_REMOVED */
3009
3010
#if defined(MBEDTLS_SSL_SRV_C)
3011
void mbedtls_ssl_conf_cert_req_ca_list(mbedtls_ssl_config *conf,
3012
                                       char cert_req_ca_list)
3013
1.77k
{
3014
1.77k
    conf->cert_req_ca_list = cert_req_ca_list;
3015
1.77k
}
3016
#endif
3017
3018
#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
3019
void mbedtls_ssl_conf_encrypt_then_mac(mbedtls_ssl_config *conf, char etm)
3020
1.80k
{
3021
1.80k
    conf->encrypt_then_mac = etm;
3022
1.80k
}
3023
#endif
3024
3025
#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
3026
void mbedtls_ssl_conf_extended_master_secret(mbedtls_ssl_config *conf, char ems)
3027
1.80k
{
3028
1.80k
    conf->extended_ms = ems;
3029
1.80k
}
3030
#endif
3031
3032
#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3033
int mbedtls_ssl_conf_max_frag_len(mbedtls_ssl_config *conf, unsigned char mfl_code)
3034
0
{
3035
0
    if (mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
3036
0
        ssl_mfl_code_to_length(mfl_code) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN) {
3037
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3038
0
    }
3039
3040
0
    conf->mfl_code = mfl_code;
3041
3042
0
    return 0;
3043
0
}
3044
#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3045
3046
void mbedtls_ssl_conf_legacy_renegotiation(mbedtls_ssl_config *conf, int allow_legacy)
3047
0
{
3048
0
    conf->allow_legacy_renegotiation = allow_legacy;
3049
0
}
3050
3051
#if defined(MBEDTLS_SSL_RENEGOTIATION)
3052
void mbedtls_ssl_conf_renegotiation(mbedtls_ssl_config *conf, int renegotiation)
3053
1.80k
{
3054
1.80k
    conf->disable_renegotiation = renegotiation;
3055
1.80k
}
3056
3057
void mbedtls_ssl_conf_renegotiation_enforced(mbedtls_ssl_config *conf, int max_records)
3058
0
{
3059
0
    conf->renego_max_records = max_records;
3060
0
}
3061
3062
void mbedtls_ssl_conf_renegotiation_period(mbedtls_ssl_config *conf,
3063
                                           const unsigned char period[8])
3064
0
{
3065
0
    memcpy(conf->renego_period, period, 8);
3066
0
}
3067
#endif /* MBEDTLS_SSL_RENEGOTIATION */
3068
3069
#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3070
#if defined(MBEDTLS_SSL_CLI_C)
3071
3072
void mbedtls_ssl_conf_session_tickets(mbedtls_ssl_config *conf, int use_tickets)
3073
5.36k
{
3074
5.36k
    conf->session_tickets &= ~MBEDTLS_SSL_SESSION_TICKETS_TLS1_2_MASK;
3075
5.36k
    conf->session_tickets |= (use_tickets != 0) <<
3076
5.36k
                             MBEDTLS_SSL_SESSION_TICKETS_TLS1_2_BIT;
3077
5.36k
}
3078
3079
#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3080
void mbedtls_ssl_conf_tls13_enable_signal_new_session_tickets(
3081
    mbedtls_ssl_config *conf, int signal_new_session_tickets)
3082
5.33k
{
3083
5.33k
    conf->session_tickets &= ~MBEDTLS_SSL_SESSION_TICKETS_TLS1_3_MASK;
3084
5.33k
    conf->session_tickets |= (signal_new_session_tickets != 0) <<
3085
5.33k
                             MBEDTLS_SSL_SESSION_TICKETS_TLS1_3_BIT;
3086
5.33k
}
3087
#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3088
#endif /* MBEDTLS_SSL_CLI_C */
3089
3090
#if defined(MBEDTLS_SSL_SRV_C)
3091
3092
#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
3093
void mbedtls_ssl_conf_new_session_tickets(mbedtls_ssl_config *conf,
3094
                                          uint16_t num_tickets)
3095
8.97k
{
3096
8.97k
    conf->new_session_tickets_count = num_tickets;
3097
8.97k
}
3098
#endif
3099
3100
void mbedtls_ssl_conf_session_tickets_cb(mbedtls_ssl_config *conf,
3101
                                         mbedtls_ssl_ticket_write_t *f_ticket_write,
3102
                                         mbedtls_ssl_ticket_parse_t *f_ticket_parse,
3103
                                         void *p_ticket)
3104
884
{
3105
884
    conf->f_ticket_write = f_ticket_write;
3106
884
    conf->f_ticket_parse = f_ticket_parse;
3107
884
    conf->p_ticket       = p_ticket;
3108
884
}
3109
#endif
3110
#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3111
3112
void mbedtls_ssl_set_export_keys_cb(mbedtls_ssl_context *ssl,
3113
                                    mbedtls_ssl_export_keys_t *f_export_keys,
3114
                                    void *p_export_keys)
3115
0
{
3116
0
    ssl->f_export_keys = f_export_keys;
3117
0
    ssl->p_export_keys = p_export_keys;
3118
0
}
3119
3120
#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3121
void mbedtls_ssl_conf_async_private_cb(
3122
    mbedtls_ssl_config *conf,
3123
    mbedtls_ssl_async_sign_t *f_async_sign,
3124
    mbedtls_ssl_async_decrypt_t *f_async_decrypt,
3125
    mbedtls_ssl_async_resume_t *f_async_resume,
3126
    mbedtls_ssl_async_cancel_t *f_async_cancel,
3127
    void *async_config_data)
3128
0
{
3129
0
    conf->f_async_sign_start = f_async_sign;
3130
0
    conf->f_async_decrypt_start = f_async_decrypt;
3131
0
    conf->f_async_resume = f_async_resume;
3132
0
    conf->f_async_cancel = f_async_cancel;
3133
0
    conf->p_async_config_data = async_config_data;
3134
0
}
3135
3136
void *mbedtls_ssl_conf_get_async_config_data(const mbedtls_ssl_config *conf)
3137
0
{
3138
0
    return conf->p_async_config_data;
3139
0
}
3140
3141
void *mbedtls_ssl_get_async_operation_data(const mbedtls_ssl_context *ssl)
3142
0
{
3143
0
    if (ssl->handshake == NULL) {
3144
0
        return NULL;
3145
0
    } else {
3146
0
        return ssl->handshake->user_async_ctx;
3147
0
    }
3148
0
}
3149
3150
void mbedtls_ssl_set_async_operation_data(mbedtls_ssl_context *ssl,
3151
                                          void *ctx)
3152
0
{
3153
0
    if (ssl->handshake != NULL) {
3154
0
        ssl->handshake->user_async_ctx = ctx;
3155
0
    }
3156
0
}
3157
#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3158
3159
/*
3160
 * SSL get accessors
3161
 */
3162
uint32_t mbedtls_ssl_get_verify_result(const mbedtls_ssl_context *ssl)
3163
0
{
3164
0
    if (ssl->session != NULL) {
3165
0
        return ssl->session->verify_result;
3166
0
    }
3167
3168
0
    if (ssl->session_negotiate != NULL) {
3169
0
        return ssl->session_negotiate->verify_result;
3170
0
    }
3171
3172
0
    return 0xFFFFFFFF;
3173
0
}
3174
3175
int mbedtls_ssl_get_ciphersuite_id_from_ssl(const mbedtls_ssl_context *ssl)
3176
0
{
3177
0
    if (ssl == NULL || ssl->session == NULL) {
3178
0
        return 0;
3179
0
    }
3180
3181
0
    return ssl->session->ciphersuite;
3182
0
}
3183
3184
const char *mbedtls_ssl_get_ciphersuite(const mbedtls_ssl_context *ssl)
3185
0
{
3186
0
    if (ssl == NULL || ssl->session == NULL) {
3187
0
        return NULL;
3188
0
    }
3189
3190
0
    return mbedtls_ssl_get_ciphersuite_name(ssl->session->ciphersuite);
3191
0
}
3192
3193
const char *mbedtls_ssl_get_version(const mbedtls_ssl_context *ssl)
3194
0
{
3195
0
#if defined(MBEDTLS_SSL_PROTO_DTLS)
3196
0
    if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
3197
0
        switch (ssl->tls_version) {
3198
0
            case MBEDTLS_SSL_VERSION_TLS1_2:
3199
0
                return "DTLSv1.2";
3200
0
            default:
3201
0
                return "unknown (DTLS)";
3202
0
        }
3203
0
    }
3204
0
#endif
3205
3206
0
    switch (ssl->tls_version) {
3207
0
        case MBEDTLS_SSL_VERSION_TLS1_2:
3208
0
            return "TLSv1.2";
3209
0
        case MBEDTLS_SSL_VERSION_TLS1_3:
3210
0
            return "TLSv1.3";
3211
0
        default:
3212
0
            return "unknown";
3213
0
    }
3214
0
}
3215
3216
#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
3217
3218
size_t mbedtls_ssl_get_output_record_size_limit(const mbedtls_ssl_context *ssl)
3219
0
{
3220
0
    const size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
3221
0
    size_t record_size_limit = max_len;
3222
3223
0
    if (ssl->session != NULL &&
3224
0
        ssl->session->record_size_limit >= MBEDTLS_SSL_RECORD_SIZE_LIMIT_MIN &&
3225
0
        ssl->session->record_size_limit < max_len) {
3226
0
        record_size_limit = ssl->session->record_size_limit;
3227
0
    }
3228
3229
    // TODO: this is currently untested
3230
    /* During a handshake, use the value being negotiated */
3231
0
    if (ssl->session_negotiate != NULL &&
3232
0
        ssl->session_negotiate->record_size_limit >= MBEDTLS_SSL_RECORD_SIZE_LIMIT_MIN &&
3233
0
        ssl->session_negotiate->record_size_limit < max_len) {
3234
0
        record_size_limit = ssl->session_negotiate->record_size_limit;
3235
0
    }
3236
3237
0
    return record_size_limit;
3238
0
}
3239
#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
3240
3241
#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3242
size_t mbedtls_ssl_get_input_max_frag_len(const mbedtls_ssl_context *ssl)
3243
10.7k
{
3244
10.7k
    size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
3245
10.7k
    size_t read_mfl;
3246
3247
10.7k
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3248
    /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
3249
10.7k
    if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3250
8.97k
        ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE) {
3251
1.46k
        return ssl_mfl_code_to_length(ssl->conf->mfl_code);
3252
1.46k
    }
3253
9.28k
#endif
3254
3255
    /* Check if a smaller max length was negotiated */
3256
9.28k
    if (ssl->session_out != NULL) {
3257
0
        read_mfl = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
3258
0
        if (read_mfl < max_len) {
3259
0
            max_len = read_mfl;
3260
0
        }
3261
0
    }
3262
3263
    /* During a handshake, use the value being negotiated */
3264
9.28k
    if (ssl->session_negotiate != NULL) {
3265
9.28k
        read_mfl = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
3266
9.28k
        if (read_mfl < max_len) {
3267
37
            max_len = read_mfl;
3268
37
        }
3269
9.28k
    }
3270
3271
9.28k
    return max_len;
3272
10.7k
}
3273
3274
size_t mbedtls_ssl_get_output_max_frag_len(const mbedtls_ssl_context *ssl)
3275
91.9k
{
3276
91.9k
    size_t max_len;
3277
3278
    /*
3279
     * Assume mfl_code is correct since it was checked when set
3280
     */
3281
91.9k
    max_len = ssl_mfl_code_to_length(ssl->conf->mfl_code);
3282
3283
    /* Check if a smaller max length was negotiated */
3284
91.9k
    if (ssl->session_out != NULL &&
3285
62.1k
        ssl_mfl_code_to_length(ssl->session_out->mfl_code) < max_len) {
3286
0
        max_len = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
3287
0
    }
3288
3289
    /* During a handshake, use the value being negotiated */
3290
91.9k
    if (ssl->session_negotiate != NULL &&
3291
91.9k
        ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code) < max_len) {
3292
45
        max_len = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
3293
45
    }
3294
3295
91.9k
    return max_len;
3296
91.9k
}
3297
#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3298
3299
#if defined(MBEDTLS_SSL_PROTO_DTLS)
3300
size_t mbedtls_ssl_get_current_mtu(const mbedtls_ssl_context *ssl)
3301
126k
{
3302
126k
    if (ssl->handshake == NULL || ssl->handshake->mtu == 0) {
3303
126k
        return ssl->mtu;
3304
126k
    }
3305
3306
0
    if (ssl->mtu == 0) {
3307
0
        return ssl->handshake->mtu;
3308
0
    }
3309
3310
0
    return ssl->mtu < ssl->handshake->mtu ?
3311
0
           ssl->mtu : ssl->handshake->mtu;
3312
0
}
3313
#endif /* MBEDTLS_SSL_PROTO_DTLS */
3314
3315
int mbedtls_ssl_get_max_out_record_payload(const mbedtls_ssl_context *ssl)
3316
0
{
3317
0
    size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
3318
3319
#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3320
    !defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT) && \
3321
    !defined(MBEDTLS_SSL_PROTO_DTLS)
3322
    (void) ssl;
3323
#endif
3324
3325
0
#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3326
0
    const size_t mfl = mbedtls_ssl_get_output_max_frag_len(ssl);
3327
3328
0
    if (max_len > mfl) {
3329
0
        max_len = mfl;
3330
0
    }
3331
0
#endif
3332
3333
0
#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
3334
0
    const size_t record_size_limit = mbedtls_ssl_get_output_record_size_limit(ssl);
3335
3336
0
    if (max_len > record_size_limit) {
3337
0
        max_len = record_size_limit;
3338
0
    }
3339
0
#endif
3340
3341
0
    if (ssl->transform_out != NULL &&
3342
0
        ssl->transform_out->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
3343
        /*
3344
         * In TLS 1.3 case, when records are protected, `max_len` as computed
3345
         * above is the maximum length of the TLSInnerPlaintext structure that
3346
         * along the plaintext payload contains the inner content type (one byte)
3347
         * and some zero padding. Given the algorithm used for padding
3348
         * in mbedtls_ssl_encrypt_buf(), compute the maximum length for
3349
         * the plaintext payload. Round down to a multiple of
3350
         * MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY and
3351
         * subtract 1.
3352
         */
3353
0
        max_len = ((max_len / MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY) *
3354
0
                   MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY) - 1;
3355
0
    }
3356
3357
0
#if defined(MBEDTLS_SSL_PROTO_DTLS)
3358
0
    if (mbedtls_ssl_get_current_mtu(ssl) != 0) {
3359
0
        const size_t mtu = mbedtls_ssl_get_current_mtu(ssl);
3360
0
        const int ret = mbedtls_ssl_get_record_expansion(ssl);
3361
0
        const size_t overhead = (size_t) ret;
3362
3363
0
        if (ret < 0) {
3364
0
            return ret;
3365
0
        }
3366
3367
0
        if (mtu <= overhead) {
3368
0
            MBEDTLS_SSL_DEBUG_MSG(1, ("MTU too low for record expansion"));
3369
0
            return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3370
0
        }
3371
3372
0
        if (max_len > mtu - overhead) {
3373
0
            max_len = mtu - overhead;
3374
0
        }
3375
0
    }
3376
0
#endif /* MBEDTLS_SSL_PROTO_DTLS */
3377
3378
#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) &&        \
3379
    !defined(MBEDTLS_SSL_PROTO_DTLS) &&                 \
3380
    !defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
3381
    ((void) ssl);
3382
#endif
3383
3384
0
    return (int) max_len;
3385
0
}
3386
3387
int mbedtls_ssl_get_max_in_record_payload(const mbedtls_ssl_context *ssl)
3388
0
{
3389
0
    size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
3390
3391
#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3392
    (void) ssl;
3393
#endif
3394
3395
0
#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3396
0
    const size_t mfl = mbedtls_ssl_get_input_max_frag_len(ssl);
3397
3398
0
    if (max_len > mfl) {
3399
0
        max_len = mfl;
3400
0
    }
3401
0
#endif
3402
3403
0
    return (int) max_len;
3404
0
}
3405
3406
#if defined(MBEDTLS_X509_CRT_PARSE_C)
3407
const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert(const mbedtls_ssl_context *ssl)
3408
0
{
3409
0
    if (ssl == NULL || ssl->session == NULL) {
3410
0
        return NULL;
3411
0
    }
3412
3413
0
#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3414
0
    return ssl->session->peer_cert;
3415
#else
3416
    return NULL;
3417
#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3418
0
}
3419
#endif /* MBEDTLS_X509_CRT_PARSE_C */
3420
3421
#if defined(MBEDTLS_SSL_CLI_C)
3422
int mbedtls_ssl_get_session(const mbedtls_ssl_context *ssl,
3423
                            mbedtls_ssl_session *dst)
3424
0
{
3425
0
    int ret;
3426
3427
0
    if (ssl == NULL ||
3428
0
        dst == NULL ||
3429
0
        ssl->session == NULL ||
3430
0
        ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
3431
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3432
0
    }
3433
3434
    /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
3435
     * idempotent: Each session can only be exported once.
3436
     *
3437
     * (This is in preparation for TLS 1.3 support where we will
3438
     * need the ability to export multiple sessions (aka tickets),
3439
     * which will be achieved by calling mbedtls_ssl_get_session()
3440
     * multiple times until it fails.)
3441
     *
3442
     * Check whether we have already exported the current session,
3443
     * and fail if so.
3444
     */
3445
0
    if (ssl->session->exported == 1) {
3446
0
        return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3447
0
    }
3448
3449
0
    ret = mbedtls_ssl_session_copy(dst, ssl->session);
3450
0
    if (ret != 0) {
3451
0
        return ret;
3452
0
    }
3453
3454
    /* Remember that we've exported the session. */
3455
0
    ssl->session->exported = 1;
3456
0
    return 0;
3457
0
}
3458
#endif /* MBEDTLS_SSL_CLI_C */
3459
3460
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3461
3462
/* Serialization of TLS 1.2 sessions
3463
 *
3464
 * For more detail, see the description of ssl_session_save().
3465
 */
3466
static size_t ssl_tls12_session_save(const mbedtls_ssl_session *session,
3467
                                     unsigned char *buf,
3468
                                     size_t buf_len)
3469
0
{
3470
0
    unsigned char *p = buf;
3471
0
    size_t used = 0;
3472
3473
0
#if defined(MBEDTLS_HAVE_TIME)
3474
0
    uint64_t start;
3475
0
#endif
3476
0
#if defined(MBEDTLS_X509_CRT_PARSE_C)
3477
0
#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3478
0
    size_t cert_len;
3479
0
#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3480
0
#endif /* MBEDTLS_X509_CRT_PARSE_C */
3481
3482
    /*
3483
     * Time
3484
     */
3485
0
#if defined(MBEDTLS_HAVE_TIME)
3486
0
    used += 8;
3487
3488
0
    if (used <= buf_len) {
3489
0
        start = (uint64_t) session->start;
3490
3491
0
        MBEDTLS_PUT_UINT64_BE(start, p, 0);
3492
0
        p += 8;
3493
0
    }
3494
0
#endif /* MBEDTLS_HAVE_TIME */
3495
3496
    /*
3497
     * Basic mandatory fields
3498
     */
3499
0
    used += 1 /* id_len */
3500
0
            + sizeof(session->id)
3501
0
            + sizeof(session->master)
3502
0
            + 4; /* verify_result */
3503
3504
0
    if (used <= buf_len) {
3505
0
        *p++ = MBEDTLS_BYTE_0(session->id_len);
3506
0
        memcpy(p, session->id, 32);
3507
0
        p += 32;
3508
3509
0
        memcpy(p, session->master, 48);
3510
0
        p += 48;
3511
3512
0
        MBEDTLS_PUT_UINT32_BE(session->verify_result, p, 0);
3513
0
        p += 4;
3514
0
    }
3515
3516
    /*
3517
     * Peer's end-entity certificate
3518
     */
3519
0
#if defined(MBEDTLS_X509_CRT_PARSE_C)
3520
0
#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3521
0
    if (session->peer_cert == NULL) {
3522
0
        cert_len = 0;
3523
0
    } else {
3524
0
        cert_len = session->peer_cert->raw.len;
3525
0
    }
3526
3527
0
    used += 3 + cert_len;
3528
3529
0
    if (used <= buf_len) {
3530
0
        *p++ = MBEDTLS_BYTE_2(cert_len);
3531
0
        *p++ = MBEDTLS_BYTE_1(cert_len);
3532
0
        *p++ = MBEDTLS_BYTE_0(cert_len);
3533
3534
0
        if (session->peer_cert != NULL) {
3535
0
            memcpy(p, session->peer_cert->raw.p, cert_len);
3536
0
            p += cert_len;
3537
0
        }
3538
0
    }
3539
#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3540
    if (session->peer_cert_digest != NULL) {
3541
        used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
3542
        if (used <= buf_len) {
3543
            *p++ = (unsigned char) session->peer_cert_digest_type;
3544
            *p++ = (unsigned char) session->peer_cert_digest_len;
3545
            memcpy(p, session->peer_cert_digest,
3546
                   session->peer_cert_digest_len);
3547
            p += session->peer_cert_digest_len;
3548
        }
3549
    } else {
3550
        used += 2;
3551
        if (used <= buf_len) {
3552
            *p++ = (unsigned char) MBEDTLS_MD_NONE;
3553
            *p++ = 0;
3554
        }
3555
    }
3556
#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3557
0
#endif /* MBEDTLS_X509_CRT_PARSE_C */
3558
3559
    /*
3560
     * Session ticket if any, plus associated data
3561
     */
3562
0
#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3563
0
#if defined(MBEDTLS_SSL_CLI_C)
3564
0
    if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
3565
0
        used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
3566
3567
0
        if (used <= buf_len) {
3568
0
            *p++ = MBEDTLS_BYTE_2(session->ticket_len);
3569
0
            *p++ = MBEDTLS_BYTE_1(session->ticket_len);
3570
0
            *p++ = MBEDTLS_BYTE_0(session->ticket_len);
3571
3572
0
            if (session->ticket != NULL) {
3573
0
                memcpy(p, session->ticket, session->ticket_len);
3574
0
                p += session->ticket_len;
3575
0
            }
3576
3577
0
            MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
3578
0
            p += 4;
3579
0
        }
3580
0
    }
3581
0
#endif /* MBEDTLS_SSL_CLI_C */
3582
0
#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
3583
0
    if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
3584
0
        used += 8;
3585
3586
0
        if (used <= buf_len) {
3587
0
            MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_creation_time, p, 0);
3588
0
            p += 8;
3589
0
        }
3590
0
    }
3591
0
#endif /* MBEDTLS_HAVE_TIME && MBEDTLS_SSL_SRV_C */
3592
0
#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3593
3594
    /*
3595
     * Misc extension-related info
3596
     */
3597
0
#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3598
0
    used += 1;
3599
3600
0
    if (used <= buf_len) {
3601
0
        *p++ = session->mfl_code;
3602
0
    }
3603
0
#endif
3604
3605
0
#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
3606
0
    used += 1;
3607
3608
0
    if (used <= buf_len) {
3609
0
        *p++ = MBEDTLS_BYTE_0(session->encrypt_then_mac);
3610
0
    }
3611
0
#endif
3612
3613
0
    return used;
3614
0
}
3615
3616
MBEDTLS_CHECK_RETURN_CRITICAL
3617
static int ssl_tls12_session_load(mbedtls_ssl_session *session,
3618
                                  const unsigned char *buf,
3619
                                  size_t len)
3620
0
{
3621
0
#if defined(MBEDTLS_HAVE_TIME)
3622
0
    uint64_t start;
3623
0
#endif
3624
0
#if defined(MBEDTLS_X509_CRT_PARSE_C)
3625
0
#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3626
0
    size_t cert_len;
3627
0
#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3628
0
#endif /* MBEDTLS_X509_CRT_PARSE_C */
3629
3630
0
    const unsigned char *p = buf;
3631
0
    const unsigned char * const end = buf + len;
3632
3633
    /*
3634
     * Time
3635
     */
3636
0
#if defined(MBEDTLS_HAVE_TIME)
3637
0
    if (8 > (size_t) (end - p)) {
3638
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3639
0
    }
3640
3641
0
    start = MBEDTLS_GET_UINT64_BE(p, 0);
3642
0
    p += 8;
3643
3644
0
    session->start = (mbedtls_time_t) start;
3645
0
#endif /* MBEDTLS_HAVE_TIME */
3646
3647
    /*
3648
     * Basic mandatory fields
3649
     */
3650
0
    if (1 + 32 + 48 + 4 > (size_t) (end - p)) {
3651
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3652
0
    }
3653
3654
0
    session->id_len = *p++;
3655
0
    if (session->id_len > sizeof(session->id)) {
3656
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3657
0
    }
3658
0
    memcpy(session->id, p, 32);
3659
0
    p += 32;
3660
3661
0
    memcpy(session->master, p, 48);
3662
0
    p += 48;
3663
3664
0
    session->verify_result = MBEDTLS_GET_UINT32_BE(p, 0);
3665
0
    p += 4;
3666
3667
    /* Immediately clear invalid pointer values that have been read, in case
3668
     * we exit early before we replaced them with valid ones. */
3669
0
#if defined(MBEDTLS_X509_CRT_PARSE_C)
3670
0
#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3671
0
    session->peer_cert = NULL;
3672
#else
3673
    session->peer_cert_digest = NULL;
3674
#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3675
0
#endif /* MBEDTLS_X509_CRT_PARSE_C */
3676
0
#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
3677
0
    session->ticket = NULL;
3678
0
#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
3679
3680
    /*
3681
     * Peer certificate
3682
     */
3683
0
#if defined(MBEDTLS_X509_CRT_PARSE_C)
3684
0
#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3685
    /* Deserialize CRT from the end of the ticket. */
3686
0
    if (3 > (size_t) (end - p)) {
3687
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3688
0
    }
3689
3690
0
    cert_len = MBEDTLS_GET_UINT24_BE(p, 0);
3691
0
    p += 3;
3692
3693
0
    if (cert_len != 0) {
3694
0
        int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3695
3696
0
        if (cert_len > (size_t) (end - p)) {
3697
0
            return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3698
0
        }
3699
3700
0
        session->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
3701
3702
0
        if (session->peer_cert == NULL) {
3703
0
            return MBEDTLS_ERR_SSL_ALLOC_FAILED;
3704
0
        }
3705
3706
0
        mbedtls_x509_crt_init(session->peer_cert);
3707
3708
0
        if ((ret = mbedtls_x509_crt_parse_der(session->peer_cert,
3709
0
                                              p, cert_len)) != 0) {
3710
0
            mbedtls_x509_crt_free(session->peer_cert);
3711
0
            mbedtls_free(session->peer_cert);
3712
0
            session->peer_cert = NULL;
3713
0
            return ret;
3714
0
        }
3715
3716
0
        p += cert_len;
3717
0
    }
3718
#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3719
    /* Deserialize CRT digest from the end of the ticket. */
3720
    if (2 > (size_t) (end - p)) {
3721
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3722
    }
3723
3724
    session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
3725
    session->peer_cert_digest_len  = (size_t) *p++;
3726
3727
    if (session->peer_cert_digest_len != 0) {
3728
        const mbedtls_md_info_t *md_info =
3729
            mbedtls_md_info_from_type(session->peer_cert_digest_type);
3730
        if (md_info == NULL) {
3731
            return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3732
        }
3733
        if (session->peer_cert_digest_len != mbedtls_md_get_size(md_info)) {
3734
            return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3735
        }
3736
3737
        if (session->peer_cert_digest_len > (size_t) (end - p)) {
3738
            return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3739
        }
3740
3741
        session->peer_cert_digest =
3742
            mbedtls_calloc(1, session->peer_cert_digest_len);
3743
        if (session->peer_cert_digest == NULL) {
3744
            return MBEDTLS_ERR_SSL_ALLOC_FAILED;
3745
        }
3746
3747
        memcpy(session->peer_cert_digest, p,
3748
               session->peer_cert_digest_len);
3749
        p += session->peer_cert_digest_len;
3750
    }
3751
#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3752
0
#endif /* MBEDTLS_X509_CRT_PARSE_C */
3753
3754
    /*
3755
     * Session ticket and associated data
3756
     */
3757
0
#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3758
0
#if defined(MBEDTLS_SSL_CLI_C)
3759
0
    if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
3760
0
        if (3 > (size_t) (end - p)) {
3761
0
            return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3762
0
        }
3763
3764
0
        session->ticket_len = MBEDTLS_GET_UINT24_BE(p, 0);
3765
0
        p += 3;
3766
3767
0
        if (session->ticket_len != 0) {
3768
0
            if (session->ticket_len > (size_t) (end - p)) {
3769
0
                return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3770
0
            }
3771
3772
0
            session->ticket = mbedtls_calloc(1, session->ticket_len);
3773
0
            if (session->ticket == NULL) {
3774
0
                return MBEDTLS_ERR_SSL_ALLOC_FAILED;
3775
0
            }
3776
3777
0
            memcpy(session->ticket, p, session->ticket_len);
3778
0
            p += session->ticket_len;
3779
0
        }
3780
3781
0
        if (4 > (size_t) (end - p)) {
3782
0
            return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3783
0
        }
3784
3785
0
        session->ticket_lifetime = MBEDTLS_GET_UINT32_BE(p, 0);
3786
0
        p += 4;
3787
0
    }
3788
0
#endif /* MBEDTLS_SSL_CLI_C */
3789
0
#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
3790
0
    if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
3791
0
        if (8 > (size_t) (end - p)) {
3792
0
            return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3793
0
        }
3794
0
        session->ticket_creation_time = MBEDTLS_GET_UINT64_BE(p, 0);
3795
0
        p += 8;
3796
0
    }
3797
0
#endif /* MBEDTLS_HAVE_TIME && MBEDTLS_SSL_SRV_C */
3798
0
#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3799
3800
    /*
3801
     * Misc extension-related info
3802
     */
3803
0
#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3804
0
    if (1 > (size_t) (end - p)) {
3805
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3806
0
    }
3807
3808
0
    session->mfl_code = *p++;
3809
0
#endif
3810
3811
0
#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
3812
0
    if (1 > (size_t) (end - p)) {
3813
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3814
0
    }
3815
3816
0
    session->encrypt_then_mac = *p++;
3817
0
#endif
3818
3819
    /* Done, should have consumed entire buffer */
3820
0
    if (p != end) {
3821
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3822
0
    }
3823
3824
0
    return 0;
3825
0
}
3826
3827
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3828
3829
#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3830
/* Serialization of TLS 1.3 sessions:
3831
 *
3832
 * For more detail, see the description of ssl_session_save().
3833
 */
3834
#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3835
MBEDTLS_CHECK_RETURN_CRITICAL
3836
static int ssl_tls13_session_save(const mbedtls_ssl_session *session,
3837
                                  unsigned char *buf,
3838
                                  size_t buf_len,
3839
                                  size_t *olen)
3840
0
{
3841
0
    unsigned char *p = buf;
3842
0
#if defined(MBEDTLS_SSL_CLI_C) && \
3843
0
    defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3844
0
    size_t hostname_len = (session->hostname == NULL) ?
3845
0
                          0 : strlen(session->hostname) + 1;
3846
0
#endif
3847
3848
0
#if defined(MBEDTLS_SSL_SRV_C) && \
3849
0
    defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN)
3850
0
    const size_t alpn_len = (session->ticket_alpn == NULL) ?
3851
0
                            0 : strlen(session->ticket_alpn) + 1;
3852
0
#endif
3853
0
    size_t needed =   4  /* ticket_age_add */
3854
0
                    + 1  /* ticket_flags */
3855
0
                    + 1; /* resumption_key length */
3856
3857
0
    *olen = 0;
3858
3859
0
    if (session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN) {
3860
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3861
0
    }
3862
0
    needed += session->resumption_key_len;  /* resumption_key */
3863
3864
0
#if defined(MBEDTLS_SSL_EARLY_DATA)
3865
0
    needed += 4;                            /* max_early_data_size */
3866
0
#endif
3867
0
#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
3868
0
    needed += 2;                            /* record_size_limit */
3869
0
#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
3870
3871
0
#if defined(MBEDTLS_HAVE_TIME)
3872
0
    needed += 8; /* ticket_creation_time or ticket_reception_time */
3873
0
#endif
3874
3875
0
#if defined(MBEDTLS_SSL_SRV_C)
3876
0
    if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
3877
0
#if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN)
3878
0
        needed +=   2                         /* alpn_len */
3879
0
                  + alpn_len;                 /* alpn */
3880
0
#endif
3881
0
    }
3882
0
#endif /* MBEDTLS_SSL_SRV_C */
3883
3884
0
#if defined(MBEDTLS_SSL_CLI_C)
3885
0
    if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
3886
0
#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3887
0
        needed +=  2                        /* hostname_len */
3888
0
                  + hostname_len;           /* hostname */
3889
0
#endif
3890
3891
0
        needed +=   4                       /* ticket_lifetime */
3892
0
                  + 2;                      /* ticket_len */
3893
3894
        /* Check size_t overflow */
3895
0
        if (session->ticket_len > SIZE_MAX - needed) {
3896
0
            return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3897
0
        }
3898
3899
0
        needed += session->ticket_len;      /* ticket */
3900
0
    }
3901
0
#endif /* MBEDTLS_SSL_CLI_C */
3902
3903
0
    *olen = needed;
3904
0
    if (needed > buf_len) {
3905
0
        return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
3906
0
    }
3907
3908
0
    MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 0);
3909
0
    p[4] = session->ticket_flags;
3910
3911
    /* save resumption_key */
3912
0
    p[5] = session->resumption_key_len;
3913
0
    p += 6;
3914
0
    memcpy(p, session->resumption_key, session->resumption_key_len);
3915
0
    p += session->resumption_key_len;
3916
3917
0
#if defined(MBEDTLS_SSL_EARLY_DATA)
3918
0
    MBEDTLS_PUT_UINT32_BE(session->max_early_data_size, p, 0);
3919
0
    p += 4;
3920
0
#endif
3921
0
#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
3922
0
    MBEDTLS_PUT_UINT16_BE(session->record_size_limit, p, 0);
3923
0
    p += 2;
3924
0
#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
3925
3926
0
#if defined(MBEDTLS_SSL_SRV_C)
3927
0
    if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
3928
0
#if defined(MBEDTLS_HAVE_TIME)
3929
0
        MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_creation_time, p, 0);
3930
0
        p += 8;
3931
0
#endif /* MBEDTLS_HAVE_TIME */
3932
3933
0
#if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN)
3934
0
        MBEDTLS_PUT_UINT16_BE(alpn_len, p, 0);
3935
0
        p += 2;
3936
3937
0
        if (alpn_len > 0) {
3938
            /* save chosen alpn */
3939
0
            memcpy(p, session->ticket_alpn, alpn_len);
3940
0
            p += alpn_len;
3941
0
        }
3942
0
#endif /* MBEDTLS_SSL_EARLY_DATA && MBEDTLS_SSL_ALPN */
3943
0
    }
3944
0
#endif /* MBEDTLS_SSL_SRV_C */
3945
3946
0
#if defined(MBEDTLS_SSL_CLI_C)
3947
0
    if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
3948
0
#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3949
0
        MBEDTLS_PUT_UINT16_BE(hostname_len, p, 0);
3950
0
        p += 2;
3951
0
        if (hostname_len > 0) {
3952
            /* save host name */
3953
0
            memcpy(p, session->hostname, hostname_len);
3954
0
            p += hostname_len;
3955
0
        }
3956
0
#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
3957
3958
0
#if defined(MBEDTLS_HAVE_TIME)
3959
0
        MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_reception_time, p, 0);
3960
0
        p += 8;
3961
0
#endif
3962
0
        MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
3963
0
        p += 4;
3964
3965
0
        MBEDTLS_PUT_UINT16_BE(session->ticket_len, p, 0);
3966
0
        p += 2;
3967
3968
0
        if (session->ticket != NULL && session->ticket_len > 0) {
3969
0
            memcpy(p, session->ticket, session->ticket_len);
3970
0
            p += session->ticket_len;
3971
0
        }
3972
0
    }
3973
0
#endif /* MBEDTLS_SSL_CLI_C */
3974
0
    return 0;
3975
0
}
3976
3977
MBEDTLS_CHECK_RETURN_CRITICAL
3978
static int ssl_tls13_session_load(mbedtls_ssl_session *session,
3979
                                  const unsigned char *buf,
3980
                                  size_t len)
3981
0
{
3982
0
    const unsigned char *p = buf;
3983
0
    const unsigned char *end = buf + len;
3984
3985
0
    if (end - p < 6) {
3986
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3987
0
    }
3988
0
    session->ticket_age_add = MBEDTLS_GET_UINT32_BE(p, 0);
3989
0
    session->ticket_flags = p[4];
3990
3991
    /* load resumption_key */
3992
0
    session->resumption_key_len = p[5];
3993
0
    p += 6;
3994
3995
0
    if (end - p < session->resumption_key_len) {
3996
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3997
0
    }
3998
3999
0
    if (sizeof(session->resumption_key) < session->resumption_key_len) {
4000
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4001
0
    }
4002
0
    memcpy(session->resumption_key, p, session->resumption_key_len);
4003
0
    p += session->resumption_key_len;
4004
4005
0
#if defined(MBEDTLS_SSL_EARLY_DATA)
4006
0
    if (end - p < 4) {
4007
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4008
0
    }
4009
0
    session->max_early_data_size = MBEDTLS_GET_UINT32_BE(p, 0);
4010
0
    p += 4;
4011
0
#endif
4012
0
#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
4013
0
    if (end - p < 2) {
4014
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4015
0
    }
4016
0
    session->record_size_limit = MBEDTLS_GET_UINT16_BE(p, 0);
4017
0
    p += 2;
4018
0
#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
4019
4020
0
#if  defined(MBEDTLS_SSL_SRV_C)
4021
0
    if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
4022
0
#if defined(MBEDTLS_HAVE_TIME)
4023
0
        if (end - p < 8) {
4024
0
            return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4025
0
        }
4026
0
        session->ticket_creation_time = MBEDTLS_GET_UINT64_BE(p, 0);
4027
0
        p += 8;
4028
0
#endif /* MBEDTLS_HAVE_TIME */
4029
4030
0
#if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN)
4031
0
        size_t alpn_len;
4032
4033
0
        if (end - p < 2) {
4034
0
            return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4035
0
        }
4036
4037
0
        alpn_len = MBEDTLS_GET_UINT16_BE(p, 0);
4038
0
        p += 2;
4039
4040
0
        if (end - p < (long int) alpn_len) {
4041
0
            return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4042
0
        }
4043
4044
0
        if (alpn_len > 0) {
4045
            /* The data is about to be used as a null-terminated string, so
4046
             * check that it actually is one. */
4047
0
            if (p[alpn_len - 1] != '\0') {
4048
0
                return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4049
0
            }
4050
4051
0
            int ret = mbedtls_ssl_session_set_ticket_alpn(session, (char *) p);
4052
0
            if (ret != 0) {
4053
0
                return ret;
4054
0
            }
4055
0
            p += alpn_len;
4056
0
        }
4057
0
#endif /* MBEDTLS_SSL_EARLY_DATA && MBEDTLS_SSL_ALPN */
4058
0
    }
4059
0
#endif /* MBEDTLS_SSL_SRV_C */
4060
4061
0
#if defined(MBEDTLS_SSL_CLI_C)
4062
0
    if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
4063
0
#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
4064
0
        size_t hostname_len;
4065
        /* load host name */
4066
0
        if (end - p < 2) {
4067
0
            return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4068
0
        }
4069
0
        hostname_len = MBEDTLS_GET_UINT16_BE(p, 0);
4070
0
        p += 2;
4071
4072
0
        if (end - p < (long int) hostname_len) {
4073
0
            return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4074
0
        }
4075
0
        if (hostname_len > 0) {
4076
            /* The data is about to be used as a null-terminated string, so
4077
             * check that it actually is one. */
4078
0
            if (p[hostname_len - 1] != '\0') {
4079
0
                return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4080
0
            }
4081
4082
0
            int ret = mbedtls_ssl_session_set_hostname(session, (const char *) p);
4083
0
            if (ret != 0) {
4084
0
                return ret;
4085
0
            }
4086
0
            p += hostname_len;
4087
0
        }
4088
0
#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
4089
4090
0
#if defined(MBEDTLS_HAVE_TIME)
4091
0
        if (end - p < 8) {
4092
0
            return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4093
0
        }
4094
0
        session->ticket_reception_time = MBEDTLS_GET_UINT64_BE(p, 0);
4095
0
        p += 8;
4096
0
#endif
4097
0
        if (end - p < 4) {
4098
0
            return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4099
0
        }
4100
0
        session->ticket_lifetime = MBEDTLS_GET_UINT32_BE(p, 0);
4101
0
        p += 4;
4102
4103
0
        if (end - p <  2) {
4104
0
            return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4105
0
        }
4106
0
        session->ticket_len = MBEDTLS_GET_UINT16_BE(p, 0);
4107
0
        p += 2;
4108
4109
0
        if (end - p < (long int) session->ticket_len) {
4110
0
            return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4111
0
        }
4112
0
        if (session->ticket_len > 0) {
4113
0
            session->ticket = mbedtls_calloc(1, session->ticket_len);
4114
0
            if (session->ticket == NULL) {
4115
0
                return MBEDTLS_ERR_SSL_ALLOC_FAILED;
4116
0
            }
4117
0
            memcpy(session->ticket, p, session->ticket_len);
4118
0
            p += session->ticket_len;
4119
0
        }
4120
0
    }
4121
0
#endif /* MBEDTLS_SSL_CLI_C */
4122
4123
0
    if (p != end) {
4124
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4125
0
    }
4126
4127
0
    return 0;
4128
4129
0
}
4130
#else /* MBEDTLS_SSL_SESSION_TICKETS */
4131
MBEDTLS_CHECK_RETURN_CRITICAL
4132
static int ssl_tls13_session_save(const mbedtls_ssl_session *session,
4133
                                  unsigned char *buf,
4134
                                  size_t buf_len,
4135
                                  size_t *olen)
4136
{
4137
    ((void) session);
4138
    ((void) buf);
4139
    ((void) buf_len);
4140
    *olen = 0;
4141
    return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
4142
}
4143
4144
static int ssl_tls13_session_load(const mbedtls_ssl_session *session,
4145
                                  const unsigned char *buf,
4146
                                  size_t buf_len)
4147
{
4148
    ((void) session);
4149
    ((void) buf);
4150
    ((void) buf_len);
4151
    return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
4152
}
4153
#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
4154
#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
4155
4156
/*
4157
 * Define ticket header determining Mbed TLS version
4158
 * and structure of the ticket.
4159
 */
4160
4161
/*
4162
 * Define bitflag determining compile-time settings influencing
4163
 * structure of serialized SSL sessions.
4164
 */
4165
4166
#if defined(MBEDTLS_HAVE_TIME)
4167
#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
4168
#else
4169
#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
4170
#endif /* MBEDTLS_HAVE_TIME */
4171
4172
#if defined(MBEDTLS_X509_CRT_PARSE_C)
4173
#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
4174
#else
4175
#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
4176
#endif /* MBEDTLS_X509_CRT_PARSE_C */
4177
4178
#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
4179
#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT 1
4180
#else
4181
#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT 0
4182
#endif /* MBEDTLS_SSL_SESSION_TICKETS */
4183
4184
#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
4185
#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
4186
#else
4187
#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
4188
#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
4189
4190
#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
4191
#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
4192
#else
4193
#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
4194
#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
4195
4196
#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
4197
#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
4198
#else
4199
#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
4200
#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
4201
4202
#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4203
#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
4204
#else
4205
#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
4206
#endif /* MBEDTLS_SSL_SESSION_TICKETS */
4207
4208
#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
4209
#define SSL_SERIALIZED_SESSION_CONFIG_SNI 1
4210
#else
4211
#define SSL_SERIALIZED_SESSION_CONFIG_SNI 0
4212
#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
4213
4214
#if defined(MBEDTLS_SSL_EARLY_DATA)
4215
#define SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA 1
4216
#else
4217
#define SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA 0
4218
#endif /* MBEDTLS_SSL_EARLY_DATA */
4219
4220
#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
4221
#define SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE 1
4222
#else
4223
#define SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE 0
4224
#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
4225
4226
#if defined(MBEDTLS_SSL_ALPN) && defined(MBEDTLS_SSL_SRV_C) && \
4227
    defined(MBEDTLS_SSL_EARLY_DATA)
4228
#define SSL_SERIALIZED_SESSION_CONFIG_ALPN 1
4229
#else
4230
#define SSL_SERIALIZED_SESSION_CONFIG_ALPN 0
4231
#endif /* MBEDTLS_SSL_ALPN */
4232
4233
#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT          0
4234
#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT           1
4235
#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
4236
#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT           3
4237
#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT           4
4238
#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT        5
4239
#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT_BIT 6
4240
#define SSL_SERIALIZED_SESSION_CONFIG_SNI_BIT           7
4241
#define SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA_BIT    8
4242
#define SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE_BIT   9
4243
#define SSL_SERIALIZED_SESSION_CONFIG_ALPN_BIT          10
4244
4245
#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG                           \
4246
    ((uint16_t) (                                                      \
4247
         (SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT) | \
4248
         (SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT) | \
4249
         (SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << \
4250
             SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT) | \
4251
         (SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT) | \
4252
         (SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT) | \
4253
         (SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT) | \
4254
         (SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT << \
4255
             SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT_BIT) | \
4256
         (SSL_SERIALIZED_SESSION_CONFIG_SNI << SSL_SERIALIZED_SESSION_CONFIG_SNI_BIT) | \
4257
         (SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA << \
4258
             SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA_BIT) | \
4259
         (SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE << \
4260
             SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE_BIT) | \
4261
         (SSL_SERIALIZED_SESSION_CONFIG_ALPN << \
4262
             SSL_SERIALIZED_SESSION_CONFIG_ALPN_BIT)))
4263
4264
static const unsigned char ssl_serialized_session_header[] = {
4265
    MBEDTLS_VERSION_MAJOR,
4266
    MBEDTLS_VERSION_MINOR,
4267
    MBEDTLS_VERSION_PATCH,
4268
    MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
4269
    MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
4270
};
4271
4272
/*
4273
 * Serialize a session in the following format:
4274
 * (in the presentation language of TLS, RFC 8446 section 3)
4275
 *
4276
 * TLS 1.2 session:
4277
 *
4278
 * struct {
4279
 * #if defined(MBEDTLS_SSL_SESSION_TICKETS)
4280
 *    opaque ticket<0..2^24-1>;       // length 0 means no ticket
4281
 *    uint32 ticket_lifetime;
4282
 * #endif
4283
 * } ClientOnlyData;
4284
 *
4285
 * struct {
4286
 * #if defined(MBEDTLS_HAVE_TIME)
4287
 *    uint64 start_time;
4288
 * #endif
4289
 *     uint8 session_id_len;           // at most 32
4290
 *     opaque session_id[32];
4291
 *     opaque master[48];              // fixed length in the standard
4292
 *     uint32 verify_result;
4293
 * #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE
4294
 *    opaque peer_cert<0..2^24-1>;    // length 0 means no peer cert
4295
 * #else
4296
 *    uint8 peer_cert_digest_type;
4297
 *    opaque peer_cert_digest<0..2^8-1>
4298
 * #endif
4299
 *     select (endpoint) {
4300
 *         case client: ClientOnlyData;
4301
 *         case server: uint64 ticket_creation_time;
4302
 *     };
4303
 * #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
4304
 *    uint8 mfl_code;                 // up to 255 according to standard
4305
 * #endif
4306
 * #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
4307
 *    uint8 encrypt_then_mac;         // 0 or 1
4308
 * #endif
4309
 * } serialized_session_tls12;
4310
 *
4311
 *
4312
 * TLS 1.3 Session:
4313
 *
4314
 * struct {
4315
 * #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
4316
 *    opaque hostname<0..2^16-1>;
4317
 * #endif
4318
 * #if defined(MBEDTLS_HAVE_TIME)
4319
 *    uint64 ticket_reception_time;
4320
 * #endif
4321
 *    uint32 ticket_lifetime;
4322
 *    opaque ticket<1..2^16-1>;
4323
 * } ClientOnlyData;
4324
 *
4325
 * struct {
4326
 *    uint32 ticket_age_add;
4327
 *    uint8 ticket_flags;
4328
 *    opaque resumption_key<0..255>;
4329
 * #if defined(MBEDTLS_SSL_EARLY_DATA)
4330
 *    uint32 max_early_data_size;
4331
 * #endif
4332
 * #if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
4333
 *    uint16 record_size_limit;
4334
 * #endif
4335
 *    select ( endpoint ) {
4336
 *         case client: ClientOnlyData;
4337
 *         case server:
4338
 * #if defined(MBEDTLS_HAVE_TIME)
4339
 *                      uint64 ticket_creation_time;
4340
 * #endif
4341
 * #if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN)
4342
 *                      opaque ticket_alpn<0..256>;
4343
 * #endif
4344
 *     };
4345
 * } serialized_session_tls13;
4346
 *
4347
 *
4348
 * SSL session:
4349
 *
4350
 * struct {
4351
 *
4352
 *    opaque mbedtls_version[3];   // library version: major, minor, patch
4353
 *    opaque session_format[2];    // library-version specific 16-bit field
4354
 *                                 // determining the format of the remaining
4355
 *                                 // serialized data.
4356
 *
4357
 *          Note: When updating the format, remember to keep
4358
 *          these version+format bytes.
4359
 *
4360
 *                                 // In this version, `session_format` determines
4361
 *                                 // the setting of those compile-time
4362
 *                                 // configuration options which influence
4363
 *                                 // the structure of mbedtls_ssl_session.
4364
 *
4365
 *    uint8_t minor_ver;           // Protocol minor version. Possible values:
4366
 *                                 // - TLS 1.2 (0x0303)
4367
 *                                 // - TLS 1.3 (0x0304)
4368
 *    uint8_t endpoint;
4369
 *    uint16_t ciphersuite;
4370
 *
4371
 *    select (serialized_session.tls_version) {
4372
 *
4373
 *      case MBEDTLS_SSL_VERSION_TLS1_2:
4374
 *        serialized_session_tls12 data;
4375
 *      case MBEDTLS_SSL_VERSION_TLS1_3:
4376
 *        serialized_session_tls13 data;
4377
 *
4378
 *   };
4379
 *
4380
 * } serialized_session;
4381
 *
4382
 */
4383
4384
MBEDTLS_CHECK_RETURN_CRITICAL
4385
static int ssl_session_save(const mbedtls_ssl_session *session,
4386
                            unsigned char omit_header,
4387
                            unsigned char *buf,
4388
                            size_t buf_len,
4389
                            size_t *olen)
4390
0
{
4391
0
    unsigned char *p = buf;
4392
0
    size_t used = 0;
4393
0
    size_t remaining_len;
4394
0
#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
4395
0
    size_t out_len;
4396
0
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
4397
0
#endif
4398
0
    if (session == NULL) {
4399
0
        return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
4400
0
    }
4401
4402
0
    if (!omit_header) {
4403
        /*
4404
         * Add Mbed TLS version identifier
4405
         */
4406
0
        used += sizeof(ssl_serialized_session_header);
4407
4408
0
        if (used <= buf_len) {
4409
0
            memcpy(p, ssl_serialized_session_header,
4410
0
                   sizeof(ssl_serialized_session_header));
4411
0
            p += sizeof(ssl_serialized_session_header);
4412
0
        }
4413
0
    }
4414
4415
    /*
4416
     * TLS version identifier, endpoint, ciphersuite
4417
     */
4418
0
    used += 1    /* TLS version */
4419
0
            + 1  /* endpoint */
4420
0
            + 2; /* ciphersuite */
4421
0
    if (used <= buf_len) {
4422
0
        *p++ = MBEDTLS_BYTE_0(session->tls_version);
4423
0
        *p++ = session->endpoint;
4424
0
        MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 0);
4425
0
        p += 2;
4426
0
    }
4427
4428
    /* Forward to version-specific serialization routine. */
4429
0
    remaining_len = (buf_len >= used) ? buf_len - used : 0;
4430
0
    switch (session->tls_version) {
4431
0
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4432
0
        case MBEDTLS_SSL_VERSION_TLS1_2:
4433
0
            used += ssl_tls12_session_save(session, p, remaining_len);
4434
0
            break;
4435
0
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4436
4437
0
#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
4438
0
        case MBEDTLS_SSL_VERSION_TLS1_3:
4439
0
            ret = ssl_tls13_session_save(session, p, remaining_len, &out_len);
4440
0
            if (ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
4441
0
                return ret;
4442
0
            }
4443
0
            used += out_len;
4444
0
            break;
4445
0
#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
4446
4447
0
        default:
4448
0
            return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
4449
0
    }
4450
4451
0
    *olen = used;
4452
0
    if (used > buf_len) {
4453
0
        return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4454
0
    }
4455
4456
0
    return 0;
4457
0
}
4458
4459
/*
4460
 * Public wrapper for ssl_session_save()
4461
 */
4462
int mbedtls_ssl_session_save(const mbedtls_ssl_session *session,
4463
                             unsigned char *buf,
4464
                             size_t buf_len,
4465
                             size_t *olen)
4466
0
{
4467
0
    return ssl_session_save(session, 0, buf, buf_len, olen);
4468
0
}
4469
4470
/*
4471
 * Deserialize session, see mbedtls_ssl_session_save() for format.
4472
 *
4473
 * This internal version is wrapped by a public function that cleans up in
4474
 * case of error, and has an extra option omit_header.
4475
 */
4476
MBEDTLS_CHECK_RETURN_CRITICAL
4477
static int ssl_session_load(mbedtls_ssl_session *session,
4478
                            unsigned char omit_header,
4479
                            const unsigned char *buf,
4480
                            size_t len)
4481
0
{
4482
0
    const unsigned char *p = buf;
4483
0
    const unsigned char * const end = buf + len;
4484
0
    size_t remaining_len;
4485
4486
4487
0
    if (session == NULL) {
4488
0
        return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
4489
0
    }
4490
4491
0
    if (!omit_header) {
4492
        /*
4493
         * Check Mbed TLS version identifier
4494
         */
4495
4496
0
        if ((size_t) (end - p) < sizeof(ssl_serialized_session_header)) {
4497
0
            return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4498
0
        }
4499
4500
0
        if (memcmp(p, ssl_serialized_session_header,
4501
0
                   sizeof(ssl_serialized_session_header)) != 0) {
4502
0
            return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
4503
0
        }
4504
0
        p += sizeof(ssl_serialized_session_header);
4505
0
    }
4506
4507
    /*
4508
     * TLS version identifier, endpoint, ciphersuite
4509
     */
4510
0
    if (4 > (size_t) (end - p)) {
4511
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4512
0
    }
4513
0
    session->tls_version = (mbedtls_ssl_protocol_version) (0x0300 | *p++);
4514
0
    session->endpoint = *p++;
4515
0
    session->ciphersuite = MBEDTLS_GET_UINT16_BE(p, 0);
4516
0
    p += 2;
4517
4518
    /* Dispatch according to TLS version. */
4519
0
    remaining_len = (size_t) (end - p);
4520
0
    switch (session->tls_version) {
4521
0
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4522
0
        case MBEDTLS_SSL_VERSION_TLS1_2:
4523
0
            return ssl_tls12_session_load(session, p, remaining_len);
4524
0
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4525
4526
0
#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
4527
0
        case MBEDTLS_SSL_VERSION_TLS1_3:
4528
0
            return ssl_tls13_session_load(session, p, remaining_len);
4529
0
#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
4530
4531
0
        default:
4532
0
            return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4533
0
    }
4534
0
}
4535
4536
/*
4537
 * Deserialize session: public wrapper for error cleaning
4538
 */
4539
int mbedtls_ssl_session_load(mbedtls_ssl_session *session,
4540
                             const unsigned char *buf,
4541
                             size_t len)
4542
0
{
4543
0
    int ret = ssl_session_load(session, 0, buf, len);
4544
4545
0
    if (ret != 0) {
4546
0
        mbedtls_ssl_session_free(session);
4547
0
    }
4548
4549
0
    return ret;
4550
0
}
4551
4552
/*
4553
 * Perform a single step of the SSL handshake
4554
 */
4555
MBEDTLS_CHECK_RETURN_CRITICAL
4556
static int ssl_prepare_handshake_step(mbedtls_ssl_context *ssl)
4557
41.8k
{
4558
41.8k
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
4559
4560
    /*
4561
     * We may have not been able to send to the peer all the handshake data
4562
     * that were written into the output buffer by the previous handshake step,
4563
     * if the write to the network callback returned with the
4564
     * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
4565
     * We proceed to the next handshake step only when all data from the
4566
     * previous one have been sent to the peer, thus we make sure that this is
4567
     * the case here by calling `mbedtls_ssl_flush_output()`. The function may
4568
     * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
4569
     * we have to wait before to go ahead.
4570
     * In the case of TLS 1.3, handshake step handlers do not send data to the
4571
     * peer. Data are only sent here and through
4572
     * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
4573
     * alert occurred.
4574
     */
4575
41.8k
    if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) {
4576
0
        return ret;
4577
0
    }
4578
4579
41.8k
#if defined(MBEDTLS_SSL_PROTO_DTLS)
4580
41.8k
    if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4581
35.1k
        ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING) {
4582
0
        if ((ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
4583
0
            return ret;
4584
0
        }
4585
0
    }
4586
41.8k
#endif /* MBEDTLS_SSL_PROTO_DTLS */
4587
4588
41.8k
    return ret;
4589
41.8k
}
4590
4591
int mbedtls_ssl_handshake_step(mbedtls_ssl_context *ssl)
4592
41.8k
{
4593
41.8k
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
4594
4595
41.8k
    if (ssl            == NULL                       ||
4596
41.8k
        ssl->conf      == NULL                       ||
4597
41.8k
        ssl->handshake == NULL                       ||
4598
41.8k
        ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER) {
4599
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4600
0
    }
4601
4602
41.8k
    ret = ssl_prepare_handshake_step(ssl);
4603
41.8k
    if (ret != 0) {
4604
0
        return ret;
4605
0
    }
4606
4607
41.8k
    ret = mbedtls_ssl_handle_pending_alert(ssl);
4608
41.8k
    if (ret != 0) {
4609
0
        goto cleanup;
4610
0
    }
4611
4612
    /* If ssl->conf->endpoint is not one of MBEDTLS_SSL_IS_CLIENT or
4613
     * MBEDTLS_SSL_IS_SERVER, this is the return code we give */
4614
41.8k
    ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4615
4616
41.8k
#if defined(MBEDTLS_SSL_CLI_C)
4617
41.8k
    if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
4618
31.4k
        MBEDTLS_SSL_DEBUG_MSG(2, ("client state: %s",
4619
31.4k
                                  mbedtls_ssl_states_str((mbedtls_ssl_states) ssl->state)));
4620
4621
31.4k
        switch (ssl->state) {
4622
5.33k
            case MBEDTLS_SSL_HELLO_REQUEST:
4623
5.33k
                mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
4624
5.33k
                ret = 0;
4625
5.33k
                break;
4626
4627
5.80k
            case MBEDTLS_SSL_CLIENT_HELLO:
4628
5.80k
                ret = mbedtls_ssl_write_client_hello(ssl);
4629
5.80k
                break;
4630
4631
20.3k
            default:
4632
20.3k
#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
4633
20.3k
                if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
4634
0
                    ret = mbedtls_ssl_tls13_handshake_client_step(ssl);
4635
20.3k
                } else {
4636
20.3k
                    ret = mbedtls_ssl_handshake_client_step(ssl);
4637
20.3k
                }
4638
#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
4639
                ret = mbedtls_ssl_handshake_client_step(ssl);
4640
#else
4641
                ret = mbedtls_ssl_tls13_handshake_client_step(ssl);
4642
#endif
4643
31.4k
        }
4644
31.4k
    }
4645
41.8k
#endif /* MBEDTLS_SSL_CLI_C */
4646
4647
41.8k
#if defined(MBEDTLS_SSL_SRV_C)
4648
41.8k
    if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
4649
10.4k
#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
4650
10.4k
        if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
4651
3.55k
            ret = mbedtls_ssl_tls13_handshake_server_step(ssl);
4652
6.85k
        } else {
4653
6.85k
            ret = mbedtls_ssl_handshake_server_step(ssl);
4654
6.85k
        }
4655
#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
4656
        ret = mbedtls_ssl_handshake_server_step(ssl);
4657
#else
4658
        ret = mbedtls_ssl_tls13_handshake_server_step(ssl);
4659
#endif
4660
10.4k
    }
4661
41.8k
#endif /* MBEDTLS_SSL_SRV_C */
4662
4663
41.8k
    if (ret != 0) {
4664
        /* handshake_step return error. And it is same
4665
         * with alert_reason.
4666
         */
4667
8.97k
        if (ssl->send_alert) {
4668
836
            ret = mbedtls_ssl_handle_pending_alert(ssl);
4669
836
            goto cleanup;
4670
836
        }
4671
8.97k
    }
4672
4673
41.8k
cleanup:
4674
41.8k
    return ret;
4675
41.8k
}
4676
4677
/*
4678
 * Perform the SSL handshake
4679
 */
4680
int mbedtls_ssl_handshake(mbedtls_ssl_context *ssl)
4681
8.97k
{
4682
8.97k
    int ret = 0;
4683
4684
    /* Sanity checks */
4685
4686
8.97k
    if (ssl == NULL || ssl->conf == NULL) {
4687
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4688
0
    }
4689
4690
8.97k
#if defined(MBEDTLS_SSL_PROTO_DTLS)
4691
8.97k
    if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4692
7.16k
        (ssl->f_set_timer == NULL || ssl->f_get_timer == NULL)) {
4693
0
        MBEDTLS_SSL_DEBUG_MSG(1, ("You must use "
4694
0
                                  "mbedtls_ssl_set_timer_cb() for DTLS"));
4695
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4696
0
    }
4697
8.97k
#endif /* MBEDTLS_SSL_PROTO_DTLS */
4698
4699
8.97k
    MBEDTLS_SSL_DEBUG_MSG(2, ("=> handshake"));
4700
4701
    /* Main handshake loop */
4702
41.8k
    while (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
4703
41.8k
        ret = mbedtls_ssl_handshake_step(ssl);
4704
4705
41.8k
        if (ret != 0) {
4706
8.97k
            break;
4707
8.97k
        }
4708
41.8k
    }
4709
4710
8.97k
    MBEDTLS_SSL_DEBUG_MSG(2, ("<= handshake"));
4711
4712
8.97k
    return ret;
4713
8.97k
}
4714
4715
#if defined(MBEDTLS_SSL_RENEGOTIATION)
4716
#if defined(MBEDTLS_SSL_SRV_C)
4717
/*
4718
 * Write HelloRequest to request renegotiation on server
4719
 */
4720
MBEDTLS_CHECK_RETURN_CRITICAL
4721
static int ssl_write_hello_request(mbedtls_ssl_context *ssl)
4722
0
{
4723
0
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
4724
4725
0
    MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello request"));
4726
4727
0
    ssl->out_msglen  = 4;
4728
0
    ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4729
0
    ssl->out_msg[0]  = MBEDTLS_SSL_HS_HELLO_REQUEST;
4730
4731
0
    if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
4732
0
        MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
4733
0
        return ret;
4734
0
    }
4735
4736
0
    MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello request"));
4737
4738
0
    return 0;
4739
0
}
4740
#endif /* MBEDTLS_SSL_SRV_C */
4741
4742
/*
4743
 * Actually renegotiate current connection, triggered by either:
4744
 * - any side: calling mbedtls_ssl_renegotiate(),
4745
 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
4746
 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
4747
 *   the initial handshake is completed.
4748
 * If the handshake doesn't complete due to waiting for I/O, it will continue
4749
 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
4750
 */
4751
int mbedtls_ssl_start_renegotiation(mbedtls_ssl_context *ssl)
4752
0
{
4753
0
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
4754
4755
0
    MBEDTLS_SSL_DEBUG_MSG(2, ("=> renegotiate"));
4756
4757
0
    if ((ret = ssl_handshake_init(ssl)) != 0) {
4758
0
        return ret;
4759
0
    }
4760
4761
    /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
4762
     * the ServerHello will have message_seq = 1" */
4763
0
#if defined(MBEDTLS_SSL_PROTO_DTLS)
4764
0
    if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4765
0
        ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) {
4766
0
        if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
4767
0
            ssl->handshake->out_msg_seq = 1;
4768
0
        } else {
4769
0
            ssl->handshake->in_msg_seq = 1;
4770
0
        }
4771
0
    }
4772
0
#endif
4773
4774
0
    mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HELLO_REQUEST);
4775
0
    ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
4776
4777
0
    if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
4778
0
        MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
4779
0
        return ret;
4780
0
    }
4781
4782
0
    MBEDTLS_SSL_DEBUG_MSG(2, ("<= renegotiate"));
4783
4784
0
    return 0;
4785
0
}
4786
4787
/*
4788
 * Renegotiate current connection on client,
4789
 * or request renegotiation on server
4790
 */
4791
int mbedtls_ssl_renegotiate(mbedtls_ssl_context *ssl)
4792
0
{
4793
0
    int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
4794
4795
0
    if (ssl == NULL || ssl->conf == NULL) {
4796
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4797
0
    }
4798
4799
0
#if defined(MBEDTLS_SSL_SRV_C)
4800
    /* On server, just send the request */
4801
0
    if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
4802
0
        if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
4803
0
            return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4804
0
        }
4805
4806
0
        ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
4807
4808
        /* Did we already try/start sending HelloRequest? */
4809
0
        if (ssl->out_left != 0) {
4810
0
            return mbedtls_ssl_flush_output(ssl);
4811
0
        }
4812
4813
0
        return ssl_write_hello_request(ssl);
4814
0
    }
4815
0
#endif /* MBEDTLS_SSL_SRV_C */
4816
4817
0
#if defined(MBEDTLS_SSL_CLI_C)
4818
    /*
4819
     * On client, either start the renegotiation process or,
4820
     * if already in progress, continue the handshake
4821
     */
4822
0
    if (ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
4823
0
        if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
4824
0
            return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4825
0
        }
4826
4827
0
        if ((ret = mbedtls_ssl_start_renegotiation(ssl)) != 0) {
4828
0
            MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_start_renegotiation", ret);
4829
0
            return ret;
4830
0
        }
4831
0
    } else {
4832
0
        if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
4833
0
            MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
4834
0
            return ret;
4835
0
        }
4836
0
    }
4837
0
#endif /* MBEDTLS_SSL_CLI_C */
4838
4839
0
    return ret;
4840
0
}
4841
#endif /* MBEDTLS_SSL_RENEGOTIATION */
4842
4843
void mbedtls_ssl_handshake_free(mbedtls_ssl_context *ssl)
4844
10.7k
{
4845
10.7k
    mbedtls_ssl_handshake_params *handshake = ssl->handshake;
4846
4847
10.7k
    if (handshake == NULL) {
4848
0
        return;
4849
0
    }
4850
4851
10.7k
#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
4852
10.7k
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
4853
10.7k
    if (ssl->handshake->group_list_heap_allocated) {
4854
0
        mbedtls_free((void *) handshake->group_list);
4855
0
    }
4856
10.7k
    handshake->group_list = NULL;
4857
10.7k
#endif /* MBEDTLS_DEPRECATED_REMOVED */
4858
10.7k
#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
4859
4860
10.7k
#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
4861
10.7k
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
4862
10.7k
    if (ssl->handshake->sig_algs_heap_allocated) {
4863
0
        mbedtls_free((void *) handshake->sig_algs);
4864
0
    }
4865
10.7k
    handshake->sig_algs = NULL;
4866
10.7k
#endif /* MBEDTLS_DEPRECATED_REMOVED */
4867
10.7k
#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
4868
10.7k
    if (ssl->handshake->certificate_request_context) {
4869
0
        mbedtls_free((void *) handshake->certificate_request_context);
4870
0
    }
4871
10.7k
#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
4872
10.7k
#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
4873
4874
10.7k
#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
4875
10.7k
    if (ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0) {
4876
0
        ssl->conf->f_async_cancel(ssl);
4877
0
        handshake->async_in_progress = 0;
4878
0
    }
4879
10.7k
#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
4880
4881
10.7k
#if defined(MBEDTLS_MD_CAN_SHA256)
4882
#if defined(MBEDTLS_USE_PSA_CRYPTO)
4883
    psa_hash_abort(&handshake->fin_sha256_psa);
4884
#else
4885
    mbedtls_md_free(&handshake->fin_sha256);
4886
#endif
4887
10.7k
#endif
4888
10.7k
#if defined(MBEDTLS_MD_CAN_SHA384)
4889
#if defined(MBEDTLS_USE_PSA_CRYPTO)
4890
    psa_hash_abort(&handshake->fin_sha384_psa);
4891
#else
4892
    mbedtls_md_free(&handshake->fin_sha384);
4893
#endif
4894
10.7k
#endif
4895
4896
10.7k
#if defined(MBEDTLS_DHM_C)
4897
10.7k
    mbedtls_dhm_free(&handshake->dhm_ctx);
4898
10.7k
#endif
4899
#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
4900
    defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED)
4901
    mbedtls_ecdh_free(&handshake->ecdh_ctx);
4902
#endif
4903
4904
10.7k
#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
4905
#if defined(MBEDTLS_USE_PSA_CRYPTO)
4906
    psa_pake_abort(&handshake->psa_pake_ctx);
4907
    /*
4908
     * Opaque keys are not stored in the handshake's data and it's the user
4909
     * responsibility to destroy them. Clear ones, instead, are created by
4910
     * the TLS library and should be destroyed at the same level
4911
     */
4912
0
    if (!mbedtls_svc_key_id_is_null(handshake->psa_pake_password)) {
4913
0
        psa_destroy_key(handshake->psa_pake_password);
4914
0
    }
4915
0
    handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
4916
#else
4917
    mbedtls_ecjpake_free(&handshake->ecjpake_ctx);
4918
#endif /* MBEDTLS_USE_PSA_CRYPTO */
4919
10.7k
#if defined(MBEDTLS_SSL_CLI_C)
4920
10.7k
    mbedtls_free(handshake->ecjpake_cache);
4921
10.7k
    handshake->ecjpake_cache = NULL;
4922
10.7k
    handshake->ecjpake_cache_len = 0;
4923
10.7k
#endif
4924
10.7k
#endif
4925
4926
10.7k
#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_ANY_ENABLED) || \
4927
10.7k
    defined(MBEDTLS_KEY_EXCHANGE_WITH_ECDSA_ANY_ENABLED) || \
4928
10.7k
    defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
4929
    /* explicit void pointer cast for buggy MS compiler */
4930
10.7k
    mbedtls_free((void *) handshake->curves_tls_id);
4931
10.7k
#endif
4932
4933
10.7k
#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
4934
#if defined(MBEDTLS_USE_PSA_CRYPTO)
4935
0
    if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
4936
        /* The maintenance of the external PSK key slot is the
4937
         * user's responsibility. */
4938
0
        if (ssl->handshake->psk_opaque_is_internal) {
4939
0
            psa_destroy_key(ssl->handshake->psk_opaque);
4940
0
            ssl->handshake->psk_opaque_is_internal = 0;
4941
0
        }
4942
0
        ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4943
0
    }
4944
#else
4945
10.7k
    if (handshake->psk != NULL) {
4946
0
        mbedtls_zeroize_and_free(handshake->psk, handshake->psk_len);
4947
0
    }
4948
#endif /* MBEDTLS_USE_PSA_CRYPTO */
4949
10.7k
#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
4950
4951
10.7k
#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4952
10.7k
    defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
4953
    /*
4954
     * Free only the linked list wrapper, not the keys themselves
4955
     * since the belong to the SNI callback
4956
     */
4957
10.7k
    ssl_key_cert_free(handshake->sni_key_cert);
4958
10.7k
#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
4959
4960
10.7k
#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
4961
10.7k
    mbedtls_x509_crt_restart_free(&handshake->ecrs_ctx);
4962
10.7k
    if (handshake->ecrs_peer_cert != NULL) {
4963
0
        mbedtls_x509_crt_free(handshake->ecrs_peer_cert);
4964
0
        mbedtls_free(handshake->ecrs_peer_cert);
4965
0
    }
4966
10.7k
#endif
4967
4968
#if defined(MBEDTLS_X509_CRT_PARSE_C) &&        \
4969
    !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
4970
    mbedtls_pk_free(&handshake->peer_pubkey);
4971
#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4972
4973
10.7k
#if defined(MBEDTLS_SSL_CLI_C) && \
4974
10.7k
    (defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3))
4975
10.7k
    mbedtls_free(handshake->cookie);
4976
10.7k
#endif /* MBEDTLS_SSL_CLI_C &&
4977
          ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
4978
4979
10.7k
#if defined(MBEDTLS_SSL_PROTO_DTLS)
4980
10.7k
    mbedtls_ssl_flight_free(handshake->flight);
4981
10.7k
    mbedtls_ssl_buffering_free(ssl);
4982
10.7k
#endif /* MBEDTLS_SSL_PROTO_DTLS */
4983
4984
10.7k
#if defined(MBEDTLS_KEY_EXCHANGE_SOME_XXDH_PSA_ANY_ENABLED)
4985
10.7k
    if (handshake->xxdh_psa_privkey_is_external == 0) {
4986
10.7k
        psa_destroy_key(handshake->xxdh_psa_privkey);
4987
10.7k
    }
4988
10.7k
#endif /* MBEDTLS_KEY_EXCHANGE_SOME_XXDH_PSA_ANY_ENABLED */
4989
4990
10.7k
#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
4991
10.7k
    mbedtls_ssl_transform_free(handshake->transform_handshake);
4992
10.7k
    mbedtls_free(handshake->transform_handshake);
4993
10.7k
#if defined(MBEDTLS_SSL_EARLY_DATA)
4994
10.7k
    mbedtls_ssl_transform_free(handshake->transform_earlydata);
4995
10.7k
    mbedtls_free(handshake->transform_earlydata);
4996
10.7k
#endif
4997
10.7k
#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
4998
4999
5000
10.7k
#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
5001
    /* If the buffers are too big - reallocate. Because of the way Mbed TLS
5002
     * processes datagrams and the fact that a datagram is allowed to have
5003
     * several records in it, it is possible that the I/O buffers are not
5004
     * empty at this stage */
5005
10.7k
    handle_buffer_resizing(ssl, 1, mbedtls_ssl_get_input_buflen(ssl),
5006
10.7k
                           mbedtls_ssl_get_output_buflen(ssl));
5007
10.7k
#endif
5008
5009
    /* mbedtls_platform_zeroize MUST be last one in this function */
5010
10.7k
    mbedtls_platform_zeroize(handshake,
5011
10.7k
                             sizeof(mbedtls_ssl_handshake_params));
5012
10.7k
}
mbedtls_ssl_handshake_free
Line
Count
Source
4844
10.7k
{
4845
10.7k
    mbedtls_ssl_handshake_params *handshake = ssl->handshake;
4846
4847
10.7k
    if (handshake == NULL) {
4848
0
        return;
4849
0
    }
4850
4851
10.7k
#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
4852
10.7k
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
4853
10.7k
    if (ssl->handshake->group_list_heap_allocated) {
4854
0
        mbedtls_free((void *) handshake->group_list);
4855
0
    }
4856
10.7k
    handshake->group_list = NULL;
4857
10.7k
#endif /* MBEDTLS_DEPRECATED_REMOVED */
4858
10.7k
#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
4859
4860
10.7k
#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
4861
10.7k
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
4862
10.7k
    if (ssl->handshake->sig_algs_heap_allocated) {
4863
0
        mbedtls_free((void *) handshake->sig_algs);
4864
0
    }
4865
10.7k
    handshake->sig_algs = NULL;
4866
10.7k
#endif /* MBEDTLS_DEPRECATED_REMOVED */
4867
10.7k
#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
4868
10.7k
    if (ssl->handshake->certificate_request_context) {
4869
0
        mbedtls_free((void *) handshake->certificate_request_context);
4870
0
    }
4871
10.7k
#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
4872
10.7k
#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
4873
4874
10.7k
#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
4875
10.7k
    if (ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0) {
4876
0
        ssl->conf->f_async_cancel(ssl);
4877
0
        handshake->async_in_progress = 0;
4878
0
    }
4879
10.7k
#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
4880
4881
10.7k
#if defined(MBEDTLS_MD_CAN_SHA256)
4882
#if defined(MBEDTLS_USE_PSA_CRYPTO)
4883
    psa_hash_abort(&handshake->fin_sha256_psa);
4884
#else
4885
10.7k
    mbedtls_md_free(&handshake->fin_sha256);
4886
10.7k
#endif
4887
10.7k
#endif
4888
10.7k
#if defined(MBEDTLS_MD_CAN_SHA384)
4889
#if defined(MBEDTLS_USE_PSA_CRYPTO)
4890
    psa_hash_abort(&handshake->fin_sha384_psa);
4891
#else
4892
10.7k
    mbedtls_md_free(&handshake->fin_sha384);
4893
10.7k
#endif
4894
10.7k
#endif
4895
4896
10.7k
#if defined(MBEDTLS_DHM_C)
4897
10.7k
    mbedtls_dhm_free(&handshake->dhm_ctx);
4898
10.7k
#endif
4899
10.7k
#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
4900
10.7k
    defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED)
4901
10.7k
    mbedtls_ecdh_free(&handshake->ecdh_ctx);
4902
10.7k
#endif
4903
4904
10.7k
#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
4905
#if defined(MBEDTLS_USE_PSA_CRYPTO)
4906
    psa_pake_abort(&handshake->psa_pake_ctx);
4907
    /*
4908
     * Opaque keys are not stored in the handshake's data and it's the user
4909
     * responsibility to destroy them. Clear ones, instead, are created by
4910
     * the TLS library and should be destroyed at the same level
4911
     */
4912
    if (!mbedtls_svc_key_id_is_null(handshake->psa_pake_password)) {
4913
        psa_destroy_key(handshake->psa_pake_password);
4914
    }
4915
    handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
4916
#else
4917
10.7k
    mbedtls_ecjpake_free(&handshake->ecjpake_ctx);
4918
10.7k
#endif /* MBEDTLS_USE_PSA_CRYPTO */
4919
10.7k
#if defined(MBEDTLS_SSL_CLI_C)
4920
10.7k
    mbedtls_free(handshake->ecjpake_cache);
4921
10.7k
    handshake->ecjpake_cache = NULL;
4922
10.7k
    handshake->ecjpake_cache_len = 0;
4923
10.7k
#endif
4924
10.7k
#endif
4925
4926
10.7k
#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_ANY_ENABLED) || \
4927
10.7k
    defined(MBEDTLS_KEY_EXCHANGE_WITH_ECDSA_ANY_ENABLED) || \
4928
10.7k
    defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
4929
    /* explicit void pointer cast for buggy MS compiler */
4930
10.7k
    mbedtls_free((void *) handshake->curves_tls_id);
4931
10.7k
#endif
4932
4933
10.7k
#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
4934
#if defined(MBEDTLS_USE_PSA_CRYPTO)
4935
    if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
4936
        /* The maintenance of the external PSK key slot is the
4937
         * user's responsibility. */
4938
        if (ssl->handshake->psk_opaque_is_internal) {
4939
            psa_destroy_key(ssl->handshake->psk_opaque);
4940
            ssl->handshake->psk_opaque_is_internal = 0;
4941
        }
4942
        ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4943
    }
4944
#else
4945
10.7k
    if (handshake->psk != NULL) {
4946
0
        mbedtls_zeroize_and_free(handshake->psk, handshake->psk_len);
4947
0
    }
4948
10.7k
#endif /* MBEDTLS_USE_PSA_CRYPTO */
4949
10.7k
#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
4950
4951
10.7k
#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4952
10.7k
    defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
4953
    /*
4954
     * Free only the linked list wrapper, not the keys themselves
4955
     * since the belong to the SNI callback
4956
     */
4957
10.7k
    ssl_key_cert_free(handshake->sni_key_cert);
4958
10.7k
#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
4959
4960
10.7k
#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
4961
10.7k
    mbedtls_x509_crt_restart_free(&handshake->ecrs_ctx);
4962
10.7k
    if (handshake->ecrs_peer_cert != NULL) {
4963
0
        mbedtls_x509_crt_free(handshake->ecrs_peer_cert);
4964
0
        mbedtls_free(handshake->ecrs_peer_cert);
4965
0
    }
4966
10.7k
#endif
4967
4968
#if defined(MBEDTLS_X509_CRT_PARSE_C) &&        \
4969
    !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
4970
    mbedtls_pk_free(&handshake->peer_pubkey);
4971
#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4972
4973
10.7k
#if defined(MBEDTLS_SSL_CLI_C) && \
4974
10.7k
    (defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3))
4975
10.7k
    mbedtls_free(handshake->cookie);
4976
10.7k
#endif /* MBEDTLS_SSL_CLI_C &&
4977
          ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
4978
4979
10.7k
#if defined(MBEDTLS_SSL_PROTO_DTLS)
4980
10.7k
    mbedtls_ssl_flight_free(handshake->flight);
4981
10.7k
    mbedtls_ssl_buffering_free(ssl);
4982
10.7k
#endif /* MBEDTLS_SSL_PROTO_DTLS */
4983
4984
10.7k
#if defined(MBEDTLS_KEY_EXCHANGE_SOME_XXDH_PSA_ANY_ENABLED)
4985
10.7k
    if (handshake->xxdh_psa_privkey_is_external == 0) {
4986
10.7k
        psa_destroy_key(handshake->xxdh_psa_privkey);
4987
10.7k
    }
4988
10.7k
#endif /* MBEDTLS_KEY_EXCHANGE_SOME_XXDH_PSA_ANY_ENABLED */
4989
4990
10.7k
#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
4991
10.7k
    mbedtls_ssl_transform_free(handshake->transform_handshake);
4992
10.7k
    mbedtls_free(handshake->transform_handshake);
4993
10.7k
#if defined(MBEDTLS_SSL_EARLY_DATA)
4994
10.7k
    mbedtls_ssl_transform_free(handshake->transform_earlydata);
4995
10.7k
    mbedtls_free(handshake->transform_earlydata);
4996
10.7k
#endif
4997
10.7k
#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
4998
4999
5000
10.7k
#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
5001
    /* If the buffers are too big - reallocate. Because of the way Mbed TLS
5002
     * processes datagrams and the fact that a datagram is allowed to have
5003
     * several records in it, it is possible that the I/O buffers are not
5004
     * empty at this stage */
5005
10.7k
    handle_buffer_resizing(ssl, 1, mbedtls_ssl_get_input_buflen(ssl),
5006
10.7k
                           mbedtls_ssl_get_output_buflen(ssl));
5007
10.7k
#endif
5008
5009
    /* mbedtls_platform_zeroize MUST be last one in this function */
5010
10.7k
    mbedtls_platform_zeroize(handshake,
5011
10.7k
                             sizeof(mbedtls_ssl_handshake_params));
5012
10.7k
}
Unexecuted instantiation: mbedtls_ssl_handshake_free
5013
5014
void mbedtls_ssl_session_free(mbedtls_ssl_session *session)
5015
13.5k
{
5016
13.5k
    if (session == NULL) {
5017
0
        return;
5018
0
    }
5019
5020
13.5k
#if defined(MBEDTLS_X509_CRT_PARSE_C)
5021
13.5k
    ssl_clear_peer_cert(session);
5022
13.5k
#endif
5023
5024
13.5k
#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
5025
13.5k
#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
5026
13.5k
    defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
5027
13.5k
    mbedtls_free(session->hostname);
5028
13.5k
#endif
5029
13.5k
    mbedtls_free(session->ticket);
5030
13.5k
#endif
5031
5032
13.5k
#if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN) && \
5033
13.5k
    defined(MBEDTLS_SSL_SRV_C)
5034
13.5k
    mbedtls_free(session->ticket_alpn);
5035
13.5k
#endif
5036
5037
13.5k
    mbedtls_platform_zeroize(session, sizeof(mbedtls_ssl_session));
5038
5039
    /* Set verify_result to -1u to indicate 'result not available'. */
5040
13.5k
    session->verify_result = 0xFFFFFFFF;
5041
13.5k
}
5042
5043
#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
5044
5045
#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
5046
#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
5047
#else
5048
#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
5049
#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
5050
5051
#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
5052
5053
#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
5054
#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
5055
#else
5056
#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
5057
#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
5058
5059
#if defined(MBEDTLS_SSL_ALPN)
5060
#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
5061
#else
5062
#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
5063
#endif /* MBEDTLS_SSL_ALPN */
5064
5065
#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT    0
5066
#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT     1
5067
#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT      2
5068
#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT                  3
5069
5070
#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG   \
5071
    ((uint32_t) (                              \
5072
         (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << \
5073
             SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT) | \
5074
         (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << \
5075
             SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT) | \
5076
         (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << \
5077
             SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT) | \
5078
         (SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT) | \
5079
         0u))
5080
5081
static const unsigned char ssl_serialized_context_header[] = {
5082
    MBEDTLS_VERSION_MAJOR,
5083
    MBEDTLS_VERSION_MINOR,
5084
    MBEDTLS_VERSION_PATCH,
5085
    MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
5086
    MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
5087
    MBEDTLS_BYTE_2(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
5088
    MBEDTLS_BYTE_1(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
5089
    MBEDTLS_BYTE_0(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
5090
};
5091
5092
/*
5093
 * Serialize a full SSL context
5094
 *
5095
 * The format of the serialized data is:
5096
 * (in the presentation language of TLS, RFC 8446 section 3)
5097
 *
5098
 *  // header
5099
 *  opaque mbedtls_version[3];   // major, minor, patch
5100
 *  opaque context_format[5];    // version-specific field determining
5101
 *                               // the format of the remaining
5102
 *                               // serialized data.
5103
 *  Note: When updating the format, remember to keep these
5104
 *        version+format bytes. (We may make their size part of the API.)
5105
 *
5106
 *  // session sub-structure
5107
 *  opaque session<1..2^32-1>;  // see mbedtls_ssl_session_save()
5108
 *  // transform sub-structure
5109
 *  uint8 random[64];           // ServerHello.random+ClientHello.random
5110
 *  uint8 in_cid<0..2^8-1>      // Connection ID: expected incoming value
5111
 *  uint8 out_cid<0..2^8-1>     // Connection ID: outgoing value to use
5112
 *  // fields from ssl_context
5113
 *  uint32 badmac_seen_or_in_hsfraglen;         // DTLS: number of records with failing MAC
5114
 *  uint64 in_window_top;       // DTLS: last validated record seq_num
5115
 *  uint64 in_window;           // DTLS: bitmask for replay protection
5116
 *  uint8 disable_datagram_packing; // DTLS: only one record per datagram
5117
 *  uint64 cur_out_ctr;         // Record layer: outgoing sequence number
5118
 *  uint16 mtu;                 // DTLS: path mtu (max outgoing fragment size)
5119
 *  uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
5120
 *
5121
 * Note that many fields of the ssl_context or sub-structures are not
5122
 * serialized, as they fall in one of the following categories:
5123
 *
5124
 *  1. forced value (eg in_left must be 0)
5125
 *  2. pointer to dynamically-allocated memory (eg session, transform)
5126
 *  3. value can be re-derived from other data (eg session keys from MS)
5127
 *  4. value was temporary (eg content of input buffer)
5128
 *  5. value will be provided by the user again (eg I/O callbacks and context)
5129
 */
5130
int mbedtls_ssl_context_save(mbedtls_ssl_context *ssl,
5131
                             unsigned char *buf,
5132
                             size_t buf_len,
5133
                             size_t *olen)
5134
0
{
5135
0
    unsigned char *p = buf;
5136
0
    size_t used = 0;
5137
0
    size_t session_len;
5138
0
    int ret = 0;
5139
5140
    /*
5141
     * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
5142
     * this function's documentation.
5143
     *
5144
     * These are due to assumptions/limitations in the implementation. Some of
5145
     * them are likely to stay (no handshake in progress) some might go away
5146
     * (only DTLS) but are currently used to simplify the implementation.
5147
     */
5148
    /* The initial handshake must be over */
5149
0
    if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
5150
0
        MBEDTLS_SSL_DEBUG_MSG(1, ("Initial handshake isn't over"));
5151
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5152
0
    }
5153
0
    if (ssl->handshake != NULL) {
5154
0
        MBEDTLS_SSL_DEBUG_MSG(1, ("Handshake isn't completed"));
5155
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5156
0
    }
5157
    /* Double-check that sub-structures are indeed ready */
5158
0
    if (ssl->transform == NULL || ssl->session == NULL) {
5159
0
        MBEDTLS_SSL_DEBUG_MSG(1, ("Serialised structures aren't ready"));
5160
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5161
0
    }
5162
    /* There must be no pending incoming or outgoing data */
5163
0
    if (mbedtls_ssl_check_pending(ssl) != 0) {
5164
0
        MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending incoming data"));
5165
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5166
0
    }
5167
0
    if (ssl->out_left != 0) {
5168
0
        MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending outgoing data"));
5169
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5170
0
    }
5171
    /* Protocol must be DTLS, not TLS */
5172
0
    if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
5173
0
        MBEDTLS_SSL_DEBUG_MSG(1, ("Only DTLS is supported"));
5174
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5175
0
    }
5176
    /* Version must be 1.2 */
5177
0
    if (ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2) {
5178
0
        MBEDTLS_SSL_DEBUG_MSG(1, ("Only version 1.2 supported"));
5179
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5180
0
    }
5181
    /* We must be using an AEAD ciphersuite */
5182
0
    if (mbedtls_ssl_transform_uses_aead(ssl->transform) != 1) {
5183
0
        MBEDTLS_SSL_DEBUG_MSG(1, ("Only AEAD ciphersuites supported"));
5184
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5185
0
    }
5186
    /* Renegotiation must not be enabled */
5187
0
#if defined(MBEDTLS_SSL_RENEGOTIATION)
5188
0
    if (ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED) {
5189
0
        MBEDTLS_SSL_DEBUG_MSG(1, ("Renegotiation must not be enabled"));
5190
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5191
0
    }
5192
0
#endif
5193
5194
    /*
5195
     * Version and format identifier
5196
     */
5197
0
    used += sizeof(ssl_serialized_context_header);
5198
5199
0
    if (used <= buf_len) {
5200
0
        memcpy(p, ssl_serialized_context_header,
5201
0
               sizeof(ssl_serialized_context_header));
5202
0
        p += sizeof(ssl_serialized_context_header);
5203
0
    }
5204
5205
    /*
5206
     * Session (length + data)
5207
     */
5208
0
    ret = ssl_session_save(ssl->session, 1, NULL, 0, &session_len);
5209
0
    if (ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
5210
0
        return ret;
5211
0
    }
5212
5213
0
    used += 4 + session_len;
5214
0
    if (used <= buf_len) {
5215
0
        MBEDTLS_PUT_UINT32_BE(session_len, p, 0);
5216
0
        p += 4;
5217
5218
0
        ret = ssl_session_save(ssl->session, 1,
5219
0
                               p, session_len, &session_len);
5220
0
        if (ret != 0) {
5221
0
            return ret;
5222
0
        }
5223
5224
0
        p += session_len;
5225
0
    }
5226
5227
    /*
5228
     * Transform
5229
     */
5230
0
    used += sizeof(ssl->transform->randbytes);
5231
0
    if (used <= buf_len) {
5232
0
        memcpy(p, ssl->transform->randbytes,
5233
0
               sizeof(ssl->transform->randbytes));
5234
0
        p += sizeof(ssl->transform->randbytes);
5235
0
    }
5236
5237
0
#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
5238
0
    used += 2U + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
5239
0
    if (used <= buf_len) {
5240
0
        *p++ = ssl->transform->in_cid_len;
5241
0
        memcpy(p, ssl->transform->in_cid, ssl->transform->in_cid_len);
5242
0
        p += ssl->transform->in_cid_len;
5243
5244
0
        *p++ = ssl->transform->out_cid_len;
5245
0
        memcpy(p, ssl->transform->out_cid, ssl->transform->out_cid_len);
5246
0
        p += ssl->transform->out_cid_len;
5247
0
    }
5248
0
#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
5249
5250
    /*
5251
     * Saved fields from top-level ssl_context structure
5252
     */
5253
0
    used += 4;
5254
0
    if (used <= buf_len) {
5255
0
        MBEDTLS_PUT_UINT32_BE(ssl->badmac_seen_or_in_hsfraglen, p, 0);
5256
0
        p += 4;
5257
0
    }
5258
5259
0
#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
5260
0
    used += 16;
5261
0
    if (used <= buf_len) {
5262
0
        MBEDTLS_PUT_UINT64_BE(ssl->in_window_top, p, 0);
5263
0
        p += 8;
5264
5265
0
        MBEDTLS_PUT_UINT64_BE(ssl->in_window, p, 0);
5266
0
        p += 8;
5267
0
    }
5268
0
#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
5269
5270
0
#if defined(MBEDTLS_SSL_PROTO_DTLS)
5271
0
    used += 1;
5272
0
    if (used <= buf_len) {
5273
0
        *p++ = ssl->disable_datagram_packing;
5274
0
    }
5275
0
#endif /* MBEDTLS_SSL_PROTO_DTLS */
5276
5277
0
    used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
5278
0
    if (used <= buf_len) {
5279
0
        memcpy(p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN);
5280
0
        p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
5281
0
    }
5282
5283
0
#if defined(MBEDTLS_SSL_PROTO_DTLS)
5284
0
    used += 2;
5285
0
    if (used <= buf_len) {
5286
0
        MBEDTLS_PUT_UINT16_BE(ssl->mtu, p, 0);
5287
0
        p += 2;
5288
0
    }
5289
0
#endif /* MBEDTLS_SSL_PROTO_DTLS */
5290
5291
0
#if defined(MBEDTLS_SSL_ALPN)
5292
0
    {
5293
0
        const uint8_t alpn_len = ssl->alpn_chosen
5294
0
                               ? (uint8_t) strlen(ssl->alpn_chosen)
5295
0
                               : 0;
5296
5297
0
        used += 1 + alpn_len;
5298
0
        if (used <= buf_len) {
5299
0
            *p++ = alpn_len;
5300
5301
0
            if (ssl->alpn_chosen != NULL) {
5302
0
                memcpy(p, ssl->alpn_chosen, alpn_len);
5303
0
                p += alpn_len;
5304
0
            }
5305
0
        }
5306
0
    }
5307
0
#endif /* MBEDTLS_SSL_ALPN */
5308
5309
    /*
5310
     * Done
5311
     */
5312
0
    *olen = used;
5313
5314
0
    if (used > buf_len) {
5315
0
        return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5316
0
    }
5317
5318
0
    MBEDTLS_SSL_DEBUG_BUF(4, "saved context", buf, used);
5319
5320
0
    return mbedtls_ssl_session_reset_int(ssl, 0);
5321
0
}
5322
5323
/*
5324
 * Deserialize context, see mbedtls_ssl_context_save() for format.
5325
 *
5326
 * This internal version is wrapped by a public function that cleans up in
5327
 * case of error.
5328
 */
5329
MBEDTLS_CHECK_RETURN_CRITICAL
5330
static int ssl_context_load(mbedtls_ssl_context *ssl,
5331
                            const unsigned char *buf,
5332
                            size_t len)
5333
0
{
5334
0
    const unsigned char *p = buf;
5335
0
    const unsigned char * const end = buf + len;
5336
0
    size_t session_len;
5337
0
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5338
0
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5339
0
    tls_prf_fn prf_func = NULL;
5340
0
#endif
5341
5342
    /*
5343
     * The context should have been freshly setup or reset.
5344
     * Give the user an error in case of obvious misuse.
5345
     * (Checking session is useful because it won't be NULL if we're
5346
     * renegotiating, or if the user mistakenly loaded a session first.)
5347
     */
5348
0
    if (ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
5349
0
        ssl->session != NULL) {
5350
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5351
0
    }
5352
5353
    /*
5354
     * We can't check that the config matches the initial one, but we can at
5355
     * least check it matches the requirements for serializing.
5356
     */
5357
0
    if (
5358
0
#if defined(MBEDTLS_SSL_RENEGOTIATION)
5359
0
        ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
5360
0
#endif
5361
0
        ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
5362
0
        ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
5363
0
        ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2
5364
0
        ) {
5365
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5366
0
    }
5367
5368
0
    MBEDTLS_SSL_DEBUG_BUF(4, "context to load", buf, len);
5369
5370
    /*
5371
     * Check version identifier
5372
     */
5373
0
    if ((size_t) (end - p) < sizeof(ssl_serialized_context_header)) {
5374
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5375
0
    }
5376
5377
0
    if (memcmp(p, ssl_serialized_context_header,
5378
0
               sizeof(ssl_serialized_context_header)) != 0) {
5379
0
        return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
5380
0
    }
5381
0
    p += sizeof(ssl_serialized_context_header);
5382
5383
    /*
5384
     * Session
5385
     */
5386
0
    if ((size_t) (end - p) < 4) {
5387
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5388
0
    }
5389
5390
0
    session_len = MBEDTLS_GET_UINT32_BE(p, 0);
5391
0
    p += 4;
5392
5393
    /* This has been allocated by ssl_handshake_init(), called by
5394
     * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
5395
0
    ssl->session = ssl->session_negotiate;
5396
0
    ssl->session_in = ssl->session;
5397
0
    ssl->session_out = ssl->session;
5398
0
    ssl->session_negotiate = NULL;
5399
5400
0
    if ((size_t) (end - p) < session_len) {
5401
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5402
0
    }
5403
5404
0
    ret = ssl_session_load(ssl->session, 1, p, session_len);
5405
0
    if (ret != 0) {
5406
0
        mbedtls_ssl_session_free(ssl->session);
5407
0
        return ret;
5408
0
    }
5409
5410
0
    p += session_len;
5411
5412
    /*
5413
     * Transform
5414
     */
5415
5416
    /* This has been allocated by ssl_handshake_init(), called by
5417
     * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
5418
0
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5419
0
    ssl->transform = ssl->transform_negotiate;
5420
0
    ssl->transform_in = ssl->transform;
5421
0
    ssl->transform_out = ssl->transform;
5422
0
    ssl->transform_negotiate = NULL;
5423
0
#endif
5424
5425
0
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5426
0
    prf_func = ssl_tls12prf_from_cs(ssl->session->ciphersuite);
5427
0
    if (prf_func == NULL) {
5428
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5429
0
    }
5430
5431
    /* Read random bytes and populate structure */
5432
0
    if ((size_t) (end - p) < sizeof(ssl->transform->randbytes)) {
5433
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5434
0
    }
5435
5436
0
    ret = ssl_tls12_populate_transform(ssl->transform,
5437
0
                                       ssl->session->ciphersuite,
5438
0
                                       ssl->session->master,
5439
0
#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
5440
0
                                       ssl->session->encrypt_then_mac,
5441
0
#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
5442
0
                                       prf_func,
5443
0
                                       p, /* currently pointing to randbytes */
5444
0
                                       MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
5445
0
                                       ssl->conf->endpoint,
5446
0
                                       ssl);
5447
0
    if (ret != 0) {
5448
0
        return ret;
5449
0
    }
5450
0
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5451
0
    p += sizeof(ssl->transform->randbytes);
5452
5453
0
#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
5454
    /* Read connection IDs and store them */
5455
0
    if ((size_t) (end - p) < 1) {
5456
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5457
0
    }
5458
5459
0
    ssl->transform->in_cid_len = *p++;
5460
5461
0
    if ((size_t) (end - p) < ssl->transform->in_cid_len + 1u) {
5462
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5463
0
    }
5464
5465
0
    if (ssl->transform->in_cid_len > sizeof(ssl->transform->in_cid)) {
5466
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5467
0
    }
5468
5469
0
    memcpy(ssl->transform->in_cid, p, ssl->transform->in_cid_len);
5470
0
    p += ssl->transform->in_cid_len;
5471
5472
0
    ssl->transform->out_cid_len = *p++;
5473
5474
0
    if ((size_t) (end - p) < ssl->transform->out_cid_len) {
5475
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5476
0
    }
5477
5478
0
    if (ssl->transform->out_cid_len > sizeof(ssl->transform->out_cid)) {
5479
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5480
0
    }
5481
5482
0
    memcpy(ssl->transform->out_cid, p, ssl->transform->out_cid_len);
5483
0
    p += ssl->transform->out_cid_len;
5484
0
#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
5485
5486
    /*
5487
     * Saved fields from top-level ssl_context structure
5488
     */
5489
0
    if ((size_t) (end - p) < 4) {
5490
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5491
0
    }
5492
5493
0
    ssl->badmac_seen_or_in_hsfraglen = MBEDTLS_GET_UINT32_BE(p, 0);
5494
0
    p += 4;
5495
5496
0
#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
5497
0
    if ((size_t) (end - p) < 16) {
5498
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5499
0
    }
5500
5501
0
    ssl->in_window_top = MBEDTLS_GET_UINT64_BE(p, 0);
5502
0
    p += 8;
5503
5504
0
    ssl->in_window = MBEDTLS_GET_UINT64_BE(p, 0);
5505
0
    p += 8;
5506
0
#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
5507
5508
0
#if defined(MBEDTLS_SSL_PROTO_DTLS)
5509
0
    if ((size_t) (end - p) < 1) {
5510
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5511
0
    }
5512
5513
0
    ssl->disable_datagram_packing = *p++;
5514
0
#endif /* MBEDTLS_SSL_PROTO_DTLS */
5515
5516
0
    if ((size_t) (end - p) < sizeof(ssl->cur_out_ctr)) {
5517
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5518
0
    }
5519
0
    memcpy(ssl->cur_out_ctr, p, sizeof(ssl->cur_out_ctr));
5520
0
    p += sizeof(ssl->cur_out_ctr);
5521
5522
0
#if defined(MBEDTLS_SSL_PROTO_DTLS)
5523
0
    if ((size_t) (end - p) < 2) {
5524
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5525
0
    }
5526
5527
0
    ssl->mtu = MBEDTLS_GET_UINT16_BE(p, 0);
5528
0
    p += 2;
5529
0
#endif /* MBEDTLS_SSL_PROTO_DTLS */
5530
5531
0
#if defined(MBEDTLS_SSL_ALPN)
5532
0
    {
5533
0
        uint8_t alpn_len;
5534
0
        const char **cur;
5535
5536
0
        if ((size_t) (end - p) < 1) {
5537
0
            return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5538
0
        }
5539
5540
0
        alpn_len = *p++;
5541
5542
0
        if (alpn_len != 0 && ssl->conf->alpn_list != NULL) {
5543
            /* alpn_chosen should point to an item in the configured list */
5544
0
            for (cur = ssl->conf->alpn_list; *cur != NULL; cur++) {
5545
0
                if (strlen(*cur) == alpn_len &&
5546
0
                    memcmp(p, *cur, alpn_len) == 0) {
5547
0
                    ssl->alpn_chosen = *cur;
5548
0
                    break;
5549
0
                }
5550
0
            }
5551
0
        }
5552
5553
        /* can only happen on conf mismatch */
5554
0
        if (alpn_len != 0 && ssl->alpn_chosen == NULL) {
5555
0
            return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5556
0
        }
5557
5558
0
        p += alpn_len;
5559
0
    }
5560
0
#endif /* MBEDTLS_SSL_ALPN */
5561
5562
    /*
5563
     * Forced fields from top-level ssl_context structure
5564
     *
5565
     * Most of them already set to the correct value by mbedtls_ssl_init() and
5566
     * mbedtls_ssl_reset(), so we only need to set the remaining ones.
5567
     */
5568
0
    mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
5569
0
    ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5570
5571
    /* Adjust pointers for header fields of outgoing records to
5572
     * the given transform, accounting for explicit IV and CID. */
5573
0
    mbedtls_ssl_update_out_pointers(ssl, ssl->transform);
5574
5575
0
#if defined(MBEDTLS_SSL_PROTO_DTLS)
5576
0
    ssl->in_epoch = 1;
5577
0
#endif
5578
5579
    /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
5580
     * which we don't want - otherwise we'd end up freeing the wrong transform
5581
     * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
5582
     * inappropriately. */
5583
0
    if (ssl->handshake != NULL) {
5584
0
        mbedtls_ssl_handshake_free(ssl);
5585
0
        mbedtls_free(ssl->handshake);
5586
0
        ssl->handshake = NULL;
5587
0
    }
5588
5589
    /*
5590
     * Done - should have consumed entire buffer
5591
     */
5592
0
    if (p != end) {
5593
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5594
0
    }
5595
5596
0
    return 0;
5597
0
}
5598
5599
/*
5600
 * Deserialize context: public wrapper for error cleaning
5601
 */
5602
int mbedtls_ssl_context_load(mbedtls_ssl_context *context,
5603
                             const unsigned char *buf,
5604
                             size_t len)
5605
0
{
5606
0
    int ret = ssl_context_load(context, buf, len);
5607
5608
0
    if (ret != 0) {
5609
0
        mbedtls_ssl_free(context);
5610
0
    }
5611
5612
0
    return ret;
5613
0
}
5614
#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
5615
5616
/*
5617
 * Free an SSL context
5618
 */
5619
void mbedtls_ssl_free(mbedtls_ssl_context *ssl)
5620
9.01k
{
5621
9.01k
    if (ssl == NULL) {
5622
0
        return;
5623
0
    }
5624
5625
9.01k
    MBEDTLS_SSL_DEBUG_MSG(2, ("=> free"));
5626
5627
9.01k
    if (ssl->out_buf != NULL) {
5628
8.97k
#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
5629
8.97k
        size_t out_buf_len = ssl->out_buf_len;
5630
#else
5631
        size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
5632
#endif
5633
5634
8.97k
        mbedtls_zeroize_and_free(ssl->out_buf, out_buf_len);
5635
8.97k
        ssl->out_buf = NULL;
5636
8.97k
    }
5637
5638
9.01k
    if (ssl->in_buf != NULL) {
5639
8.97k
#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
5640
8.97k
        size_t in_buf_len = ssl->in_buf_len;
5641
#else
5642
        size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
5643
#endif
5644
5645
8.97k
        mbedtls_zeroize_and_free(ssl->in_buf, in_buf_len);
5646
8.97k
        ssl->in_buf = NULL;
5647
8.97k
    }
5648
5649
9.01k
    if (ssl->transform) {
5650
0
        mbedtls_ssl_transform_free(ssl->transform);
5651
0
        mbedtls_free(ssl->transform);
5652
0
    }
5653
5654
9.01k
    if (ssl->handshake) {
5655
8.97k
        mbedtls_ssl_handshake_free(ssl);
5656
8.97k
        mbedtls_free(ssl->handshake);
5657
5658
8.97k
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5659
8.97k
        mbedtls_ssl_transform_free(ssl->transform_negotiate);
5660
8.97k
        mbedtls_free(ssl->transform_negotiate);
5661
8.97k
#endif
5662
5663
8.97k
        mbedtls_ssl_session_free(ssl->session_negotiate);
5664
8.97k
        mbedtls_free(ssl->session_negotiate);
5665
8.97k
    }
5666
5667
9.01k
#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
5668
9.01k
    mbedtls_ssl_transform_free(ssl->transform_application);
5669
9.01k
    mbedtls_free(ssl->transform_application);
5670
9.01k
#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
5671
5672
9.01k
    if (ssl->session) {
5673
0
        mbedtls_ssl_session_free(ssl->session);
5674
0
        mbedtls_free(ssl->session);
5675
0
    }
5676
5677
9.01k
#if defined(MBEDTLS_X509_CRT_PARSE_C)
5678
9.01k
    mbedtls_ssl_free_hostname(ssl);
5679
9.01k
#endif
5680
5681
9.01k
#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
5682
9.01k
    mbedtls_free(ssl->cli_id);
5683
9.01k
#endif
5684
5685
9.01k
    MBEDTLS_SSL_DEBUG_MSG(2, ("<= free"));
5686
5687
    /* Actually clear after last debug message */
5688
9.01k
    mbedtls_platform_zeroize(ssl, sizeof(mbedtls_ssl_context));
5689
9.01k
}
5690
5691
/*
5692
 * Initialize mbedtls_ssl_config
5693
 */
5694
void mbedtls_ssl_config_init(mbedtls_ssl_config *conf)
5695
9.01k
{
5696
9.01k
    memset(conf, 0, sizeof(mbedtls_ssl_config));
5697
9.01k
}
5698
5699
/* The selection should be the same as mbedtls_x509_crt_profile_default in
5700
 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
5701
 * curves with a lower resource usage come first.
5702
 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
5703
 * about this list.
5704
 */
5705
static const uint16_t ssl_preset_default_groups[] = {
5706
#if defined(MBEDTLS_ECP_HAVE_CURVE25519)
5707
    MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
5708
#endif
5709
#if defined(MBEDTLS_ECP_HAVE_SECP256R1)
5710
    MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
5711
#endif
5712
#if defined(MBEDTLS_ECP_HAVE_SECP384R1)
5713
    MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
5714
#endif
5715
#if defined(MBEDTLS_ECP_HAVE_CURVE448)
5716
    MBEDTLS_SSL_IANA_TLS_GROUP_X448,
5717
#endif
5718
#if defined(MBEDTLS_ECP_HAVE_SECP521R1)
5719
    MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
5720
#endif
5721
#if defined(MBEDTLS_ECP_HAVE_BP256R1)
5722
    MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
5723
#endif
5724
#if defined(MBEDTLS_ECP_HAVE_BP384R1)
5725
    MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
5726
#endif
5727
#if defined(MBEDTLS_ECP_HAVE_BP512R1)
5728
    MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
5729
#endif
5730
#if defined(PSA_WANT_ALG_FFDH)
5731
    MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE2048,
5732
    MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE3072,
5733
    MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE4096,
5734
    MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE6144,
5735
    MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE8192,
5736
#endif
5737
    MBEDTLS_SSL_IANA_TLS_GROUP_NONE
5738
};
5739
5740
static const int ssl_preset_suiteb_ciphersuites[] = {
5741
    MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
5742
    MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
5743
    0
5744
};
5745
5746
#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
5747
5748
/* NOTICE:
5749
 *   For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
5750
 *   rules SHOULD be upheld.
5751
 *   - No duplicate entries.
5752
 *   - But if there is a good reason, do not change the order of the algorithms.
5753
 *   - ssl_tls12_preset* is for TLS 1.2 use only.
5754
 *   - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
5755
 */
5756
static const uint16_t ssl_preset_default_sig_algs[] = {
5757
5758
#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \
5759
    defined(MBEDTLS_MD_CAN_SHA256) && \
5760
    defined(PSA_WANT_ECC_SECP_R1_256)
5761
    MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
5762
    // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256)
5763
#endif
5764
5765
#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \
5766
    defined(MBEDTLS_MD_CAN_SHA384) && \
5767
    defined(PSA_WANT_ECC_SECP_R1_384)
5768
    MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
5769
    // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384)
5770
#endif
5771
5772
#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \
5773
    defined(MBEDTLS_MD_CAN_SHA512) && \
5774
    defined(PSA_WANT_ECC_SECP_R1_521)
5775
    MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
5776
    // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512)
5777
#endif
5778
5779
#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_MD_CAN_SHA512)
5780
    MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
5781
#endif
5782
5783
#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_MD_CAN_SHA384)
5784
    MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
5785
#endif
5786
5787
#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_MD_CAN_SHA256)
5788
    MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
5789
#endif
5790
5791
#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_MD_CAN_SHA512)
5792
    MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
5793
#endif /* MBEDTLS_RSA_C && MBEDTLS_MD_CAN_SHA512 */
5794
5795
#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_MD_CAN_SHA384)
5796
    MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
5797
#endif /* MBEDTLS_RSA_C && MBEDTLS_MD_CAN_SHA384 */
5798
5799
#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_MD_CAN_SHA256)
5800
    MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
5801
#endif /* MBEDTLS_RSA_C && MBEDTLS_MD_CAN_SHA256 */
5802
5803
    MBEDTLS_TLS_SIG_NONE
5804
};
5805
5806
/* NOTICE: see above */
5807
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5808
static const uint16_t ssl_tls12_preset_default_sig_algs[] = {
5809
5810
#if defined(MBEDTLS_MD_CAN_SHA512)
5811
#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
5812
    MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512),
5813
#endif
5814
#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
5815
    MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
5816
#endif
5817
#if defined(MBEDTLS_RSA_C)
5818
    MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512),
5819
#endif
5820
#endif /* MBEDTLS_MD_CAN_SHA512 */
5821
5822
#if defined(MBEDTLS_MD_CAN_SHA384)
5823
#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
5824
    MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384),
5825
#endif
5826
#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
5827
    MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
5828
#endif
5829
#if defined(MBEDTLS_RSA_C)
5830
    MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384),
5831
#endif
5832
#endif /* MBEDTLS_MD_CAN_SHA384 */
5833
5834
#if defined(MBEDTLS_MD_CAN_SHA256)
5835
#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
5836
    MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256),
5837
#endif
5838
#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
5839
    MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
5840
#endif
5841
#if defined(MBEDTLS_RSA_C)
5842
    MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256),
5843
#endif
5844
#endif /* MBEDTLS_MD_CAN_SHA256 */
5845
5846
    MBEDTLS_TLS_SIG_NONE
5847
};
5848
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5849
5850
/* NOTICE: see above */
5851
static const uint16_t ssl_preset_suiteb_sig_algs[] = {
5852
5853
#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \
5854
    defined(MBEDTLS_MD_CAN_SHA256) && \
5855
    defined(MBEDTLS_ECP_HAVE_SECP256R1)
5856
    MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
5857
    // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256)
5858
#endif
5859
5860
#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \
5861
    defined(MBEDTLS_MD_CAN_SHA384) && \
5862
    defined(MBEDTLS_ECP_HAVE_SECP384R1)
5863
    MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
5864
    // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384)
5865
#endif
5866
5867
    MBEDTLS_TLS_SIG_NONE
5868
};
5869
5870
/* NOTICE: see above */
5871
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5872
static const uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
5873
5874
#if defined(MBEDTLS_MD_CAN_SHA256)
5875
#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
5876
    MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256),
5877
#endif
5878
#endif /* MBEDTLS_MD_CAN_SHA256 */
5879
5880
#if defined(MBEDTLS_MD_CAN_SHA384)
5881
#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
5882
    MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384),
5883
#endif
5884
#endif /* MBEDTLS_MD_CAN_SHA384 */
5885
5886
    MBEDTLS_TLS_SIG_NONE
5887
};
5888
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5889
5890
#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
5891
5892
static const uint16_t ssl_preset_suiteb_groups[] = {
5893
#if defined(MBEDTLS_ECP_HAVE_SECP256R1)
5894
    MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
5895
#endif
5896
#if defined(MBEDTLS_ECP_HAVE_SECP384R1)
5897
    MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
5898
#endif
5899
    MBEDTLS_SSL_IANA_TLS_GROUP_NONE
5900
};
5901
5902
#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
5903
/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
5904
 * to make sure there are no duplicated signature algorithm entries. */
5905
MBEDTLS_CHECK_RETURN_CRITICAL
5906
static int ssl_check_no_sig_alg_duplication(const uint16_t *sig_algs)
5907
35.8k
{
5908
35.8k
    size_t i, j;
5909
35.8k
    int ret = 0;
5910
5911
233k
    for (i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) {
5912
861k
        for (j = 0; j < i; j++) {
5913
663k
            if (sig_algs[i] != sig_algs[j]) {
5914
663k
                continue;
5915
663k
            }
5916
0
            mbedtls_printf(" entry(%04x,%" MBEDTLS_PRINTF_SIZET
5917
0
                           ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
5918
0
                           sig_algs[i], j, i);
5919
0
            ret = -1;
5920
0
        }
5921
197k
    }
5922
35.8k
    return ret;
5923
35.8k
}
5924
5925
#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
5926
5927
/*
5928
 * Load default in mbedtls_ssl_config
5929
 */
5930
int mbedtls_ssl_config_defaults(mbedtls_ssl_config *conf,
5931
                                int endpoint, int transport, int preset)
5932
8.97k
{
5933
8.97k
#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
5934
8.97k
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5935
8.97k
#endif
5936
5937
8.97k
#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
5938
8.97k
    if (ssl_check_no_sig_alg_duplication(ssl_preset_suiteb_sig_algs)) {
5939
0
        mbedtls_printf("ssl_preset_suiteb_sig_algs has duplicated entries\n");
5940
0
        return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5941
0
    }
5942
5943
8.97k
    if (ssl_check_no_sig_alg_duplication(ssl_preset_default_sig_algs)) {
5944
0
        mbedtls_printf("ssl_preset_default_sig_algs has duplicated entries\n");
5945
0
        return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5946
0
    }
5947
5948
8.97k
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5949
8.97k
    if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_suiteb_sig_algs)) {
5950
0
        mbedtls_printf("ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n");
5951
0
        return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5952
0
    }
5953
5954
8.97k
    if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_default_sig_algs)) {
5955
0
        mbedtls_printf("ssl_tls12_preset_default_sig_algs has duplicated entries\n");
5956
0
        return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5957
0
    }
5958
8.97k
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5959
8.97k
#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
5960
5961
    /* Use the functions here so that they are covered in tests,
5962
     * but otherwise access member directly for efficiency */
5963
8.97k
    mbedtls_ssl_conf_endpoint(conf, endpoint);
5964
8.97k
    mbedtls_ssl_conf_transport(conf, transport);
5965
5966
    /*
5967
     * Things that are common to all presets
5968
     */
5969
8.97k
#if defined(MBEDTLS_SSL_CLI_C)
5970
8.97k
    if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
5971
5.33k
        conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
5972
5.33k
#if defined(MBEDTLS_SSL_SESSION_TICKETS)
5973
5.33k
        mbedtls_ssl_conf_session_tickets(conf, MBEDTLS_SSL_SESSION_TICKETS_ENABLED);
5974
5.33k
#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
5975
        /* Contrary to TLS 1.2 tickets, TLS 1.3 NewSessionTicket message
5976
         * handling is disabled by default in Mbed TLS 3.6.x for backward
5977
         * compatibility with client applications developed using Mbed TLS 3.5
5978
         * or earlier with the default configuration.
5979
         *
5980
         * Up to Mbed TLS 3.5, in the default configuration TLS 1.3 was
5981
         * disabled, and a Mbed TLS client with the default configuration would
5982
         * establish a TLS 1.2 connection with a TLS 1.2 and TLS 1.3 capable
5983
         * server.
5984
         *
5985
         * Starting with Mbed TLS 3.6.0, TLS 1.3 is enabled by default, and thus
5986
         * an Mbed TLS client with the default configuration establishes a
5987
         * TLS 1.3 connection with a TLS 1.2 and TLS 1.3 capable server. If
5988
         * following the handshake the TLS 1.3 server sends NewSessionTicket
5989
         * messages and the Mbed TLS client processes them, this results in
5990
         * Mbed TLS high level APIs (mbedtls_ssl_read(),
5991
         * mbedtls_ssl_handshake(), ...) to eventually return an
5992
         * #MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET non fatal error code
5993
         * (see the documentation of mbedtls_ssl_read() for more information on
5994
         * that error code). Applications unaware of that TLS 1.3 specific non
5995
         * fatal error code are then failing.
5996
         */
5997
5.33k
        mbedtls_ssl_conf_tls13_enable_signal_new_session_tickets(
5998
5.33k
            conf, MBEDTLS_SSL_TLS1_3_SIGNAL_NEW_SESSION_TICKETS_DISABLED);
5999
5.33k
#endif
6000
5.33k
#endif
6001
5.33k
    }
6002
8.97k
#endif
6003
6004
8.97k
#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
6005
8.97k
    conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
6006
8.97k
#endif
6007
6008
8.97k
#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
6009
8.97k
    conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
6010
8.97k
#endif
6011
6012
8.97k
#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
6013
8.97k
    conf->f_cookie_write = ssl_cookie_write_dummy;
6014
8.97k
    conf->f_cookie_check = ssl_cookie_check_dummy;
6015
8.97k
#endif
6016
6017
8.97k
#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
6018
8.97k
    conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
6019
8.97k
#endif
6020
6021
8.97k
#if defined(MBEDTLS_SSL_SRV_C)
6022
8.97k
    conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
6023
8.97k
    conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
6024
8.97k
#endif
6025
6026
8.97k
#if defined(MBEDTLS_SSL_PROTO_DTLS)
6027
8.97k
    conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
6028
8.97k
    conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
6029
8.97k
#endif
6030
6031
8.97k
#if defined(MBEDTLS_SSL_RENEGOTIATION)
6032
8.97k
    conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
6033
8.97k
    memset(conf->renego_period,     0x00, 2);
6034
8.97k
    memset(conf->renego_period + 2, 0xFF, 6);
6035
8.97k
#endif
6036
6037
8.97k
#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
6038
8.97k
    if (endpoint == MBEDTLS_SSL_IS_SERVER) {
6039
3.63k
        const unsigned char dhm_p[] =
6040
3.63k
            MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
6041
3.63k
        const unsigned char dhm_g[] =
6042
3.63k
            MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
6043
6044
3.63k
        if ((ret = mbedtls_ssl_conf_dh_param_bin(conf,
6045
3.63k
                                                 dhm_p, sizeof(dhm_p),
6046
3.63k
                                                 dhm_g, sizeof(dhm_g))) != 0) {
6047
0
            return ret;
6048
0
        }
6049
3.63k
    }
6050
8.97k
#endif
6051
6052
8.97k
#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
6053
6054
8.97k
#if defined(MBEDTLS_SSL_EARLY_DATA)
6055
8.97k
    mbedtls_ssl_conf_early_data(conf, MBEDTLS_SSL_EARLY_DATA_DISABLED);
6056
8.97k
#if defined(MBEDTLS_SSL_SRV_C)
6057
8.97k
    mbedtls_ssl_conf_max_early_data_size(conf, MBEDTLS_SSL_MAX_EARLY_DATA_SIZE);
6058
8.97k
#endif
6059
8.97k
#endif /* MBEDTLS_SSL_EARLY_DATA */
6060
6061
8.97k
#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
6062
8.97k
    mbedtls_ssl_conf_new_session_tickets(
6063
8.97k
        conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS);
6064
8.97k
#endif
6065
    /*
6066
     * Allow all TLS 1.3 key exchange modes by default.
6067
     */
6068
8.97k
    conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
6069
8.97k
#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
6070
6071
8.97k
    if (transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
6072
7.16k
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6073
7.16k
        conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
6074
7.16k
        conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
6075
#else
6076
        return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
6077
#endif
6078
7.16k
    } else {
6079
1.80k
#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
6080
1.80k
        conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
6081
1.80k
        conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
6082
#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
6083
        conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
6084
        conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
6085
#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
6086
        conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
6087
        conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
6088
#else
6089
        return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
6090
#endif
6091
1.80k
    }
6092
6093
    /*
6094
     * Preset-specific defaults
6095
     */
6096
8.97k
    switch (preset) {
6097
        /*
6098
         * NSA Suite B
6099
         */
6100
0
        case MBEDTLS_SSL_PRESET_SUITEB:
6101
6102
0
            conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
6103
6104
0
#if defined(MBEDTLS_X509_CRT_PARSE_C)
6105
0
            conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
6106
0
#endif
6107
6108
0
#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
6109
0
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6110
0
            if (mbedtls_ssl_conf_is_tls12_only(conf)) {
6111
0
                conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
6112
0
            } else
6113
0
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
6114
0
            conf->sig_algs = ssl_preset_suiteb_sig_algs;
6115
0
#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
6116
6117
0
#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
6118
0
            conf->curve_list = NULL;
6119
0
#endif
6120
0
            conf->group_list = ssl_preset_suiteb_groups;
6121
0
            break;
6122
6123
        /*
6124
         * Default
6125
         */
6126
8.97k
        default:
6127
6128
8.97k
            conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
6129
6130
8.97k
#if defined(MBEDTLS_X509_CRT_PARSE_C)
6131
8.97k
            conf->cert_profile = &mbedtls_x509_crt_profile_default;
6132
8.97k
#endif
6133
6134
8.97k
#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
6135
8.97k
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6136
8.97k
            if (mbedtls_ssl_conf_is_tls12_only(conf)) {
6137
7.16k
                conf->sig_algs = ssl_tls12_preset_default_sig_algs;
6138
7.16k
            } else
6139
1.80k
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
6140
1.80k
            conf->sig_algs = ssl_preset_default_sig_algs;
6141
8.97k
#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
6142
6143
8.97k
#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
6144
8.97k
            conf->curve_list = NULL;
6145
8.97k
#endif
6146
8.97k
            conf->group_list = ssl_preset_default_groups;
6147
6148
8.97k
#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
6149
8.97k
            conf->dhm_min_bitlen = 1024;
6150
8.97k
#endif
6151
8.97k
    }
6152
6153
8.97k
    return 0;
6154
8.97k
}
6155
6156
/*
6157
 * Free mbedtls_ssl_config
6158
 */
6159
void mbedtls_ssl_config_free(mbedtls_ssl_config *conf)
6160
9.01k
{
6161
9.01k
    if (conf == NULL) {
6162
0
        return;
6163
0
    }
6164
6165
9.01k
#if defined(MBEDTLS_DHM_C)
6166
9.01k
    mbedtls_mpi_free(&conf->dhm_P);
6167
9.01k
    mbedtls_mpi_free(&conf->dhm_G);
6168
9.01k
#endif
6169
6170
9.01k
#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
6171
#if defined(MBEDTLS_USE_PSA_CRYPTO)
6172
46
    if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
6173
0
        conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
6174
0
    }
6175
#endif /* MBEDTLS_USE_PSA_CRYPTO */
6176
9.01k
    if (conf->psk != NULL) {
6177
783
        mbedtls_zeroize_and_free(conf->psk, conf->psk_len);
6178
783
        conf->psk = NULL;
6179
783
        conf->psk_len = 0;
6180
783
    }
6181
6182
9.01k
    if (conf->psk_identity != NULL) {
6183
783
        mbedtls_zeroize_and_free(conf->psk_identity, conf->psk_identity_len);
6184
783
        conf->psk_identity = NULL;
6185
783
        conf->psk_identity_len = 0;
6186
783
    }
6187
9.01k
#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
6188
6189
9.01k
#if defined(MBEDTLS_X509_CRT_PARSE_C)
6190
9.01k
    ssl_key_cert_free(conf->key_cert);
6191
9.01k
#endif
6192
6193
9.01k
    mbedtls_platform_zeroize(conf, sizeof(mbedtls_ssl_config));
6194
9.01k
}
mbedtls_ssl_config_free
Line
Count
Source
6160
8.97k
{
6161
8.97k
    if (conf == NULL) {
6162
0
        return;
6163
0
    }
6164
6165
8.97k
#if defined(MBEDTLS_DHM_C)
6166
8.97k
    mbedtls_mpi_free(&conf->dhm_P);
6167
8.97k
    mbedtls_mpi_free(&conf->dhm_G);
6168
8.97k
#endif
6169
6170
8.97k
#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
6171
#if defined(MBEDTLS_USE_PSA_CRYPTO)
6172
    if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
6173
        conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
6174
    }
6175
#endif /* MBEDTLS_USE_PSA_CRYPTO */
6176
8.97k
    if (conf->psk != NULL) {
6177
783
        mbedtls_zeroize_and_free(conf->psk, conf->psk_len);
6178
783
        conf->psk = NULL;
6179
783
        conf->psk_len = 0;
6180
783
    }
6181
6182
8.97k
    if (conf->psk_identity != NULL) {
6183
783
        mbedtls_zeroize_and_free(conf->psk_identity, conf->psk_identity_len);
6184
783
        conf->psk_identity = NULL;
6185
783
        conf->psk_identity_len = 0;
6186
783
    }
6187
8.97k
#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
6188
6189
8.97k
#if defined(MBEDTLS_X509_CRT_PARSE_C)
6190
8.97k
    ssl_key_cert_free(conf->key_cert);
6191
8.97k
#endif
6192
6193
8.97k
    mbedtls_platform_zeroize(conf, sizeof(mbedtls_ssl_config));
6194
8.97k
}
mbedtls_ssl_config_free
Line
Count
Source
6160
46
{
6161
46
    if (conf == NULL) {
6162
0
        return;
6163
0
    }
6164
6165
46
#if defined(MBEDTLS_DHM_C)
6166
46
    mbedtls_mpi_free(&conf->dhm_P);
6167
46
    mbedtls_mpi_free(&conf->dhm_G);
6168
46
#endif
6169
6170
46
#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
6171
46
#if defined(MBEDTLS_USE_PSA_CRYPTO)
6172
46
    if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
6173
0
        conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
6174
0
    }
6175
46
#endif /* MBEDTLS_USE_PSA_CRYPTO */
6176
46
    if (conf->psk != NULL) {
6177
0
        mbedtls_zeroize_and_free(conf->psk, conf->psk_len);
6178
0
        conf->psk = NULL;
6179
0
        conf->psk_len = 0;
6180
0
    }
6181
6182
46
    if (conf->psk_identity != NULL) {
6183
0
        mbedtls_zeroize_and_free(conf->psk_identity, conf->psk_identity_len);
6184
0
        conf->psk_identity = NULL;
6185
0
        conf->psk_identity_len = 0;
6186
0
    }
6187
46
#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
6188
6189
46
#if defined(MBEDTLS_X509_CRT_PARSE_C)
6190
46
    ssl_key_cert_free(conf->key_cert);
6191
46
#endif
6192
6193
46
    mbedtls_platform_zeroize(conf, sizeof(mbedtls_ssl_config));
6194
46
}
6195
6196
#if defined(MBEDTLS_PK_C) && \
6197
    (defined(MBEDTLS_RSA_C) || defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED))
6198
/*
6199
 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
6200
 */
6201
unsigned char mbedtls_ssl_sig_from_pk(mbedtls_pk_context *pk)
6202
0
{
6203
0
#if defined(MBEDTLS_RSA_C)
6204
0
    if (mbedtls_pk_can_do(pk, MBEDTLS_PK_RSA)) {
6205
0
        return MBEDTLS_SSL_SIG_RSA;
6206
0
    }
6207
0
#endif
6208
0
#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED)
6209
0
    if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECDSA)) {
6210
0
        return MBEDTLS_SSL_SIG_ECDSA;
6211
0
    }
6212
0
#endif
6213
0
    return MBEDTLS_SSL_SIG_ANON;
6214
0
}
6215
6216
unsigned char mbedtls_ssl_sig_from_pk_alg(mbedtls_pk_type_t type)
6217
0
{
6218
0
    switch (type) {
6219
0
        case MBEDTLS_PK_RSA:
6220
0
            return MBEDTLS_SSL_SIG_RSA;
6221
0
        case MBEDTLS_PK_ECDSA:
6222
0
        case MBEDTLS_PK_ECKEY:
6223
0
            return MBEDTLS_SSL_SIG_ECDSA;
6224
0
        default:
6225
0
            return MBEDTLS_SSL_SIG_ANON;
6226
0
    }
6227
0
}
6228
6229
mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig(unsigned char sig)
6230
600
{
6231
600
    switch (sig) {
6232
0
#if defined(MBEDTLS_RSA_C)
6233
44
        case MBEDTLS_SSL_SIG_RSA:
6234
44
            return MBEDTLS_PK_RSA;
6235
0
#endif
6236
0
#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED)
6237
291
        case MBEDTLS_SSL_SIG_ECDSA:
6238
291
            return MBEDTLS_PK_ECDSA;
6239
0
#endif
6240
265
        default:
6241
265
            return MBEDTLS_PK_NONE;
6242
600
    }
6243
600
}
6244
#endif /* MBEDTLS_PK_C &&
6245
          ( MBEDTLS_RSA_C || MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED ) */
6246
6247
/*
6248
 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
6249
 */
6250
mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash(unsigned char hash)
6251
600
{
6252
600
    switch (hash) {
6253
0
#if defined(MBEDTLS_MD_CAN_MD5)
6254
12
        case MBEDTLS_SSL_HASH_MD5:
6255
12
            return MBEDTLS_MD_MD5;
6256
0
#endif
6257
0
#if defined(MBEDTLS_MD_CAN_SHA1)
6258
14
        case MBEDTLS_SSL_HASH_SHA1:
6259
14
            return MBEDTLS_MD_SHA1;
6260
0
#endif
6261
0
#if defined(MBEDTLS_MD_CAN_SHA224)
6262
7
        case MBEDTLS_SSL_HASH_SHA224:
6263
7
            return MBEDTLS_MD_SHA224;
6264
0
#endif
6265
0
#if defined(MBEDTLS_MD_CAN_SHA256)
6266
75
        case MBEDTLS_SSL_HASH_SHA256:
6267
75
            return MBEDTLS_MD_SHA256;
6268
0
#endif
6269
0
#if defined(MBEDTLS_MD_CAN_SHA384)
6270
113
        case MBEDTLS_SSL_HASH_SHA384:
6271
113
            return MBEDTLS_MD_SHA384;
6272
0
#endif
6273
0
#if defined(MBEDTLS_MD_CAN_SHA512)
6274
137
        case MBEDTLS_SSL_HASH_SHA512:
6275
137
            return MBEDTLS_MD_SHA512;
6276
0
#endif
6277
242
        default:
6278
242
            return MBEDTLS_MD_NONE;
6279
600
    }
6280
600
}
6281
6282
/*
6283
 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
6284
 */
6285
unsigned char mbedtls_ssl_hash_from_md_alg(int md)
6286
0
{
6287
0
    switch (md) {
6288
0
#if defined(MBEDTLS_MD_CAN_MD5)
6289
0
        case MBEDTLS_MD_MD5:
6290
0
            return MBEDTLS_SSL_HASH_MD5;
6291
0
#endif
6292
0
#if defined(MBEDTLS_MD_CAN_SHA1)
6293
0
        case MBEDTLS_MD_SHA1:
6294
0
            return MBEDTLS_SSL_HASH_SHA1;
6295
0
#endif
6296
0
#if defined(MBEDTLS_MD_CAN_SHA224)
6297
0
        case MBEDTLS_MD_SHA224:
6298
0
            return MBEDTLS_SSL_HASH_SHA224;
6299
0
#endif
6300
0
#if defined(MBEDTLS_MD_CAN_SHA256)
6301
0
        case MBEDTLS_MD_SHA256:
6302
0
            return MBEDTLS_SSL_HASH_SHA256;
6303
0
#endif
6304
0
#if defined(MBEDTLS_MD_CAN_SHA384)
6305
0
        case MBEDTLS_MD_SHA384:
6306
0
            return MBEDTLS_SSL_HASH_SHA384;
6307
0
#endif
6308
0
#if defined(MBEDTLS_MD_CAN_SHA512)
6309
0
        case MBEDTLS_MD_SHA512:
6310
0
            return MBEDTLS_SSL_HASH_SHA512;
6311
0
#endif
6312
0
        default:
6313
0
            return MBEDTLS_SSL_HASH_NONE;
6314
0
    }
6315
0
}
6316
6317
/*
6318
 * Check if a curve proposed by the peer is in our list.
6319
 * Return 0 if we're willing to use it, -1 otherwise.
6320
 */
6321
int mbedtls_ssl_check_curve_tls_id(const mbedtls_ssl_context *ssl, uint16_t tls_id)
6322
1.02k
{
6323
1.02k
    const uint16_t *group_list = mbedtls_ssl_get_groups(ssl);
6324
6325
1.02k
    if (group_list == NULL) {
6326
0
        return -1;
6327
0
    }
6328
6329
3.47k
    for (; *group_list != 0; group_list++) {
6330
3.43k
        if (*group_list == tls_id) {
6331
979
            return 0;
6332
979
        }
6333
3.43k
    }
6334
6335
42
    return -1;
6336
1.02k
}
6337
6338
#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
6339
/*
6340
 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
6341
 */
6342
int mbedtls_ssl_check_curve(const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id)
6343
1.02k
{
6344
1.02k
    uint16_t tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(grp_id);
6345
6346
1.02k
    if (tls_id == 0) {
6347
0
        return -1;
6348
0
    }
6349
6350
1.02k
    return mbedtls_ssl_check_curve_tls_id(ssl, tls_id);
6351
1.02k
}
6352
#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
6353
6354
static const struct {
6355
    uint16_t tls_id;
6356
    mbedtls_ecp_group_id ecp_group_id;
6357
    psa_ecc_family_t psa_family;
6358
    uint16_t bits;
6359
} tls_id_match_table[] =
6360
{
6361
#if defined(MBEDTLS_ECP_HAVE_SECP521R1)
6362
    { 25, MBEDTLS_ECP_DP_SECP521R1, PSA_ECC_FAMILY_SECP_R1, 521 },
6363
#endif
6364
#if defined(MBEDTLS_ECP_HAVE_BP512R1)
6365
    { 28, MBEDTLS_ECP_DP_BP512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 512 },
6366
#endif
6367
#if defined(MBEDTLS_ECP_HAVE_SECP384R1)
6368
    { 24, MBEDTLS_ECP_DP_SECP384R1, PSA_ECC_FAMILY_SECP_R1, 384 },
6369
#endif
6370
#if defined(MBEDTLS_ECP_HAVE_BP384R1)
6371
    { 27, MBEDTLS_ECP_DP_BP384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 384 },
6372
#endif
6373
#if defined(MBEDTLS_ECP_HAVE_SECP256R1)
6374
    { 23, MBEDTLS_ECP_DP_SECP256R1, PSA_ECC_FAMILY_SECP_R1, 256 },
6375
#endif
6376
#if defined(MBEDTLS_ECP_HAVE_SECP256K1)
6377
    { 22, MBEDTLS_ECP_DP_SECP256K1, PSA_ECC_FAMILY_SECP_K1, 256 },
6378
#endif
6379
#if defined(MBEDTLS_ECP_HAVE_BP256R1)
6380
    { 26, MBEDTLS_ECP_DP_BP256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 256 },
6381
#endif
6382
#if defined(MBEDTLS_ECP_HAVE_SECP224R1)
6383
    { 21, MBEDTLS_ECP_DP_SECP224R1, PSA_ECC_FAMILY_SECP_R1, 224 },
6384
#endif
6385
#if defined(MBEDTLS_ECP_HAVE_SECP224K1)
6386
    { 20, MBEDTLS_ECP_DP_SECP224K1, PSA_ECC_FAMILY_SECP_K1, 224 },
6387
#endif
6388
#if defined(MBEDTLS_ECP_HAVE_SECP192R1)
6389
    { 19, MBEDTLS_ECP_DP_SECP192R1, PSA_ECC_FAMILY_SECP_R1, 192 },
6390
#endif
6391
#if defined(MBEDTLS_ECP_HAVE_SECP192K1)
6392
    { 18, MBEDTLS_ECP_DP_SECP192K1, PSA_ECC_FAMILY_SECP_K1, 192 },
6393
#endif
6394
#if defined(MBEDTLS_ECP_HAVE_CURVE25519)
6395
    { 29, MBEDTLS_ECP_DP_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY, 255 },
6396
#endif
6397
#if defined(MBEDTLS_ECP_HAVE_CURVE448)
6398
    { 30, MBEDTLS_ECP_DP_CURVE448, PSA_ECC_FAMILY_MONTGOMERY, 448 },
6399
#endif
6400
    { 0, MBEDTLS_ECP_DP_NONE, 0, 0 },
6401
};
6402
6403
int mbedtls_ssl_get_psa_curve_info_from_tls_id(uint16_t tls_id,
6404
                                               psa_key_type_t *type,
6405
                                               size_t *bits)
6406
0
{
6407
0
    for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
6408
0
        if (tls_id_match_table[i].tls_id == tls_id) {
6409
0
            if (type != NULL) {
6410
0
                *type = PSA_KEY_TYPE_ECC_KEY_PAIR(tls_id_match_table[i].psa_family);
6411
0
            }
6412
0
            if (bits != NULL) {
6413
0
                *bits = tls_id_match_table[i].bits;
6414
0
            }
6415
0
            return PSA_SUCCESS;
6416
0
        }
6417
0
    }
6418
6419
0
    return PSA_ERROR_NOT_SUPPORTED;
6420
0
}
6421
6422
mbedtls_ecp_group_id mbedtls_ssl_get_ecp_group_id_from_tls_id(uint16_t tls_id)
6423
51.1k
{
6424
336k
    for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
6425
331k
        if (tls_id_match_table[i].tls_id == tls_id) {
6426
46.6k
            return tls_id_match_table[i].ecp_group_id;
6427
46.6k
        }
6428
331k
    }
6429
6430
4.41k
    return MBEDTLS_ECP_DP_NONE;
6431
51.1k
}
6432
6433
uint16_t mbedtls_ssl_get_tls_id_from_ecp_group_id(mbedtls_ecp_group_id grp_id)
6434
2.04k
{
6435
14.2k
    for (int i = 0; tls_id_match_table[i].ecp_group_id != MBEDTLS_ECP_DP_NONE;
6436
14.2k
         i++) {
6437
14.2k
        if (tls_id_match_table[i].ecp_group_id == grp_id) {
6438
2.04k
            return tls_id_match_table[i].tls_id;
6439
2.04k
        }
6440
14.2k
    }
6441
6442
0
    return 0;
6443
2.04k
}
6444
6445
#if defined(MBEDTLS_DEBUG_C)
6446
static const struct {
6447
    uint16_t tls_id;
6448
    const char *name;
6449
} tls_id_curve_name_table[] =
6450
{
6451
    { MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1, "secp521r1" },
6452
    { MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1, "brainpoolP512r1" },
6453
    { MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, "secp384r1" },
6454
    { MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1, "brainpoolP384r1" },
6455
    { MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, "secp256r1" },
6456
    { MBEDTLS_SSL_IANA_TLS_GROUP_SECP256K1, "secp256k1" },
6457
    { MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1, "brainpoolP256r1" },
6458
    { MBEDTLS_SSL_IANA_TLS_GROUP_SECP224R1, "secp224r1" },
6459
    { MBEDTLS_SSL_IANA_TLS_GROUP_SECP224K1, "secp224k1" },
6460
    { MBEDTLS_SSL_IANA_TLS_GROUP_SECP192R1, "secp192r1" },
6461
    { MBEDTLS_SSL_IANA_TLS_GROUP_SECP192K1, "secp192k1" },
6462
    { MBEDTLS_SSL_IANA_TLS_GROUP_X25519, "x25519" },
6463
    { MBEDTLS_SSL_IANA_TLS_GROUP_X448, "x448" },
6464
    { 0, NULL },
6465
};
6466
6467
const char *mbedtls_ssl_get_curve_name_from_tls_id(uint16_t tls_id)
6468
1.09k
{
6469
7.50k
    for (int i = 0; tls_id_curve_name_table[i].tls_id != 0; i++) {
6470
7.50k
        if (tls_id_curve_name_table[i].tls_id == tls_id) {
6471
1.09k
            return tls_id_curve_name_table[i].name;
6472
1.09k
        }
6473
7.50k
    }
6474
6475
0
    return NULL;
6476
1.09k
}
6477
#endif
6478
6479
#if defined(MBEDTLS_USE_PSA_CRYPTO)
6480
int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl,
6481
                                         const mbedtls_md_type_t md,
6482
                                         unsigned char *dst,
6483
                                         size_t dst_len,
6484
                                         size_t *olen)
6485
0
{
6486
0
    psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
6487
0
    psa_hash_operation_t *hash_operation_to_clone;
6488
0
    psa_hash_operation_t hash_operation = psa_hash_operation_init();
6489
6490
0
    *olen = 0;
6491
6492
0
    switch (md) {
6493
0
#if defined(MBEDTLS_MD_CAN_SHA384)
6494
0
        case MBEDTLS_MD_SHA384:
6495
0
            hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
6496
0
            break;
6497
0
#endif
6498
6499
0
#if defined(MBEDTLS_MD_CAN_SHA256)
6500
0
        case MBEDTLS_MD_SHA256:
6501
0
            hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
6502
0
            break;
6503
0
#endif
6504
6505
0
        default:
6506
0
            goto exit;
6507
0
    }
6508
6509
0
    status = psa_hash_clone(hash_operation_to_clone, &hash_operation);
6510
0
    if (status != PSA_SUCCESS) {
6511
0
        goto exit;
6512
0
    }
6513
6514
0
    status = psa_hash_finish(&hash_operation, dst, dst_len, olen);
6515
0
    if (status != PSA_SUCCESS) {
6516
0
        goto exit;
6517
0
    }
6518
6519
0
exit:
6520
#if !defined(MBEDTLS_MD_CAN_SHA384) && \
6521
    !defined(MBEDTLS_MD_CAN_SHA256)
6522
    (void) ssl;
6523
#endif
6524
0
    return PSA_TO_MBEDTLS_ERR(status);
6525
0
}
6526
#else /* MBEDTLS_USE_PSA_CRYPTO */
6527
6528
#if defined(MBEDTLS_MD_CAN_SHA384)
6529
MBEDTLS_CHECK_RETURN_CRITICAL
6530
static int ssl_get_handshake_transcript_sha384(mbedtls_ssl_context *ssl,
6531
                                               unsigned char *dst,
6532
                                               size_t dst_len,
6533
                                               size_t *olen)
6534
0
{
6535
0
    int ret;
6536
0
    mbedtls_md_context_t sha384;
6537
6538
0
    if (dst_len < 48) {
6539
0
        return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
6540
0
    }
6541
6542
0
    mbedtls_md_init(&sha384);
6543
0
    ret = mbedtls_md_setup(&sha384, mbedtls_md_info_from_type(MBEDTLS_MD_SHA384), 0);
6544
0
    if (ret != 0) {
6545
0
        goto exit;
6546
0
    }
6547
0
    ret = mbedtls_md_clone(&sha384, &ssl->handshake->fin_sha384);
6548
0
    if (ret != 0) {
6549
0
        goto exit;
6550
0
    }
6551
6552
0
    if ((ret = mbedtls_md_finish(&sha384, dst)) != 0) {
6553
0
        MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
6554
0
        goto exit;
6555
0
    }
6556
6557
0
    *olen = 48;
6558
6559
0
exit:
6560
6561
0
    mbedtls_md_free(&sha384);
6562
0
    return ret;
6563
0
}
6564
#endif /* MBEDTLS_MD_CAN_SHA384 */
6565
6566
#if defined(MBEDTLS_MD_CAN_SHA256)
6567
MBEDTLS_CHECK_RETURN_CRITICAL
6568
static int ssl_get_handshake_transcript_sha256(mbedtls_ssl_context *ssl,
6569
                                               unsigned char *dst,
6570
                                               size_t dst_len,
6571
                                               size_t *olen)
6572
0
{
6573
0
    int ret;
6574
0
    mbedtls_md_context_t sha256;
6575
6576
0
    if (dst_len < 32) {
6577
0
        return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
6578
0
    }
6579
6580
0
    mbedtls_md_init(&sha256);
6581
0
    ret = mbedtls_md_setup(&sha256, mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), 0);
6582
0
    if (ret != 0) {
6583
0
        goto exit;
6584
0
    }
6585
0
    ret = mbedtls_md_clone(&sha256, &ssl->handshake->fin_sha256);
6586
0
    if (ret != 0) {
6587
0
        goto exit;
6588
0
    }
6589
6590
0
    if ((ret = mbedtls_md_finish(&sha256, dst)) != 0) {
6591
0
        MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
6592
0
        goto exit;
6593
0
    }
6594
6595
0
    *olen = 32;
6596
6597
0
exit:
6598
6599
0
    mbedtls_md_free(&sha256);
6600
0
    return ret;
6601
0
}
6602
#endif /* MBEDTLS_MD_CAN_SHA256 */
6603
6604
int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl,
6605
                                         const mbedtls_md_type_t md,
6606
                                         unsigned char *dst,
6607
                                         size_t dst_len,
6608
                                         size_t *olen)
6609
0
{
6610
0
    switch (md) {
6611
6612
0
#if defined(MBEDTLS_MD_CAN_SHA384)
6613
0
        case MBEDTLS_MD_SHA384:
6614
0
            return ssl_get_handshake_transcript_sha384(ssl, dst, dst_len, olen);
6615
0
#endif /* MBEDTLS_MD_CAN_SHA384*/
6616
6617
0
#if defined(MBEDTLS_MD_CAN_SHA256)
6618
0
        case MBEDTLS_MD_SHA256:
6619
0
            return ssl_get_handshake_transcript_sha256(ssl, dst, dst_len, olen);
6620
0
#endif /* MBEDTLS_MD_CAN_SHA256*/
6621
6622
0
        default:
6623
#if !defined(MBEDTLS_MD_CAN_SHA384) && \
6624
            !defined(MBEDTLS_MD_CAN_SHA256)
6625
            (void) ssl;
6626
            (void) dst;
6627
            (void) dst_len;
6628
            (void) olen;
6629
#endif
6630
0
            break;
6631
0
    }
6632
0
    return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
6633
0
}
6634
6635
#endif /* !MBEDTLS_USE_PSA_CRYPTO */
6636
6637
#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
6638
/* mbedtls_ssl_parse_sig_alg_ext()
6639
 *
6640
 * The `extension_data` field of signature algorithm contains  a `SignatureSchemeList`
6641
 * value (TLS 1.3 RFC8446):
6642
 *      enum {
6643
 *         ....
6644
 *        ecdsa_secp256r1_sha256( 0x0403 ),
6645
 *        ecdsa_secp384r1_sha384( 0x0503 ),
6646
 *        ecdsa_secp521r1_sha512( 0x0603 ),
6647
 *         ....
6648
 *      } SignatureScheme;
6649
 *
6650
 *      struct {
6651
 *         SignatureScheme supported_signature_algorithms<2..2^16-2>;
6652
 *      } SignatureSchemeList;
6653
 *
6654
 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
6655
 * value (TLS 1.2 RFC5246):
6656
 *      enum {
6657
 *          none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
6658
 *          sha512(6), (255)
6659
 *      } HashAlgorithm;
6660
 *
6661
 *      enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
6662
 *        SignatureAlgorithm;
6663
 *
6664
 *      struct {
6665
 *          HashAlgorithm hash;
6666
 *          SignatureAlgorithm signature;
6667
 *      } SignatureAndHashAlgorithm;
6668
 *
6669
 *      SignatureAndHashAlgorithm
6670
 *        supported_signature_algorithms<2..2^16-2>;
6671
 *
6672
 * The TLS 1.3 signature algorithm extension was defined to be a compatible
6673
 * generalization of the TLS 1.2 signature algorithm extension.
6674
 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
6675
 * `SignatureScheme` field of TLS 1.3
6676
 *
6677
 */
6678
int mbedtls_ssl_parse_sig_alg_ext(mbedtls_ssl_context *ssl,
6679
                                  const unsigned char *buf,
6680
                                  const unsigned char *end)
6681
1.95k
{
6682
1.95k
    const unsigned char *p = buf;
6683
1.95k
    size_t supported_sig_algs_len = 0;
6684
1.95k
    const unsigned char *supported_sig_algs_end;
6685
1.95k
    uint16_t sig_alg;
6686
1.95k
    uint32_t common_idx = 0;
6687
6688
1.95k
    MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
6689
1.94k
    supported_sig_algs_len = MBEDTLS_GET_UINT16_BE(p, 0);
6690
1.94k
    p += 2;
6691
6692
1.94k
    memset(ssl->handshake->received_sig_algs, 0,
6693
1.94k
           sizeof(ssl->handshake->received_sig_algs));
6694
6695
1.94k
    MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, supported_sig_algs_len);
6696
1.91k
    supported_sig_algs_end = p + supported_sig_algs_len;
6697
88.4k
    while (p < supported_sig_algs_end) {
6698
86.6k
        MBEDTLS_SSL_CHK_BUF_READ_PTR(p, supported_sig_algs_end, 2);
6699
86.5k
        sig_alg = MBEDTLS_GET_UINT16_BE(p, 0);
6700
86.5k
        p += 2;
6701
86.5k
        MBEDTLS_SSL_DEBUG_MSG(4, ("received signature algorithm: 0x%x %s",
6702
86.5k
                                  sig_alg,
6703
86.5k
                                  mbedtls_ssl_sig_alg_to_str(sig_alg)));
6704
86.5k
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6705
86.5k
        if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
6706
86.5k
            (!(mbedtls_ssl_sig_alg_is_supported(ssl, sig_alg) &&
6707
78.9k
               mbedtls_ssl_sig_alg_is_offered(ssl, sig_alg)))) {
6708
78.9k
            continue;
6709
78.9k
        }
6710
7.61k
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
6711
6712
7.61k
        MBEDTLS_SSL_DEBUG_MSG(4, ("valid signature algorithm: %s",
6713
7.61k
                                  mbedtls_ssl_sig_alg_to_str(sig_alg)));
6714
6715
7.61k
        if (common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE) {
6716
3.53k
            ssl->handshake->received_sig_algs[common_idx] = sig_alg;
6717
3.53k
            common_idx += 1;
6718
3.53k
        }
6719
7.61k
    }
6720
    /* Check that we consumed all the message. */
6721
1.78k
    if (p != end) {
6722
97
        MBEDTLS_SSL_DEBUG_MSG(1,
6723
97
                              ("Signature algorithms extension length misaligned"));
6724
97
        MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
6725
97
                                     MBEDTLS_ERR_SSL_DECODE_ERROR);
6726
97
        return MBEDTLS_ERR_SSL_DECODE_ERROR;
6727
97
    }
6728
6729
1.69k
    if (common_idx == 0) {
6730
92
        MBEDTLS_SSL_DEBUG_MSG(3, ("no signature algorithm in common"));
6731
92
        MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
6732
92
                                     MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
6733
92
        return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
6734
92
    }
6735
6736
1.59k
    ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
6737
1.59k
    return 0;
6738
1.69k
}
6739
6740
#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
6741
6742
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6743
6744
#if defined(MBEDTLS_USE_PSA_CRYPTO)
6745
6746
static psa_status_t setup_psa_key_derivation(psa_key_derivation_operation_t *derivation,
6747
                                             mbedtls_svc_key_id_t key,
6748
                                             psa_algorithm_t alg,
6749
                                             const unsigned char *raw_psk, size_t raw_psk_length,
6750
                                             const unsigned char *seed, size_t seed_length,
6751
                                             const unsigned char *label, size_t label_length,
6752
                                             const unsigned char *other_secret,
6753
                                             size_t other_secret_length,
6754
                                             size_t capacity)
6755
0
{
6756
0
    psa_status_t status;
6757
6758
0
    status = psa_key_derivation_setup(derivation, alg);
6759
0
    if (status != PSA_SUCCESS) {
6760
0
        return status;
6761
0
    }
6762
6763
0
    if (PSA_ALG_IS_TLS12_PRF(alg) || PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
6764
0
        status = psa_key_derivation_input_bytes(derivation,
6765
0
                                                PSA_KEY_DERIVATION_INPUT_SEED,
6766
0
                                                seed, seed_length);
6767
0
        if (status != PSA_SUCCESS) {
6768
0
            return status;
6769
0
        }
6770
6771
0
        if (other_secret != NULL) {
6772
0
            status = psa_key_derivation_input_bytes(derivation,
6773
0
                                                    PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
6774
0
                                                    other_secret, other_secret_length);
6775
0
            if (status != PSA_SUCCESS) {
6776
0
                return status;
6777
0
            }
6778
0
        }
6779
6780
0
        if (mbedtls_svc_key_id_is_null(key)) {
6781
0
            status = psa_key_derivation_input_bytes(
6782
0
                derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
6783
0
                raw_psk, raw_psk_length);
6784
0
        } else {
6785
0
            status = psa_key_derivation_input_key(
6786
0
                derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key);
6787
0
        }
6788
0
        if (status != PSA_SUCCESS) {
6789
0
            return status;
6790
0
        }
6791
6792
0
        status = psa_key_derivation_input_bytes(derivation,
6793
0
                                                PSA_KEY_DERIVATION_INPUT_LABEL,
6794
0
                                                label, label_length);
6795
0
        if (status != PSA_SUCCESS) {
6796
0
            return status;
6797
0
        }
6798
0
    } else {
6799
0
        return PSA_ERROR_NOT_SUPPORTED;
6800
0
    }
6801
6802
0
    status = psa_key_derivation_set_capacity(derivation, capacity);
6803
0
    if (status != PSA_SUCCESS) {
6804
0
        return status;
6805
0
    }
6806
6807
0
    return PSA_SUCCESS;
6808
0
}
6809
6810
#if defined(PSA_WANT_ALG_SHA_384) || \
6811
    defined(PSA_WANT_ALG_SHA_256)
6812
MBEDTLS_CHECK_RETURN_CRITICAL
6813
static int tls_prf_generic(mbedtls_md_type_t md_type,
6814
                           const unsigned char *secret, size_t slen,
6815
                           const char *label, size_t label_len,
6816
                           const unsigned char *random, size_t rlen,
6817
                           unsigned char *dstbuf, size_t dlen)
6818
0
{
6819
0
    psa_status_t status;
6820
0
    psa_algorithm_t alg;
6821
0
    mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
6822
0
    psa_key_derivation_operation_t derivation =
6823
0
        PSA_KEY_DERIVATION_OPERATION_INIT;
6824
6825
0
    if (md_type == MBEDTLS_MD_SHA384) {
6826
0
        alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
6827
0
    } else {
6828
0
        alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
6829
0
    }
6830
6831
    /* Normally a "secret" should be long enough to be impossible to
6832
     * find by brute force, and in particular should not be empty. But
6833
     * this PRF is also used to derive an IV, in particular in EAP-TLS,
6834
     * and for this use case it makes sense to have a 0-length "secret".
6835
     * Since the key API doesn't allow importing a key of length 0,
6836
     * keep master_key=0, which setup_psa_key_derivation() understands
6837
     * to mean a 0-length "secret" input. */
6838
0
    if (slen != 0) {
6839
0
        psa_key_attributes_t key_attributes = psa_key_attributes_init();
6840
0
        psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
6841
0
        psa_set_key_algorithm(&key_attributes, alg);
6842
0
        psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE);
6843
6844
0
        status = psa_import_key(&key_attributes, secret, slen, &master_key);
6845
0
        if (status != PSA_SUCCESS) {
6846
0
            return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6847
0
        }
6848
0
    }
6849
6850
0
    status = setup_psa_key_derivation(&derivation,
6851
0
                                      master_key, alg,
6852
0
                                      NULL, 0,
6853
0
                                      random, rlen,
6854
0
                                      (unsigned char const *) label,
6855
0
                                      label_len,
6856
0
                                      NULL, 0,
6857
0
                                      dlen);
6858
0
    if (status != PSA_SUCCESS) {
6859
0
        psa_key_derivation_abort(&derivation);
6860
0
        psa_destroy_key(master_key);
6861
0
        return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6862
0
    }
6863
6864
0
    status = psa_key_derivation_output_bytes(&derivation, dstbuf, dlen);
6865
0
    if (status != PSA_SUCCESS) {
6866
0
        psa_key_derivation_abort(&derivation);
6867
0
        psa_destroy_key(master_key);
6868
0
        return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6869
0
    }
6870
6871
0
    status = psa_key_derivation_abort(&derivation);
6872
0
    if (status != PSA_SUCCESS) {
6873
0
        psa_destroy_key(master_key);
6874
0
        return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6875
0
    }
6876
6877
0
    if (!mbedtls_svc_key_id_is_null(master_key)) {
6878
0
        status = psa_destroy_key(master_key);
6879
0
    }
6880
0
    if (status != PSA_SUCCESS) {
6881
0
        return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6882
0
    }
6883
6884
0
    return 0;
6885
0
}
6886
#endif /* PSA_WANT_ALG_SHA_256 || PSA_WANT_ALG_SHA_384 */
6887
#else /* MBEDTLS_USE_PSA_CRYPTO */
6888
6889
#if defined(MBEDTLS_MD_C) &&       \
6890
    (defined(MBEDTLS_MD_CAN_SHA256) || \
6891
    defined(MBEDTLS_MD_CAN_SHA384))
6892
MBEDTLS_CHECK_RETURN_CRITICAL
6893
static int tls_prf_generic(mbedtls_md_type_t md_type,
6894
                           const unsigned char *secret, size_t slen,
6895
                           const char *label, size_t label_len,
6896
                           const unsigned char *random, size_t rlen,
6897
                           unsigned char *dstbuf, size_t dlen)
6898
3.34k
{
6899
3.34k
    size_t nb;
6900
3.34k
    size_t i, j, k, md_len;
6901
3.34k
    unsigned char *tmp;
6902
3.34k
    size_t tmp_len = 0;
6903
3.34k
    unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
6904
3.34k
    const mbedtls_md_info_t *md_info;
6905
3.34k
    mbedtls_md_context_t md_ctx;
6906
3.34k
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6907
6908
3.34k
    mbedtls_md_init(&md_ctx);
6909
6910
3.34k
    if ((md_info = mbedtls_md_info_from_type(md_type)) == NULL) {
6911
0
        return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
6912
0
    }
6913
6914
3.34k
    md_len = mbedtls_md_get_size(md_info);
6915
6916
3.34k
    tmp_len = md_len + label_len + rlen;
6917
3.34k
    tmp = mbedtls_calloc(1, tmp_len);
6918
3.34k
    if (tmp == NULL) {
6919
0
        ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6920
0
        goto exit;
6921
0
    }
6922
6923
3.34k
    nb = label_len;
6924
3.34k
    memcpy(tmp + md_len, label, nb);
6925
3.34k
    memcpy(tmp + md_len + nb, random, rlen);
6926
3.34k
    nb += rlen;
6927
6928
    /*
6929
     * Compute P_<hash>(secret, label + random)[0..dlen]
6930
     */
6931
3.34k
    if ((ret = mbedtls_md_setup(&md_ctx, md_info, 1)) != 0) {
6932
0
        goto exit;
6933
0
    }
6934
6935
3.34k
    ret = mbedtls_md_hmac_starts(&md_ctx, secret, slen);
6936
3.34k
    if (ret != 0) {
6937
0
        goto exit;
6938
0
    }
6939
3.34k
    ret = mbedtls_md_hmac_update(&md_ctx, tmp + md_len, nb);
6940
3.34k
    if (ret != 0) {
6941
0
        goto exit;
6942
0
    }
6943
3.34k
    ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
6944
3.34k
    if (ret != 0) {
6945
0
        goto exit;
6946
0
    }
6947
6948
14.0k
    for (i = 0; i < dlen; i += md_len) {
6949
10.7k
        ret = mbedtls_md_hmac_reset(&md_ctx);
6950
10.7k
        if (ret != 0) {
6951
0
            goto exit;
6952
0
        }
6953
10.7k
        ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len + nb);
6954
10.7k
        if (ret != 0) {
6955
0
            goto exit;
6956
0
        }
6957
10.7k
        ret = mbedtls_md_hmac_finish(&md_ctx, h_i);
6958
10.7k
        if (ret != 0) {
6959
0
            goto exit;
6960
0
        }
6961
6962
10.7k
        ret = mbedtls_md_hmac_reset(&md_ctx);
6963
10.7k
        if (ret != 0) {
6964
0
            goto exit;
6965
0
        }
6966
10.7k
        ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len);
6967
10.7k
        if (ret != 0) {
6968
0
            goto exit;
6969
0
        }
6970
10.7k
        ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
6971
10.7k
        if (ret != 0) {
6972
0
            goto exit;
6973
0
        }
6974
6975
10.7k
        k = (i + md_len > dlen) ? dlen % md_len : md_len;
6976
6977
324k
        for (j = 0; j < k; j++) {
6978
314k
            dstbuf[i + j]  = h_i[j];
6979
314k
        }
6980
10.7k
    }
6981
6982
3.34k
exit:
6983
3.34k
    mbedtls_md_free(&md_ctx);
6984
6985
3.34k
    if (tmp != NULL) {
6986
3.34k
        mbedtls_platform_zeroize(tmp, tmp_len);
6987
3.34k
    }
6988
6989
3.34k
    mbedtls_platform_zeroize(h_i, sizeof(h_i));
6990
6991
3.34k
    mbedtls_free(tmp);
6992
6993
3.34k
    return ret;
6994
3.34k
}
6995
#endif /* MBEDTLS_MD_C && ( MBEDTLS_MD_CAN_SHA256 || MBEDTLS_MD_CAN_SHA384 ) */
6996
#endif /* MBEDTLS_USE_PSA_CRYPTO */
6997
6998
#if defined(MBEDTLS_MD_CAN_SHA256)
6999
MBEDTLS_CHECK_RETURN_CRITICAL
7000
static int tls_prf_sha256(const unsigned char *secret, size_t slen,
7001
                          const char *label,
7002
                          const unsigned char *random, size_t rlen,
7003
                          unsigned char *dstbuf, size_t dlen)
7004
2.80k
{
7005
2.80k
    return tls_prf_generic(MBEDTLS_MD_SHA256, secret, slen,
7006
2.80k
                           label, strlen(label), random, rlen, dstbuf, dlen);
7007
2.80k
}
7008
#endif /* MBEDTLS_MD_CAN_SHA256*/
7009
7010
#if defined(MBEDTLS_MD_CAN_SHA384)
7011
MBEDTLS_CHECK_RETURN_CRITICAL
7012
static int tls_prf_sha384(const unsigned char *secret, size_t slen,
7013
                          const char *label,
7014
                          const unsigned char *random, size_t rlen,
7015
                          unsigned char *dstbuf, size_t dlen)
7016
535
{
7017
535
    return tls_prf_generic(MBEDTLS_MD_SHA384, secret, slen,
7018
535
                           label, strlen(label), random, rlen, dstbuf, dlen);
7019
535
}
7020
#endif /* MBEDTLS_MD_CAN_SHA384*/
7021
7022
/*
7023
 * Set appropriate PRF function and other SSL / TLS1.2 functions
7024
 *
7025
 * Inputs:
7026
 * - hash associated with the ciphersuite (only used by TLS 1.2)
7027
 *
7028
 * Outputs:
7029
 * - the tls_prf, calc_verify and calc_finished members of handshake structure
7030
 */
7031
MBEDTLS_CHECK_RETURN_CRITICAL
7032
static int ssl_set_handshake_prfs(mbedtls_ssl_handshake_params *handshake,
7033
                                  mbedtls_md_type_t hash)
7034
979
{
7035
979
#if defined(MBEDTLS_MD_CAN_SHA384)
7036
979
    if (hash == MBEDTLS_MD_SHA384) {
7037
156
        handshake->tls_prf = tls_prf_sha384;
7038
156
        handshake->calc_verify = ssl_calc_verify_tls_sha384;
7039
156
        handshake->calc_finished = ssl_calc_finished_tls_sha384;
7040
156
    } else
7041
823
#endif
7042
823
#if defined(MBEDTLS_MD_CAN_SHA256)
7043
823
    {
7044
823
        (void) hash;
7045
823
        handshake->tls_prf = tls_prf_sha256;
7046
823
        handshake->calc_verify = ssl_calc_verify_tls_sha256;
7047
823
        handshake->calc_finished = ssl_calc_finished_tls_sha256;
7048
823
    }
7049
#else
7050
    {
7051
        (void) handshake;
7052
        (void) hash;
7053
        return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7054
    }
7055
#endif
7056
7057
979
    return 0;
7058
979
}
7059
7060
/*
7061
 * Compute master secret if needed
7062
 *
7063
 * Parameters:
7064
 * [in/out] handshake
7065
 *          [in] resume, premaster, extended_ms, calc_verify, tls_prf
7066
 *               (PSA-PSK) ciphersuite_info, psk_opaque
7067
 *          [out] premaster (cleared)
7068
 * [out] master
7069
 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
7070
 *      debug: conf->f_dbg, conf->p_dbg
7071
 *      EMS: passed to calc_verify (debug + session_negotiate)
7072
 *      PSA-PSA: conf
7073
 */
7074
MBEDTLS_CHECK_RETURN_CRITICAL
7075
static int ssl_compute_master(mbedtls_ssl_handshake_params *handshake,
7076
                              unsigned char *master,
7077
                              const mbedtls_ssl_context *ssl)
7078
979
{
7079
979
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7080
7081
    /* cf. RFC 5246, Section 8.1:
7082
     * "The master secret is always exactly 48 bytes in length." */
7083
979
    size_t const master_secret_len = 48;
7084
7085
979
#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
7086
979
    unsigned char session_hash[48];
7087
979
#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
7088
7089
    /* The label for the KDF used for key expansion.
7090
     * This is either "master secret" or "extended master secret"
7091
     * depending on whether the Extended Master Secret extension
7092
     * is used. */
7093
979
    char const *lbl = "master secret";
7094
7095
    /* The seed for the KDF used for key expansion.
7096
     * - If the Extended Master Secret extension is not used,
7097
     *   this is ClientHello.Random + ServerHello.Random
7098
     *   (see Sect. 8.1 in RFC 5246).
7099
     * - If the Extended Master Secret extension is used,
7100
     *   this is the transcript of the handshake so far.
7101
     *   (see Sect. 4 in RFC 7627). */
7102
979
    unsigned char const *seed = handshake->randbytes;
7103
979
    size_t seed_len = 64;
7104
7105
#if !defined(MBEDTLS_DEBUG_C) &&                    \
7106
    !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
7107
    !(defined(MBEDTLS_USE_PSA_CRYPTO) &&            \
7108
    defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
7109
    ssl = NULL; /* make sure we don't use it except for those cases */
7110
    (void) ssl;
7111
#endif
7112
7113
979
    if (handshake->resume != 0) {
7114
0
        MBEDTLS_SSL_DEBUG_MSG(3, ("no premaster (session resumed)"));
7115
0
        return 0;
7116
0
    }
7117
7118
979
#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
7119
979
    if (handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED) {
7120
112
        lbl  = "extended master secret";
7121
112
        ret = handshake->calc_verify(ssl, session_hash, &seed_len);
7122
112
        if (ret != 0) {
7123
0
            MBEDTLS_SSL_DEBUG_RET(1, "calc_verify", ret);
7124
0
            return ret;
7125
0
        }
7126
112
        if (seed_len > sizeof(session_hash)) {
7127
0
            MBEDTLS_SSL_DEBUG_MSG(1, ("bad session hash length"));
7128
0
            return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7129
0
        }
7130
112
        seed = session_hash;
7131
7132
112
        MBEDTLS_SSL_DEBUG_BUF(3, "session hash for extended master secret",
7133
112
                              session_hash, seed_len);
7134
112
    }
7135
0
#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
7136
7137
#if defined(MBEDTLS_USE_PSA_CRYPTO) &&                   \
7138
    defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
7139
0
    if (mbedtls_ssl_ciphersuite_uses_psk(handshake->ciphersuite_info) == 1) {
7140
        /* Perform PSK-to-MS expansion in a single step. */
7141
0
        psa_status_t status;
7142
0
        psa_algorithm_t alg;
7143
0
        mbedtls_svc_key_id_t psk;
7144
0
        psa_key_derivation_operation_t derivation =
7145
0
            PSA_KEY_DERIVATION_OPERATION_INIT;
7146
0
        mbedtls_md_type_t hash_alg = (mbedtls_md_type_t) handshake->ciphersuite_info->mac;
7147
7148
0
        MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PSK-to-MS expansion"));
7149
7150
        psk = mbedtls_ssl_get_opaque_psk(ssl);
7151
7152
0
        if (hash_alg == MBEDTLS_MD_SHA384) {
7153
0
            alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
7154
0
        } else {
7155
0
            alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
7156
0
        }
7157
7158
        size_t other_secret_len = 0;
7159
        unsigned char *other_secret = NULL;
7160
7161
        switch (handshake->ciphersuite_info->key_exchange) {
7162
            /* Provide other secret.
7163
             * Other secret is stored in premaster, where first 2 bytes hold the
7164
             * length of the other key.
7165
             */
7166
0
            case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
7167
                /* For RSA-PSK other key length is always 48 bytes. */
7168
0
                other_secret_len = 48;
7169
0
                other_secret = handshake->premaster + 2;
7170
0
                break;
7171
0
            case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
7172
0
            case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
7173
0
                other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
7174
0
                other_secret = handshake->premaster + 2;
7175
0
                break;
7176
0
            default:
7177
0
                break;
7178
        }
7179
7180
0
        status = setup_psa_key_derivation(&derivation, psk, alg,
7181
0
                                          ssl->conf->psk, ssl->conf->psk_len,
7182
0
                                          seed, seed_len,
7183
0
                                          (unsigned char const *) lbl,
7184
0
                                          (size_t) strlen(lbl),
7185
0
                                          other_secret, other_secret_len,
7186
0
                                          master_secret_len);
7187
0
        if (status != PSA_SUCCESS) {
7188
0
            psa_key_derivation_abort(&derivation);
7189
0
            return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
7190
0
        }
7191
7192
0
        status = psa_key_derivation_output_bytes(&derivation,
7193
0
                                                 master,
7194
0
                                                 master_secret_len);
7195
0
        if (status != PSA_SUCCESS) {
7196
0
            psa_key_derivation_abort(&derivation);
7197
0
            return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
7198
0
        }
7199
7200
0
        status = psa_key_derivation_abort(&derivation);
7201
0
        if (status != PSA_SUCCESS) {
7202
0
            return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
7203
0
        }
7204
0
    } else
7205
0
#endif
7206
0
    {
7207
#if defined(MBEDTLS_USE_PSA_CRYPTO) &&                              \
7208
        defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
7209
0
        if (handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
7210
0
            psa_status_t status;
7211
0
            psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
7212
0
            psa_key_derivation_operation_t derivation =
7213
0
                PSA_KEY_DERIVATION_OPERATION_INIT;
7214
7215
0
            MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PMS KDF for ECJPAKE"));
7216
7217
0
            handshake->pmslen = PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE;
7218
7219
            status = psa_key_derivation_setup(&derivation, alg);
7220
0
            if (status != PSA_SUCCESS) {
7221
0
                return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
7222
0
            }
7223
7224
0
            status = psa_key_derivation_set_capacity(&derivation,
7225
0
                                                     PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
7226
0
            if (status != PSA_SUCCESS) {
7227
0
                psa_key_derivation_abort(&derivation);
7228
0
                return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
7229
0
            }
7230
7231
0
            status = psa_pake_get_implicit_key(&handshake->psa_pake_ctx,
7232
0
                                               &derivation);
7233
0
            if (status != PSA_SUCCESS) {
7234
0
                psa_key_derivation_abort(&derivation);
7235
0
                return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
7236
0
            }
7237
7238
0
            status = psa_key_derivation_output_bytes(&derivation,
7239
0
                                                     handshake->premaster,
7240
0
                                                     handshake->pmslen);
7241
0
            if (status != PSA_SUCCESS) {
7242
0
                psa_key_derivation_abort(&derivation);
7243
0
                return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
7244
0
            }
7245
7246
0
            status = psa_key_derivation_abort(&derivation);
7247
0
            if (status != PSA_SUCCESS) {
7248
0
                return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
7249
0
            }
7250
0
        }
7251
0
#endif
7252
0
        ret = handshake->tls_prf(handshake->premaster, handshake->pmslen,
7253
0
                                 lbl, seed, seed_len,
7254
0
                                 master,
7255
0
                                 master_secret_len);
7256
979
        if (ret != 0) {
7257
0
            MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
7258
0
            return ret;
7259
0
        }
7260
7261
979
        MBEDTLS_SSL_DEBUG_BUF(3, "premaster secret",
7262
0
                              handshake->premaster,
7263
0
                              handshake->pmslen);
7264
7265
0
        mbedtls_platform_zeroize(handshake->premaster,
7266
0
                                 sizeof(handshake->premaster));
7267
0
    }
7268
7269
0
    return 0;
7270
979
}
ssl_tls.c:ssl_compute_master
Line
Count
Source
7078
979
{
7079
979
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7080
7081
    /* cf. RFC 5246, Section 8.1:
7082
     * "The master secret is always exactly 48 bytes in length." */
7083
979
    size_t const master_secret_len = 48;
7084
7085
979
#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
7086
979
    unsigned char session_hash[48];
7087
979
#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
7088
7089
    /* The label for the KDF used for key expansion.
7090
     * This is either "master secret" or "extended master secret"
7091
     * depending on whether the Extended Master Secret extension
7092
     * is used. */
7093
979
    char const *lbl = "master secret";
7094
7095
    /* The seed for the KDF used for key expansion.
7096
     * - If the Extended Master Secret extension is not used,
7097
     *   this is ClientHello.Random + ServerHello.Random
7098
     *   (see Sect. 8.1 in RFC 5246).
7099
     * - If the Extended Master Secret extension is used,
7100
     *   this is the transcript of the handshake so far.
7101
     *   (see Sect. 4 in RFC 7627). */
7102
979
    unsigned char const *seed = handshake->randbytes;
7103
979
    size_t seed_len = 64;
7104
7105
#if !defined(MBEDTLS_DEBUG_C) &&                    \
7106
    !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
7107
    !(defined(MBEDTLS_USE_PSA_CRYPTO) &&            \
7108
    defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
7109
    ssl = NULL; /* make sure we don't use it except for those cases */
7110
    (void) ssl;
7111
#endif
7112
7113
979
    if (handshake->resume != 0) {
7114
0
        MBEDTLS_SSL_DEBUG_MSG(3, ("no premaster (session resumed)"));
7115
0
        return 0;
7116
0
    }
7117
7118
979
#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
7119
979
    if (handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED) {
7120
112
        lbl  = "extended master secret";
7121
112
        ret = handshake->calc_verify(ssl, session_hash, &seed_len);
7122
112
        if (ret != 0) {
7123
0
            MBEDTLS_SSL_DEBUG_RET(1, "calc_verify", ret);
7124
0
            return ret;
7125
0
        }
7126
112
        if (seed_len > sizeof(session_hash)) {
7127
0
            MBEDTLS_SSL_DEBUG_MSG(1, ("bad session hash length"));
7128
0
            return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7129
0
        }
7130
112
        seed = session_hash;
7131
7132
112
        MBEDTLS_SSL_DEBUG_BUF(3, "session hash for extended master secret",
7133
112
                              session_hash, seed_len);
7134
112
    }
7135
979
#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
7136
7137
#if defined(MBEDTLS_USE_PSA_CRYPTO) &&                   \
7138
    defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
7139
    if (mbedtls_ssl_ciphersuite_uses_psk(handshake->ciphersuite_info) == 1) {
7140
        /* Perform PSK-to-MS expansion in a single step. */
7141
        psa_status_t status;
7142
        psa_algorithm_t alg;
7143
        mbedtls_svc_key_id_t psk;
7144
        psa_key_derivation_operation_t derivation =
7145
            PSA_KEY_DERIVATION_OPERATION_INIT;
7146
        mbedtls_md_type_t hash_alg = (mbedtls_md_type_t) handshake->ciphersuite_info->mac;
7147
7148
        MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PSK-to-MS expansion"));
7149
7150
        psk = mbedtls_ssl_get_opaque_psk(ssl);
7151
7152
        if (hash_alg == MBEDTLS_MD_SHA384) {
7153
            alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
7154
        } else {
7155
            alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
7156
        }
7157
7158
        size_t other_secret_len = 0;
7159
        unsigned char *other_secret = NULL;
7160
7161
        switch (handshake->ciphersuite_info->key_exchange) {
7162
            /* Provide other secret.
7163
             * Other secret is stored in premaster, where first 2 bytes hold the
7164
             * length of the other key.
7165
             */
7166
            case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
7167
                /* For RSA-PSK other key length is always 48 bytes. */
7168
                other_secret_len = 48;
7169
                other_secret = handshake->premaster + 2;
7170
                break;
7171
            case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
7172
            case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
7173
                other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
7174
                other_secret = handshake->premaster + 2;
7175
                break;
7176
            default:
7177
                break;
7178
        }
7179
7180
        status = setup_psa_key_derivation(&derivation, psk, alg,
7181
                                          ssl->conf->psk, ssl->conf->psk_len,
7182
                                          seed, seed_len,
7183
                                          (unsigned char const *) lbl,
7184
                                          (size_t) strlen(lbl),
7185
                                          other_secret, other_secret_len,
7186
                                          master_secret_len);
7187
        if (status != PSA_SUCCESS) {
7188
            psa_key_derivation_abort(&derivation);
7189
            return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
7190
        }
7191
7192
        status = psa_key_derivation_output_bytes(&derivation,
7193
                                                 master,
7194
                                                 master_secret_len);
7195
        if (status != PSA_SUCCESS) {
7196
            psa_key_derivation_abort(&derivation);
7197
            return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
7198
        }
7199
7200
        status = psa_key_derivation_abort(&derivation);
7201
        if (status != PSA_SUCCESS) {
7202
            return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
7203
        }
7204
    } else
7205
#endif
7206
979
    {
7207
#if defined(MBEDTLS_USE_PSA_CRYPTO) &&                              \
7208
        defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
7209
        if (handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
7210
            psa_status_t status;
7211
            psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
7212
            psa_key_derivation_operation_t derivation =
7213
                PSA_KEY_DERIVATION_OPERATION_INIT;
7214
7215
            MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PMS KDF for ECJPAKE"));
7216
7217
            handshake->pmslen = PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE;
7218
7219
            status = psa_key_derivation_setup(&derivation, alg);
7220
            if (status != PSA_SUCCESS) {
7221
                return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
7222
            }
7223
7224
            status = psa_key_derivation_set_capacity(&derivation,
7225
                                                     PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
7226
            if (status != PSA_SUCCESS) {
7227
                psa_key_derivation_abort(&derivation);
7228
                return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
7229
            }
7230
7231
            status = psa_pake_get_implicit_key(&handshake->psa_pake_ctx,
7232
                                               &derivation);
7233
            if (status != PSA_SUCCESS) {
7234
                psa_key_derivation_abort(&derivation);
7235
                return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
7236
            }
7237
7238
            status = psa_key_derivation_output_bytes(&derivation,
7239
                                                     handshake->premaster,
7240
                                                     handshake->pmslen);
7241
            if (status != PSA_SUCCESS) {
7242
                psa_key_derivation_abort(&derivation);
7243
                return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
7244
            }
7245
7246
            status = psa_key_derivation_abort(&derivation);
7247
            if (status != PSA_SUCCESS) {
7248
                return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
7249
            }
7250
        }
7251
#endif
7252
979
        ret = handshake->tls_prf(handshake->premaster, handshake->pmslen,
7253
979
                                 lbl, seed, seed_len,
7254
979
                                 master,
7255
979
                                 master_secret_len);
7256
979
        if (ret != 0) {
7257
0
            MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
7258
0
            return ret;
7259
0
        }
7260
7261
979
        MBEDTLS_SSL_DEBUG_BUF(3, "premaster secret",
7262
979
                              handshake->premaster,
7263
979
                              handshake->pmslen);
7264
7265
979
        mbedtls_platform_zeroize(handshake->premaster,
7266
979
                                 sizeof(handshake->premaster));
7267
979
    }
7268
7269
0
    return 0;
7270
979
}
Unexecuted instantiation: ssl_tls.c:ssl_compute_master
7271
7272
int mbedtls_ssl_derive_keys(mbedtls_ssl_context *ssl)
7273
979
{
7274
979
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7275
979
    const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
7276
979
        ssl->handshake->ciphersuite_info;
7277
7278
979
    MBEDTLS_SSL_DEBUG_MSG(2, ("=> derive keys"));
7279
7280
    /* Set PRF, calc_verify and calc_finished function pointers */
7281
979
    ret = ssl_set_handshake_prfs(ssl->handshake,
7282
979
                                 (mbedtls_md_type_t) ciphersuite_info->mac);
7283
979
    if (ret != 0) {
7284
0
        MBEDTLS_SSL_DEBUG_RET(1, "ssl_set_handshake_prfs", ret);
7285
0
        return ret;
7286
0
    }
7287
7288
    /* Compute master secret if needed */
7289
979
    ret = ssl_compute_master(ssl->handshake,
7290
979
                             ssl->session_negotiate->master,
7291
979
                             ssl);
7292
979
    if (ret != 0) {
7293
0
        MBEDTLS_SSL_DEBUG_RET(1, "ssl_compute_master", ret);
7294
0
        return ret;
7295
0
    }
7296
7297
    /* Swap the client and server random values:
7298
     * - MS derivation wanted client+server (RFC 5246 8.1)
7299
     * - key derivation wants server+client (RFC 5246 6.3) */
7300
979
    {
7301
979
        unsigned char tmp[64];
7302
979
        memcpy(tmp, ssl->handshake->randbytes, 64);
7303
979
        memcpy(ssl->handshake->randbytes, tmp + 32, 32);
7304
979
        memcpy(ssl->handshake->randbytes + 32, tmp, 32);
7305
979
        mbedtls_platform_zeroize(tmp, sizeof(tmp));
7306
979
    }
7307
7308
    /* Populate transform structure */
7309
979
    ret = ssl_tls12_populate_transform(ssl->transform_negotiate,
7310
979
                                       ssl->session_negotiate->ciphersuite,
7311
979
                                       ssl->session_negotiate->master,
7312
979
#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
7313
979
                                       ssl->session_negotiate->encrypt_then_mac,
7314
979
#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
7315
979
                                       ssl->handshake->tls_prf,
7316
979
                                       ssl->handshake->randbytes,
7317
979
                                       ssl->tls_version,
7318
979
                                       ssl->conf->endpoint,
7319
979
                                       ssl);
7320
979
    if (ret != 0) {
7321
0
        MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls12_populate_transform", ret);
7322
0
        return ret;
7323
0
    }
7324
7325
    /* We no longer need Server/ClientHello.random values */
7326
979
    mbedtls_platform_zeroize(ssl->handshake->randbytes,
7327
979
                             sizeof(ssl->handshake->randbytes));
7328
7329
979
    MBEDTLS_SSL_DEBUG_MSG(2, ("<= derive keys"));
7330
7331
979
    return 0;
7332
979
}
7333
7334
int mbedtls_ssl_set_calc_verify_md(mbedtls_ssl_context *ssl, int md)
7335
0
{
7336
0
    switch (md) {
7337
0
#if defined(MBEDTLS_MD_CAN_SHA384)
7338
0
        case MBEDTLS_SSL_HASH_SHA384:
7339
0
            ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
7340
0
            break;
7341
0
#endif
7342
0
#if defined(MBEDTLS_MD_CAN_SHA256)
7343
0
        case MBEDTLS_SSL_HASH_SHA256:
7344
0
            ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
7345
0
            break;
7346
0
#endif
7347
0
        default:
7348
0
            return -1;
7349
0
    }
7350
#if !defined(MBEDTLS_MD_CAN_SHA384) && \
7351
    !defined(MBEDTLS_MD_CAN_SHA256)
7352
    (void) ssl;
7353
#endif
7354
0
    return 0;
7355
0
}
7356
7357
#if defined(MBEDTLS_USE_PSA_CRYPTO)
7358
static int ssl_calc_verify_tls_psa(const mbedtls_ssl_context *ssl,
7359
                                   const psa_hash_operation_t *hs_op,
7360
                                   size_t buffer_size,
7361
                                   unsigned char *hash,
7362
                                   size_t *hlen)
7363
0
{
7364
0
    psa_status_t status;
7365
0
    psa_hash_operation_t cloned_op = psa_hash_operation_init();
7366
7367
#if !defined(MBEDTLS_DEBUG_C)
7368
    (void) ssl;
7369
#endif
7370
0
    MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify"));
7371
0
    status = psa_hash_clone(hs_op, &cloned_op);
7372
0
    if (status != PSA_SUCCESS) {
7373
0
        goto exit;
7374
0
    }
7375
7376
0
    status = psa_hash_finish(&cloned_op, hash, buffer_size, hlen);
7377
0
    if (status != PSA_SUCCESS) {
7378
0
        goto exit;
7379
0
    }
7380
7381
0
    MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen);
7382
0
    MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify"));
7383
7384
0
exit:
7385
0
    psa_hash_abort(&cloned_op);
7386
0
    return PSA_TO_MBEDTLS_ERR(status);
7387
0
}
7388
#else
7389
static int ssl_calc_verify_tls_legacy(const mbedtls_ssl_context *ssl,
7390
                                      const mbedtls_md_context_t *hs_ctx,
7391
                                      unsigned char *hash,
7392
                                      size_t *hlen)
7393
112
{
7394
112
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7395
112
    mbedtls_md_context_t cloned_ctx;
7396
7397
112
    mbedtls_md_init(&cloned_ctx);
7398
7399
#if !defined(MBEDTLS_DEBUG_C)
7400
    (void) ssl;
7401
#endif
7402
112
    MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify"));
7403
7404
112
    ret = mbedtls_md_setup(&cloned_ctx, mbedtls_md_info_from_ctx(hs_ctx), 0);
7405
112
    if (ret != 0) {
7406
0
        goto exit;
7407
0
    }
7408
112
    ret = mbedtls_md_clone(&cloned_ctx, hs_ctx);
7409
112
    if (ret != 0) {
7410
0
        goto exit;
7411
0
    }
7412
7413
112
    ret = mbedtls_md_finish(&cloned_ctx, hash);
7414
112
    if (ret != 0) {
7415
0
        goto exit;
7416
0
    }
7417
7418
112
    *hlen = mbedtls_md_get_size(mbedtls_md_info_from_ctx(hs_ctx));
7419
7420
112
    MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
7421
112
    MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
7422
7423
112
exit:
7424
112
    mbedtls_md_free(&cloned_ctx);
7425
112
    return ret;
7426
112
}
7427
#endif /* MBEDTLS_USE_PSA_CRYPTO */
7428
7429
#if defined(MBEDTLS_MD_CAN_SHA256)
7430
int ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *ssl,
7431
                               unsigned char *hash,
7432
                               size_t *hlen)
7433
81
{
7434
#if defined(MBEDTLS_USE_PSA_CRYPTO)
7435
    return ssl_calc_verify_tls_psa(ssl, &ssl->handshake->fin_sha256_psa, 32,
7436
                                   hash, hlen);
7437
#else
7438
81
    return ssl_calc_verify_tls_legacy(ssl, &ssl->handshake->fin_sha256,
7439
81
                                      hash, hlen);
7440
81
#endif /* MBEDTLS_USE_PSA_CRYPTO */
7441
81
}
7442
#endif /* MBEDTLS_MD_CAN_SHA256 */
7443
7444
#if defined(MBEDTLS_MD_CAN_SHA384)
7445
int ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *ssl,
7446
                               unsigned char *hash,
7447
                               size_t *hlen)
7448
31
{
7449
#if defined(MBEDTLS_USE_PSA_CRYPTO)
7450
    return ssl_calc_verify_tls_psa(ssl, &ssl->handshake->fin_sha384_psa, 48,
7451
                                   hash, hlen);
7452
#else
7453
31
    return ssl_calc_verify_tls_legacy(ssl, &ssl->handshake->fin_sha384,
7454
31
                                      hash, hlen);
7455
31
#endif /* MBEDTLS_USE_PSA_CRYPTO */
7456
31
}
7457
#endif /* MBEDTLS_MD_CAN_SHA384 */
7458
7459
#if !defined(MBEDTLS_USE_PSA_CRYPTO) &&                      \
7460
    defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
7461
int mbedtls_ssl_psk_derive_premaster(mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex)
7462
0
{
7463
0
    unsigned char *p = ssl->handshake->premaster;
7464
0
    unsigned char *end = p + sizeof(ssl->handshake->premaster);
7465
0
    const unsigned char *psk = NULL;
7466
0
    size_t psk_len = 0;
7467
0
    int psk_ret = mbedtls_ssl_get_psk(ssl, &psk, &psk_len);
7468
7469
0
    if (psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED) {
7470
        /*
7471
         * This should never happen because the existence of a PSK is always
7472
         * checked before calling this function.
7473
         *
7474
         * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
7475
         * the shared secret without PSK.
7476
         */
7477
0
        if (key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
7478
0
            MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
7479
0
            return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7480
0
        }
7481
0
    }
7482
7483
    /*
7484
     * PMS = struct {
7485
     *     opaque other_secret<0..2^16-1>;
7486
     *     opaque psk<0..2^16-1>;
7487
     * };
7488
     * with "other_secret" depending on the particular key exchange
7489
     */
7490
0
#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
7491
0
    if (key_ex == MBEDTLS_KEY_EXCHANGE_PSK) {
7492
0
        if (end - p < 2) {
7493
0
            return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
7494
0
        }
7495
7496
0
        MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
7497
0
        p += 2;
7498
7499
0
        if (end < p || (size_t) (end - p) < psk_len) {
7500
0
            return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
7501
0
        }
7502
7503
0
        memset(p, 0, psk_len);
7504
0
        p += psk_len;
7505
0
    } else
7506
0
#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
7507
0
#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
7508
0
    if (key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
7509
        /*
7510
         * other_secret already set by the ClientKeyExchange message,
7511
         * and is 48 bytes long
7512
         */
7513
0
        if (end - p < 2) {
7514
0
            return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
7515
0
        }
7516
7517
0
        *p++ = 0;
7518
0
        *p++ = 48;
7519
0
        p += 48;
7520
0
    } else
7521
0
#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
7522
0
#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
7523
0
    if (key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
7524
0
        int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7525
0
        size_t len;
7526
7527
        /* Write length only when we know the actual value */
7528
0
        if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
7529
0
                                           p + 2, (size_t) (end - (p + 2)), &len,
7530
0
                                           ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
7531
0
            MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
7532
0
            return ret;
7533
0
        }
7534
0
        MBEDTLS_PUT_UINT16_BE(len, p, 0);
7535
0
        p += 2 + len;
7536
7537
0
        MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
7538
0
    } else
7539
0
#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
7540
0
#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
7541
0
    if (key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
7542
0
        int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7543
0
        size_t zlen;
7544
7545
0
        if ((ret = mbedtls_ecdh_calc_secret(&ssl->handshake->ecdh_ctx, &zlen,
7546
0
                                            p + 2, (size_t) (end - (p + 2)),
7547
0
                                            ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
7548
0
            MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret);
7549
0
            return ret;
7550
0
        }
7551
7552
0
        MBEDTLS_PUT_UINT16_BE(zlen, p, 0);
7553
0
        p += 2 + zlen;
7554
7555
0
        MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
7556
0
                               MBEDTLS_DEBUG_ECDH_Z);
7557
0
    } else
7558
0
#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
7559
0
    {
7560
0
        MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
7561
0
        return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7562
0
    }
7563
7564
    /* opaque psk<0..2^16-1>; */
7565
0
    if (end - p < 2) {
7566
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
7567
0
    }
7568
7569
0
    MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
7570
0
    p += 2;
7571
7572
0
    if (end < p || (size_t) (end - p) < psk_len) {
7573
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
7574
0
    }
7575
7576
0
    memcpy(p, psk, psk_len);
7577
0
    p += psk_len;
7578
7579
0
    ssl->handshake->pmslen = (size_t) (p - ssl->handshake->premaster);
7580
7581
0
    return 0;
7582
0
}
7583
#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
7584
7585
#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
7586
MBEDTLS_CHECK_RETURN_CRITICAL
7587
static int ssl_write_hello_request(mbedtls_ssl_context *ssl);
7588
7589
#if defined(MBEDTLS_SSL_PROTO_DTLS)
7590
int mbedtls_ssl_resend_hello_request(mbedtls_ssl_context *ssl)
7591
0
{
7592
    /* If renegotiation is not enforced, retransmit until we would reach max
7593
     * timeout if we were using the usual handshake doubling scheme */
7594
0
    if (ssl->conf->renego_max_records < 0) {
7595
0
        uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
7596
0
        unsigned char doublings = 1;
7597
7598
0
        while (ratio != 0) {
7599
0
            ++doublings;
7600
0
            ratio >>= 1;
7601
0
        }
7602
7603
0
        if (++ssl->renego_records_seen > doublings) {
7604
0
            MBEDTLS_SSL_DEBUG_MSG(2, ("no longer retransmitting hello request"));
7605
0
            return 0;
7606
0
        }
7607
0
    }
7608
7609
0
    return ssl_write_hello_request(ssl);
7610
0
}
7611
#endif
7612
#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
7613
7614
/*
7615
 * Handshake functions
7616
 */
7617
#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
7618
/* No certificate support -> dummy functions */
7619
int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
7620
{
7621
    const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7622
        ssl->handshake->ciphersuite_info;
7623
7624
    MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
7625
7626
    if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
7627
        MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
7628
        mbedtls_ssl_handshake_increment_state(ssl);
7629
        return 0;
7630
    }
7631
7632
    MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
7633
    return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7634
}
7635
7636
int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
7637
{
7638
    const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7639
        ssl->handshake->ciphersuite_info;
7640
7641
    MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
7642
7643
    if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
7644
        MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
7645
        mbedtls_ssl_handshake_increment_state(ssl);
7646
        return 0;
7647
    }
7648
7649
    MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
7650
    return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7651
}
7652
7653
#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
7654
/* Some certificate support -> implement write and parse */
7655
7656
int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
7657
1.25k
{
7658
1.25k
    int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
7659
1.25k
    size_t i, n;
7660
1.25k
    const mbedtls_x509_crt *crt;
7661
1.25k
    const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7662
1.25k
        ssl->handshake->ciphersuite_info;
7663
7664
1.25k
    MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
7665
7666
1.25k
    if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
7667
277
        MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
7668
277
        mbedtls_ssl_handshake_increment_state(ssl);
7669
277
        return 0;
7670
277
    }
7671
7672
982
#if defined(MBEDTLS_SSL_CLI_C)
7673
982
    if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
7674
982
        if (ssl->handshake->client_auth == 0) {
7675
980
            MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
7676
980
            mbedtls_ssl_handshake_increment_state(ssl);
7677
980
            return 0;
7678
980
        }
7679
982
    }
7680
2
#endif /* MBEDTLS_SSL_CLI_C */
7681
2
#if defined(MBEDTLS_SSL_SRV_C)
7682
2
    if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
7683
0
        if (mbedtls_ssl_own_cert(ssl) == NULL) {
7684
            /* Should never happen because we shouldn't have picked the
7685
             * ciphersuite if we don't have a certificate. */
7686
0
            return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7687
0
        }
7688
0
    }
7689
2
#endif
7690
7691
2
    MBEDTLS_SSL_DEBUG_CRT(3, "own certificate", mbedtls_ssl_own_cert(ssl));
7692
7693
    /*
7694
     *     0  .  0    handshake type
7695
     *     1  .  3    handshake length
7696
     *     4  .  6    length of all certs
7697
     *     7  .  9    length of cert. 1
7698
     *    10  . n-1   peer certificate
7699
     *     n  . n+2   length of cert. 2
7700
     *    n+3 . ...   upper level cert, etc.
7701
     */
7702
2
    i = 7;
7703
2
    crt = mbedtls_ssl_own_cert(ssl);
7704
7705
2
    while (crt != NULL) {
7706
0
        n = crt->raw.len;
7707
0
        if (n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i) {
7708
0
            MBEDTLS_SSL_DEBUG_MSG(1, ("certificate too large, %" MBEDTLS_PRINTF_SIZET
7709
0
                                      " > %" MBEDTLS_PRINTF_SIZET,
7710
0
                                      i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN));
7711
0
            return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
7712
0
        }
7713
7714
0
        ssl->out_msg[i] = MBEDTLS_BYTE_2(n);
7715
0
        ssl->out_msg[i + 1] = MBEDTLS_BYTE_1(n);
7716
0
        ssl->out_msg[i + 2] = MBEDTLS_BYTE_0(n);
7717
7718
0
        i += 3; memcpy(ssl->out_msg + i, crt->raw.p, n);
7719
0
        i += n; crt = crt->next;
7720
0
    }
7721
7722
2
    ssl->out_msg[4]  = MBEDTLS_BYTE_2(i - 7);
7723
2
    ssl->out_msg[5]  = MBEDTLS_BYTE_1(i - 7);
7724
2
    ssl->out_msg[6]  = MBEDTLS_BYTE_0(i - 7);
7725
7726
2
    ssl->out_msglen  = i;
7727
2
    ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7728
2
    ssl->out_msg[0]  = MBEDTLS_SSL_HS_CERTIFICATE;
7729
7730
2
    mbedtls_ssl_handshake_increment_state(ssl);
7731
7732
2
    if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
7733
0
        MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
7734
0
        return ret;
7735
0
    }
7736
7737
2
    MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate"));
7738
7739
2
    return ret;
7740
2
}
7741
7742
#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
7743
7744
#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7745
MBEDTLS_CHECK_RETURN_CRITICAL
7746
static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
7747
                                        unsigned char *crt_buf,
7748
                                        size_t crt_buf_len)
7749
0
{
7750
0
    mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
7751
7752
0
    if (peer_crt == NULL) {
7753
0
        return -1;
7754
0
    }
7755
7756
0
    if (peer_crt->raw.len != crt_buf_len) {
7757
0
        return -1;
7758
0
    }
7759
7760
0
    return memcmp(peer_crt->raw.p, crt_buf, peer_crt->raw.len);
7761
0
}
7762
#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7763
MBEDTLS_CHECK_RETURN_CRITICAL
7764
static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
7765
                                        unsigned char *crt_buf,
7766
                                        size_t crt_buf_len)
7767
{
7768
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7769
    unsigned char const * const peer_cert_digest =
7770
        ssl->session->peer_cert_digest;
7771
    mbedtls_md_type_t const peer_cert_digest_type =
7772
        ssl->session->peer_cert_digest_type;
7773
    mbedtls_md_info_t const * const digest_info =
7774
        mbedtls_md_info_from_type(peer_cert_digest_type);
7775
    unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
7776
    size_t digest_len;
7777
7778
    if (peer_cert_digest == NULL || digest_info == NULL) {
7779
        return -1;
7780
    }
7781
7782
    digest_len = mbedtls_md_get_size(digest_info);
7783
    if (digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN) {
7784
        return -1;
7785
    }
7786
7787
    ret = mbedtls_md(digest_info, crt_buf, crt_buf_len, tmp_digest);
7788
    if (ret != 0) {
7789
        return -1;
7790
    }
7791
7792
    return memcmp(tmp_digest, peer_cert_digest, digest_len);
7793
}
7794
#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7795
#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7796
7797
/*
7798
 * Once the certificate message is read, parse it into a cert chain and
7799
 * perform basic checks, but leave actual verification to the caller
7800
 */
7801
MBEDTLS_CHECK_RETURN_CRITICAL
7802
static int ssl_parse_certificate_chain(mbedtls_ssl_context *ssl,
7803
                                       mbedtls_x509_crt *chain)
7804
3.86k
{
7805
3.86k
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7806
3.86k
#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
7807
3.86k
    int crt_cnt = 0;
7808
3.86k
#endif
7809
3.86k
    size_t i, n;
7810
3.86k
    uint8_t alert;
7811
7812
3.86k
    if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
7813
1
        MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7814
1
        mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7815
1
                                       MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
7816
1
        return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7817
1
    }
7818
7819
3.86k
    if (ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE) {
7820
12
        mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7821
12
                                       MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
7822
12
        return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7823
12
    }
7824
7825
3.85k
    if (ssl->in_hslen < mbedtls_ssl_hs_hdr_len(ssl) + 3 + 3) {
7826
2
        MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7827
2
        mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7828
2
                                       MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7829
2
        return MBEDTLS_ERR_SSL_DECODE_ERROR;
7830
2
    }
7831
7832
3.85k
    i = mbedtls_ssl_hs_hdr_len(ssl);
7833
7834
    /*
7835
     * Same message structure as in mbedtls_ssl_write_certificate()
7836
     */
7837
3.85k
    n = MBEDTLS_GET_UINT16_BE(ssl->in_msg, i + 1);
7838
7839
3.85k
    if (ssl->in_msg[i] != 0 ||
7840
3.84k
        ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len(ssl)) {
7841
29
        MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7842
29
        mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7843
29
                                       MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7844
29
        return MBEDTLS_ERR_SSL_DECODE_ERROR;
7845
29
    }
7846
7847
    /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
7848
3.82k
    i += 3;
7849
7850
    /* Iterate through and parse the CRTs in the provided chain. */
7851
6.26k
    while (i < ssl->in_hslen) {
7852
        /* Check that there's room for the next CRT's length fields. */
7853
4.23k
        if (i + 3 > ssl->in_hslen) {
7854
2
            MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7855
2
            mbedtls_ssl_send_alert_message(ssl,
7856
2
                                           MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7857
2
                                           MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7858
2
            return MBEDTLS_ERR_SSL_DECODE_ERROR;
7859
2
        }
7860
        /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
7861
         * anything beyond 2**16 ~ 64K. */
7862
4.23k
        if (ssl->in_msg[i] != 0) {
7863
13
            MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7864
13
            mbedtls_ssl_send_alert_message(ssl,
7865
13
                                           MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7866
13
                                           MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT);
7867
13
            return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
7868
13
        }
7869
7870
        /* Read length of the next CRT in the chain. */
7871
4.22k
        n = MBEDTLS_GET_UINT16_BE(ssl->in_msg, i + 1);
7872
4.22k
        i += 3;
7873
7874
4.22k
        if (n < 128 || i + n > ssl->in_hslen) {
7875
36
            MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7876
36
            mbedtls_ssl_send_alert_message(ssl,
7877
36
                                           MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7878
36
                                           MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7879
36
            return MBEDTLS_ERR_SSL_DECODE_ERROR;
7880
36
        }
7881
7882
        /* Check if we're handling the first CRT in the chain. */
7883
4.18k
#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
7884
4.18k
        if (crt_cnt++ == 0 &&
7885
3.79k
            ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
7886
3.79k
            ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
7887
            /* During client-side renegotiation, check that the server's
7888
             * end-CRTs hasn't changed compared to the initial handshake,
7889
             * mitigating the triple handshake attack. On success, reuse
7890
             * the original end-CRT instead of parsing it again. */
7891
0
            MBEDTLS_SSL_DEBUG_MSG(3, ("Check that peer CRT hasn't changed during renegotiation"));
7892
0
            if (ssl_check_peer_crt_unchanged(ssl,
7893
0
                                             &ssl->in_msg[i],
7894
0
                                             n) != 0) {
7895
0
                MBEDTLS_SSL_DEBUG_MSG(1, ("new server cert during renegotiation"));
7896
0
                mbedtls_ssl_send_alert_message(ssl,
7897
0
                                               MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7898
0
                                               MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED);
7899
0
                return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
7900
0
            }
7901
7902
            /* Now we can safely free the original chain. */
7903
0
            ssl_clear_peer_cert(ssl->session);
7904
0
        }
7905
4.18k
#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7906
7907
        /* Parse the next certificate in the chain. */
7908
4.18k
#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7909
4.18k
        ret = mbedtls_x509_crt_parse_der(chain, ssl->in_msg + i, n);
7910
#else
7911
        /* If we don't need to store the CRT chain permanently, parse
7912
         * it in-place from the input buffer instead of making a copy. */
7913
        ret = mbedtls_x509_crt_parse_der_nocopy(chain, ssl->in_msg + i, n);
7914
#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7915
4.18k
        switch (ret) {
7916
1.53k
            case 0: /*ok*/
7917
2.44k
            case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
7918
                /* Ignore certificate with an unknown algorithm: maybe a
7919
                   prior certificate was already trusted. */
7920
2.44k
                break;
7921
7922
0
            case MBEDTLS_ERR_X509_ALLOC_FAILED:
7923
0
                alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
7924
0
                goto crt_parse_der_failed;
7925
7926
20
            case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
7927
20
                alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7928
20
                goto crt_parse_der_failed;
7929
7930
1.72k
            default:
7931
1.72k
                alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7932
1.74k
crt_parse_der_failed:
7933
1.74k
                mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert);
7934
1.74k
                MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
7935
1.74k
                return ret;
7936
4.18k
        }
7937
7938
2.44k
        i += n;
7939
2.44k
    }
7940
7941
2.02k
    MBEDTLS_SSL_DEBUG_CRT(3, "peer certificate", chain);
7942
2.02k
    return 0;
7943
3.82k
}
7944
7945
#if defined(MBEDTLS_SSL_SRV_C)
7946
MBEDTLS_CHECK_RETURN_CRITICAL
7947
static int ssl_srv_check_client_no_crt_notification(mbedtls_ssl_context *ssl)
7948
3.86k
{
7949
3.86k
    if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
7950
3.86k
        return -1;
7951
3.86k
    }
7952
7953
0
    if (ssl->in_hslen   == 3 + mbedtls_ssl_hs_hdr_len(ssl) &&
7954
0
        ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE    &&
7955
0
        ssl->in_msg[0]  == MBEDTLS_SSL_HS_CERTIFICATE   &&
7956
0
        memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl), "\0\0\0", 3) == 0) {
7957
0
        MBEDTLS_SSL_DEBUG_MSG(1, ("peer has no certificate"));
7958
0
        return 0;
7959
0
    }
7960
0
    return -1;
7961
0
}
7962
#endif /* MBEDTLS_SSL_SRV_C */
7963
7964
/* Check if a certificate message is expected.
7965
 * Return either
7966
 * - SSL_CERTIFICATE_EXPECTED, or
7967
 * - SSL_CERTIFICATE_SKIP
7968
 * indicating whether a Certificate message is expected or not.
7969
 */
7970
3.90k
#define SSL_CERTIFICATE_EXPECTED 0
7971
4.44k
#define SSL_CERTIFICATE_SKIP     1
7972
MBEDTLS_CHECK_RETURN_CRITICAL
7973
static int ssl_parse_certificate_coordinate(mbedtls_ssl_context *ssl,
7974
                                            int authmode)
7975
4.17k
{
7976
4.17k
    const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7977
4.17k
        ssl->handshake->ciphersuite_info;
7978
7979
4.17k
    if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
7980
271
        ssl->session_negotiate->verify_result = 0;
7981
271
        return SSL_CERTIFICATE_SKIP;
7982
271
    }
7983
7984
3.90k
#if defined(MBEDTLS_SSL_SRV_C)
7985
3.90k
    if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
7986
0
        if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
7987
0
            return SSL_CERTIFICATE_SKIP;
7988
0
        }
7989
7990
0
        if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
7991
0
            ssl->session_negotiate->verify_result =
7992
0
                MBEDTLS_X509_BADCERT_SKIP_VERIFY;
7993
0
            return SSL_CERTIFICATE_SKIP;
7994
0
        }
7995
0
    }
7996
#else
7997
    ((void) authmode);
7998
#endif /* MBEDTLS_SSL_SRV_C */
7999
8000
3.90k
    return SSL_CERTIFICATE_EXPECTED;
8001
3.90k
}
8002
8003
#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8004
MBEDTLS_CHECK_RETURN_CRITICAL
8005
static int ssl_remember_peer_crt_digest(mbedtls_ssl_context *ssl,
8006
                                        unsigned char *start, size_t len)
8007
{
8008
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8009
    /* Remember digest of the peer's end-CRT. */
8010
    ssl->session_negotiate->peer_cert_digest =
8011
        mbedtls_calloc(1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN);
8012
    if (ssl->session_negotiate->peer_cert_digest == NULL) {
8013
        MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%d bytes) failed",
8014
                                  MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN));
8015
        mbedtls_ssl_send_alert_message(ssl,
8016
                                       MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8017
                                       MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
8018
8019
        return MBEDTLS_ERR_SSL_ALLOC_FAILED;
8020
    }
8021
8022
    ret = mbedtls_md(mbedtls_md_info_from_type(
8023
                         MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE),
8024
                     start, len,
8025
                     ssl->session_negotiate->peer_cert_digest);
8026
8027
    ssl->session_negotiate->peer_cert_digest_type =
8028
        MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
8029
    ssl->session_negotiate->peer_cert_digest_len =
8030
        MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
8031
8032
    return ret;
8033
}
8034
8035
MBEDTLS_CHECK_RETURN_CRITICAL
8036
static int ssl_remember_peer_pubkey(mbedtls_ssl_context *ssl,
8037
                                    unsigned char *start, size_t len)
8038
{
8039
    unsigned char *end = start + len;
8040
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8041
8042
    /* Make a copy of the peer's raw public key. */
8043
    mbedtls_pk_init(&ssl->handshake->peer_pubkey);
8044
    ret = mbedtls_pk_parse_subpubkey(&start, end,
8045
                                     &ssl->handshake->peer_pubkey);
8046
    if (ret != 0) {
8047
        /* We should have parsed the public key before. */
8048
        return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8049
    }
8050
8051
    return 0;
8052
}
8053
#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8054
8055
int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
8056
4.17k
{
8057
4.17k
    int ret = 0;
8058
4.17k
    int crt_expected;
8059
    /* Authmode: precedence order is SNI if used else configuration */
8060
4.17k
#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8061
4.17k
    const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
8062
4.17k
                       ? ssl->handshake->sni_authmode
8063
4.17k
                       : ssl->conf->authmode;
8064
#else
8065
    const int authmode = ssl->conf->authmode;
8066
#endif
8067
4.17k
    void *rs_ctx = NULL;
8068
4.17k
    mbedtls_x509_crt *chain = NULL;
8069
8070
4.17k
    MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
8071
8072
4.17k
    crt_expected = ssl_parse_certificate_coordinate(ssl, authmode);
8073
4.17k
    if (crt_expected == SSL_CERTIFICATE_SKIP) {
8074
271
        MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
8075
271
        goto exit;
8076
271
    }
8077
8078
3.90k
#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
8079
3.90k
    if (ssl->handshake->ecrs_enabled &&
8080
298
        ssl->handshake->ecrs_state == ssl_ecrs_crt_verify) {
8081
0
        chain = ssl->handshake->ecrs_peer_cert;
8082
0
        ssl->handshake->ecrs_peer_cert = NULL;
8083
0
        goto crt_verify;
8084
0
    }
8085
3.90k
#endif
8086
8087
3.90k
    if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
8088
        /* mbedtls_ssl_read_record may have sent an alert already. We
8089
           let it decide whether to alert. */
8090
39
        MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
8091
39
        goto exit;
8092
39
    }
8093
8094
3.86k
#if defined(MBEDTLS_SSL_SRV_C)
8095
3.86k
    if (ssl_srv_check_client_no_crt_notification(ssl) == 0) {
8096
0
        ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
8097
8098
0
        if (authmode != MBEDTLS_SSL_VERIFY_OPTIONAL) {
8099
0
            ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
8100
0
        }
8101
8102
0
        goto exit;
8103
0
    }
8104
3.86k
#endif /* MBEDTLS_SSL_SRV_C */
8105
8106
    /* Clear existing peer CRT structure in case we tried to
8107
     * reuse a session but it failed, and allocate a new one. */
8108
3.86k
    ssl_clear_peer_cert(ssl->session_negotiate);
8109
8110
3.86k
    chain = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
8111
3.86k
    if (chain == NULL) {
8112
0
        MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
8113
0
                                  sizeof(mbedtls_x509_crt)));
8114
0
        mbedtls_ssl_send_alert_message(ssl,
8115
0
                                       MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8116
0
                                       MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
8117
8118
0
        ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
8119
0
        goto exit;
8120
0
    }
8121
3.86k
    mbedtls_x509_crt_init(chain);
8122
8123
3.86k
    ret = ssl_parse_certificate_chain(ssl, chain);
8124
3.86k
    if (ret != 0) {
8125
1.83k
        goto exit;
8126
1.83k
    }
8127
8128
2.02k
#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
8129
2.02k
    if (ssl->handshake->ecrs_enabled) {
8130
270
        ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
8131
270
    }
8132
8133
2.02k
crt_verify:
8134
2.02k
    if (ssl->handshake->ecrs_enabled) {
8135
270
        rs_ctx = &ssl->handshake->ecrs_ctx;
8136
270
    }
8137
2.02k
#endif
8138
8139
2.02k
    ret = mbedtls_ssl_verify_certificate(ssl, authmode, chain,
8140
2.02k
                                         ssl->handshake->ciphersuite_info,
8141
2.02k
                                         rs_ctx);
8142
2.02k
    if (ret != 0) {
8143
0
        goto exit;
8144
0
    }
8145
8146
#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8147
    {
8148
        unsigned char *crt_start, *pk_start;
8149
        size_t crt_len, pk_len;
8150
8151
        /* We parse the CRT chain without copying, so
8152
         * these pointers point into the input buffer,
8153
         * and are hence still valid after freeing the
8154
         * CRT chain. */
8155
8156
        crt_start = chain->raw.p;
8157
        crt_len   = chain->raw.len;
8158
8159
        pk_start = chain->pk_raw.p;
8160
        pk_len   = chain->pk_raw.len;
8161
8162
        /* Free the CRT structures before computing
8163
         * digest and copying the peer's public key. */
8164
        mbedtls_x509_crt_free(chain);
8165
        mbedtls_free(chain);
8166
        chain = NULL;
8167
8168
        ret = ssl_remember_peer_crt_digest(ssl, crt_start, crt_len);
8169
        if (ret != 0) {
8170
            goto exit;
8171
        }
8172
8173
        ret = ssl_remember_peer_pubkey(ssl, pk_start, pk_len);
8174
        if (ret != 0) {
8175
            goto exit;
8176
        }
8177
    }
8178
#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8179
    /* Pass ownership to session structure. */
8180
2.02k
    ssl->session_negotiate->peer_cert = chain;
8181
2.02k
    chain = NULL;
8182
2.02k
#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8183
8184
2.02k
    MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate"));
8185
8186
4.17k
exit:
8187
8188
4.17k
    if (ret == 0) {
8189
2.30k
        mbedtls_ssl_handshake_increment_state(ssl);
8190
2.30k
    }
8191
8192
4.17k
#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
8193
4.17k
    if (ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) {
8194
0
        ssl->handshake->ecrs_peer_cert = chain;
8195
0
        chain = NULL;
8196
0
    }
8197
4.17k
#endif
8198
8199
4.17k
    if (chain != NULL) {
8200
1.83k
        mbedtls_x509_crt_free(chain);
8201
1.83k
        mbedtls_free(chain);
8202
1.83k
    }
8203
8204
4.17k
    return ret;
8205
2.02k
}
8206
#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8207
8208
static int ssl_calc_finished_tls_generic(mbedtls_ssl_context *ssl, void *ctx,
8209
                                         unsigned char *padbuf, size_t hlen,
8210
                                         unsigned char *buf, int from)
8211
1.38k
{
8212
1.38k
    unsigned int len = 12;
8213
1.38k
    const char *sender;
8214
#if defined(MBEDTLS_USE_PSA_CRYPTO)
8215
    psa_status_t status;
8216
    psa_hash_operation_t *hs_op = ctx;
8217
    psa_hash_operation_t cloned_op = PSA_HASH_OPERATION_INIT;
8218
    size_t hash_size;
8219
#else
8220
1.38k
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8221
1.38k
    mbedtls_md_context_t *hs_ctx = ctx;
8222
1.38k
    mbedtls_md_context_t cloned_ctx;
8223
1.38k
    mbedtls_md_init(&cloned_ctx);
8224
1.38k
#endif
8225
8226
1.38k
    mbedtls_ssl_session *session = ssl->session_negotiate;
8227
1.38k
    if (!session) {
8228
0
        session = ssl->session;
8229
0
    }
8230
8231
1.38k
    sender = (from == MBEDTLS_SSL_IS_CLIENT)
8232
1.38k
             ? "client finished"
8233
1.38k
             : "server finished";
8234
8235
#if defined(MBEDTLS_USE_PSA_CRYPTO)
8236
    MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls"));
8237
8238
    status = psa_hash_clone(hs_op, &cloned_op);
8239
    if (status != PSA_SUCCESS) {
8240
        goto exit;
8241
    }
8242
8243
    status = psa_hash_finish(&cloned_op, padbuf, hlen, &hash_size);
8244
    if (status != PSA_SUCCESS) {
8245
        goto exit;
8246
    }
8247
    MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, hlen);
8248
#else
8249
1.38k
    MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls"));
8250
8251
1.38k
    ret = mbedtls_md_setup(&cloned_ctx, mbedtls_md_info_from_ctx(hs_ctx), 0);
8252
1.38k
    if (ret != 0) {
8253
0
        goto exit;
8254
0
    }
8255
1.38k
    ret = mbedtls_md_clone(&cloned_ctx, hs_ctx);
8256
1.38k
    if (ret != 0) {
8257
0
        goto exit;
8258
0
    }
8259
8260
1.38k
    ret = mbedtls_md_finish(&cloned_ctx, padbuf);
8261
1.38k
    if (ret != 0) {
8262
0
        goto exit;
8263
0
    }
8264
1.38k
#endif /* MBEDTLS_USE_PSA_CRYPTO */
8265
8266
1.38k
    MBEDTLS_SSL_DEBUG_BUF(4, "finished output", padbuf, hlen);
8267
8268
    /*
8269
     * TLSv1.2:
8270
     *   hash = PRF( master, finished_label,
8271
     *               Hash( handshake ) )[0.11]
8272
     */
8273
1.38k
    ssl->handshake->tls_prf(session->master, 48, sender,
8274
1.38k
                            padbuf, hlen, buf, len);
8275
8276
1.38k
    MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
8277
8278
1.38k
    mbedtls_platform_zeroize(padbuf, hlen);
8279
8280
1.38k
    MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
8281
8282
1.38k
exit:
8283
#if defined(MBEDTLS_USE_PSA_CRYPTO)
8284
    psa_hash_abort(&cloned_op);
8285
    return PSA_TO_MBEDTLS_ERR(status);
8286
#else
8287
1.38k
    mbedtls_md_free(&cloned_ctx);
8288
1.38k
    return ret;
8289
1.38k
#endif /* MBEDTLS_USE_PSA_CRYPTO */
8290
1.38k
}
8291
8292
#if defined(MBEDTLS_MD_CAN_SHA256)
8293
static int ssl_calc_finished_tls_sha256(
8294
    mbedtls_ssl_context *ssl, unsigned char *buf, int from)
8295
1.15k
{
8296
1.15k
    unsigned char padbuf[32];
8297
1.15k
    return ssl_calc_finished_tls_generic(ssl,
8298
#if defined(MBEDTLS_USE_PSA_CRYPTO)
8299
                                         &ssl->handshake->fin_sha256_psa,
8300
#else
8301
1.15k
                                         &ssl->handshake->fin_sha256,
8302
1.15k
#endif
8303
1.15k
                                         padbuf, sizeof(padbuf),
8304
1.15k
                                         buf, from);
8305
1.15k
}
8306
#endif /* MBEDTLS_MD_CAN_SHA256*/
8307
8308
8309
#if defined(MBEDTLS_MD_CAN_SHA384)
8310
static int ssl_calc_finished_tls_sha384(
8311
    mbedtls_ssl_context *ssl, unsigned char *buf, int from)
8312
223
{
8313
223
    unsigned char padbuf[48];
8314
223
    return ssl_calc_finished_tls_generic(ssl,
8315
#if defined(MBEDTLS_USE_PSA_CRYPTO)
8316
                                         &ssl->handshake->fin_sha384_psa,
8317
#else
8318
223
                                         &ssl->handshake->fin_sha384,
8319
223
#endif
8320
223
                                         padbuf, sizeof(padbuf),
8321
223
                                         buf, from);
8322
223
}
8323
#endif /* MBEDTLS_MD_CAN_SHA384*/
8324
8325
void mbedtls_ssl_handshake_wrapup_free_hs_transform(mbedtls_ssl_context *ssl)
8326
0
{
8327
0
    MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup: final free"));
8328
8329
    /*
8330
     * Free our handshake params
8331
     */
8332
0
    mbedtls_ssl_handshake_free(ssl);
8333
0
    mbedtls_free(ssl->handshake);
8334
0
    ssl->handshake = NULL;
8335
8336
    /*
8337
     * Free the previous transform and switch in the current one
8338
     */
8339
0
    if (ssl->transform) {
8340
0
        mbedtls_ssl_transform_free(ssl->transform);
8341
0
        mbedtls_free(ssl->transform);
8342
0
    }
8343
0
    ssl->transform = ssl->transform_negotiate;
8344
0
    ssl->transform_negotiate = NULL;
8345
8346
0
    MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup: final free"));
8347
0
}
8348
8349
void mbedtls_ssl_handshake_wrapup(mbedtls_ssl_context *ssl)
8350
0
{
8351
0
    int resume = ssl->handshake->resume;
8352
8353
0
    MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup"));
8354
8355
0
#if defined(MBEDTLS_SSL_RENEGOTIATION)
8356
0
    if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
8357
0
        ssl->renego_status =  MBEDTLS_SSL_RENEGOTIATION_DONE;
8358
0
        ssl->renego_records_seen = 0;
8359
0
    }
8360
0
#endif
8361
8362
    /*
8363
     * Free the previous session and switch in the current one
8364
     */
8365
0
    if (ssl->session) {
8366
0
#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8367
        /* RFC 7366 3.1: keep the EtM state */
8368
0
        ssl->session_negotiate->encrypt_then_mac =
8369
0
            ssl->session->encrypt_then_mac;
8370
0
#endif
8371
8372
0
        mbedtls_ssl_session_free(ssl->session);
8373
0
        mbedtls_free(ssl->session);
8374
0
    }
8375
0
    ssl->session = ssl->session_negotiate;
8376
0
    ssl->session_negotiate = NULL;
8377
8378
    /*
8379
     * Add cache entry
8380
     */
8381
0
    if (ssl->conf->f_set_cache != NULL &&
8382
0
        ssl->session->id_len != 0 &&
8383
0
        resume == 0) {
8384
0
        if (ssl->conf->f_set_cache(ssl->conf->p_cache,
8385
0
                                   ssl->session->id,
8386
0
                                   ssl->session->id_len,
8387
0
                                   ssl->session) != 0) {
8388
0
            MBEDTLS_SSL_DEBUG_MSG(1, ("cache did not store session"));
8389
0
        }
8390
0
    }
8391
8392
0
#if defined(MBEDTLS_SSL_PROTO_DTLS)
8393
0
    if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8394
0
        ssl->handshake->flight != NULL) {
8395
        /* Cancel handshake timer */
8396
0
        mbedtls_ssl_set_timer(ssl, 0);
8397
8398
        /* Keep last flight around in case we need to resend it:
8399
         * we need the handshake and transform structures for that */
8400
0
        MBEDTLS_SSL_DEBUG_MSG(3, ("skip freeing handshake and transform"));
8401
0
    } else
8402
0
#endif
8403
0
    mbedtls_ssl_handshake_wrapup_free_hs_transform(ssl);
8404
8405
0
    mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
8406
8407
0
    MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup"));
8408
0
}
8409
8410
int mbedtls_ssl_write_finished(mbedtls_ssl_context *ssl)
8411
979
{
8412
979
    int ret;
8413
979
    unsigned int hash_len;
8414
8415
979
    MBEDTLS_SSL_DEBUG_MSG(2, ("=> write finished"));
8416
8417
979
    mbedtls_ssl_update_out_pointers(ssl, ssl->transform_negotiate);
8418
8419
979
    ret = ssl->handshake->calc_finished(ssl, ssl->out_msg + 4, ssl->conf->endpoint);
8420
979
    if (ret != 0) {
8421
0
        MBEDTLS_SSL_DEBUG_RET(1, "calc_finished", ret);
8422
0
        return ret;
8423
0
    }
8424
8425
    /*
8426
     * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
8427
     * may define some other value. Currently (early 2016), no defined
8428
     * ciphersuite does this (and this is unlikely to change as activity has
8429
     * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
8430
     */
8431
979
    hash_len = 12;
8432
8433
979
#if defined(MBEDTLS_SSL_RENEGOTIATION)
8434
979
    ssl->verify_data_len = hash_len;
8435
979
    memcpy(ssl->own_verify_data, ssl->out_msg + 4, hash_len);
8436
979
#endif
8437
8438
979
    ssl->out_msglen  = 4 + hash_len;
8439
979
    ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8440
979
    ssl->out_msg[0]  = MBEDTLS_SSL_HS_FINISHED;
8441
8442
    /*
8443
     * In case of session resuming, invert the client and server
8444
     * ChangeCipherSpec messages order.
8445
     */
8446
979
    if (ssl->handshake->resume != 0) {
8447
0
#if defined(MBEDTLS_SSL_CLI_C)
8448
0
        if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
8449
0
            mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP);
8450
0
        }
8451
0
#endif
8452
0
#if defined(MBEDTLS_SSL_SRV_C)
8453
0
        if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
8454
0
            mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC);
8455
0
        }
8456
0
#endif
8457
979
    } else {
8458
979
        mbedtls_ssl_handshake_increment_state(ssl);
8459
979
    }
8460
8461
    /*
8462
     * Switch to our negotiated transform and session parameters for outbound
8463
     * data.
8464
     */
8465
979
    MBEDTLS_SSL_DEBUG_MSG(3, ("switching to new transform spec for outbound data"));
8466
8467
979
#if defined(MBEDTLS_SSL_PROTO_DTLS)
8468
979
    if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
8469
979
        unsigned char i;
8470
8471
        /* Remember current epoch settings for resending */
8472
979
        ssl->handshake->alt_transform_out = ssl->transform_out;
8473
979
        memcpy(ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
8474
979
               sizeof(ssl->handshake->alt_out_ctr));
8475
8476
        /* Set sequence_number to zero */
8477
979
        memset(&ssl->cur_out_ctr[2], 0, sizeof(ssl->cur_out_ctr) - 2);
8478
8479
8480
        /* Increment epoch */
8481
979
        for (i = 2; i > 0; i--) {
8482
979
            if (++ssl->cur_out_ctr[i - 1] != 0) {
8483
979
                break;
8484
979
            }
8485
979
        }
8486
8487
        /* The loop goes to its end iff the counter is wrapping */
8488
979
        if (i == 0) {
8489
0
            MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS epoch would wrap"));
8490
0
            return MBEDTLS_ERR_SSL_COUNTER_WRAPPING;
8491
0
        }
8492
979
    } else
8493
0
#endif /* MBEDTLS_SSL_PROTO_DTLS */
8494
0
    memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
8495
8496
979
    ssl->transform_out = ssl->transform_negotiate;
8497
979
    ssl->session_out = ssl->session_negotiate;
8498
8499
979
#if defined(MBEDTLS_SSL_PROTO_DTLS)
8500
979
    if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
8501
979
        mbedtls_ssl_send_flight_completed(ssl);
8502
979
    }
8503
979
#endif
8504
8505
979
    if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
8506
0
        MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
8507
0
        return ret;
8508
0
    }
8509
8510
979
#if defined(MBEDTLS_SSL_PROTO_DTLS)
8511
979
    if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8512
979
        (ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
8513
0
        MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flight_transmit", ret);
8514
0
        return ret;
8515
0
    }
8516
979
#endif
8517
8518
979
    MBEDTLS_SSL_DEBUG_MSG(2, ("<= write finished"));
8519
8520
979
    return 0;
8521
979
}
8522
8523
#define SSL_MAX_HASH_LEN 12
8524
8525
int mbedtls_ssl_parse_finished(mbedtls_ssl_context *ssl)
8526
403
{
8527
403
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8528
403
    unsigned int hash_len = 12;
8529
403
    unsigned char buf[SSL_MAX_HASH_LEN];
8530
8531
403
    MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse finished"));
8532
8533
403
    ret = ssl->handshake->calc_finished(ssl, buf, ssl->conf->endpoint ^ 1);
8534
403
    if (ret != 0) {
8535
0
        MBEDTLS_SSL_DEBUG_RET(1, "calc_finished", ret);
8536
0
        return ret;
8537
0
    }
8538
8539
403
    if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
8540
403
        MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
8541
403
        goto exit;
8542
403
    }
8543
8544
0
    if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
8545
0
        MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
8546
0
        mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8547
0
                                       MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
8548
0
        ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
8549
0
        goto exit;
8550
0
    }
8551
8552
0
    if (ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED) {
8553
0
        mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8554
0
                                       MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
8555
0
        ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
8556
0
        goto exit;
8557
0
    }
8558
8559
0
    if (ssl->in_hslen  != mbedtls_ssl_hs_hdr_len(ssl) + hash_len) {
8560
0
        MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
8561
0
        mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8562
0
                                       MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
8563
0
        ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
8564
0
        goto exit;
8565
0
    }
8566
8567
0
    if (mbedtls_ct_memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl),
8568
0
                          buf, hash_len) != 0) {
8569
0
        MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
8570
0
        mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8571
0
                                       MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR);
8572
0
        ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
8573
0
        goto exit;
8574
0
    }
8575
8576
0
#if defined(MBEDTLS_SSL_RENEGOTIATION)
8577
0
    ssl->verify_data_len = hash_len;
8578
0
    memcpy(ssl->peer_verify_data, buf, hash_len);
8579
0
#endif
8580
8581
0
    if (ssl->handshake->resume != 0) {
8582
0
#if defined(MBEDTLS_SSL_CLI_C)
8583
0
        if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
8584
0
            mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC);
8585
0
        }
8586
0
#endif
8587
0
#if defined(MBEDTLS_SSL_SRV_C)
8588
0
        if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
8589
0
            mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP);
8590
0
        }
8591
0
#endif
8592
0
    } else {
8593
0
        mbedtls_ssl_handshake_increment_state(ssl);
8594
0
    }
8595
8596
0
#if defined(MBEDTLS_SSL_PROTO_DTLS)
8597
0
    if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
8598
0
        mbedtls_ssl_recv_flight_completed(ssl);
8599
0
    }
8600
0
#endif
8601
8602
0
    MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse finished"));
8603
8604
403
exit:
8605
403
    mbedtls_platform_zeroize(buf, hash_len);
8606
403
    return ret;
8607
0
}
8608
8609
#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
8610
/*
8611
 * Helper to get TLS 1.2 PRF from ciphersuite
8612
 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
8613
 */
8614
static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id)
8615
0
{
8616
0
    const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
8617
0
        mbedtls_ssl_ciphersuite_from_id(ciphersuite_id);
8618
0
#if defined(MBEDTLS_MD_CAN_SHA384)
8619
0
    if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
8620
0
        return tls_prf_sha384;
8621
0
    } else
8622
0
#endif
8623
0
#if defined(MBEDTLS_MD_CAN_SHA256)
8624
0
    {
8625
0
        if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA256) {
8626
0
            return tls_prf_sha256;
8627
0
        }
8628
0
    }
8629
0
#endif
8630
#if !defined(MBEDTLS_MD_CAN_SHA384) && \
8631
    !defined(MBEDTLS_MD_CAN_SHA256)
8632
    (void) ciphersuite_info;
8633
#endif
8634
8635
0
    return NULL;
8636
0
}
8637
#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
8638
8639
static mbedtls_tls_prf_types tls_prf_get_type(mbedtls_ssl_tls_prf_cb *tls_prf)
8640
0
{
8641
0
    ((void) tls_prf);
8642
0
#if defined(MBEDTLS_MD_CAN_SHA384)
8643
0
    if (tls_prf == tls_prf_sha384) {
8644
0
        return MBEDTLS_SSL_TLS_PRF_SHA384;
8645
0
    } else
8646
0
#endif
8647
0
#if defined(MBEDTLS_MD_CAN_SHA256)
8648
0
    if (tls_prf == tls_prf_sha256) {
8649
0
        return MBEDTLS_SSL_TLS_PRF_SHA256;
8650
0
    } else
8651
0
#endif
8652
0
    return MBEDTLS_SSL_TLS_PRF_NONE;
8653
0
}
8654
8655
/*
8656
 * Populate a transform structure with session keys and all the other
8657
 * necessary information.
8658
 *
8659
 * Parameters:
8660
 * - [in/out]: transform: structure to populate
8661
 *      [in] must be just initialised with mbedtls_ssl_transform_init()
8662
 *      [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
8663
 * - [in] ciphersuite
8664
 * - [in] master
8665
 * - [in] encrypt_then_mac
8666
 * - [in] tls_prf: pointer to PRF to use for key derivation
8667
 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
8668
 * - [in] tls_version: TLS version
8669
 * - [in] endpoint: client or server
8670
 * - [in] ssl: used for:
8671
 *        - ssl->conf->{f,p}_export_keys
8672
 *      [in] optionally used for:
8673
 *        - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
8674
 */
8675
MBEDTLS_CHECK_RETURN_CRITICAL
8676
static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform,
8677
                                        int ciphersuite,
8678
                                        const unsigned char master[48],
8679
#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
8680
                                        int encrypt_then_mac,
8681
#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
8682
                                        ssl_tls_prf_t tls_prf,
8683
                                        const unsigned char randbytes[64],
8684
                                        mbedtls_ssl_protocol_version tls_version,
8685
                                        unsigned endpoint,
8686
                                        const mbedtls_ssl_context *ssl)
8687
979
{
8688
979
    int ret = 0;
8689
979
    unsigned char keyblk[256];
8690
979
    unsigned char *key1;
8691
979
    unsigned char *key2;
8692
979
    unsigned char *mac_enc;
8693
979
    unsigned char *mac_dec;
8694
979
    size_t mac_key_len = 0;
8695
979
    size_t iv_copy_len;
8696
979
    size_t keylen;
8697
979
    const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
8698
979
    mbedtls_ssl_mode_t ssl_mode;
8699
#if !defined(MBEDTLS_USE_PSA_CRYPTO)
8700
    const mbedtls_cipher_info_t *cipher_info;
8701
    const mbedtls_md_info_t *md_info;
8702
#endif /* !MBEDTLS_USE_PSA_CRYPTO */
8703
8704
#if defined(MBEDTLS_USE_PSA_CRYPTO)
8705
    psa_key_type_t key_type;
8706
0
    psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8707
    psa_algorithm_t alg;
8708
    psa_algorithm_t mac_alg = 0;
8709
    size_t key_bits;
8710
0
    psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
8711
#endif
8712
8713
    /*
8714
     * Some data just needs copying into the structure
8715
     */
8716
979
#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
8717
979
    transform->encrypt_then_mac = encrypt_then_mac;
8718
979
#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
8719
979
    transform->tls_version = tls_version;
8720
8721
979
#if defined(MBEDTLS_SSL_KEEP_RANDBYTES)
8722
979
    memcpy(transform->randbytes, randbytes, sizeof(transform->randbytes));
8723
979
#endif
8724
8725
979
#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
8726
979
    if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
8727
        /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
8728
         * generation separate. This should never happen. */
8729
0
        return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8730
0
    }
8731
979
#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
8732
8733
    /*
8734
     * Get various info structures
8735
     */
8736
979
    ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(ciphersuite);
8737
979
    if (ciphersuite_info == NULL) {
8738
0
        MBEDTLS_SSL_DEBUG_MSG(1, ("ciphersuite info for %d not found",
8739
0
                                  ciphersuite));
8740
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
8741
0
    }
8742
8743
979
    ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
8744
979
#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
8745
979
        encrypt_then_mac,
8746
979
#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
8747
979
        ciphersuite_info);
8748
8749
979
    if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
8750
191
        transform->taglen =
8751
191
            ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
8752
191
    }
8753
8754
#if defined(MBEDTLS_USE_PSA_CRYPTO)
8755
0
    if ((status = mbedtls_ssl_cipher_to_psa((mbedtls_cipher_type_t) ciphersuite_info->cipher,
8756
0
                                            transform->taglen,
8757
0
                                            &alg,
8758
0
                                            &key_type,
8759
0
                                            &key_bits)) != PSA_SUCCESS) {
8760
0
        ret = PSA_TO_MBEDTLS_ERR(status);
8761
0
        MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_cipher_to_psa", ret);
8762
0
        goto end;
8763
0
    }
8764
#else
8765
    cipher_info = mbedtls_cipher_info_from_type((mbedtls_cipher_type_t) ciphersuite_info->cipher);
8766
979
    if (cipher_info == NULL) {
8767
0
        MBEDTLS_SSL_DEBUG_MSG(1, ("cipher info for %u not found",
8768
0
                                  ciphersuite_info->cipher));
8769
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
8770
0
    }
8771
979
#endif /* MBEDTLS_USE_PSA_CRYPTO */
8772
8773
#if defined(MBEDTLS_USE_PSA_CRYPTO)
8774
0
    mac_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
8775
0
    if (mac_alg == 0) {
8776
0
        MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_md_psa_alg_from_type for %u not found",
8777
0
                                  (unsigned) ciphersuite_info->mac));
8778
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
8779
0
    }
8780
#else
8781
979
    md_info = mbedtls_md_info_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
8782
979
    if (md_info == NULL) {
8783
0
        MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_md info for %u not found",
8784
0
                                  (unsigned) ciphersuite_info->mac));
8785
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
8786
0
    }
8787
979
#endif /* MBEDTLS_USE_PSA_CRYPTO */
8788
8789
979
#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
8790
    /* Copy own and peer's CID if the use of the CID
8791
     * extension has been negotiated. */
8792
979
    if (ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED) {
8793
0
        MBEDTLS_SSL_DEBUG_MSG(3, ("Copy CIDs into SSL transform"));
8794
8795
0
        transform->in_cid_len = ssl->own_cid_len;
8796
0
        memcpy(transform->in_cid, ssl->own_cid, ssl->own_cid_len);
8797
0
        MBEDTLS_SSL_DEBUG_BUF(3, "Incoming CID", transform->in_cid,
8798
0
                              transform->in_cid_len);
8799
8800
0
        transform->out_cid_len = ssl->handshake->peer_cid_len;
8801
0
        memcpy(transform->out_cid, ssl->handshake->peer_cid,
8802
0
               ssl->handshake->peer_cid_len);
8803
0
        MBEDTLS_SSL_DEBUG_BUF(3, "Outgoing CID", transform->out_cid,
8804
0
                              transform->out_cid_len);
8805
0
    }
8806
979
#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
8807
8808
    /*
8809
     * Compute key block using the PRF
8810
     */
8811
979
    ret = tls_prf(master, 48, "key expansion", randbytes, 64, keyblk, 256);
8812
979
    if (ret != 0) {
8813
0
        MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
8814
0
        return ret;
8815
0
    }
8816
8817
979
    MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite = %s",
8818
979
                              mbedtls_ssl_get_ciphersuite_name(ciphersuite)));
8819
979
    MBEDTLS_SSL_DEBUG_BUF(3, "master secret", master, 48);
8820
979
    MBEDTLS_SSL_DEBUG_BUF(4, "random bytes", randbytes, 64);
8821
979
    MBEDTLS_SSL_DEBUG_BUF(4, "key block", keyblk, 256);
8822
8823
    /*
8824
     * Determine the appropriate key, IV and MAC length.
8825
     */
8826
8827
#if defined(MBEDTLS_USE_PSA_CRYPTO)
8828
0
    keylen = PSA_BITS_TO_BYTES(key_bits);
8829
#else
8830
    keylen = mbedtls_cipher_info_get_key_bitlen(cipher_info) / 8;
8831
#endif
8832
8833
979
#if defined(MBEDTLS_SSL_HAVE_AEAD)
8834
979
    if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
8835
191
        size_t explicit_ivlen;
8836
8837
191
        transform->maclen = 0;
8838
191
        mac_key_len = 0;
8839
8840
        /* All modes haves 96-bit IVs, but the length of the static parts vary
8841
         * with mode and version:
8842
         * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
8843
         *   (to be concatenated with a dynamically chosen IV of 8 Bytes)
8844
         * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
8845
         *   a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
8846
         *   sequence number).
8847
         */
8848
191
        transform->ivlen = 12;
8849
8850
191
        int is_chachapoly = 0;
8851
#if defined(MBEDTLS_USE_PSA_CRYPTO)
8852
0
        is_chachapoly = (key_type == PSA_KEY_TYPE_CHACHA20);
8853
#else
8854
        is_chachapoly = (mbedtls_cipher_info_get_mode(cipher_info)
8855
                         == MBEDTLS_MODE_CHACHAPOLY);
8856
#endif /* MBEDTLS_USE_PSA_CRYPTO */
8857
8858
191
        if (is_chachapoly) {
8859
0
            transform->fixed_ivlen = 12;
8860
191
        } else {
8861
191
            transform->fixed_ivlen = 4;
8862
191
        }
8863
8864
        /* Minimum length of encrypted record */
8865
191
        explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
8866
191
        transform->minlen = explicit_ivlen + transform->taglen;
8867
191
    } else
8868
788
#endif /* MBEDTLS_SSL_HAVE_AEAD */
8869
788
#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
8870
788
    if (ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
8871
383
        ssl_mode == MBEDTLS_SSL_MODE_CBC ||
8872
788
        ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
8873
#if defined(MBEDTLS_USE_PSA_CRYPTO)
8874
0
        size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type);
8875
#else
8876
        size_t block_size = mbedtls_cipher_info_get_block_size(cipher_info);
8877
#endif /* MBEDTLS_USE_PSA_CRYPTO */
8878
8879
#if defined(MBEDTLS_USE_PSA_CRYPTO)
8880
        /* Get MAC length */
8881
0
        mac_key_len = PSA_HASH_LENGTH(mac_alg);
8882
#else
8883
        /* Initialize HMAC contexts */
8884
788
        if ((ret = mbedtls_md_setup(&transform->md_ctx_enc, md_info, 1)) != 0 ||
8885
788
            (ret = mbedtls_md_setup(&transform->md_ctx_dec, md_info, 1)) != 0) {
8886
0
            MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
8887
0
            goto end;
8888
0
        }
8889
8890
        /* Get MAC length */
8891
788
        mac_key_len = mbedtls_md_get_size(md_info);
8892
788
#endif /* MBEDTLS_USE_PSA_CRYPTO */
8893
788
        transform->maclen = mac_key_len;
8894
8895
        /* IV length */
8896
#if defined(MBEDTLS_USE_PSA_CRYPTO)
8897
0
        transform->ivlen = PSA_CIPHER_IV_LENGTH(key_type, alg);
8898
#else
8899
        transform->ivlen = mbedtls_cipher_info_get_iv_size(cipher_info);
8900
#endif /* MBEDTLS_USE_PSA_CRYPTO */
8901
8902
        /* Minimum length */
8903
788
        if (ssl_mode == MBEDTLS_SSL_MODE_STREAM) {
8904
405
            transform->minlen = transform->maclen;
8905
405
        } else {
8906
            /*
8907
             * GenericBlockCipher:
8908
             * 1. if EtM is in use: one block plus MAC
8909
             *    otherwise: * first multiple of blocklen greater than maclen
8910
             * 2. IV
8911
             */
8912
383
#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8913
383
            if (ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
8914
88
                transform->minlen = transform->maclen
8915
88
                                    + block_size;
8916
88
            } else
8917
295
#endif
8918
295
            {
8919
295
                transform->minlen = transform->maclen
8920
295
                                    + block_size
8921
295
                                    - transform->maclen % block_size;
8922
295
            }
8923
8924
383
            if (tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
8925
383
                transform->minlen += transform->ivlen;
8926
383
            } else {
8927
0
                MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
8928
0
                ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8929
0
                goto end;
8930
0
            }
8931
383
        }
8932
788
    } else
8933
0
#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8934
0
    {
8935
0
        MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
8936
0
        return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8937
0
    }
8938
8939
979
    MBEDTLS_SSL_DEBUG_MSG(3, ("keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
8940
979
                              (unsigned) keylen,
8941
979
                              (unsigned) transform->minlen,
8942
979
                              (unsigned) transform->ivlen,
8943
979
                              (unsigned) transform->maclen));
8944
8945
    /*
8946
     * Finally setup the cipher contexts, IVs and MAC secrets.
8947
     */
8948
979
#if defined(MBEDTLS_SSL_CLI_C)
8949
979
    if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
8950
979
        key1 = keyblk + mac_key_len * 2;
8951
979
        key2 = keyblk + mac_key_len * 2 + keylen;
8952
8953
979
        mac_enc = keyblk;
8954
979
        mac_dec = keyblk + mac_key_len;
8955
8956
979
        iv_copy_len = (transform->fixed_ivlen) ?
8957
788
                      transform->fixed_ivlen : transform->ivlen;
8958
979
        memcpy(transform->iv_enc, key2 + keylen,  iv_copy_len);
8959
979
        memcpy(transform->iv_dec, key2 + keylen + iv_copy_len,
8960
979
               iv_copy_len);
8961
979
    } else
8962
0
#endif /* MBEDTLS_SSL_CLI_C */
8963
0
#if defined(MBEDTLS_SSL_SRV_C)
8964
0
    if (endpoint == MBEDTLS_SSL_IS_SERVER) {
8965
0
        key1 = keyblk + mac_key_len * 2 + keylen;
8966
0
        key2 = keyblk + mac_key_len * 2;
8967
8968
0
        mac_enc = keyblk + mac_key_len;
8969
0
        mac_dec = keyblk;
8970
8971
0
        iv_copy_len = (transform->fixed_ivlen) ?
8972
0
                      transform->fixed_ivlen : transform->ivlen;
8973
0
        memcpy(transform->iv_dec, key1 + keylen,  iv_copy_len);
8974
0
        memcpy(transform->iv_enc, key1 + keylen + iv_copy_len,
8975
0
               iv_copy_len);
8976
0
    } else
8977
0
#endif /* MBEDTLS_SSL_SRV_C */
8978
0
    {
8979
0
        MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
8980
0
        ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8981
0
        goto end;
8982
0
    }
8983
8984
979
    if (ssl->f_export_keys != NULL) {
8985
0
        ssl->f_export_keys(ssl->p_export_keys,
8986
0
                           MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
8987
0
                           master, 48,
8988
0
                           randbytes + 32,
8989
0
                           randbytes,
8990
0
                           tls_prf_get_type(tls_prf));
8991
0
    }
8992
8993
#if defined(MBEDTLS_USE_PSA_CRYPTO)
8994
    transform->psa_alg = alg;
8995
8996
0
    if (alg != MBEDTLS_SSL_NULL_CIPHER) {
8997
0
        psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
8998
0
        psa_set_key_algorithm(&attributes, alg);
8999
0
        psa_set_key_type(&attributes, key_type);
9000
9001
0
        if ((status = psa_import_key(&attributes,
9002
0
                                     key1,
9003
0
                                     PSA_BITS_TO_BYTES(key_bits),
9004
0
                                     &transform->psa_key_enc)) != PSA_SUCCESS) {
9005
0
            MBEDTLS_SSL_DEBUG_RET(3, "psa_import_key", (int) status);
9006
0
            ret = PSA_TO_MBEDTLS_ERR(status);
9007
0
            MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret);
9008
0
            goto end;
9009
0
        }
9010
9011
0
        psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
9012
9013
0
        if ((status = psa_import_key(&attributes,
9014
0
                                     key2,
9015
0
                                     PSA_BITS_TO_BYTES(key_bits),
9016
0
                                     &transform->psa_key_dec)) != PSA_SUCCESS) {
9017
0
            ret = PSA_TO_MBEDTLS_ERR(status);
9018
0
            MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret);
9019
0
            goto end;
9020
0
        }
9021
0
    }
9022
#else
9023
979
    if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_enc,
9024
979
                                    cipher_info)) != 0) {
9025
0
        MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
9026
0
        goto end;
9027
0
    }
9028
9029
979
    if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_dec,
9030
979
                                    cipher_info)) != 0) {
9031
0
        MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
9032
0
        goto end;
9033
0
    }
9034
9035
979
    if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_enc, key1,
9036
979
                                     (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
9037
979
                                     MBEDTLS_ENCRYPT)) != 0) {
9038
0
        MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
9039
0
        goto end;
9040
0
    }
9041
9042
979
    if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_dec, key2,
9043
979
                                     (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
9044
979
                                     MBEDTLS_DECRYPT)) != 0) {
9045
0
        MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
9046
0
        goto end;
9047
0
    }
9048
9049
979
#if defined(MBEDTLS_CIPHER_MODE_CBC)
9050
979
    if (mbedtls_cipher_info_get_mode(cipher_info) == MBEDTLS_MODE_CBC) {
9051
383
        if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_enc,
9052
383
                                                   MBEDTLS_PADDING_NONE)) != 0) {
9053
0
            MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
9054
0
            goto end;
9055
0
        }
9056
9057
383
        if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_dec,
9058
383
                                                   MBEDTLS_PADDING_NONE)) != 0) {
9059
0
            MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
9060
0
            goto end;
9061
0
        }
9062
383
    }
9063
979
#endif /* MBEDTLS_CIPHER_MODE_CBC */
9064
979
#endif /* MBEDTLS_USE_PSA_CRYPTO */
9065
9066
979
#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
9067
    /* For HMAC-based ciphersuites, initialize the HMAC transforms.
9068
       For AEAD-based ciphersuites, there is nothing to do here. */
9069
979
    if (mac_key_len != 0) {
9070
#if defined(MBEDTLS_USE_PSA_CRYPTO)
9071
0
        transform->psa_mac_alg = PSA_ALG_HMAC(mac_alg);
9072
9073
0
        psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
9074
0
        psa_set_key_algorithm(&attributes, PSA_ALG_HMAC(mac_alg));
9075
0
        psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
9076
9077
0
        if ((status = psa_import_key(&attributes,
9078
0
                                     mac_enc, mac_key_len,
9079
0
                                     &transform->psa_mac_enc)) != PSA_SUCCESS) {
9080
0
            ret = PSA_TO_MBEDTLS_ERR(status);
9081
0
            MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret);
9082
0
            goto end;
9083
0
        }
9084
9085
0
        if ((transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER) ||
9086
0
            ((transform->psa_alg == PSA_ALG_CBC_NO_PADDING)
9087
0
#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
9088
0
             && (transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED)
9089
0
#endif
9090
0
            )) {
9091
            /* mbedtls_ct_hmac() requires the key to be exportable */
9092
0
            psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT |
9093
0
                                    PSA_KEY_USAGE_VERIFY_HASH);
9094
0
        } else {
9095
0
            psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
9096
0
        }
9097
9098
0
        if ((status = psa_import_key(&attributes,
9099
0
                                     mac_dec, mac_key_len,
9100
0
                                     &transform->psa_mac_dec)) != PSA_SUCCESS) {
9101
0
            ret = PSA_TO_MBEDTLS_ERR(status);
9102
0
            MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret);
9103
0
            goto end;
9104
0
        }
9105
#else
9106
        ret = mbedtls_md_hmac_starts(&transform->md_ctx_enc, mac_enc, mac_key_len);
9107
788
        if (ret != 0) {
9108
0
            goto end;
9109
0
        }
9110
788
        ret = mbedtls_md_hmac_starts(&transform->md_ctx_dec, mac_dec, mac_key_len);
9111
788
        if (ret != 0) {
9112
0
            goto end;
9113
0
        }
9114
#endif /* MBEDTLS_USE_PSA_CRYPTO */
9115
788
    }
9116
979
#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
9117
9118
979
    ((void) mac_dec);
9119
979
    ((void) mac_enc);
9120
9121
979
end:
9122
979
    mbedtls_platform_zeroize(keyblk, sizeof(keyblk));
9123
979
    return ret;
9124
979
}
ssl_tls.c:ssl_tls12_populate_transform
Line
Count
Source
8687
979
{
8688
979
    int ret = 0;
8689
979
    unsigned char keyblk[256];
8690
979
    unsigned char *key1;
8691
979
    unsigned char *key2;
8692
979
    unsigned char *mac_enc;
8693
979
    unsigned char *mac_dec;
8694
979
    size_t mac_key_len = 0;
8695
979
    size_t iv_copy_len;
8696
979
    size_t keylen;
8697
979
    const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
8698
979
    mbedtls_ssl_mode_t ssl_mode;
8699
979
#if !defined(MBEDTLS_USE_PSA_CRYPTO)
8700
979
    const mbedtls_cipher_info_t *cipher_info;
8701
979
    const mbedtls_md_info_t *md_info;
8702
979
#endif /* !MBEDTLS_USE_PSA_CRYPTO */
8703
8704
#if defined(MBEDTLS_USE_PSA_CRYPTO)
8705
    psa_key_type_t key_type;
8706
    psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8707
    psa_algorithm_t alg;
8708
    psa_algorithm_t mac_alg = 0;
8709
    size_t key_bits;
8710
    psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
8711
#endif
8712
8713
    /*
8714
     * Some data just needs copying into the structure
8715
     */
8716
979
#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
8717
979
    transform->encrypt_then_mac = encrypt_then_mac;
8718
979
#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
8719
979
    transform->tls_version = tls_version;
8720
8721
979
#if defined(MBEDTLS_SSL_KEEP_RANDBYTES)
8722
979
    memcpy(transform->randbytes, randbytes, sizeof(transform->randbytes));
8723
979
#endif
8724
8725
979
#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
8726
979
    if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
8727
        /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
8728
         * generation separate. This should never happen. */
8729
0
        return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8730
0
    }
8731
979
#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
8732
8733
    /*
8734
     * Get various info structures
8735
     */
8736
979
    ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(ciphersuite);
8737
979
    if (ciphersuite_info == NULL) {
8738
0
        MBEDTLS_SSL_DEBUG_MSG(1, ("ciphersuite info for %d not found",
8739
0
                                  ciphersuite));
8740
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
8741
0
    }
8742
8743
979
    ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
8744
979
#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
8745
979
        encrypt_then_mac,
8746
979
#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
8747
979
        ciphersuite_info);
8748
8749
979
    if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
8750
191
        transform->taglen =
8751
191
            ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
8752
191
    }
8753
8754
#if defined(MBEDTLS_USE_PSA_CRYPTO)
8755
    if ((status = mbedtls_ssl_cipher_to_psa((mbedtls_cipher_type_t) ciphersuite_info->cipher,
8756
                                            transform->taglen,
8757
                                            &alg,
8758
                                            &key_type,
8759
                                            &key_bits)) != PSA_SUCCESS) {
8760
        ret = PSA_TO_MBEDTLS_ERR(status);
8761
        MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_cipher_to_psa", ret);
8762
        goto end;
8763
    }
8764
#else
8765
979
    cipher_info = mbedtls_cipher_info_from_type((mbedtls_cipher_type_t) ciphersuite_info->cipher);
8766
979
    if (cipher_info == NULL) {
8767
0
        MBEDTLS_SSL_DEBUG_MSG(1, ("cipher info for %u not found",
8768
0
                                  ciphersuite_info->cipher));
8769
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
8770
0
    }
8771
979
#endif /* MBEDTLS_USE_PSA_CRYPTO */
8772
8773
#if defined(MBEDTLS_USE_PSA_CRYPTO)
8774
    mac_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
8775
    if (mac_alg == 0) {
8776
        MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_md_psa_alg_from_type for %u not found",
8777
                                  (unsigned) ciphersuite_info->mac));
8778
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
8779
    }
8780
#else
8781
979
    md_info = mbedtls_md_info_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
8782
979
    if (md_info == NULL) {
8783
0
        MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_md info for %u not found",
8784
0
                                  (unsigned) ciphersuite_info->mac));
8785
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
8786
0
    }
8787
979
#endif /* MBEDTLS_USE_PSA_CRYPTO */
8788
8789
979
#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
8790
    /* Copy own and peer's CID if the use of the CID
8791
     * extension has been negotiated. */
8792
979
    if (ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED) {
8793
0
        MBEDTLS_SSL_DEBUG_MSG(3, ("Copy CIDs into SSL transform"));
8794
8795
0
        transform->in_cid_len = ssl->own_cid_len;
8796
0
        memcpy(transform->in_cid, ssl->own_cid, ssl->own_cid_len);
8797
0
        MBEDTLS_SSL_DEBUG_BUF(3, "Incoming CID", transform->in_cid,
8798
0
                              transform->in_cid_len);
8799
8800
0
        transform->out_cid_len = ssl->handshake->peer_cid_len;
8801
0
        memcpy(transform->out_cid, ssl->handshake->peer_cid,
8802
0
               ssl->handshake->peer_cid_len);
8803
0
        MBEDTLS_SSL_DEBUG_BUF(3, "Outgoing CID", transform->out_cid,
8804
0
                              transform->out_cid_len);
8805
0
    }
8806
979
#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
8807
8808
    /*
8809
     * Compute key block using the PRF
8810
     */
8811
979
    ret = tls_prf(master, 48, "key expansion", randbytes, 64, keyblk, 256);
8812
979
    if (ret != 0) {
8813
0
        MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
8814
0
        return ret;
8815
0
    }
8816
8817
979
    MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite = %s",
8818
979
                              mbedtls_ssl_get_ciphersuite_name(ciphersuite)));
8819
979
    MBEDTLS_SSL_DEBUG_BUF(3, "master secret", master, 48);
8820
979
    MBEDTLS_SSL_DEBUG_BUF(4, "random bytes", randbytes, 64);
8821
979
    MBEDTLS_SSL_DEBUG_BUF(4, "key block", keyblk, 256);
8822
8823
    /*
8824
     * Determine the appropriate key, IV and MAC length.
8825
     */
8826
8827
#if defined(MBEDTLS_USE_PSA_CRYPTO)
8828
    keylen = PSA_BITS_TO_BYTES(key_bits);
8829
#else
8830
979
    keylen = mbedtls_cipher_info_get_key_bitlen(cipher_info) / 8;
8831
979
#endif
8832
8833
979
#if defined(MBEDTLS_SSL_HAVE_AEAD)
8834
979
    if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
8835
191
        size_t explicit_ivlen;
8836
8837
191
        transform->maclen = 0;
8838
191
        mac_key_len = 0;
8839
8840
        /* All modes haves 96-bit IVs, but the length of the static parts vary
8841
         * with mode and version:
8842
         * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
8843
         *   (to be concatenated with a dynamically chosen IV of 8 Bytes)
8844
         * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
8845
         *   a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
8846
         *   sequence number).
8847
         */
8848
191
        transform->ivlen = 12;
8849
8850
191
        int is_chachapoly = 0;
8851
#if defined(MBEDTLS_USE_PSA_CRYPTO)
8852
        is_chachapoly = (key_type == PSA_KEY_TYPE_CHACHA20);
8853
#else
8854
191
        is_chachapoly = (mbedtls_cipher_info_get_mode(cipher_info)
8855
191
                         == MBEDTLS_MODE_CHACHAPOLY);
8856
191
#endif /* MBEDTLS_USE_PSA_CRYPTO */
8857
8858
191
        if (is_chachapoly) {
8859
0
            transform->fixed_ivlen = 12;
8860
191
        } else {
8861
191
            transform->fixed_ivlen = 4;
8862
191
        }
8863
8864
        /* Minimum length of encrypted record */
8865
191
        explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
8866
191
        transform->minlen = explicit_ivlen + transform->taglen;
8867
191
    } else
8868
788
#endif /* MBEDTLS_SSL_HAVE_AEAD */
8869
788
#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
8870
788
    if (ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
8871
383
        ssl_mode == MBEDTLS_SSL_MODE_CBC ||
8872
788
        ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
8873
#if defined(MBEDTLS_USE_PSA_CRYPTO)
8874
        size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type);
8875
#else
8876
788
        size_t block_size = mbedtls_cipher_info_get_block_size(cipher_info);
8877
788
#endif /* MBEDTLS_USE_PSA_CRYPTO */
8878
8879
#if defined(MBEDTLS_USE_PSA_CRYPTO)
8880
        /* Get MAC length */
8881
        mac_key_len = PSA_HASH_LENGTH(mac_alg);
8882
#else
8883
        /* Initialize HMAC contexts */
8884
788
        if ((ret = mbedtls_md_setup(&transform->md_ctx_enc, md_info, 1)) != 0 ||
8885
788
            (ret = mbedtls_md_setup(&transform->md_ctx_dec, md_info, 1)) != 0) {
8886
0
            MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
8887
0
            goto end;
8888
0
        }
8889
8890
        /* Get MAC length */
8891
788
        mac_key_len = mbedtls_md_get_size(md_info);
8892
788
#endif /* MBEDTLS_USE_PSA_CRYPTO */
8893
788
        transform->maclen = mac_key_len;
8894
8895
        /* IV length */
8896
#if defined(MBEDTLS_USE_PSA_CRYPTO)
8897
        transform->ivlen = PSA_CIPHER_IV_LENGTH(key_type, alg);
8898
#else
8899
788
        transform->ivlen = mbedtls_cipher_info_get_iv_size(cipher_info);
8900
788
#endif /* MBEDTLS_USE_PSA_CRYPTO */
8901
8902
        /* Minimum length */
8903
788
        if (ssl_mode == MBEDTLS_SSL_MODE_STREAM) {
8904
405
            transform->minlen = transform->maclen;
8905
405
        } else {
8906
            /*
8907
             * GenericBlockCipher:
8908
             * 1. if EtM is in use: one block plus MAC
8909
             *    otherwise: * first multiple of blocklen greater than maclen
8910
             * 2. IV
8911
             */
8912
383
#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8913
383
            if (ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
8914
88
                transform->minlen = transform->maclen
8915
88
                                    + block_size;
8916
88
            } else
8917
295
#endif
8918
295
            {
8919
295
                transform->minlen = transform->maclen
8920
295
                                    + block_size
8921
295
                                    - transform->maclen % block_size;
8922
295
            }
8923
8924
383
            if (tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
8925
383
                transform->minlen += transform->ivlen;
8926
383
            } else {
8927
0
                MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
8928
0
                ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8929
0
                goto end;
8930
0
            }
8931
383
        }
8932
788
    } else
8933
0
#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8934
0
    {
8935
0
        MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
8936
0
        return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8937
0
    }
8938
8939
979
    MBEDTLS_SSL_DEBUG_MSG(3, ("keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
8940
979
                              (unsigned) keylen,
8941
979
                              (unsigned) transform->minlen,
8942
979
                              (unsigned) transform->ivlen,
8943
979
                              (unsigned) transform->maclen));
8944
8945
    /*
8946
     * Finally setup the cipher contexts, IVs and MAC secrets.
8947
     */
8948
979
#if defined(MBEDTLS_SSL_CLI_C)
8949
979
    if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
8950
979
        key1 = keyblk + mac_key_len * 2;
8951
979
        key2 = keyblk + mac_key_len * 2 + keylen;
8952
8953
979
        mac_enc = keyblk;
8954
979
        mac_dec = keyblk + mac_key_len;
8955
8956
979
        iv_copy_len = (transform->fixed_ivlen) ?
8957
788
                      transform->fixed_ivlen : transform->ivlen;
8958
979
        memcpy(transform->iv_enc, key2 + keylen,  iv_copy_len);
8959
979
        memcpy(transform->iv_dec, key2 + keylen + iv_copy_len,
8960
979
               iv_copy_len);
8961
979
    } else
8962
0
#endif /* MBEDTLS_SSL_CLI_C */
8963
0
#if defined(MBEDTLS_SSL_SRV_C)
8964
0
    if (endpoint == MBEDTLS_SSL_IS_SERVER) {
8965
0
        key1 = keyblk + mac_key_len * 2 + keylen;
8966
0
        key2 = keyblk + mac_key_len * 2;
8967
8968
0
        mac_enc = keyblk + mac_key_len;
8969
0
        mac_dec = keyblk;
8970
8971
0
        iv_copy_len = (transform->fixed_ivlen) ?
8972
0
                      transform->fixed_ivlen : transform->ivlen;
8973
0
        memcpy(transform->iv_dec, key1 + keylen,  iv_copy_len);
8974
0
        memcpy(transform->iv_enc, key1 + keylen + iv_copy_len,
8975
0
               iv_copy_len);
8976
0
    } else
8977
0
#endif /* MBEDTLS_SSL_SRV_C */
8978
0
    {
8979
0
        MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
8980
0
        ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8981
0
        goto end;
8982
0
    }
8983
8984
979
    if (ssl->f_export_keys != NULL) {
8985
0
        ssl->f_export_keys(ssl->p_export_keys,
8986
0
                           MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
8987
0
                           master, 48,
8988
0
                           randbytes + 32,
8989
0
                           randbytes,
8990
0
                           tls_prf_get_type(tls_prf));
8991
0
    }
8992
8993
#if defined(MBEDTLS_USE_PSA_CRYPTO)
8994
    transform->psa_alg = alg;
8995
8996
    if (alg != MBEDTLS_SSL_NULL_CIPHER) {
8997
        psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
8998
        psa_set_key_algorithm(&attributes, alg);
8999
        psa_set_key_type(&attributes, key_type);
9000
9001
        if ((status = psa_import_key(&attributes,
9002
                                     key1,
9003
                                     PSA_BITS_TO_BYTES(key_bits),
9004
                                     &transform->psa_key_enc)) != PSA_SUCCESS) {
9005
            MBEDTLS_SSL_DEBUG_RET(3, "psa_import_key", (int) status);
9006
            ret = PSA_TO_MBEDTLS_ERR(status);
9007
            MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret);
9008
            goto end;
9009
        }
9010
9011
        psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
9012
9013
        if ((status = psa_import_key(&attributes,
9014
                                     key2,
9015
                                     PSA_BITS_TO_BYTES(key_bits),
9016
                                     &transform->psa_key_dec)) != PSA_SUCCESS) {
9017
            ret = PSA_TO_MBEDTLS_ERR(status);
9018
            MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret);
9019
            goto end;
9020
        }
9021
    }
9022
#else
9023
979
    if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_enc,
9024
979
                                    cipher_info)) != 0) {
9025
0
        MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
9026
0
        goto end;
9027
0
    }
9028
9029
979
    if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_dec,
9030
979
                                    cipher_info)) != 0) {
9031
0
        MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
9032
0
        goto end;
9033
0
    }
9034
9035
979
    if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_enc, key1,
9036
979
                                     (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
9037
979
                                     MBEDTLS_ENCRYPT)) != 0) {
9038
0
        MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
9039
0
        goto end;
9040
0
    }
9041
9042
979
    if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_dec, key2,
9043
979
                                     (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
9044
979
                                     MBEDTLS_DECRYPT)) != 0) {
9045
0
        MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
9046
0
        goto end;
9047
0
    }
9048
9049
979
#if defined(MBEDTLS_CIPHER_MODE_CBC)
9050
979
    if (mbedtls_cipher_info_get_mode(cipher_info) == MBEDTLS_MODE_CBC) {
9051
383
        if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_enc,
9052
383
                                                   MBEDTLS_PADDING_NONE)) != 0) {
9053
0
            MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
9054
0
            goto end;
9055
0
        }
9056
9057
383
        if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_dec,
9058
383
                                                   MBEDTLS_PADDING_NONE)) != 0) {
9059
0
            MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
9060
0
            goto end;
9061
0
        }
9062
383
    }
9063
979
#endif /* MBEDTLS_CIPHER_MODE_CBC */
9064
979
#endif /* MBEDTLS_USE_PSA_CRYPTO */
9065
9066
979
#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
9067
    /* For HMAC-based ciphersuites, initialize the HMAC transforms.
9068
       For AEAD-based ciphersuites, there is nothing to do here. */
9069
979
    if (mac_key_len != 0) {
9070
#if defined(MBEDTLS_USE_PSA_CRYPTO)
9071
        transform->psa_mac_alg = PSA_ALG_HMAC(mac_alg);
9072
9073
        psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
9074
        psa_set_key_algorithm(&attributes, PSA_ALG_HMAC(mac_alg));
9075
        psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
9076
9077
        if ((status = psa_import_key(&attributes,
9078
                                     mac_enc, mac_key_len,
9079
                                     &transform->psa_mac_enc)) != PSA_SUCCESS) {
9080
            ret = PSA_TO_MBEDTLS_ERR(status);
9081
            MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret);
9082
            goto end;
9083
        }
9084
9085
        if ((transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER) ||
9086
            ((transform->psa_alg == PSA_ALG_CBC_NO_PADDING)
9087
#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
9088
             && (transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED)
9089
#endif
9090
            )) {
9091
            /* mbedtls_ct_hmac() requires the key to be exportable */
9092
            psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT |
9093
                                    PSA_KEY_USAGE_VERIFY_HASH);
9094
        } else {
9095
            psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
9096
        }
9097
9098
        if ((status = psa_import_key(&attributes,
9099
                                     mac_dec, mac_key_len,
9100
                                     &transform->psa_mac_dec)) != PSA_SUCCESS) {
9101
            ret = PSA_TO_MBEDTLS_ERR(status);
9102
            MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret);
9103
            goto end;
9104
        }
9105
#else
9106
788
        ret = mbedtls_md_hmac_starts(&transform->md_ctx_enc, mac_enc, mac_key_len);
9107
788
        if (ret != 0) {
9108
0
            goto end;
9109
0
        }
9110
788
        ret = mbedtls_md_hmac_starts(&transform->md_ctx_dec, mac_dec, mac_key_len);
9111
788
        if (ret != 0) {
9112
0
            goto end;
9113
0
        }
9114
788
#endif /* MBEDTLS_USE_PSA_CRYPTO */
9115
788
    }
9116
979
#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
9117
9118
979
    ((void) mac_dec);
9119
979
    ((void) mac_enc);
9120
9121
979
end:
9122
979
    mbedtls_platform_zeroize(keyblk, sizeof(keyblk));
9123
979
    return ret;
9124
979
}
Unexecuted instantiation: ssl_tls.c:ssl_tls12_populate_transform
9125
9126
#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) && \
9127
    defined(MBEDTLS_USE_PSA_CRYPTO)
9128
int mbedtls_psa_ecjpake_read_round(
9129
    psa_pake_operation_t *pake_ctx,
9130
    const unsigned char *buf,
9131
    size_t len, mbedtls_ecjpake_rounds_t round)
9132
0
{
9133
0
    psa_status_t status;
9134
0
    size_t input_offset = 0;
9135
    /*
9136
     * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
9137
     * At round two perform a single cycle
9138
     */
9139
0
    unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
9140
9141
0
    for (; remaining_steps > 0; remaining_steps--) {
9142
0
        for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
9143
0
             step <= PSA_PAKE_STEP_ZK_PROOF;
9144
0
             ++step) {
9145
            /* Length is stored at the first byte */
9146
0
            size_t length = buf[input_offset];
9147
0
            input_offset += 1;
9148
9149
0
            if (input_offset + length > len) {
9150
0
                return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
9151
0
            }
9152
9153
0
            status = psa_pake_input(pake_ctx, step,
9154
0
                                    buf + input_offset, length);
9155
0
            if (status != PSA_SUCCESS) {
9156
0
                return PSA_TO_MBEDTLS_ERR(status);
9157
0
            }
9158
9159
0
            input_offset += length;
9160
0
        }
9161
0
    }
9162
9163
0
    if (input_offset != len) {
9164
0
        return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
9165
0
    }
9166
9167
0
    return 0;
9168
0
}
9169
9170
int mbedtls_psa_ecjpake_write_round(
9171
    psa_pake_operation_t *pake_ctx,
9172
    unsigned char *buf,
9173
    size_t len, size_t *olen,
9174
    mbedtls_ecjpake_rounds_t round)
9175
0
{
9176
0
    psa_status_t status;
9177
0
    size_t output_offset = 0;
9178
0
    size_t output_len;
9179
    /*
9180
     * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
9181
     * At round two perform a single cycle
9182
     */
9183
0
    unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
9184
9185
0
    for (; remaining_steps > 0; remaining_steps--) {
9186
0
        for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
9187
0
             step <= PSA_PAKE_STEP_ZK_PROOF;
9188
0
             ++step) {
9189
            /*
9190
             * For each step, prepend 1 byte with the length of the data as
9191
             * given by psa_pake_output().
9192
             */
9193
0
            status = psa_pake_output(pake_ctx, step,
9194
0
                                     buf + output_offset + 1,
9195
0
                                     len - output_offset - 1,
9196
0
                                     &output_len);
9197
0
            if (status != PSA_SUCCESS) {
9198
0
                return PSA_TO_MBEDTLS_ERR(status);
9199
0
            }
9200
9201
0
            *(buf + output_offset) = (uint8_t) output_len;
9202
9203
0
            output_offset += output_len + 1;
9204
0
        }
9205
0
    }
9206
9207
0
    *olen = output_offset;
9208
9209
0
    return 0;
9210
0
}
9211
#endif //MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED && MBEDTLS_USE_PSA_CRYPTO
9212
9213
#if defined(MBEDTLS_USE_PSA_CRYPTO)
9214
int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
9215
                                           unsigned char *hash, size_t *hashlen,
9216
                                           unsigned char *data, size_t data_len,
9217
                                           mbedtls_md_type_t md_alg)
9218
{
9219
    psa_status_t status;
9220
    psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
9221
    psa_algorithm_t hash_alg = mbedtls_md_psa_alg_from_type(md_alg);
9222
9223
    MBEDTLS_SSL_DEBUG_MSG(3, ("Perform PSA-based computation of digest of ServerKeyExchange"));
9224
9225
    if ((status = psa_hash_setup(&hash_operation,
9226
                                 hash_alg)) != PSA_SUCCESS) {
9227
        MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_setup", status);
9228
        goto exit;
9229
    }
9230
9231
    if ((status = psa_hash_update(&hash_operation, ssl->handshake->randbytes,
9232
                                  64)) != PSA_SUCCESS) {
9233
        MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
9234
        goto exit;
9235
    }
9236
9237
    if ((status = psa_hash_update(&hash_operation,
9238
                                  data, data_len)) != PSA_SUCCESS) {
9239
        MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
9240
        goto exit;
9241
    }
9242
9243
    if ((status = psa_hash_finish(&hash_operation, hash, PSA_HASH_MAX_SIZE,
9244
                                  hashlen)) != PSA_SUCCESS) {
9245
        MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_finish", status);
9246
        goto exit;
9247
    }
9248
9249
exit:
9250
    if (status != PSA_SUCCESS) {
9251
        mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9252
                                       MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
9253
        switch (status) {
9254
            case PSA_ERROR_NOT_SUPPORTED:
9255
                return MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE;
9256
            case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
9257
            case PSA_ERROR_BUFFER_TOO_SMALL:
9258
                return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
9259
            case PSA_ERROR_INSUFFICIENT_MEMORY:
9260
                return MBEDTLS_ERR_MD_ALLOC_FAILED;
9261
            default:
9262
                return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
9263
        }
9264
    }
9265
    return 0;
9266
}
9267
9268
#else
9269
9270
int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
9271
                                           unsigned char *hash, size_t *hashlen,
9272
                                           unsigned char *data, size_t data_len,
9273
                                           mbedtls_md_type_t md_alg)
9274
339
{
9275
339
    int ret = 0;
9276
339
    mbedtls_md_context_t ctx;
9277
339
    const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
9278
339
    *hashlen = mbedtls_md_get_size(md_info);
9279
9280
339
    MBEDTLS_SSL_DEBUG_MSG(3, ("Perform mbedtls-based computation of digest of ServerKeyExchange"));
9281
9282
339
    mbedtls_md_init(&ctx);
9283
9284
    /*
9285
     * digitally-signed struct {
9286
     *     opaque client_random[32];
9287
     *     opaque server_random[32];
9288
     *     ServerDHParams params;
9289
     * };
9290
     */
9291
339
    if ((ret = mbedtls_md_setup(&ctx, md_info, 0)) != 0) {
9292
0
        MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
9293
0
        goto exit;
9294
0
    }
9295
339
    if ((ret = mbedtls_md_starts(&ctx)) != 0) {
9296
0
        MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_starts", ret);
9297
0
        goto exit;
9298
0
    }
9299
339
    if ((ret = mbedtls_md_update(&ctx, ssl->handshake->randbytes, 64)) != 0) {
9300
0
        MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
9301
0
        goto exit;
9302
0
    }
9303
339
    if ((ret = mbedtls_md_update(&ctx, data, data_len)) != 0) {
9304
0
        MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
9305
0
        goto exit;
9306
0
    }
9307
339
    if ((ret = mbedtls_md_finish(&ctx, hash)) != 0) {
9308
0
        MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
9309
0
        goto exit;
9310
0
    }
9311
9312
339
exit:
9313
339
    mbedtls_md_free(&ctx);
9314
9315
339
    if (ret != 0) {
9316
0
        mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9317
0
                                       MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
9318
0
    }
9319
9320
339
    return ret;
9321
339
}
9322
#endif /* MBEDTLS_USE_PSA_CRYPTO */
9323
9324
#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
9325
9326
/* Find the preferred hash for a given signature algorithm. */
9327
unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
9328
    mbedtls_ssl_context *ssl,
9329
    unsigned int sig_alg)
9330
0
{
9331
0
    unsigned int i;
9332
0
    uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
9333
9334
0
    if (sig_alg == MBEDTLS_SSL_SIG_ANON) {
9335
0
        return MBEDTLS_SSL_HASH_NONE;
9336
0
    }
9337
9338
0
    for (i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) {
9339
0
        unsigned int hash_alg_received =
9340
0
            MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
9341
0
                received_sig_algs[i]);
9342
0
        unsigned int sig_alg_received =
9343
0
            MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
9344
0
                received_sig_algs[i]);
9345
9346
0
        mbedtls_md_type_t md_alg =
9347
0
            mbedtls_ssl_md_alg_from_hash((unsigned char) hash_alg_received);
9348
0
        if (md_alg == MBEDTLS_MD_NONE) {
9349
0
            continue;
9350
0
        }
9351
9352
0
        if (sig_alg == sig_alg_received) {
9353
#if defined(MBEDTLS_USE_PSA_CRYPTO)
9354
0
            if (ssl->handshake->key_cert && ssl->handshake->key_cert->key) {
9355
0
                psa_algorithm_t psa_hash_alg =
9356
0
                    mbedtls_md_psa_alg_from_type(md_alg);
9357
9358
0
                if (sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
9359
0
                    !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key,
9360
0
                                           PSA_ALG_ECDSA(psa_hash_alg),
9361
0
                                           PSA_KEY_USAGE_SIGN_HASH)) {
9362
0
                    continue;
9363
0
                }
9364
9365
0
                if (sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
9366
0
                    !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key,
9367
0
                                           PSA_ALG_RSA_PKCS1V15_SIGN(
9368
0
                                               psa_hash_alg),
9369
0
                                           PSA_KEY_USAGE_SIGN_HASH)) {
9370
0
                    continue;
9371
0
                }
9372
0
            }
9373
0
#endif /* MBEDTLS_USE_PSA_CRYPTO */
9374
9375
0
            return hash_alg_received;
9376
0
        }
9377
0
    }
9378
9379
0
    return MBEDTLS_SSL_HASH_NONE;
9380
0
}
Unexecuted instantiation: mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg
Unexecuted instantiation: mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg
9381
9382
#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
9383
9384
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9385
9386
int mbedtls_ssl_validate_ciphersuite(
9387
    const mbedtls_ssl_context *ssl,
9388
    const mbedtls_ssl_ciphersuite_t *suite_info,
9389
    mbedtls_ssl_protocol_version min_tls_version,
9390
    mbedtls_ssl_protocol_version max_tls_version)
9391
1.10M
{
9392
1.10M
    (void) ssl;
9393
9394
1.10M
    if (suite_info == NULL) {
9395
0
        return -1;
9396
0
    }
9397
9398
1.10M
    if ((suite_info->min_tls_version > max_tls_version) ||
9399
1.07M
        (suite_info->max_tls_version < min_tls_version)) {
9400
28.9k
        return -1;
9401
28.9k
    }
9402
9403
1.07M
#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
9404
1.07M
#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
9405
#if defined(MBEDTLS_USE_PSA_CRYPTO)
9406
    if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9407
        ssl->handshake->psa_pake_ctx_is_ok != 1)
9408
#else
9409
1.07M
    if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9410
5.80k
        mbedtls_ecjpake_check(&ssl->handshake->ecjpake_ctx) != 0)
9411
5.80k
#endif /* MBEDTLS_USE_PSA_CRYPTO */
9412
5.80k
    {
9413
5.80k
        return -1;
9414
5.80k
    }
9415
1.07M
#endif
9416
9417
    /* Don't suggest PSK-based ciphersuite if no PSK is available. */
9418
1.07M
#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
9419
1.07M
    if (mbedtls_ssl_ciphersuite_uses_psk(suite_info) &&
9420
429k
        mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) {
9421
428k
        return -1;
9422
428k
    }
9423
643k
#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
9424
643k
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9425
9426
643k
    return 0;
9427
1.07M
}
9428
9429
#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
9430
/*
9431
 * Function for writing a signature algorithm extension.
9432
 *
9433
 * The `extension_data` field of signature algorithm contains  a `SignatureSchemeList`
9434
 * value (TLS 1.3 RFC8446):
9435
 *      enum {
9436
 *         ....
9437
 *        ecdsa_secp256r1_sha256( 0x0403 ),
9438
 *        ecdsa_secp384r1_sha384( 0x0503 ),
9439
 *        ecdsa_secp521r1_sha512( 0x0603 ),
9440
 *         ....
9441
 *      } SignatureScheme;
9442
 *
9443
 *      struct {
9444
 *         SignatureScheme supported_signature_algorithms<2..2^16-2>;
9445
 *      } SignatureSchemeList;
9446
 *
9447
 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
9448
 * value (TLS 1.2 RFC5246):
9449
 *      enum {
9450
 *          none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
9451
 *          sha512(6), (255)
9452
 *      } HashAlgorithm;
9453
 *
9454
 *      enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
9455
 *        SignatureAlgorithm;
9456
 *
9457
 *      struct {
9458
 *          HashAlgorithm hash;
9459
 *          SignatureAlgorithm signature;
9460
 *      } SignatureAndHashAlgorithm;
9461
 *
9462
 *      SignatureAndHashAlgorithm
9463
 *        supported_signature_algorithms<2..2^16-2>;
9464
 *
9465
 * The TLS 1.3 signature algorithm extension was defined to be a compatible
9466
 * generalization of the TLS 1.2 signature algorithm extension.
9467
 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
9468
 * `SignatureScheme` field of TLS 1.3
9469
 *
9470
 */
9471
int mbedtls_ssl_write_sig_alg_ext(mbedtls_ssl_context *ssl, unsigned char *buf,
9472
                                  const unsigned char *end, size_t *out_len)
9473
5.78k
{
9474
5.78k
    unsigned char *p = buf;
9475
5.78k
    unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
9476
5.78k
    size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
9477
9478
5.78k
    *out_len = 0;
9479
9480
5.78k
    MBEDTLS_SSL_DEBUG_MSG(3, ("adding signature_algorithms extension"));
9481
9482
    /* Check if we have space for header and length field:
9483
     * - extension_type         (2 bytes)
9484
     * - extension_data_length  (2 bytes)
9485
     * - supported_signature_algorithms_length   (2 bytes)
9486
     */
9487
5.78k
    MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
9488
5.78k
    p += 6;
9489
9490
    /*
9491
     * Write supported_signature_algorithms
9492
     */
9493
5.78k
    supported_sig_alg = p;
9494
5.78k
    const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs(ssl);
9495
5.78k
    if (sig_alg == NULL) {
9496
0
        return MBEDTLS_ERR_SSL_BAD_CONFIG;
9497
0
    }
9498
9499
57.8k
    for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
9500
52.0k
        MBEDTLS_SSL_DEBUG_MSG(3, ("got signature scheme [%x] %s",
9501
52.0k
                                  *sig_alg,
9502
52.0k
                                  mbedtls_ssl_sig_alg_to_str(*sig_alg)));
9503
52.0k
        if (!mbedtls_ssl_sig_alg_is_supported(ssl, *sig_alg)) {
9504
17.3k
            continue;
9505
17.3k
        }
9506
34.6k
        MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
9507
34.6k
        MBEDTLS_PUT_UINT16_BE(*sig_alg, p, 0);
9508
34.6k
        p += 2;
9509
34.6k
        MBEDTLS_SSL_DEBUG_MSG(3, ("sent signature scheme [%x] %s",
9510
34.6k
                                  *sig_alg,
9511
34.6k
                                  mbedtls_ssl_sig_alg_to_str(*sig_alg)));
9512
34.6k
    }
9513
9514
    /* Length of supported_signature_algorithms */
9515
5.78k
    supported_sig_alg_len = (size_t) (p - supported_sig_alg);
9516
5.78k
    if (supported_sig_alg_len == 0) {
9517
0
        MBEDTLS_SSL_DEBUG_MSG(1, ("No signature algorithms defined."));
9518
0
        return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
9519
0
    }
9520
9521
5.78k
    MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SIG_ALG, buf, 0);
9522
5.78k
    MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len + 2, buf, 2);
9523
5.78k
    MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len, buf, 4);
9524
9525
5.78k
    *out_len = (size_t) (p - buf);
9526
9527
5.78k
#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
9528
5.78k
    mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_SIG_ALG);
9529
5.78k
#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
9530
9531
5.78k
    return 0;
9532
5.78k
}
9533
#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
9534
9535
#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
9536
/*
9537
 * mbedtls_ssl_parse_server_name_ext
9538
 *
9539
 * Structure of server_name extension:
9540
 *
9541
 *  enum {
9542
 *        host_name(0), (255)
9543
 *     } NameType;
9544
 *  opaque HostName<1..2^16-1>;
9545
 *
9546
 *  struct {
9547
 *          NameType name_type;
9548
 *          select (name_type) {
9549
 *             case host_name: HostName;
9550
 *           } name;
9551
 *     } ServerName;
9552
 *  struct {
9553
 *          ServerName server_name_list<1..2^16-1>
9554
 *     } ServerNameList;
9555
 */
9556
MBEDTLS_CHECK_RETURN_CRITICAL
9557
int mbedtls_ssl_parse_server_name_ext(mbedtls_ssl_context *ssl,
9558
                                      const unsigned char *buf,
9559
                                      const unsigned char *end)
9560
2.03k
{
9561
2.03k
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
9562
2.03k
    const unsigned char *p = buf;
9563
2.03k
    size_t server_name_list_len, hostname_len;
9564
2.03k
    const unsigned char *server_name_list_end;
9565
9566
2.03k
    MBEDTLS_SSL_DEBUG_MSG(3, ("parse ServerName extension"));
9567
9568
2.03k
    MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
9569
2.00k
    server_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
9570
2.00k
    p += 2;
9571
9572
2.00k
    MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, server_name_list_len);
9573
1.96k
    server_name_list_end = p + server_name_list_len;
9574
4.08k
    while (p < server_name_list_end) {
9575
2.66k
        MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end, 3);
9576
2.65k
        hostname_len = MBEDTLS_GET_UINT16_BE(p, 1);
9577
2.65k
        MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end,
9578
2.65k
                                     hostname_len + 3);
9579
9580
2.58k
        if (p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME) {
9581
            /* sni_name is intended to be used only during the parsing of the
9582
             * ClientHello message (it is reset to NULL before the end of
9583
             * the message parsing). Thus it is ok to just point to the
9584
             * reception buffer and not make a copy of it.
9585
             */
9586
465
            ssl->handshake->sni_name = p + 3;
9587
465
            ssl->handshake->sni_name_len = hostname_len;
9588
465
            if (ssl->conf->f_sni == NULL) {
9589
465
                return 0;
9590
465
            }
9591
0
            ret = ssl->conf->f_sni(ssl->conf->p_sni,
9592
0
                                   ssl, p + 3, hostname_len);
9593
0
            if (ret != 0) {
9594
0
                MBEDTLS_SSL_DEBUG_RET(1, "ssl_sni_wrapper", ret);
9595
0
                MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
9596
0
                                             MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME);
9597
0
                return MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME;
9598
0
            }
9599
0
            return 0;
9600
0
        }
9601
9602
2.11k
        p += hostname_len + 3;
9603
2.11k
    }
9604
9605
1.41k
    return 0;
9606
1.96k
}
9607
#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
9608
9609
#if defined(MBEDTLS_SSL_ALPN)
9610
MBEDTLS_CHECK_RETURN_CRITICAL
9611
int mbedtls_ssl_parse_alpn_ext(mbedtls_ssl_context *ssl,
9612
                               const unsigned char *buf,
9613
                               const unsigned char *end)
9614
1.13k
{
9615
1.13k
    const unsigned char *p = buf;
9616
1.13k
    size_t protocol_name_list_len;
9617
1.13k
    const unsigned char *protocol_name_list;
9618
1.13k
    const unsigned char *protocol_name_list_end;
9619
1.13k
    size_t protocol_name_len;
9620
9621
    /* If ALPN not configured, just ignore the extension */
9622
1.13k
    if (ssl->conf->alpn_list == NULL) {
9623
899
        return 0;
9624
899
    }
9625
9626
    /*
9627
     * RFC7301, section 3.1
9628
     *      opaque ProtocolName<1..2^8-1>;
9629
     *
9630
     *      struct {
9631
     *          ProtocolName protocol_name_list<2..2^16-1>
9632
     *      } ProtocolNameList;
9633
     */
9634
9635
    /*
9636
     * protocol_name_list_len    2 bytes
9637
     * protocol_name_len         1 bytes
9638
     * protocol_name             >=1 byte
9639
     */
9640
236
    MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 4);
9641
9642
233
    protocol_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
9643
233
    p += 2;
9644
233
    MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, protocol_name_list_len);
9645
219
    protocol_name_list = p;
9646
219
    protocol_name_list_end = p + protocol_name_list_len;
9647
9648
    /* Validate peer's list (lengths) */
9649
1.64k
    while (p < protocol_name_list_end) {
9650
1.44k
        protocol_name_len = *p++;
9651
1.44k
        MBEDTLS_SSL_CHK_BUF_READ_PTR(p, protocol_name_list_end,
9652
1.44k
                                     protocol_name_len);
9653
1.43k
        if (protocol_name_len == 0) {
9654
6
            MBEDTLS_SSL_PEND_FATAL_ALERT(
9655
6
                MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
9656
6
                MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
9657
6
            return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
9658
6
        }
9659
9660
1.43k
        p += protocol_name_len;
9661
1.43k
    }
9662
9663
    /* Use our order of preference */
9664
466
    for (const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++) {
9665
403
        size_t const alpn_len = strlen(*alpn);
9666
403
        p = protocol_name_list;
9667
2.92k
        while (p < protocol_name_list_end) {
9668
2.66k
            protocol_name_len = *p++;
9669
2.66k
            if (protocol_name_len == alpn_len &&
9670
626
                memcmp(p, *alpn, alpn_len) == 0) {
9671
146
                ssl->alpn_chosen = *alpn;
9672
146
                return 0;
9673
146
            }
9674
9675
2.52k
            p += protocol_name_len;
9676
2.52k
        }
9677
403
    }
9678
9679
    /* If we get here, no match was found */
9680
63
    MBEDTLS_SSL_PEND_FATAL_ALERT(
9681
63
        MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
9682
63
        MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL);
9683
63
    return MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL;
9684
209
}
9685
9686
int mbedtls_ssl_write_alpn_ext(mbedtls_ssl_context *ssl,
9687
                               unsigned char *buf,
9688
                               unsigned char *end,
9689
                               size_t *out_len)
9690
277
{
9691
277
    unsigned char *p = buf;
9692
277
    size_t protocol_name_len;
9693
277
    *out_len = 0;
9694
9695
277
    if (ssl->alpn_chosen == NULL) {
9696
265
        return 0;
9697
265
    }
9698
9699
12
    protocol_name_len = strlen(ssl->alpn_chosen);
9700
12
    MBEDTLS_SSL_CHK_BUF_PTR(p, end, 7 + protocol_name_len);
9701
9702
12
    MBEDTLS_SSL_DEBUG_MSG(3, ("server side, adding alpn extension"));
9703
    /*
9704
     * 0 . 1    ext identifier
9705
     * 2 . 3    ext length
9706
     * 4 . 5    protocol list length
9707
     * 6 . 6    protocol name length
9708
     * 7 . 7+n  protocol name
9709
     */
9710
12
    MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ALPN, p, 0);
9711
9712
12
    *out_len = 7 + protocol_name_len;
9713
9714
12
    MBEDTLS_PUT_UINT16_BE(protocol_name_len + 3, p, 2);
9715
12
    MBEDTLS_PUT_UINT16_BE(protocol_name_len + 1, p, 4);
9716
    /* Note: the length of the chosen protocol has been checked to be less
9717
     * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
9718
     */
9719
12
    p[6] = MBEDTLS_BYTE_0(protocol_name_len);
9720
9721
12
    memcpy(p + 7, ssl->alpn_chosen, protocol_name_len);
9722
9723
12
#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
9724
12
    mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_ALPN);
9725
12
#endif
9726
9727
12
    return 0;
9728
12
}
9729
#endif /* MBEDTLS_SSL_ALPN */
9730
9731
#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
9732
    defined(MBEDTLS_SSL_SESSION_TICKETS) && \
9733
    defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
9734
    defined(MBEDTLS_SSL_CLI_C)
9735
int mbedtls_ssl_session_set_hostname(mbedtls_ssl_session *session,
9736
                                     const char *hostname)
9737
5.80k
{
9738
    /* Initialize to suppress unnecessary compiler warning */
9739
5.80k
    size_t hostname_len = 0;
9740
9741
    /* Check if new hostname is valid before
9742
     * making any change to current one */
9743
5.80k
    if (hostname != NULL) {
9744
5.79k
        hostname_len = strlen(hostname);
9745
9746
5.79k
        if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) {
9747
0
            return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9748
0
        }
9749
5.79k
    }
9750
9751
    /* Now it's clear that we will overwrite the old hostname,
9752
     * so we can free it safely */
9753
5.80k
    if (session->hostname != NULL) {
9754
472
        mbedtls_zeroize_and_free(session->hostname,
9755
472
                                 strlen(session->hostname));
9756
472
    }
9757
9758
    /* Passing NULL as hostname shall clear the old one */
9759
5.80k
    if (hostname == NULL) {
9760
11
        session->hostname = NULL;
9761
5.79k
    } else {
9762
5.79k
        session->hostname = mbedtls_calloc(1, hostname_len + 1);
9763
5.79k
        if (session->hostname == NULL) {
9764
0
            return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9765
0
        }
9766
9767
5.79k
        memcpy(session->hostname, hostname, hostname_len);
9768
5.79k
    }
9769
9770
5.80k
    return 0;
9771
5.80k
}
9772
#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
9773
          MBEDTLS_SSL_SESSION_TICKETS &&
9774
          MBEDTLS_SSL_SERVER_NAME_INDICATION &&
9775
          MBEDTLS_SSL_CLI_C */
9776
9777
#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_EARLY_DATA) && \
9778
    defined(MBEDTLS_SSL_ALPN)
9779
int mbedtls_ssl_session_set_ticket_alpn(mbedtls_ssl_session *session,
9780
                                        const char *alpn)
9781
0
{
9782
0
    size_t alpn_len = 0;
9783
9784
0
    if (alpn != NULL) {
9785
0
        alpn_len = strlen(alpn);
9786
9787
0
        if (alpn_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN) {
9788
0
            return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9789
0
        }
9790
0
    }
9791
9792
0
    if (session->ticket_alpn != NULL) {
9793
0
        mbedtls_zeroize_and_free(session->ticket_alpn,
9794
0
                                 strlen(session->ticket_alpn));
9795
0
        session->ticket_alpn = NULL;
9796
0
    }
9797
9798
0
    if (alpn != NULL) {
9799
0
        session->ticket_alpn = mbedtls_calloc(alpn_len + 1, 1);
9800
0
        if (session->ticket_alpn == NULL) {
9801
0
            return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9802
0
        }
9803
0
        memcpy(session->ticket_alpn, alpn, alpn_len);
9804
0
    }
9805
9806
0
    return 0;
9807
0
}
9808
#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_EARLY_DATA && MBEDTLS_SSL_ALPN */
9809
9810
/*
9811
 * The following functions are used by 1.2 and 1.3, client and server.
9812
 */
9813
#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
9814
int mbedtls_ssl_check_cert_usage(const mbedtls_x509_crt *cert,
9815
                                 const mbedtls_ssl_ciphersuite_t *ciphersuite,
9816
                                 int recv_endpoint,
9817
                                 mbedtls_ssl_protocol_version tls_version,
9818
                                 uint32_t *flags)
9819
0
{
9820
0
    int ret = 0;
9821
0
    unsigned int usage = 0;
9822
0
    const char *ext_oid;
9823
0
    size_t ext_len;
9824
9825
    /*
9826
     * keyUsage
9827
     */
9828
9829
    /* Note: don't guard this with MBEDTLS_SSL_CLI_C because the server wants
9830
     * to check what a compliant client will think while choosing which cert
9831
     * to send to the client. */
9832
0
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9833
0
    if (tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
9834
0
        recv_endpoint == MBEDTLS_SSL_IS_CLIENT) {
9835
        /* TLS 1.2 server part of the key exchange */
9836
0
        switch (ciphersuite->key_exchange) {
9837
0
            case MBEDTLS_KEY_EXCHANGE_RSA:
9838
0
            case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
9839
0
                usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
9840
0
                break;
9841
9842
0
            case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
9843
0
            case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
9844
0
            case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
9845
0
                usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
9846
0
                break;
9847
9848
0
            case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
9849
0
            case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
9850
0
                usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
9851
0
                break;
9852
9853
            /* Don't use default: we want warnings when adding new values */
9854
0
            case MBEDTLS_KEY_EXCHANGE_NONE:
9855
0
            case MBEDTLS_KEY_EXCHANGE_PSK:
9856
0
            case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
9857
0
            case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
9858
0
            case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
9859
0
                usage = 0;
9860
0
        }
9861
0
    } else
9862
0
#endif
9863
0
    {
9864
        /* This is either TLS 1.3 authentication, which always uses signatures,
9865
         * or 1.2 client auth: rsa_sign and mbedtls_ecdsa_sign are the only
9866
         * options we implement, both using signatures. */
9867
0
        (void) tls_version;
9868
0
        (void) ciphersuite;
9869
0
        usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
9870
0
    }
9871
9872
0
    if (mbedtls_x509_crt_check_key_usage(cert, usage) != 0) {
9873
0
        *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
9874
0
        ret = -1;
9875
0
    }
9876
9877
    /*
9878
     * extKeyUsage
9879
     */
9880
9881
0
    if (recv_endpoint == MBEDTLS_SSL_IS_CLIENT) {
9882
0
        ext_oid = MBEDTLS_OID_SERVER_AUTH;
9883
0
        ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH);
9884
0
    } else {
9885
0
        ext_oid = MBEDTLS_OID_CLIENT_AUTH;
9886
0
        ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_CLIENT_AUTH);
9887
0
    }
9888
9889
0
    if (mbedtls_x509_crt_check_extended_key_usage(cert, ext_oid, ext_len) != 0) {
9890
0
        *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
9891
0
        ret = -1;
9892
0
    }
9893
9894
0
    return ret;
9895
0
}
9896
9897
static int get_hostname_for_verification(mbedtls_ssl_context *ssl,
9898
                                         const char **hostname)
9899
0
{
9900
0
    if (!mbedtls_ssl_has_set_hostname_been_called(ssl)) {
9901
0
        MBEDTLS_SSL_DEBUG_MSG(1, ("Certificate verification without having set hostname"));
9902
#if !defined(MBEDTLS_SSL_CLI_ALLOW_WEAK_CERTIFICATE_VERIFICATION_WITHOUT_HOSTNAME)
9903
        if (mbedtls_ssl_conf_get_endpoint(ssl->conf) == MBEDTLS_SSL_IS_CLIENT &&
9904
            ssl->conf->authmode == MBEDTLS_SSL_VERIFY_REQUIRED) {
9905
            return MBEDTLS_ERR_SSL_CERTIFICATE_VERIFICATION_WITHOUT_HOSTNAME;
9906
        }
9907
#endif
9908
0
    }
9909
9910
0
    *hostname = mbedtls_ssl_get_hostname_pointer(ssl);
9911
0
    if (*hostname == NULL) {
9912
0
        MBEDTLS_SSL_DEBUG_MSG(2, ("Certificate verification without CN verification"));
9913
0
    }
9914
9915
0
    return 0;
9916
0
}
9917
9918
int mbedtls_ssl_verify_certificate(mbedtls_ssl_context *ssl,
9919
                                   int authmode,
9920
                                   mbedtls_x509_crt *chain,
9921
                                   const mbedtls_ssl_ciphersuite_t *ciphersuite_info,
9922
                                   void *rs_ctx)
9923
2.02k
{
9924
2.02k
    if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
9925
2.02k
        ssl->session_negotiate->verify_result = 0;
9926
2.02k
        return 0;
9927
2.02k
    }
9928
9929
    /*
9930
     * Primary check: use the appropriate X.509 verification function
9931
     */
9932
0
    int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
9933
0
    void *p_vrfy;
9934
0
    if (ssl->f_vrfy != NULL) {
9935
0
        MBEDTLS_SSL_DEBUG_MSG(3, ("Use context-specific verification callback"));
9936
0
        f_vrfy = ssl->f_vrfy;
9937
0
        p_vrfy = ssl->p_vrfy;
9938
0
    } else {
9939
0
        MBEDTLS_SSL_DEBUG_MSG(3, ("Use configuration-specific verification callback"));
9940
0
        f_vrfy = ssl->conf->f_vrfy;
9941
0
        p_vrfy = ssl->conf->p_vrfy;
9942
0
    }
9943
9944
0
    const char *hostname = "";
9945
0
    int ret = get_hostname_for_verification(ssl, &hostname);
9946
0
    if (ret != 0) {
9947
0
        MBEDTLS_SSL_DEBUG_RET(1, "get_hostname_for_verification", ret);
9948
0
        return ret;
9949
0
    }
9950
9951
0
    int have_ca_chain_or_callback = 0;
9952
0
#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
9953
0
    if (ssl->conf->f_ca_cb != NULL) {
9954
0
        ((void) rs_ctx);
9955
0
        have_ca_chain_or_callback = 1;
9956
9957
0
        MBEDTLS_SSL_DEBUG_MSG(3, ("use CA callback for X.509 CRT verification"));
9958
0
        ret = mbedtls_x509_crt_verify_with_ca_cb(
9959
0
            chain,
9960
0
            ssl->conf->f_ca_cb,
9961
0
            ssl->conf->p_ca_cb,
9962
0
            ssl->conf->cert_profile,
9963
0
            hostname,
9964
0
            &ssl->session_negotiate->verify_result,
9965
0
            f_vrfy, p_vrfy);
9966
0
    } else
9967
0
#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
9968
0
    {
9969
0
        mbedtls_x509_crt *ca_chain;
9970
0
        mbedtls_x509_crl *ca_crl;
9971
0
#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
9972
0
        if (ssl->handshake->sni_ca_chain != NULL) {
9973
0
            ca_chain = ssl->handshake->sni_ca_chain;
9974
0
            ca_crl   = ssl->handshake->sni_ca_crl;
9975
0
        } else
9976
0
#endif
9977
0
        {
9978
0
            ca_chain = ssl->conf->ca_chain;
9979
0
            ca_crl   = ssl->conf->ca_crl;
9980
0
        }
9981
9982
0
        if (ca_chain != NULL) {
9983
0
            have_ca_chain_or_callback = 1;
9984
0
        }
9985
9986
0
        ret = mbedtls_x509_crt_verify_restartable(
9987
0
            chain,
9988
0
            ca_chain, ca_crl,
9989
0
            ssl->conf->cert_profile,
9990
0
            hostname,
9991
0
            &ssl->session_negotiate->verify_result,
9992
0
            f_vrfy, p_vrfy, rs_ctx);
9993
0
    }
9994
9995
0
    if (ret != 0) {
9996
0
        MBEDTLS_SSL_DEBUG_RET(1, "x509_verify_cert", ret);
9997
0
    }
9998
9999
0
#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
10000
0
    if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
10001
0
        return MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
10002
0
    }
10003
0
#endif
10004
10005
    /*
10006
     * Secondary checks: always done, but change 'ret' only if it was 0
10007
     */
10008
10009
    /* With TLS 1.2 and ECC certs, check that the curve used by the
10010
     * certificate is on our list of acceptable curves.
10011
     *
10012
     * With TLS 1.3 this is not needed because the curve is part of the
10013
     * signature algorithm (eg ecdsa_secp256r1_sha256) which is checked when
10014
     * we validate the signature made with the key associated to this cert.
10015
     */
10016
0
#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10017
0
    defined(MBEDTLS_PK_HAVE_ECC_KEYS)
10018
0
    if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
10019
0
        mbedtls_pk_can_do(&chain->pk, MBEDTLS_PK_ECKEY)) {
10020
0
        if (mbedtls_ssl_check_curve(ssl, mbedtls_pk_get_ec_group_id(&chain->pk)) != 0) {
10021
0
            MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (EC key curve)"));
10022
0
            ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
10023
0
            if (ret == 0) {
10024
0
                ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
10025
0
            }
10026
0
        }
10027
0
    }
10028
0
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_PK_HAVE_ECC_KEYS */
10029
10030
    /* Check X.509 usage extensions (keyUsage, extKeyUsage) */
10031
0
    if (mbedtls_ssl_check_cert_usage(chain,
10032
0
                                     ciphersuite_info,
10033
0
                                     ssl->conf->endpoint,
10034
0
                                     ssl->tls_version,
10035
0
                                     &ssl->session_negotiate->verify_result) != 0) {
10036
0
        MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (usage extensions)"));
10037
0
        if (ret == 0) {
10038
0
            ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
10039
0
        }
10040
0
    }
10041
10042
    /* With authmode optional, we want to keep going if the certificate was
10043
     * unacceptable, but still fail on other errors (out of memory etc),
10044
     * including fatal errors from the f_vrfy callback.
10045
     *
10046
     * The only acceptable errors are:
10047
     * - MBEDTLS_ERR_X509_CERT_VERIFY_FAILED: cert rejected by primary check;
10048
     * - MBEDTLS_ERR_SSL_BAD_CERTIFICATE: cert rejected by secondary checks.
10049
     * Anything else is a fatal error. */
10050
0
    if (authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
10051
0
        (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
10052
0
         ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE)) {
10053
0
        ret = 0;
10054
0
    }
10055
10056
    /* Return a specific error as this is a user error: inconsistent
10057
     * configuration - can't verify without trust anchors. */
10058
0
    if (have_ca_chain_or_callback == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED) {
10059
0
        MBEDTLS_SSL_DEBUG_MSG(1, ("got no CA chain"));
10060
0
        ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
10061
0
    }
10062
10063
0
    if (ret != 0) {
10064
0
        uint8_t alert;
10065
10066
        /* The certificate may have been rejected for several reasons.
10067
           Pick one and send the corresponding alert. Which alert to send
10068
           may be a subject of debate in some cases. */
10069
0
        if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER) {
10070
0
            alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
10071
0
        } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH) {
10072
0
            alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
10073
0
        } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE) {
10074
0
            alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
10075
0
        } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE) {
10076
0
            alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
10077
0
        } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK) {
10078
0
            alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
10079
0
        } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY) {
10080
0
            alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
10081
0
        } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED) {
10082
0
            alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
10083
0
        } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED) {
10084
0
            alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
10085
0
        } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED) {
10086
0
            alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
10087
0
        } else {
10088
0
            alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
10089
0
        }
10090
0
        mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10091
0
                                       alert);
10092
0
    }
10093
10094
0
#if defined(MBEDTLS_DEBUG_C)
10095
0
    if (ssl->session_negotiate->verify_result != 0) {
10096
0
        MBEDTLS_SSL_DEBUG_MSG(3, ("! Certificate verification flags %08x",
10097
0
                                  (unsigned int) ssl->session_negotiate->verify_result));
10098
0
    } else {
10099
0
        MBEDTLS_SSL_DEBUG_MSG(3, ("Certificate verification flags clear"));
10100
0
    }
10101
0
#endif /* MBEDTLS_DEBUG_C */
10102
10103
0
    return ret;
10104
0
}
10105
#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
10106
10107
#if defined(MBEDTLS_SSL_KEYING_MATERIAL_EXPORT)
10108
10109
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10110
static int mbedtls_ssl_tls12_export_keying_material(const mbedtls_ssl_context *ssl,
10111
                                                    const mbedtls_md_type_t hash_alg,
10112
                                                    uint8_t *out,
10113
                                                    const size_t key_len,
10114
                                                    const char *label,
10115
                                                    const size_t label_len,
10116
                                                    const unsigned char *context,
10117
                                                    const size_t context_len,
10118
                                                    const int use_context)
10119
0
{
10120
0
    int ret = 0;
10121
0
    unsigned char *prf_input = NULL;
10122
10123
    /* The input to the PRF is client_random, then server_random.
10124
     * If a context is provided, this is then followed by the context length
10125
     * as a 16-bit big-endian integer, and then the context itself. */
10126
0
    const size_t randbytes_len = MBEDTLS_CLIENT_HELLO_RANDOM_LEN + MBEDTLS_SERVER_HELLO_RANDOM_LEN;
10127
0
    size_t prf_input_len = randbytes_len;
10128
0
    if (use_context) {
10129
0
        if (context_len > UINT16_MAX) {
10130
0
            return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
10131
0
        }
10132
10133
        /* This does not overflow a 32-bit size_t because the current value of
10134
         * prf_input_len is 64 (length of client_random + server_random) and
10135
         * context_len fits into two bytes (checked above). */
10136
0
        prf_input_len += sizeof(uint16_t) + context_len;
10137
0
    }
10138
10139
0
    prf_input = mbedtls_calloc(prf_input_len, sizeof(unsigned char));
10140
0
    if (prf_input == NULL) {
10141
0
        return MBEDTLS_ERR_SSL_ALLOC_FAILED;
10142
0
    }
10143
10144
0
    memcpy(prf_input,
10145
0
           ssl->transform->randbytes + MBEDTLS_SERVER_HELLO_RANDOM_LEN,
10146
0
           MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
10147
0
    memcpy(prf_input + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
10148
0
           ssl->transform->randbytes,
10149
0
           MBEDTLS_SERVER_HELLO_RANDOM_LEN);
10150
0
    if (use_context) {
10151
0
        MBEDTLS_PUT_UINT16_BE(context_len, prf_input, randbytes_len);
10152
0
        memcpy(prf_input + randbytes_len + sizeof(uint16_t), context, context_len);
10153
0
    }
10154
0
    ret = tls_prf_generic(hash_alg, ssl->session->master, sizeof(ssl->session->master),
10155
0
                          label, label_len,
10156
0
                          prf_input, prf_input_len,
10157
0
                          out, key_len);
10158
0
    mbedtls_free(prf_input);
10159
0
    return ret;
10160
0
}
10161
#endif /* defined(MBEDTLS_SSL_PROTO_TLS1_2) */
10162
10163
#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
10164
static int mbedtls_ssl_tls13_export_keying_material(mbedtls_ssl_context *ssl,
10165
                                                    const mbedtls_md_type_t hash_alg,
10166
                                                    uint8_t *out,
10167
                                                    const size_t key_len,
10168
                                                    const char *label,
10169
                                                    const size_t label_len,
10170
                                                    const unsigned char *context,
10171
                                                    const size_t context_len)
10172
0
{
10173
0
    const psa_algorithm_t psa_hash_alg = mbedtls_md_psa_alg_from_type(hash_alg);
10174
0
    const size_t hash_len = PSA_HASH_LENGTH(psa_hash_alg);
10175
0
    const unsigned char *secret = ssl->session->app_secrets.exporter_master_secret;
10176
10177
    /* The length of the label must be at most 249 bytes to fit into the HkdfLabel
10178
     * struct as defined in RFC 8446, Section 7.1.
10179
     *
10180
     * The length of the context is unlimited even though the context field in the
10181
     * struct can only hold up to 255 bytes. This is because we place a *hash* of
10182
     * the context in the field. */
10183
0
    if (label_len > 249) {
10184
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
10185
0
    }
10186
10187
0
    return mbedtls_ssl_tls13_exporter(psa_hash_alg, secret, hash_len,
10188
0
                                      (const unsigned char *) label, label_len,
10189
0
                                      context, context_len, out, key_len);
10190
0
}
10191
#endif /* defined(MBEDTLS_SSL_PROTO_TLS1_3) */
10192
10193
int mbedtls_ssl_export_keying_material(mbedtls_ssl_context *ssl,
10194
                                       uint8_t *out, const size_t key_len,
10195
                                       const char *label, const size_t label_len,
10196
                                       const unsigned char *context, const size_t context_len,
10197
                                       const int use_context)
10198
0
{
10199
0
    if (!mbedtls_ssl_is_handshake_over(ssl)) {
10200
        /* TODO: Change this to a more appropriate error code when one is available. */
10201
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
10202
0
    }
10203
10204
0
    if (key_len > MBEDTLS_SSL_EXPORT_MAX_KEY_LEN) {
10205
0
        return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
10206
0
    }
10207
10208
0
    int ciphersuite_id = mbedtls_ssl_get_ciphersuite_id_from_ssl(ssl);
10209
0
    const mbedtls_ssl_ciphersuite_t *ciphersuite = mbedtls_ssl_ciphersuite_from_id(ciphersuite_id);
10210
0
    const mbedtls_md_type_t hash_alg = ciphersuite->mac;
10211
10212
0
    switch (mbedtls_ssl_get_version_number(ssl)) {
10213
0
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10214
0
        case MBEDTLS_SSL_VERSION_TLS1_2:
10215
0
            return mbedtls_ssl_tls12_export_keying_material(ssl, hash_alg, out, key_len,
10216
0
                                                            label, label_len,
10217
0
                                                            context, context_len, use_context);
10218
0
#endif
10219
0
#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
10220
0
        case MBEDTLS_SSL_VERSION_TLS1_3:
10221
0
            return mbedtls_ssl_tls13_export_keying_material(ssl,
10222
0
                                                            hash_alg,
10223
0
                                                            out,
10224
0
                                                            key_len,
10225
0
                                                            label,
10226
0
                                                            label_len,
10227
0
                                                            use_context ? context : NULL,
10228
0
                                                            use_context ? context_len : 0);
10229
0
#endif
10230
0
        default:
10231
0
            return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
10232
0
    }
10233
0
}
10234
10235
#endif /* defined(MBEDTLS_SSL_KEYING_MATERIAL_EXPORT) */
10236
10237
#endif /* MBEDTLS_SSL_TLS_C */