Coverage Report

Created: 2023-11-19 06:42

/src/openthread/third_party/mbedtls/repo/library/ssl_tls.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 *  SSLv3/TLSv1 shared functions
3
 *
4
 *  Copyright The Mbed TLS Contributors
5
 *  SPDX-License-Identifier: Apache-2.0
6
 *
7
 *  Licensed under the Apache License, Version 2.0 (the "License"); you may
8
 *  not use this file except in compliance with the License.
9
 *  You may obtain a copy of the License at
10
 *
11
 *  http://www.apache.org/licenses/LICENSE-2.0
12
 *
13
 *  Unless required by applicable law or agreed to in writing, software
14
 *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15
 *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 *  See the License for the specific language governing permissions and
17
 *  limitations under the License.
18
 */
19
/*
20
 *  The SSL 3.0 specification was drafted by Netscape in 1996,
21
 *  and became an IETF standard in 1999.
22
 *
23
 *  http://wp.netscape.com/eng/ssl3/
24
 *  http://www.ietf.org/rfc/rfc2246.txt
25
 *  http://www.ietf.org/rfc/rfc4346.txt
26
 */
27
28
#include "common.h"
29
30
#if defined(MBEDTLS_SSL_TLS_C)
31
32
#if defined(MBEDTLS_PLATFORM_C)
33
#include "mbedtls/platform.h"
34
#else
35
#include <stdlib.h>
36
#define mbedtls_calloc    calloc
37
#define mbedtls_free      free
38
#endif
39
40
#include "mbedtls/ssl.h"
41
#include "mbedtls/ssl_internal.h"
42
#include "mbedtls/debug.h"
43
#include "mbedtls/error.h"
44
#include "mbedtls/platform_util.h"
45
#include "mbedtls/version.h"
46
#include "mbedtls/constant_time.h"
47
48
#include <string.h>
49
50
#if defined(MBEDTLS_USE_PSA_CRYPTO)
51
#include "mbedtls/psa_util.h"
52
#include "psa/crypto.h"
53
#endif
54
55
#if defined(MBEDTLS_X509_CRT_PARSE_C)
56
#include "mbedtls/oid.h"
57
#endif
58
59
#if defined(MBEDTLS_SSL_PROTO_DTLS)
60
61
#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
62
/* Top-level Connection ID API */
63
64
int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
65
                          size_t len,
66
                          int ignore_other_cid )
67
{
68
    if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
69
        return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
70
71
    if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
72
        ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
73
    {
74
        return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
75
    }
76
77
    conf->ignore_unexpected_cid = ignore_other_cid;
78
    conf->cid_len = len;
79
    return( 0 );
80
}
81
82
int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
83
                         int enable,
84
                         unsigned char const *own_cid,
85
                         size_t own_cid_len )
86
{
87
    if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
88
        return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
89
90
    ssl->negotiate_cid = enable;
91
    if( enable == MBEDTLS_SSL_CID_DISABLED )
92
    {
93
        MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
94
        return( 0 );
95
    }
96
    MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
97
    MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
98
99
    if( own_cid_len != ssl->conf->cid_len )
100
    {
101
        MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
102
                                    (unsigned) own_cid_len,
103
                                    (unsigned) ssl->conf->cid_len ) );
104
        return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
105
    }
106
107
    memcpy( ssl->own_cid, own_cid, own_cid_len );
108
    /* Truncation is not an issue here because
109
     * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
110
    ssl->own_cid_len = (uint8_t) own_cid_len;
111
112
    return( 0 );
113
}
114
115
int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
116
                     int *enabled,
117
                     unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
118
                     size_t *peer_cid_len )
119
{
120
    *enabled = MBEDTLS_SSL_CID_DISABLED;
121
122
    if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
123
        ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
124
    {
125
        return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
126
    }
127
128
    /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
129
     * were used, but client and server requested the empty CID.
130
     * This is indistinguishable from not using the CID extension
131
     * in the first place. */
132
    if( ssl->transform_in->in_cid_len  == 0 &&
133
        ssl->transform_in->out_cid_len == 0 )
134
    {
135
        return( 0 );
136
    }
137
138
    if( peer_cid_len != NULL )
139
    {
140
        *peer_cid_len = ssl->transform_in->out_cid_len;
141
        if( peer_cid != NULL )
142
        {
143
            memcpy( peer_cid, ssl->transform_in->out_cid,
144
                    ssl->transform_in->out_cid_len );
145
        }
146
    }
147
148
    *enabled = MBEDTLS_SSL_CID_ENABLED;
149
150
    return( 0 );
151
}
152
#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
153
154
#endif /* MBEDTLS_SSL_PROTO_DTLS */
155
156
#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
157
/*
158
 * Convert max_fragment_length codes to length.
159
 * RFC 6066 says:
160
 *    enum{
161
 *        2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
162
 *    } MaxFragmentLength;
163
 * and we add 0 -> extension unused
164
 */
165
static unsigned int ssl_mfl_code_to_length( int mfl )
166
0
{
167
0
    switch( mfl )
168
0
    {
169
0
    case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
170
0
        return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
171
0
    case MBEDTLS_SSL_MAX_FRAG_LEN_512:
172
0
        return 512;
173
0
    case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
174
0
        return 1024;
175
0
    case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
176
0
        return 2048;
177
0
    case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
178
0
        return 4096;
179
0
    default:
180
0
        return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
181
0
    }
182
0
}
183
#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
184
185
int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
186
                              const mbedtls_ssl_session *src )
187
0
{
188
0
    mbedtls_ssl_session_free( dst );
189
0
    memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
190
191
#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
192
    dst->ticket = NULL;
193
#endif
194
195
0
#if defined(MBEDTLS_X509_CRT_PARSE_C)
196
197
#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
198
    if( src->peer_cert != NULL )
199
    {
200
        int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
201
202
        dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
203
        if( dst->peer_cert == NULL )
204
            return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
205
206
        mbedtls_x509_crt_init( dst->peer_cert );
207
208
        if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
209
                                        src->peer_cert->raw.len ) ) != 0 )
210
        {
211
            mbedtls_free( dst->peer_cert );
212
            dst->peer_cert = NULL;
213
            return( ret );
214
        }
215
    }
216
#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
217
0
    if( src->peer_cert_digest != NULL )
218
0
    {
219
0
        dst->peer_cert_digest =
220
0
            mbedtls_calloc( 1, src->peer_cert_digest_len );
221
0
        if( dst->peer_cert_digest == NULL )
222
0
            return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
223
224
0
        memcpy( dst->peer_cert_digest, src->peer_cert_digest,
225
0
                src->peer_cert_digest_len );
226
0
        dst->peer_cert_digest_type = src->peer_cert_digest_type;
227
0
        dst->peer_cert_digest_len = src->peer_cert_digest_len;
228
0
    }
229
0
#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
230
231
0
#endif /* MBEDTLS_X509_CRT_PARSE_C */
232
233
#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
234
    if( src->ticket != NULL )
235
    {
236
        dst->ticket = mbedtls_calloc( 1, src->ticket_len );
237
        if( dst->ticket == NULL )
238
            return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
239
240
        memcpy( dst->ticket, src->ticket, src->ticket_len );
241
    }
242
#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
243
244
0
    return( 0 );
245
0
}
246
247
#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
248
static int resize_buffer( unsigned char **buffer, size_t len_new, size_t *len_old )
249
{
250
    unsigned char* resized_buffer = mbedtls_calloc( 1, len_new );
251
    if( resized_buffer == NULL )
252
        return -1;
253
254
    /* We want to copy len_new bytes when downsizing the buffer, and
255
     * len_old bytes when upsizing, so we choose the smaller of two sizes,
256
     * to fit one buffer into another. Size checks, ensuring that no data is
257
     * lost, are done outside of this function. */
258
    memcpy( resized_buffer, *buffer,
259
            ( len_new < *len_old ) ? len_new : *len_old );
260
    mbedtls_platform_zeroize( *buffer, *len_old );
261
    mbedtls_free( *buffer );
262
263
    *buffer = resized_buffer;
264
    *len_old = len_new;
265
266
    return 0;
267
}
268
269
static void handle_buffer_resizing( mbedtls_ssl_context *ssl, int downsizing,
270
                                    size_t in_buf_new_len,
271
                                    size_t out_buf_new_len )
272
{
273
    int modified = 0;
274
    size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0;
275
    size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0;
276
    if( ssl->in_buf != NULL )
277
    {
278
        written_in = ssl->in_msg - ssl->in_buf;
279
        iv_offset_in = ssl->in_iv - ssl->in_buf;
280
        len_offset_in = ssl->in_len - ssl->in_buf;
281
        if( downsizing ?
282
            ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len :
283
            ssl->in_buf_len < in_buf_new_len )
284
        {
285
            if( resize_buffer( &ssl->in_buf, in_buf_new_len, &ssl->in_buf_len ) != 0 )
286
            {
287
                MBEDTLS_SSL_DEBUG_MSG( 1, ( "input buffer resizing failed - out of memory" ) );
288
            }
289
            else
290
            {
291
                MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET,
292
                                            in_buf_new_len ) );
293
                modified = 1;
294
            }
295
        }
296
    }
297
298
    if( ssl->out_buf != NULL )
299
    {
300
        written_out = ssl->out_msg - ssl->out_buf;
301
        iv_offset_out = ssl->out_iv - ssl->out_buf;
302
        len_offset_out = ssl->out_len - ssl->out_buf;
303
        if( downsizing ?
304
            ssl->out_buf_len > out_buf_new_len && ssl->out_left < out_buf_new_len :
305
            ssl->out_buf_len < out_buf_new_len )
306
        {
307
            if( resize_buffer( &ssl->out_buf, out_buf_new_len, &ssl->out_buf_len ) != 0 )
308
            {
309
                MBEDTLS_SSL_DEBUG_MSG( 1, ( "output buffer resizing failed - out of memory" ) );
310
            }
311
            else
312
            {
313
                MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET,
314
                                            out_buf_new_len ) );
315
                modified = 1;
316
            }
317
        }
318
    }
319
    if( modified )
320
    {
321
        /* Update pointers here to avoid doing it twice. */
322
        mbedtls_ssl_reset_in_out_pointers( ssl );
323
        /* Fields below might not be properly updated with record
324
         * splitting or with CID, so they are manually updated here. */
325
        ssl->out_msg = ssl->out_buf + written_out;
326
        ssl->out_len = ssl->out_buf + len_offset_out;
327
        ssl->out_iv = ssl->out_buf + iv_offset_out;
328
329
        ssl->in_msg = ssl->in_buf + written_in;
330
        ssl->in_len = ssl->in_buf + len_offset_in;
331
        ssl->in_iv = ssl->in_buf + iv_offset_in;
332
    }
333
}
334
#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
335
336
/*
337
 * Key material generation
338
 */
339
#if defined(MBEDTLS_SSL_PROTO_SSL3)
340
static int ssl3_prf( const unsigned char *secret, size_t slen,
341
                     const char *label,
342
                     const unsigned char *random, size_t rlen,
343
                     unsigned char *dstbuf, size_t dlen )
344
{
345
    int ret = 0;
346
    size_t i;
347
    mbedtls_md5_context md5;
348
    mbedtls_sha1_context sha1;
349
    unsigned char padding[16];
350
    unsigned char sha1sum[20];
351
    ((void)label);
352
353
    mbedtls_md5_init(  &md5  );
354
    mbedtls_sha1_init( &sha1 );
355
356
    /*
357
     *  SSLv3:
358
     *    block =
359
     *      MD5( secret + SHA1( 'A'    + secret + random ) ) +
360
     *      MD5( secret + SHA1( 'BB'   + secret + random ) ) +
361
     *      MD5( secret + SHA1( 'CCC'  + secret + random ) ) +
362
     *      ...
363
     */
364
    for( i = 0; i < dlen / 16; i++ )
365
    {
366
        memset( padding, (unsigned char) ('A' + i), 1 + i );
367
368
        if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
369
            goto exit;
370
        if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
371
            goto exit;
372
        if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
373
            goto exit;
374
        if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
375
            goto exit;
376
        if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
377
            goto exit;
378
379
        if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
380
            goto exit;
381
        if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
382
            goto exit;
383
        if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
384
            goto exit;
385
        if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
386
            goto exit;
387
    }
388
389
exit:
390
    mbedtls_md5_free(  &md5  );
391
    mbedtls_sha1_free( &sha1 );
392
393
    mbedtls_platform_zeroize( padding, sizeof( padding ) );
394
    mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
395
396
    return( ret );
397
}
398
#endif /* MBEDTLS_SSL_PROTO_SSL3 */
399
400
#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
401
static int tls1_prf( 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
    size_t nb, hs;
407
    size_t i, j, k;
408
    const unsigned char *S1, *S2;
409
    unsigned char *tmp;
410
    size_t tmp_len = 0;
411
    unsigned char h_i[20];
412
    const mbedtls_md_info_t *md_info;
413
    mbedtls_md_context_t md_ctx;
414
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
415
416
    mbedtls_md_init( &md_ctx );
417
418
    tmp_len = 20 + strlen( label ) + rlen;
419
    tmp = mbedtls_calloc( 1, tmp_len );
420
    if( tmp == NULL )
421
    {
422
        ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
423
        goto exit;
424
    }
425
426
    hs = ( slen + 1 ) / 2;
427
    S1 = secret;
428
    S2 = secret + slen - hs;
429
430
    nb = strlen( label );
431
    memcpy( tmp + 20, label, nb );
432
    memcpy( tmp + 20 + nb, random, rlen );
433
    nb += rlen;
434
435
    /*
436
     * First compute P_md5(secret,label+random)[0..dlen]
437
     */
438
    if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
439
    {
440
        ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
441
        goto exit;
442
    }
443
444
    if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
445
    {
446
        goto exit;
447
    }
448
449
    ret = mbedtls_md_hmac_starts( &md_ctx, S1, hs );
450
    if( ret != 0 )
451
        goto exit;
452
    ret = mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
453
    if( ret != 0 )
454
        goto exit;
455
    ret = mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
456
    if( ret != 0 )
457
        goto exit;
458
459
    for( i = 0; i < dlen; i += 16 )
460
    {
461
        ret = mbedtls_md_hmac_reset ( &md_ctx );
462
        if( ret != 0 )
463
            goto exit;
464
        ret = mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
465
        if( ret != 0 )
466
            goto exit;
467
        ret = mbedtls_md_hmac_finish( &md_ctx, h_i );
468
        if( ret != 0 )
469
            goto exit;
470
471
        ret = mbedtls_md_hmac_reset ( &md_ctx );
472
        if( ret != 0 )
473
            goto exit;
474
        ret = mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
475
        if( ret != 0 )
476
            goto exit;
477
        ret = mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
478
        if( ret != 0 )
479
            goto exit;
480
481
        k = ( i + 16 > dlen ) ? dlen % 16 : 16;
482
483
        for( j = 0; j < k; j++ )
484
            dstbuf[i + j]  = h_i[j];
485
    }
486
487
    mbedtls_md_free( &md_ctx );
488
489
    /*
490
     * XOR out with P_sha1(secret,label+random)[0..dlen]
491
     */
492
    if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
493
    {
494
        ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
495
        goto exit;
496
    }
497
498
    if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
499
    {
500
        goto exit;
501
    }
502
503
    ret = mbedtls_md_hmac_starts( &md_ctx, S2, hs );
504
    if( ret != 0 )
505
        goto exit;
506
    ret = mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
507
    if( ret != 0 )
508
        goto exit;
509
    ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
510
    if( ret != 0 )
511
        goto exit;
512
513
    for( i = 0; i < dlen; i += 20 )
514
    {
515
        ret = mbedtls_md_hmac_reset ( &md_ctx );
516
        if( ret != 0 )
517
            goto exit;
518
        ret = mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
519
        if( ret != 0 )
520
            goto exit;
521
        ret = mbedtls_md_hmac_finish( &md_ctx, h_i );
522
        if( ret != 0 )
523
            goto exit;
524
525
        ret = mbedtls_md_hmac_reset ( &md_ctx );
526
        if( ret != 0 )
527
            goto exit;
528
        ret = mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
529
        if( ret != 0 )
530
            goto exit;
531
        ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
532
        if( ret != 0 )
533
            goto exit;
534
535
        k = ( i + 20 > dlen ) ? dlen % 20 : 20;
536
537
        for( j = 0; j < k; j++ )
538
            dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
539
    }
540
541
exit:
542
    mbedtls_md_free( &md_ctx );
543
544
    mbedtls_platform_zeroize( tmp, tmp_len );
545
    mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
546
547
    mbedtls_free( tmp );
548
    return( ret );
549
}
550
#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
551
552
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
553
#if defined(MBEDTLS_USE_PSA_CRYPTO)
554
555
static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation,
556
                                              psa_key_id_t key,
557
                                              psa_algorithm_t alg,
558
                                              const unsigned char* seed, size_t seed_length,
559
                                              const unsigned char* label, size_t label_length,
560
                                              size_t capacity )
561
{
562
    psa_status_t status;
563
564
    status = psa_key_derivation_setup( derivation, alg );
565
    if( status != PSA_SUCCESS )
566
        return( status );
567
568
    if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
569
    {
570
        status = psa_key_derivation_input_bytes( derivation,
571
                                                 PSA_KEY_DERIVATION_INPUT_SEED,
572
                                                 seed, seed_length );
573
        if( status != PSA_SUCCESS )
574
            return( status );
575
576
        if( mbedtls_svc_key_id_is_null( key ) )
577
        {
578
            status = psa_key_derivation_input_bytes(
579
                derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
580
                NULL, 0 );
581
        }
582
        else
583
        {
584
            status = psa_key_derivation_input_key(
585
                derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key );
586
        }
587
        if( status != PSA_SUCCESS )
588
            return( status );
589
590
        status = psa_key_derivation_input_bytes( derivation,
591
                                                 PSA_KEY_DERIVATION_INPUT_LABEL,
592
                                                 label, label_length );
593
        if( status != PSA_SUCCESS )
594
            return( status );
595
    }
596
    else
597
    {
598
        return( PSA_ERROR_NOT_SUPPORTED );
599
    }
600
601
    status = psa_key_derivation_set_capacity( derivation, capacity );
602
    if( status != PSA_SUCCESS )
603
        return( status );
604
605
    return( PSA_SUCCESS );
606
}
607
608
static int tls_prf_generic( mbedtls_md_type_t md_type,
609
                            const unsigned char *secret, size_t slen,
610
                            const char *label,
611
                            const unsigned char *random, size_t rlen,
612
                            unsigned char *dstbuf, size_t dlen )
613
{
614
    psa_status_t status;
615
    psa_algorithm_t alg;
616
    psa_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
617
    psa_key_derivation_operation_t derivation =
618
        PSA_KEY_DERIVATION_OPERATION_INIT;
619
620
    if( md_type == MBEDTLS_MD_SHA384 )
621
        alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
622
    else
623
        alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
624
625
    /* Normally a "secret" should be long enough to be impossible to
626
     * find by brute force, and in particular should not be empty. But
627
     * this PRF is also used to derive an IV, in particular in EAP-TLS,
628
     * and for this use case it makes sense to have a 0-length "secret".
629
     * Since the key API doesn't allow importing a key of length 0,
630
     * keep master_key=0, which setup_psa_key_derivation() understands
631
     * to mean a 0-length "secret" input. */
632
    if( slen != 0 )
633
    {
634
        psa_key_attributes_t key_attributes = psa_key_attributes_init();
635
        psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
636
        psa_set_key_algorithm( &key_attributes, alg );
637
        psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
638
639
        status = psa_import_key( &key_attributes, secret, slen, &master_key );
640
        if( status != PSA_SUCCESS )
641
            return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
642
    }
643
644
    status = setup_psa_key_derivation( &derivation,
645
                                       master_key, alg,
646
                                       random, rlen,
647
                                       (unsigned char const *) label,
648
                                       (size_t) strlen( label ),
649
                                       dlen );
650
    if( status != PSA_SUCCESS )
651
    {
652
        psa_key_derivation_abort( &derivation );
653
        psa_destroy_key( master_key );
654
        return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
655
    }
656
657
    status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen );
