1a8e1175bSopenharmony_ci/**
2a8e1175bSopenharmony_ci * \file ssl_misc.h
3a8e1175bSopenharmony_ci *
4a8e1175bSopenharmony_ci * \brief Internal functions shared by the SSL modules
5a8e1175bSopenharmony_ci */
6a8e1175bSopenharmony_ci/*
7a8e1175bSopenharmony_ci *  Copyright The Mbed TLS Contributors
8a8e1175bSopenharmony_ci *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
9a8e1175bSopenharmony_ci */
10a8e1175bSopenharmony_ci#ifndef MBEDTLS_SSL_MISC_H
11a8e1175bSopenharmony_ci#define MBEDTLS_SSL_MISC_H
12a8e1175bSopenharmony_ci
13a8e1175bSopenharmony_ci#include "mbedtls/build_info.h"
14a8e1175bSopenharmony_ci
15a8e1175bSopenharmony_ci#include "mbedtls/error.h"
16a8e1175bSopenharmony_ci
17a8e1175bSopenharmony_ci#include "mbedtls/ssl.h"
18a8e1175bSopenharmony_ci#include "mbedtls/cipher.h"
19a8e1175bSopenharmony_ci
20a8e1175bSopenharmony_ci#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
21a8e1175bSopenharmony_ci#include "psa/crypto.h"
22a8e1175bSopenharmony_ci#include "psa_util_internal.h"
23a8e1175bSopenharmony_ci#endif
24a8e1175bSopenharmony_ci
25a8e1175bSopenharmony_ci#if defined(MBEDTLS_MD_CAN_MD5)
26a8e1175bSopenharmony_ci#include "mbedtls/md5.h"
27a8e1175bSopenharmony_ci#endif
28a8e1175bSopenharmony_ci
29a8e1175bSopenharmony_ci#if defined(MBEDTLS_MD_CAN_SHA1)
30a8e1175bSopenharmony_ci#include "mbedtls/sha1.h"
31a8e1175bSopenharmony_ci#endif
32a8e1175bSopenharmony_ci
33a8e1175bSopenharmony_ci#if defined(MBEDTLS_MD_CAN_SHA256)
34a8e1175bSopenharmony_ci#include "mbedtls/sha256.h"
35a8e1175bSopenharmony_ci#endif
36a8e1175bSopenharmony_ci
37a8e1175bSopenharmony_ci#if defined(MBEDTLS_MD_CAN_SHA512)
38a8e1175bSopenharmony_ci#include "mbedtls/sha512.h"
39a8e1175bSopenharmony_ci#endif
40a8e1175bSopenharmony_ci
41a8e1175bSopenharmony_ci#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) && \
42a8e1175bSopenharmony_ci    !defined(MBEDTLS_USE_PSA_CRYPTO)
43a8e1175bSopenharmony_ci#include "mbedtls/ecjpake.h"
44a8e1175bSopenharmony_ci#endif
45a8e1175bSopenharmony_ci
46a8e1175bSopenharmony_ci#include "mbedtls/pk.h"
47a8e1175bSopenharmony_ci#include "ssl_ciphersuites_internal.h"
48a8e1175bSopenharmony_ci#include "x509_internal.h"
49a8e1175bSopenharmony_ci#include "pk_internal.h"
50a8e1175bSopenharmony_ci#include "common.h"
51a8e1175bSopenharmony_ci
52a8e1175bSopenharmony_ci/* Shorthand for restartable ECC */
53a8e1175bSopenharmony_ci#if defined(MBEDTLS_ECP_RESTARTABLE) && \
54a8e1175bSopenharmony_ci    defined(MBEDTLS_SSL_CLI_C) && \
55a8e1175bSopenharmony_ci    defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
56a8e1175bSopenharmony_ci    defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
57a8e1175bSopenharmony_ci#define MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED
58a8e1175bSopenharmony_ci#endif
59a8e1175bSopenharmony_ci
60a8e1175bSopenharmony_ci#define MBEDTLS_SSL_INITIAL_HANDSHAKE           0
61a8e1175bSopenharmony_ci#define MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS   1   /* In progress */
62a8e1175bSopenharmony_ci#define MBEDTLS_SSL_RENEGOTIATION_DONE          2   /* Done or aborted */
63a8e1175bSopenharmony_ci#define MBEDTLS_SSL_RENEGOTIATION_PENDING       3   /* Requested (server only) */
64a8e1175bSopenharmony_ci
65a8e1175bSopenharmony_ci/* Faked handshake message identity for HelloRetryRequest. */
66a8e1175bSopenharmony_ci#define MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST (-MBEDTLS_SSL_HS_SERVER_HELLO)
67a8e1175bSopenharmony_ci
68a8e1175bSopenharmony_ci/*
69a8e1175bSopenharmony_ci * Internal identity of handshake extensions
70a8e1175bSopenharmony_ci */
71a8e1175bSopenharmony_ci#define MBEDTLS_SSL_EXT_ID_UNRECOGNIZED                0
72a8e1175bSopenharmony_ci#define MBEDTLS_SSL_EXT_ID_SERVERNAME                  1
73a8e1175bSopenharmony_ci#define MBEDTLS_SSL_EXT_ID_SERVERNAME_HOSTNAME         1
74a8e1175bSopenharmony_ci#define MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH         2
75a8e1175bSopenharmony_ci#define MBEDTLS_SSL_EXT_ID_STATUS_REQUEST              3
76a8e1175bSopenharmony_ci#define MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS            4
77a8e1175bSopenharmony_ci#define MBEDTLS_SSL_EXT_ID_SUPPORTED_ELLIPTIC_CURVES   4
78a8e1175bSopenharmony_ci#define MBEDTLS_SSL_EXT_ID_SIG_ALG                     5
79a8e1175bSopenharmony_ci#define MBEDTLS_SSL_EXT_ID_USE_SRTP                    6
80a8e1175bSopenharmony_ci#define MBEDTLS_SSL_EXT_ID_HEARTBEAT                   7
81a8e1175bSopenharmony_ci#define MBEDTLS_SSL_EXT_ID_ALPN                        8
82a8e1175bSopenharmony_ci#define MBEDTLS_SSL_EXT_ID_SCT                         9
83a8e1175bSopenharmony_ci#define MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE              10
84a8e1175bSopenharmony_ci#define MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE             11
85a8e1175bSopenharmony_ci#define MBEDTLS_SSL_EXT_ID_PADDING                    12
86a8e1175bSopenharmony_ci#define MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY             13
87a8e1175bSopenharmony_ci#define MBEDTLS_SSL_EXT_ID_EARLY_DATA                 14
88a8e1175bSopenharmony_ci#define MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS         15
89a8e1175bSopenharmony_ci#define MBEDTLS_SSL_EXT_ID_COOKIE                     16
90a8e1175bSopenharmony_ci#define MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES     17
91a8e1175bSopenharmony_ci#define MBEDTLS_SSL_EXT_ID_CERT_AUTH                  18
92a8e1175bSopenharmony_ci#define MBEDTLS_SSL_EXT_ID_OID_FILTERS                19
93a8e1175bSopenharmony_ci#define MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH        20
94a8e1175bSopenharmony_ci#define MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT               21
95a8e1175bSopenharmony_ci#define MBEDTLS_SSL_EXT_ID_KEY_SHARE                  22
96a8e1175bSopenharmony_ci#define MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC             23
97a8e1175bSopenharmony_ci#define MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS    24
98a8e1175bSopenharmony_ci#define MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC           25
99a8e1175bSopenharmony_ci#define MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET     26
100a8e1175bSopenharmony_ci#define MBEDTLS_SSL_EXT_ID_SESSION_TICKET             27
101a8e1175bSopenharmony_ci#define MBEDTLS_SSL_EXT_ID_RECORD_SIZE_LIMIT          28
102a8e1175bSopenharmony_ci
103a8e1175bSopenharmony_ci/* Utility for translating IANA extension type. */
104a8e1175bSopenharmony_ciuint32_t mbedtls_ssl_get_extension_id(unsigned int extension_type);
105a8e1175bSopenharmony_ciuint32_t mbedtls_ssl_get_extension_mask(unsigned int extension_type);
106a8e1175bSopenharmony_ci/* Macros used to define mask constants */
107a8e1175bSopenharmony_ci#define MBEDTLS_SSL_EXT_MASK(id)       (1ULL << (MBEDTLS_SSL_EXT_ID_##id))
108a8e1175bSopenharmony_ci/* Reset value of extension mask */
109a8e1175bSopenharmony_ci#define MBEDTLS_SSL_EXT_MASK_NONE                                              0
110a8e1175bSopenharmony_ci
111a8e1175bSopenharmony_ci/* In messages containing extension requests, we should ignore unrecognized
112a8e1175bSopenharmony_ci * extensions. In messages containing extension responses, unrecognized
113a8e1175bSopenharmony_ci * extensions should result in handshake abortion. Messages containing
114a8e1175bSopenharmony_ci * extension requests include ClientHello, CertificateRequest and
115a8e1175bSopenharmony_ci * NewSessionTicket. Messages containing extension responses include
116a8e1175bSopenharmony_ci * ServerHello, HelloRetryRequest, EncryptedExtensions and Certificate.
117a8e1175bSopenharmony_ci *
118a8e1175bSopenharmony_ci * RFC 8446 section 4.1.3
119a8e1175bSopenharmony_ci *
120a8e1175bSopenharmony_ci * The ServerHello MUST only include extensions which are required to establish
121a8e1175bSopenharmony_ci * the cryptographic context and negotiate the protocol version.
122a8e1175bSopenharmony_ci *
123a8e1175bSopenharmony_ci * RFC 8446 section 4.2
124a8e1175bSopenharmony_ci *
125a8e1175bSopenharmony_ci * If an implementation receives an extension which it recognizes and which is
126a8e1175bSopenharmony_ci * not specified for the message in which it appears, it MUST abort the handshake
127a8e1175bSopenharmony_ci * with an "illegal_parameter" alert.
128a8e1175bSopenharmony_ci */
129a8e1175bSopenharmony_ci
130a8e1175bSopenharmony_ci/* Extensions that are not recognized by TLS 1.3 */
131a8e1175bSopenharmony_ci#define MBEDTLS_SSL_TLS1_3_EXT_MASK_UNRECOGNIZED                               \
132a8e1175bSopenharmony_ci    (MBEDTLS_SSL_EXT_MASK(SUPPORTED_POINT_FORMATS)                | \
133a8e1175bSopenharmony_ci     MBEDTLS_SSL_EXT_MASK(ENCRYPT_THEN_MAC)                       | \
134a8e1175bSopenharmony_ci     MBEDTLS_SSL_EXT_MASK(EXTENDED_MASTER_SECRET)                 | \
135a8e1175bSopenharmony_ci     MBEDTLS_SSL_EXT_MASK(SESSION_TICKET)                         | \
136a8e1175bSopenharmony_ci     MBEDTLS_SSL_EXT_MASK(TRUNCATED_HMAC)                         | \
137a8e1175bSopenharmony_ci     MBEDTLS_SSL_EXT_MASK(UNRECOGNIZED))
138a8e1175bSopenharmony_ci
139a8e1175bSopenharmony_ci/* RFC 8446 section 4.2. Allowed extensions for ClientHello */
140a8e1175bSopenharmony_ci#define MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_CH                                  \
141a8e1175bSopenharmony_ci    (MBEDTLS_SSL_EXT_MASK(SERVERNAME)                             | \
142a8e1175bSopenharmony_ci     MBEDTLS_SSL_EXT_MASK(MAX_FRAGMENT_LENGTH)                    | \
143a8e1175bSopenharmony_ci     MBEDTLS_SSL_EXT_MASK(STATUS_REQUEST)                         | \
144a8e1175bSopenharmony_ci     MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS)                       | \
145a8e1175bSopenharmony_ci     MBEDTLS_SSL_EXT_MASK(SIG_ALG)                                | \
146a8e1175bSopenharmony_ci     MBEDTLS_SSL_EXT_MASK(USE_SRTP)                               | \
147a8e1175bSopenharmony_ci     MBEDTLS_SSL_EXT_MASK(HEARTBEAT)                              | \
148a8e1175bSopenharmony_ci     MBEDTLS_SSL_EXT_MASK(ALPN)                                   | \
149a8e1175bSopenharmony_ci     MBEDTLS_SSL_EXT_MASK(SCT)                                    | \
150a8e1175bSopenharmony_ci     MBEDTLS_SSL_EXT_MASK(CLI_CERT_TYPE)                          | \
151a8e1175bSopenharmony_ci     MBEDTLS_SSL_EXT_MASK(SERV_CERT_TYPE)                         | \
152a8e1175bSopenharmony_ci     MBEDTLS_SSL_EXT_MASK(PADDING)                                | \
153a8e1175bSopenharmony_ci     MBEDTLS_SSL_EXT_MASK(KEY_SHARE)                              | \
154a8e1175bSopenharmony_ci     MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY)                         | \
155a8e1175bSopenharmony_ci     MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES)                 | \
156a8e1175bSopenharmony_ci     MBEDTLS_SSL_EXT_MASK(EARLY_DATA)                             | \
157a8e1175bSopenharmony_ci     MBEDTLS_SSL_EXT_MASK(COOKIE)                                 | \
158a8e1175bSopenharmony_ci     MBEDTLS_SSL_EXT_MASK(SUPPORTED_VERSIONS)                     | \
159a8e1175bSopenharmony_ci     MBEDTLS_SSL_EXT_MASK(CERT_AUTH)                              | \
160a8e1175bSopenharmony_ci     MBEDTLS_SSL_EXT_MASK(POST_HANDSHAKE_AUTH)                    | \
161a8e1175bSopenharmony_ci     MBEDTLS_SSL_EXT_MASK(SIG_ALG_CERT)                           | \
162a8e1175bSopenharmony_ci     MBEDTLS_SSL_EXT_MASK(RECORD_SIZE_LIMIT)                      | \
163a8e1175bSopenharmony_ci     MBEDTLS_SSL_TLS1_3_EXT_MASK_UNRECOGNIZED)
164a8e1175bSopenharmony_ci
165a8e1175bSopenharmony_ci/* RFC 8446 section 4.2. Allowed extensions for EncryptedExtensions */
166a8e1175bSopenharmony_ci#define MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_EE                                  \
167a8e1175bSopenharmony_ci    (MBEDTLS_SSL_EXT_MASK(SERVERNAME)                             | \
168a8e1175bSopenharmony_ci     MBEDTLS_SSL_EXT_MASK(MAX_FRAGMENT_LENGTH)                    | \
169a8e1175bSopenharmony_ci     MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS)                       | \
170a8e1175bSopenharmony_ci     MBEDTLS_SSL_EXT_MASK(USE_SRTP)                               | \
171a8e1175bSopenharmony_ci     MBEDTLS_SSL_EXT_MASK(HEARTBEAT)                              | \
172a8e1175bSopenharmony_ci     MBEDTLS_SSL_EXT_MASK(ALPN)                                   | \
173a8e1175bSopenharmony_ci     MBEDTLS_SSL_EXT_MASK(CLI_CERT_TYPE)                          | \
174a8e1175bSopenharmony_ci     MBEDTLS_SSL_EXT_MASK(SERV_CERT_TYPE)                         | \
175a8e1175bSopenharmony_ci     MBEDTLS_SSL_EXT_MASK(EARLY_DATA)                             | \
176a8e1175bSopenharmony_ci     MBEDTLS_SSL_EXT_MASK(RECORD_SIZE_LIMIT))
177a8e1175bSopenharmony_ci
178a8e1175bSopenharmony_ci/* RFC 8446 section 4.2. Allowed extensions for CertificateRequest */
179a8e1175bSopenharmony_ci#define MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_CR                                  \
180a8e1175bSopenharmony_ci    (MBEDTLS_SSL_EXT_MASK(STATUS_REQUEST)                         | \
181a8e1175bSopenharmony_ci     MBEDTLS_SSL_EXT_MASK(SIG_ALG)                                | \
182a8e1175bSopenharmony_ci     MBEDTLS_SSL_EXT_MASK(SCT)                                    | \
183a8e1175bSopenharmony_ci     MBEDTLS_SSL_EXT_MASK(CERT_AUTH)                              | \
184a8e1175bSopenharmony_ci     MBEDTLS_SSL_EXT_MASK(OID_FILTERS)                            | \
185a8e1175bSopenharmony_ci     MBEDTLS_SSL_EXT_MASK(SIG_ALG_CERT)                           | \
186a8e1175bSopenharmony_ci     MBEDTLS_SSL_TLS1_3_EXT_MASK_UNRECOGNIZED)
187a8e1175bSopenharmony_ci
188a8e1175bSopenharmony_ci/* RFC 8446 section 4.2. Allowed extensions for Certificate */
189a8e1175bSopenharmony_ci#define MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_CT                                  \
190a8e1175bSopenharmony_ci    (MBEDTLS_SSL_EXT_MASK(STATUS_REQUEST)                         | \
191a8e1175bSopenharmony_ci     MBEDTLS_SSL_EXT_MASK(SCT))
192a8e1175bSopenharmony_ci
193a8e1175bSopenharmony_ci/* RFC 8446 section 4.2. Allowed extensions for ServerHello */
194a8e1175bSopenharmony_ci#define MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_SH                                  \
195a8e1175bSopenharmony_ci    (MBEDTLS_SSL_EXT_MASK(KEY_SHARE)                              | \
196a8e1175bSopenharmony_ci     MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY)                         | \
197a8e1175bSopenharmony_ci     MBEDTLS_SSL_EXT_MASK(SUPPORTED_VERSIONS))
198a8e1175bSopenharmony_ci
199a8e1175bSopenharmony_ci/* RFC 8446 section 4.2. Allowed extensions for HelloRetryRequest */
200a8e1175bSopenharmony_ci#define MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_HRR                                 \
201a8e1175bSopenharmony_ci    (MBEDTLS_SSL_EXT_MASK(KEY_SHARE)                              | \
202a8e1175bSopenharmony_ci     MBEDTLS_SSL_EXT_MASK(COOKIE)                                 | \
203a8e1175bSopenharmony_ci     MBEDTLS_SSL_EXT_MASK(SUPPORTED_VERSIONS))
204a8e1175bSopenharmony_ci
205a8e1175bSopenharmony_ci/* RFC 8446 section 4.2. Allowed extensions for NewSessionTicket */
206a8e1175bSopenharmony_ci#define MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_NST                                 \
207a8e1175bSopenharmony_ci    (MBEDTLS_SSL_EXT_MASK(EARLY_DATA)                             | \
208a8e1175bSopenharmony_ci     MBEDTLS_SSL_TLS1_3_EXT_MASK_UNRECOGNIZED)
209a8e1175bSopenharmony_ci
210a8e1175bSopenharmony_ci/*
211a8e1175bSopenharmony_ci * Helper macros for function call with return check.
212a8e1175bSopenharmony_ci */
213a8e1175bSopenharmony_ci/*
214a8e1175bSopenharmony_ci * Exit when return non-zero value
215a8e1175bSopenharmony_ci */
216a8e1175bSopenharmony_ci#define MBEDTLS_SSL_PROC_CHK(f)                               \
217a8e1175bSopenharmony_ci    do {                                                        \
218a8e1175bSopenharmony_ci        ret = (f);                                            \
219a8e1175bSopenharmony_ci        if (ret != 0)                                          \
220a8e1175bSopenharmony_ci        {                                                       \
221a8e1175bSopenharmony_ci            goto cleanup;                                       \
222a8e1175bSopenharmony_ci        }                                                       \
223a8e1175bSopenharmony_ci    } while (0)
224a8e1175bSopenharmony_ci/*
225a8e1175bSopenharmony_ci * Exit when return negative value
226a8e1175bSopenharmony_ci */
227a8e1175bSopenharmony_ci#define MBEDTLS_SSL_PROC_CHK_NEG(f)                           \
228a8e1175bSopenharmony_ci    do {                                                        \
229a8e1175bSopenharmony_ci        ret = (f);                                            \
230a8e1175bSopenharmony_ci        if (ret < 0)                                           \
231a8e1175bSopenharmony_ci        {                                                       \
232a8e1175bSopenharmony_ci            goto cleanup;                                       \
233a8e1175bSopenharmony_ci        }                                                       \
234a8e1175bSopenharmony_ci    } while (0)
235a8e1175bSopenharmony_ci
236a8e1175bSopenharmony_ci/*
237a8e1175bSopenharmony_ci * DTLS retransmission states, see RFC 6347 4.2.4
238a8e1175bSopenharmony_ci *
239a8e1175bSopenharmony_ci * The SENDING state is merged in PREPARING for initial sends,
240a8e1175bSopenharmony_ci * but is distinct for resends.
241a8e1175bSopenharmony_ci *
242a8e1175bSopenharmony_ci * Note: initial state is wrong for server, but is not used anyway.
243a8e1175bSopenharmony_ci */
244a8e1175bSopenharmony_ci#define MBEDTLS_SSL_RETRANS_PREPARING       0
245a8e1175bSopenharmony_ci#define MBEDTLS_SSL_RETRANS_SENDING         1
246a8e1175bSopenharmony_ci#define MBEDTLS_SSL_RETRANS_WAITING         2
247a8e1175bSopenharmony_ci#define MBEDTLS_SSL_RETRANS_FINISHED        3
248a8e1175bSopenharmony_ci
249a8e1175bSopenharmony_ci/*
250a8e1175bSopenharmony_ci * Allow extra bytes for record, authentication and encryption overhead:
251a8e1175bSopenharmony_ci * counter (8) + header (5) + IV(16) + MAC (16-48) + padding (0-256).
252a8e1175bSopenharmony_ci */
253a8e1175bSopenharmony_ci
254a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
255a8e1175bSopenharmony_ci
256a8e1175bSopenharmony_ci/* This macro determines whether CBC is supported. */
257a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_HAVE_CBC)      &&                                  \
258a8e1175bSopenharmony_ci    (defined(MBEDTLS_SSL_HAVE_AES)     ||                                  \
259a8e1175bSopenharmony_ci    defined(MBEDTLS_SSL_HAVE_CAMELLIA) ||                                  \
260a8e1175bSopenharmony_ci    defined(MBEDTLS_SSL_HAVE_ARIA))
261a8e1175bSopenharmony_ci#define MBEDTLS_SSL_SOME_SUITES_USE_CBC
262a8e1175bSopenharmony_ci#endif
263a8e1175bSopenharmony_ci
264a8e1175bSopenharmony_ci/* This macro determines whether a ciphersuite using a
265a8e1175bSopenharmony_ci * stream cipher can be used. */
266a8e1175bSopenharmony_ci#if defined(MBEDTLS_CIPHER_NULL_CIPHER)
267a8e1175bSopenharmony_ci#define MBEDTLS_SSL_SOME_SUITES_USE_STREAM
268a8e1175bSopenharmony_ci#endif
269a8e1175bSopenharmony_ci
270a8e1175bSopenharmony_ci/* This macro determines whether the CBC construct used in TLS 1.2 is supported. */
271a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC) && \
272a8e1175bSopenharmony_ci    defined(MBEDTLS_SSL_PROTO_TLS1_2)
273a8e1175bSopenharmony_ci#define MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC
274a8e1175bSopenharmony_ci#endif
275a8e1175bSopenharmony_ci
276a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_SOME_SUITES_USE_STREAM) || \
277a8e1175bSopenharmony_ci    defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC)
278a8e1175bSopenharmony_ci#define MBEDTLS_SSL_SOME_SUITES_USE_MAC
279a8e1175bSopenharmony_ci#endif
280a8e1175bSopenharmony_ci
281a8e1175bSopenharmony_ci/* This macro determines whether a ciphersuite uses Encrypt-then-MAC with CBC */
282a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC) && \
283a8e1175bSopenharmony_ci    defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
284a8e1175bSopenharmony_ci#define MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM
285a8e1175bSopenharmony_ci#endif
286a8e1175bSopenharmony_ci
287a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
288a8e1175bSopenharmony_ci
289a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
290a8e1175bSopenharmony_ci/* Ciphersuites using HMAC */
291a8e1175bSopenharmony_ci#if defined(MBEDTLS_MD_CAN_SHA384)
292a8e1175bSopenharmony_ci#define MBEDTLS_SSL_MAC_ADD                 48  /* SHA-384 used for HMAC */
293a8e1175bSopenharmony_ci#elif defined(MBEDTLS_MD_CAN_SHA256)
294a8e1175bSopenharmony_ci#define MBEDTLS_SSL_MAC_ADD                 32  /* SHA-256 used for HMAC */
295a8e1175bSopenharmony_ci#else
296a8e1175bSopenharmony_ci#define MBEDTLS_SSL_MAC_ADD                 20  /* SHA-1   used for HMAC */
297a8e1175bSopenharmony_ci#endif
298a8e1175bSopenharmony_ci#else /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
299a8e1175bSopenharmony_ci/* AEAD ciphersuites: GCM and CCM use a 128 bits tag */
300a8e1175bSopenharmony_ci#define MBEDTLS_SSL_MAC_ADD                 16
301a8e1175bSopenharmony_ci#endif
302a8e1175bSopenharmony_ci
303a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_HAVE_CBC)
304a8e1175bSopenharmony_ci#define MBEDTLS_SSL_PADDING_ADD            256
305a8e1175bSopenharmony_ci#else
306a8e1175bSopenharmony_ci#define MBEDTLS_SSL_PADDING_ADD              0
307a8e1175bSopenharmony_ci#endif
308a8e1175bSopenharmony_ci
309a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
310a8e1175bSopenharmony_ci#define MBEDTLS_SSL_MAX_CID_EXPANSION      MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY
311a8e1175bSopenharmony_ci#else
312a8e1175bSopenharmony_ci#define MBEDTLS_SSL_MAX_CID_EXPANSION        0
313a8e1175bSopenharmony_ci#endif
314a8e1175bSopenharmony_ci
315a8e1175bSopenharmony_ci#define MBEDTLS_SSL_PAYLOAD_OVERHEAD (MBEDTLS_MAX_IV_LENGTH +          \
316a8e1175bSopenharmony_ci                                      MBEDTLS_SSL_MAC_ADD +            \
317a8e1175bSopenharmony_ci                                      MBEDTLS_SSL_PADDING_ADD +        \
318a8e1175bSopenharmony_ci                                      MBEDTLS_SSL_MAX_CID_EXPANSION    \
319a8e1175bSopenharmony_ci                                      )
320a8e1175bSopenharmony_ci
321a8e1175bSopenharmony_ci#define MBEDTLS_SSL_IN_PAYLOAD_LEN (MBEDTLS_SSL_PAYLOAD_OVERHEAD + \
322a8e1175bSopenharmony_ci                                    (MBEDTLS_SSL_IN_CONTENT_LEN))
323a8e1175bSopenharmony_ci
324a8e1175bSopenharmony_ci#define MBEDTLS_SSL_OUT_PAYLOAD_LEN (MBEDTLS_SSL_PAYLOAD_OVERHEAD + \
325a8e1175bSopenharmony_ci                                     (MBEDTLS_SSL_OUT_CONTENT_LEN))
326a8e1175bSopenharmony_ci
327a8e1175bSopenharmony_ci/* The maximum number of buffered handshake messages. */
328a8e1175bSopenharmony_ci#define MBEDTLS_SSL_MAX_BUFFERED_HS 4
329a8e1175bSopenharmony_ci
330a8e1175bSopenharmony_ci/* Maximum length we can advertise as our max content length for
331a8e1175bSopenharmony_ci   RFC 6066 max_fragment_length extension negotiation purposes
332a8e1175bSopenharmony_ci   (the lesser of both sizes, if they are unequal.)
333a8e1175bSopenharmony_ci */
334a8e1175bSopenharmony_ci#define MBEDTLS_TLS_EXT_ADV_CONTENT_LEN (                            \
335a8e1175bSopenharmony_ci        (MBEDTLS_SSL_IN_CONTENT_LEN > MBEDTLS_SSL_OUT_CONTENT_LEN)   \
336a8e1175bSopenharmony_ci        ? (MBEDTLS_SSL_OUT_CONTENT_LEN)                            \
337a8e1175bSopenharmony_ci        : (MBEDTLS_SSL_IN_CONTENT_LEN)                             \
338a8e1175bSopenharmony_ci        )
339a8e1175bSopenharmony_ci
340a8e1175bSopenharmony_ci/* Maximum size in bytes of list in signature algorithms ext., RFC 5246/8446 */
341a8e1175bSopenharmony_ci#define MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN       65534
342a8e1175bSopenharmony_ci
343a8e1175bSopenharmony_ci/* Minimum size in bytes of list in signature algorithms ext., RFC 5246/8446 */
344a8e1175bSopenharmony_ci#define MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN       2
345a8e1175bSopenharmony_ci
346a8e1175bSopenharmony_ci/* Maximum size in bytes of list in supported elliptic curve ext., RFC 4492 */
347a8e1175bSopenharmony_ci#define MBEDTLS_SSL_MAX_CURVE_LIST_LEN         65535
348a8e1175bSopenharmony_ci
349a8e1175bSopenharmony_ci#define MBEDTLS_RECEIVED_SIG_ALGS_SIZE         20
350a8e1175bSopenharmony_ci
351a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
352a8e1175bSopenharmony_ci
353a8e1175bSopenharmony_ci#define MBEDTLS_TLS_SIG_NONE MBEDTLS_TLS1_3_SIG_NONE
354a8e1175bSopenharmony_ci
355a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
356a8e1175bSopenharmony_ci#define MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(sig, hash) ((hash << 8) | sig)
357a8e1175bSopenharmony_ci#define MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(alg) (alg & 0xFF)
358a8e1175bSopenharmony_ci#define MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(alg) (alg >> 8)
359a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
360a8e1175bSopenharmony_ci
361a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
362a8e1175bSopenharmony_ci
363a8e1175bSopenharmony_ci/*
364a8e1175bSopenharmony_ci * Check that we obey the standard's message size bounds
365a8e1175bSopenharmony_ci */
366a8e1175bSopenharmony_ci
367a8e1175bSopenharmony_ci#if MBEDTLS_SSL_IN_CONTENT_LEN > 16384
368a8e1175bSopenharmony_ci#error "Bad configuration - incoming record content too large."
369a8e1175bSopenharmony_ci#endif
370a8e1175bSopenharmony_ci
371a8e1175bSopenharmony_ci#if MBEDTLS_SSL_OUT_CONTENT_LEN > 16384
372a8e1175bSopenharmony_ci#error "Bad configuration - outgoing record content too large."
373a8e1175bSopenharmony_ci#endif
374a8e1175bSopenharmony_ci
375a8e1175bSopenharmony_ci#if MBEDTLS_SSL_IN_PAYLOAD_LEN > MBEDTLS_SSL_IN_CONTENT_LEN + 2048
376a8e1175bSopenharmony_ci#error "Bad configuration - incoming protected record payload too large."
377a8e1175bSopenharmony_ci#endif
378a8e1175bSopenharmony_ci
379a8e1175bSopenharmony_ci#if MBEDTLS_SSL_OUT_PAYLOAD_LEN > MBEDTLS_SSL_OUT_CONTENT_LEN + 2048
380a8e1175bSopenharmony_ci#error "Bad configuration - outgoing protected record payload too large."
381a8e1175bSopenharmony_ci#endif
382a8e1175bSopenharmony_ci
383a8e1175bSopenharmony_ci/* Calculate buffer sizes */
384a8e1175bSopenharmony_ci
385a8e1175bSopenharmony_ci/* Note: Even though the TLS record header is only 5 bytes
386a8e1175bSopenharmony_ci   long, we're internally using 8 bytes to store the
387a8e1175bSopenharmony_ci   implicit sequence number. */
388a8e1175bSopenharmony_ci#define MBEDTLS_SSL_HEADER_LEN 13
389a8e1175bSopenharmony_ci
390a8e1175bSopenharmony_ci#if !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
391a8e1175bSopenharmony_ci#define MBEDTLS_SSL_IN_BUFFER_LEN  \
392a8e1175bSopenharmony_ci    ((MBEDTLS_SSL_HEADER_LEN) + (MBEDTLS_SSL_IN_PAYLOAD_LEN))
393a8e1175bSopenharmony_ci#else
394a8e1175bSopenharmony_ci#define MBEDTLS_SSL_IN_BUFFER_LEN  \
395a8e1175bSopenharmony_ci    ((MBEDTLS_SSL_HEADER_LEN) + (MBEDTLS_SSL_IN_PAYLOAD_LEN) \
396a8e1175bSopenharmony_ci     + (MBEDTLS_SSL_CID_IN_LEN_MAX))
397a8e1175bSopenharmony_ci#endif
398a8e1175bSopenharmony_ci
399a8e1175bSopenharmony_ci#if !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
400a8e1175bSopenharmony_ci#define MBEDTLS_SSL_OUT_BUFFER_LEN  \
401a8e1175bSopenharmony_ci    ((MBEDTLS_SSL_HEADER_LEN) + (MBEDTLS_SSL_OUT_PAYLOAD_LEN))
402a8e1175bSopenharmony_ci#else
403a8e1175bSopenharmony_ci#define MBEDTLS_SSL_OUT_BUFFER_LEN                               \
404a8e1175bSopenharmony_ci    ((MBEDTLS_SSL_HEADER_LEN) + (MBEDTLS_SSL_OUT_PAYLOAD_LEN)    \
405a8e1175bSopenharmony_ci     + (MBEDTLS_SSL_CID_OUT_LEN_MAX))
406a8e1175bSopenharmony_ci#endif
407a8e1175bSopenharmony_ci
408a8e1175bSopenharmony_ci#define MBEDTLS_CLIENT_HELLO_RANDOM_LEN 32
409a8e1175bSopenharmony_ci#define MBEDTLS_SERVER_HELLO_RANDOM_LEN 32
410a8e1175bSopenharmony_ci
411a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
412a8e1175bSopenharmony_ci/**
413a8e1175bSopenharmony_ci * \brief          Return the maximum fragment length (payload, in bytes) for
414a8e1175bSopenharmony_ci *                 the output buffer. For the client, this is the configured
415a8e1175bSopenharmony_ci *                 value. For the server, it is the minimum of two - the
416a8e1175bSopenharmony_ci *                 configured value and the negotiated one.
417a8e1175bSopenharmony_ci *
418a8e1175bSopenharmony_ci * \sa             mbedtls_ssl_conf_max_frag_len()
419a8e1175bSopenharmony_ci * \sa             mbedtls_ssl_get_max_out_record_payload()
420a8e1175bSopenharmony_ci *
421a8e1175bSopenharmony_ci * \param ssl      SSL context
422a8e1175bSopenharmony_ci *
423a8e1175bSopenharmony_ci * \return         Current maximum fragment length for the output buffer.
424a8e1175bSopenharmony_ci */
425a8e1175bSopenharmony_cisize_t mbedtls_ssl_get_output_max_frag_len(const mbedtls_ssl_context *ssl);
426a8e1175bSopenharmony_ci
427a8e1175bSopenharmony_ci/**
428a8e1175bSopenharmony_ci * \brief          Return the maximum fragment length (payload, in bytes) for
429a8e1175bSopenharmony_ci *                 the input buffer. This is the negotiated maximum fragment
430a8e1175bSopenharmony_ci *                 length, or, if there is none, MBEDTLS_SSL_IN_CONTENT_LEN.
431a8e1175bSopenharmony_ci *                 If it is not defined either, the value is 2^14. This function
432a8e1175bSopenharmony_ci *                 works as its predecessor, \c mbedtls_ssl_get_max_frag_len().
433a8e1175bSopenharmony_ci *
434a8e1175bSopenharmony_ci * \sa             mbedtls_ssl_conf_max_frag_len()
435a8e1175bSopenharmony_ci * \sa             mbedtls_ssl_get_max_in_record_payload()
436a8e1175bSopenharmony_ci *
437a8e1175bSopenharmony_ci * \param ssl      SSL context
438a8e1175bSopenharmony_ci *
439a8e1175bSopenharmony_ci * \return         Current maximum fragment length for the output buffer.
440a8e1175bSopenharmony_ci */
441a8e1175bSopenharmony_cisize_t mbedtls_ssl_get_input_max_frag_len(const mbedtls_ssl_context *ssl);
442a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
443a8e1175bSopenharmony_ci
444a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
445a8e1175bSopenharmony_ci/**
446a8e1175bSopenharmony_ci * \brief    Get the size limit in bytes for the protected outgoing records
447a8e1175bSopenharmony_ci *           as defined in RFC 8449
448a8e1175bSopenharmony_ci *
449a8e1175bSopenharmony_ci * \param ssl      SSL context
450a8e1175bSopenharmony_ci *
451a8e1175bSopenharmony_ci * \return         The size limit in bytes for the protected outgoing
452a8e1175bSopenharmony_ci *                 records as defined in RFC 8449.
453a8e1175bSopenharmony_ci */
454a8e1175bSopenharmony_cisize_t mbedtls_ssl_get_output_record_size_limit(const mbedtls_ssl_context *ssl);
455a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
456a8e1175bSopenharmony_ci
457a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
458a8e1175bSopenharmony_cistatic inline size_t mbedtls_ssl_get_output_buflen(const mbedtls_ssl_context *ctx)
459a8e1175bSopenharmony_ci{
460a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
461a8e1175bSopenharmony_ci    return mbedtls_ssl_get_output_max_frag_len(ctx)
462a8e1175bSopenharmony_ci           + MBEDTLS_SSL_HEADER_LEN + MBEDTLS_SSL_PAYLOAD_OVERHEAD
463a8e1175bSopenharmony_ci           + MBEDTLS_SSL_CID_OUT_LEN_MAX;
464a8e1175bSopenharmony_ci#else
465a8e1175bSopenharmony_ci    return mbedtls_ssl_get_output_max_frag_len(ctx)
466a8e1175bSopenharmony_ci           + MBEDTLS_SSL_HEADER_LEN + MBEDTLS_SSL_PAYLOAD_OVERHEAD;
467a8e1175bSopenharmony_ci#endif
468a8e1175bSopenharmony_ci}
469a8e1175bSopenharmony_ci
470a8e1175bSopenharmony_cistatic inline size_t mbedtls_ssl_get_input_buflen(const mbedtls_ssl_context *ctx)
471a8e1175bSopenharmony_ci{
472a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
473a8e1175bSopenharmony_ci    return mbedtls_ssl_get_input_max_frag_len(ctx)
474a8e1175bSopenharmony_ci           + MBEDTLS_SSL_HEADER_LEN + MBEDTLS_SSL_PAYLOAD_OVERHEAD
475a8e1175bSopenharmony_ci           + MBEDTLS_SSL_CID_IN_LEN_MAX;
476a8e1175bSopenharmony_ci#else
477a8e1175bSopenharmony_ci    return mbedtls_ssl_get_input_max_frag_len(ctx)
478a8e1175bSopenharmony_ci           + MBEDTLS_SSL_HEADER_LEN + MBEDTLS_SSL_PAYLOAD_OVERHEAD;
479a8e1175bSopenharmony_ci#endif
480a8e1175bSopenharmony_ci}
481a8e1175bSopenharmony_ci#endif
482a8e1175bSopenharmony_ci
483a8e1175bSopenharmony_ci/*
484a8e1175bSopenharmony_ci * TLS extension flags (for extensions with outgoing ServerHello content
485a8e1175bSopenharmony_ci * that need it (e.g. for RENEGOTIATION_INFO the server already knows because
486a8e1175bSopenharmony_ci * of state of the renegotiation flag, so no indicator is required)
487a8e1175bSopenharmony_ci */
488a8e1175bSopenharmony_ci#define MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT (1 << 0)
489a8e1175bSopenharmony_ci#define MBEDTLS_TLS_EXT_ECJPAKE_KKPP_OK                 (1 << 1)
490a8e1175bSopenharmony_ci
491a8e1175bSopenharmony_ci/**
492a8e1175bSopenharmony_ci * \brief        This function checks if the remaining size in a buffer is
493a8e1175bSopenharmony_ci *               greater or equal than a needed space.
494a8e1175bSopenharmony_ci *
495a8e1175bSopenharmony_ci * \param cur    Pointer to the current position in the buffer.
496a8e1175bSopenharmony_ci * \param end    Pointer to one past the end of the buffer.
497a8e1175bSopenharmony_ci * \param need   Needed space in bytes.
498a8e1175bSopenharmony_ci *
499a8e1175bSopenharmony_ci * \return       Zero if the needed space is available in the buffer, non-zero
500a8e1175bSopenharmony_ci *               otherwise.
501a8e1175bSopenharmony_ci */
502a8e1175bSopenharmony_ci#if !defined(MBEDTLS_TEST_HOOKS)
503a8e1175bSopenharmony_cistatic inline int mbedtls_ssl_chk_buf_ptr(const uint8_t *cur,
504a8e1175bSopenharmony_ci                                          const uint8_t *end, size_t need)
505a8e1175bSopenharmony_ci{
506a8e1175bSopenharmony_ci    return (cur > end) || (need > (size_t) (end - cur));
507a8e1175bSopenharmony_ci}
508a8e1175bSopenharmony_ci#else
509a8e1175bSopenharmony_citypedef struct {
510a8e1175bSopenharmony_ci    const uint8_t *cur;
511a8e1175bSopenharmony_ci    const uint8_t *end;
512a8e1175bSopenharmony_ci    size_t need;
513a8e1175bSopenharmony_ci} mbedtls_ssl_chk_buf_ptr_args;
514a8e1175bSopenharmony_ci
515a8e1175bSopenharmony_civoid mbedtls_ssl_set_chk_buf_ptr_fail_args(
516a8e1175bSopenharmony_ci    const uint8_t *cur, const uint8_t *end, size_t need);
517a8e1175bSopenharmony_civoid mbedtls_ssl_reset_chk_buf_ptr_fail_args(void);
518a8e1175bSopenharmony_ci
519a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL
520a8e1175bSopenharmony_ciint mbedtls_ssl_cmp_chk_buf_ptr_fail_args(mbedtls_ssl_chk_buf_ptr_args *args);
521a8e1175bSopenharmony_ci
522a8e1175bSopenharmony_cistatic inline int mbedtls_ssl_chk_buf_ptr(const uint8_t *cur,
523a8e1175bSopenharmony_ci                                          const uint8_t *end, size_t need)
524a8e1175bSopenharmony_ci{
525a8e1175bSopenharmony_ci    if ((cur > end) || (need > (size_t) (end - cur))) {
526a8e1175bSopenharmony_ci        mbedtls_ssl_set_chk_buf_ptr_fail_args(cur, end, need);
527a8e1175bSopenharmony_ci        return 1;
528a8e1175bSopenharmony_ci    }
529a8e1175bSopenharmony_ci    return 0;
530a8e1175bSopenharmony_ci}
531a8e1175bSopenharmony_ci#endif /* MBEDTLS_TEST_HOOKS */
532a8e1175bSopenharmony_ci
533a8e1175bSopenharmony_ci/**
534a8e1175bSopenharmony_ci * \brief        This macro checks if the remaining size in a buffer is
535a8e1175bSopenharmony_ci *               greater or equal than a needed space. If it is not the case,
536a8e1175bSopenharmony_ci *               it returns an SSL_BUFFER_TOO_SMALL error.
537a8e1175bSopenharmony_ci *
538a8e1175bSopenharmony_ci * \param cur    Pointer to the current position in the buffer.
539a8e1175bSopenharmony_ci * \param end    Pointer to one past the end of the buffer.
540a8e1175bSopenharmony_ci * \param need   Needed space in bytes.
541a8e1175bSopenharmony_ci *
542a8e1175bSopenharmony_ci */
543a8e1175bSopenharmony_ci#define MBEDTLS_SSL_CHK_BUF_PTR(cur, end, need)                        \
544a8e1175bSopenharmony_ci    do {                                                                 \
545a8e1175bSopenharmony_ci        if (mbedtls_ssl_chk_buf_ptr((cur), (end), (need)) != 0) \
546a8e1175bSopenharmony_ci        {                                                                \
547a8e1175bSopenharmony_ci            return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;                  \
548a8e1175bSopenharmony_ci        }                                                                \
549a8e1175bSopenharmony_ci    } while (0)
550a8e1175bSopenharmony_ci
551a8e1175bSopenharmony_ci/**
552a8e1175bSopenharmony_ci * \brief        This macro checks if the remaining length in an input buffer is
553a8e1175bSopenharmony_ci *               greater or equal than a needed length. If it is not the case, it
554a8e1175bSopenharmony_ci *               returns #MBEDTLS_ERR_SSL_DECODE_ERROR error and pends a
555a8e1175bSopenharmony_ci *               #MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR alert message.
556a8e1175bSopenharmony_ci *
557a8e1175bSopenharmony_ci *               This is a function-like macro. It is guaranteed to evaluate each
558a8e1175bSopenharmony_ci *               argument exactly once.
559a8e1175bSopenharmony_ci *
560a8e1175bSopenharmony_ci * \param cur    Pointer to the current position in the buffer.
561a8e1175bSopenharmony_ci * \param end    Pointer to one past the end of the buffer.
562a8e1175bSopenharmony_ci * \param need   Needed length in bytes.
563a8e1175bSopenharmony_ci *
564a8e1175bSopenharmony_ci */
565a8e1175bSopenharmony_ci#define MBEDTLS_SSL_CHK_BUF_READ_PTR(cur, end, need)                          \
566a8e1175bSopenharmony_ci    do {                                                                        \
567a8e1175bSopenharmony_ci        if (mbedtls_ssl_chk_buf_ptr((cur), (end), (need)) != 0)        \
568a8e1175bSopenharmony_ci        {                                                                       \
569a8e1175bSopenharmony_ci            MBEDTLS_SSL_DEBUG_MSG(1,                                           \
570a8e1175bSopenharmony_ci                                  ("missing input data in %s", __func__));  \
571a8e1175bSopenharmony_ci            MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,   \
572a8e1175bSopenharmony_ci                                         MBEDTLS_ERR_SSL_DECODE_ERROR);       \
573a8e1175bSopenharmony_ci            return MBEDTLS_ERR_SSL_DECODE_ERROR;                             \
574a8e1175bSopenharmony_ci        }                                                                       \
575a8e1175bSopenharmony_ci    } while (0)
576a8e1175bSopenharmony_ci
577a8e1175bSopenharmony_ci#ifdef __cplusplus
578a8e1175bSopenharmony_ciextern "C" {
579a8e1175bSopenharmony_ci#endif
580a8e1175bSopenharmony_ci
581a8e1175bSopenharmony_citypedef int  mbedtls_ssl_tls_prf_cb(const unsigned char *secret, size_t slen,
582a8e1175bSopenharmony_ci                                    const char *label,
583a8e1175bSopenharmony_ci                                    const unsigned char *random, size_t rlen,
584a8e1175bSopenharmony_ci                                    unsigned char *dstbuf, size_t dlen);
585a8e1175bSopenharmony_ci
586a8e1175bSopenharmony_ci/* cipher.h exports the maximum IV, key and block length from
587a8e1175bSopenharmony_ci * all ciphers enabled in the config, regardless of whether those
588a8e1175bSopenharmony_ci * ciphers are actually usable in SSL/TLS. Notably, XTS is enabled
589a8e1175bSopenharmony_ci * in the default configuration and uses 64 Byte keys, but it is
590a8e1175bSopenharmony_ci * not used for record protection in SSL/TLS.
591a8e1175bSopenharmony_ci *
592a8e1175bSopenharmony_ci * In order to prevent unnecessary inflation of key structures,
593a8e1175bSopenharmony_ci * we introduce SSL-specific variants of the max-{key,block,IV}
594a8e1175bSopenharmony_ci * macros here which are meant to only take those ciphers into
595a8e1175bSopenharmony_ci * account which can be negotiated in SSL/TLS.
596a8e1175bSopenharmony_ci *
597a8e1175bSopenharmony_ci * Since the current definitions of MBEDTLS_MAX_{KEY|BLOCK|IV}_LENGTH
598a8e1175bSopenharmony_ci * in cipher.h are rough overapproximations of the real maxima, here
599a8e1175bSopenharmony_ci * we content ourselves with replicating those overapproximations
600a8e1175bSopenharmony_ci * for the maximum block and IV length, and excluding XTS from the
601a8e1175bSopenharmony_ci * computation of the maximum key length. */
602a8e1175bSopenharmony_ci#define MBEDTLS_SSL_MAX_BLOCK_LENGTH 16
603a8e1175bSopenharmony_ci#define MBEDTLS_SSL_MAX_IV_LENGTH    16
604a8e1175bSopenharmony_ci#define MBEDTLS_SSL_MAX_KEY_LENGTH   32
605a8e1175bSopenharmony_ci
606a8e1175bSopenharmony_ci/**
607a8e1175bSopenharmony_ci * \brief   The data structure holding the cryptographic material (key and IV)
608a8e1175bSopenharmony_ci *          used for record protection in TLS 1.3.
609a8e1175bSopenharmony_ci */
610a8e1175bSopenharmony_cistruct mbedtls_ssl_key_set {
611a8e1175bSopenharmony_ci    /*! The key for client->server records. */
612a8e1175bSopenharmony_ci    unsigned char client_write_key[MBEDTLS_SSL_MAX_KEY_LENGTH];
613a8e1175bSopenharmony_ci    /*! The key for server->client records. */
614a8e1175bSopenharmony_ci    unsigned char server_write_key[MBEDTLS_SSL_MAX_KEY_LENGTH];
615a8e1175bSopenharmony_ci    /*! The IV  for client->server records. */
616a8e1175bSopenharmony_ci    unsigned char client_write_iv[MBEDTLS_SSL_MAX_IV_LENGTH];
617a8e1175bSopenharmony_ci    /*! The IV  for server->client records. */
618a8e1175bSopenharmony_ci    unsigned char server_write_iv[MBEDTLS_SSL_MAX_IV_LENGTH];
619a8e1175bSopenharmony_ci
620a8e1175bSopenharmony_ci    size_t key_len; /*!< The length of client_write_key and
621a8e1175bSopenharmony_ci                     *   server_write_key, in Bytes. */
622a8e1175bSopenharmony_ci    size_t iv_len;  /*!< The length of client_write_iv and
623a8e1175bSopenharmony_ci                     *   server_write_iv, in Bytes. */
624a8e1175bSopenharmony_ci};
625a8e1175bSopenharmony_citypedef struct mbedtls_ssl_key_set mbedtls_ssl_key_set;
626a8e1175bSopenharmony_ci
627a8e1175bSopenharmony_citypedef struct {
628a8e1175bSopenharmony_ci    unsigned char binder_key[MBEDTLS_TLS1_3_MD_MAX_SIZE];
629a8e1175bSopenharmony_ci    unsigned char client_early_traffic_secret[MBEDTLS_TLS1_3_MD_MAX_SIZE];
630a8e1175bSopenharmony_ci    unsigned char early_exporter_master_secret[MBEDTLS_TLS1_3_MD_MAX_SIZE];
631a8e1175bSopenharmony_ci} mbedtls_ssl_tls13_early_secrets;
632a8e1175bSopenharmony_ci
633a8e1175bSopenharmony_citypedef struct {
634a8e1175bSopenharmony_ci    unsigned char client_handshake_traffic_secret[MBEDTLS_TLS1_3_MD_MAX_SIZE];
635a8e1175bSopenharmony_ci    unsigned char server_handshake_traffic_secret[MBEDTLS_TLS1_3_MD_MAX_SIZE];
636a8e1175bSopenharmony_ci} mbedtls_ssl_tls13_handshake_secrets;
637a8e1175bSopenharmony_ci
638a8e1175bSopenharmony_ci/*
639a8e1175bSopenharmony_ci * This structure contains the parameters only needed during handshake.
640a8e1175bSopenharmony_ci */
641a8e1175bSopenharmony_cistruct mbedtls_ssl_handshake_params {
642a8e1175bSopenharmony_ci    /* Frequently-used boolean or byte fields (placed early to take
643a8e1175bSopenharmony_ci     * advantage of smaller code size for indirect access on Arm Thumb) */
644a8e1175bSopenharmony_ci    uint8_t resume;                     /*!<  session resume indicator*/
645a8e1175bSopenharmony_ci    uint8_t cli_exts;                   /*!< client extension presence*/
646a8e1175bSopenharmony_ci
647a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
648a8e1175bSopenharmony_ci    uint8_t sni_authmode;               /*!< authmode from SNI callback     */
649a8e1175bSopenharmony_ci#endif
650a8e1175bSopenharmony_ci
651a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_SRV_C)
652a8e1175bSopenharmony_ci    /* Flag indicating if a CertificateRequest message has been sent
653a8e1175bSopenharmony_ci     * to the client or not. */
654a8e1175bSopenharmony_ci    uint8_t certificate_request_sent;
655a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_EARLY_DATA)
656a8e1175bSopenharmony_ci    /* Flag indicating if the server has accepted early data or not. */
657a8e1175bSopenharmony_ci    uint8_t early_data_accepted;
658a8e1175bSopenharmony_ci#endif
659a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_SRV_C */
660a8e1175bSopenharmony_ci
661a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_SESSION_TICKETS)
662a8e1175bSopenharmony_ci    uint8_t new_session_ticket;         /*!< use NewSessionTicket?    */
663a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_SESSION_TICKETS */
664a8e1175bSopenharmony_ci
665a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_CLI_C)
666a8e1175bSopenharmony_ci    /** Minimum TLS version to be negotiated.
667a8e1175bSopenharmony_ci     *
668a8e1175bSopenharmony_ci     * It is set up in the ClientHello writing preparation stage and used
669a8e1175bSopenharmony_ci     * throughout the ClientHello writing. Not relevant anymore as soon as
670a8e1175bSopenharmony_ci     * the protocol version has been negotiated thus as soon as the
671a8e1175bSopenharmony_ci     * ServerHello is received.
672a8e1175bSopenharmony_ci     * For a fresh handshake not linked to any previous handshake, it is
673a8e1175bSopenharmony_ci     * equal to the configured minimum minor version to be negotiated. When
674a8e1175bSopenharmony_ci     * renegotiating or resuming a session, it is equal to the previously
675a8e1175bSopenharmony_ci     * negotiated minor version.
676a8e1175bSopenharmony_ci     *
677a8e1175bSopenharmony_ci     * There is no maximum TLS version field in this handshake context.
678a8e1175bSopenharmony_ci     * From the start of the handshake, we need to define a current protocol
679a8e1175bSopenharmony_ci     * version for the record layer which we define as the maximum TLS
680a8e1175bSopenharmony_ci     * version to be negotiated. The `tls_version` field of the SSL context is
681a8e1175bSopenharmony_ci     * used to store this maximum value until it contains the actual
682a8e1175bSopenharmony_ci     * negotiated value.
683a8e1175bSopenharmony_ci     */
684a8e1175bSopenharmony_ci    mbedtls_ssl_protocol_version min_tls_version;
685a8e1175bSopenharmony_ci#endif
686a8e1175bSopenharmony_ci
687a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
688a8e1175bSopenharmony_ci    uint8_t extended_ms;                /*!< use Extended Master Secret? */
689a8e1175bSopenharmony_ci#endif
690a8e1175bSopenharmony_ci
691a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
692a8e1175bSopenharmony_ci    uint8_t async_in_progress; /*!< an asynchronous operation is in progress */
693a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
694a8e1175bSopenharmony_ci
695a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_PROTO_DTLS)
696a8e1175bSopenharmony_ci    unsigned char retransmit_state;     /*!<  Retransmission state           */
697a8e1175bSopenharmony_ci#endif
698a8e1175bSopenharmony_ci
699a8e1175bSopenharmony_ci#if !defined(MBEDTLS_DEPRECATED_REMOVED)
700a8e1175bSopenharmony_ci    unsigned char group_list_heap_allocated;
701a8e1175bSopenharmony_ci    unsigned char sig_algs_heap_allocated;
702a8e1175bSopenharmony_ci#endif
703a8e1175bSopenharmony_ci
704a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
705a8e1175bSopenharmony_ci    uint8_t ecrs_enabled;               /*!< Handshake supports EC restart? */
706a8e1175bSopenharmony_ci    enum { /* this complements ssl->state with info on intra-state operations */
707a8e1175bSopenharmony_ci        ssl_ecrs_none = 0,              /*!< nothing going on (yet)         */
708a8e1175bSopenharmony_ci        ssl_ecrs_crt_verify,            /*!< Certificate: crt_verify()      */
709a8e1175bSopenharmony_ci        ssl_ecrs_ske_start_processing,  /*!< ServerKeyExchange: pk_verify() */
710a8e1175bSopenharmony_ci        ssl_ecrs_cke_ecdh_calc_secret,  /*!< ClientKeyExchange: ECDH step 2 */
711a8e1175bSopenharmony_ci        ssl_ecrs_crt_vrfy_sign,         /*!< CertificateVerify: pk_sign()   */
712a8e1175bSopenharmony_ci    } ecrs_state;                       /*!< current (or last) operation    */
713a8e1175bSopenharmony_ci    mbedtls_x509_crt *ecrs_peer_cert;   /*!< The peer's CRT chain.          */
714a8e1175bSopenharmony_ci    size_t ecrs_n;                      /*!< place for saving a length      */
715a8e1175bSopenharmony_ci#endif
716a8e1175bSopenharmony_ci
717a8e1175bSopenharmony_ci    mbedtls_ssl_ciphersuite_t const *ciphersuite_info;
718a8e1175bSopenharmony_ci
719a8e1175bSopenharmony_ci    MBEDTLS_CHECK_RETURN_CRITICAL
720a8e1175bSopenharmony_ci    int (*update_checksum)(mbedtls_ssl_context *, const unsigned char *, size_t);
721a8e1175bSopenharmony_ci    MBEDTLS_CHECK_RETURN_CRITICAL
722a8e1175bSopenharmony_ci    int (*calc_verify)(const mbedtls_ssl_context *, unsigned char *, size_t *);
723a8e1175bSopenharmony_ci    MBEDTLS_CHECK_RETURN_CRITICAL
724a8e1175bSopenharmony_ci    int (*calc_finished)(mbedtls_ssl_context *, unsigned char *, int);
725a8e1175bSopenharmony_ci    mbedtls_ssl_tls_prf_cb *tls_prf;
726a8e1175bSopenharmony_ci
727a8e1175bSopenharmony_ci    /*
728a8e1175bSopenharmony_ci     * Handshake specific crypto variables
729a8e1175bSopenharmony_ci     */
730a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
731a8e1175bSopenharmony_ci    uint8_t key_exchange_mode; /*!< Selected key exchange mode */
732a8e1175bSopenharmony_ci
733a8e1175bSopenharmony_ci    /**
734a8e1175bSopenharmony_ci     * Flag indicating if, in the course of the current handshake, an
735a8e1175bSopenharmony_ci     * HelloRetryRequest message has been sent by the server or received by
736a8e1175bSopenharmony_ci     * the client (<> 0) or not (0).
737a8e1175bSopenharmony_ci     */
738a8e1175bSopenharmony_ci    uint8_t hello_retry_request_flag;
739a8e1175bSopenharmony_ci
740a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
741a8e1175bSopenharmony_ci    /**
742a8e1175bSopenharmony_ci     * Flag indicating if, in the course of the current handshake, a dummy
743a8e1175bSopenharmony_ci     * change_cipher_spec (CCS) record has already been sent. Used to send only
744a8e1175bSopenharmony_ci     * one CCS per handshake while not complicating the handshake state
745a8e1175bSopenharmony_ci     * transitions for that purpose.
746a8e1175bSopenharmony_ci     */
747a8e1175bSopenharmony_ci    uint8_t ccs_sent;
748a8e1175bSopenharmony_ci#endif
749a8e1175bSopenharmony_ci
750a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_SRV_C)
751a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
752a8e1175bSopenharmony_ci    uint8_t tls13_kex_modes; /*!< Key exchange modes supported by the client */
753a8e1175bSopenharmony_ci#endif
754a8e1175bSopenharmony_ci    /** selected_group of key_share extension in HelloRetryRequest message. */
755a8e1175bSopenharmony_ci    uint16_t hrr_selected_group;
756a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_SESSION_TICKETS)
757a8e1175bSopenharmony_ci    uint16_t new_session_tickets_count;         /*!< number of session tickets */
758a8e1175bSopenharmony_ci#endif
759a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_SRV_C */
760a8e1175bSopenharmony_ci
761a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
762a8e1175bSopenharmony_ci
763a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
764a8e1175bSopenharmony_ci    uint16_t received_sig_algs[MBEDTLS_RECEIVED_SIG_ALGS_SIZE];
765a8e1175bSopenharmony_ci#endif
766a8e1175bSopenharmony_ci
767a8e1175bSopenharmony_ci#if !defined(MBEDTLS_DEPRECATED_REMOVED)
768a8e1175bSopenharmony_ci    const uint16_t *group_list;
769a8e1175bSopenharmony_ci    const uint16_t *sig_algs;
770a8e1175bSopenharmony_ci#endif
771a8e1175bSopenharmony_ci
772a8e1175bSopenharmony_ci#if defined(MBEDTLS_DHM_C)
773a8e1175bSopenharmony_ci    mbedtls_dhm_context dhm_ctx;                /*!<  DHM key exchange        */
774a8e1175bSopenharmony_ci#endif
775a8e1175bSopenharmony_ci
776a8e1175bSopenharmony_ci#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
777a8e1175bSopenharmony_ci    defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED)
778a8e1175bSopenharmony_ci    mbedtls_ecdh_context ecdh_ctx;              /*!<  ECDH key exchange       */
779a8e1175bSopenharmony_ci#endif /* !MBEDTLS_USE_PSA_CRYPTO &&
780a8e1175bSopenharmony_ci          MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED */
781a8e1175bSopenharmony_ci
782a8e1175bSopenharmony_ci#if defined(MBEDTLS_KEY_EXCHANGE_SOME_XXDH_PSA_ANY_ENABLED)
783a8e1175bSopenharmony_ci    psa_key_type_t xxdh_psa_type;
784a8e1175bSopenharmony_ci    size_t xxdh_psa_bits;
785a8e1175bSopenharmony_ci    mbedtls_svc_key_id_t xxdh_psa_privkey;
786a8e1175bSopenharmony_ci    uint8_t xxdh_psa_privkey_is_external;
787a8e1175bSopenharmony_ci    unsigned char xxdh_psa_peerkey[PSA_EXPORT_PUBLIC_KEY_MAX_SIZE];
788a8e1175bSopenharmony_ci    size_t xxdh_psa_peerkey_len;
789a8e1175bSopenharmony_ci#endif /* MBEDTLS_KEY_EXCHANGE_SOME_XXDH_PSA_ANY_ENABLED */
790a8e1175bSopenharmony_ci
791a8e1175bSopenharmony_ci#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
792a8e1175bSopenharmony_ci#if defined(MBEDTLS_USE_PSA_CRYPTO)
793a8e1175bSopenharmony_ci    psa_pake_operation_t psa_pake_ctx;        /*!< EC J-PAKE key exchange */
794a8e1175bSopenharmony_ci    mbedtls_svc_key_id_t psa_pake_password;
795a8e1175bSopenharmony_ci    uint8_t psa_pake_ctx_is_ok;
796a8e1175bSopenharmony_ci#else
797a8e1175bSopenharmony_ci    mbedtls_ecjpake_context ecjpake_ctx;        /*!< EC J-PAKE key exchange */
798a8e1175bSopenharmony_ci#endif /* MBEDTLS_USE_PSA_CRYPTO */
799a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_CLI_C)
800a8e1175bSopenharmony_ci    unsigned char *ecjpake_cache;               /*!< Cache for ClientHello ext */
801a8e1175bSopenharmony_ci    size_t ecjpake_cache_len;                   /*!< Length of cached data */
802a8e1175bSopenharmony_ci#endif
803a8e1175bSopenharmony_ci#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
804a8e1175bSopenharmony_ci
805a8e1175bSopenharmony_ci#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_ANY_ENABLED) || \
806a8e1175bSopenharmony_ci    defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) || \
807a8e1175bSopenharmony_ci    defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
808a8e1175bSopenharmony_ci    uint16_t *curves_tls_id;      /*!<  List of TLS IDs of supported elliptic curves */
809a8e1175bSopenharmony_ci#endif
810a8e1175bSopenharmony_ci
811a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
812a8e1175bSopenharmony_ci#if defined(MBEDTLS_USE_PSA_CRYPTO)
813a8e1175bSopenharmony_ci    mbedtls_svc_key_id_t psk_opaque;            /*!< Opaque PSK from the callback   */
814a8e1175bSopenharmony_ci    uint8_t psk_opaque_is_internal;
815a8e1175bSopenharmony_ci#else
816a8e1175bSopenharmony_ci    unsigned char *psk;                 /*!<  PSK from the callback         */
817a8e1175bSopenharmony_ci    size_t psk_len;                     /*!<  Length of PSK from callback   */
818a8e1175bSopenharmony_ci#endif /* MBEDTLS_USE_PSA_CRYPTO */
819a8e1175bSopenharmony_ci    uint16_t    selected_identity;
820a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
821a8e1175bSopenharmony_ci
822a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
823a8e1175bSopenharmony_ci    mbedtls_x509_crt_restart_ctx ecrs_ctx;  /*!< restart context            */
824a8e1175bSopenharmony_ci#endif
825a8e1175bSopenharmony_ci
826a8e1175bSopenharmony_ci#if defined(MBEDTLS_X509_CRT_PARSE_C)
827a8e1175bSopenharmony_ci    mbedtls_ssl_key_cert *key_cert;     /*!< chosen key/cert pair (server)  */
828a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
829a8e1175bSopenharmony_ci    mbedtls_ssl_key_cert *sni_key_cert; /*!< key/cert list from SNI         */
830a8e1175bSopenharmony_ci    mbedtls_x509_crt *sni_ca_chain;     /*!< trusted CAs from SNI callback  */
831a8e1175bSopenharmony_ci    mbedtls_x509_crl *sni_ca_crl;       /*!< trusted CAs CRLs from SNI      */
832a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
833a8e1175bSopenharmony_ci#endif /* MBEDTLS_X509_CRT_PARSE_C */
834a8e1175bSopenharmony_ci
835a8e1175bSopenharmony_ci#if defined(MBEDTLS_X509_CRT_PARSE_C) &&        \
836a8e1175bSopenharmony_ci    !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
837a8e1175bSopenharmony_ci    mbedtls_pk_context peer_pubkey;     /*!< The public key from the peer.  */
838a8e1175bSopenharmony_ci#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
839a8e1175bSopenharmony_ci
840a8e1175bSopenharmony_ci    struct {
841a8e1175bSopenharmony_ci        size_t total_bytes_buffered; /*!< Cumulative size of heap allocated
842a8e1175bSopenharmony_ci                                      *   buffers used for message buffering. */
843a8e1175bSopenharmony_ci
844a8e1175bSopenharmony_ci        uint8_t seen_ccs;               /*!< Indicates if a CCS message has
845a8e1175bSopenharmony_ci                                         *   been seen in the current flight. */
846a8e1175bSopenharmony_ci
847a8e1175bSopenharmony_ci        struct mbedtls_ssl_hs_buffer {
848a8e1175bSopenharmony_ci            unsigned is_valid      : 1;
849a8e1175bSopenharmony_ci            unsigned is_fragmented : 1;
850a8e1175bSopenharmony_ci            unsigned is_complete   : 1;
851a8e1175bSopenharmony_ci            unsigned char *data;
852a8e1175bSopenharmony_ci            size_t data_len;
853a8e1175bSopenharmony_ci        } hs[MBEDTLS_SSL_MAX_BUFFERED_HS];
854a8e1175bSopenharmony_ci
855a8e1175bSopenharmony_ci        struct {
856a8e1175bSopenharmony_ci            unsigned char *data;
857a8e1175bSopenharmony_ci            size_t len;
858a8e1175bSopenharmony_ci            unsigned epoch;
859a8e1175bSopenharmony_ci        } future_record;
860a8e1175bSopenharmony_ci
861a8e1175bSopenharmony_ci    } buffering;
862a8e1175bSopenharmony_ci
863a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_CLI_C) && \
864a8e1175bSopenharmony_ci    (defined(MBEDTLS_SSL_PROTO_DTLS) || \
865a8e1175bSopenharmony_ci    defined(MBEDTLS_SSL_PROTO_TLS1_3))
866a8e1175bSopenharmony_ci    unsigned char *cookie;              /*!< HelloVerifyRequest cookie for DTLS
867a8e1175bSopenharmony_ci                                         *   HelloRetryRequest cookie for TLS 1.3 */
868a8e1175bSopenharmony_ci#if !defined(MBEDTLS_SSL_PROTO_TLS1_3)
869a8e1175bSopenharmony_ci    /* RFC 6347 page 15
870a8e1175bSopenharmony_ci       ...
871a8e1175bSopenharmony_ci       opaque cookie<0..2^8-1>;
872a8e1175bSopenharmony_ci       ...
873a8e1175bSopenharmony_ci     */
874a8e1175bSopenharmony_ci    uint8_t cookie_len;
875a8e1175bSopenharmony_ci#else
876a8e1175bSopenharmony_ci    /* RFC 8446 page 39
877a8e1175bSopenharmony_ci       ...
878a8e1175bSopenharmony_ci       opaque cookie<0..2^16-1>;
879a8e1175bSopenharmony_ci       ...
880a8e1175bSopenharmony_ci       If TLS1_3 is enabled, the max length is 2^16 - 1
881a8e1175bSopenharmony_ci     */
882a8e1175bSopenharmony_ci    uint16_t cookie_len;                /*!< DTLS: HelloVerifyRequest cookie length
883a8e1175bSopenharmony_ci                                         *   TLS1_3: HelloRetryRequest cookie length */
884a8e1175bSopenharmony_ci#endif
885a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_CLI_C &&
886a8e1175bSopenharmony_ci          ( MBEDTLS_SSL_PROTO_DTLS ||
887a8e1175bSopenharmony_ci            MBEDTLS_SSL_PROTO_TLS1_3 ) */
888a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_PROTO_DTLS)
889a8e1175bSopenharmony_ci    unsigned char cookie_verify_result; /*!< Srv: flag for sending a cookie */
890a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_DTLS */
891a8e1175bSopenharmony_ci
892a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_PROTO_DTLS)
893a8e1175bSopenharmony_ci    unsigned int out_msg_seq;           /*!<  Outgoing handshake sequence number */
894a8e1175bSopenharmony_ci    unsigned int in_msg_seq;            /*!<  Incoming handshake sequence number */
895a8e1175bSopenharmony_ci
896a8e1175bSopenharmony_ci    uint32_t retransmit_timeout;        /*!<  Current value of timeout       */
897a8e1175bSopenharmony_ci    mbedtls_ssl_flight_item *flight;    /*!<  Current outgoing flight        */
898a8e1175bSopenharmony_ci    mbedtls_ssl_flight_item *cur_msg;   /*!<  Current message in flight      */
899a8e1175bSopenharmony_ci    unsigned char *cur_msg_p;           /*!<  Position in current message    */
900a8e1175bSopenharmony_ci    unsigned int in_flight_start_seq;   /*!<  Minimum message sequence in the
901a8e1175bSopenharmony_ci                                              flight being received          */
902a8e1175bSopenharmony_ci    mbedtls_ssl_transform *alt_transform_out;   /*!<  Alternative transform for
903a8e1175bSopenharmony_ci                                                   resending messages             */
904a8e1175bSopenharmony_ci    unsigned char alt_out_ctr[MBEDTLS_SSL_SEQUENCE_NUMBER_LEN]; /*!<  Alternative record epoch/counter
905a8e1175bSopenharmony_ci                                                                      for resending messages         */
906a8e1175bSopenharmony_ci
907a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
908a8e1175bSopenharmony_ci    /* The state of CID configuration in this handshake. */
909a8e1175bSopenharmony_ci
910a8e1175bSopenharmony_ci    uint8_t cid_in_use; /*!< This indicates whether the use of the CID extension
911a8e1175bSopenharmony_ci                         *   has been negotiated. Possible values are
912a8e1175bSopenharmony_ci                         *   #MBEDTLS_SSL_CID_ENABLED and
913a8e1175bSopenharmony_ci                         *   #MBEDTLS_SSL_CID_DISABLED. */
914a8e1175bSopenharmony_ci    unsigned char peer_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX];   /*! The peer's CID */
915a8e1175bSopenharmony_ci    uint8_t peer_cid_len;                                  /*!< The length of
916a8e1175bSopenharmony_ci                                                            *   \c peer_cid.  */
917a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
918a8e1175bSopenharmony_ci
919a8e1175bSopenharmony_ci    uint16_t mtu;                       /*!<  Handshake mtu, used to fragment outgoing messages */
920a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_PROTO_DTLS */
921a8e1175bSopenharmony_ci
922a8e1175bSopenharmony_ci    /*
923a8e1175bSopenharmony_ci     * Checksum contexts
924a8e1175bSopenharmony_ci     */
925a8e1175bSopenharmony_ci#if defined(MBEDTLS_MD_CAN_SHA256)
926a8e1175bSopenharmony_ci#if defined(MBEDTLS_USE_PSA_CRYPTO)
927a8e1175bSopenharmony_ci    psa_hash_operation_t fin_sha256_psa;
928a8e1175bSopenharmony_ci#else
929a8e1175bSopenharmony_ci    mbedtls_md_context_t fin_sha256;
930a8e1175bSopenharmony_ci#endif
931a8e1175bSopenharmony_ci#endif
932a8e1175bSopenharmony_ci#if defined(MBEDTLS_MD_CAN_SHA384)
933a8e1175bSopenharmony_ci#if defined(MBEDTLS_USE_PSA_CRYPTO)
934a8e1175bSopenharmony_ci    psa_hash_operation_t fin_sha384_psa;
935a8e1175bSopenharmony_ci#else
936a8e1175bSopenharmony_ci    mbedtls_md_context_t fin_sha384;
937a8e1175bSopenharmony_ci#endif
938a8e1175bSopenharmony_ci#endif
939a8e1175bSopenharmony_ci
940a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
941a8e1175bSopenharmony_ci    uint16_t offered_group_id; /* The NamedGroup value for the group
942a8e1175bSopenharmony_ci                                * that is being used for ephemeral
943a8e1175bSopenharmony_ci                                * key exchange.
944a8e1175bSopenharmony_ci                                *
945a8e1175bSopenharmony_ci                                * On the client: Defaults to the first
946a8e1175bSopenharmony_ci                                * entry in the client's group list,
947a8e1175bSopenharmony_ci                                * but can be overwritten by the HRR. */
948a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
949a8e1175bSopenharmony_ci
950a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_CLI_C)
951a8e1175bSopenharmony_ci    uint8_t client_auth;       /*!< used to check if CertificateRequest has been
952a8e1175bSopenharmony_ci                                    received from server side. If CertificateRequest
953a8e1175bSopenharmony_ci                                    has been received, Certificate and CertificateVerify
954a8e1175bSopenharmony_ci                                    should be sent to server */
955a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_CLI_C */
956a8e1175bSopenharmony_ci    /*
957a8e1175bSopenharmony_ci     * State-local variables used during the processing
958a8e1175bSopenharmony_ci     * of a specific handshake state.
959a8e1175bSopenharmony_ci     */
960a8e1175bSopenharmony_ci    union {
961a8e1175bSopenharmony_ci        /* Outgoing Finished message */
962a8e1175bSopenharmony_ci        struct {
963a8e1175bSopenharmony_ci            uint8_t preparation_done;
964a8e1175bSopenharmony_ci
965a8e1175bSopenharmony_ci            /* Buffer holding digest of the handshake up to
966a8e1175bSopenharmony_ci             * but excluding the outgoing finished message. */
967a8e1175bSopenharmony_ci            unsigned char digest[MBEDTLS_TLS1_3_MD_MAX_SIZE];
968a8e1175bSopenharmony_ci            size_t digest_len;
969a8e1175bSopenharmony_ci        } finished_out;
970a8e1175bSopenharmony_ci
971a8e1175bSopenharmony_ci        /* Incoming Finished message */
972a8e1175bSopenharmony_ci        struct {
973a8e1175bSopenharmony_ci            uint8_t preparation_done;
974a8e1175bSopenharmony_ci
975a8e1175bSopenharmony_ci            /* Buffer holding digest of the handshake up to but
976a8e1175bSopenharmony_ci             * excluding the peer's incoming finished message. */
977a8e1175bSopenharmony_ci            unsigned char digest[MBEDTLS_TLS1_3_MD_MAX_SIZE];
978a8e1175bSopenharmony_ci            size_t digest_len;
979a8e1175bSopenharmony_ci        } finished_in;
980a8e1175bSopenharmony_ci
981a8e1175bSopenharmony_ci    } state_local;
982a8e1175bSopenharmony_ci
983a8e1175bSopenharmony_ci    /* End of state-local variables. */
984a8e1175bSopenharmony_ci
985a8e1175bSopenharmony_ci    unsigned char randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN +
986a8e1175bSopenharmony_ci                            MBEDTLS_SERVER_HELLO_RANDOM_LEN];
987a8e1175bSopenharmony_ci    /*!<  random bytes            */
988a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
989a8e1175bSopenharmony_ci    unsigned char premaster[MBEDTLS_PREMASTER_SIZE];
990a8e1175bSopenharmony_ci    /*!<  premaster secret        */
991a8e1175bSopenharmony_ci    size_t pmslen;                      /*!<  premaster length        */
992a8e1175bSopenharmony_ci#endif
993a8e1175bSopenharmony_ci
994a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
995a8e1175bSopenharmony_ci    uint32_t sent_extensions;       /*!< extensions sent by endpoint */
996a8e1175bSopenharmony_ci    uint32_t received_extensions;   /*!< extensions received by endpoint */
997a8e1175bSopenharmony_ci
998a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
999a8e1175bSopenharmony_ci    unsigned char certificate_request_context_len;
1000a8e1175bSopenharmony_ci    unsigned char *certificate_request_context;
1001a8e1175bSopenharmony_ci#endif
1002a8e1175bSopenharmony_ci
1003a8e1175bSopenharmony_ci    /** TLS 1.3 transform for encrypted handshake messages. */
1004a8e1175bSopenharmony_ci    mbedtls_ssl_transform *transform_handshake;
1005a8e1175bSopenharmony_ci    union {
1006a8e1175bSopenharmony_ci        unsigned char early[MBEDTLS_TLS1_3_MD_MAX_SIZE];
1007a8e1175bSopenharmony_ci        unsigned char handshake[MBEDTLS_TLS1_3_MD_MAX_SIZE];
1008a8e1175bSopenharmony_ci        unsigned char app[MBEDTLS_TLS1_3_MD_MAX_SIZE];
1009a8e1175bSopenharmony_ci    } tls13_master_secrets;
1010a8e1175bSopenharmony_ci
1011a8e1175bSopenharmony_ci    mbedtls_ssl_tls13_handshake_secrets tls13_hs_secrets;
1012a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_EARLY_DATA)
1013a8e1175bSopenharmony_ci    /** TLS 1.3 transform for early data and handshake messages. */
1014a8e1175bSopenharmony_ci    mbedtls_ssl_transform *transform_earlydata;
1015a8e1175bSopenharmony_ci#endif
1016a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1017a8e1175bSopenharmony_ci
1018a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
1019a8e1175bSopenharmony_ci    /** Asynchronous operation context. This field is meant for use by the
1020a8e1175bSopenharmony_ci     * asynchronous operation callbacks (mbedtls_ssl_config::f_async_sign_start,
1021a8e1175bSopenharmony_ci     * mbedtls_ssl_config::f_async_decrypt_start,
1022a8e1175bSopenharmony_ci     * mbedtls_ssl_config::f_async_resume, mbedtls_ssl_config::f_async_cancel).
1023a8e1175bSopenharmony_ci     * The library does not use it internally. */
1024a8e1175bSopenharmony_ci    void *user_async_ctx;
1025a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
1026a8e1175bSopenharmony_ci
1027a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1028a8e1175bSopenharmony_ci    const unsigned char *sni_name;      /*!< raw SNI                        */
1029a8e1175bSopenharmony_ci    size_t sni_name_len;                /*!< raw SNI len                    */
1030a8e1175bSopenharmony_ci#if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
1031a8e1175bSopenharmony_ci    const mbedtls_x509_crt *dn_hints;   /*!< acceptable client cert issuers */
1032a8e1175bSopenharmony_ci#endif
1033a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1034a8e1175bSopenharmony_ci};
1035a8e1175bSopenharmony_ci
1036a8e1175bSopenharmony_citypedef struct mbedtls_ssl_hs_buffer mbedtls_ssl_hs_buffer;
1037a8e1175bSopenharmony_ci
1038a8e1175bSopenharmony_ci/*
1039a8e1175bSopenharmony_ci * Representation of decryption/encryption transformations on records
1040a8e1175bSopenharmony_ci *
1041a8e1175bSopenharmony_ci * There are the following general types of record transformations:
1042a8e1175bSopenharmony_ci * - Stream transformations (TLS versions == 1.2 only)
1043a8e1175bSopenharmony_ci *   Transformation adding a MAC and applying a stream-cipher
1044a8e1175bSopenharmony_ci *   to the authenticated message.
1045a8e1175bSopenharmony_ci * - CBC block cipher transformations ([D]TLS versions == 1.2 only)
1046a8e1175bSopenharmony_ci *   For TLS 1.2, no IV is generated at key extraction time, but every
1047a8e1175bSopenharmony_ci *   encrypted record is explicitly prefixed by the IV with which it was
1048a8e1175bSopenharmony_ci *   encrypted.
1049a8e1175bSopenharmony_ci * - AEAD transformations ([D]TLS versions == 1.2 only)
1050a8e1175bSopenharmony_ci *   These come in two fundamentally different versions, the first one
1051a8e1175bSopenharmony_ci *   used in TLS 1.2, excluding ChaChaPoly ciphersuites, and the second
1052a8e1175bSopenharmony_ci *   one used for ChaChaPoly ciphersuites in TLS 1.2 as well as for TLS 1.3.
1053a8e1175bSopenharmony_ci *   In the first transformation, the IV to be used for a record is obtained
1054a8e1175bSopenharmony_ci *   as the concatenation of an explicit, static 4-byte IV and the 8-byte
1055a8e1175bSopenharmony_ci *   record sequence number, and explicitly prepending this sequence number
1056a8e1175bSopenharmony_ci *   to the encrypted record. In contrast, in the second transformation
1057a8e1175bSopenharmony_ci *   the IV is obtained by XOR'ing a static IV obtained at key extraction
1058a8e1175bSopenharmony_ci *   time with the 8-byte record sequence number, without prepending the
1059a8e1175bSopenharmony_ci *   latter to the encrypted record.
1060a8e1175bSopenharmony_ci *
1061a8e1175bSopenharmony_ci * Additionally, DTLS 1.2 + CID as well as TLS 1.3 use an inner plaintext
1062a8e1175bSopenharmony_ci * which allows to add flexible length padding and to hide a record's true
1063a8e1175bSopenharmony_ci * content type.
1064a8e1175bSopenharmony_ci *
1065a8e1175bSopenharmony_ci * In addition to type and version, the following parameters are relevant:
1066a8e1175bSopenharmony_ci * - The symmetric cipher algorithm to be used.
1067a8e1175bSopenharmony_ci * - The (static) encryption/decryption keys for the cipher.
1068a8e1175bSopenharmony_ci * - For stream/CBC, the type of message digest to be used.
1069a8e1175bSopenharmony_ci * - For stream/CBC, (static) encryption/decryption keys for the digest.
1070a8e1175bSopenharmony_ci * - For AEAD transformations, the size (potentially 0) of an explicit,
1071a8e1175bSopenharmony_ci *   random initialization vector placed in encrypted records.
1072a8e1175bSopenharmony_ci * - For some transformations (currently AEAD) an implicit IV. It is static
1073a8e1175bSopenharmony_ci *   and (if present) is combined with the explicit IV in a transformation-
1074a8e1175bSopenharmony_ci *   -dependent way (e.g. appending in TLS 1.2 and XOR'ing in TLS 1.3).
1075a8e1175bSopenharmony_ci * - For stream/CBC, a flag determining the order of encryption and MAC.
1076a8e1175bSopenharmony_ci * - The details of the transformation depend on the SSL/TLS version.
1077a8e1175bSopenharmony_ci * - The length of the authentication tag.
1078a8e1175bSopenharmony_ci *
1079a8e1175bSopenharmony_ci * The struct below refines this abstract view as follows:
1080a8e1175bSopenharmony_ci * - The cipher underlying the transformation is managed in
1081a8e1175bSopenharmony_ci *   cipher contexts cipher_ctx_{enc/dec}, which must have the
1082a8e1175bSopenharmony_ci *   same cipher type. The mode of these cipher contexts determines
1083a8e1175bSopenharmony_ci *   the type of the transformation in the sense above: e.g., if
1084a8e1175bSopenharmony_ci *   the type is MBEDTLS_CIPHER_AES_256_CBC resp. MBEDTLS_CIPHER_AES_192_GCM
1085a8e1175bSopenharmony_ci *   then the transformation has type CBC resp. AEAD.
1086a8e1175bSopenharmony_ci * - The cipher keys are never stored explicitly but
1087a8e1175bSopenharmony_ci *   are maintained within cipher_ctx_{enc/dec}.
1088a8e1175bSopenharmony_ci * - For stream/CBC transformations, the message digest contexts
1089a8e1175bSopenharmony_ci *   used for the MAC's are stored in md_ctx_{enc/dec}. These contexts
1090a8e1175bSopenharmony_ci *   are unused for AEAD transformations.
1091a8e1175bSopenharmony_ci * - For stream/CBC transformations, the MAC keys are not stored explicitly
1092a8e1175bSopenharmony_ci *   but maintained within md_ctx_{enc/dec}.
1093a8e1175bSopenharmony_ci * - The mac_enc and mac_dec fields are unused for EAD transformations.
1094a8e1175bSopenharmony_ci * - For transformations using an implicit IV maintained within
1095a8e1175bSopenharmony_ci *   the transformation context, its contents are stored within
1096a8e1175bSopenharmony_ci *   iv_{enc/dec}.
1097a8e1175bSopenharmony_ci * - The value of ivlen indicates the length of the IV.
1098a8e1175bSopenharmony_ci *   This is redundant in case of stream/CBC transformations
1099a8e1175bSopenharmony_ci *   which always use 0 resp. the cipher's block length as the
1100a8e1175bSopenharmony_ci *   IV length, but is needed for AEAD ciphers and may be
1101a8e1175bSopenharmony_ci *   different from the underlying cipher's block length
1102a8e1175bSopenharmony_ci *   in this case.
1103a8e1175bSopenharmony_ci * - The field fixed_ivlen is nonzero for AEAD transformations only
1104a8e1175bSopenharmony_ci *   and indicates the length of the static part of the IV which is
1105a8e1175bSopenharmony_ci *   constant throughout the communication, and which is stored in
1106a8e1175bSopenharmony_ci *   the first fixed_ivlen bytes of the iv_{enc/dec} arrays.
1107a8e1175bSopenharmony_ci * - tls_version denotes the 2-byte TLS version
1108a8e1175bSopenharmony_ci * - For stream/CBC transformations, maclen denotes the length of the
1109a8e1175bSopenharmony_ci *   authentication tag, while taglen is unused and 0.
1110a8e1175bSopenharmony_ci * - For AEAD transformations, taglen denotes the length of the
1111a8e1175bSopenharmony_ci *   authentication tag, while maclen is unused and 0.
1112a8e1175bSopenharmony_ci * - For CBC transformations, encrypt_then_mac determines the
1113a8e1175bSopenharmony_ci *   order of encryption and authentication. This field is unused
1114a8e1175bSopenharmony_ci *   in other transformations.
1115a8e1175bSopenharmony_ci *
1116a8e1175bSopenharmony_ci */
1117a8e1175bSopenharmony_cistruct mbedtls_ssl_transform {
1118a8e1175bSopenharmony_ci    /*
1119a8e1175bSopenharmony_ci     * Session specific crypto layer
1120a8e1175bSopenharmony_ci     */
1121a8e1175bSopenharmony_ci    size_t minlen;                      /*!<  min. ciphertext length  */
1122a8e1175bSopenharmony_ci    size_t ivlen;                       /*!<  IV length               */
1123a8e1175bSopenharmony_ci    size_t fixed_ivlen;                 /*!<  Fixed part of IV (AEAD) */
1124a8e1175bSopenharmony_ci    size_t maclen;                      /*!<  MAC(CBC) len            */
1125a8e1175bSopenharmony_ci    size_t taglen;                      /*!<  TAG(AEAD) len           */
1126a8e1175bSopenharmony_ci
1127a8e1175bSopenharmony_ci    unsigned char iv_enc[16];           /*!<  IV (encryption)         */
1128a8e1175bSopenharmony_ci    unsigned char iv_dec[16];           /*!<  IV (decryption)         */
1129a8e1175bSopenharmony_ci
1130a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
1131a8e1175bSopenharmony_ci
1132a8e1175bSopenharmony_ci#if defined(MBEDTLS_USE_PSA_CRYPTO)
1133a8e1175bSopenharmony_ci    mbedtls_svc_key_id_t psa_mac_enc;           /*!<  MAC (encryption)        */
1134a8e1175bSopenharmony_ci    mbedtls_svc_key_id_t psa_mac_dec;           /*!<  MAC (decryption)        */
1135a8e1175bSopenharmony_ci    psa_algorithm_t psa_mac_alg;                /*!<  psa MAC algorithm       */
1136a8e1175bSopenharmony_ci#else
1137a8e1175bSopenharmony_ci    mbedtls_md_context_t md_ctx_enc;            /*!<  MAC (encryption)        */
1138a8e1175bSopenharmony_ci    mbedtls_md_context_t md_ctx_dec;            /*!<  MAC (decryption)        */
1139a8e1175bSopenharmony_ci#endif /* MBEDTLS_USE_PSA_CRYPTO */
1140a8e1175bSopenharmony_ci
1141a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1142a8e1175bSopenharmony_ci    int encrypt_then_mac;       /*!< flag for EtM activation                */
1143a8e1175bSopenharmony_ci#endif
1144a8e1175bSopenharmony_ci
1145a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
1146a8e1175bSopenharmony_ci
1147a8e1175bSopenharmony_ci    mbedtls_ssl_protocol_version tls_version;
1148a8e1175bSopenharmony_ci
1149a8e1175bSopenharmony_ci#if defined(MBEDTLS_USE_PSA_CRYPTO)
1150a8e1175bSopenharmony_ci    mbedtls_svc_key_id_t psa_key_enc;           /*!<  psa encryption key      */
1151a8e1175bSopenharmony_ci    mbedtls_svc_key_id_t psa_key_dec;           /*!<  psa decryption key      */
1152a8e1175bSopenharmony_ci    psa_algorithm_t psa_alg;                    /*!<  psa algorithm           */
1153a8e1175bSopenharmony_ci#else
1154a8e1175bSopenharmony_ci    mbedtls_cipher_context_t cipher_ctx_enc;    /*!<  encryption context      */
1155a8e1175bSopenharmony_ci    mbedtls_cipher_context_t cipher_ctx_dec;    /*!<  decryption context      */
1156a8e1175bSopenharmony_ci#endif /* MBEDTLS_USE_PSA_CRYPTO */
1157a8e1175bSopenharmony_ci
1158a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
1159a8e1175bSopenharmony_ci    uint8_t in_cid_len;
1160a8e1175bSopenharmony_ci    uint8_t out_cid_len;
1161a8e1175bSopenharmony_ci    unsigned char in_cid[MBEDTLS_SSL_CID_IN_LEN_MAX];
1162a8e1175bSopenharmony_ci    unsigned char out_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX];
1163a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
1164a8e1175bSopenharmony_ci
1165a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1166a8e1175bSopenharmony_ci    /* We need the Hello random bytes in order to re-derive keys from the
1167a8e1175bSopenharmony_ci     * Master Secret and other session info,
1168a8e1175bSopenharmony_ci     * see ssl_tls12_populate_transform() */
1169a8e1175bSopenharmony_ci    unsigned char randbytes[MBEDTLS_SERVER_HELLO_RANDOM_LEN +
1170a8e1175bSopenharmony_ci                            MBEDTLS_CLIENT_HELLO_RANDOM_LEN];
1171a8e1175bSopenharmony_ci    /*!< ServerHello.random+ClientHello.random */
1172a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
1173a8e1175bSopenharmony_ci};
1174a8e1175bSopenharmony_ci
1175a8e1175bSopenharmony_ci/*
1176a8e1175bSopenharmony_ci * Return 1 if the transform uses an AEAD cipher, 0 otherwise.
1177a8e1175bSopenharmony_ci * Equivalently, return 0 if a separate MAC is used, 1 otherwise.
1178a8e1175bSopenharmony_ci */
1179a8e1175bSopenharmony_cistatic inline int mbedtls_ssl_transform_uses_aead(
1180a8e1175bSopenharmony_ci    const mbedtls_ssl_transform *transform)
1181a8e1175bSopenharmony_ci{
1182a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
1183a8e1175bSopenharmony_ci    return transform->maclen == 0 && transform->taglen != 0;
1184a8e1175bSopenharmony_ci#else
1185a8e1175bSopenharmony_ci    (void) transform;
1186a8e1175bSopenharmony_ci    return 1;
1187a8e1175bSopenharmony_ci#endif
1188a8e1175bSopenharmony_ci}
1189a8e1175bSopenharmony_ci
1190a8e1175bSopenharmony_ci/*
1191a8e1175bSopenharmony_ci * Internal representation of record frames
1192a8e1175bSopenharmony_ci *
1193a8e1175bSopenharmony_ci * Instances come in two flavors:
1194a8e1175bSopenharmony_ci * (1) Encrypted
1195a8e1175bSopenharmony_ci *     These always have data_offset = 0
1196a8e1175bSopenharmony_ci * (2) Unencrypted
1197a8e1175bSopenharmony_ci *     These have data_offset set to the amount of
1198a8e1175bSopenharmony_ci *     pre-expansion during record protection. Concretely,
1199a8e1175bSopenharmony_ci *     this is the length of the fixed part of the explicit IV
1200a8e1175bSopenharmony_ci *     used for encryption, or 0 if no explicit IV is used
1201a8e1175bSopenharmony_ci *     (e.g. for stream ciphers).
1202a8e1175bSopenharmony_ci *
1203a8e1175bSopenharmony_ci * The reason for the data_offset in the unencrypted case
1204a8e1175bSopenharmony_ci * is to allow for in-place conversion of an unencrypted to
1205a8e1175bSopenharmony_ci * an encrypted record. If the offset wasn't included, the
1206a8e1175bSopenharmony_ci * encrypted content would need to be shifted afterwards to
1207a8e1175bSopenharmony_ci * make space for the fixed IV.
1208a8e1175bSopenharmony_ci *
1209a8e1175bSopenharmony_ci */
1210a8e1175bSopenharmony_ci#if MBEDTLS_SSL_CID_OUT_LEN_MAX > MBEDTLS_SSL_CID_IN_LEN_MAX
1211a8e1175bSopenharmony_ci#define MBEDTLS_SSL_CID_LEN_MAX MBEDTLS_SSL_CID_OUT_LEN_MAX
1212a8e1175bSopenharmony_ci#else
1213a8e1175bSopenharmony_ci#define MBEDTLS_SSL_CID_LEN_MAX MBEDTLS_SSL_CID_IN_LEN_MAX
1214a8e1175bSopenharmony_ci#endif
1215a8e1175bSopenharmony_ci
1216a8e1175bSopenharmony_citypedef struct {
1217a8e1175bSopenharmony_ci    uint8_t ctr[MBEDTLS_SSL_SEQUENCE_NUMBER_LEN];  /* In TLS:  The implicit record sequence number.
1218a8e1175bSopenharmony_ci                                                    * In DTLS: The 2-byte epoch followed by
1219a8e1175bSopenharmony_ci                                                    *          the 6-byte sequence number.
1220a8e1175bSopenharmony_ci                                                    * This is stored as a raw big endian byte array
1221a8e1175bSopenharmony_ci                                                    * as opposed to a uint64_t because we rarely
1222a8e1175bSopenharmony_ci                                                    * need to perform arithmetic on this, but do
1223a8e1175bSopenharmony_ci                                                    * need it as a Byte array for the purpose of
1224a8e1175bSopenharmony_ci                                                    * MAC computations.                             */
1225a8e1175bSopenharmony_ci    uint8_t type;           /* The record content type.                      */
1226a8e1175bSopenharmony_ci    uint8_t ver[2];         /* SSL/TLS version as present on the wire.
1227a8e1175bSopenharmony_ci                             * Convert to internal presentation of versions
1228a8e1175bSopenharmony_ci                             * using mbedtls_ssl_read_version() and
1229a8e1175bSopenharmony_ci                             * mbedtls_ssl_write_version().
1230a8e1175bSopenharmony_ci                             * Keep wire-format for MAC computations.        */
1231a8e1175bSopenharmony_ci
1232a8e1175bSopenharmony_ci    unsigned char *buf;     /* Memory buffer enclosing the record content    */
1233a8e1175bSopenharmony_ci    size_t buf_len;         /* Buffer length                                 */
1234a8e1175bSopenharmony_ci    size_t data_offset;     /* Offset of record content                      */
1235a8e1175bSopenharmony_ci    size_t data_len;        /* Length of record content                      */
1236a8e1175bSopenharmony_ci
1237a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
1238a8e1175bSopenharmony_ci    uint8_t cid_len;        /* Length of the CID (0 if not present)          */
1239a8e1175bSopenharmony_ci    unsigned char cid[MBEDTLS_SSL_CID_LEN_MAX];   /* The CID                 */
1240a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
1241a8e1175bSopenharmony_ci} mbedtls_record;
1242a8e1175bSopenharmony_ci
1243a8e1175bSopenharmony_ci#if defined(MBEDTLS_X509_CRT_PARSE_C)
1244a8e1175bSopenharmony_ci/*
1245a8e1175bSopenharmony_ci * List of certificate + private key pairs
1246a8e1175bSopenharmony_ci */
1247a8e1175bSopenharmony_cistruct mbedtls_ssl_key_cert {
1248a8e1175bSopenharmony_ci    mbedtls_x509_crt *cert;                 /*!< cert                       */
1249a8e1175bSopenharmony_ci    mbedtls_pk_context *key;                /*!< private key                */
1250a8e1175bSopenharmony_ci    mbedtls_ssl_key_cert *next;             /*!< next key/cert pair         */
1251a8e1175bSopenharmony_ci};
1252a8e1175bSopenharmony_ci#endif /* MBEDTLS_X509_CRT_PARSE_C */
1253a8e1175bSopenharmony_ci
1254a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_PROTO_DTLS)
1255a8e1175bSopenharmony_ci/*
1256a8e1175bSopenharmony_ci * List of handshake messages kept around for resending
1257a8e1175bSopenharmony_ci */
1258a8e1175bSopenharmony_cistruct mbedtls_ssl_flight_item {
1259a8e1175bSopenharmony_ci    unsigned char *p;       /*!< message, including handshake headers   */
1260a8e1175bSopenharmony_ci    size_t len;             /*!< length of p                            */
1261a8e1175bSopenharmony_ci    unsigned char type;     /*!< type of the message: handshake or CCS  */
1262a8e1175bSopenharmony_ci    mbedtls_ssl_flight_item *next;  /*!< next handshake message(s)              */
1263a8e1175bSopenharmony_ci};
1264a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_PROTO_DTLS */
1265a8e1175bSopenharmony_ci
1266a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1267a8e1175bSopenharmony_ci/**
1268a8e1175bSopenharmony_ci * \brief Given an SSL context and its associated configuration, write the TLS
1269a8e1175bSopenharmony_ci *        1.2 specific extensions of the ClientHello message.
1270a8e1175bSopenharmony_ci *
1271a8e1175bSopenharmony_ci * \param[in]   ssl     SSL context
1272a8e1175bSopenharmony_ci * \param[in]   buf     Base address of the buffer where to write the extensions
1273a8e1175bSopenharmony_ci * \param[in]   end     End address of the buffer where to write the extensions
1274a8e1175bSopenharmony_ci * \param       uses_ec Whether one proposed ciphersuite uses an elliptic curve
1275a8e1175bSopenharmony_ci *                      (<> 0) or not ( 0 ).
1276a8e1175bSopenharmony_ci * \param[out]  out_len Length of the data written into the buffer \p buf
1277a8e1175bSopenharmony_ci */
1278a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL
1279a8e1175bSopenharmony_ciint mbedtls_ssl_tls12_write_client_hello_exts(mbedtls_ssl_context *ssl,
1280a8e1175bSopenharmony_ci                                              unsigned char *buf,
1281a8e1175bSopenharmony_ci                                              const unsigned char *end,
1282a8e1175bSopenharmony_ci                                              int uses_ec,
1283a8e1175bSopenharmony_ci                                              size_t *out_len);
1284a8e1175bSopenharmony_ci#endif
1285a8e1175bSopenharmony_ci
1286a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
1287a8e1175bSopenharmony_ci    defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
1288a8e1175bSopenharmony_ci
1289a8e1175bSopenharmony_ci/**
1290a8e1175bSopenharmony_ci * \brief Find the preferred hash for a given signature algorithm.
1291a8e1175bSopenharmony_ci *
1292a8e1175bSopenharmony_ci * \param[in]   ssl     SSL context
1293a8e1175bSopenharmony_ci * \param[in]   sig_alg A signature algorithm identifier as defined in the
1294a8e1175bSopenharmony_ci *                      TLS 1.2 SignatureAlgorithm enumeration.
1295a8e1175bSopenharmony_ci *
1296a8e1175bSopenharmony_ci * \return  The preferred hash algorithm for \p sig_alg. It is a hash algorithm
1297a8e1175bSopenharmony_ci *          identifier as defined in the TLS 1.2 HashAlgorithm enumeration.
1298a8e1175bSopenharmony_ci */
1299a8e1175bSopenharmony_ciunsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
1300a8e1175bSopenharmony_ci    mbedtls_ssl_context *ssl,
1301a8e1175bSopenharmony_ci    unsigned int sig_alg);
1302a8e1175bSopenharmony_ci
1303a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
1304a8e1175bSopenharmony_ci          MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
1305a8e1175bSopenharmony_ci
1306a8e1175bSopenharmony_ci/**
1307a8e1175bSopenharmony_ci * \brief           Free referenced items in an SSL transform context and clear
1308a8e1175bSopenharmony_ci *                  memory
1309a8e1175bSopenharmony_ci *
1310a8e1175bSopenharmony_ci * \param transform SSL transform context
1311a8e1175bSopenharmony_ci */
1312a8e1175bSopenharmony_civoid mbedtls_ssl_transform_free(mbedtls_ssl_transform *transform);
1313a8e1175bSopenharmony_ci
1314a8e1175bSopenharmony_ci/**
1315a8e1175bSopenharmony_ci * \brief           Free referenced items in an SSL handshake context and clear
1316a8e1175bSopenharmony_ci *                  memory
1317a8e1175bSopenharmony_ci *
1318a8e1175bSopenharmony_ci * \param ssl       SSL context
1319a8e1175bSopenharmony_ci */
1320a8e1175bSopenharmony_civoid mbedtls_ssl_handshake_free(mbedtls_ssl_context *ssl);
1321a8e1175bSopenharmony_ci
1322a8e1175bSopenharmony_ci/* set inbound transform of ssl context */
1323a8e1175bSopenharmony_civoid mbedtls_ssl_set_inbound_transform(mbedtls_ssl_context *ssl,
1324a8e1175bSopenharmony_ci                                       mbedtls_ssl_transform *transform);
1325a8e1175bSopenharmony_ci
1326a8e1175bSopenharmony_ci/* set outbound transform of ssl context */
1327a8e1175bSopenharmony_civoid mbedtls_ssl_set_outbound_transform(mbedtls_ssl_context *ssl,
1328a8e1175bSopenharmony_ci                                        mbedtls_ssl_transform *transform);
1329a8e1175bSopenharmony_ci
1330a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL
1331a8e1175bSopenharmony_ciint mbedtls_ssl_handshake_client_step(mbedtls_ssl_context *ssl);
1332a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL
1333a8e1175bSopenharmony_ciint mbedtls_ssl_handshake_server_step(mbedtls_ssl_context *ssl);
1334a8e1175bSopenharmony_civoid mbedtls_ssl_handshake_wrapup(mbedtls_ssl_context *ssl);
1335a8e1175bSopenharmony_cistatic inline void mbedtls_ssl_handshake_set_state(mbedtls_ssl_context *ssl,
1336a8e1175bSopenharmony_ci                                                   mbedtls_ssl_states state)
1337a8e1175bSopenharmony_ci{
1338a8e1175bSopenharmony_ci    ssl->state = (int) state;
1339a8e1175bSopenharmony_ci}
1340a8e1175bSopenharmony_ci
1341a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL
1342a8e1175bSopenharmony_ciint mbedtls_ssl_send_fatal_handshake_failure(mbedtls_ssl_context *ssl);
1343a8e1175bSopenharmony_ci
1344a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL
1345a8e1175bSopenharmony_ciint mbedtls_ssl_reset_checksum(mbedtls_ssl_context *ssl);
1346a8e1175bSopenharmony_ci
1347a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1348a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL
1349a8e1175bSopenharmony_ciint mbedtls_ssl_derive_keys(mbedtls_ssl_context *ssl);
1350a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_PROTO_TLS1_2  */
1351a8e1175bSopenharmony_ci
1352a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL
1353a8e1175bSopenharmony_ciint mbedtls_ssl_handle_message_type(mbedtls_ssl_context *ssl);
1354a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL
1355a8e1175bSopenharmony_ciint mbedtls_ssl_prepare_handshake_record(mbedtls_ssl_context *ssl);
1356a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL
1357a8e1175bSopenharmony_ciint mbedtls_ssl_update_handshake_status(mbedtls_ssl_context *ssl);
1358a8e1175bSopenharmony_ci
1359a8e1175bSopenharmony_ci/**
1360a8e1175bSopenharmony_ci * \brief       Update record layer
1361a8e1175bSopenharmony_ci *
1362a8e1175bSopenharmony_ci *              This function roughly separates the implementation
1363a8e1175bSopenharmony_ci *              of the logic of (D)TLS from the implementation
1364a8e1175bSopenharmony_ci *              of the secure transport.
1365a8e1175bSopenharmony_ci *
1366a8e1175bSopenharmony_ci * \param  ssl              The SSL context to use.
1367a8e1175bSopenharmony_ci * \param  update_hs_digest This indicates if the handshake digest
1368a8e1175bSopenharmony_ci *                          should be automatically updated in case
1369a8e1175bSopenharmony_ci *                          a handshake message is found.
1370a8e1175bSopenharmony_ci *
1371a8e1175bSopenharmony_ci * \return      0 or non-zero error code.
1372a8e1175bSopenharmony_ci *
1373a8e1175bSopenharmony_ci * \note        A clarification on what is called 'record layer' here
1374a8e1175bSopenharmony_ci *              is in order, as many sensible definitions are possible:
1375a8e1175bSopenharmony_ci *
1376a8e1175bSopenharmony_ci *              The record layer takes as input an untrusted underlying
1377a8e1175bSopenharmony_ci *              transport (stream or datagram) and transforms it into
1378a8e1175bSopenharmony_ci *              a serially multiplexed, secure transport, which
1379a8e1175bSopenharmony_ci *              conceptually provides the following:
1380a8e1175bSopenharmony_ci *
1381a8e1175bSopenharmony_ci *              (1) Three datagram based, content-agnostic transports
1382a8e1175bSopenharmony_ci *                  for handshake, alert and CCS messages.
1383a8e1175bSopenharmony_ci *              (2) One stream- or datagram-based transport
1384a8e1175bSopenharmony_ci *                  for application data.
1385a8e1175bSopenharmony_ci *              (3) Functionality for changing the underlying transform
1386a8e1175bSopenharmony_ci *                  securing the contents.
1387a8e1175bSopenharmony_ci *
1388a8e1175bSopenharmony_ci *              The interface to this functionality is given as follows:
1389a8e1175bSopenharmony_ci *
1390a8e1175bSopenharmony_ci *              a Updating
1391a8e1175bSopenharmony_ci *                [Currently implemented by mbedtls_ssl_read_record]
1392a8e1175bSopenharmony_ci *
1393a8e1175bSopenharmony_ci *                Check if and on which of the four 'ports' data is pending:
1394a8e1175bSopenharmony_ci *                Nothing, a controlling datagram of type (1), or application
1395a8e1175bSopenharmony_ci *                data (2). In any case data is present, internal buffers
1396a8e1175bSopenharmony_ci *                provide access to the data for the user to process it.
1397a8e1175bSopenharmony_ci *                Consumption of type (1) datagrams is done automatically
1398a8e1175bSopenharmony_ci *                on the next update, invalidating that the internal buffers
1399a8e1175bSopenharmony_ci *                for previous datagrams, while consumption of application
1400a8e1175bSopenharmony_ci *                data (2) is user-controlled.
1401a8e1175bSopenharmony_ci *
1402a8e1175bSopenharmony_ci *              b Reading of application data
1403a8e1175bSopenharmony_ci *                [Currently manual adaption of ssl->in_offt pointer]
1404a8e1175bSopenharmony_ci *
1405a8e1175bSopenharmony_ci *                As mentioned in the last paragraph, consumption of data
1406a8e1175bSopenharmony_ci *                is different from the automatic consumption of control
1407a8e1175bSopenharmony_ci *                datagrams (1) because application data is treated as a stream.
1408a8e1175bSopenharmony_ci *
1409a8e1175bSopenharmony_ci *              c Tracking availability of application data
1410a8e1175bSopenharmony_ci *                [Currently manually through decreasing ssl->in_msglen]
1411a8e1175bSopenharmony_ci *
1412a8e1175bSopenharmony_ci *                For efficiency and to retain datagram semantics for
1413a8e1175bSopenharmony_ci *                application data in case of DTLS, the record layer
1414a8e1175bSopenharmony_ci *                provides functionality for checking how much application
1415a8e1175bSopenharmony_ci *                data is still available in the internal buffer.
1416a8e1175bSopenharmony_ci *
1417a8e1175bSopenharmony_ci *              d Changing the transformation securing the communication.
1418a8e1175bSopenharmony_ci *
1419a8e1175bSopenharmony_ci *              Given an opaque implementation of the record layer in the
1420a8e1175bSopenharmony_ci *              above sense, it should be possible to implement the logic
1421a8e1175bSopenharmony_ci *              of (D)TLS on top of it without the need to know anything
1422a8e1175bSopenharmony_ci *              about the record layer's internals. This is done e.g.
1423a8e1175bSopenharmony_ci *              in all the handshake handling functions, and in the
1424a8e1175bSopenharmony_ci *              application data reading function mbedtls_ssl_read.
1425a8e1175bSopenharmony_ci *
1426a8e1175bSopenharmony_ci * \note        The above tries to give a conceptual picture of the
1427a8e1175bSopenharmony_ci *              record layer, but the current implementation deviates
1428a8e1175bSopenharmony_ci *              from it in some places. For example, our implementation of
1429a8e1175bSopenharmony_ci *              the update functionality through mbedtls_ssl_read_record
1430a8e1175bSopenharmony_ci *              discards datagrams depending on the current state, which
1431a8e1175bSopenharmony_ci *              wouldn't fall under the record layer's responsibility
1432a8e1175bSopenharmony_ci *              following the above definition.
1433a8e1175bSopenharmony_ci *
1434a8e1175bSopenharmony_ci */
1435a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL
1436a8e1175bSopenharmony_ciint mbedtls_ssl_read_record(mbedtls_ssl_context *ssl,
1437a8e1175bSopenharmony_ci                            unsigned update_hs_digest);
1438a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL
1439a8e1175bSopenharmony_ciint mbedtls_ssl_fetch_input(mbedtls_ssl_context *ssl, size_t nb_want);
1440a8e1175bSopenharmony_ci
1441a8e1175bSopenharmony_ci/*
1442a8e1175bSopenharmony_ci * Write handshake message header
1443a8e1175bSopenharmony_ci */
1444a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL
1445a8e1175bSopenharmony_ciint mbedtls_ssl_start_handshake_msg(mbedtls_ssl_context *ssl, unsigned char hs_type,
1446a8e1175bSopenharmony_ci                                    unsigned char **buf, size_t *buf_len);
1447a8e1175bSopenharmony_ci
1448a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL
1449a8e1175bSopenharmony_ciint mbedtls_ssl_write_handshake_msg_ext(mbedtls_ssl_context *ssl,
1450a8e1175bSopenharmony_ci                                        int update_checksum,
1451a8e1175bSopenharmony_ci                                        int force_flush);
1452a8e1175bSopenharmony_cistatic inline int mbedtls_ssl_write_handshake_msg(mbedtls_ssl_context *ssl)
1453a8e1175bSopenharmony_ci{
1454a8e1175bSopenharmony_ci    return mbedtls_ssl_write_handshake_msg_ext(ssl, 1 /* update checksum */, 1 /* force flush */);
1455a8e1175bSopenharmony_ci}
1456a8e1175bSopenharmony_ci
1457a8e1175bSopenharmony_ci/*
1458a8e1175bSopenharmony_ci * Write handshake message tail
1459a8e1175bSopenharmony_ci */
1460a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL
1461a8e1175bSopenharmony_ciint mbedtls_ssl_finish_handshake_msg(mbedtls_ssl_context *ssl,
1462a8e1175bSopenharmony_ci                                     size_t buf_len, size_t msg_len);
1463a8e1175bSopenharmony_ci
1464a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL
1465a8e1175bSopenharmony_ciint mbedtls_ssl_write_record(mbedtls_ssl_context *ssl, int force_flush);
1466a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL
1467a8e1175bSopenharmony_ciint mbedtls_ssl_flush_output(mbedtls_ssl_context *ssl);
1468a8e1175bSopenharmony_ci
1469a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL
1470a8e1175bSopenharmony_ciint mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl);
1471a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL
1472a8e1175bSopenharmony_ciint mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl);
1473a8e1175bSopenharmony_ci
1474a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL
1475a8e1175bSopenharmony_ciint mbedtls_ssl_parse_change_cipher_spec(mbedtls_ssl_context *ssl);
1476a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL
1477a8e1175bSopenharmony_ciint mbedtls_ssl_write_change_cipher_spec(mbedtls_ssl_context *ssl);
1478a8e1175bSopenharmony_ci
1479a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL
1480a8e1175bSopenharmony_ciint mbedtls_ssl_parse_finished(mbedtls_ssl_context *ssl);
1481a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL
1482a8e1175bSopenharmony_ciint mbedtls_ssl_write_finished(mbedtls_ssl_context *ssl);
1483a8e1175bSopenharmony_ci
1484a8e1175bSopenharmony_civoid mbedtls_ssl_optimize_checksum(mbedtls_ssl_context *ssl,
1485a8e1175bSopenharmony_ci                                   const mbedtls_ssl_ciphersuite_t *ciphersuite_info);
1486a8e1175bSopenharmony_ci
1487a8e1175bSopenharmony_ci/*
1488a8e1175bSopenharmony_ci * Update checksum of handshake messages.
1489a8e1175bSopenharmony_ci */
1490a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL
1491a8e1175bSopenharmony_ciint mbedtls_ssl_add_hs_msg_to_checksum(mbedtls_ssl_context *ssl,
1492a8e1175bSopenharmony_ci                                       unsigned hs_type,
1493a8e1175bSopenharmony_ci                                       unsigned char const *msg,
1494a8e1175bSopenharmony_ci                                       size_t msg_len);
1495a8e1175bSopenharmony_ci
1496a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL
1497a8e1175bSopenharmony_ciint mbedtls_ssl_add_hs_hdr_to_checksum(mbedtls_ssl_context *ssl,
1498a8e1175bSopenharmony_ci                                       unsigned hs_type,
1499a8e1175bSopenharmony_ci                                       size_t total_hs_len);
1500a8e1175bSopenharmony_ci
1501a8e1175bSopenharmony_ci#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
1502a8e1175bSopenharmony_ci#if !defined(MBEDTLS_USE_PSA_CRYPTO)
1503a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL
1504a8e1175bSopenharmony_ciint mbedtls_ssl_psk_derive_premaster(mbedtls_ssl_context *ssl,
1505a8e1175bSopenharmony_ci                                     mbedtls_key_exchange_type_t key_ex);
1506a8e1175bSopenharmony_ci#endif /* !MBEDTLS_USE_PSA_CRYPTO */
1507a8e1175bSopenharmony_ci#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
1508a8e1175bSopenharmony_ci
1509a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
1510a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_CLI_C)
1511a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL
1512a8e1175bSopenharmony_ciint mbedtls_ssl_conf_has_static_psk(mbedtls_ssl_config const *conf);
1513a8e1175bSopenharmony_ci#endif
1514a8e1175bSopenharmony_ci#if defined(MBEDTLS_USE_PSA_CRYPTO)
1515a8e1175bSopenharmony_ci/**
1516a8e1175bSopenharmony_ci * Get the first defined opaque PSK by order of precedence:
1517a8e1175bSopenharmony_ci * 1. handshake PSK set by \c mbedtls_ssl_set_hs_psk_opaque() in the PSK
1518a8e1175bSopenharmony_ci *    callback
1519a8e1175bSopenharmony_ci * 2. static PSK configured by \c mbedtls_ssl_conf_psk_opaque()
1520a8e1175bSopenharmony_ci * Return an opaque PSK
1521a8e1175bSopenharmony_ci */
1522a8e1175bSopenharmony_cistatic inline mbedtls_svc_key_id_t mbedtls_ssl_get_opaque_psk(
1523a8e1175bSopenharmony_ci    const mbedtls_ssl_context *ssl)
1524a8e1175bSopenharmony_ci{
1525a8e1175bSopenharmony_ci    if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
1526a8e1175bSopenharmony_ci        return ssl->handshake->psk_opaque;
1527a8e1175bSopenharmony_ci    }
1528a8e1175bSopenharmony_ci
1529a8e1175bSopenharmony_ci    if (!mbedtls_svc_key_id_is_null(ssl->conf->psk_opaque)) {
1530a8e1175bSopenharmony_ci        return ssl->conf->psk_opaque;
1531a8e1175bSopenharmony_ci    }
1532a8e1175bSopenharmony_ci
1533a8e1175bSopenharmony_ci    return MBEDTLS_SVC_KEY_ID_INIT;
1534a8e1175bSopenharmony_ci}
1535a8e1175bSopenharmony_ci#else
1536a8e1175bSopenharmony_ci
1537a8e1175bSopenharmony_ci/**
1538a8e1175bSopenharmony_ci * Get the first defined PSK by order of precedence:
1539a8e1175bSopenharmony_ci * 1. handshake PSK set by \c mbedtls_ssl_set_hs_psk() in the PSK callback
1540a8e1175bSopenharmony_ci * 2. static PSK configured by \c mbedtls_ssl_conf_psk()
1541a8e1175bSopenharmony_ci * Return a code and update the pair (PSK, PSK length) passed to this function
1542a8e1175bSopenharmony_ci */
1543a8e1175bSopenharmony_cistatic inline int mbedtls_ssl_get_psk(const mbedtls_ssl_context *ssl,
1544a8e1175bSopenharmony_ci                                      const unsigned char **psk, size_t *psk_len)
1545a8e1175bSopenharmony_ci{
1546a8e1175bSopenharmony_ci    if (ssl->MBEDTLS_PRIVATE(handshake)->psk != NULL && ssl->MBEDTLS_PRIVATE(handshake)->psk_len > 0)
1547a8e1175bSopenharmony_ci    {
1548a8e1175bSopenharmony_ci        *psk = ssl->MBEDTLS_PRIVATE(handshake)->psk;
1549a8e1175bSopenharmony_ci        *psk_len = ssl->MBEDTLS_PRIVATE(handshake)->psk_len;
1550a8e1175bSopenharmony_ci    } else if (ssl->MBEDTLS_PRIVATE(conf)->MBEDTLS_PRIVATE(psk) != NULL &&
1551a8e1175bSopenharmony_ci               ssl->MBEDTLS_PRIVATE(conf)->MBEDTLS_PRIVATE(psk_len) > 0) {
1552a8e1175bSopenharmony_ci        *psk = ssl->MBEDTLS_PRIVATE(conf)->MBEDTLS_PRIVATE(psk);
1553a8e1175bSopenharmony_ci        *psk_len = ssl->MBEDTLS_PRIVATE(conf)->MBEDTLS_PRIVATE(psk_len);
1554a8e1175bSopenharmony_ci    } else {
1555a8e1175bSopenharmony_ci        *psk = NULL;
1556a8e1175bSopenharmony_ci        *psk_len = 0;
1557a8e1175bSopenharmony_ci        return MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED;
1558a8e1175bSopenharmony_ci    }
1559a8e1175bSopenharmony_ci
1560a8e1175bSopenharmony_ci    return 0;
1561a8e1175bSopenharmony_ci}
1562a8e1175bSopenharmony_ci#endif /* MBEDTLS_USE_PSA_CRYPTO */
1563a8e1175bSopenharmony_ci
1564a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
1565a8e1175bSopenharmony_ci
1566a8e1175bSopenharmony_ci#if defined(MBEDTLS_PK_C)
1567a8e1175bSopenharmony_ciunsigned char mbedtls_ssl_sig_from_pk(mbedtls_pk_context *pk);
1568a8e1175bSopenharmony_ciunsigned char mbedtls_ssl_sig_from_pk_alg(mbedtls_pk_type_t type);
1569a8e1175bSopenharmony_cimbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig(unsigned char sig);
1570a8e1175bSopenharmony_ci#endif
1571a8e1175bSopenharmony_ci
1572a8e1175bSopenharmony_cimbedtls_md_type_t mbedtls_ssl_md_alg_from_hash(unsigned char hash);
1573a8e1175bSopenharmony_ciunsigned char mbedtls_ssl_hash_from_md_alg(int md);
1574a8e1175bSopenharmony_ci
1575a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1576a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL
1577a8e1175bSopenharmony_ciint mbedtls_ssl_set_calc_verify_md(mbedtls_ssl_context *ssl, int md);
1578a8e1175bSopenharmony_ci#endif
1579a8e1175bSopenharmony_ci
1580a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL
1581a8e1175bSopenharmony_ciint mbedtls_ssl_check_curve_tls_id(const mbedtls_ssl_context *ssl, uint16_t tls_id);
1582a8e1175bSopenharmony_ci#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
1583a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL
1584a8e1175bSopenharmony_ciint mbedtls_ssl_check_curve(const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id);
1585a8e1175bSopenharmony_ci#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
1586a8e1175bSopenharmony_ci
1587a8e1175bSopenharmony_ci/**
1588a8e1175bSopenharmony_ci * \brief Return PSA EC info for the specified TLS ID.
1589a8e1175bSopenharmony_ci *
1590a8e1175bSopenharmony_ci * \param tls_id    The TLS ID to look for
1591a8e1175bSopenharmony_ci * \param type      If the TLD ID is supported, then proper \c psa_key_type_t
1592a8e1175bSopenharmony_ci *                  value is returned here. Can be NULL.
1593a8e1175bSopenharmony_ci * \param bits      If the TLD ID is supported, then proper bit size is returned
1594a8e1175bSopenharmony_ci *                  here. Can be NULL.
1595a8e1175bSopenharmony_ci * \return          PSA_SUCCESS if the TLS ID is supported,
1596a8e1175bSopenharmony_ci *                  PSA_ERROR_NOT_SUPPORTED otherwise
1597a8e1175bSopenharmony_ci *
1598a8e1175bSopenharmony_ci * \note            If either \c family or \c bits parameters are NULL, then
1599a8e1175bSopenharmony_ci *                  the corresponding value is not returned.
1600a8e1175bSopenharmony_ci *                  The function can be called with both parameters as NULL
1601a8e1175bSopenharmony_ci *                  simply to check if a specific TLS ID is supported.
1602a8e1175bSopenharmony_ci */
1603a8e1175bSopenharmony_ciint mbedtls_ssl_get_psa_curve_info_from_tls_id(uint16_t tls_id,
1604a8e1175bSopenharmony_ci                                               psa_key_type_t *type,
1605a8e1175bSopenharmony_ci                                               size_t *bits);
1606a8e1175bSopenharmony_ci
1607a8e1175bSopenharmony_ci/**
1608a8e1175bSopenharmony_ci * \brief Return \c mbedtls_ecp_group_id for the specified TLS ID.
1609a8e1175bSopenharmony_ci *
1610a8e1175bSopenharmony_ci * \param tls_id    The TLS ID to look for
1611a8e1175bSopenharmony_ci * \return          Proper \c mbedtls_ecp_group_id if the TLS ID is supported,
1612a8e1175bSopenharmony_ci *                  or MBEDTLS_ECP_DP_NONE otherwise
1613a8e1175bSopenharmony_ci */
1614a8e1175bSopenharmony_cimbedtls_ecp_group_id mbedtls_ssl_get_ecp_group_id_from_tls_id(uint16_t tls_id);
1615a8e1175bSopenharmony_ci
1616a8e1175bSopenharmony_ci/**
1617a8e1175bSopenharmony_ci * \brief Return TLS ID for the specified \c mbedtls_ecp_group_id.
1618a8e1175bSopenharmony_ci *
1619a8e1175bSopenharmony_ci * \param grp_id    The \c mbedtls_ecp_group_id ID to look for
1620a8e1175bSopenharmony_ci * \return          Proper TLS ID if the \c mbedtls_ecp_group_id is supported,
1621a8e1175bSopenharmony_ci *                  or 0 otherwise
1622a8e1175bSopenharmony_ci */
1623a8e1175bSopenharmony_ciuint16_t mbedtls_ssl_get_tls_id_from_ecp_group_id(mbedtls_ecp_group_id grp_id);
1624a8e1175bSopenharmony_ci
1625a8e1175bSopenharmony_ci#if defined(MBEDTLS_DEBUG_C)
1626a8e1175bSopenharmony_ci/**
1627a8e1175bSopenharmony_ci * \brief Return EC's name for the specified TLS ID.
1628a8e1175bSopenharmony_ci *
1629a8e1175bSopenharmony_ci * \param tls_id    The TLS ID to look for
1630a8e1175bSopenharmony_ci * \return          A pointer to a const string with the proper name. If TLS
1631a8e1175bSopenharmony_ci *                  ID is not supported, a NULL pointer is returned instead.
1632a8e1175bSopenharmony_ci */
1633a8e1175bSopenharmony_ciconst char *mbedtls_ssl_get_curve_name_from_tls_id(uint16_t tls_id);
1634a8e1175bSopenharmony_ci#endif
1635a8e1175bSopenharmony_ci
1636a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_DTLS_SRTP)
1637a8e1175bSopenharmony_cistatic inline mbedtls_ssl_srtp_profile mbedtls_ssl_check_srtp_profile_value
1638a8e1175bSopenharmony_ci    (const uint16_t srtp_profile_value)
1639a8e1175bSopenharmony_ci{
1640a8e1175bSopenharmony_ci    switch (srtp_profile_value) {
1641a8e1175bSopenharmony_ci        case MBEDTLS_TLS_SRTP_AES128_CM_HMAC_SHA1_80:
1642a8e1175bSopenharmony_ci        case MBEDTLS_TLS_SRTP_AES128_CM_HMAC_SHA1_32:
1643a8e1175bSopenharmony_ci        case MBEDTLS_TLS_SRTP_NULL_HMAC_SHA1_80:
1644a8e1175bSopenharmony_ci        case MBEDTLS_TLS_SRTP_NULL_HMAC_SHA1_32:
1645a8e1175bSopenharmony_ci            return srtp_profile_value;
1646a8e1175bSopenharmony_ci        default: break;
1647a8e1175bSopenharmony_ci    }
1648a8e1175bSopenharmony_ci    return MBEDTLS_TLS_SRTP_UNSET;
1649a8e1175bSopenharmony_ci}
1650a8e1175bSopenharmony_ci#endif
1651a8e1175bSopenharmony_ci
1652a8e1175bSopenharmony_ci#if defined(MBEDTLS_X509_CRT_PARSE_C)
1653a8e1175bSopenharmony_cistatic inline mbedtls_pk_context *mbedtls_ssl_own_key(mbedtls_ssl_context *ssl)
1654a8e1175bSopenharmony_ci{
1655a8e1175bSopenharmony_ci    mbedtls_ssl_key_cert *key_cert;
1656a8e1175bSopenharmony_ci
1657a8e1175bSopenharmony_ci    if (ssl->MBEDTLS_PRIVATE(handshake) != NULL && ssl->MBEDTLS_PRIVATE(handshake)->key_cert != NULL) {
1658a8e1175bSopenharmony_ci        key_cert = ssl->MBEDTLS_PRIVATE(handshake)->key_cert;
1659a8e1175bSopenharmony_ci    } else {
1660a8e1175bSopenharmony_ci        key_cert = ssl->MBEDTLS_PRIVATE(conf)->MBEDTLS_PRIVATE(key_cert);
1661a8e1175bSopenharmony_ci    }
1662a8e1175bSopenharmony_ci
1663a8e1175bSopenharmony_ci    return key_cert == NULL ? NULL : key_cert->key;
1664a8e1175bSopenharmony_ci}
1665a8e1175bSopenharmony_ci
1666a8e1175bSopenharmony_cistatic inline mbedtls_x509_crt *mbedtls_ssl_own_cert(mbedtls_ssl_context *ssl)
1667a8e1175bSopenharmony_ci{
1668a8e1175bSopenharmony_ci    mbedtls_ssl_key_cert *key_cert;
1669a8e1175bSopenharmony_ci
1670a8e1175bSopenharmony_ci    if (ssl->MBEDTLS_PRIVATE(handshake) != NULL && ssl->MBEDTLS_PRIVATE(handshake)->key_cert != NULL) {
1671a8e1175bSopenharmony_ci        key_cert = ssl->MBEDTLS_PRIVATE(handshake)->key_cert;
1672a8e1175bSopenharmony_ci    } else {
1673a8e1175bSopenharmony_ci        key_cert = ssl->MBEDTLS_PRIVATE(conf)->MBEDTLS_PRIVATE(key_cert);
1674a8e1175bSopenharmony_ci    }
1675a8e1175bSopenharmony_ci
1676a8e1175bSopenharmony_ci    return key_cert == NULL ? NULL : key_cert->cert;
1677a8e1175bSopenharmony_ci}
1678a8e1175bSopenharmony_ci
1679a8e1175bSopenharmony_ci/*
1680a8e1175bSopenharmony_ci * Check usage of a certificate wrt extensions:
1681a8e1175bSopenharmony_ci * keyUsage, extendedKeyUsage (later), and nSCertType (later).
1682a8e1175bSopenharmony_ci *
1683a8e1175bSopenharmony_ci * Warning: cert_endpoint is the endpoint of the cert (ie, of our peer when we
1684a8e1175bSopenharmony_ci * check a cert we received from them)!
1685a8e1175bSopenharmony_ci *
1686a8e1175bSopenharmony_ci * Return 0 if everything is OK, -1 if not.
1687a8e1175bSopenharmony_ci */
1688a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL
1689a8e1175bSopenharmony_ciint mbedtls_ssl_check_cert_usage(const mbedtls_x509_crt *cert,
1690a8e1175bSopenharmony_ci                                 const mbedtls_ssl_ciphersuite_t *ciphersuite,
1691a8e1175bSopenharmony_ci                                 int cert_endpoint,
1692a8e1175bSopenharmony_ci                                 uint32_t *flags);
1693a8e1175bSopenharmony_ci#endif /* MBEDTLS_X509_CRT_PARSE_C */
1694a8e1175bSopenharmony_ci
1695a8e1175bSopenharmony_civoid mbedtls_ssl_write_version(unsigned char version[2], int transport,
1696a8e1175bSopenharmony_ci                               mbedtls_ssl_protocol_version tls_version);
1697a8e1175bSopenharmony_ciuint16_t mbedtls_ssl_read_version(const unsigned char version[2],
1698a8e1175bSopenharmony_ci                                  int transport);
1699a8e1175bSopenharmony_ci
1700a8e1175bSopenharmony_cistatic inline size_t mbedtls_ssl_in_hdr_len(const mbedtls_ssl_context *ssl)
1701a8e1175bSopenharmony_ci{
1702a8e1175bSopenharmony_ci#if !defined(MBEDTLS_SSL_PROTO_DTLS)
1703a8e1175bSopenharmony_ci    ((void) ssl);
1704a8e1175bSopenharmony_ci#endif
1705a8e1175bSopenharmony_ci
1706a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_PROTO_DTLS)
1707a8e1175bSopenharmony_ci    if (ssl->MBEDTLS_PRIVATE(conf)->MBEDTLS_PRIVATE(transport) == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1708a8e1175bSopenharmony_ci        return 13;
1709a8e1175bSopenharmony_ci    } else
1710a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_PROTO_DTLS */
1711a8e1175bSopenharmony_ci    {
1712a8e1175bSopenharmony_ci        return 5;
1713a8e1175bSopenharmony_ci    }
1714a8e1175bSopenharmony_ci}
1715a8e1175bSopenharmony_ci
1716a8e1175bSopenharmony_cistatic inline size_t mbedtls_ssl_out_hdr_len(const mbedtls_ssl_context *ssl)
1717a8e1175bSopenharmony_ci{
1718a8e1175bSopenharmony_ci    return (size_t) (ssl->MBEDTLS_PRIVATE(out_iv) - ssl->MBEDTLS_PRIVATE(out_hdr));
1719a8e1175bSopenharmony_ci}
1720a8e1175bSopenharmony_ci
1721a8e1175bSopenharmony_cistatic inline size_t mbedtls_ssl_hs_hdr_len(const mbedtls_ssl_context *ssl)
1722a8e1175bSopenharmony_ci{
1723a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_PROTO_DTLS)
1724a8e1175bSopenharmony_ci    if(ssl->MBEDTLS_PRIVATE(conf)->MBEDTLS_PRIVATE(transport) == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1725a8e1175bSopenharmony_ci        return 12;
1726a8e1175bSopenharmony_ci    }
1727a8e1175bSopenharmony_ci#else
1728a8e1175bSopenharmony_ci    ((void) ssl);
1729a8e1175bSopenharmony_ci#endif
1730a8e1175bSopenharmony_ci    return 4;
1731a8e1175bSopenharmony_ci}
1732a8e1175bSopenharmony_ci
1733a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_PROTO_DTLS)
1734a8e1175bSopenharmony_civoid mbedtls_ssl_send_flight_completed(mbedtls_ssl_context *ssl);
1735a8e1175bSopenharmony_civoid mbedtls_ssl_recv_flight_completed(mbedtls_ssl_context *ssl);
1736a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL
1737a8e1175bSopenharmony_ciint mbedtls_ssl_resend(mbedtls_ssl_context *ssl);
1738a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL
1739a8e1175bSopenharmony_ciint mbedtls_ssl_flight_transmit(mbedtls_ssl_context *ssl);
1740a8e1175bSopenharmony_ci#endif
1741a8e1175bSopenharmony_ci
1742a8e1175bSopenharmony_ci/* Visible for testing purposes only */
1743a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
1744a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL
1745a8e1175bSopenharmony_ciint mbedtls_ssl_dtls_replay_check(mbedtls_ssl_context const *ssl);
1746a8e1175bSopenharmony_civoid mbedtls_ssl_dtls_replay_update(mbedtls_ssl_context *ssl);
1747a8e1175bSopenharmony_ci#endif
1748a8e1175bSopenharmony_ci
1749a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL
1750a8e1175bSopenharmony_ciint mbedtls_ssl_session_copy(mbedtls_ssl_session *dst,
1751a8e1175bSopenharmony_ci                             const mbedtls_ssl_session *src);
1752a8e1175bSopenharmony_ci
1753a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1754a8e1175bSopenharmony_ci/* The hash buffer must have at least MBEDTLS_MD_MAX_SIZE bytes of length. */
1755a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL
1756a8e1175bSopenharmony_ciint mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
1757a8e1175bSopenharmony_ci                                           unsigned char *hash, size_t *hashlen,
1758a8e1175bSopenharmony_ci                                           unsigned char *data, size_t data_len,
1759a8e1175bSopenharmony_ci                                           mbedtls_md_type_t md_alg);
1760a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1761a8e1175bSopenharmony_ci
1762a8e1175bSopenharmony_ci#ifdef __cplusplus
1763a8e1175bSopenharmony_ci}
1764a8e1175bSopenharmony_ci#endif
1765a8e1175bSopenharmony_ci
1766a8e1175bSopenharmony_civoid mbedtls_ssl_transform_init(mbedtls_ssl_transform *transform);
1767a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL
1768a8e1175bSopenharmony_ciint mbedtls_ssl_encrypt_buf(mbedtls_ssl_context *ssl,
1769a8e1175bSopenharmony_ci                            mbedtls_ssl_transform *transform,
1770a8e1175bSopenharmony_ci                            mbedtls_record *rec,
1771a8e1175bSopenharmony_ci                            int (*f_rng)(void *, unsigned char *, size_t),
1772a8e1175bSopenharmony_ci                            void *p_rng);
1773a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL
1774a8e1175bSopenharmony_ciint mbedtls_ssl_decrypt_buf(mbedtls_ssl_context const *ssl,
1775a8e1175bSopenharmony_ci                            mbedtls_ssl_transform *transform,
1776a8e1175bSopenharmony_ci                            mbedtls_record *rec);
1777a8e1175bSopenharmony_ci
1778a8e1175bSopenharmony_ci/* Length of the "epoch" field in the record header */
1779a8e1175bSopenharmony_cistatic inline size_t mbedtls_ssl_ep_len(const mbedtls_ssl_context *ssl)
1780a8e1175bSopenharmony_ci{
1781a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_PROTO_DTLS)
1782a8e1175bSopenharmony_ci    if (ssl->MBEDTLS_PRIVATE(conf)->MBEDTLS_PRIVATE(transport) == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1783a8e1175bSopenharmony_ci        return 2;
1784a8e1175bSopenharmony_ci    }
1785a8e1175bSopenharmony_ci#else
1786a8e1175bSopenharmony_ci    ((void) ssl);
1787a8e1175bSopenharmony_ci#endif
1788a8e1175bSopenharmony_ci    return 0;
1789a8e1175bSopenharmony_ci}
1790a8e1175bSopenharmony_ci
1791a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_PROTO_DTLS)
1792a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL
1793a8e1175bSopenharmony_ciint mbedtls_ssl_resend_hello_request(mbedtls_ssl_context *ssl);
1794a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_PROTO_DTLS */
1795a8e1175bSopenharmony_ci
1796a8e1175bSopenharmony_civoid mbedtls_ssl_set_timer(mbedtls_ssl_context *ssl, uint32_t millisecs);
1797a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL
1798a8e1175bSopenharmony_ciint mbedtls_ssl_check_timer(mbedtls_ssl_context *ssl);
1799a8e1175bSopenharmony_ci
1800a8e1175bSopenharmony_civoid mbedtls_ssl_reset_in_out_pointers(mbedtls_ssl_context *ssl);
1801a8e1175bSopenharmony_civoid mbedtls_ssl_update_out_pointers(mbedtls_ssl_context *ssl,
1802a8e1175bSopenharmony_ci                                     mbedtls_ssl_transform *transform);
1803a8e1175bSopenharmony_civoid mbedtls_ssl_update_in_pointers(mbedtls_ssl_context *ssl);
1804a8e1175bSopenharmony_ci
1805a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL
1806a8e1175bSopenharmony_ciint mbedtls_ssl_session_reset_int(mbedtls_ssl_context *ssl, int partial);
1807a8e1175bSopenharmony_civoid mbedtls_ssl_session_reset_msg_layer(mbedtls_ssl_context *ssl,
1808a8e1175bSopenharmony_ci                                         int partial);
1809a8e1175bSopenharmony_ci
1810a8e1175bSopenharmony_ci/*
1811a8e1175bSopenharmony_ci * Send pending alert
1812a8e1175bSopenharmony_ci */
1813a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL
1814a8e1175bSopenharmony_ciint mbedtls_ssl_handle_pending_alert(mbedtls_ssl_context *ssl);
1815a8e1175bSopenharmony_ci
1816a8e1175bSopenharmony_ci/*
1817a8e1175bSopenharmony_ci * Set pending fatal alert flag.
1818a8e1175bSopenharmony_ci */
1819a8e1175bSopenharmony_civoid mbedtls_ssl_pend_fatal_alert(mbedtls_ssl_context *ssl,
1820a8e1175bSopenharmony_ci                                  unsigned char alert_type,
1821a8e1175bSopenharmony_ci                                  int alert_reason);
1822a8e1175bSopenharmony_ci
1823a8e1175bSopenharmony_ci/* Alias of mbedtls_ssl_pend_fatal_alert */
1824a8e1175bSopenharmony_ci#define MBEDTLS_SSL_PEND_FATAL_ALERT(type, user_return_value)         \
1825a8e1175bSopenharmony_ci    mbedtls_ssl_pend_fatal_alert(ssl, type, user_return_value)
1826a8e1175bSopenharmony_ci
1827a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
1828a8e1175bSopenharmony_civoid mbedtls_ssl_dtls_replay_reset(mbedtls_ssl_context *ssl);
1829a8e1175bSopenharmony_ci#endif
1830a8e1175bSopenharmony_ci
1831a8e1175bSopenharmony_civoid mbedtls_ssl_handshake_wrapup_free_hs_transform(mbedtls_ssl_context *ssl);
1832a8e1175bSopenharmony_ci
1833a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_RENEGOTIATION)
1834a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL
1835a8e1175bSopenharmony_ciint mbedtls_ssl_start_renegotiation(mbedtls_ssl_context *ssl);
1836a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_RENEGOTIATION */
1837a8e1175bSopenharmony_ci
1838a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_PROTO_DTLS)
1839a8e1175bSopenharmony_cisize_t mbedtls_ssl_get_current_mtu(const mbedtls_ssl_context *ssl);
1840a8e1175bSopenharmony_civoid mbedtls_ssl_buffering_free(mbedtls_ssl_context *ssl);
1841a8e1175bSopenharmony_civoid mbedtls_ssl_flight_free(mbedtls_ssl_flight_item *flight);
1842a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_PROTO_DTLS */
1843a8e1175bSopenharmony_ci
1844a8e1175bSopenharmony_ci/**
1845a8e1175bSopenharmony_ci * ssl utils functions for checking configuration.
1846a8e1175bSopenharmony_ci */
1847a8e1175bSopenharmony_ci
1848a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1849a8e1175bSopenharmony_cistatic inline int mbedtls_ssl_conf_is_tls13_only(const mbedtls_ssl_config *conf)
1850a8e1175bSopenharmony_ci{
1851a8e1175bSopenharmony_ci    return conf->MBEDTLS_PRIVATE(min_tls_version) == MBEDTLS_SSL_VERSION_TLS1_3 &&
1852a8e1175bSopenharmony_ci           conf->MBEDTLS_PRIVATE(max_tls_version) == MBEDTLS_SSL_VERSION_TLS1_3;
1853a8e1175bSopenharmony_ci}
1854a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1855a8e1175bSopenharmony_ci
1856a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1857a8e1175bSopenharmony_cistatic inline int mbedtls_ssl_conf_is_tls12_only(const mbedtls_ssl_config *conf)
1858a8e1175bSopenharmony_ci{
1859a8e1175bSopenharmony_ci    return conf->MBEDTLS_PRIVATE(min_tls_version) == MBEDTLS_SSL_VERSION_TLS1_2 &&
1860a8e1175bSopenharmony_ci           conf->MBEDTLS_PRIVATE(max_tls_version) == MBEDTLS_SSL_VERSION_TLS1_2;
1861a8e1175bSopenharmony_ci}
1862a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1863a8e1175bSopenharmony_ci
1864a8e1175bSopenharmony_cistatic inline int mbedtls_ssl_conf_is_tls13_enabled(const mbedtls_ssl_config *conf)
1865a8e1175bSopenharmony_ci{
1866a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1867a8e1175bSopenharmony_ci    return conf->MBEDTLS_PRIVATE(min_tls_version) <= MBEDTLS_SSL_VERSION_TLS1_3 &&
1868a8e1175bSopenharmony_ci           conf->MBEDTLS_PRIVATE(max_tls_version) >= MBEDTLS_SSL_VERSION_TLS1_3;
1869a8e1175bSopenharmony_ci#else
1870a8e1175bSopenharmony_ci    ((void) conf);
1871a8e1175bSopenharmony_ci    return 0;
1872a8e1175bSopenharmony_ci#endif
1873a8e1175bSopenharmony_ci}
1874a8e1175bSopenharmony_ci
1875a8e1175bSopenharmony_cistatic inline int mbedtls_ssl_conf_is_tls12_enabled(const mbedtls_ssl_config *conf)
1876a8e1175bSopenharmony_ci{
1877a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1878a8e1175bSopenharmony_ci    return conf->MBEDTLS_PRIVATE(min_tls_version) <= MBEDTLS_SSL_VERSION_TLS1_2 &&
1879a8e1175bSopenharmony_ci           conf->MBEDTLS_PRIVATE(max_tls_version) >= MBEDTLS_SSL_VERSION_TLS1_2;
1880a8e1175bSopenharmony_ci#else
1881a8e1175bSopenharmony_ci    ((void) conf);
1882a8e1175bSopenharmony_ci    return 0;
1883a8e1175bSopenharmony_ci#endif
1884a8e1175bSopenharmony_ci}
1885a8e1175bSopenharmony_ci
1886a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
1887a8e1175bSopenharmony_cistatic inline int mbedtls_ssl_conf_is_hybrid_tls12_tls13(const mbedtls_ssl_config *conf)
1888a8e1175bSopenharmony_ci{
1889a8e1175bSopenharmony_ci    return conf->MBEDTLS_PRIVATE(min_tls_version) == MBEDTLS_SSL_VERSION_TLS1_2 &&
1890a8e1175bSopenharmony_ci           conf->MBEDTLS_PRIVATE(max_tls_version) == MBEDTLS_SSL_VERSION_TLS1_3;
1891a8e1175bSopenharmony_ci}
1892a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_TLS1_3 */
1893a8e1175bSopenharmony_ci
1894a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1895a8e1175bSopenharmony_ciextern const uint8_t mbedtls_ssl_tls13_hello_retry_request_magic[
1896a8e1175bSopenharmony_ci    MBEDTLS_SERVER_HELLO_RANDOM_LEN];
1897a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL
1898a8e1175bSopenharmony_ciint mbedtls_ssl_tls13_process_finished_message(mbedtls_ssl_context *ssl);
1899a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL
1900a8e1175bSopenharmony_ciint mbedtls_ssl_tls13_write_finished_message(mbedtls_ssl_context *ssl);
1901a8e1175bSopenharmony_civoid mbedtls_ssl_tls13_handshake_wrapup(mbedtls_ssl_context *ssl);
1902a8e1175bSopenharmony_ci
1903a8e1175bSopenharmony_ci/**
1904a8e1175bSopenharmony_ci * \brief Given an SSL context and its associated configuration, write the TLS
1905a8e1175bSopenharmony_ci *        1.3 specific extensions of the ClientHello message.
1906a8e1175bSopenharmony_ci *
1907a8e1175bSopenharmony_ci * \param[in]   ssl     SSL context
1908a8e1175bSopenharmony_ci * \param[in]   buf     Base address of the buffer where to write the extensions
1909a8e1175bSopenharmony_ci * \param[in]   end     End address of the buffer where to write the extensions
1910a8e1175bSopenharmony_ci * \param[out]  out_len Length of the data written into the buffer \p buf
1911a8e1175bSopenharmony_ci */
1912a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL
1913a8e1175bSopenharmony_ciint mbedtls_ssl_tls13_write_client_hello_exts(mbedtls_ssl_context *ssl,
1914a8e1175bSopenharmony_ci                                              unsigned char *buf,
1915a8e1175bSopenharmony_ci                                              unsigned char *end,
1916a8e1175bSopenharmony_ci                                              size_t *out_len);
1917a8e1175bSopenharmony_ci
1918a8e1175bSopenharmony_ci/**
1919a8e1175bSopenharmony_ci * \brief           TLS 1.3 client side state machine entry
1920a8e1175bSopenharmony_ci *
1921a8e1175bSopenharmony_ci * \param ssl       SSL context
1922a8e1175bSopenharmony_ci */
1923a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL
1924a8e1175bSopenharmony_ciint mbedtls_ssl_tls13_handshake_client_step(mbedtls_ssl_context *ssl);
1925a8e1175bSopenharmony_ci
1926a8e1175bSopenharmony_ci/**
1927a8e1175bSopenharmony_ci * \brief           TLS 1.3 server side state machine entry
1928a8e1175bSopenharmony_ci *
1929a8e1175bSopenharmony_ci * \param ssl       SSL context
1930a8e1175bSopenharmony_ci */
1931a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL
1932a8e1175bSopenharmony_ciint mbedtls_ssl_tls13_handshake_server_step(mbedtls_ssl_context *ssl);
1933a8e1175bSopenharmony_ci
1934a8e1175bSopenharmony_ci
1935a8e1175bSopenharmony_ci/*
1936a8e1175bSopenharmony_ci * Helper functions around key exchange modes.
1937a8e1175bSopenharmony_ci */
1938a8e1175bSopenharmony_cistatic inline int mbedtls_ssl_conf_tls13_is_kex_mode_enabled(mbedtls_ssl_context *ssl,
1939a8e1175bSopenharmony_ci                                                             int kex_mode_mask)
1940a8e1175bSopenharmony_ci{
1941a8e1175bSopenharmony_ci    return (ssl->conf->tls13_kex_modes & kex_mode_mask) != 0;
1942a8e1175bSopenharmony_ci}
1943a8e1175bSopenharmony_ci
1944a8e1175bSopenharmony_cistatic inline int mbedtls_ssl_conf_tls13_is_psk_enabled(mbedtls_ssl_context *ssl)
1945a8e1175bSopenharmony_ci{
1946a8e1175bSopenharmony_ci    return mbedtls_ssl_conf_tls13_is_kex_mode_enabled(ssl,
1947a8e1175bSopenharmony_ci                                                      MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK);
1948a8e1175bSopenharmony_ci}
1949a8e1175bSopenharmony_ci
1950a8e1175bSopenharmony_cistatic inline int mbedtls_ssl_conf_tls13_is_psk_ephemeral_enabled(mbedtls_ssl_context *ssl)
1951a8e1175bSopenharmony_ci{
1952a8e1175bSopenharmony_ci    return mbedtls_ssl_conf_tls13_is_kex_mode_enabled(ssl,
1953a8e1175bSopenharmony_ci                                                      MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL);
1954a8e1175bSopenharmony_ci}
1955a8e1175bSopenharmony_ci
1956a8e1175bSopenharmony_cistatic inline int mbedtls_ssl_conf_tls13_is_ephemeral_enabled(mbedtls_ssl_context *ssl)
1957a8e1175bSopenharmony_ci{
1958a8e1175bSopenharmony_ci    return mbedtls_ssl_conf_tls13_is_kex_mode_enabled(ssl,
1959a8e1175bSopenharmony_ci                                                      MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL);
1960a8e1175bSopenharmony_ci}
1961a8e1175bSopenharmony_ci
1962a8e1175bSopenharmony_cistatic inline int mbedtls_ssl_conf_tls13_is_some_ephemeral_enabled(mbedtls_ssl_context *ssl)
1963a8e1175bSopenharmony_ci{
1964a8e1175bSopenharmony_ci    return mbedtls_ssl_conf_tls13_is_kex_mode_enabled(ssl,
1965a8e1175bSopenharmony_ci                                                      MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ALL);
1966a8e1175bSopenharmony_ci}
1967a8e1175bSopenharmony_ci
1968a8e1175bSopenharmony_cistatic inline int mbedtls_ssl_conf_tls13_is_some_psk_enabled(mbedtls_ssl_context *ssl)
1969a8e1175bSopenharmony_ci{
1970a8e1175bSopenharmony_ci    return mbedtls_ssl_conf_tls13_is_kex_mode_enabled(ssl,
1971a8e1175bSopenharmony_ci                                                      MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL);
1972a8e1175bSopenharmony_ci}
1973a8e1175bSopenharmony_ci
1974a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_SRV_C) && \
1975a8e1175bSopenharmony_ci    defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
1976a8e1175bSopenharmony_ci/**
1977a8e1175bSopenharmony_ci * Given a list of key exchange modes, check if at least one of them is
1978a8e1175bSopenharmony_ci * supported by peer.
1979a8e1175bSopenharmony_ci *
1980a8e1175bSopenharmony_ci * \param[in] ssl  SSL context
1981a8e1175bSopenharmony_ci * \param kex_modes_mask  Mask of the key exchange modes to check
1982a8e1175bSopenharmony_ci *
1983a8e1175bSopenharmony_ci * \return Non-zero if at least one of the key exchange modes is supported by
1984a8e1175bSopenharmony_ci *         the peer, otherwise \c 0.
1985a8e1175bSopenharmony_ci */
1986a8e1175bSopenharmony_cistatic inline int mbedtls_ssl_tls13_is_kex_mode_supported(mbedtls_ssl_context *ssl,
1987a8e1175bSopenharmony_ci                                                          int kex_modes_mask)
1988a8e1175bSopenharmony_ci{
1989a8e1175bSopenharmony_ci    return (ssl->handshake->tls13_kex_modes & kex_modes_mask) != 0;
1990a8e1175bSopenharmony_ci}
1991a8e1175bSopenharmony_ci
1992a8e1175bSopenharmony_cistatic inline int mbedtls_ssl_tls13_is_psk_supported(mbedtls_ssl_context *ssl)
1993a8e1175bSopenharmony_ci{
1994a8e1175bSopenharmony_ci    return mbedtls_ssl_tls13_is_kex_mode_supported(ssl,
1995a8e1175bSopenharmony_ci                                                   MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK);
1996a8e1175bSopenharmony_ci}
1997a8e1175bSopenharmony_ci
1998a8e1175bSopenharmony_cistatic inline int mbedtls_ssl_tls13_is_psk_ephemeral_supported(
1999a8e1175bSopenharmony_ci    mbedtls_ssl_context *ssl)
2000a8e1175bSopenharmony_ci{
2001a8e1175bSopenharmony_ci    return mbedtls_ssl_tls13_is_kex_mode_supported(ssl,
2002a8e1175bSopenharmony_ci                                                   MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL);
2003a8e1175bSopenharmony_ci}
2004a8e1175bSopenharmony_ci
2005a8e1175bSopenharmony_cistatic inline int mbedtls_ssl_tls13_is_ephemeral_supported(mbedtls_ssl_context *ssl)
2006a8e1175bSopenharmony_ci{
2007a8e1175bSopenharmony_ci    return mbedtls_ssl_tls13_is_kex_mode_supported(ssl,
2008a8e1175bSopenharmony_ci                                                   MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL);
2009a8e1175bSopenharmony_ci}
2010a8e1175bSopenharmony_ci
2011a8e1175bSopenharmony_cistatic inline int mbedtls_ssl_tls13_is_some_ephemeral_supported(mbedtls_ssl_context *ssl)
2012a8e1175bSopenharmony_ci{
2013a8e1175bSopenharmony_ci    return mbedtls_ssl_tls13_is_kex_mode_supported(ssl,
2014a8e1175bSopenharmony_ci                                                   MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ALL);
2015a8e1175bSopenharmony_ci}
2016a8e1175bSopenharmony_ci
2017a8e1175bSopenharmony_cistatic inline int mbedtls_ssl_tls13_is_some_psk_supported(mbedtls_ssl_context *ssl)
2018a8e1175bSopenharmony_ci{
2019a8e1175bSopenharmony_ci    return mbedtls_ssl_tls13_is_kex_mode_supported(ssl,
2020a8e1175bSopenharmony_ci                                                   MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL);
2021a8e1175bSopenharmony_ci}
2022a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_SRV_C &&
2023a8e1175bSopenharmony_ci          MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
2024a8e1175bSopenharmony_ci
2025a8e1175bSopenharmony_ci/*
2026a8e1175bSopenharmony_ci * Helper functions for extensions checking.
2027a8e1175bSopenharmony_ci */
2028a8e1175bSopenharmony_ci
2029a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL
2030a8e1175bSopenharmony_ciint mbedtls_ssl_tls13_check_received_extension(
2031a8e1175bSopenharmony_ci    mbedtls_ssl_context *ssl,
2032a8e1175bSopenharmony_ci    int hs_msg_type,
2033a8e1175bSopenharmony_ci    unsigned int received_extension_type,
2034a8e1175bSopenharmony_ci    uint32_t hs_msg_allowed_extensions_mask);
2035a8e1175bSopenharmony_ci
2036a8e1175bSopenharmony_cistatic inline void mbedtls_ssl_tls13_set_hs_sent_ext_mask(
2037a8e1175bSopenharmony_ci    mbedtls_ssl_context *ssl, unsigned int extension_type)
2038a8e1175bSopenharmony_ci{
2039a8e1175bSopenharmony_ci    ssl->handshake->sent_extensions |=
2040a8e1175bSopenharmony_ci        mbedtls_ssl_get_extension_mask(extension_type);
2041a8e1175bSopenharmony_ci}
2042a8e1175bSopenharmony_ci
2043a8e1175bSopenharmony_ci/*
2044a8e1175bSopenharmony_ci * Helper functions to check the selected key exchange mode.
2045a8e1175bSopenharmony_ci */
2046a8e1175bSopenharmony_cistatic inline int mbedtls_ssl_tls13_key_exchange_mode_check(
2047a8e1175bSopenharmony_ci    mbedtls_ssl_context *ssl, int kex_mask)
2048a8e1175bSopenharmony_ci{
2049a8e1175bSopenharmony_ci    return (ssl->handshake->key_exchange_mode & kex_mask) != 0;
2050a8e1175bSopenharmony_ci}
2051a8e1175bSopenharmony_ci
2052a8e1175bSopenharmony_cistatic inline int mbedtls_ssl_tls13_key_exchange_mode_with_psk(
2053a8e1175bSopenharmony_ci    mbedtls_ssl_context *ssl)
2054a8e1175bSopenharmony_ci{
2055a8e1175bSopenharmony_ci    return mbedtls_ssl_tls13_key_exchange_mode_check(ssl,
2056a8e1175bSopenharmony_ci                                                     MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL);
2057a8e1175bSopenharmony_ci}
2058a8e1175bSopenharmony_ci
2059a8e1175bSopenharmony_cistatic inline int mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(
2060a8e1175bSopenharmony_ci    mbedtls_ssl_context *ssl)
2061a8e1175bSopenharmony_ci{
2062a8e1175bSopenharmony_ci    return mbedtls_ssl_tls13_key_exchange_mode_check(ssl,
2063a8e1175bSopenharmony_ci                                                     MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ALL);
2064a8e1175bSopenharmony_ci}
2065a8e1175bSopenharmony_ci
2066a8e1175bSopenharmony_ci/*
2067a8e1175bSopenharmony_ci * Fetch TLS 1.3 handshake message header
2068a8e1175bSopenharmony_ci */
2069a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL
2070a8e1175bSopenharmony_ciint mbedtls_ssl_tls13_fetch_handshake_msg(mbedtls_ssl_context *ssl,
2071a8e1175bSopenharmony_ci                                          unsigned hs_type,
2072a8e1175bSopenharmony_ci                                          unsigned char **buf,
2073a8e1175bSopenharmony_ci                                          size_t *buf_len);
2074a8e1175bSopenharmony_ci
2075a8e1175bSopenharmony_ci/**
2076a8e1175bSopenharmony_ci * \brief Detect if a list of extensions contains a supported_versions
2077a8e1175bSopenharmony_ci *        extension or not.
2078a8e1175bSopenharmony_ci *
2079a8e1175bSopenharmony_ci * \param[in] ssl  SSL context
2080a8e1175bSopenharmony_ci * \param[in] buf  Address of the first byte of the extensions vector.
2081a8e1175bSopenharmony_ci * \param[in] end  End of the buffer containing the list of extensions.
2082a8e1175bSopenharmony_ci * \param[out] supported_versions_data  If the extension is present, address of
2083a8e1175bSopenharmony_ci *                                      its first byte of data, NULL otherwise.
2084a8e1175bSopenharmony_ci * \param[out] supported_versions_data_end  If the extension is present, address
2085a8e1175bSopenharmony_ci *                                          of the first byte immediately
2086a8e1175bSopenharmony_ci *                                          following the extension data, NULL
2087a8e1175bSopenharmony_ci *                                          otherwise.
2088a8e1175bSopenharmony_ci * \return 0  if the list of extensions does not contain a supported_versions
2089a8e1175bSopenharmony_ci *            extension.
2090a8e1175bSopenharmony_ci * \return 1  if the list of extensions contains a supported_versions
2091a8e1175bSopenharmony_ci *            extension.
2092a8e1175bSopenharmony_ci * \return    A negative value if an error occurred while parsing the
2093a8e1175bSopenharmony_ci *            extensions.
2094a8e1175bSopenharmony_ci */
2095a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL
2096a8e1175bSopenharmony_ciint mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts(
2097a8e1175bSopenharmony_ci    mbedtls_ssl_context *ssl,
2098a8e1175bSopenharmony_ci    const unsigned char *buf, const unsigned char *end,
2099a8e1175bSopenharmony_ci    const unsigned char **supported_versions_data,
2100a8e1175bSopenharmony_ci    const unsigned char **supported_versions_data_end);
2101a8e1175bSopenharmony_ci
2102a8e1175bSopenharmony_ci/*
2103a8e1175bSopenharmony_ci * Handler of TLS 1.3 server certificate message
2104a8e1175bSopenharmony_ci */
2105a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL
2106a8e1175bSopenharmony_ciint mbedtls_ssl_tls13_process_certificate(mbedtls_ssl_context *ssl);
2107a8e1175bSopenharmony_ci
2108a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
2109a8e1175bSopenharmony_ci/*
2110a8e1175bSopenharmony_ci * Handler of TLS 1.3 write Certificate message
2111a8e1175bSopenharmony_ci */
2112a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL
2113a8e1175bSopenharmony_ciint mbedtls_ssl_tls13_write_certificate(mbedtls_ssl_context *ssl);
2114a8e1175bSopenharmony_ci
2115a8e1175bSopenharmony_ci/*
2116a8e1175bSopenharmony_ci * Handler of TLS 1.3 write Certificate Verify message
2117a8e1175bSopenharmony_ci */
2118a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL
2119a8e1175bSopenharmony_ciint mbedtls_ssl_tls13_write_certificate_verify(mbedtls_ssl_context *ssl);
2120a8e1175bSopenharmony_ci
2121a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
2122a8e1175bSopenharmony_ci
2123a8e1175bSopenharmony_ci/*
2124a8e1175bSopenharmony_ci * Generic handler of Certificate Verify
2125a8e1175bSopenharmony_ci */
2126a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL
2127a8e1175bSopenharmony_ciint mbedtls_ssl_tls13_process_certificate_verify(mbedtls_ssl_context *ssl);
2128a8e1175bSopenharmony_ci
2129a8e1175bSopenharmony_ci/*
2130a8e1175bSopenharmony_ci * Write of dummy-CCS's for middlebox compatibility
2131a8e1175bSopenharmony_ci */
2132a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL
2133a8e1175bSopenharmony_ciint mbedtls_ssl_tls13_write_change_cipher_spec(mbedtls_ssl_context *ssl);
2134a8e1175bSopenharmony_ci
2135a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL
2136a8e1175bSopenharmony_ciint mbedtls_ssl_reset_transcript_for_hrr(mbedtls_ssl_context *ssl);
2137a8e1175bSopenharmony_ci
2138a8e1175bSopenharmony_ci#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
2139a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL
2140a8e1175bSopenharmony_ciint mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange(
2141a8e1175bSopenharmony_ci    mbedtls_ssl_context *ssl,
2142a8e1175bSopenharmony_ci    uint16_t named_group,
2143a8e1175bSopenharmony_ci    unsigned char *buf,
2144a8e1175bSopenharmony_ci    unsigned char *end,
2145a8e1175bSopenharmony_ci    size_t *out_len);
2146a8e1175bSopenharmony_ci#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH */
2147a8e1175bSopenharmony_ci
2148a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_EARLY_DATA)
2149a8e1175bSopenharmony_ciint mbedtls_ssl_tls13_write_early_data_ext(mbedtls_ssl_context *ssl,
2150a8e1175bSopenharmony_ci                                           int in_new_session_ticket,
2151a8e1175bSopenharmony_ci                                           unsigned char *buf,
2152a8e1175bSopenharmony_ci                                           const unsigned char *end,
2153a8e1175bSopenharmony_ci                                           size_t *out_len);
2154a8e1175bSopenharmony_ci
2155a8e1175bSopenharmony_ciint mbedtls_ssl_tls13_check_early_data_len(mbedtls_ssl_context *ssl,
2156a8e1175bSopenharmony_ci                                           size_t early_data_len);
2157a8e1175bSopenharmony_ci
2158a8e1175bSopenharmony_citypedef enum {
2159a8e1175bSopenharmony_ci/*
2160a8e1175bSopenharmony_ci * The client has not sent the first ClientHello yet, the negotiation of early
2161a8e1175bSopenharmony_ci * data has not started yet.
2162a8e1175bSopenharmony_ci */
2163a8e1175bSopenharmony_ci    MBEDTLS_SSL_EARLY_DATA_STATE_IDLE,
2164a8e1175bSopenharmony_ci
2165a8e1175bSopenharmony_ci/*
2166a8e1175bSopenharmony_ci * In its ClientHello, the client has not included an early data indication
2167a8e1175bSopenharmony_ci * extension.
2168a8e1175bSopenharmony_ci */
2169a8e1175bSopenharmony_ci    MBEDTLS_SSL_EARLY_DATA_STATE_NO_IND_SENT,
2170a8e1175bSopenharmony_ci
2171a8e1175bSopenharmony_ci/*
2172a8e1175bSopenharmony_ci * The client has sent an early data indication extension in its first
2173a8e1175bSopenharmony_ci * ClientHello, it has not received the response (ServerHello or
2174a8e1175bSopenharmony_ci * HelloRetryRequest) from the server yet. The transform to protect early data
2175a8e1175bSopenharmony_ci * is not set either as for middlebox compatibility a dummy CCS may have to be
2176a8e1175bSopenharmony_ci * sent in clear. Early data cannot be sent to the server yet.
2177a8e1175bSopenharmony_ci */
2178a8e1175bSopenharmony_ci    MBEDTLS_SSL_EARLY_DATA_STATE_IND_SENT,
2179a8e1175bSopenharmony_ci
2180a8e1175bSopenharmony_ci/*
2181a8e1175bSopenharmony_ci * The client has sent an early data indication extension in its first
2182a8e1175bSopenharmony_ci * ClientHello, it has not received the response (ServerHello or
2183a8e1175bSopenharmony_ci * HelloRetryRequest) from the server yet. The transform to protect early data
2184a8e1175bSopenharmony_ci * has been set and early data can be written now.
2185a8e1175bSopenharmony_ci */
2186a8e1175bSopenharmony_ci    MBEDTLS_SSL_EARLY_DATA_STATE_CAN_WRITE,
2187a8e1175bSopenharmony_ci
2188a8e1175bSopenharmony_ci/*
2189a8e1175bSopenharmony_ci * The client has indicated the use of early data and the server has accepted
2190a8e1175bSopenharmony_ci * it.
2191a8e1175bSopenharmony_ci */
2192a8e1175bSopenharmony_ci    MBEDTLS_SSL_EARLY_DATA_STATE_ACCEPTED,
2193a8e1175bSopenharmony_ci
2194a8e1175bSopenharmony_ci/*
2195a8e1175bSopenharmony_ci * The client has indicated the use of early data but the server has rejected
2196a8e1175bSopenharmony_ci * it.
2197a8e1175bSopenharmony_ci */
2198a8e1175bSopenharmony_ci    MBEDTLS_SSL_EARLY_DATA_STATE_REJECTED,
2199a8e1175bSopenharmony_ci
2200a8e1175bSopenharmony_ci/*
2201a8e1175bSopenharmony_ci * The client has sent an early data indication extension in its first
2202a8e1175bSopenharmony_ci * ClientHello, the server has accepted them and the client has received the
2203a8e1175bSopenharmony_ci * server Finished message. It cannot send early data to the server anymore.
2204a8e1175bSopenharmony_ci */
2205a8e1175bSopenharmony_ci    MBEDTLS_SSL_EARLY_DATA_STATE_SERVER_FINISHED_RECEIVED,
2206a8e1175bSopenharmony_ci
2207a8e1175bSopenharmony_ci} mbedtls_ssl_early_data_state;
2208a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_EARLY_DATA */
2209a8e1175bSopenharmony_ci
2210a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2211a8e1175bSopenharmony_ci
2212a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
2213a8e1175bSopenharmony_ci/*
2214a8e1175bSopenharmony_ci * Write Signature Algorithm extension
2215a8e1175bSopenharmony_ci */
2216a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL
2217a8e1175bSopenharmony_ciint mbedtls_ssl_write_sig_alg_ext(mbedtls_ssl_context *ssl, unsigned char *buf,
2218a8e1175bSopenharmony_ci                                  const unsigned char *end, size_t *out_len);
2219a8e1175bSopenharmony_ci/*
2220a8e1175bSopenharmony_ci * Parse TLS Signature Algorithm extension
2221a8e1175bSopenharmony_ci */
2222a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL
2223a8e1175bSopenharmony_ciint mbedtls_ssl_parse_sig_alg_ext(mbedtls_ssl_context *ssl,
2224a8e1175bSopenharmony_ci                                  const unsigned char *buf,
2225a8e1175bSopenharmony_ci                                  const unsigned char *end);
2226a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
2227a8e1175bSopenharmony_ci
2228a8e1175bSopenharmony_ci/* Get handshake transcript */
2229a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL
2230a8e1175bSopenharmony_ciint mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl,
2231a8e1175bSopenharmony_ci                                         const mbedtls_md_type_t md,
2232a8e1175bSopenharmony_ci                                         unsigned char *dst,
2233a8e1175bSopenharmony_ci                                         size_t dst_len,
2234a8e1175bSopenharmony_ci                                         size_t *olen);
2235a8e1175bSopenharmony_ci
2236a8e1175bSopenharmony_ci/*
2237a8e1175bSopenharmony_ci * Return supported groups.
2238a8e1175bSopenharmony_ci *
2239a8e1175bSopenharmony_ci * In future, invocations can be changed to ssl->conf->group_list
2240a8e1175bSopenharmony_ci * when mbedtls_ssl_conf_curves() is deleted.
2241a8e1175bSopenharmony_ci *
2242a8e1175bSopenharmony_ci * ssl->handshake->group_list is either a translation of curve_list to IANA TLS group
2243a8e1175bSopenharmony_ci * identifiers when mbedtls_ssl_conf_curves() has been used, or a pointer to
2244a8e1175bSopenharmony_ci * ssl->conf->group_list when mbedtls_ssl_conf_groups() has been more recently invoked.
2245a8e1175bSopenharmony_ci *
2246a8e1175bSopenharmony_ci */
2247a8e1175bSopenharmony_cistatic inline const void *mbedtls_ssl_get_groups(const mbedtls_ssl_context *ssl)
2248a8e1175bSopenharmony_ci{
2249a8e1175bSopenharmony_ci    #if defined(MBEDTLS_DEPRECATED_REMOVED) || !defined(MBEDTLS_ECP_C)
2250a8e1175bSopenharmony_ci    return ssl->MBEDTLS_PRIVATE(conf)->MBEDTLS_PRIVATE(group_list);
2251a8e1175bSopenharmony_ci    #else
2252a8e1175bSopenharmony_ci    if ((ssl->MBEDTLS_PRIVATE(handshake) != NULL) && (ssl->MBEDTLS_PRIVATE(handshake)->group_list != NULL)) {
2253a8e1175bSopenharmony_ci        return ssl->MBEDTLS_PRIVATE(handshake)->group_list;
2254a8e1175bSopenharmony_ci    } else {
2255a8e1175bSopenharmony_ci        return ssl->MBEDTLS_PRIVATE(conf)->MBEDTLS_PRIVATE(group_list);
2256a8e1175bSopenharmony_ci    }
2257a8e1175bSopenharmony_ci    #endif
2258a8e1175bSopenharmony_ci}
2259a8e1175bSopenharmony_ci
2260a8e1175bSopenharmony_ci/*
2261a8e1175bSopenharmony_ci * Helper functions for NamedGroup.
2262a8e1175bSopenharmony_ci */
2263a8e1175bSopenharmony_cistatic inline int mbedtls_ssl_tls12_named_group_is_ecdhe(uint16_t named_group)
2264a8e1175bSopenharmony_ci{
2265a8e1175bSopenharmony_ci    /*
2266a8e1175bSopenharmony_ci     * RFC 8422 section 5.1.1
2267a8e1175bSopenharmony_ci     */
2268a8e1175bSopenharmony_ci    return named_group == MBEDTLS_SSL_IANA_TLS_GROUP_X25519    ||
2269a8e1175bSopenharmony_ci           named_group == MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1   ||
2270a8e1175bSopenharmony_ci           named_group == MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1   ||
2271a8e1175bSopenharmony_ci           named_group == MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1   ||
2272a8e1175bSopenharmony_ci           named_group == MBEDTLS_SSL_IANA_TLS_GROUP_X448      ||
2273a8e1175bSopenharmony_ci           /* Below deprecated curves should be removed with notice to users */
2274a8e1175bSopenharmony_ci           named_group == MBEDTLS_SSL_IANA_TLS_GROUP_SECP192K1 ||
2275a8e1175bSopenharmony_ci           named_group == MBEDTLS_SSL_IANA_TLS_GROUP_SECP192R1 ||
2276a8e1175bSopenharmony_ci           named_group == MBEDTLS_SSL_IANA_TLS_GROUP_SECP224K1 ||
2277a8e1175bSopenharmony_ci           named_group == MBEDTLS_SSL_IANA_TLS_GROUP_SECP224R1 ||
2278a8e1175bSopenharmony_ci           named_group == MBEDTLS_SSL_IANA_TLS_GROUP_SECP256K1 ||
2279a8e1175bSopenharmony_ci           named_group == MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1 ||
2280a8e1175bSopenharmony_ci           named_group == MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1 ||
2281a8e1175bSopenharmony_ci           named_group == MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1;
2282a8e1175bSopenharmony_ci}
2283a8e1175bSopenharmony_ci
2284a8e1175bSopenharmony_cistatic inline int mbedtls_ssl_tls13_named_group_is_ecdhe(uint16_t named_group)
2285a8e1175bSopenharmony_ci{
2286a8e1175bSopenharmony_ci    return named_group == MBEDTLS_SSL_IANA_TLS_GROUP_X25519    ||
2287a8e1175bSopenharmony_ci           named_group == MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1 ||
2288a8e1175bSopenharmony_ci           named_group == MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1 ||
2289a8e1175bSopenharmony_ci           named_group == MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1 ||
2290a8e1175bSopenharmony_ci           named_group == MBEDTLS_SSL_IANA_TLS_GROUP_X448;
2291a8e1175bSopenharmony_ci}
2292a8e1175bSopenharmony_ci
2293a8e1175bSopenharmony_cistatic inline int mbedtls_ssl_tls13_named_group_is_ffdh(uint16_t named_group)
2294a8e1175bSopenharmony_ci{
2295a8e1175bSopenharmony_ci    return named_group >= MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE2048 &&
2296a8e1175bSopenharmony_ci           named_group <= MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE8192;
2297a8e1175bSopenharmony_ci}
2298a8e1175bSopenharmony_ci
2299a8e1175bSopenharmony_cistatic inline int mbedtls_ssl_named_group_is_offered(
2300a8e1175bSopenharmony_ci    const mbedtls_ssl_context *ssl, uint16_t named_group)
2301a8e1175bSopenharmony_ci{
2302a8e1175bSopenharmony_ci    const uint16_t *group_list = mbedtls_ssl_get_groups(ssl);
2303a8e1175bSopenharmony_ci
2304a8e1175bSopenharmony_ci    if (group_list == NULL) {
2305a8e1175bSopenharmony_ci        return 0;
2306a8e1175bSopenharmony_ci    }
2307a8e1175bSopenharmony_ci
2308a8e1175bSopenharmony_ci    for (; *group_list != 0; group_list++) {
2309a8e1175bSopenharmony_ci        if (*group_list == named_group) {
2310a8e1175bSopenharmony_ci            return 1;
2311a8e1175bSopenharmony_ci        }
2312a8e1175bSopenharmony_ci    }
2313a8e1175bSopenharmony_ci
2314a8e1175bSopenharmony_ci    return 0;
2315a8e1175bSopenharmony_ci}
2316a8e1175bSopenharmony_ci
2317a8e1175bSopenharmony_cistatic inline int mbedtls_ssl_named_group_is_supported(uint16_t named_group)
2318a8e1175bSopenharmony_ci{
2319a8e1175bSopenharmony_ci#if defined(PSA_WANT_ALG_ECDH)
2320a8e1175bSopenharmony_ci    if (mbedtls_ssl_tls13_named_group_is_ecdhe(named_group)) {
2321a8e1175bSopenharmony_ci        if (mbedtls_ssl_get_ecp_group_id_from_tls_id(named_group) !=
2322a8e1175bSopenharmony_ci            MBEDTLS_ECP_DP_NONE) {
2323a8e1175bSopenharmony_ci            return 1;
2324a8e1175bSopenharmony_ci        }
2325a8e1175bSopenharmony_ci    }
2326a8e1175bSopenharmony_ci#endif
2327a8e1175bSopenharmony_ci#if defined(PSA_WANT_ALG_FFDH)
2328a8e1175bSopenharmony_ci    if (mbedtls_ssl_tls13_named_group_is_ffdh(named_group)) {
2329a8e1175bSopenharmony_ci        return 1;
2330a8e1175bSopenharmony_ci    }
2331a8e1175bSopenharmony_ci#endif
2332a8e1175bSopenharmony_ci#if !defined(PSA_WANT_ALG_ECDH) && !defined(PSA_WANT_ALG_FFDH)
2333a8e1175bSopenharmony_ci    (void) named_group;
2334a8e1175bSopenharmony_ci#endif
2335a8e1175bSopenharmony_ci    return 0;
2336a8e1175bSopenharmony_ci}
2337a8e1175bSopenharmony_ci
2338a8e1175bSopenharmony_ci/*
2339a8e1175bSopenharmony_ci * Return supported signature algorithms.
2340a8e1175bSopenharmony_ci *
2341a8e1175bSopenharmony_ci * In future, invocations can be changed to ssl->conf->sig_algs when
2342a8e1175bSopenharmony_ci * mbedtls_ssl_conf_sig_hashes() is deleted.
2343a8e1175bSopenharmony_ci *
2344a8e1175bSopenharmony_ci * ssl->handshake->sig_algs is either a translation of sig_hashes to IANA TLS
2345a8e1175bSopenharmony_ci * signature algorithm identifiers when mbedtls_ssl_conf_sig_hashes() has been
2346a8e1175bSopenharmony_ci * used, or a pointer to ssl->conf->sig_algs when mbedtls_ssl_conf_sig_algs() has
2347a8e1175bSopenharmony_ci * been more recently invoked.
2348a8e1175bSopenharmony_ci *
2349a8e1175bSopenharmony_ci */
2350a8e1175bSopenharmony_cistatic inline const void *mbedtls_ssl_get_sig_algs(
2351a8e1175bSopenharmony_ci    const mbedtls_ssl_context *ssl)
2352a8e1175bSopenharmony_ci{
2353a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
2354a8e1175bSopenharmony_ci
2355a8e1175bSopenharmony_ci#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2356a8e1175bSopenharmony_ci    if (ssl->MBEDTLS_PRIVATE(handshake) != NULL &&
2357a8e1175bSopenharmony_ci        ssl->MBEDTLS_PRIVATE(handshake)->sig_algs_heap_allocated == 1 &&
2358a8e1175bSopenharmony_ci        ssl->MBEDTLS_PRIVATE(handshake)->sig_algs != NULL) {
2359a8e1175bSopenharmony_ci        return ssl->MBEDTLS_PRIVATE(handshake)->sig_algs;
2360a8e1175bSopenharmony_ci    }
2361a8e1175bSopenharmony_ci#endif
2362a8e1175bSopenharmony_ci    return ssl->MBEDTLS_PRIVATE(conf)->MBEDTLS_PRIVATE(sig_algs);
2363a8e1175bSopenharmony_ci
2364a8e1175bSopenharmony_ci#else /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
2365a8e1175bSopenharmony_ci
2366a8e1175bSopenharmony_ci    ((void) ssl);
2367a8e1175bSopenharmony_ci    return NULL;
2368a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
2369a8e1175bSopenharmony_ci}
2370a8e1175bSopenharmony_ci
2371a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
2372a8e1175bSopenharmony_cistatic inline int mbedtls_ssl_sig_alg_is_received(const mbedtls_ssl_context *ssl,
2373a8e1175bSopenharmony_ci                                                  uint16_t own_sig_alg)
2374a8e1175bSopenharmony_ci{
2375a8e1175bSopenharmony_ci    const uint16_t *sig_alg = ssl->MBEDTLS_PRIVATE(handshake)->MBEDTLS_PRIVATE(received_sig_algs);
2376a8e1175bSopenharmony_ci    if (sig_alg == NULL) {
2377a8e1175bSopenharmony_ci        return 0;
2378a8e1175bSopenharmony_ci    }
2379a8e1175bSopenharmony_ci
2380a8e1175bSopenharmony_ci    for (; *sig_alg != MBEDTLS_TLS_SIG_NONE; sig_alg++) {
2381a8e1175bSopenharmony_ci        if (*sig_alg == own_sig_alg) {
2382a8e1175bSopenharmony_ci            return 1;
2383a8e1175bSopenharmony_ci        }
2384a8e1175bSopenharmony_ci    }
2385a8e1175bSopenharmony_ci    return 0;
2386a8e1175bSopenharmony_ci}
2387a8e1175bSopenharmony_ci
2388a8e1175bSopenharmony_cistatic inline int mbedtls_ssl_tls13_sig_alg_for_cert_verify_is_supported(
2389a8e1175bSopenharmony_ci    const uint16_t sig_alg)
2390a8e1175bSopenharmony_ci{
2391a8e1175bSopenharmony_ci    switch (sig_alg) {
2392a8e1175bSopenharmony_ci#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
2393a8e1175bSopenharmony_ci#if defined(PSA_WANT_ALG_SHA_256) && defined(PSA_WANT_ECC_SECP_R1_256)
2394a8e1175bSopenharmony_ci        case MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256:
2395a8e1175bSopenharmony_ci            break;
2396a8e1175bSopenharmony_ci#endif /* PSA_WANT_ALG_SHA_256 && MBEDTLS_ECP_DP_SECP256R1_ENABLED */
2397a8e1175bSopenharmony_ci#if defined(PSA_WANT_ALG_SHA_384) && defined(PSA_WANT_ECC_SECP_R1_384)
2398a8e1175bSopenharmony_ci        case MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384:
2399a8e1175bSopenharmony_ci            break;
2400a8e1175bSopenharmony_ci#endif /* PSA_WANT_ALG_SHA_384 && MBEDTLS_ECP_DP_SECP384R1_ENABLED */
2401a8e1175bSopenharmony_ci#if defined(PSA_WANT_ALG_SHA_512) && defined(PSA_WANT_ECC_SECP_R1_521)
2402a8e1175bSopenharmony_ci        case MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512:
2403a8e1175bSopenharmony_ci            break;
2404a8e1175bSopenharmony_ci#endif /* PSA_WANT_ALG_SHA_512 && MBEDTLS_ECP_DP_SECP521R1_ENABLED */
2405a8e1175bSopenharmony_ci#endif /* MBEDTLS_PK_CAN_ECDSA_SOME */
2406a8e1175bSopenharmony_ci
2407a8e1175bSopenharmony_ci#if defined(MBEDTLS_PKCS1_V21)
2408a8e1175bSopenharmony_ci#if defined(PSA_WANT_ALG_SHA_256)
2409a8e1175bSopenharmony_ci        case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256:
2410a8e1175bSopenharmony_ci            break;
2411a8e1175bSopenharmony_ci#endif /* PSA_WANT_ALG_SHA_256  */
2412a8e1175bSopenharmony_ci#if defined(PSA_WANT_ALG_SHA_384)
2413a8e1175bSopenharmony_ci        case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384:
2414a8e1175bSopenharmony_ci            break;
2415a8e1175bSopenharmony_ci#endif /* PSA_WANT_ALG_SHA_384 */
2416a8e1175bSopenharmony_ci#if defined(PSA_WANT_ALG_SHA_512)
2417a8e1175bSopenharmony_ci        case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512:
2418a8e1175bSopenharmony_ci            break;
2419a8e1175bSopenharmony_ci#endif /* PSA_WANT_ALG_SHA_512 */
2420a8e1175bSopenharmony_ci#endif /* MBEDTLS_PKCS1_V21 */
2421a8e1175bSopenharmony_ci        default:
2422a8e1175bSopenharmony_ci            return 0;
2423a8e1175bSopenharmony_ci    }
2424a8e1175bSopenharmony_ci    return 1;
2425a8e1175bSopenharmony_ci
2426a8e1175bSopenharmony_ci}
2427a8e1175bSopenharmony_ci
2428a8e1175bSopenharmony_cistatic inline int mbedtls_ssl_tls13_sig_alg_is_supported(
2429a8e1175bSopenharmony_ci    const uint16_t sig_alg)
2430a8e1175bSopenharmony_ci{
2431a8e1175bSopenharmony_ci    switch (sig_alg) {
2432a8e1175bSopenharmony_ci#if defined(MBEDTLS_PKCS1_V15)
2433a8e1175bSopenharmony_ci#if defined(MBEDTLS_MD_CAN_SHA256)
2434a8e1175bSopenharmony_ci        case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256:
2435a8e1175bSopenharmony_ci            break;
2436a8e1175bSopenharmony_ci#endif /* MBEDTLS_MD_CAN_SHA256 */
2437a8e1175bSopenharmony_ci#if defined(MBEDTLS_MD_CAN_SHA384)
2438a8e1175bSopenharmony_ci        case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384:
2439a8e1175bSopenharmony_ci            break;
2440a8e1175bSopenharmony_ci#endif /* MBEDTLS_MD_CAN_SHA384 */
2441a8e1175bSopenharmony_ci#if defined(MBEDTLS_MD_CAN_SHA512)
2442a8e1175bSopenharmony_ci        case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512:
2443a8e1175bSopenharmony_ci            break;
2444a8e1175bSopenharmony_ci#endif /* MBEDTLS_MD_CAN_SHA512 */
2445a8e1175bSopenharmony_ci#endif /* MBEDTLS_PKCS1_V15 */
2446a8e1175bSopenharmony_ci        default:
2447a8e1175bSopenharmony_ci            return mbedtls_ssl_tls13_sig_alg_for_cert_verify_is_supported(
2448a8e1175bSopenharmony_ci                sig_alg);
2449a8e1175bSopenharmony_ci    }
2450a8e1175bSopenharmony_ci    return 1;
2451a8e1175bSopenharmony_ci}
2452a8e1175bSopenharmony_ci
2453a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL
2454a8e1175bSopenharmony_ciint mbedtls_ssl_tls13_check_sig_alg_cert_key_match(uint16_t sig_alg,
2455a8e1175bSopenharmony_ci                                                   mbedtls_pk_context *key);
2456a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
2457a8e1175bSopenharmony_ci
2458a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
2459a8e1175bSopenharmony_cistatic inline int mbedtls_ssl_sig_alg_is_offered(const mbedtls_ssl_context *ssl,
2460a8e1175bSopenharmony_ci                                                 uint16_t proposed_sig_alg)
2461a8e1175bSopenharmony_ci{
2462a8e1175bSopenharmony_ci    const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs(ssl);
2463a8e1175bSopenharmony_ci    if (sig_alg == NULL) {
2464a8e1175bSopenharmony_ci        return 0;
2465a8e1175bSopenharmony_ci    }
2466a8e1175bSopenharmony_ci
2467a8e1175bSopenharmony_ci    for (; *sig_alg != MBEDTLS_TLS_SIG_NONE; sig_alg++) {
2468a8e1175bSopenharmony_ci        if (*sig_alg == proposed_sig_alg) {
2469a8e1175bSopenharmony_ci            return 1;
2470a8e1175bSopenharmony_ci        }
2471a8e1175bSopenharmony_ci    }
2472a8e1175bSopenharmony_ci    return 0;
2473a8e1175bSopenharmony_ci}
2474a8e1175bSopenharmony_ci
2475a8e1175bSopenharmony_cistatic inline int mbedtls_ssl_get_pk_type_and_md_alg_from_sig_alg(
2476a8e1175bSopenharmony_ci    uint16_t sig_alg, mbedtls_pk_type_t *pk_type, mbedtls_md_type_t *md_alg)
2477a8e1175bSopenharmony_ci{
2478a8e1175bSopenharmony_ci    *pk_type = mbedtls_ssl_pk_alg_from_sig(sig_alg & 0xff);
2479a8e1175bSopenharmony_ci    *md_alg = mbedtls_ssl_md_alg_from_hash((sig_alg >> 8) & 0xff);
2480a8e1175bSopenharmony_ci
2481a8e1175bSopenharmony_ci    if (*pk_type != MBEDTLS_PK_NONE && *md_alg != MBEDTLS_MD_NONE) {
2482a8e1175bSopenharmony_ci        return 0;
2483a8e1175bSopenharmony_ci    }
2484a8e1175bSopenharmony_ci
2485a8e1175bSopenharmony_ci    switch (sig_alg) {
2486a8e1175bSopenharmony_ci#if defined(MBEDTLS_PKCS1_V21)
2487a8e1175bSopenharmony_ci#if defined(MBEDTLS_MD_CAN_SHA256)
2488a8e1175bSopenharmony_ci        case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256:
2489a8e1175bSopenharmony_ci            *md_alg = MBEDTLS_MD_SHA256;
2490a8e1175bSopenharmony_ci            *pk_type = MBEDTLS_PK_RSASSA_PSS;
2491a8e1175bSopenharmony_ci            break;
2492a8e1175bSopenharmony_ci#endif /* MBEDTLS_MD_CAN_SHA256  */
2493a8e1175bSopenharmony_ci#if defined(MBEDTLS_MD_CAN_SHA384)
2494a8e1175bSopenharmony_ci        case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384:
2495a8e1175bSopenharmony_ci            *md_alg = MBEDTLS_MD_SHA384;
2496a8e1175bSopenharmony_ci            *pk_type = MBEDTLS_PK_RSASSA_PSS;
2497a8e1175bSopenharmony_ci            break;
2498a8e1175bSopenharmony_ci#endif /* MBEDTLS_MD_CAN_SHA384 */
2499a8e1175bSopenharmony_ci#if defined(MBEDTLS_MD_CAN_SHA512)
2500a8e1175bSopenharmony_ci        case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512:
2501a8e1175bSopenharmony_ci            *md_alg = MBEDTLS_MD_SHA512;
2502a8e1175bSopenharmony_ci            *pk_type = MBEDTLS_PK_RSASSA_PSS;
2503a8e1175bSopenharmony_ci            break;
2504a8e1175bSopenharmony_ci#endif /* MBEDTLS_MD_CAN_SHA512 */
2505a8e1175bSopenharmony_ci#endif /* MBEDTLS_PKCS1_V21 */
2506a8e1175bSopenharmony_ci        default:
2507a8e1175bSopenharmony_ci            return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
2508a8e1175bSopenharmony_ci    }
2509a8e1175bSopenharmony_ci    return 0;
2510a8e1175bSopenharmony_ci}
2511a8e1175bSopenharmony_ci
2512a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2513a8e1175bSopenharmony_cistatic inline int mbedtls_ssl_tls12_sig_alg_is_supported(
2514a8e1175bSopenharmony_ci    const uint16_t sig_alg)
2515a8e1175bSopenharmony_ci{
2516a8e1175bSopenharmony_ci    /* High byte is hash */
2517a8e1175bSopenharmony_ci    unsigned char hash = MBEDTLS_BYTE_1(sig_alg);
2518a8e1175bSopenharmony_ci    unsigned char sig = MBEDTLS_BYTE_0(sig_alg);
2519a8e1175bSopenharmony_ci
2520a8e1175bSopenharmony_ci    switch (hash) {
2521a8e1175bSopenharmony_ci#if defined(MBEDTLS_MD_CAN_MD5)
2522a8e1175bSopenharmony_ci        case MBEDTLS_SSL_HASH_MD5:
2523a8e1175bSopenharmony_ci            break;
2524a8e1175bSopenharmony_ci#endif
2525a8e1175bSopenharmony_ci
2526a8e1175bSopenharmony_ci#if defined(MBEDTLS_MD_CAN_SHA1)
2527a8e1175bSopenharmony_ci        case MBEDTLS_SSL_HASH_SHA1:
2528a8e1175bSopenharmony_ci            break;
2529a8e1175bSopenharmony_ci#endif
2530a8e1175bSopenharmony_ci
2531a8e1175bSopenharmony_ci#if defined(MBEDTLS_MD_CAN_SHA224)
2532a8e1175bSopenharmony_ci        case MBEDTLS_SSL_HASH_SHA224:
2533a8e1175bSopenharmony_ci            break;
2534a8e1175bSopenharmony_ci#endif
2535a8e1175bSopenharmony_ci
2536a8e1175bSopenharmony_ci#if defined(MBEDTLS_MD_CAN_SHA256)
2537a8e1175bSopenharmony_ci        case MBEDTLS_SSL_HASH_SHA256:
2538a8e1175bSopenharmony_ci            break;
2539a8e1175bSopenharmony_ci#endif
2540a8e1175bSopenharmony_ci
2541a8e1175bSopenharmony_ci#if defined(MBEDTLS_MD_CAN_SHA384)
2542a8e1175bSopenharmony_ci        case MBEDTLS_SSL_HASH_SHA384:
2543a8e1175bSopenharmony_ci            break;
2544a8e1175bSopenharmony_ci#endif
2545a8e1175bSopenharmony_ci
2546a8e1175bSopenharmony_ci#if defined(MBEDTLS_MD_CAN_SHA512)
2547a8e1175bSopenharmony_ci        case MBEDTLS_SSL_HASH_SHA512:
2548a8e1175bSopenharmony_ci            break;
2549a8e1175bSopenharmony_ci#endif
2550a8e1175bSopenharmony_ci
2551a8e1175bSopenharmony_ci        default:
2552a8e1175bSopenharmony_ci            return 0;
2553a8e1175bSopenharmony_ci    }
2554a8e1175bSopenharmony_ci
2555a8e1175bSopenharmony_ci    switch (sig) {
2556a8e1175bSopenharmony_ci#if defined(MBEDTLS_RSA_C)
2557a8e1175bSopenharmony_ci        case MBEDTLS_SSL_SIG_RSA:
2558a8e1175bSopenharmony_ci            break;
2559a8e1175bSopenharmony_ci#endif
2560a8e1175bSopenharmony_ci
2561a8e1175bSopenharmony_ci#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
2562a8e1175bSopenharmony_ci        case MBEDTLS_SSL_SIG_ECDSA:
2563a8e1175bSopenharmony_ci            break;
2564a8e1175bSopenharmony_ci#endif
2565a8e1175bSopenharmony_ci
2566a8e1175bSopenharmony_ci        default:
2567a8e1175bSopenharmony_ci            return 0;
2568a8e1175bSopenharmony_ci    }
2569a8e1175bSopenharmony_ci
2570a8e1175bSopenharmony_ci    return 1;
2571a8e1175bSopenharmony_ci}
2572a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2573a8e1175bSopenharmony_ci
2574a8e1175bSopenharmony_cistatic inline int mbedtls_ssl_sig_alg_is_supported(
2575a8e1175bSopenharmony_ci    const mbedtls_ssl_context *ssl,
2576a8e1175bSopenharmony_ci    const uint16_t sig_alg)
2577a8e1175bSopenharmony_ci{
2578a8e1175bSopenharmony_ci
2579a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2580a8e1175bSopenharmony_ci    if (ssl->MBEDTLS_PRIVATE(tls_version) == MBEDTLS_SSL_VERSION_TLS1_2) {
2581a8e1175bSopenharmony_ci        return mbedtls_ssl_tls12_sig_alg_is_supported(sig_alg);
2582a8e1175bSopenharmony_ci    }
2583a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2584a8e1175bSopenharmony_ci
2585a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
2586a8e1175bSopenharmony_ci    if (ssl->MBEDTLS_PRIVATE(tls_version) == MBEDTLS_SSL_VERSION_TLS1_3) {
2587a8e1175bSopenharmony_ci        return mbedtls_ssl_tls13_sig_alg_is_supported(sig_alg);
2588a8e1175bSopenharmony_ci    }
2589a8e1175bSopenharmony_ci#endif
2590a8e1175bSopenharmony_ci    ((void) ssl);
2591a8e1175bSopenharmony_ci    ((void) sig_alg);
2592a8e1175bSopenharmony_ci    return 0;
2593a8e1175bSopenharmony_ci}
2594a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
2595a8e1175bSopenharmony_ci
2596a8e1175bSopenharmony_ci#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
2597a8e1175bSopenharmony_ci/* Corresponding PSA algorithm for MBEDTLS_CIPHER_NULL.
2598a8e1175bSopenharmony_ci * Same value is used for PSA_ALG_CATEGORY_CIPHER, hence it is
2599a8e1175bSopenharmony_ci * guaranteed to not be a valid PSA algorithm identifier.
2600a8e1175bSopenharmony_ci */
2601a8e1175bSopenharmony_ci#define MBEDTLS_SSL_NULL_CIPHER 0x04000000
2602a8e1175bSopenharmony_ci
2603a8e1175bSopenharmony_ci/**
2604a8e1175bSopenharmony_ci * \brief       Translate mbedtls cipher type/taglen pair to psa:
2605a8e1175bSopenharmony_ci *              algorithm, key type and key size.
2606a8e1175bSopenharmony_ci *
2607a8e1175bSopenharmony_ci * \param  mbedtls_cipher_type [in] given mbedtls cipher type
2608a8e1175bSopenharmony_ci * \param  taglen              [in] given tag length
2609a8e1175bSopenharmony_ci *                                  0 - default tag length
2610a8e1175bSopenharmony_ci * \param  alg                 [out] corresponding PSA alg
2611a8e1175bSopenharmony_ci *                                   There is no corresponding PSA
2612a8e1175bSopenharmony_ci *                                   alg for MBEDTLS_CIPHER_NULL, so
2613a8e1175bSopenharmony_ci *                                   in this case MBEDTLS_SSL_NULL_CIPHER
2614a8e1175bSopenharmony_ci *                                   is returned via this parameter
2615a8e1175bSopenharmony_ci * \param  key_type            [out] corresponding PSA key type
2616a8e1175bSopenharmony_ci * \param  key_size            [out] corresponding PSA key size
2617a8e1175bSopenharmony_ci *
2618a8e1175bSopenharmony_ci * \return                     PSA_SUCCESS on success or PSA_ERROR_NOT_SUPPORTED if
2619a8e1175bSopenharmony_ci *                             conversion is not supported.
2620a8e1175bSopenharmony_ci */
2621a8e1175bSopenharmony_cipsa_status_t mbedtls_ssl_cipher_to_psa(mbedtls_cipher_type_t mbedtls_cipher_type,
2622a8e1175bSopenharmony_ci                                       size_t taglen,
2623a8e1175bSopenharmony_ci                                       psa_algorithm_t *alg,
2624a8e1175bSopenharmony_ci                                       psa_key_type_t *key_type,
2625a8e1175bSopenharmony_ci                                       size_t *key_size);
2626a8e1175bSopenharmony_ci
2627a8e1175bSopenharmony_ci#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2628a8e1175bSopenharmony_ci/**
2629a8e1175bSopenharmony_ci * \brief       Convert given PSA status to mbedtls error code.
2630a8e1175bSopenharmony_ci *
2631a8e1175bSopenharmony_ci * \param  status      [in] given PSA status
2632a8e1175bSopenharmony_ci *
2633a8e1175bSopenharmony_ci * \return             corresponding mbedtls error code
2634a8e1175bSopenharmony_ci */
2635a8e1175bSopenharmony_cistatic inline MBEDTLS_DEPRECATED int psa_ssl_status_to_mbedtls(psa_status_t status)
2636a8e1175bSopenharmony_ci{
2637a8e1175bSopenharmony_ci    switch (status) {
2638a8e1175bSopenharmony_ci        case PSA_SUCCESS:
2639a8e1175bSopenharmony_ci            return 0;
2640a8e1175bSopenharmony_ci        case PSA_ERROR_INSUFFICIENT_MEMORY:
2641a8e1175bSopenharmony_ci            return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2642a8e1175bSopenharmony_ci        case PSA_ERROR_NOT_SUPPORTED:
2643a8e1175bSopenharmony_ci            return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
2644a8e1175bSopenharmony_ci        case PSA_ERROR_INVALID_SIGNATURE:
2645a8e1175bSopenharmony_ci            return MBEDTLS_ERR_SSL_INVALID_MAC;
2646a8e1175bSopenharmony_ci        case PSA_ERROR_INVALID_ARGUMENT:
2647a8e1175bSopenharmony_ci            return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2648a8e1175bSopenharmony_ci        case PSA_ERROR_BAD_STATE:
2649a8e1175bSopenharmony_ci            return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2650a8e1175bSopenharmony_ci        case PSA_ERROR_BUFFER_TOO_SMALL:
2651a8e1175bSopenharmony_ci            return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
2652a8e1175bSopenharmony_ci        default:
2653a8e1175bSopenharmony_ci            return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
2654a8e1175bSopenharmony_ci    }
2655a8e1175bSopenharmony_ci}
2656a8e1175bSopenharmony_ci#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2657a8e1175bSopenharmony_ci#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
2658a8e1175bSopenharmony_ci
2659a8e1175bSopenharmony_ci#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) && \
2660a8e1175bSopenharmony_ci    defined(MBEDTLS_USE_PSA_CRYPTO)
2661a8e1175bSopenharmony_ci
2662a8e1175bSopenharmony_citypedef enum {
2663a8e1175bSopenharmony_ci    MBEDTLS_ECJPAKE_ROUND_ONE,
2664a8e1175bSopenharmony_ci    MBEDTLS_ECJPAKE_ROUND_TWO
2665a8e1175bSopenharmony_ci} mbedtls_ecjpake_rounds_t;
2666a8e1175bSopenharmony_ci
2667a8e1175bSopenharmony_ci/**
2668a8e1175bSopenharmony_ci * \brief       Parse the provided input buffer for getting the first round
2669a8e1175bSopenharmony_ci *              of key exchange. This code is common between server and client
2670a8e1175bSopenharmony_ci *
2671a8e1175bSopenharmony_ci * \param  pake_ctx [in] the PAKE's operation/context structure
2672a8e1175bSopenharmony_ci * \param  buf      [in] input buffer to parse
2673a8e1175bSopenharmony_ci * \param  len      [in] length of the input buffer
2674a8e1175bSopenharmony_ci * \param  round    [in] either MBEDTLS_ECJPAKE_ROUND_ONE or
2675a8e1175bSopenharmony_ci *                       MBEDTLS_ECJPAKE_ROUND_TWO
2676a8e1175bSopenharmony_ci *
2677a8e1175bSopenharmony_ci * \return               0 on success or a negative error code in case of failure
2678a8e1175bSopenharmony_ci */
2679a8e1175bSopenharmony_ciint mbedtls_psa_ecjpake_read_round(
2680a8e1175bSopenharmony_ci    psa_pake_operation_t *pake_ctx,
2681a8e1175bSopenharmony_ci    const unsigned char *buf,
2682a8e1175bSopenharmony_ci    size_t len, mbedtls_ecjpake_rounds_t round);
2683a8e1175bSopenharmony_ci
2684a8e1175bSopenharmony_ci/**
2685a8e1175bSopenharmony_ci * \brief       Write the first round of key exchange into the provided output
2686a8e1175bSopenharmony_ci *              buffer. This code is common between server and client
2687a8e1175bSopenharmony_ci *
2688a8e1175bSopenharmony_ci * \param  pake_ctx [in] the PAKE's operation/context structure
2689a8e1175bSopenharmony_ci * \param  buf      [out] the output buffer in which data will be written to
2690a8e1175bSopenharmony_ci * \param  len      [in] length of the output buffer
2691a8e1175bSopenharmony_ci * \param  olen     [out] the length of the data really written on the buffer
2692a8e1175bSopenharmony_ci * \param  round    [in] either MBEDTLS_ECJPAKE_ROUND_ONE or
2693a8e1175bSopenharmony_ci *                       MBEDTLS_ECJPAKE_ROUND_TWO
2694a8e1175bSopenharmony_ci *
2695a8e1175bSopenharmony_ci * \return               0 on success or a negative error code in case of failure
2696a8e1175bSopenharmony_ci */
2697a8e1175bSopenharmony_ciint mbedtls_psa_ecjpake_write_round(
2698a8e1175bSopenharmony_ci    psa_pake_operation_t *pake_ctx,
2699a8e1175bSopenharmony_ci    unsigned char *buf,
2700a8e1175bSopenharmony_ci    size_t len, size_t *olen,
2701a8e1175bSopenharmony_ci    mbedtls_ecjpake_rounds_t round);
2702a8e1175bSopenharmony_ci
2703a8e1175bSopenharmony_ci#endif //MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED && MBEDTLS_USE_PSA_CRYPTO
2704a8e1175bSopenharmony_ci
2705a8e1175bSopenharmony_ci/**
2706a8e1175bSopenharmony_ci * \brief       TLS record protection modes
2707a8e1175bSopenharmony_ci */
2708a8e1175bSopenharmony_citypedef enum {
2709a8e1175bSopenharmony_ci    MBEDTLS_SSL_MODE_STREAM = 0,
2710a8e1175bSopenharmony_ci    MBEDTLS_SSL_MODE_CBC,
2711a8e1175bSopenharmony_ci    MBEDTLS_SSL_MODE_CBC_ETM,
2712a8e1175bSopenharmony_ci    MBEDTLS_SSL_MODE_AEAD
2713a8e1175bSopenharmony_ci} mbedtls_ssl_mode_t;
2714a8e1175bSopenharmony_ci
2715a8e1175bSopenharmony_cimbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
2716a8e1175bSopenharmony_ci    const mbedtls_ssl_transform *transform);
2717a8e1175bSopenharmony_ci
2718a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
2719a8e1175bSopenharmony_cimbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
2720a8e1175bSopenharmony_ci    int encrypt_then_mac,
2721a8e1175bSopenharmony_ci    const mbedtls_ssl_ciphersuite_t *suite);
2722a8e1175bSopenharmony_ci#else
2723a8e1175bSopenharmony_cimbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
2724a8e1175bSopenharmony_ci    const mbedtls_ssl_ciphersuite_t *suite);
2725a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
2726a8e1175bSopenharmony_ci
2727a8e1175bSopenharmony_ci#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
2728a8e1175bSopenharmony_ci
2729a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL
2730a8e1175bSopenharmony_ciint mbedtls_ssl_tls13_read_public_xxdhe_share(mbedtls_ssl_context *ssl,
2731a8e1175bSopenharmony_ci                                              const unsigned char *buf,
2732a8e1175bSopenharmony_ci                                              size_t buf_len);
2733a8e1175bSopenharmony_ci
2734a8e1175bSopenharmony_ci#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH */
2735a8e1175bSopenharmony_ci
2736a8e1175bSopenharmony_cistatic inline int mbedtls_ssl_tls13_cipher_suite_is_offered(
2737a8e1175bSopenharmony_ci    mbedtls_ssl_context *ssl, int cipher_suite)
2738a8e1175bSopenharmony_ci{
2739a8e1175bSopenharmony_ci    const int *ciphersuite_list = ssl->MBEDTLS_PRIVATE(conf)->MBEDTLS_PRIVATE(ciphersuite_list);
2740a8e1175bSopenharmony_ci
2741a8e1175bSopenharmony_ci    /* Check whether we have offered this ciphersuite */
2742a8e1175bSopenharmony_ci    for (size_t i = 0; ciphersuite_list[i] != 0; i++) {
2743a8e1175bSopenharmony_ci        if (ciphersuite_list[i] == cipher_suite) {
2744a8e1175bSopenharmony_ci            return 1;
2745a8e1175bSopenharmony_ci        }
2746a8e1175bSopenharmony_ci    }
2747a8e1175bSopenharmony_ci    return 0;
2748a8e1175bSopenharmony_ci}
2749a8e1175bSopenharmony_ci
2750a8e1175bSopenharmony_ci/**
2751a8e1175bSopenharmony_ci * \brief Validate cipher suite against config in SSL context.
2752a8e1175bSopenharmony_ci *
2753a8e1175bSopenharmony_ci * \param ssl              SSL context
2754a8e1175bSopenharmony_ci * \param suite_info       Cipher suite to validate
2755a8e1175bSopenharmony_ci * \param min_tls_version  Minimal TLS version to accept a cipher suite
2756a8e1175bSopenharmony_ci * \param max_tls_version  Maximal TLS version to accept a cipher suite
2757a8e1175bSopenharmony_ci *
2758a8e1175bSopenharmony_ci * \return 0 if valid, negative value otherwise.
2759a8e1175bSopenharmony_ci */
2760a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL
2761a8e1175bSopenharmony_ciint mbedtls_ssl_validate_ciphersuite(
2762a8e1175bSopenharmony_ci    const mbedtls_ssl_context *ssl,
2763a8e1175bSopenharmony_ci    const mbedtls_ssl_ciphersuite_t *suite_info,
2764a8e1175bSopenharmony_ci    mbedtls_ssl_protocol_version min_tls_version,
2765a8e1175bSopenharmony_ci    mbedtls_ssl_protocol_version max_tls_version);
2766a8e1175bSopenharmony_ci
2767a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2768a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL
2769a8e1175bSopenharmony_ciint mbedtls_ssl_parse_server_name_ext(mbedtls_ssl_context *ssl,
2770a8e1175bSopenharmony_ci                                      const unsigned char *buf,
2771a8e1175bSopenharmony_ci                                      const unsigned char *end);
2772a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
2773a8e1175bSopenharmony_ci
2774a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
2775a8e1175bSopenharmony_ci#define MBEDTLS_SSL_RECORD_SIZE_LIMIT_EXTENSION_DATA_LENGTH (2)
2776a8e1175bSopenharmony_ci#define MBEDTLS_SSL_RECORD_SIZE_LIMIT_MIN (64)      /* As defined in RFC 8449 */
2777a8e1175bSopenharmony_ci
2778a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL
2779a8e1175bSopenharmony_ciint mbedtls_ssl_tls13_parse_record_size_limit_ext(mbedtls_ssl_context *ssl,
2780a8e1175bSopenharmony_ci                                                  const unsigned char *buf,
2781a8e1175bSopenharmony_ci                                                  const unsigned char *end);
2782a8e1175bSopenharmony_ci
2783a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL
2784a8e1175bSopenharmony_ciint mbedtls_ssl_tls13_write_record_size_limit_ext(mbedtls_ssl_context *ssl,
2785a8e1175bSopenharmony_ci                                                  unsigned char *buf,
2786a8e1175bSopenharmony_ci                                                  const unsigned char *end,
2787a8e1175bSopenharmony_ci                                                  size_t *out_len);
2788a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
2789a8e1175bSopenharmony_ci
2790a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_ALPN)
2791a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL
2792a8e1175bSopenharmony_ciint mbedtls_ssl_parse_alpn_ext(mbedtls_ssl_context *ssl,
2793a8e1175bSopenharmony_ci                               const unsigned char *buf,
2794a8e1175bSopenharmony_ci                               const unsigned char *end);
2795a8e1175bSopenharmony_ci
2796a8e1175bSopenharmony_ci
2797a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL
2798a8e1175bSopenharmony_ciint mbedtls_ssl_write_alpn_ext(mbedtls_ssl_context *ssl,
2799a8e1175bSopenharmony_ci                               unsigned char *buf,
2800a8e1175bSopenharmony_ci                               unsigned char *end,
2801a8e1175bSopenharmony_ci                               size_t *out_len);
2802a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_ALPN */
2803a8e1175bSopenharmony_ci
2804a8e1175bSopenharmony_ci#if defined(MBEDTLS_TEST_HOOKS)
2805a8e1175bSopenharmony_ciint mbedtls_ssl_check_dtls_clihlo_cookie(
2806a8e1175bSopenharmony_ci    mbedtls_ssl_context *ssl,
2807a8e1175bSopenharmony_ci    const unsigned char *cli_id, size_t cli_id_len,
2808a8e1175bSopenharmony_ci    const unsigned char *in, size_t in_len,
2809a8e1175bSopenharmony_ci    unsigned char *obuf, size_t buf_len, size_t *olen);
2810a8e1175bSopenharmony_ci#endif
2811a8e1175bSopenharmony_ci
2812a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
2813a8e1175bSopenharmony_ci/**
2814a8e1175bSopenharmony_ci * \brief Given an SSL context and its associated configuration, write the TLS
2815a8e1175bSopenharmony_ci *        1.3 specific Pre-Shared key extension.
2816a8e1175bSopenharmony_ci *
2817a8e1175bSopenharmony_ci * \param[in]   ssl     SSL context
2818a8e1175bSopenharmony_ci * \param[in]   buf     Base address of the buffer where to write the extension
2819a8e1175bSopenharmony_ci * \param[in]   end     End address of the buffer where to write the extension
2820a8e1175bSopenharmony_ci * \param[out]  out_len Length in bytes of the Pre-Shared key extension: data
2821a8e1175bSopenharmony_ci *                      written into the buffer \p buf by this function plus
2822a8e1175bSopenharmony_ci *                      the length of the binders to be written.
2823a8e1175bSopenharmony_ci * \param[out]  binders_len Length of the binders to be written at the end of
2824a8e1175bSopenharmony_ci *                          the extension.
2825a8e1175bSopenharmony_ci */
2826a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL
2827a8e1175bSopenharmony_ciint mbedtls_ssl_tls13_write_identities_of_pre_shared_key_ext(
2828a8e1175bSopenharmony_ci    mbedtls_ssl_context *ssl,
2829a8e1175bSopenharmony_ci    unsigned char *buf, unsigned char *end,
2830a8e1175bSopenharmony_ci    size_t *out_len, size_t *binders_len);
2831a8e1175bSopenharmony_ci
2832a8e1175bSopenharmony_ci/**
2833a8e1175bSopenharmony_ci * \brief Given an SSL context and its associated configuration, write the TLS
2834a8e1175bSopenharmony_ci *        1.3 specific Pre-Shared key extension binders at the end of the
2835a8e1175bSopenharmony_ci *        ClientHello.
2836a8e1175bSopenharmony_ci *
2837a8e1175bSopenharmony_ci * \param[in]   ssl     SSL context
2838a8e1175bSopenharmony_ci * \param[in]   buf     Base address of the buffer where to write the binders
2839a8e1175bSopenharmony_ci * \param[in]   end     End address of the buffer where to write the binders
2840a8e1175bSopenharmony_ci */
2841a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL
2842a8e1175bSopenharmony_ciint mbedtls_ssl_tls13_write_binders_of_pre_shared_key_ext(
2843a8e1175bSopenharmony_ci    mbedtls_ssl_context *ssl,
2844a8e1175bSopenharmony_ci    unsigned char *buf, unsigned char *end);
2845a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
2846a8e1175bSopenharmony_ci
2847a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
2848a8e1175bSopenharmony_ci    defined(MBEDTLS_SSL_SESSION_TICKETS) && \
2849a8e1175bSopenharmony_ci    defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
2850a8e1175bSopenharmony_ci    defined(MBEDTLS_SSL_CLI_C)
2851a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL
2852a8e1175bSopenharmony_ciint mbedtls_ssl_session_set_hostname(mbedtls_ssl_session *session,
2853a8e1175bSopenharmony_ci                                     const char *hostname);
2854a8e1175bSopenharmony_ci#endif
2855a8e1175bSopenharmony_ci
2856a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_EARLY_DATA) && \
2857a8e1175bSopenharmony_ci    defined(MBEDTLS_SSL_ALPN)
2858a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL
2859a8e1175bSopenharmony_ciint mbedtls_ssl_session_set_ticket_alpn(mbedtls_ssl_session *session,
2860a8e1175bSopenharmony_ci                                        const char *alpn);
2861a8e1175bSopenharmony_ci#endif
2862a8e1175bSopenharmony_ci
2863a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
2864a8e1175bSopenharmony_ci
2865a8e1175bSopenharmony_ci#define MBEDTLS_SSL_TLS1_3_MAX_ALLOWED_TICKET_LIFETIME (604800)
2866a8e1175bSopenharmony_ci
2867a8e1175bSopenharmony_cistatic inline unsigned int mbedtls_ssl_tls13_session_get_ticket_flags(
2868a8e1175bSopenharmony_ci    mbedtls_ssl_session *session, unsigned int flags)
2869a8e1175bSopenharmony_ci{
2870a8e1175bSopenharmony_ci    return session->ticket_flags &
2871a8e1175bSopenharmony_ci           (flags & MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK);
2872a8e1175bSopenharmony_ci}
2873a8e1175bSopenharmony_ci
2874a8e1175bSopenharmony_ci/**
2875a8e1175bSopenharmony_ci * Check if at least one of the given flags is set in
2876a8e1175bSopenharmony_ci * the session ticket. See the definition of
2877a8e1175bSopenharmony_ci * `MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK` to get all
2878a8e1175bSopenharmony_ci * permitted flags.
2879a8e1175bSopenharmony_ci */
2880a8e1175bSopenharmony_cistatic inline int mbedtls_ssl_tls13_session_ticket_has_flags(
2881a8e1175bSopenharmony_ci    mbedtls_ssl_session *session, unsigned int flags)
2882a8e1175bSopenharmony_ci{
2883a8e1175bSopenharmony_ci    return mbedtls_ssl_tls13_session_get_ticket_flags(session, flags) != 0;
2884a8e1175bSopenharmony_ci}
2885a8e1175bSopenharmony_ci
2886a8e1175bSopenharmony_cistatic inline int mbedtls_ssl_tls13_session_ticket_allow_psk(
2887a8e1175bSopenharmony_ci    mbedtls_ssl_session *session)
2888a8e1175bSopenharmony_ci{
2889a8e1175bSopenharmony_ci    return mbedtls_ssl_tls13_session_ticket_has_flags(
2890a8e1175bSopenharmony_ci        session, MBEDTLS_SSL_TLS1_3_TICKET_ALLOW_PSK_RESUMPTION);
2891a8e1175bSopenharmony_ci}
2892a8e1175bSopenharmony_ci
2893a8e1175bSopenharmony_cistatic inline int mbedtls_ssl_tls13_session_ticket_allow_psk_ephemeral(
2894a8e1175bSopenharmony_ci    mbedtls_ssl_session *session)
2895a8e1175bSopenharmony_ci{
2896a8e1175bSopenharmony_ci    return mbedtls_ssl_tls13_session_ticket_has_flags(
2897a8e1175bSopenharmony_ci        session, MBEDTLS_SSL_TLS1_3_TICKET_ALLOW_PSK_EPHEMERAL_RESUMPTION);
2898a8e1175bSopenharmony_ci}
2899a8e1175bSopenharmony_ci
2900a8e1175bSopenharmony_cistatic inline unsigned int mbedtls_ssl_tls13_session_ticket_allow_early_data(
2901a8e1175bSopenharmony_ci    mbedtls_ssl_session *session)
2902a8e1175bSopenharmony_ci{
2903a8e1175bSopenharmony_ci    return mbedtls_ssl_tls13_session_ticket_has_flags(
2904a8e1175bSopenharmony_ci        session, MBEDTLS_SSL_TLS1_3_TICKET_ALLOW_EARLY_DATA);
2905a8e1175bSopenharmony_ci}
2906a8e1175bSopenharmony_ci
2907a8e1175bSopenharmony_cistatic inline void mbedtls_ssl_tls13_session_set_ticket_flags(
2908a8e1175bSopenharmony_ci    mbedtls_ssl_session *session, unsigned int flags)
2909a8e1175bSopenharmony_ci{
2910a8e1175bSopenharmony_ci    session->ticket_flags |= (flags & MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK);
2911a8e1175bSopenharmony_ci}
2912a8e1175bSopenharmony_ci
2913a8e1175bSopenharmony_cistatic inline void mbedtls_ssl_tls13_session_clear_ticket_flags(
2914a8e1175bSopenharmony_ci    mbedtls_ssl_session *session, unsigned int flags)
2915a8e1175bSopenharmony_ci{
2916a8e1175bSopenharmony_ci    session->ticket_flags &= ~(flags & MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK);
2917a8e1175bSopenharmony_ci}
2918a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_PROTO_TLS1_3 && MBEDTLS_SSL_SESSION_TICKETS */
2919a8e1175bSopenharmony_ci
2920a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
2921a8e1175bSopenharmony_ciint mbedtls_ssl_tls13_finalize_client_hello(mbedtls_ssl_context *ssl);
2922a8e1175bSopenharmony_ci#endif
2923a8e1175bSopenharmony_ci
2924a8e1175bSopenharmony_ci#if defined(MBEDTLS_TEST_HOOKS) && defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
2925a8e1175bSopenharmony_ci
2926a8e1175bSopenharmony_ci/** Compute the HMAC of variable-length data with constant flow.
2927a8e1175bSopenharmony_ci *
2928a8e1175bSopenharmony_ci * This function computes the HMAC of the concatenation of \p add_data and \p
2929a8e1175bSopenharmony_ci * data, and does with a code flow and memory access pattern that does not
2930a8e1175bSopenharmony_ci * depend on \p data_len_secret, but only on \p min_data_len and \p
2931a8e1175bSopenharmony_ci * max_data_len. In particular, this function always reads exactly \p
2932a8e1175bSopenharmony_ci * max_data_len bytes from \p data.
2933a8e1175bSopenharmony_ci *
2934a8e1175bSopenharmony_ci * \param ctx               The HMAC context. It must have keys configured
2935a8e1175bSopenharmony_ci *                          with mbedtls_md_hmac_starts() and use one of the
2936a8e1175bSopenharmony_ci *                          following hashes: SHA-384, SHA-256, SHA-1 or MD-5.
2937a8e1175bSopenharmony_ci *                          It is reset using mbedtls_md_hmac_reset() after
2938a8e1175bSopenharmony_ci *                          the computation is complete to prepare for the
2939a8e1175bSopenharmony_ci *                          next computation.
2940a8e1175bSopenharmony_ci * \param add_data          The first part of the message whose HMAC is being
2941a8e1175bSopenharmony_ci *                          calculated. This must point to a readable buffer
2942a8e1175bSopenharmony_ci *                          of \p add_data_len bytes.
2943a8e1175bSopenharmony_ci * \param add_data_len      The length of \p add_data in bytes.
2944a8e1175bSopenharmony_ci * \param data              The buffer containing the second part of the
2945a8e1175bSopenharmony_ci *                          message. This must point to a readable buffer
2946a8e1175bSopenharmony_ci *                          of \p max_data_len bytes.
2947a8e1175bSopenharmony_ci * \param data_len_secret   The length of the data to process in \p data.
2948a8e1175bSopenharmony_ci *                          This must be no less than \p min_data_len and no
2949a8e1175bSopenharmony_ci *                          greater than \p max_data_len.
2950a8e1175bSopenharmony_ci * \param min_data_len      The minimal length of the second part of the
2951a8e1175bSopenharmony_ci *                          message, read from \p data.
2952a8e1175bSopenharmony_ci * \param max_data_len      The maximal length of the second part of the
2953a8e1175bSopenharmony_ci *                          message, read from \p data.
2954a8e1175bSopenharmony_ci * \param output            The HMAC will be written here. This must point to
2955a8e1175bSopenharmony_ci *                          a writable buffer of sufficient size to hold the
2956a8e1175bSopenharmony_ci *                          HMAC value.
2957a8e1175bSopenharmony_ci *
2958a8e1175bSopenharmony_ci * \retval 0 on success.
2959a8e1175bSopenharmony_ci * \retval #MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED
2960a8e1175bSopenharmony_ci *         The hardware accelerator failed.
2961a8e1175bSopenharmony_ci */
2962a8e1175bSopenharmony_ci#if defined(MBEDTLS_USE_PSA_CRYPTO)
2963a8e1175bSopenharmony_ciint mbedtls_ct_hmac(mbedtls_svc_key_id_t key,
2964a8e1175bSopenharmony_ci                    psa_algorithm_t mac_alg,
2965a8e1175bSopenharmony_ci                    const unsigned char *add_data,
2966a8e1175bSopenharmony_ci                    size_t add_data_len,
2967a8e1175bSopenharmony_ci                    const unsigned char *data,
2968a8e1175bSopenharmony_ci                    size_t data_len_secret,
2969a8e1175bSopenharmony_ci                    size_t min_data_len,
2970a8e1175bSopenharmony_ci                    size_t max_data_len,
2971a8e1175bSopenharmony_ci                    unsigned char *output);
2972a8e1175bSopenharmony_ci#else
2973a8e1175bSopenharmony_ciint mbedtls_ct_hmac(mbedtls_md_context_t *ctx,
2974a8e1175bSopenharmony_ci                    const unsigned char *add_data,
2975a8e1175bSopenharmony_ci                    size_t add_data_len,
2976a8e1175bSopenharmony_ci                    const unsigned char *data,
2977a8e1175bSopenharmony_ci                    size_t data_len_secret,
2978a8e1175bSopenharmony_ci                    size_t min_data_len,
2979a8e1175bSopenharmony_ci                    size_t max_data_len,
2980a8e1175bSopenharmony_ci                    unsigned char *output);
2981a8e1175bSopenharmony_ci#endif /* defined(MBEDTLS_USE_PSA_CRYPTO) */
2982a8e1175bSopenharmony_ci#endif /* MBEDTLS_TEST_HOOKS && defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) */
2983a8e1175bSopenharmony_ci
2984a8e1175bSopenharmony_ci#endif /* ssl_misc.h */
2985