Coverage Report

Created: 2024-07-15 06:09

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