658
    if( status != PSA_SUCCESS )
659
    {
660
        psa_key_derivation_abort( &derivation );
661
        psa_destroy_key( master_key );
662
        return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
663
    }
664
665
    status = psa_key_derivation_abort( &derivation );
666
    if( status != PSA_SUCCESS )
667
    {
668
        psa_destroy_key( master_key );
669
        return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
670
    }
671
672
    if( ! mbedtls_svc_key_id_is_null( master_key ) )
673
        status = psa_destroy_key( master_key );
674
    if( status != PSA_SUCCESS )
675
        return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
676
677
    return( 0 );
678
}
679
680
#else /* MBEDTLS_USE_PSA_CRYPTO */
681
682
static int tls_prf_generic( mbedtls_md_type_t md_type,
683
                            const unsigned char *secret, size_t slen,
684
                            const char *label,
685
                            const unsigned char *random, size_t rlen,
686
                            unsigned char *dstbuf, size_t dlen )
687
0
{
688
0
    size_t nb;
689
0
    size_t i, j, k, md_len;
690
0
    unsigned char *tmp;
691
0
    size_t tmp_len = 0;
692
0
    unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
693
0
    const mbedtls_md_info_t *md_info;
694
0
    mbedtls_md_context_t md_ctx;
695
0
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
696
697
0
    mbedtls_md_init( &md_ctx );
698
699
0
    if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
700
0
        return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
701
702
0
    md_len = mbedtls_md_get_size( md_info );
703
704
0
    tmp_len = md_len + strlen( label ) + rlen;
705
0
    tmp = mbedtls_calloc( 1, tmp_len );
706
0
    if( tmp == NULL )
707
0
    {
708
0
        ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
709
0
        goto exit;
710
0
    }
711
712
0
    nb = strlen( label );
713
0
    memcpy( tmp + md_len, label, nb );
714
0
    memcpy( tmp + md_len + nb, random, rlen );
715
0
    nb += rlen;
716
717
    /*
718
     * Compute P_<hash>(secret, label + random)[0..dlen]
719
     */
720
0
    if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
721
0
        goto exit;
722
723
0
    ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen );
724
0
    if( ret != 0 )
725
0
        goto exit;
726
0
    ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
727
0
    if( ret != 0 )
728
0
        goto exit;
729
0
    ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
730
0
    if( ret != 0 )
731
0
        goto exit;
732
733
0
    for( i = 0; i < dlen; i += md_len )
734
0
    {
735
0
        ret = mbedtls_md_hmac_reset ( &md_ctx );
736
0
        if( ret != 0 )
737
0
            goto exit;
738
0
        ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
739
0
        if( ret != 0 )
740
0
            goto exit;
741
0
        ret = mbedtls_md_hmac_finish( &md_ctx, h_i );
742
0
        if( ret != 0 )
743
0
            goto exit;
744
745
0
        ret = mbedtls_md_hmac_reset ( &md_ctx );
746
0
        if( ret != 0 )
747
0
            goto exit;
748
0
        ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
749
0
        if( ret != 0 )
750
0
            goto exit;
751
0
        ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
752
0
        if( ret != 0 )
753
0
            goto exit;
754
755
0
        k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
756
757
0
        for( j = 0; j < k; j++ )
758
0
            dstbuf[i + j]  = h_i[j];
759
0
    }
760
761
0
exit:
762
0
    mbedtls_md_free( &md_ctx );
763
764
0
    mbedtls_platform_zeroize( tmp, tmp_len );
765
0
    mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
766
767
0
    mbedtls_free( tmp );
768
769
0
    return( ret );
770
0
}
771
#endif /* MBEDTLS_USE_PSA_CRYPTO */
772
#if defined(MBEDTLS_SHA256_C)
773
static int tls_prf_sha256( const unsigned char *secret, size_t slen,
774
                           const char *label,
775
                           const unsigned char *random, size_t rlen,
776
                           unsigned char *dstbuf, size_t dlen )
777
0
{
778
0
    return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
779
0
                             label, random, rlen, dstbuf, dlen ) );
780
0
}
781
#endif /* MBEDTLS_SHA256_C */
782
783
#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
784
static int tls_prf_sha384( const unsigned char *secret, size_t slen,
785
                           const char *label,
786
                           const unsigned char *random, size_t rlen,
787
                           unsigned char *dstbuf, size_t dlen )
788
{
789
    return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
790
                             label, random, rlen, dstbuf, dlen ) );
791
}
792
#endif /* MBEDTLS_SHA512_C && !MBEDTLS_SHA512_NO_SHA384 */
793
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
794
795
static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
796
797
#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
798
    defined(MBEDTLS_SSL_PROTO_TLS1_1)
799
static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
800
#endif
801
802
#if defined(MBEDTLS_SSL_PROTO_SSL3)
803
static void ssl_calc_verify_ssl( const mbedtls_ssl_context *, unsigned char *, size_t * );
804
static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
805
#endif
806
807
#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
808
static void ssl_calc_verify_tls( const mbedtls_ssl_context *, unsigned char*, size_t * );
809
static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
810
#endif
811
812
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
813
#if defined(MBEDTLS_SHA256_C)
814
static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
815
static void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *,unsigned char*, size_t * );
816
static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
817
#endif
818
819
#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
820
static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
821
static void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *, unsigned char*, size_t * );
822
static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
823
#endif
824
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
825
826
#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) && \
827
    defined(MBEDTLS_USE_PSA_CRYPTO)
828
static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
829
{
830
    if( ssl->conf->f_psk != NULL )
831
    {
832
        /* If we've used a callback to select the PSK,
833
         * the static configuration is irrelevant. */
834
        if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
835
            return( 1 );
836
837
        return( 0 );
838
    }
839
840
    if( ! mbedtls_svc_key_id_is_null( ssl->conf->psk_opaque ) )
841
        return( 1 );
842
843
    return( 0 );
844
}
845
#endif /* MBEDTLS_USE_PSA_CRYPTO &&
846
          MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
847
848
#if defined(MBEDTLS_SSL_EXPORT_KEYS)
849
static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
850
0
{
851
#if defined(MBEDTLS_SSL_PROTO_SSL3)
852
    if( tls_prf == ssl3_prf )
853
    {
854
        return( MBEDTLS_SSL_TLS_PRF_SSL3 );
855
    }
856
    else
857
#endif
858
#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
859
    if( tls_prf == tls1_prf )
860
    {
861
        return( MBEDTLS_SSL_TLS_PRF_TLS1 );
862
    }
863
    else
864
#endif
865
0
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
866
#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
867
    if( tls_prf == tls_prf_sha384 )
868
    {
869
        return( MBEDTLS_SSL_TLS_PRF_SHA384 );
870
    }
871
    else
872
#endif
873
0
#if defined(MBEDTLS_SHA256_C)
874
0
    if( tls_prf == tls_prf_sha256 )
875
0
    {
876
0
        return( MBEDTLS_SSL_TLS_PRF_SHA256 );
877
0
    }
878
0
    else
879
0
#endif
880
0
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
881
0
    return( MBEDTLS_SSL_TLS_PRF_NONE );
882
0
}
883
#endif /* MBEDTLS_SSL_EXPORT_KEYS */
884
885
int  mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf,
886
                          const unsigned char *secret, size_t slen,
887
                          const char *label,
888
                          const unsigned char *random, size_t rlen,
889
                          unsigned char *dstbuf, size_t dlen )
890
0
{
891
0
    mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
892
893
0
    switch( prf )
894
0
    {
895
#if defined(MBEDTLS_SSL_PROTO_SSL3)
896
        case MBEDTLS_SSL_TLS_PRF_SSL3:
897
            tls_prf = ssl3_prf;
898
        break;
899
#endif /* MBEDTLS_SSL_PROTO_SSL3 */
900
#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
901
        case MBEDTLS_SSL_TLS_PRF_TLS1:
902
            tls_prf = tls1_prf;
903
        break;
904
#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
905
906
0
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
907
#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
908
        case MBEDTLS_SSL_TLS_PRF_SHA384:
909
            tls_prf = tls_prf_sha384;
910
        break;
911
#endif /* MBEDTLS_SHA512_C && !MBEDTLS_SHA512_NO_SHA384 */
912
0
#if defined(MBEDTLS_SHA256_C)
913
0
        case MBEDTLS_SSL_TLS_PRF_SHA256:
914
0
            tls_prf = tls_prf_sha256;
915
0
        break;
916
0
#endif /* MBEDTLS_SHA256_C */
917
0
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
918
0
    default:
919
0
        return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
920
0
    }
921
922
0
    return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
923
0
}
924
925
/* Type for the TLS PRF */
926
typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
927
                          const unsigned char *, size_t,
928
                          unsigned char *, size_t);
929
930
/*
931
 * Populate a transform structure with session keys and all the other
932
 * necessary information.
933
 *
934
 * Parameters:
935
 * - [in/out]: transform: structure to populate
936
 *      [in] must be just initialised with mbedtls_ssl_transform_init()
937
 *      [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
938
 * - [in] ciphersuite
939
 * - [in] master
940
 * - [in] encrypt_then_mac
941
 * - [in] trunc_hmac
942
 * - [in] compression
943
 * - [in] tls_prf: pointer to PRF to use for key derivation
944
 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
945
 * - [in] minor_ver: SSL/TLS minor version
946
 * - [in] endpoint: client or server
947
 * - [in] ssl: optionally used for:
948
 *        - MBEDTLS_SSL_HW_RECORD_ACCEL: whole context (non-const)
949
 *        - MBEDTLS_SSL_EXPORT_KEYS: ssl->conf->{f,p}_export_keys
950
 *        - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
951
 */
952
static int ssl_populate_transform( mbedtls_ssl_transform *transform,
953
                                   int ciphersuite,
954
                                   const unsigned char master[48],
955
#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
956
#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
957
                                   int encrypt_then_mac,
958
#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
959
#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
960
                                   int trunc_hmac,
961
#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
962
#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
963
#if defined(MBEDTLS_ZLIB_SUPPORT)
964
                                   int compression,
965
#endif
966
                                   ssl_tls_prf_t tls_prf,
967
                                   const unsigned char randbytes[64],
968
                                   int minor_ver,
969
                                   unsigned endpoint,
970
#if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
971
                                   const
972
#endif
973
                                   mbedtls_ssl_context *ssl )
974
0
{
975
0
    int ret = 0;
976
#if defined(MBEDTLS_USE_PSA_CRYPTO)
977
    int psa_fallthrough;
978
#endif /* MBEDTLS_USE_PSA_CRYPTO */
979
0
    unsigned char keyblk[256];
980
0
    unsigned char *key1;
981
0
    unsigned char *key2;
982
0
    unsigned char *mac_enc;
983
0
    unsigned char *mac_dec;
984
0
    size_t mac_key_len = 0;
985
0
    size_t iv_copy_len;
986
0
    unsigned keylen;
987
0
    const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
988
0
    const mbedtls_cipher_info_t *cipher_info;
989
0
    const mbedtls_md_info_t *md_info;
990
991
#if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) && \
992
    !defined(MBEDTLS_SSL_EXPORT_KEYS) && \
993
    !defined(MBEDTLS_DEBUG_C)
994
    ssl = NULL; /* make sure we don't use it except for those cases */
995
    (void) ssl;
996
#endif
997
998
    /*
999
     * Some data just needs copying into the structure
1000
     */
1001
#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
1002
    defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1003
    transform->encrypt_then_mac = encrypt_then_mac;
1004
#endif
1005
0
    transform->minor_ver = minor_ver;
1006
1007
#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1008
    memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
1009
#endif
1010
1011
    /*
1012
     * Get various info structures
1013
     */
1014
0
    ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
1015
0
    if( ciphersuite_info == NULL )
1016
0
    {
1017
0
        MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
1018
0
                                    ciphersuite ) );
1019
0
        return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1020
0
    }
1021
1022
0
    cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
1023
0
    if( cipher_info == NULL )
1024
0
    {
1025
0
        MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found",
1026
0
                                    ciphersuite_info->cipher ) );
1027
0
        return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1028
0
    }
1029
1030
0
    md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
1031
0
    if( md_info == NULL )
1032
0
    {
1033
0
        MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found",
1034
0
                            (unsigned) ciphersuite_info->mac ) );
1035
0
        return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1036
0
    }
1037
1038
#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
1039
    /* Copy own and peer's CID if the use of the CID
1040
     * extension has been negotiated. */
1041
    if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
1042
    {
1043
        MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
1044
1045
        transform->in_cid_len = ssl->own_cid_len;
1046
        memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
1047
        MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
1048
                               transform->in_cid_len );
1049
1050
        transform->out_cid_len = ssl->handshake->peer_cid_len;
1051
        memcpy( transform->out_cid, ssl->handshake->peer_cid,
1052
                ssl->handshake->peer_cid_len );
1053
        MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
1054
                               transform->out_cid_len );
1055
    }
1056
#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
1057
1058
    /*
1059
     * Compute key block using the PRF
1060
     */
1061
0
    ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
1062
0
    if( ret != 0 )
1063
0
    {
1064
0
        MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1065
0
        return( ret );
1066
0
    }
1067
1068
0
    MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
1069
0
                           mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
1070
0
    MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
1071
0
    MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
1072
0
    MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
1073
1074
    /*
1075
     * Determine the appropriate key, IV and MAC length.
1076
     */
1077
1078
0
    keylen = cipher_info->key_bitlen / 8;
1079
1080
0
#if defined(MBEDTLS_GCM_C) ||                           \
1081
0
    defined(MBEDTLS_CCM_C) ||                           \
1082
0
    defined(MBEDTLS_CHACHAPOLY_C)
1083
0
    if( cipher_info->mode == MBEDTLS_MODE_GCM ||
1084
0
        cipher_info->mode == MBEDTLS_MODE_CCM ||
1085
0
        cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1086
0
    {
1087
0
        size_t explicit_ivlen;
1088
1089
0
        transform->maclen = 0;
1090
0
        mac_key_len = 0;
1091
0
        transform->taglen =
1092
0
            ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
1093
1094
        /* All modes haves 96-bit IVs, but the length of the static parts vary
1095
         * with mode and version:
1096
         * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
1097
         *   (to be concatenated with a dynamically chosen IV of 8 Bytes)
1098
         * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
1099
         *   a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
1100
         *   sequence number).
1101
         */
1102
0
        transform->ivlen = 12;
1103
#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
1104
        if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
1105
        {
1106
            transform->fixed_ivlen = 12;
1107
        }
1108
        else
1109
#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
1110
0
        {
1111
0
            if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1112
0
                transform->fixed_ivlen = 12;
1113
0
            else
1114
0
                transform->fixed_ivlen = 4;
1115
0
        }
1116
1117
        /* Minimum length of encrypted record */
1118
0
        explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
1119
0
        transform->minlen = explicit_ivlen + transform->taglen;
1120
0
    }
1121
0
    else
1122
0
#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1123
#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1124
    if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1125
        cipher_info->mode == MBEDTLS_MODE_CBC )
1126
    {
1127
        /* Initialize HMAC contexts */
1128
        if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1129
            ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
1130
        {
1131
            MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
1132
            goto end;
1133
        }
1134
1135
        /* Get MAC length */
1136
        mac_key_len = mbedtls_md_get_size( md_info );
1137
        transform->maclen = mac_key_len;
1138
1139
#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1140
        /*
1141
         * If HMAC is to be truncated, we shall keep the leftmost bytes,
1142
         * (rfc 6066 page 13 or rfc 2104 section 4),
1143
         * so we only need to adjust the length here.
1144
         */
1145
        if( trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
1146
        {
1147
            transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
1148
1149
#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1150
            /* Fall back to old, non-compliant version of the truncated
1151
             * HMAC implementation which also truncates the key
1152
             * (Mbed TLS versions from 1.3 to 2.6.0) */
1153
            mac_key_len = transform->maclen;
1154
#endif
1155
        }
1156
#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
1157
1158
        /* IV length */
1159
        transform->ivlen = cipher_info->iv_size;
1160
1161
        /* Minimum length */
1162
        if( cipher_info->mode == MBEDTLS_MODE_STREAM )
1163
            transform->minlen = transform->maclen;
1164
        else
1165
        {
1166
            /*
1167
             * GenericBlockCipher:
1168
             * 1. if EtM is in use: one block plus MAC
1169
             *    otherwise: * first multiple of blocklen greater than maclen
1170
             * 2. IV except for SSL3 and TLS 1.0
1171
             */
1172
#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1173
            if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
1174
            {
1175
                transform->minlen = transform->maclen
1176
                                  + cipher_info->block_size;
1177
            }
1178
            else
1179
#endif
1180
            {
1181
                transform->minlen = transform->maclen
1182
                                  + cipher_info->block_size
1183
                                  - transform->maclen % cipher_info->block_size;
1184
            }
1185
1186
#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1187
            if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1188
                minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
1189
                ; /* No need to adjust minlen */
1190
            else
1191
#endif
1192
#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1193
            if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1194
                minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
1195
            {
1196
                transform->minlen += transform->ivlen;
1197
            }
1198
            else
1199
#endif
1200
            {
1201
                MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1202
                ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1203
                goto end;
1204
            }
1205
        }
1206
    }
1207
    else
1208
#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1209
0
    {
1210
0
        MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1211
0
        return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1212
0
    }
1213
1214
0
    MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
1215
0
                                (unsigned) keylen,
1216
0
                                (unsigned) transform->minlen,
1217
0
                                (unsigned) transform->ivlen,
1218
0
                                (unsigned) transform->maclen ) );
1219
1220
    /*
1221
     * Finally setup the cipher contexts, IVs and MAC secrets.
1222
     */
1223
0
#if defined(MBEDTLS_SSL_CLI_C)
1224
0
    if( endpoint == MBEDTLS_SSL_IS_CLIENT )
1225
0
    {
1226
0
        key1 = keyblk + mac_key_len * 2;
1227
0
        key2 = keyblk + mac_key_len * 2 + keylen;
1228
1229
0
        mac_enc = keyblk;
1230
0
        mac_dec = keyblk + mac_key_len;
1231
1232
        /*
1233
         * This is not used in TLS v1.1.
1234
         */
1235
0
        iv_copy_len = ( transform->fixed_ivlen ) ?
1236
0
                            transform->fixed_ivlen : transform->ivlen;
1237
0
        memcpy( transform->iv_enc, key2 + keylen,  iv_copy_len );
1238
0
        memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
1239
0
                iv_copy_len );
1240
0
    }
1241
0
    else
1242
0
#endif /* MBEDTLS_SSL_CLI_C */
1243
0
#if defined(MBEDTLS_SSL_SRV_C)
1244
0
    if( endpoint == MBEDTLS_SSL_IS_SERVER )
1245
0
    {
1246
0
        key1 = keyblk + mac_key_len * 2 + keylen;
1247
0
        key2 = keyblk + mac_key_len * 2;
1248
1249
0
        mac_enc = keyblk + mac_key_len;
1250
0
        mac_dec = keyblk;
1251
1252
        /*
1253
         * This is not used in TLS v1.1.
1254
         */
1255
0
        iv_copy_len = ( transform->fixed_ivlen ) ?
1256
0
                            transform->fixed_ivlen : transform->ivlen;
1257
0
        memcpy( transform->iv_dec, key1 + keylen,  iv_copy_len );
1258
0
        memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
1259
0
                iv_copy_len );
1260
0
    }
1261
0
    else
1262
0
#endif /* MBEDTLS_SSL_SRV_C */
1263
0
    {
1264
0
        MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1265
0
        ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1266
0
        goto end;
1267
0
    }
1268
1269
#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1270
#if defined(MBEDTLS_SSL_PROTO_SSL3)
1271
    if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
1272
    {
1273
        if( mac_key_len > sizeof( transform->mac_enc ) )
1274
        {
1275
            MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1276
            ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1277
            goto end;
1278
        }
1279
1280
        memcpy( transform->mac_enc, mac_enc, mac_key_len );
1281
        memcpy( transform->mac_dec, mac_dec, mac_key_len );
1282
    }
1283
    else
1284
#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1285
#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1286
    defined(MBEDTLS_SSL_PROTO_TLS1_2)
