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