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