1287
    if( minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
1288
    {
1289
        /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1290
           For AEAD-based ciphersuites, there is nothing to do here. */
1291
        if( mac_key_len != 0 )
1292
        {
1293
            ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc,
1294
                                          mac_enc, mac_key_len );
1295
            if( ret != 0 )
1296
                goto end;
1297
            ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec,
1298
                                          mac_dec, mac_key_len );
1299
            if( ret != 0 )
1300
                goto end;
1301
        }
1302
    }
1303
    else
1304
#endif
1305
    {
1306
        MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1307
        ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1308
        goto end;
1309
    }
1310
#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1311
1312
#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1313
    if( mbedtls_ssl_hw_record_init != NULL )
1314
    {
1315
        ret = 0;
1316
1317
        MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
1318
1319
        if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
1320
                                        transform->iv_enc, transform->iv_dec,
1321
                                        iv_copy_len,
1322
                                        mac_enc, mac_dec,
1323
                                        mac_key_len ) ) != 0 )
1324
        {
1325
            MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1326
            ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1327
            goto end;
1328
        }
1329
    }
1330
#else
1331
0
    ((void) mac_dec);
1332
0
    ((void) mac_enc);
1333
0
#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
1334
1335
0
#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1336
0
    if( ssl->conf->f_export_keys != NULL )
1337
0
    {
1338
0
        ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1339
0
                                  master, keyblk,
1340
0
                                  mac_key_len, keylen,
1341
0
                                  iv_copy_len );
1342
0
    }
1343
1344
0
    if( ssl->conf->f_export_keys_ext != NULL )
1345
0
    {
1346
0
        ssl->conf->f_export_keys_ext( ssl->conf->p_export_keys,
1347
0
                                      master, keyblk,
1348
0
                                      mac_key_len, keylen,
1349
0
                                      iv_copy_len,
1350
0
                                      randbytes + 32,
1351
0
                                      randbytes,
1352
0
                                      tls_prf_get_type( tls_prf ) );
1353
0
    }
1354
0
#endif
1355
1356
#if defined(MBEDTLS_USE_PSA_CRYPTO)
1357
1358
    /* Only use PSA-based ciphers for TLS-1.2.
1359
     * That's relevant at least for TLS-1.0, where
1360
     * we assume that mbedtls_cipher_crypt() updates
1361
     * the structure field for the IV, which the PSA-based
1362
     * implementation currently doesn't. */
1363
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1364
    if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
1365
    {
1366
        ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc,
1367
                                        cipher_info, transform->taglen );
1368
        if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1369
        {
1370
            MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
1371
            goto end;
1372
        }
1373
1374
        if( ret == 0 )
1375
        {
1376
            MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based encryption cipher context" ) );
1377
            psa_fallthrough = 0;
1378
        }
1379
        else
1380
        {
1381
            MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) );
1382
            psa_fallthrough = 1;
1383
        }
1384
    }
1385
    else
1386
        psa_fallthrough = 1;
1387
#else
1388
    psa_fallthrough = 1;
1389
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1390
1391
    if( psa_fallthrough == 1 )
1392
#endif /* MBEDTLS_USE_PSA_CRYPTO */
1393
0
    if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
1394
0
                                 cipher_info ) ) != 0 )
1395
0
    {
1396
0
        MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
1397
0
        goto end;
1398
0
    }
1399
1400
#if defined(MBEDTLS_USE_PSA_CRYPTO)
1401
    /* Only use PSA-based ciphers for TLS-1.2.
1402
     * That's relevant at least for TLS-1.0, where
1403
     * we assume that mbedtls_cipher_crypt() updates
1404
     * the structure field for the IV, which the PSA-based
1405
     * implementation currently doesn't. */
1406
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1407
    if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
1408
    {
1409
        ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec,
1410
                                        cipher_info, transform->taglen );
1411
        if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1412
        {
1413
            MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
1414
            goto end;
1415
        }
1416
1417
        if( ret == 0 )
1418
        {
1419
            MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based decryption cipher context" ) );
1420
            psa_fallthrough = 0;
1421
        }
1422
        else
1423
        {
1424
            MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) );
1425
            psa_fallthrough = 1;
1426
        }
1427
    }
1428
    else
1429
        psa_fallthrough = 1;
1430
#else
1431
    psa_fallthrough = 1;
1432
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1433
1434
    if( psa_fallthrough == 1 )
1435
#endif /* MBEDTLS_USE_PSA_CRYPTO */
1436
0
    if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
1437
0
                                 cipher_info ) ) != 0 )
1438
0
    {
1439
0
        MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
1440
0
        goto end;
1441
0
    }
1442
1443
0
    if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
1444
0
                               cipher_info->key_bitlen,
1445
0
                               MBEDTLS_ENCRYPT ) ) != 0 )
1446
0
    {
1447
0
        MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
1448
0
        goto end;
1449
0
    }
1450
1451
0
    if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
1452
0
                               cipher_info->key_bitlen,
1453
0
                               MBEDTLS_DECRYPT ) ) != 0 )
1454
0
    {
1455
0
        MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
1456
0
        goto end;
1457
0
    }
1458
1459
#if defined(MBEDTLS_CIPHER_MODE_CBC)
1460
    if( cipher_info->mode == MBEDTLS_MODE_CBC )
1461
    {
1462
        if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1463
                                             MBEDTLS_PADDING_NONE ) ) != 0 )
1464
        {
1465
            MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
1466
            goto end;
1467
        }
1468
1469
        if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1470
                                             MBEDTLS_PADDING_NONE ) ) != 0 )
1471
        {
1472
            MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
1473
            goto end;
1474
        }
1475
    }
1476
#endif /* MBEDTLS_CIPHER_MODE_CBC */
1477
1478
1479
    /* Initialize Zlib contexts */
1480
#if defined(MBEDTLS_ZLIB_SUPPORT)
1481
    if( compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
1482
    {
1483
        MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
1484
1485
        memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1486
        memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
1487
1488
        if( deflateInit( &transform->ctx_deflate,
1489
                         Z_DEFAULT_COMPRESSION )   != Z_OK ||
1490
            inflateInit( &transform->ctx_inflate ) != Z_OK )
1491
        {
1492
            MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1493
            ret = MBEDTLS_ERR_SSL_COMPRESSION_FAILED;
1494
            goto end;
1495
        }
1496
    }
1497
#endif /* MBEDTLS_ZLIB_SUPPORT */
1498
1499
0
end:
1500
0
    mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
1501
0
    return( ret );
1502
0
}
1503
1504
/*
1505
 * Set appropriate PRF function and other SSL / TLS 1.0/1.1 / TLS1.2 functions
1506
 *
1507
 * Inputs:
1508
 * - SSL/TLS minor version
1509
 * - hash associated with the ciphersuite (only used by TLS 1.2)
1510
 *
1511
 * Outputs:
1512
 * - the tls_prf, calc_verify and calc_finished members of handshake structure
1513
 */
1514
static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
1515
                                   int minor_ver,
1516
                                   mbedtls_md_type_t hash )
1517
0
{
1518
0
#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) ||       \
1519
0
    !( defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384) )
1520
0
    (void) hash;
1521
0
#endif
1522
1523
#if defined(MBEDTLS_SSL_PROTO_SSL3)
1524
    if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
1525
    {
1526
        handshake->tls_prf = ssl3_prf;
1527
        handshake->calc_verify = ssl_calc_verify_ssl;
1528
        handshake->calc_finished = ssl_calc_finished_ssl;
1529
    }
1530
    else
1531
#endif
1532
#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1533
    if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
1534
    {
1535
        handshake->tls_prf = tls1_prf;
1536
        handshake->calc_verify = ssl_calc_verify_tls;
1537
        handshake->calc_finished = ssl_calc_finished_tls;
1538
    }
1539
    else
1540
#endif
1541
0
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1542
#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
1543
    if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1544
        hash == MBEDTLS_MD_SHA384 )
1545
    {
1546
        handshake->tls_prf = tls_prf_sha384;
1547
        handshake->calc_verify = ssl_calc_verify_tls_sha384;
1548
        handshake->calc_finished = ssl_calc_finished_tls_sha384;
1549
    }
1550
    else
1551
#endif
1552
0
#if defined(MBEDTLS_SHA256_C)
1553
0
    if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
1554
0
    {
1555
0
        handshake->tls_prf = tls_prf_sha256;
1556
0
        handshake->calc_verify = ssl_calc_verify_tls_sha256;
1557
0
        handshake->calc_finished = ssl_calc_finished_tls_sha256;
1558
0
    }
1559
0
    else
1560
0
#endif
1561
0
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1562
0
    {
1563
0
        return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1564
0
    }
1565
1566
0
    return( 0 );
1567
0
}
1568
1569
/*
1570
 * Compute master secret if needed
1571
 *
1572
 * Parameters:
1573
 * [in/out] handshake
1574
 *          [in] resume, premaster, extended_ms, calc_verify, tls_prf
1575
 *               (PSA-PSK) ciphersuite_info, psk_opaque
1576
 *          [out] premaster (cleared)
1577
 * [out] master
1578
 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
1579
 *      debug: conf->f_dbg, conf->p_dbg
1580
 *      EMS: passed to calc_verify (debug + (SSL3) session_negotiate)
1581
 *      PSA-PSA: minor_ver, conf
1582
 */
1583
static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
1584
                               unsigned char *master,
1585
                               const mbedtls_ssl_context *ssl )
1586
0
{
1587
0
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1588
1589
    /* cf. RFC 5246, Section 8.1:
1590
     * "The master secret is always exactly 48 bytes in length." */
1591
0
    size_t const master_secret_len = 48;
1592
1593
#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
1594
    unsigned char session_hash[48];
1595
#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
1596
1597
    /* The label for the KDF used for key expansion.
1598
     * This is either "master secret" or "extended master secret"
1599
     * depending on whether the Extended Master Secret extension
1600
     * is used. */
1601
0
    char const *lbl = "master secret";
1602
1603
    /* The salt for the KDF used for key expansion.
1604
     * - If the Extended Master Secret extension is not used,
1605
     *   this is ClientHello.Random + ServerHello.Random
1606
     *   (see Sect. 8.1 in RFC 5246).
1607
     * - If the Extended Master Secret extension is used,
1608
     *   this is the transcript of the handshake so far.
1609
     *   (see Sect. 4 in RFC 7627). */
1610
0
    unsigned char const *salt = handshake->randbytes;
1611
0
    size_t salt_len = 64;
1612
1613
0
#if !defined(MBEDTLS_DEBUG_C) &&                    \
1614
0
    !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
1615
0
    !(defined(MBEDTLS_USE_PSA_CRYPTO) &&            \
1616
0
      defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
1617
0
    ssl = NULL; /* make sure we don't use it except for those cases */
1618
0
    (void) ssl;
1619
0
#endif
1620
1621
0
    if( handshake->resume != 0 )
1622
0
    {
1623
0
        MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1624
0
        return( 0 );
1625
0
    }
1626
1627
#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
1628
    if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
1629
    {
1630
        lbl  = "extended master secret";
1631
        salt = session_hash;
1632
        handshake->calc_verify( ssl, session_hash, &salt_len );
1633
1634
        MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
1635
                                  session_hash, salt_len );
1636
    }
1637
#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
1638
1639
#if defined(MBEDTLS_USE_PSA_CRYPTO) &&          \
1640
    defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1641
    if( handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
1642
        ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1643
        ssl_use_opaque_psk( ssl ) == 1 )
1644
    {
1645
        /* Perform PSK-to-MS expansion in a single step. */
1646
        psa_status_t status;
1647
        psa_algorithm_t alg;
1648
        psa_key_id_t psk;
1649
        psa_key_derivation_operation_t derivation =
1650
            PSA_KEY_DERIVATION_OPERATION_INIT;
1651
        mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
1652
1653
        MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
1654
1655
        psk = mbedtls_ssl_get_opaque_psk( ssl );
1656
1657
        if( hash_alg == MBEDTLS_MD_SHA384 )
1658
            alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
1659
        else
1660
            alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
1661
1662
        status = setup_psa_key_derivation( &derivation, psk, alg,
1663
                                           salt, salt_len,
1664
                                           (unsigned char const *) lbl,
1665
                                           (size_t) strlen( lbl ),
1666
                                           master_secret_len );
1667
        if( status != PSA_SUCCESS )
1668
        {
1669
            psa_key_derivation_abort( &derivation );
1670
            return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1671
        }
1672
1673
        status = psa_key_derivation_output_bytes( &derivation,
1674
                                                  master,
1675
                                                  master_secret_len );
1676
        if( status != PSA_SUCCESS )
1677
        {
1678
            psa_key_derivation_abort( &derivation );
1679
            return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1680
        }
1681
1682
        status = psa_key_derivation_abort( &derivation );
1683
        if( status != PSA_SUCCESS )
1684
            return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1685
    }
1686
    else
1687
#endif
1688
0
    {
1689
0
        ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
1690
0
                                  lbl, salt, salt_len,
1691
0
                                  master,
1692
0
                                  master_secret_len );
1693
0
        if( ret != 0 )
1694
0
        {
1695
0
            MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1696
0
            return( ret );
1697
0
        }
1698
1699
0
        MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
1700
0
                               handshake->premaster,
1701
0
                               handshake->pmslen );
1702
1703
0
        mbedtls_platform_zeroize( handshake->premaster,
1704
0
                                  sizeof(handshake->premaster) );
1705
0
    }
1706
1707
0
    return( 0 );
1708
0
}
1709
1710
int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
1711
0
{
1712
0
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1713
0
    const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
1714
0
        ssl->handshake->ciphersuite_info;
1715
1716
0
    MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
1717
1718
    /* Set PRF, calc_verify and calc_finished function pointers */
1719
0
    ret = ssl_set_handshake_prfs( ssl->handshake,
1720
0
                                  ssl->minor_ver,
1721
0
                                  ciphersuite_info->mac );
1722
0
    if( ret != 0 )
1723
0
    {
1724
0
        MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
1725
0
        return( ret );
1726
0
    }
1727
1728
    /* Compute master secret if needed */
1729
0
    ret = ssl_compute_master( ssl->handshake,
1730
0
                              ssl->session_negotiate->master,
1731
0
                              ssl );
1732
0
    if( ret != 0 )
1733
0
    {
1734
0
        MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
1735
0
        return( ret );
1736
0
    }
1737
1738
    /* Swap the client and server random values:
1739
     * - MS derivation wanted client+server (RFC 5246 8.1)
1740
     * - key derivation wants server+client (RFC 5246 6.3) */
1741
0
    {
1742
0
        unsigned char tmp[64];
1743
0
        memcpy( tmp, ssl->handshake->randbytes, 64 );
1744
0
        memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
1745
0
        memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
1746
0
        mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
1747
0
    }
1748
1749
    /* Populate transform structure */
1750
0
    ret = ssl_populate_transform( ssl->transform_negotiate,
1751
0
                                  ssl->session_negotiate->ciphersuite,
1752
0
                                  ssl->session_negotiate->master,
1753
#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1754
#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1755
                                  ssl->session_negotiate->encrypt_then_mac,
1756
#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
1757
#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1758
                                  ssl->session_negotiate->trunc_hmac,
1759
#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
1760
#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1761
#if defined(MBEDTLS_ZLIB_SUPPORT)
1762
                                  ssl->session_negotiate->compression,
1763
#endif
1764
0
                                  ssl->handshake->tls_prf,
1765
0
                                  ssl->handshake->randbytes,
1766
0
                                  ssl->minor_ver,
1767
0
                                  ssl->conf->endpoint,
1768
0
                                  ssl );
1769
0
    if( ret != 0 )
1770
0
    {
1771
0
        MBEDTLS_SSL_DEBUG_RET( 1, "ssl_populate_transform", ret );
1772
0
        return( ret );
1773
0
    }
1774
1775
    /* We no longer need Server/ClientHello.random values */
1776
0
    mbedtls_platform_zeroize( ssl->handshake->randbytes,
1777
0
                      sizeof( ssl->handshake->randbytes ) );
1778
1779
    /* Allocate compression buffer */
1780
#if defined(MBEDTLS_ZLIB_SUPPORT)
1781
    if( ssl->session_negotiate->compression == MBEDTLS_SSL_COMPRESS_DEFLATE &&
1782
        ssl->compress_buf == NULL )
1783
    {
1784
        MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
1785
        ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
1786
        if( ssl->compress_buf == NULL )
1787
        {
1788
            MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
1789
                                        MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
1790
            return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1791
        }
1792
    }
1793
#endif
1794
1795
0
    MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
1796
1797
0
    return( 0 );
1798
0
}
1799
1800
#if defined(MBEDTLS_SSL_PROTO_SSL3)
1801
void ssl_calc_verify_ssl( const mbedtls_ssl_context *ssl,
1802
                          unsigned char *hash,
1803
                          size_t *hlen )
1804
{
1805
    mbedtls_md5_context md5;
1806
    mbedtls_sha1_context sha1;
1807
    unsigned char pad_1[48];
1808
    unsigned char pad_2[48];
1809
1810
    MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
1811
1812
    mbedtls_md5_init( &md5 );
1813
    mbedtls_sha1_init( &sha1 );
1814
1815
    mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1816
    mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
1817
1818
    memset( pad_1, 0x36, 48 );
1819
    memset( pad_2, 0x5C, 48 );
1820
1821
    mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1822
    mbedtls_md5_update_ret( &md5, pad_1, 48 );
1823
    mbedtls_md5_finish_ret( &md5, hash );
1824
1825
    mbedtls_md5_starts_ret( &md5 );
1826
    mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1827
    mbedtls_md5_update_ret( &md5, pad_2, 48 );
1828
    mbedtls_md5_update_ret( &md5, hash,  16 );
1829
    mbedtls_md5_finish_ret( &md5, hash );
1830
1831
    mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1832
    mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1833
    mbedtls_sha1_finish_ret( &sha1, hash + 16 );
1834
1835
    mbedtls_sha1_starts_ret( &sha1 );
1836
    mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1837
    mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1838
    mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1839
    mbedtls_sha1_finish_ret( &sha1, hash + 16 );
1840
1841
    *hlen = 36;
1842
1843
    MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1844
    MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1845
1846
    mbedtls_md5_free(  &md5  );
1847
    mbedtls_sha1_free( &sha1 );
1848
1849
    return;
1850
}
1851
#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1852
1853
#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1854
void ssl_calc_verify_tls( const mbedtls_ssl_context *ssl,
1855
                          unsigned char *hash,
1856
                          size_t *hlen )
1857
{
1858
    mbedtls_md5_context md5;
1859
    mbedtls_sha1_context sha1;
1860
1861
    MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
1862
1863
    mbedtls_md5_init( &md5 );
1864
    mbedtls_sha1_init( &sha1 );
1865
1866
    mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1867
    mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
1868
1869
    mbedtls_md5_finish_ret( &md5,  hash );
1870
    mbedtls_sha1_finish_ret( &sha1, hash + 16 );
1871
1872
    *hlen = 36;
1873
1874
    MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1875
    MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1876
1877
    mbedtls_md5_free(  &md5  );
1878
    mbedtls_sha1_free( &sha1 );
1879
1880
    return;
1881
}
1882
#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
1883
1884
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1885
#if defined(MBEDTLS_SHA256_C)
1886
void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
1887
                                 unsigned char *hash,
1888
                                 size_t *hlen )
1889
0
{
1890
#if defined(MBEDTLS_USE_PSA_CRYPTO)
1891
    size_t hash_size;
1892
    psa_status_t status;
1893
    psa_hash_operation_t sha256_psa = psa_hash_operation_init();
1894
1895
    MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
1896
    status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
1897
    if( status != PSA_SUCCESS )
1898
    {
1899
        MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1900
        return;
1901
    }
1902
1903
    status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
1904
    if( status != PSA_SUCCESS )
1905
    {
1906
        MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1907
        return;
1908
    }
1909
1910
    *hlen = 32;
1911
    MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
1912
    MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1913
#else
1914
0
    mbedtls_sha256_context sha256;
1915
1916
0
    mbedtls_sha256_init( &sha256 );
1917
1918
0
    MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
1919
1920
0
    mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
1921
0
    mbedtls_sha256_finish_ret( &sha256, hash );
1922
1923
0
    *hlen = 32;
1924
1925
0
    MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1926
0
    MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1927
1928
0
    mbedtls_sha256_free( &sha256 );
1929
0
#endif /* MBEDTLS_USE_PSA_CRYPTO */
1930
0
    return;
1931
0
}
1932
#endif /* MBEDTLS_SHA256_C */
1933
1934
#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
1935
void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
1936
                                 unsigned char *hash,
1937
                                 size_t *hlen )
1938
{
1939
#if defined(MBEDTLS_USE_PSA_CRYPTO)
1940
    size_t hash_size;
1941
    psa_status_t status;
1942
    psa_hash_operation_t sha384_psa = psa_hash_operation_init();
1943
1944
    MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
1945
    status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
1946
    if( status != PSA_SUCCESS )
1947
    {
1948
        MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1949
        return;
1950
    }
1951
1952
    status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
1953
    if( status != PSA_SUCCESS )
1954
    {
1955
        MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1956
        return;
1957
    }
1958
1959
    *hlen = 48;
1960
    MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
1961
    MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1962
#else
1963
    mbedtls_sha512_context sha512;
1964
1965
    mbedtls_sha512_init( &sha512 );
1966
1967
    MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
1968
1969
    mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
1970
    mbedtls_sha512_finish_ret( &sha512, hash );
1971
1972
    *hlen = 48;
1973
1974
    MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1975
    MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1976
1977
    mbedtls_sha512_free( &sha512 );
1978
#endif /* MBEDTLS_USE_PSA_CRYPTO */
1979
    return;
1980
}
1981
#endif /* MBEDTLS_SHA512_C && !MBEDTLS_SHA512_NO_SHA384 */
1982
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1983
1984
#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
1985
int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
1986
0
{
1987
0
    unsigned char *p = ssl->handshake->premaster;
1988
0
    unsigned char *end = p + sizeof( ssl->handshake->premaster );
1989
0
    const unsigned char *psk = NULL;
1990
0
    size_t psk_len = 0;
1991
1992
0
    if( mbedtls_ssl_get_psk( ssl, &psk, &psk_len )
1993
0
            == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED )
1994
0
    {
1995
        /*
1996
         * This should never happen because the existence of a PSK is always
1997
         * checked before calling this function
1998
         */
1999
0
        MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2000
0
        return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2001
0
    }
2002
2003
    /*
2004
     * PMS = struct {
2005
     *     opaque other_secret<0..2^16-1>;
2006
     *     opaque psk<0..2^16-1>;
2007
     * };
2008
     * with "other_secret" depending on the particular key exchange
2009
     */
2010
0
#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
2011
0
    if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
2012
0
    {
2013
0
        if( end - p < 2 )
2014
0
            return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2015
2016
0
        MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
2017
0
        p += 2;
2018
2019
0
        if( end < p || (size_t)( end - p ) < psk_len )
2020
0
            return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2021
2022
0
        memset( p, 0, psk_len );
2023
0
        p += psk_len;
2024
0
    }
2025
0
    else
2026
0
#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
2027
#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
2028
    if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
2029
    {
2030
        /*
2031
         * other_secret already set by the ClientKeyExchange message,
2032
         * and is 48 bytes long
2033
         */
2034
        if( end - p < 2 )
2035
            return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2036
2037
        *p++ = 0;
2038
        *p++ = 48;
2039
        p += 48;
2040
    }
2041
    else
2042
#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2043
#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
2044
    if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
2045
    {
2046
        int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2047
        size_t len;
2048
2049
        /* Write length only when we know the actual value */
2050
        if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
2051
                                      p + 2, end - ( p + 2 ), &len,
2052
                                      ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
2053
        {
2054
            MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
2055
            return( ret );
2056
        }
2057
        MBEDTLS_PUT_UINT16_BE( len, p, 0 );
2058
        p += 2 + len;
2059
2060
        MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K  );
2061
    }
2062
    else
2063
#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
2064
#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2065
    if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
2066
    {
2067
        int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2068
        size_t zlen;
2069
2070
        if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
2071
                                       p + 2, end - ( p + 2 ),
2072
                                       ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
2073
        {
2074
            MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
2075
            return( ret );
2076
        }
2077
2078
        MBEDTLS_PUT_UINT16_BE( zlen, p, 0 );
2079
        p += 2 + zlen;
2080
2081
        MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
2082
                                MBEDTLS_DEBUG_ECDH_Z );
2083
    }
2084
    else
2085
#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
2086
0
    {
2087
0
        MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2088
0
        return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2089
0
    }
2090
2091
    /* opaque psk<0..2^16-1>; */
2092
0
    if( end - p < 2 )
2093
0
        return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2094
2095
0
    MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
2096
0
    p += 2;
2097
2098
0
    if( end < p || (size_t)( end - p ) < psk_len )
2099
0
        return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2100
2101
0
    memcpy( p, psk, psk_len );
2102
0
    p += psk_len;
2103
2104
0
    ssl->handshake->pmslen = p - ssl->handshake->premaster;
2105
2106
0
    return( 0 );
2107
0
}
2108
#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
2109
2110
#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2111
static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
2112
2113
#if defined(MBEDTLS_SSL_PROTO_DTLS)
2114
int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl )
2115
{
2116
    /* If renegotiation is not enforced, retransmit until we would reach max
2117
     * timeout if we were using the usual handshake doubling scheme */
2118
    if( ssl->conf->renego_max_records < 0 )
2119
    {
2120
        uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
2121
        unsigned char doublings = 1;
2122
2123
        while( ratio != 0 )
2124
        {
2125
            ++doublings;
2126
            ratio >>= 1;
2127
        }
2128
2129
        if( ++ssl->renego_records_seen > doublings )
2130
        {
2131
            MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
2132
            return( 0 );
2133
        }
2134
    }
2135
2136
    return( ssl_write_hello_request( ssl ) );
2137
}
2138
#endif
2139
#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
2140
2141
#if defined(MBEDTLS_X509_CRT_PARSE_C)
2142
static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
2143
4.60k
{
2144
#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2145
    if( session->peer_cert != NULL )
2146
    {
2147
        mbedtls_x509_crt_free( session->peer_cert );
2148
        mbedtls_free( session->peer_cert );
2149
        session->peer_cert = NULL;
2150
    }
2151
#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2152
4.60k
    if( session->peer_cert_digest != NULL )
2153
0
    {
2154
        /* Zeroization is not necessary. */
2155
0
        mbedtls_free( session->peer_cert_digest );
2156
0
        session->peer_cert_digest      = NULL;
2157
0
        session->peer_cert_digest_type = MBEDTLS_MD_NONE;
2158
0
        session->peer_cert_digest_len  = 0;
2159
0
    }
2160
4.60k
#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2161
4.60k
}
2162
#endif /* MBEDTLS_X509_CRT_PARSE_C */
2163
2164
/*
2165
 * Handshake functions
2166
 */
2167
#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
2168
/* No certificate support -> dummy functions */
2169
int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
2170
{
2171
    const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
2172
        ssl->handshake->ciphersuite_info;
2173
2174
    MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2175
2176
    if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
2177
    {
2178
        MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2179
        ssl->state++;
2180
        return( 0 );
2181
    }
2182
2183
    MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2184
    return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2185
}
2186
2187
int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
2188
{
2189
    const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
2190
        ssl->handshake->ciphersuite_info;
2191
2192
    MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2193
2194
    if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
2195
    {
2196
        MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2197
        ssl->state++;
2198
        return( 0 );
2199
    }
2200
2201
    MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2202
    return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2203
}
2204
2205
#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
2206
/* Some certificate support -> implement write and parse */
2207
2208
int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
2209
0
{
2210
0
    int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
2211
0
    size_t i, n;
2212
0
    const mbedtls_x509_crt *crt;
2213
0
    const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
2214
0
        ssl->handshake->ciphersuite_info;
2215
2216
0
    MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2217
2218
0
    if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
2219
0
    {
2220
0
        MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2221
0
        ssl->state++;
2222
0
        return( 0 );
2223
0
    }
2224
2225
0
#if defined(MBEDTLS_SSL_CLI_C)
2226
0
    if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
2227
0
    {
2228
0
        if( ssl->client_auth == 0 )
2229
0
        {
2230
0
            MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2231
0
            ssl->state++;
2232
0
            return( 0 );
2233
0
        }
2234
2235
#if defined(MBEDTLS_SSL_PROTO_SSL3)
2236
        /*
2237
         * If using SSLv3 and got no cert, send an Alert message
2238
         * (otherwise an empty Certificate message will be sent).
2239
         */
2240
        if( mbedtls_ssl_own_cert( ssl )  == NULL &&
2241
            ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
2242
        {
2243
            ssl->out_msglen  = 2;
2244
            ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
2245
            ssl->out_msg[0]  = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
2246
            ssl->out_msg[1]  = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
2247
2248
            MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
2249
            goto write_msg;
2250
        }
2251
#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2252
0
    }
2253
0
#endif /* MBEDTLS_SSL_CLI_C */
2254
0
#if defined(MBEDTLS_SSL_SRV_C)
2255
0
    if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
2256
0
    {
2257
0
        if( mbedtls_ssl_own_cert( ssl ) == NULL )
2258
0
        {
2259
0
            MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
2260
0
            return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
2261
0
        }
2262
0
    }
2263
0
#endif
2264
2265
0
    MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
2266
2267
    /*
2268
     *     0  .  0    handshake type
2269
     *     1  .  3    handshake length
2270
     *     4  .  6    length of all certs
2271
     *     7  .  9    length of cert. 1
2272
     *    10  . n-1   peer certificate
2273
     *     n  . n+2   length of cert. 2
2274
     *    n+3 . ...   upper level cert, etc.
2275
     */
2276
0
    i = 7;
2277
0
    crt = mbedtls_ssl_own_cert( ssl );
2278
2279
0
    while( crt != NULL )
2280
0
    {
2281
0
        n = crt->raw.len;
2282
0
        if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
2283
0
        {
2284
0
            MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET
2285
0
                                        " > %" MBEDTLS_PRINTF_SIZET,
2286
0
                           i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2287
0
            return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
2288
0
        }
2289
2290
0
        ssl->out_msg[i    ] = MBEDTLS_BYTE_2( n );
2291
0
        ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n );
2292
0
        ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n );
2293
2294
0
        i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
2295
0
        i += n; crt = crt->next;
2296
0
    }
2297
2298
0
    ssl->out_msg[4]  = MBEDTLS_BYTE_2( i - 7 );
2299
0
    ssl->out_msg[5]  = MBEDTLS_BYTE_1( i - 7 );
2300
0
    ssl->out_msg[6]  = MBEDTLS_BYTE_0( i - 7 );
2301
2302
0
    ssl->out_msglen  = i;
2303
0
    ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
2304
0
    ssl->out_msg[0]  = MBEDTLS_SSL_HS_CERTIFICATE;
2305
2306
#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
2307
write_msg:
2308
#endif
2309
2310
0
    ssl->state++;
2311
2312
0
    if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
2313
0
    {
2314
0
        MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
2315
0
        return( ret );
2316
0
    }
2317
2318
0
    MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
2319
2320
0
    return( ret );
2321
0
}
2322
2323
#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
2324
2325
#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2326
static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
2327
                                         unsigned char *crt_buf,
2328
                                         size_t crt_buf_len )
2329
{
2330
    mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
2331
2332
    if( peer_crt == NULL )
2333
        return( -1 );
2334
2335
    if( peer_crt->raw.len != crt_buf_len )
2336
        return( -1 );
2337
2338
    return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) );
2339
}
2340
#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2341
static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
2342
                                         unsigned char *crt_buf,
2343
                                         size_t crt_buf_len )
2344
{
2345
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2346
    unsigned char const * const peer_cert_digest =
2347
        ssl->session->peer_cert_digest;
2348
    mbedtls_md_type_t const peer_cert_digest_type =
2349
        ssl->session->peer_cert_digest_type;
2350
    mbedtls_md_info_t const * const digest_info =
2351
        mbedtls_md_info_from_type( peer_cert_digest_type );
2352
    unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
2353
    size_t digest_len;
2354
2355
    if( peer_cert_digest == NULL || digest_info == NULL )
2356
        return( -1 );
2357
2358
    digest_len = mbedtls_md_get_size( digest_info );
2359
    if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
2360
        return( -1 );
2361
2362
    ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
2363
    if( ret != 0 )
2364
        return( -1 );
2365
2366
    return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
2367
}
2368
#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2369
#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
2370
2371
/*
2372
 * Once the certificate message is read, parse it into a cert chain and
2373
 * perform basic checks, but leave actual verification to the caller
2374
 */
2375
static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
2376
                                        mbedtls_x509_crt *chain )
2377
0
{
2378
0
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2379
#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
2380
    int crt_cnt=0;
2381
#endif
2382
0
    size_t i, n;
2383
0
    uint8_t alert;
2384
2385
0
    if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
2386
0
    {
2387
0
        MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
2388
0
        mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2389
0
                                        MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
2390
0
        return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
2391
0
    }
2392
2393
0
    if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
2394
0
        ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
2395
0
    {
2396
0
        MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
2397
0
        mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2398
0
                                        MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
2399
0
        return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
2400
0
    }
2401
2402
0
    i = mbedtls_ssl_hs_hdr_len( ssl );
2403
2404
    /*
2405
     * Same message structure as in mbedtls_ssl_write_certificate()
2406
     */
2407
0
    n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
2408
2409
0
    if( ssl->in_msg[i] != 0 ||
2410
0
        ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
2411
0
    {
2412
0
        MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
2413
0
        mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2414
0
                                        MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
2415
0
        return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
2416
0
    }
2417
2418
    /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
2419
0
    i += 3;
2420
2421
    /* Iterate through and parse the CRTs in the provided chain. */
2422
0
    while( i < ssl->in_hslen )
2423
0
    {
2424
        /* Check that there's room for the next CRT's length fields. */
2425
0
        if ( i + 3 > ssl->in_hslen ) {
2426
0
            MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
2427
0
            mbedtls_ssl_send_alert_message( ssl,
2428
0
                              MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2429
0
                              MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
2430
0
            return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
2431
0
        }
2432
        /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
2433
         * anything beyond 2**16 ~ 64K. */
2434
0
        if( ssl->in_msg[i] != 0 )
2435
0
        {
2436
0
            MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
2437
0
            mbedtls_ssl_send_alert_message( ssl,
2438
0
                            MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2439
0
                            MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
2440
0
            return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
2441
0
        }
2442
2443
        /* Read length of the next CRT in the chain. */
2444
0
        n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
2445
0
            | (unsigned int) ssl->in_msg[i + 2];
2446
0
        i += 3;
2447
2448
0
        if( n < 128 || i + n > ssl->in_hslen )
2449
0
        {
2450
0
            MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
2451
0
            mbedtls_ssl_send_alert_message( ssl,
2452
0
                                 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2453
0
                                 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
2454
0
            return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
2455
0
        }
2456
2457
        /* Check if we're handling the first CRT in the chain. */
2458
#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
2459
        if( crt_cnt++ == 0 &&
2460
            ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2461
            ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
2462
        {
2463
            /* During client-side renegotiation, check that the server's
2464
             * end-CRTs hasn't changed compared to the initial handshake,
2465
             * mitigating the triple handshake attack. On success, reuse
2466
             * the original end-CRT instead of parsing it again. */
2467
            MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
2468
            if( ssl_check_peer_crt_unchanged( ssl,
2469
                                              &ssl->in_msg[i],
2470
                                              n ) != 0 )
2471
            {
2472
                MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
2473
                mbedtls_ssl_send_alert_message( ssl,
2474
                                                MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2475
                                                MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
2476
                return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
2477
            }
2478
2479
            /* Now we can safely free the original chain. */
2480
            ssl_clear_peer_cert( ssl->session );
2481
        }
2482
#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
2483
2484
        /* Parse the next certificate in the chain. */
2485
#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2486
        ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
2487
#else
2488
        /* If we don't need to store the CRT chain permanently, parse
2489
         * it in-place from the input buffer instead of making a copy. */
2490
0
        ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
2491
0
#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2492
0
        switch( ret )
2493
0
        {
2494
0
            case 0: /*ok*/
2495
0
            case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
2496
                /* Ignore certificate with an unknown algorithm: maybe a
2497
                   prior certificate was already trusted. */
2498
0
                break;
2499
2500
0
            case MBEDTLS_ERR_X509_ALLOC_FAILED:
2501
0
                alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
2502
0
                goto crt_parse_der_failed;
2503
2504
0
            case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
2505
0
                alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
2506
0
                goto crt_parse_der_failed;
2507
2508
0
            default:
2509
0
                alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
2510
0
            crt_parse_der_failed:
2511
0
                mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
2512
0
                MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
2513
0
                return( ret );
2514
0
        }
2515
2516
0
        i += n;
2517
0
    }
2518
2519
0
    MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
2520
0
    return( 0 );
2521
0
}
2522
2523
#if defined(MBEDTLS_SSL_SRV_C)
2524
static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
2525
0
{
2526
0
    if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
2527
0
        return( -1 );
2528
2529
#if defined(MBEDTLS_SSL_PROTO_SSL3)
2530
    /*
2531
     * Check if the client sent an empty certificate
2532
     */
2533
    if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
2534
    {
2535
        if( ssl->in_msglen  == 2                        &&
2536
            ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT            &&
2537
            ssl->in_msg[0]  == MBEDTLS_SSL_ALERT_LEVEL_WARNING  &&
2538
            ssl->in_msg[1]  == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
2539
        {
2540
            MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
2541
            return( 0 );
2542
        }
2543
2544
        return( -1 );
2545
    }
2546
#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2547
2548
0
#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2549
0
    defined(MBEDTLS_SSL_PROTO_TLS1_2)
2550
0
    if( ssl->in_hslen   == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
2551
0
        ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE    &&
2552
0
        ssl->in_msg[0]  == MBEDTLS_SSL_HS_CERTIFICATE   &&
2553
0
        memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
2554
0
    {
2555
0
        MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
2556
0
        return( 0 );
2557
0
    }
2558
2559
0
    return( -1 );
2560
0
#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2561
          MBEDTLS_SSL_PROTO_TLS1_2 */
2562
0
}
2563
#endif /* MBEDTLS_SSL_SRV_C */
2564
2565
/* Check if a certificate message is expected.
2566
 * Return either
2567
 * - SSL_CERTIFICATE_EXPECTED, or
2568
 * - SSL_CERTIFICATE_SKIP
2569
 * indicating whether a Certificate message is expected or not.
2570
 */
2571
0
#define SSL_CERTIFICATE_EXPECTED 0
2572
0
#define SSL_CERTIFICATE_SKIP     1
2573
static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
2574
                                             int authmode )
2575
0
{
2576
0
    const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
2577
0
        ssl->handshake->ciphersuite_info;
2578
2579
0
    if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
2580
0
        return( SSL_CERTIFICATE_SKIP );
2581
2582
0
#if defined(MBEDTLS_SSL_SRV_C)
2583
0
    if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
2584
0
    {
2585
0
        if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
2586
0
            return( SSL_CERTIFICATE_SKIP );
2587
2588
0
        if( authmode == MBEDTLS_SSL_VERIFY_NONE )
2589
0
        {
2590
0
            ssl->session_negotiate->verify_result =
2591
0
                MBEDTLS_X509_BADCERT_SKIP_VERIFY;
2592
0
            return( SSL_CERTIFICATE_SKIP );
2593
0
        }
2594
0
    }
2595
#else
2596
    ((void) authmode);
2597
#endif /* MBEDTLS_SSL_SRV_C */
2598
2599
0
    return( SSL_CERTIFICATE_EXPECTED );
2600
0
}
2601
2602
static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
2603
                                         int authmode,
2604
                                         mbedtls_x509_crt *chain,
2605
                                         void *rs_ctx )
2606
0
{
2607
0
    int ret = 0;
2608
0
    const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
2609
0
        ssl->handshake->ciphersuite_info;
2610
0
    int have_ca_chain = 0;
2611
2612
0
    int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
2613
0
    void *p_vrfy;
2614
2615
0
    if( authmode == MBEDTLS_SSL_VERIFY_NONE )
2616
0
        return( 0 );
2617
2618
0
    if( ssl->f_vrfy != NULL )
2619
0
    {
2620
0
        MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
2621
0
        f_vrfy = ssl->f_vrfy;
2622
0
        p_vrfy = ssl->p_vrfy;
2623
0
    }
2624
0
    else
2625
0
    {
2626
0
        MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
2627
0
        f_vrfy = ssl->conf->f_vrfy;
2628
0
        p_vrfy = ssl->conf->p_vrfy;
2629
0
    }
2630
2631
    /*
2632
     * Main check: verify certificate
2633
     */
2634
#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
2635
    if( ssl->conf->f_ca_cb != NULL )
2636
    {
2637
        ((void) rs_ctx);
2638
        have_ca_chain = 1;
2639
2640
        MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
2641
        ret = mbedtls_x509_crt_verify_with_ca_cb(
2642
            chain,
2643
            ssl->conf->f_ca_cb,
2644
            ssl->conf->p_ca_cb,
2645
            ssl->conf->cert_profile,
2646
            ssl->hostname,
2647
            &ssl->session_negotiate->verify_result,
2648
            f_vrfy, p_vrfy );
2649
    }
2650
    else
2651
#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2652
0
    {
2653
0
        mbedtls_x509_crt *ca_chain;
2654
0
        mbedtls_x509_crl *ca_crl;
2655
2656
#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2657
        if( ssl->handshake->sni_ca_chain != NULL )
2658
        {
2659
            ca_chain = ssl->handshake->sni_ca_chain;
2660
            ca_crl   = ssl->handshake->sni_ca_crl;
2661
        }
2662
        else
2663
#endif
2664
0
        {
2665
0
            ca_chain = ssl->conf->ca_chain;
2666
0
            ca_crl   = ssl->conf->ca_crl;
2667
0
        }
2668
2669
0
        if( ca_chain != NULL )
2670
0
            have_ca_chain = 1;
2671
2672
0
        ret = mbedtls_x509_crt_verify_restartable(
2673
0
            chain,
2674
0
            ca_chain, ca_crl,
2675
0
            ssl->conf->cert_profile,
2676
0
            ssl->hostname,
2677
0
            &ssl->session_negotiate->verify_result,
2678
0
            f_vrfy, p_vrfy, rs_ctx );
2679
0
    }
2680
2681
0
    if( ret != 0 )
2682
0
    {
2683
0
        MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
2684
0
    }
2685
2686
#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
2687
    if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2688
        return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
2689
#endif
2690
2691
    /*
2692
     * Secondary checks: always done, but change 'ret' only if it was 0
2693
     */
2694
2695
0
#if defined(MBEDTLS_ECP_C)
2696
0
    {
2697
0
        const mbedtls_pk_context *pk = &chain->pk;
2698
2699
        /* If certificate uses an EC key, make sure the curve is OK */
2700
0
        if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
2701
0
            mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
2702
0
        {
2703
0
            ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
2704
2705
0
            MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
2706
0
            if( ret == 0 )
2707
0
                ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
2708
0
        }
2709
0
    }
2710
0
#endif /* MBEDTLS_ECP_C */
2711
2712
0
    if( mbedtls_ssl_check_cert_usage( chain,
2713
0
                                      ciphersuite_info,
2714
0
                                      ! ssl->conf->endpoint,
2715
0
                                      &ssl->session_negotiate->verify_result ) != 0 )
2716
0
    {
2717
0
        MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
2718
0
        if( ret == 0 )
2719
0
            ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
2720
0
    }
2721
2722
    /* mbedtls_x509_crt_verify_with_profile is supposed to report a
2723
     * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
2724
     * with details encoded in the verification flags. All other kinds
2725
     * of error codes, including those from the user provided f_vrfy
2726
     * functions, are treated as fatal and lead to a failure of
2727
     * ssl_parse_certificate even if verification was optional. */
2728
0
    if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
2729
0
        ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
2730
0
          ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
2731
0
    {
2732
0
        ret = 0;
2733
0
    }
2734
2735
0
    if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
2736
0
    {
2737
0
        MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
2738
0
        ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
2739
0
    }
2740
2741
0
    if( ret != 0 )
2742
0
    {
2743
0
        uint8_t alert;
2744
2745
        /* The certificate may have been rejected for several reasons.
2746
           Pick one and send the corresponding alert. Which alert to send
2747
           may be a subject of debate in some cases. */
2748
0
        if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
2749
0
            alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
2750
0
        else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
2751
0
            alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
2752
0
        else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
2753
0
            alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
2754
0
        else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
2755
0
            alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
2756
0
        else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
2757
0
            alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
2758
0
        else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
2759
0
            alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
2760
0
        else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
2761
0
            alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
2762
0
        else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
2763
0
            alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
2764
0
        else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
2765
0
            alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
2766
0
        else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
2767
0
            alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
2768
0
        else
2769
0
            alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
2770
0
        mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2771
0
                                        alert );
2772
0
    }
2773
2774
#if defined(MBEDTLS_DEBUG_C)
2775
    if( ssl->session_negotiate->verify_result != 0 )
2776
    {
2777
        MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
2778
                                    (unsigned int) ssl->session_negotiate->verify_result ) );
2779
    }
2780
    else
2781
    {
2782
        MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
2783
    }
2784
#endif /* MBEDTLS_DEBUG_C */
2785
2786
0
    return( ret );
2787
0
}
2788
2789
#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2790
static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
2791
                                         unsigned char *start, size_t len )
2792
0
{
2793
0
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2794
    /* Remember digest of the peer's end-CRT. */
2795
0
    ssl->session_negotiate->peer_cert_digest =
2796
0
        mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
2797
0
    if( ssl->session_negotiate->peer_cert_digest == NULL )
2798
0
    {
2799
0
        MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
2800
0
                                    MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) );
2801
0
        mbedtls_ssl_send_alert_message( ssl,
2802
0
                                        MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2803
0
                                        MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
2804
2805
0
        return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2806
0
    }
2807
2808
0
    ret = mbedtls_md( mbedtls_md_info_from_type(
2809
0
                          MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
2810
0
                      start, len,
2811
0
                      ssl->session_negotiate->peer_cert_digest );
2812
2813
0
    ssl->session_negotiate->peer_cert_digest_type =
2814
0
        MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
2815
0
    ssl->session_negotiate->peer_cert_digest_len =
2816
0
        MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
2817
2818
0
    return( ret );
2819
0
}
2820
2821
static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
2822
                                     unsigned char *start, size_t len )
2823
0
{
2824
0
    unsigned char *end = start + len;
2825
0
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2826
2827
    /* Make a copy of the peer's raw public key. */
2828
0
    mbedtls_pk_init( &ssl->handshake->peer_pubkey );
2829
0
    ret = mbedtls_pk_parse_subpubkey( &start, end,
2830
0
                                      &ssl->handshake->peer_pubkey );
2831
0
    if( ret != 0 )
2832
0
    {
2833
        /* We should have parsed the public key before. */
2834
0
        return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2835
0
    }
2836
2837
0
    return( 0 );
2838
0
}
2839
#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2840
2841
int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
2842
0
{
2843
0
    int ret = 0;
2844
0
    int crt_expected;
2845
#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2846
    const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
2847
                       ? ssl->handshake->sni_authmode
2848
                       : ssl->conf->authmode;
2849
#else
2850
0
    const int authmode = ssl->conf->authmode;
2851
0
#endif
2852
0
    void *rs_ctx = NULL;
2853
0
    mbedtls_x509_crt *chain = NULL;
2854
2855
0
    MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2856
2857
0
    crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
2858
0
    if( crt_expected == SSL_CERTIFICATE_SKIP )
2859
0
    {
2860
0
        MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2861
0
        goto exit;
2862
0
    }
2863
2864
#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
2865
    if( ssl->handshake->ecrs_enabled &&
2866
        ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
2867
    {
2868
        chain = ssl->handshake->ecrs_peer_cert;
2869
        ssl->handshake->ecrs_peer_cert = NULL;
2870
        goto crt_verify;
2871
    }
2872
#endif
2873
2874
0
    if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
2875
0
    {
2876
        /* mbedtls_ssl_read_record may have sent an alert already. We
2877
           let it decide whether to alert. */
2878
0
        MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
2879
0
        goto exit;
2880
0
    }
2881
2882
0
#if defined(MBEDTLS_SSL_SRV_C)
2883
0
    if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
2884
0
    {
2885
0
        ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
2886
2887
0
        if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL )
2888
0
            ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
2889
2890
0
        goto exit;
2891
0
    }
2892
0
#endif /* MBEDTLS_SSL_SRV_C */
2893
2894
    /* Clear existing peer CRT structure in case we tried to
2895
     * reuse a session but it failed, and allocate a new one. */
2896
0
    ssl_clear_peer_cert( ssl->session_negotiate );
2897
2898
0
    chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
2899
0
    if( chain == NULL )
2900
0
    {
2901
0
        MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
2902
0
                                    sizeof( mbedtls_x509_crt ) ) );
2903
0
        mbedtls_ssl_send_alert_message( ssl,
2904
0
                                        MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2905
0
                                        MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
2906
2907
0
        ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
2908
0
        goto exit;
2909
0
    }
2910
0
    mbedtls_x509_crt_init( chain );
2911
2912
0
    ret = ssl_parse_certificate_chain( ssl, chain );
2913
0
    if( ret != 0 )
2914
0
        goto exit;
2915
2916
#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
2917
    if( ssl->handshake->ecrs_enabled)
2918
        ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
2919
2920
crt_verify:
2921
    if( ssl->handshake->ecrs_enabled)
2922
        rs_ctx = &ssl->handshake->ecrs_ctx;
2923
#endif
2924
2925
0
    ret = ssl_parse_certificate_verify( ssl, authmode,
2926
0
                                        chain, rs_ctx );
2927
0
    if( ret != 0 )
2928
0
        goto exit;
2929
2930
0
#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2931
0
    {
2932
0
        unsigned char *crt_start, *pk_start;
2933
0
        size_t crt_len, pk_len;
2934
2935
        /* We parse the CRT chain without copying, so
2936
         * these pointers point into the input buffer,
2937
         * and are hence still valid after freeing the
2938
         * CRT chain. */
2939
2940
0
        crt_start = chain->raw.p;
2941
0
        crt_len   = chain->raw.len;
2942
2943
0
        pk_start = chain->pk_raw.p;
2944
0
        pk_len   = chain->pk_raw.len;
2945
2946
        /* Free the CRT structures before computing
2947
         * digest and copying the peer's public key. */
2948
0
        mbedtls_x509_crt_free( chain );
2949
0
        mbedtls_free( chain );
2950
0
        chain = NULL;
2951
2952
0
        ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
2953
0
        if( ret != 0 )
2954
0
            goto exit;
2955
2956
0
        ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
2957
0
        if( ret != 0 )
2958
0
            goto exit;
2959
0
    }
2960
#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2961
    /* Pass ownership to session structure. */
2962
    ssl->session_negotiate->peer_cert = chain;
2963
    chain = NULL;
2964
#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2965
2966
0
    MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
2967
2968
0
exit:
2969
2970
0
    if( ret == 0 )
2971
0
        ssl->state++;
2972
2973
#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
2974
    if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
2975
    {
2976
        ssl->handshake->ecrs_peer_cert = chain;
2977
        chain = NULL;
2978
    }
2979
#endif
2980
2981
0
    if( chain != NULL )
2982
0
    {
2983
0
        mbedtls_x509_crt_free( chain );
2984
0
        mbedtls_free( chain );
2985
0
    }
2986
2987
0
    return( ret );
2988
0
}
2989
#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
2990
2991
void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
2992
                            const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
2993
0
{
2994
0
    ((void) ciphersuite_info);
2995
2996
#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
2997
    defined(MBEDTLS_SSL_PROTO_TLS1_1)
2998
    if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
2999
        ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
3000
    else
3001
#endif
3002
0
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3003
#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
3004
    if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
3005
        ssl->handshake->update_checksum = ssl_update_checksum_sha384;
3006
    else
3007
#endif
3008
0
#if defined(MBEDTLS_SHA256_C)
3009
0
    if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
3010
0
        ssl->handshake->update_checksum = ssl_update_checksum_sha256;
3011
0
    else
3012
0
#endif
3013
0
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3014
0
    {
3015
0
        MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3016
0
        return;
3017
0
    }
3018
0
}
3019
3020
void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
3021
0
{
3022
#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
3023
    defined(MBEDTLS_SSL_PROTO_TLS1_1)
3024
     mbedtls_md5_starts_ret( &ssl->handshake->fin_md5  );
3025
    mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
3026
#endif
3027
0
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3028
0
#if defined(MBEDTLS_SHA256_C)
3029
#if defined(MBEDTLS_USE_PSA_CRYPTO)
3030
    psa_hash_abort( &ssl->handshake->fin_sha256_psa );
3031
    psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
3032
#else
3033
0
    mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
3034
0
#endif
3035
0
#endif
3036
#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
3037
#if defined(MBEDTLS_USE_PSA_CRYPTO)
3038
    psa_hash_abort( &ssl->handshake->fin_sha384_psa );
3039
    psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
3040
#else
3041
    mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
3042
#endif
3043
#endif
3044
0
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3045
0
}
3046
3047
static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
3048
                                       const unsigned char *buf, size_t len )
3049
1.38k
{
3050
#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
3051
    defined(MBEDTLS_SSL_PROTO_TLS1_1)
3052
     mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
3053
    mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
3054
#endif
3055
1.38k
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3056
1.38k
#if defined(MBEDTLS_SHA256_C)
3057
#if defined(MBEDTLS_USE_PSA_CRYPTO)
3058
    psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
3059
#else
3060
1.38k
    mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
3061
1.38k
#endif
3062
1.38k
#endif
3063
#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
3064
#if defined(MBEDTLS_USE_PSA_CRYPTO)
3065
    psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
3066
#else
3067
    mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
3068
#endif
3069
#endif
3070
1.38k
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3071
1.38k
}
3072
3073
#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
3074
    defined(MBEDTLS_SSL_PROTO_TLS1_1)
3075
static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
3076
                                         const unsigned char *buf, size_t len )
3077
{
3078
     mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
3079
    mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
3080
}
3081
#endif
3082
3083
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3084
#if defined(MBEDTLS_SHA256_C)
3085
static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
3086
                                        const unsigned char *buf, size_t len )
3087
0
{
3088
#if defined(MBEDTLS_USE_PSA_CRYPTO)
3089
    psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
3090
#else
3091
0
    mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
3092
0
#endif
3093
0
}
3094
#endif
3095
3096
#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
3097
static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
3098
                                        const unsigned char *buf, size_t len )
3099
{
3100
#if defined(MBEDTLS_USE_PSA_CRYPTO)
3101
    psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
3102
#else
3103
    mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
3104
#endif
3105
}
3106
#endif
3107
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3108
3109
#if defined(MBEDTLS_SSL_PROTO_SSL3)
3110
static void ssl_calc_finished_ssl(
3111
                mbedtls_ssl_context *ssl, unsigned char *buf, int from )
3112
{
3113
    const char *sender;
3114
    mbedtls_md5_context  md5;
3115
    mbedtls_sha1_context sha1;
3116
3117
    unsigned char padbuf[48];
3118
    unsigned char md5sum[16];
3119
    unsigned char sha1sum[20];
3120
3121
    mbedtls_ssl_session *session = ssl->session_negotiate;
3122
    if( !session )
3123
        session = ssl->session;
3124
3125
    MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc  finished ssl" ) );
3126
3127
    mbedtls_md5_init( &md5 );
3128
    mbedtls_sha1_init( &sha1 );
3129
3130
    mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
3131
    mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
3132
3133
    /*
3134
     * SSLv3:
3135
     *   hash =
3136
     *      MD5( master + pad2 +
3137
     *          MD5( handshake + sender + master + pad1 ) )
3138
     *   + SHA1( master + pad2 +
3139
     *         SHA1( handshake + sender + master + pad1 ) )
3140
     */
3141
3142
#if !defined(MBEDTLS_MD5_ALT)
3143
    MBEDTLS_SSL_DEBUG_BUF( 4, "finished  md5 state", (unsigned char *)
3144
                    md5.state, sizeof(  md5.state ) );
3145
#endif
3146
3147
#if !defined(MBEDTLS_SHA1_ALT)
3148
    MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
3149
                   sha1.state, sizeof( sha1.state ) );
3150
#endif
3151
3152
    sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
3153
                                       : "SRVR";
3154
3155
    memset( padbuf, 0x36, 48 );
3156
3157
    mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
3158
    mbedtls_md5_update_ret( &md5, session->master, 48 );
3159
    mbedtls_md5_update_ret( &md5, padbuf, 48 );
3160
    mbedtls_md5_finish_ret( &md5, md5sum );
3161
3162
    mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
3163
    mbedtls_sha1_update_ret( &sha1, session->master, 48 );
3164
    mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
3165
    mbedtls_sha1_finish_ret( &sha1, sha1sum );
3166
3167
    memset( padbuf, 0x5C, 48 );
3168
3169
    mbedtls_md5_starts_ret( &md5 );
3170
    mbedtls_md5_update_ret( &md5, session->master, 48 );
3171
    mbedtls_md5_update_ret( &md5, padbuf, 48 );
3172
    mbedtls_md5_update_ret( &md5, md5sum, 16 );
3173
    mbedtls_md5_finish_ret( &md5, buf );
3174
3175
    mbedtls_sha1_starts_ret( &sha1 );
3176
    mbedtls_sha1_update_ret( &sha1, session->master, 48 );
3177
    mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
3178
    mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
3179
    mbedtls_sha1_finish_ret( &sha1, buf + 16 );
3180
3181
    MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
3182
3183
    mbedtls_md5_free(  &md5  );
3184
    mbedtls_sha1_free( &sha1 );
3185
3186
    mbedtls_platform_zeroize(  padbuf, sizeof(  padbuf ) );
3187
    mbedtls_platform_zeroize(  md5sum, sizeof(  md5sum ) );
3188
    mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
3189
3190
    MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc  finished" ) );
3191
}
3192
#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3193
3194
#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
3195
static void ssl_calc_finished_tls(
3196
                mbedtls_ssl_context *ssl, unsigned char *buf, int from )
3197
{
3198
    int len = 12;
3199
    const char *sender;
3200
    mbedtls_md5_context  md5;
3201
    mbedtls_sha1_context sha1;
3202
    unsigned char padbuf[36];
3203
3204
    mbedtls_ssl_session *session = ssl->session_negotiate;
3205
    if( !session )
3206
        session = ssl->session;
3207
3208
    MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc  finished tls" ) );
3209
3210
    mbedtls_md5_init( &md5 );
3211
    mbedtls_sha1_init( &sha1 );
3212
3213
    mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
3214
    mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
3215
3216
    /*
3217
     * TLSv1:
3218
     *   hash = PRF( master, finished_label,
3219
     *               MD5( handshake ) + SHA1( handshake ) )[0..11]
3220
     */
3221
3222
#if !defined(MBEDTLS_MD5_ALT)
3223
    MBEDTLS_SSL_DEBUG_BUF( 4, "finished  md5 state", (unsigned char *)
3224
                    md5.state, sizeof(  md5.state ) );
3225
#endif
3226
3227
#if !defined(MBEDTLS_SHA1_ALT)
3228
    MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
3229
                   sha1.state, sizeof( sha1.state ) );
3230
#endif
3231
3232
    sender = ( from == MBEDTLS_SSL_IS_CLIENT )
3233
             ? "client finished"
3234
             : "server finished";
3235
3236
    mbedtls_md5_finish_ret(  &md5, padbuf );
3237
    mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
3238
3239
    ssl->handshake->tls_prf( session->master, 48, sender,
3240
                             padbuf, 36, buf, len );
3241
3242
    MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3243
3244
    mbedtls_md5_free(  &md5  );
3245
    mbedtls_sha1_free( &sha1 );
3246
3247
    mbedtls_platform_zeroize(  padbuf, sizeof(  padbuf ) );
3248
3249
    MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc  finished" ) );
3250
}
3251
#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
3252
3253
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3254
#if defined(MBEDTLS_SHA256_C)
3255
static void ssl_calc_finished_tls_sha256(
3256
                mbedtls_ssl_context *ssl, unsigned char *buf, int from )
3257
0
{
3258
0
    int len = 12;
3259
0
    const char *sender;
3260
0
    unsigned char padbuf[32];
3261
#if defined(MBEDTLS_USE_PSA_CRYPTO)
3262
    size_t hash_size;
3263
    psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
3264
    psa_status_t status;
3265
#else
3266
0
    mbedtls_sha256_context sha256;
3267
0
#endif
3268
3269
0
    mbedtls_ssl_session *session = ssl->session_negotiate;
3270
0
    if( !session )
3271
0
        session = ssl->session;
3272
3273
0
    sender = ( from == MBEDTLS_SSL_IS_CLIENT )
3274
0
             ? "client finished"
3275
0
             : "server finished";
3276
3277
#if defined(MBEDTLS_USE_PSA_CRYPTO)
3278
    sha256_psa = psa_hash_operation_init();
3279
3280
    MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
3281
3282
    status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
3283
    if( status != PSA_SUCCESS )
3284
    {
3285
        MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
3286
        return;
3287
    }
3288
3289
    status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
3290
    if( status != PSA_SUCCESS )
3291
    {
3292
        MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
3293
        return;
3294
    }
3295
    MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
3296
#else
3297
3298
0
    mbedtls_sha256_init( &sha256 );
3299
3300
0
    MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc  finished tls sha256" ) );
3301
3302
0
    mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
3303
3304
    /*
3305
     * TLSv1.2:
3306
     *   hash = PRF( master, finished_label,
3307
     *               Hash( handshake ) )[0.11]
3308
     */
3309
3310
0
#if !defined(MBEDTLS_SHA256_ALT)
3311
0
    MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
3312
0
                   sha256.state, sizeof( sha256.state ) );
3313
0
#endif
3314
3315
0
    mbedtls_sha256_finish_ret( &sha256, padbuf );
3316
0
    mbedtls_sha256_free( &sha256 );
3317
0
#endif /* MBEDTLS_USE_PSA_CRYPTO */
3318
3319
0
    ssl->handshake->tls_prf( session->master, 48, sender,
3320
0
                             padbuf, 32, buf, len );
3321
3322
0
    MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3323
3324
0
    mbedtls_platform_zeroize(  padbuf, sizeof(  padbuf ) );
3325
3326
0
    MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc  finished" ) );
3327
0
}
3328
#endif /* MBEDTLS_SHA256_C */
3329
3330
#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
3331
3332
static void ssl_calc_finished_tls_sha384(
3333
                mbedtls_ssl_context *ssl, unsigned char *buf, int from )
3334
{
3335
    int len = 12;
3336
    const char *sender;
3337
    unsigned char padbuf[48];
3338
#if defined(MBEDTLS_USE_PSA_CRYPTO)
3339
    size_t hash_size;
3340
    psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
3341
    psa_status_t status;
3342
#else
3343
    mbedtls_sha512_context sha512;
3344
#endif
3345
3346
    mbedtls_ssl_session *session = ssl->session_negotiate;
3347
    if( !session )
3348
        session = ssl->session;
3349
3350
    sender = ( from == MBEDTLS_SSL_IS_CLIENT )
3351
                ? "client finished"
3352
                : "server finished";
3353
3354
#if defined(MBEDTLS_USE_PSA_CRYPTO)
3355
    sha384_psa = psa_hash_operation_init();
3356
3357
    MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
3358
3359
    status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
3360
    if( status != PSA_SUCCESS )
3361
    {
3362
        MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
3363
        return;
3364
    }
3365
3366
    status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
3367
    if( status != PSA_SUCCESS )
3368
    {
3369
        MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
3370
        return;
3371
    }
3372
    MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
3373
#else
3374
    mbedtls_sha512_init( &sha512 );
3375
3376
    MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc  finished tls sha384" ) );
3377
3378
    mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
3379
3380
    /*
3381
     * TLSv1.2:
3382
     *   hash = PRF( master, finished_label,
3383
     *               Hash( handshake ) )[0.11]
3384
     */
3385
3386
#if !defined(MBEDTLS_SHA512_ALT)
3387
    MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
3388
                   sha512.state, sizeof( sha512.state ) );
3389
#endif
3390
    /* mbedtls_sha512_finish_ret's output parameter is declared as a
3391
     * 64-byte buffer, but sice we're using SHA-384, we know that the
3392
     * output fits in 48 bytes. This is correct C, but GCC 11.1 warns
3393
     * about it.
3394
     */
3395
#if defined(__GNUC__) && __GNUC__ >= 11
3396
#pragma GCC diagnostic push
3397
#pragma GCC diagnostic ignored "-Wstringop-overflow"
3398
#endif
3399
    mbedtls_sha512_finish_ret( &sha512, padbuf );
3400
#if defined(__GNUC__) && __GNUC__ >= 11
3401
#pragma GCC diagnostic pop
3402
#endif
3403
3404
    mbedtls_sha512_free( &sha512 );
3405
#endif
3406
3407
    ssl->handshake->tls_prf( session->master, 48, sender,
3408
                             padbuf, 48, buf, len );
3409
3410
    MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3411
3412
    mbedtls_platform_zeroize(  padbuf, sizeof( padbuf ) );
3413
3414
    MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc  finished" ) );
3415
}
3416
#endif /* MBEDTLS_SHA512_C && !MBEDTLS_SHA512_NO_SHA384 */
3417
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3418
3419
void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
3420
0
{
3421
0
    MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
3422
3423
    /*
3424
     * Free our handshake params
3425
     */
3426
0
    mbedtls_ssl_handshake_free( ssl );
3427
0
    mbedtls_free( ssl->handshake );
3428
0
    ssl->handshake = NULL;
3429
3430
    /*
3431
     * Free the previous transform and swith in the current one
3432
     */
3433
0
    if( ssl->transform )
3434
0
    {
3435
0
        mbedtls_ssl_transform_free( ssl->transform );
3436
0
        mbedtls_free( ssl->transform );
3437
0
    }
3438
0
    ssl->transform = ssl->transform_negotiate;
3439
0
    ssl->transform_negotiate = NULL;
3440
3441
0
    MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
3442
0
}
3443
3444
void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
3445
0
{
3446
0
    int resume = ssl->handshake->resume;
3447
3448
0
    MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
3449
3450
#if defined(MBEDTLS_SSL_RENEGOTIATION)
3451
    if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
3452
    {
3453
        ssl->renego_status =  MBEDTLS_SSL_RENEGOTIATION_DONE;
3454
        ssl->renego_records_seen = 0;
3455
    }
3456
#endif
3457
3458
    /*
3459
     * Free the previous session and switch in the current one
3460
     */
3461
0
    if( ssl->session )
3462
0
    {
3463
#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
3464
        /* RFC 7366 3.1: keep the EtM state */
3465
        ssl->session_negotiate->encrypt_then_mac =
3466
                  ssl->session->encrypt_then_mac;
3467
#endif
3468
3469
0
        mbedtls_ssl_session_free( ssl->session );
3470
0
        mbedtls_free( ssl->session );
3471
0
    }
3472
0
    ssl->session = ssl->session_negotiate;
3473
0
    ssl->session_negotiate = NULL;
3474
3475
    /*
3476
     * Add cache entry
3477
     */
3478
0
    if( ssl->conf->f_set_cache != NULL &&
3479
0
        ssl->session->id_len != 0 &&
3480
0
        resume == 0 )
3481
0
    {
3482
0
        if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
3483
0
            MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
3484
0
    }
3485
3486
0
#if defined(MBEDTLS_SSL_PROTO_DTLS)
3487
0
    if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3488
0
        ssl->handshake->flight != NULL )
3489
0
    {
3490
        /* Cancel handshake timer */
3491
0
        mbedtls_ssl_set_timer( ssl, 0 );
3492
3493
        /* Keep last flight around in case we need to resend it:
3494
         * we need the handshake and transform structures for that */
3495
0
        MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
3496
0
    }
3497
0
    else
3498
0
#endif
3499
0
        mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
3500
3501
0
    ssl->state++;
3502
3503
0
    MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
3504
0
}
3505
3506
int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
3507
0
{
3508
0
    int ret, hash_len;
3509
3510
0
    MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
3511
3512
0
    mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate );
3513
3514
0
    ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
3515
3516
    /*
3517
     * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
3518
     * may define some other value. Currently (early 2016), no defined
3519
     * ciphersuite does this (and this is unlikely to change as activity has
3520
     * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
3521
     */
3522
0
    hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
3523
3524
#if defined(MBEDTLS_SSL_RENEGOTIATION)
3525
    ssl->verify_data_len = hash_len;
3526
    memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
3527
#endif
3528
3529
0
    ssl->out_msglen  = 4 + hash_len;
3530
0
    ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3531
0
    ssl->out_msg[0]  = MBEDTLS_SSL_HS_FINISHED;
3532
3533
    /*
3534
     * In case of session resuming, invert the client and server
3535
     * ChangeCipherSpec messages order.
3536
     */
3537
0
    if( ssl->handshake->resume != 0 )
3538
0
    {
3539
0
#if defined(MBEDTLS_SSL_CLI_C)
3540
0
        if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
3541
0
            ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
3542
0
#endif
3543
0
#if defined(MBEDTLS_SSL_SRV_C)
3544
0
        if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
3545
0
            ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
3546
0
#endif
3547
0
    }
3548
0
    else
3549
0
        ssl->state++;
3550
3551
    /*
3552
     * Switch to our negotiated transform and session parameters for outbound
3553
     * data.
3554
     */
3555
0
    MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
3556
3557
0
#if defined(MBEDTLS_SSL_PROTO_DTLS)
3558
0
    if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3559
0
    {
3560
0
        unsigned char i;
3561
3562
        /* Remember current epoch settings for resending */
3563
0
        ssl->handshake->alt_transform_out = ssl->transform_out;
3564
0
        memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
3565
3566
        /* Set sequence_number to zero */
3567
0
        memset( ssl->cur_out_ctr + 2, 0, 6 );
3568
3569
        /* Increment epoch */
3570
0
        for( i = 2; i > 0; i-- )
3571
0
            if( ++ssl->cur_out_ctr[i - 1] != 0 )
3572
0
                break;
3573
3574
        /* The loop goes to its end iff the counter is wrapping */
3575
0
        if( i == 0 )
3576
0
        {
3577
0
            MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
3578
0
            return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3579
0
        }
3580
0
    }
3581
0
    else
3582
0
#endif /* MBEDTLS_SSL_PROTO_DTLS */
3583
0
    memset( ssl->cur_out_ctr, 0, 8 );
3584
3585
0
    ssl->transform_out = ssl->transform_negotiate;
3586
0
    ssl->session_out = ssl->session_negotiate;
3587
3588
#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3589
    if( mbedtls_ssl_hw_record_activate != NULL )
3590
    {
3591
        if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
3592
        {
3593
            MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3594
            return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
3595
        }
3596
    }
3597
#endif
3598
3599
0
#if defined(MBEDTLS_SSL_PROTO_DTLS)
3600
0
    if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3601
0
        mbedtls_ssl_send_flight_completed( ssl );
3602
0
#endif
3603
3604
0
    if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
3605
0
    {
3606
0
        MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
3607
0
        return( ret );
3608
0
    }
3609
3610
0
#if defined(MBEDTLS_SSL_PROTO_DTLS)
3611
0
    if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3612
0
        ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
3613
0
    {
3614
0
        MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
3615
0
        return( ret );
3616
0
    }
3617
0
#endif
3618
3619
0
    MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
3620
3621
0
    return( 0 );
3622
0
}
3623
3624
#if defined(MBEDTLS_SSL_PROTO_SSL3)
3625
#define SSL_MAX_HASH_LEN 36
3626
#else
3627
#define SSL_MAX_HASH_LEN 12
3628
#endif
3629
3630
int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
3631
0
{
3632
0
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3633
0
    unsigned int hash_len;
3634
0
    unsigned char buf[SSL_MAX_HASH_LEN];
3635
3636
0
    MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
3637
3638
    /* There is currently no ciphersuite using another length with TLS 1.2 */
3639
#if defined(MBEDTLS_SSL_PROTO_SSL3)
3640
    if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
3641
        hash_len = 36;
3642
    else
3643
#endif
3644
0
        hash_len = 12;
3645
3646
0
    ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
3647
3648
0
    if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
3649
0
    {
3650
0
        MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
3651
0
        goto exit;
3652
0
    }
3653
3654
0
    if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
3655
0
    {
3656
0
        MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
3657
0
        mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3658
0
                                        MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
3659
0
        ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
3660
0
        goto exit;
3661
0
    }
3662
3663
0
    if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
3664
0
        ssl->in_hslen  != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
3665
0
    {
3666
0
        MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
3667
0
        mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3668
0
                                        MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
3669
0
        ret = MBEDTLS_ERR_SSL_BAD_HS_FINISHED;
3670
0
        goto exit;
3671
0
    }
3672
3673
0
    if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
3674
0
                      buf, hash_len ) != 0 )
3675
0
    {
3676
0
        MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
3677
0
        mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3678
0
                                        MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
3679
0
        ret = MBEDTLS_ERR_SSL_BAD_HS_FINISHED;
3680
0
        goto exit;
3681
0
    }
3682
3683
#if defined(MBEDTLS_SSL_RENEGOTIATION)
3684
    ssl->verify_data_len = hash_len;
3685
    memcpy( ssl->peer_verify_data, buf, hash_len );
3686
#endif
3687
3688
0
    if( ssl->handshake->resume != 0 )
3689
0
    {
3690
0
#if defined(MBEDTLS_SSL_CLI_C)
3691
0
        if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
3692
0
            ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
3693
0
#endif
3694
0
#if defined(MBEDTLS_SSL_SRV_C)
3695
0
        if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
3696
0
            ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
3697
0
#endif
3698
0
    }
3699
0
    else
3700
0
        ssl->state++;
3701
3702
0
#if defined(MBEDTLS_SSL_PROTO_DTLS)
3703
0
    if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3704
0
        mbedtls_ssl_recv_flight_completed( ssl );
3705
0
#endif
3706
3707
0
    MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
3708
3709
0
exit:
3710
0
    mbedtls_platform_zeroize( buf, hash_len );
3711
0
    return( ret );
3712
0
}
3713
3714
static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
3715
4.60k
{
3716
4.60k
    memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
3717
3718
#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
3719
    defined(MBEDTLS_SSL_PROTO_TLS1_1)
3720
     mbedtls_md5_init(   &handshake->fin_md5  );
3721
    mbedtls_sha1_init(   &handshake->fin_sha1 );
3722
     mbedtls_md5_starts_ret( &handshake->fin_md5  );
3723
    mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
3724
#endif
3725
4.60k
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3726
4.60k
#if defined(MBEDTLS_SHA256_C)
3727
#if defined(MBEDTLS_USE_PSA_CRYPTO)
3728
    handshake->fin_sha256_psa = psa_hash_operation_init();
3729
    psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
3730
#else
3731
4.60k
    mbedtls_sha256_init(   &handshake->fin_sha256    );
3732
4.60k
    mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
3733
4.60k
#endif
3734
4.60k
#endif
3735
#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
3736
#if defined(MBEDTLS_USE_PSA_CRYPTO)
3737
    handshake->fin_sha384_psa = psa_hash_operation_init();
3738
    psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
3739
#else
3740
    mbedtls_sha512_init(   &handshake->fin_sha512    );
3741
    mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
3742
#endif
3743
#endif
3744
4.60k
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3745
3746
4.60k
    handshake->update_checksum = ssl_update_checksum_start;
3747
3748
4.60k
#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
3749
4.60k
    defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
3750
4.60k
    mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
3751
4.60k
#endif
3752
3753
#if defined(MBEDTLS_DHM_C)
3754
    mbedtls_dhm_init( &handshake->dhm_ctx );
3755
#endif
3756
4.60k
#if defined(MBEDTLS_ECDH_C)
3757
4.60k
    mbedtls_ecdh_init( &handshake->ecdh_ctx );
3758
4.60k
#endif
3759
4.60k
#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
3760
4.60k
    mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
3761
4.60k
#if defined(MBEDTLS_SSL_CLI_C)
3762
4.60k
    handshake->ecjpake_cache = NULL;
3763
4.60k
    handshake->ecjpake_cache_len = 0;
3764
4.60k
#endif
3765
4.60k
#endif
3766
3767
#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
3768
    mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
3769
#endif
3770
3771
#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3772
    handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
3773
#endif
3774
3775
4.60k
#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3776
4.60k
    !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3777
4.60k
    mbedtls_pk_init( &handshake->peer_pubkey );
3778
4.60k
#endif
3779
4.60k
}
3780
3781
void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
3782
4.60k
{
3783
4.60k
    memset( transform, 0, sizeof(mbedtls_ssl_transform) );
3784
3785
4.60k
    mbedtls_cipher_init( &transform->cipher_ctx_enc );
3786
4.60k
    mbedtls_cipher_init( &transform->cipher_ctx_dec );
3787
3788
#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
3789
    mbedtls_md_init( &transform->md_ctx_enc );
3790
    mbedtls_md_init( &transform->md_ctx_dec );
3791
#endif
3792
4.60k
}
3793
3794
void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
3795
4.60k
{
3796
4.60k
    memset( session, 0, sizeof(mbedtls_ssl_session) );
3797
4.60k
}
3798
3799
static int ssl_handshake_init( mbedtls_ssl_context *ssl )
3800
4.60k
{
3801
    /* Clear old handshake information if present */
3802
4.60k
    if( ssl->transform_negotiate )
3803
0
        mbedtls_ssl_transform_free( ssl->transform_negotiate );
3804
4.60k
    if( ssl->session_negotiate )
3805
0
        mbedtls_ssl_session_free( ssl->session_negotiate );
3806
4.60k
    if( ssl->handshake )
3807
0
        mbedtls_ssl_handshake_free( ssl );
3808
3809
    /*
3810
     * Either the pointers are now NULL or cleared properly and can be freed.
3811
     * Now allocate missing structures.
3812
     */
3813
4.60k
    if( ssl->transform_negotiate == NULL )
3814
4.60k
    {
3815
4.60k
        ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
3816
4.60k
    }
3817
3818
4.60k
    if( ssl->session_negotiate == NULL )
3819
4.60k
    {
3820
4.60k
        ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
3821
4.60k
    }
3822
3823
4.60k
    if( ssl->handshake == NULL )
3824
4.60k
    {
3825
4.60k
        ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
3826
4.60k
    }
3827
#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3828
    /* If the buffers are too small - reallocate */
3829
3830
    handle_buffer_resizing( ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
3831
                                    MBEDTLS_SSL_OUT_BUFFER_LEN );
3832
#endif
3833
3834
    /* All pointers should exist and can be directly freed without issue */
3835
4.60k
    if( ssl->handshake == NULL ||
3836
4.60k
        ssl->transform_negotiate == NULL ||
3837
4.60k
        ssl->session_negotiate == NULL )
3838
0
    {
3839
0
        MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
3840
3841
0
        mbedtls_free( ssl->handshake );
3842
0
        mbedtls_free( ssl->transform_negotiate );
3843
0
        mbedtls_free( ssl->session_negotiate );
3844
3845
0
        ssl->handshake = NULL;
3846
0
        ssl->transform_negotiate = NULL;
3847
0
        ssl->session_negotiate = NULL;
3848
3849
0
        return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
3850
0
    }
3851
3852
    /* Initialize structures */
3853
4.60k
    mbedtls_ssl_session_init( ssl->session_negotiate );
3854
4.60k
    mbedtls_ssl_transform_init( ssl->transform_negotiate );
3855
4.60k
    ssl_handshake_params_init( ssl->handshake );
3856
3857
4.60k
#if defined(MBEDTLS_SSL_PROTO_DTLS)
3858
4.60k
    if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3859
4.60k
    {
3860
4.60k
        ssl->handshake->alt_transform_out = ssl->transform_out;
3861
3862
4.60k
        if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
3863
0
            ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
3864
4.60k
        else
3865
4.60k
            ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
3866
3867
4.60k
        mbedtls_ssl_set_timer( ssl, 0 );
3868
4.60k
    }
3869
4.60k
#endif
3870
3871
4.60k
    return( 0 );
3872
4.60k
}
3873
3874
#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
3875
/* Dummy cookie callbacks for defaults */
3876
static int ssl_cookie_write_dummy( void *ctx,
3877
                      unsigned char **p, unsigned char *end,
3878
                      const unsigned char *cli_id, size_t cli_id_len )
3879
0
{
3880
0
    ((void) ctx);
3881
0
    ((void) p);
3882
0
    ((void) end);
3883
0
    ((void) cli_id);
3884
0
    ((void) cli_id_len);
3885
3886
0
    return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3887
0
}
3888
3889
static int ssl_cookie_check_dummy( void *ctx,
3890
                      const unsigned char *cookie, size_t cookie_len,
3891
                      const unsigned char *cli_id, size_t cli_id_len )
3892
0
{
3893
0
    ((void) ctx);
3894
0
    ((void) cookie);
3895
0
    ((void) cookie_len);
3896
0
    ((void) cli_id);
3897
0
    ((void) cli_id_len);
3898
3899
0
    return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3900
0
}
3901
#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
3902
3903
/*
3904
 * Initialize an SSL context
3905
 */
3906
void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
3907
4.66k
{
3908
4.66k
    memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
3909
4.66k
}
3910
3911
/*
3912
 * Setup an SSL context
3913
 */
3914
3915
int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
3916
                       const mbedtls_ssl_config *conf )
3917
4.66k
{
3918
4.66k
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3919
4.66k
    size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
3920
4.66k
    size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
3921
3922
4.66k
    ssl->conf = conf;
3923
3924
    /*
3925
     * Prepare base structures
3926
     */
3927
3928
    /* Set to NULL in case of an error condition */
3929
4.66k
    ssl->out_buf = NULL;
3930
3931
#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3932
    ssl->in_buf_len = in_buf_len;
3933
#endif
3934
4.66k
    ssl->in_buf = mbedtls_calloc( 1, in_buf_len );
3935
4.66k
    if( ssl->in_buf == NULL )
3936
56
    {
3937
56
        MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len ) );
3938
56
        ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
3939
56
        goto error;
3940
56
    }
3941
3942
#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3943
    ssl->out_buf_len = out_buf_len;
3944
#endif
3945
4.61k
    ssl->out_buf = mbedtls_calloc( 1, out_buf_len );
3946
4.61k
    if( ssl->out_buf == NULL )
3947
1
    {
3948
1
        MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len ) );
3949
1
        ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
3950
1
        goto error;
3951
1
    }
3952
3953
4.60k
    mbedtls_ssl_reset_in_out_pointers( ssl );
3954
3955
#if defined(MBEDTLS_SSL_DTLS_SRTP)
3956
    memset( &ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info) );
3957
#endif
3958
3959
4.60k
    if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3960
0
        goto error;
3961
3962
4.60k
    return( 0 );
3963
3964
57
error:
3965
57
    mbedtls_free( ssl->in_buf );
3966
57
    mbedtls_free( ssl->out_buf );
3967
3968
57
    ssl->conf = NULL;
3969
3970
#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3971
    ssl->in_buf_len = 0;
3972
    ssl->out_buf_len = 0;
3973
#endif
3974
57
    ssl->in_buf = NULL;
3975
57
    ssl->out_buf = NULL;
3976
3977
57
    ssl->in_hdr = NULL;
3978
57
    ssl->in_ctr = NULL;
3979
57
    ssl->in_len = NULL;
3980
57
    ssl->in_iv = NULL;
3981
57
    ssl->in_msg = NULL;
3982
3983
57
    ssl->out_hdr = NULL;
3984
57
    ssl->out_ctr = NULL;
3985
57
    ssl->out_len = NULL;
3986
57
    ssl->out_iv = NULL;
3987
57
    ssl->out_msg = NULL;
3988
3989
57
    return( ret );
3990
4.60k
}
3991
3992
/*
3993
 * Reset an initialized and used SSL context for re-use while retaining
3994
 * all application-set variables, function pointers and data.
3995
 *
3996
 * If partial is non-zero, keep data in the input buffer and client ID.
3997
 * (Use when a DTLS client reconnects from the same port.)
3998
 */
3999
int mbedtls_ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
4000
0
{
4001
0
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
4002
#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4003
    size_t in_buf_len = ssl->in_buf_len;
4004
    size_t out_buf_len = ssl->out_buf_len;
4005
#else
4006
0
    size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4007
0
    size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
4008
0
#endif
4009
4010
0
#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) ||     \
4011
0
    !defined(MBEDTLS_SSL_SRV_C)
4012
0
    ((void) partial);
4013
0
#endif
4014
4015
0
    ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
4016
4017
    /* Cancel any possibly running timer */
4018
0
    mbedtls_ssl_set_timer( ssl, 0 );
4019
4020
#if defined(MBEDTLS_SSL_RENEGOTIATION)
4021
    ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
4022
    ssl->renego_records_seen = 0;
4023
4024
    ssl->verify_data_len = 0;
4025
    memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
4026
    memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
4027
#endif
4028
0
    ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
4029
4030
0
    ssl->in_offt = NULL;
4031
0
    mbedtls_ssl_reset_in_out_pointers( ssl );
4032
4033
0
    ssl->in_msgtype = 0;
4034
0
    ssl->in_msglen = 0;
4035
0
#if defined(MBEDTLS_SSL_PROTO_DTLS)
4036
0
    ssl->next_record_offset = 0;
4037
0
    ssl->in_epoch = 0;
4038
0
#endif
4039
0
#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4040
0
    mbedtls_ssl_dtls_replay_reset( ssl );
4041
0
#endif
4042
4043
0
    ssl->in_hslen = 0;
4044
0
    ssl->nb_zero = 0;
4045
4046
0
    ssl->keep_current_message = 0;
4047
4048
0
    ssl->out_msgtype = 0;
4049
0
    ssl->out_msglen = 0;
4050
0
    ssl->out_left = 0;
4051
#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
4052
    if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
4053
        ssl->split_done = 0;
4054
#endif
4055
4056
0
    memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
4057
4058
0
    ssl->transform_in = NULL;
4059
0
    ssl->transform_out = NULL;
4060
4061
0
    ssl->session_in = NULL;
4062
0
    ssl->session_out = NULL;
4063
4064
0
    memset( ssl->out_buf, 0, out_buf_len );
4065
4066
#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4067
    if( partial == 0 )
4068
#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
4069
0
    {
4070
0
        ssl->in_left = 0;
4071
0
        memset( ssl->in_buf, 0, in_buf_len );
4072
0
    }
4073
4074
#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4075
    if( mbedtls_ssl_hw_record_reset != NULL )
4076
    {
4077
        MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
4078
        if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
4079
        {
4080
            MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
4081
            return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
4082
        }
4083
    }
4084
#endif
4085
4086
0
    if( ssl->transform )
4087
0
    {
4088
0
        mbedtls_ssl_transform_free( ssl->transform );
4089
0
        mbedtls_free( ssl->transform );
4090
0
        ssl->transform = NULL;
4091
0
    }
4092
4093
0
    if( ssl->session )
4094
0
    {
4095
0
        mbedtls_ssl_session_free( ssl->session );
4096
0
        mbedtls_free( ssl->session );
4097
0
        ssl->session = NULL;
4098
0
    }
4099
4100
#if defined(MBEDTLS_SSL_ALPN)
4101
    ssl->alpn_chosen = NULL;
4102
#endif
4103
4104
0
#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
4105
#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
4106
    if( partial == 0 )
4107
#endif
4108
0
    {
4109
0
        mbedtls_free( ssl->cli_id );
4110
0
        ssl->cli_id = NULL;
4111
0
        ssl->cli_id_len = 0;
4112
0
    }
4113
0
#endif
4114
4115
0
    if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4116
0
        return( ret );
4117
4118
0
    return( 0 );
4119
0
}
4120
4121
/*
4122
 * Reset an initialized and used SSL context for re-use while retaining
4123
 * all application-set variables, function pointers and data.
4124
 */
4125
int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
4126
0
{
4127
0
    return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
4128
0
}
4129
4130
/*
4131
 * SSL set accessors
4132
 */
4133
void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
4134
4.60k
{
4135
4.60k
    conf->endpoint   = endpoint;
4136
4.60k
}
4137
4138
void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
4139
4.60k
{
4140
4.60k
    conf->transport = transport;
4141
4.60k
}
4142
4143
#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4144
void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
4145
0
{
4146
0
    conf->anti_replay = mode;
4147
0
}
4148
#endif
4149
4150
#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
4151
void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
4152
{
4153
    conf->badmac_limit = limit;
4154
}
4155
#endif
4156
4157
#if defined(MBEDTLS_SSL_PROTO_DTLS)
4158
4159
void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
4160
                                       unsigned allow_packing )
4161
0
{
4162
0
    ssl->disable_datagram_packing = !allow_packing;
4163
0
}
4164
4165
void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
4166
                                         uint32_t min, uint32_t max )
4167
4.60k
{
4168
4.60k
    conf->hs_timeout_min = min;
4169
4.60k
    conf->hs_timeout_max = max;
4170
4.60k
}
4171
#endif
4172
4173
void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
4174
4.66k
{
4175
4.66k
    conf->authmode   = authmode;
4176
4.66k
}
4177
4178
#if defined(MBEDTLS_X509_CRT_PARSE_C)
4179
void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
4180
                     int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
4181
                     void *p_vrfy )
4182
0
{
4183
0
    conf->f_vrfy      = f_vrfy;
4184
0
    conf->p_vrfy      = p_vrfy;
4185
0
}
4186
#endif /* MBEDTLS_X509_CRT_PARSE_C */
4187
4188
void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
4189
                  int (*f_rng)(void *, unsigned char *, size_t),
4190
                  void *p_rng )
4191
4.66k
{
4192
4.66k
    conf->f_rng      = f_rng;
4193
4.66k
    conf->p_rng      = p_rng;
4194
4.66k
}
4195
4196
void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
4197
                  void (*f_dbg)(void *, int, const char *, int, const char *),
4198
                  void  *p_dbg )
4199
4.60k
{
4200
4.60k
    conf->f_dbg      = f_dbg;
4201
4.60k
    conf->p_dbg      = p_dbg;
4202
4.60k
}
4203
4204
void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
4205
        void *p_bio,
4206
        mbedtls_ssl_send_t *f_send,
4207
        mbedtls_ssl_recv_t *f_recv,
4208
        mbedtls_ssl_recv_timeout_t *f_recv_timeout )
4209
4.60k
{
4210
4.60k
    ssl->p_bio          = p_bio;
4211
4.60k
    ssl->f_send         = f_send;
4212
4.60k
    ssl->f_recv         = f_recv;
4213
4.60k
    ssl->f_recv_timeout = f_recv_timeout;
4214
4.60k
}
4215
4216
#if defined(MBEDTLS_SSL_PROTO_DTLS)
4217
void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
4218
0
{
4219
0
    ssl->mtu = mtu;
4220
0
}
4221
#endif
4222
4223
void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
4224
0
{
4225
0
    conf->read_timeout   = timeout;
4226
0
}
4227
4228
void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
4229
                               void *p_timer,
4230
                               mbedtls_ssl_set_timer_t *f_set_timer,
4231
                               mbedtls_ssl_get_timer_t *f_get_timer )
4232
4.60k
{
4233
4.60k
    ssl->p_timer        = p_timer;
4234
4.60k
    ssl->f_set_timer    = f_set_timer;
4235
4.60k
    ssl->f_get_timer    = f_get_timer;
4236
4237
    /* Make sure we start with no timer running */
4238
4.60k
    mbedtls_ssl_set_timer( ssl, 0 );
4239
4.60k
}
4240
4241
#if defined(MBEDTLS_SSL_SRV_C)
4242
void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
4243
        void *p_cache,
4244
        int (*f_get_cache)(void *, mbedtls_ssl_session *),
4245
        int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
4246
0
{
4247
0
    conf->p_cache = p_cache;
4248
0
    conf->f_get_cache = f_get_cache;
4249
0
    conf->f_set_cache = f_set_cache;
4250
0
}
4251
#endif /* MBEDTLS_SSL_SRV_C */
4252
4253
#if defined(MBEDTLS_SSL_CLI_C)
4254
int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
4255
0
{
4256
0
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
4257
4258
0
    if( ssl == NULL ||
4259
0
        session == NULL ||
4260
0
        ssl->session_negotiate == NULL ||
4261
0
        ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
4262
0
    {
4263
0
        return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4264
0
    }
4265
4266
0
    if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
4267
0
                                          session ) ) != 0 )
4268
0
        return( ret );
4269
4270
0
    ssl->handshake->resume = 1;
4271
4272
0
    return( 0 );
4273
0
}
4274
#endif /* MBEDTLS_SSL_CLI_C */
4275
4276
void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
4277
                                   const int *ciphersuites )
4278
4.66k
{
4279
4.66k
    conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
4280
4.66k
    conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
4281
4.66k
    conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
4282
4.66k
    conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
4283
4.66k
}
4284
4285
void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
4286
                                       const int *ciphersuites,
4287
                                       int major, int minor )
4288
0
{
4289
0
    if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
4290
0
        return;
4291
4292
0
    if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
4293
0
        return;
4294
4295
0
    conf->ciphersuite_list[minor] = ciphersuites;
4296
0
}
4297
4298
#if defined(MBEDTLS_X509_CRT_PARSE_C)
4299
void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
4300
                                    const mbedtls_x509_crt_profile *profile )
4301
0
{
4302
0
    conf->cert_profile = profile;
4303
0
}
4304
4305
/* Append a new keycert entry to a (possibly empty) list */
4306
static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
4307
                                mbedtls_x509_crt *cert,
4308
                                mbedtls_pk_context *key )
4309
0
{
4310
0
    mbedtls_ssl_key_cert *new_cert;
4311
4312
0
    new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
4313
0
    if( new_cert == NULL )
4314
0
        return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
4315
4316
0
    new_cert->cert = cert;
4317
0
    new_cert->key  = key;
4318
0
    new_cert->next = NULL;
4319
4320
    /* Update head is the list was null, else add to the end */
4321
0
    if( *head == NULL )
4322
0
    {
4323
0
        *head = new_cert;
4324
0
    }
4325
0
    else
4326
0
    {
4327
0
        mbedtls_ssl_key_cert *cur = *head;
4328
0
        while( cur->next != NULL )
4329
0
            cur = cur->next;
4330
0
        cur->next = new_cert;
4331
0
    }
4332
4333
0
    return( 0 );
4334
0
}
4335
4336
int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
4337
                              mbedtls_x509_crt *own_cert,
4338
                              mbedtls_pk_context *pk_key )
4339
0
{
4340
0
    return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
4341
0
}
4342
4343
void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
4344
                               mbedtls_x509_crt *ca_chain,
4345
                               mbedtls_x509_crl *ca_crl )
4346
0
{
4347
0
    conf->ca_chain   = ca_chain;
4348
0
    conf->ca_crl     = ca_crl;
4349
4350
#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
4351
    /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
4352
     * cannot be used together. */
4353
    conf->f_ca_cb = NULL;
4354
    conf->p_ca_cb = NULL;
4355
#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
4356
0
}
4357
4358
#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
4359
void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
4360
                             mbedtls_x509_crt_ca_cb_t f_ca_cb,
4361
                             void *p_ca_cb )
4362
{
4363
    conf->f_ca_cb = f_ca_cb;
4364
    conf->p_ca_cb = p_ca_cb;
4365
4366
    /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
4367
     * cannot be used together. */
4368
    conf->ca_chain   = NULL;
4369
    conf->ca_crl     = NULL;
4370
}
4371
#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
4372
#endif /* MBEDTLS_X509_CRT_PARSE_C */
4373
4374
#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
4375
int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
4376
                                 mbedtls_x509_crt *own_cert,
4377
                                 mbedtls_pk_context *pk_key )
4378
{
4379
    return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
4380
                                 own_cert, pk_key ) );
4381
}
4382
4383
void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
4384
                                  mbedtls_x509_crt *ca_chain,
4385
                                  mbedtls_x509_crl *ca_crl )
4386
{
4387
    ssl->handshake->sni_ca_chain   = ca_chain;
4388
    ssl->handshake->sni_ca_crl     = ca_crl;
4389
}
4390
4391
void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
4392
                                  int authmode )
4393
{
4394
    ssl->handshake->sni_authmode = authmode;
4395
}
4396
#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
4397
4398
#if defined(MBEDTLS_X509_CRT_PARSE_C)
4399
void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
4400
                     int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
4401
                     void *p_vrfy )
4402
0
{
4403
0
    ssl->f_vrfy = f_vrfy;
4404
0
    ssl->p_vrfy = p_vrfy;
4405
0
}
4406
#endif
4407
4408
#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
4409
/*
4410
 * Set EC J-PAKE password for current handshake
4411
 */
4412
int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
4413
                                         const unsigned char *pw,
4414
                                         size_t pw_len )
4415
4.60k
{
4416
4.60k
    mbedtls_ecjpake_role role;
4417
4418
4.60k
    if( ssl->handshake == NULL || ssl->conf == NULL )
4419
0
        return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4420
4421
4.60k
    if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
4422
4.60k
        role = MBEDTLS_ECJPAKE_SERVER;
4423
0
    else
4424
0
        role = MBEDTLS_ECJPAKE_CLIENT;
4425
4426
4.60k
    return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
4427
4.60k
                                   role,
4428
4.60k
                                   MBEDTLS_MD_SHA256,
4429
4.60k
                                   MBEDTLS_ECP_DP_SECP256R1,
4430
4.60k
                                   pw, pw_len ) );
4431
4.60k
}
4432
#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
4433
4434
#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
4435
4436
static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
4437
0
{
4438
    /* Remove reference to existing PSK, if any. */
4439
#if defined(MBEDTLS_USE_PSA_CRYPTO)
4440
    if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
4441
    {
4442
        /* The maintenance of the PSK key slot is the
4443
         * user's responsibility. */
4444
        conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4445
    }
4446
    /* This and the following branch should never
4447
     * be taken simultaenously as we maintain the
4448
     * invariant that raw and opaque PSKs are never
4449
     * configured simultaneously. As a safeguard,
4450
     * though, `else` is omitted here. */
4451
#endif /* MBEDTLS_USE_PSA_CRYPTO */
4452
0
    if( conf->psk != NULL )
4453
0
    {
4454
0
        mbedtls_platform_zeroize( conf->psk, conf->psk_len );
4455
4456
0
        mbedtls_free( conf->psk );
4457
0
        conf->psk = NULL;
4458
0
        conf->psk_len = 0;
4459
0
    }
4460
4461
    /* Remove reference to PSK identity, if any. */
4462
0
    if( conf->psk_identity != NULL )
4463
0
    {
4464
0
        mbedtls_free( conf->psk_identity );
4465
0
        conf->psk_identity = NULL;
4466
0
        conf->psk_identity_len = 0;
4467
0
    }
4468
0
}
4469
4470
/* This function assumes that PSK identity in the SSL config is unset.
4471
 * It checks that the provided identity is well-formed and attempts
4472
 * to make a copy of it in the SSL config.
4473
 * On failure, the PSK identity in the config remains unset. */
4474
static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
4475
                                      unsigned char const *psk_identity,
4476
                                      size_t psk_identity_len )
4477
0
{
4478
    /* Identity len will be encoded on two bytes */
4479
0
    if( psk_identity               == NULL ||
4480
0
        ( psk_identity_len >> 16 ) != 0    ||
4481
0
        psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
4482
0
    {
4483
0
        return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4484
0
    }
4485
4486
0
    conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
4487
0
    if( conf->psk_identity == NULL )
4488
0
        return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
4489
4490
0
    conf->psk_identity_len = psk_identity_len;
4491
0
    memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
4492
4493
0
    return( 0 );
4494
0
}
4495
4496
int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
4497
                const unsigned char *psk, size_t psk_len,
4498
                const unsigned char *psk_identity, size_t psk_identity_len )
4499
0
{
4500
0
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
4501
    /* Remove opaque/raw PSK + PSK Identity */
4502
0
    ssl_conf_remove_psk( conf );
4503
4504
    /* Check and set raw PSK */
4505
0
    if( psk == NULL )
4506
0
        return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4507
0
    if( psk_len == 0 )
4508
0
        return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4509
0
    if( psk_len > MBEDTLS_PSK_MAX_LEN )
4510
0
        return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4511
4512
0
    if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
4513
0
        return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
4514
0
    conf->psk_len = psk_len;
4515
0
    memcpy( conf->psk, psk, conf->psk_len );
4516
4517
    /* Check and set PSK Identity */
4518
0
    ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
4519
0
    if( ret != 0 )
4520
0
        ssl_conf_remove_psk( conf );
4521
4522
0
    return( ret );
4523
0
}
4524
4525
static void ssl_remove_psk( mbedtls_ssl_context *ssl )
4526
0
{
4527
#if defined(MBEDTLS_USE_PSA_CRYPTO)
4528
    if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
4529
    {
4530
        ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4531
    }
4532
    else
4533
#endif /* MBEDTLS_USE_PSA_CRYPTO */
4534
0
    if( ssl->handshake->psk != NULL )
4535
0
    {
4536
0
        mbedtls_platform_zeroize( ssl->handshake->psk,
4537
0
                                  ssl->handshake->psk_len );
4538
0
        mbedtls_free( ssl->handshake->psk );
4539
0
        ssl->handshake->psk_len = 0;
4540
0
    }
4541
0
}
4542
4543
int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
4544
                            const unsigned char *psk, size_t psk_len )
4545
0
{
4546
0
    if( psk == NULL || ssl->handshake == NULL )
4547
0
        return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4548
4549
0
    if( psk_len > MBEDTLS_PSK_MAX_LEN )
4550
0
        return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4551
4552
0
    ssl_remove_psk( ssl );
4553
4554
0
    if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
4555
0
        return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
4556
4557
0
    ssl->handshake->psk_len = psk_len;
4558
0
    memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
4559
4560
0
    return( 0 );
4561
0
}
4562
4563
#if defined(MBEDTLS_USE_PSA_CRYPTO)
4564
int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
4565
                                 psa_key_id_t psk,
4566
                                 const unsigned char *psk_identity,
4567
                                 size_t psk_identity_len )
4568
{
4569
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
4570
    /* Clear opaque/raw PSK + PSK Identity, if present. */
4571
    ssl_conf_remove_psk( conf );
4572
4573
    /* Check and set opaque PSK */
4574
    if( mbedtls_svc_key_id_is_null( psk ) )
4575
        return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4576
    conf->psk_opaque = psk;
4577
4578
    /* Check and set PSK Identity */
4579
    ret = ssl_conf_set_psk_identity( conf, psk_identity,
4580
                                     psk_identity_len );
4581
    if( ret != 0 )
4582
        ssl_conf_remove_psk( conf );
4583
4584
    return( ret );
4585
}
4586
4587
int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
4588
                                   psa_key_id_t psk )
4589
{
4590
    if( ( mbedtls_svc_key_id_is_null( psk ) ) ||
4591
        ( ssl->handshake == NULL ) )
4592
        return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4593
4594
    ssl_remove_psk( ssl );
4595
    ssl->handshake->psk_opaque = psk;
4596
    return( 0 );
4597
}
4598
#endif /* MBEDTLS_USE_PSA_CRYPTO */
4599
4600
void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
4601
                     int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
4602
                     size_t),
4603
                     void *p_psk )
4604
0
{
4605
0
    conf->f_psk = f_psk;
4606
0
    conf->p_psk = p_psk;
4607
0
}
4608
#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
4609
4610
#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
4611
4612
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
4613
int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
4614
{
4615
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
4616
4617
    if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
4618
        ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
4619
    {
4620
        mbedtls_mpi_free( &conf->dhm_P );
4621
        mbedtls_mpi_free( &conf->dhm_G );
4622
        return( ret );
4623
    }
4624
4625
    return( 0 );
4626
}
4627
#endif /* MBEDTLS_DEPRECATED_REMOVED */
4628
4629
int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
4630
                                   const unsigned char *dhm_P, size_t P_len,
4631
                                   const unsigned char *dhm_G, size_t G_len )
4632
{
4633
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
4634
4635
    if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
4636
        ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
4637
    {
4638
        mbedtls_mpi_free( &conf->dhm_P );
4639
        mbedtls_mpi_free( &conf->dhm_G );
4640
        return( ret );
4641
    }
4642
4643
    return( 0 );
4644
}
4645
4646
int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
4647
{
4648
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
4649
4650
    if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
4651
        ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
4652
    {
4653
        mbedtls_mpi_free( &conf->dhm_P );
4654
        mbedtls_mpi_free( &conf->dhm_G );
4655
        return( ret );
4656
    }
4657
4658
    return( 0 );
4659
}
4660
#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
4661
4662
#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
4663
/*
4664
 * Set the minimum length for Diffie-Hellman parameters
4665
 */
4666
void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
4667
                                      unsigned int bitlen )
4668
{
4669
    conf->dhm_min_bitlen = bitlen;
4670
}
4671
#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
4672
4673
#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
4674
/*
4675
 * Set allowed/preferred hashes for handshake signatures
4676
 */
4677
void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
4678
                                  const int *hashes )
4679
4.60k
{
4680
4.60k
    conf->sig_hashes = hashes;
4681
4.60k
}
4682
#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
4683
4684
#if defined(MBEDTLS_ECP_C)
4685
/*
4686
 * Set the allowed elliptic curves
4687
 */
4688
void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
4689
                             const mbedtls_ecp_group_id *curve_list )
4690
4.60k
{
4691
4.60k
    conf->curve_list = curve_list;
4692
4.60k
}
4693
#endif /* MBEDTLS_ECP_C */
4694
4695
#if defined(MBEDTLS_X509_CRT_PARSE_C)
4696
int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
4697
0
{
4698
    /* Initialize to suppress unnecessary compiler warning */
4699
0
    size_t hostname_len = 0;
4700
4701
    /* Check if new hostname is valid before
4702
     * making any change to current one */
4703
0
    if( hostname != NULL )
4704
0
    {
4705
0
        hostname_len = strlen( hostname );
4706
4707
0
        if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
4708
0
            return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4709
0
    }
4710
4711
    /* Now it's clear that we will overwrite the old hostname,
4712
     * so we can free it safely */
4713
4714
0
    if( ssl->hostname != NULL )
4715
0
    {
4716
0
        mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
4717
0
        mbedtls_free( ssl->hostname );
4718
0
    }
4719
4720
    /* Passing NULL as hostname shall clear the old one */
4721
4722
0
    if( hostname == NULL )
4723
0
    {
4724
0
        ssl->hostname = NULL;
4725
0
    }
4726
0
    else
4727
0
    {
4728
0
        ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
4729
0
        if( ssl->hostname == NULL )
4730
0
            return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
4731
4732
0
        memcpy( ssl->hostname, hostname, hostname_len );
4733
4734
0
        ssl->hostname[hostname_len] = '\0';
4735
0
    }
4736
4737
0
    return( 0 );
4738
0
}
4739
#endif /* MBEDTLS_X509_CRT_PARSE_C */
4740
4741
#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
4742
void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
4743
                  int (*f_sni)(void *, mbedtls_ssl_context *,
4744
                                const unsigned char *, size_t),
4745
                  void *p_sni )
4746
{
4747
    conf->f_sni = f_sni;
4748
    conf->p_sni = p_sni;
4749
}
4750
#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
4751
4752
#if defined(MBEDTLS_SSL_ALPN)
4753
int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
4754
{
4755
    size_t cur_len, tot_len;
4756
    const char **p;
4757
4758
    /*
4759
     * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
4760
     * MUST NOT be truncated."
4761
     * We check lengths now rather than later.
4762
     */
4763
    tot_len = 0;
4764
    for( p = protos; *p != NULL; p++ )
4765
    {
4766
        cur_len = strlen( *p );
4767
        tot_len += cur_len;
4768
4769
        if( ( cur_len == 0 ) ||
4770
            ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) ||
4771
            ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) )
4772
            return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4773
    }
4774
4775
    conf->alpn_list = protos;
4776
4777
    return( 0 );
4778
}
4779
4780
const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
4781
{
4782
    return( ssl->alpn_chosen );
4783
}
4784
#endif /* MBEDTLS_SSL_ALPN */
4785
4786
#if defined(MBEDTLS_SSL_DTLS_SRTP)
4787
void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf,
4788
                                                int support_mki_value )
4789
{
4790
    conf->dtls_srtp_mki_support = support_mki_value;
4791
}
4792
4793
int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl,
4794
                                         unsigned char *mki_value,
4795
                                         uint16_t mki_len )
4796
{
4797
    if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH )
4798
    {
4799
        return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4800
    }
4801
4802
    if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED )
4803
    {
4804
        return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4805
    }
4806
4807
    memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len );
4808
    ssl->dtls_srtp_info.mki_len = mki_len;
4809
    return( 0 );
4810
}
4811
4812
int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf,
4813
                                                    const mbedtls_ssl_srtp_profile *profiles )
4814
{
4815
    const mbedtls_ssl_srtp_profile *p;
4816
    size_t list_size = 0;
4817
4818
    /* check the profiles list: all entry must be valid,
4819
     * its size cannot be more than the total number of supported profiles, currently 4 */
4820
    for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
4821
                       list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
4822
         p++ )
4823
    {
4824
        if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET )
4825
        {
4826
            list_size++;
4827
        }
4828
        else
4829
        {
4830
            /* unsupported value, stop parsing and set the size to an error value */
4831
            list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
4832
        }
4833
    }
4834
4835
    if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH )
4836
    {
4837
                conf->dtls_srtp_profile_list = NULL;
4838
                conf->dtls_srtp_profile_list_len = 0;
4839
                return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4840
    }
4841
4842
    conf->dtls_srtp_profile_list = profiles;
4843
    conf->dtls_srtp_profile_list_len = list_size;
4844
4845
    return( 0 );
4846
}
4847
4848
void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl,
4849
                                                   mbedtls_dtls_srtp_info *dtls_srtp_info )
4850
{
4851
    dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
4852
    /* do not copy the mki value if there is no chosen profile */
4853
    if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
4854
    {
4855
        dtls_srtp_info->mki_len = 0;
4856
    }
4857
    else
4858
    {
4859
        dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
4860
        memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
4861
                ssl->dtls_srtp_info.mki_len );
4862
    }
4863
}
4864
#endif /* MBEDTLS_SSL_DTLS_SRTP */
4865
4866
void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
4867
4.66k
{
4868
4.66k
    conf->max_major_ver = major;
4869
4.66k
    conf->max_minor_ver = minor;
4870
4.66k
}
4871
4872
void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
4873
4.66k
{
4874
4.66k
    conf->min_major_ver = major;
4875
4.66k
    conf->min_minor_ver = minor;
4876
4.66k
}
4877
4878
#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
4879
void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
4880
{
4881
    conf->fallback = fallback;
4882
}
4883
#endif
4884
4885
#if defined(MBEDTLS_SSL_SRV_C)
4886
void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
4887
                                          char cert_req_ca_list )
4888
0
{
4889
0
    conf->cert_req_ca_list = cert_req_ca_list;
4890
0
}
4891
#endif
4892
4893
#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
4894
void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
4895
{
4896
    conf->encrypt_then_mac = etm;
4897
}
4898
#endif
4899
4900
#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
4901
void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
4902
{
4903
    conf->extended_ms = ems;
4904
}
4905
#endif
4906
4907
#if defined(MBEDTLS_ARC4_C)
4908
void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
4909
{
4910
    conf->arc4_disabled = arc4;
4911
}
4912
#endif
4913
4914
#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
4915
int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
4916
0
{
4917
0
    if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
4918
0
        ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
4919
0
    {
4920
0
        return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4921
0
    }
4922
4923
0
    conf->mfl_code = mfl_code;
4924
4925
0
    return( 0 );
4926
0
}
4927
#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
4928
4929
#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
4930
void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
4931
{
4932
    conf->trunc_hmac = truncate;
4933
}
4934
#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
4935
4936
#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
4937
void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
4938
{
4939
    conf->cbc_record_splitting = split;
4940
}
4941
#endif
4942
4943
void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
4944
0
{
4945
0
    conf->allow_legacy_renegotiation = allow_legacy;
4946
0
}
4947
4948
#if defined(MBEDTLS_SSL_RENEGOTIATION)
4949
void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
4950
{
4951
    conf->disable_renegotiation = renegotiation;
4952
}
4953
4954
void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
4955
{
4956
    conf->renego_max_records = max_records;
4957
}
4958
4959
void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
4960
                                   const unsigned char period[8] )
4961
{
4962
    memcpy( conf->renego_period, period, 8 );
4963
}
4964
#endif /* MBEDTLS_SSL_RENEGOTIATION */
4965
4966
#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4967
#if defined(MBEDTLS_SSL_CLI_C)
4968
void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
4969
{
4970
    conf->session_tickets = use_tickets;
4971
}
4972
#endif
4973
4974
#if defined(MBEDTLS_SSL_SRV_C)
4975
void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
4976
        mbedtls_ssl_ticket_write_t *f_ticket_write,
4977
        mbedtls_ssl_ticket_parse_t *f_ticket_parse,
4978
        void *p_ticket )
4979
{
4980
    conf->f_ticket_write = f_ticket_write;
4981
    conf->f_ticket_parse = f_ticket_parse;
4982
    conf->p_ticket       = p_ticket;
4983
}
4984
#endif
4985
#endif /* MBEDTLS_SSL_SESSION_TICKETS */
4986
4987
#if defined(MBEDTLS_SSL_EXPORT_KEYS)
4988
void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
4989
        mbedtls_ssl_export_keys_t *f_export_keys,
4990
        void *p_export_keys )
4991
4.60k
{
4992
4.60k
    conf->f_export_keys = f_export_keys;
4993
4.60k
    conf->p_export_keys = p_export_keys;
4994
4.60k
}
4995
4996
void mbedtls_ssl_conf_export_keys_ext_cb( mbedtls_ssl_config *conf,
4997
        mbedtls_ssl_export_keys_ext_t *f_export_keys_ext,
4998
        void *p_export_keys )
4999
0
{
5000
0
    conf->f_export_keys_ext = f_export_keys_ext;
5001
0
    conf->p_export_keys = p_export_keys;
5002
0
}
5003
#endif
5004
5005
#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
5006
void mbedtls_ssl_conf_async_private_cb(
5007
    mbedtls_ssl_config *conf,
5008
    mbedtls_ssl_async_sign_t *f_async_sign,
5009
    mbedtls_ssl_async_decrypt_t *f_async_decrypt,
5010
    mbedtls_ssl_async_resume_t *f_async_resume,
5011
    mbedtls_ssl_async_cancel_t *f_async_cancel,
5012
    void *async_config_data )
5013
{
5014
    conf->f_async_sign_start = f_async_sign;
5015
    conf->f_async_decrypt_start = f_async_decrypt;
5016
    conf->f_async_resume = f_async_resume;
5017
    conf->f_async_cancel = f_async_cancel;
5018
    conf->p_async_config_data = async_config_data;
5019
}
5020
5021
void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
5022
{
5023
    return( conf->p_async_config_data );
5024
}
5025
5026
void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
5027
{
5028
    if( ssl->handshake == NULL )
5029
        return( NULL );
5030
    else
5031
        return( ssl->handshake->user_async_ctx );
5032
}
5033
5034
void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
5035
                                 void *ctx )
5036
{
5037
    if( ssl->handshake != NULL )
5038
        ssl->handshake->user_async_ctx = ctx;
5039
}
5040
#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
5041
5042
/*
5043
 * SSL get accessors
5044
 */
5045
uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
5046
0
{
5047
0
    if( ssl->session != NULL )
5048
0
        return( ssl->session->verify_result );
5049
5050
0
    if( ssl->session_negotiate != NULL )
5051
0
        return( ssl->session_negotiate->verify_result );
5052
5053
0
    return( 0xFFFFFFFF );
5054
0
}
5055
5056
const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
5057
0
{
5058
0
    if( ssl == NULL || ssl->session == NULL )
5059
0
        return( NULL );
5060
5061
0
    return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
5062
0
}
5063
5064
const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
5065
0
{
5066
0
#if defined(MBEDTLS_SSL_PROTO_DTLS)
5067
0
    if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5068
0
    {
5069
0
        switch( ssl->minor_ver )
5070
0
        {
5071
0
            case MBEDTLS_SSL_MINOR_VERSION_2:
5072
0
                return( "DTLSv1.0" );
5073
5074
0
            case MBEDTLS_SSL_MINOR_VERSION_3:
5075
0
                return( "DTLSv1.2" );
5076
5077
0
            default:
5078
0
                return( "unknown (DTLS)" );
5079
0
        }
5080
0
    }
5081
0
#endif
5082
5083
0
    switch( ssl->minor_ver )
5084
0
    {
5085
0
        case MBEDTLS_SSL_MINOR_VERSION_0:
5086
0
            return( "SSLv3.0" );
5087
5088
0
        case MBEDTLS_SSL_MINOR_VERSION_1:
5089
0
            return( "TLSv1.0" );
5090
5091
0
        case MBEDTLS_SSL_MINOR_VERSION_2:
5092
0
            return( "TLSv1.1" );
5093
5094
0
        case MBEDTLS_SSL_MINOR_VERSION_3:
5095
0
            return( "TLSv1.2" );
5096
5097
0
        default:
5098
0
            return( "unknown" );
5099
0
    }
5100
0
}
5101
5102
#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
5103
size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl )
5104
0
{
5105
0
    size_t max_len = MBEDTLS_SSL_MAX_CONTENT_LEN;
5106
0
    size_t read_mfl;
5107
5108
    /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
5109
0
    if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
5110
0
        ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE )
5111
0
    {
5112
0
        return ssl_mfl_code_to_length( ssl->conf->mfl_code );
5113
0
    }
5114
5115
    /* Check if a smaller max length was negotiated */
5116
0
    if( ssl->session_out != NULL )
5117
0
    {
5118
0
        read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
5119
0
        if( read_mfl < max_len )
5120
0
        {
5121
0
            max_len = read_mfl;
5122
0
        }
5123
0
    }
5124
5125
    // During a handshake, use the value being negotiated
5126
0
    if( ssl->session_negotiate != NULL )
5127
0
    {
5128
0
        read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
5129
0
        if( read_mfl < max_len )
5130
0
        {
5131
0
            max_len = read_mfl;
5132
0
        }
5133
0
    }
5134
5135
0
    return( max_len );
5136
0
}
5137
5138
size_t mbedtls_ssl_get_output_max_frag_len( const mbedtls_ssl_context *ssl )
5139
0
{
5140
0
    size_t max_len;
5141
5142
    /*
5143
     * Assume mfl_code is correct since it was checked when set
5144
     */
5145
0
    max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
5146
5147
    /* Check if a smaller max length was negotiated */
5148
0
    if( ssl->session_out != NULL &&
5149
0
        ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
5150
0
    {
5151
0
        max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
5152
0
    }
5153
5154
    /* During a handshake, use the value being negotiated */
5155
0
    if( ssl->session_negotiate != NULL &&
5156
0
        ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
5157
0
    {
5158
0
        max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
5159
0
    }
5160
5161
0
    return( max_len );
5162
0
}
5163
5164
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
5165
size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
5166
0
{
5167
0
    return mbedtls_ssl_get_output_max_frag_len( ssl );
5168
0
}
5169
#endif /* !MBEDTLS_DEPRECATED_REMOVED */
5170
#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
5171
5172
#if defined(MBEDTLS_SSL_PROTO_DTLS)
5173
size_t mbedtls_ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
5174
4.54k
{
5175
    /* Return unlimited mtu for client hello messages to avoid fragmentation. */
5176
4.54k
    if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
5177
4.54k
        ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
5178
0
          ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
5179
0
        return ( 0 );
5180
5181
4.54k
    if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
5182
4.54k
        return( ssl->mtu );
5183
5184
0
    if( ssl->mtu == 0 )
5185
0
        return( ssl->handshake->mtu );