blob: 851d6cd9a3d5a98621a29b099bb273615c6e70e8 [file] [log] [blame]
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +02001/**
Chris Jones84a773f2021-03-05 18:38:47 +00002 * \file ssl_misc.h
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +02003 *
4 * \brief Internal functions shared by the SSL modules
Darryl Greena40a1012018-01-05 15:33:17 +00005 */
6/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02007 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02008 * SPDX-License-Identifier: Apache-2.0
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License"); you may
11 * not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020021 */
Chris Jones84a773f2021-03-05 18:38:47 +000022#ifndef MBEDTLS_SSL_MISC_H
23#define MBEDTLS_SSL_MISC_H
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020024
Bence Szépkútic662b362021-05-27 11:25:03 +020025#include "mbedtls/build_info.h"
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050026
Jaeden Amero6609aef2019-07-04 20:01:14 +010027#include "mbedtls/ssl.h"
28#include "mbedtls/cipher.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020029
Andrzej Kurekeb342242019-01-29 09:14:33 -050030#if defined(MBEDTLS_USE_PSA_CRYPTO)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020031# include "psa/crypto.h"
Andrzej Kurekeb342242019-01-29 09:14:33 -050032#endif
33
Manuel Pégourié-Gonnard56273da2015-05-26 12:19:45 +020034#if defined(MBEDTLS_MD5_C)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020035# include "mbedtls/md5.h"
Manuel Pégourié-Gonnard56273da2015-05-26 12:19:45 +020036#endif
37
38#if defined(MBEDTLS_SHA1_C)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020039# include "mbedtls/sha1.h"
Manuel Pégourié-Gonnard56273da2015-05-26 12:19:45 +020040#endif
41
42#if defined(MBEDTLS_SHA256_C)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020043# include "mbedtls/sha256.h"
Manuel Pégourié-Gonnard56273da2015-05-26 12:19:45 +020044#endif
45
46#if defined(MBEDTLS_SHA512_C)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020047# include "mbedtls/sha512.h"
Manuel Pégourié-Gonnard56273da2015-05-26 12:19:45 +020048#endif
49
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020050#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020051# include "mbedtls/ecjpake.h"
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020052#endif
53
Hanno Beckerdf51dbe2019-02-18 16:41:55 +000054#if defined(MBEDTLS_USE_PSA_CRYPTO)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020055# include "psa/crypto.h"
56# include "mbedtls/psa_util.h"
Hanno Beckerdf51dbe2019-02-18 16:41:55 +000057#endif /* MBEDTLS_USE_PSA_CRYPTO */
58
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020059#if (defined(__ARMCC_VERSION) || defined(_MSC_VER)) && !defined(inline) && \
60 !defined(__cplusplus)
61# define inline __inline
Manuel Pégourié-Gonnard20af64d2015-07-07 18:33:39 +020062#endif
Manuel Pégourié-Gonnard065122c2015-05-26 12:31:46 +020063
Manuel Pégourié-Gonnardcac90a12021-06-04 11:42:30 +020064/* Legacy minor version numbers as defined by:
65 * - RFC 2246: ProtocolVersion version = { 3, 1 }; // TLS v1.0
66 * - RFC 4346: ProtocolVersion version = { 3, 2 }; // TLS v1.1
67 *
68 * We no longer support these versions, but some code still references those
TRodziewicz458280e2021-07-07 11:33:06 +020069 * constants as part of negotiating with the peer, so keep them available
70 * internally.
Manuel Pégourié-Gonnardcac90a12021-06-04 11:42:30 +020071 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020072#define MBEDTLS_SSL_MINOR_VERSION_1 1
73#define MBEDTLS_SSL_MINOR_VERSION_2 2
Manuel Pégourié-Gonnardcac90a12021-06-04 11:42:30 +020074
Manuel Pégourié-Gonnard065122c2015-05-26 12:31:46 +020075/* Determine minimum supported version */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020076#define MBEDTLS_SSL_MIN_MAJOR_VERSION MBEDTLS_SSL_MAJOR_VERSION_3
Manuel Pégourié-Gonnard065122c2015-05-26 12:31:46 +020077
Manuel Pégourié-Gonnard065122c2015-05-26 12:31:46 +020078#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020079# define MBEDTLS_SSL_MIN_MINOR_VERSION MBEDTLS_SSL_MINOR_VERSION_3
Manuel Pégourié-Gonnard065122c2015-05-26 12:31:46 +020080#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard065122c2015-05-26 12:31:46 +020081
TRodziewicz4ca18aa2021-05-20 14:46:20 +020082#define MBEDTLS_SSL_MIN_VALID_MINOR_VERSION MBEDTLS_SSL_MINOR_VERSION_3
Ron Eldor5e9f14d2017-05-28 10:46:38 +030083#define MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION MBEDTLS_SSL_MAJOR_VERSION_3
84
Manuel Pégourié-Gonnard065122c2015-05-26 12:31:46 +020085/* Determine maximum supported version */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020086#define MBEDTLS_SSL_MAX_MAJOR_VERSION MBEDTLS_SSL_MAJOR_VERSION_3
Manuel Pégourié-Gonnard065122c2015-05-26 12:31:46 +020087
88#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020089# define MBEDTLS_SSL_MAX_MINOR_VERSION MBEDTLS_SSL_MINOR_VERSION_3
Manuel Pégourié-Gonnard065122c2015-05-26 12:31:46 +020090#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
91
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020092/* Shorthand for restartable ECC */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020093#if defined(MBEDTLS_ECP_RESTARTABLE) && defined(MBEDTLS_SSL_CLI_C) && \
94 defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +020095 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020096# define MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +020097#endif
98
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020099#define MBEDTLS_SSL_INITIAL_HANDSHAKE 0
100#define MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS 1 /* In progress */
101#define MBEDTLS_SSL_RENEGOTIATION_DONE 2 /* Done or aborted */
102#define MBEDTLS_SSL_RENEGOTIATION_PENDING 3 /* Requested (server only) */
Manuel Pégourié-Gonnard065122c2015-05-26 12:31:46 +0200103
104/*
105 * DTLS retransmission states, see RFC 6347 4.2.4
106 *
107 * The SENDING state is merged in PREPARING for initial sends,
108 * but is distinct for resends.
109 *
110 * Note: initial state is wrong for server, but is not used anyway.
111 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200112#define MBEDTLS_SSL_RETRANS_PREPARING 0
113#define MBEDTLS_SSL_RETRANS_SENDING 1
114#define MBEDTLS_SSL_RETRANS_WAITING 2
115#define MBEDTLS_SSL_RETRANS_FINISHED 3
Manuel Pégourié-Gonnard065122c2015-05-26 12:31:46 +0200116
117/*
118 * Allow extra bytes for record, authentication and encryption overhead:
Mateusz Starzyka3a99842021-02-19 14:27:22 +0100119 * counter (8) + header (5) + IV(16) + MAC (16-48) + padding (0-256).
Manuel Pégourié-Gonnard065122c2015-05-26 12:31:46 +0200120 */
Manuel Pégourié-Gonnard065122c2015-05-26 12:31:46 +0200121
TRodziewicz4ca18aa2021-05-20 14:46:20 +0200122#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker0cc46612020-11-30 08:56:52 +0000123
Manuel Pégourié-Gonnard05579c42020-07-31 12:53:39 +0200124/* This macro determines whether CBC is supported. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200125# if defined(MBEDTLS_CIPHER_MODE_CBC) && \
126 (defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || \
127 defined(MBEDTLS_ARIA_C) || defined(MBEDTLS_DES_C))
128# define MBEDTLS_SSL_SOME_SUITES_USE_CBC
129# endif
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +0200130
Hanno Becker0cc46612020-11-30 08:56:52 +0000131/* This macro determines whether a ciphersuite using a
132 * stream cipher can be used. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200133# if defined(MBEDTLS_CIPHER_NULL_CIPHER)
134# define MBEDTLS_SSL_SOME_SUITES_USE_STREAM
135# endif
Hanno Becker0cc46612020-11-30 08:56:52 +0000136
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200137/* This macro determines whether the CBC construct used in TLS 1.2 is supported.
138 */
139# if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC) && \
140 defined(MBEDTLS_SSL_PROTO_TLS1_2)
141# define MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC
142# endif
Manuel Pégourié-Gonnarded0e8642020-07-21 11:20:30 +0200143
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200144# if defined(MBEDTLS_SSL_SOME_SUITES_USE_STREAM) || \
145 defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC)
146# define MBEDTLS_SSL_SOME_SUITES_USE_MAC
147# endif
Hanno Becker52344c22018-01-03 15:24:20 +0000148
TRodziewicz4ca18aa2021-05-20 14:46:20 +0200149#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker0cc46612020-11-30 08:56:52 +0000150
Hanno Beckerfd86ca82020-11-30 08:54:23 +0000151#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Manuel Pégourié-Gonnard065122c2015-05-26 12:31:46 +0200152/* Ciphersuites using HMAC */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200153# if defined(MBEDTLS_SHA384_C)
154# define MBEDTLS_SSL_MAC_ADD 48 /* SHA-384 used for HMAC */
155# elif defined(MBEDTLS_SHA256_C)
156# define MBEDTLS_SSL_MAC_ADD 32 /* SHA-256 used for HMAC */
157# else
158# define MBEDTLS_SSL_MAC_ADD 20 /* SHA-1 used for HMAC */
159# endif
Hanno Beckerfd86ca82020-11-30 08:54:23 +0000160#else /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Manuel Pégourié-Gonnard065122c2015-05-26 12:31:46 +0200161/* AEAD ciphersuites: GCM and CCM use a 128 bits tag */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200162# define MBEDTLS_SSL_MAC_ADD 16
Manuel Pégourié-Gonnard065122c2015-05-26 12:31:46 +0200163#endif
164
165#if defined(MBEDTLS_CIPHER_MODE_CBC)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200166# define MBEDTLS_SSL_PADDING_ADD 256
Manuel Pégourié-Gonnard065122c2015-05-26 12:31:46 +0200167#else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200168# define MBEDTLS_SSL_PADDING_ADD 0
Manuel Pégourié-Gonnard065122c2015-05-26 12:31:46 +0200169#endif
170
Hanno Beckera0e20d02019-05-15 14:03:01 +0100171#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200172# define MBEDTLS_SSL_MAX_CID_EXPANSION \
173 MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY
Hanno Becker6cbad552019-05-08 15:40:11 +0100174#else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200175# define MBEDTLS_SSL_MAX_CID_EXPANSION 0
Hanno Becker6cbad552019-05-08 15:40:11 +0100176#endif
177
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200178#define MBEDTLS_SSL_PAYLOAD_OVERHEAD \
179 (MBEDTLS_MAX_IV_LENGTH + MBEDTLS_SSL_MAC_ADD + MBEDTLS_SSL_PADDING_ADD + \
180 MBEDTLS_SSL_MAX_CID_EXPANSION)
Angus Grattond8213d02016-05-25 20:56:48 +1000181
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200182#define MBEDTLS_SSL_IN_PAYLOAD_LEN \
183 (MBEDTLS_SSL_PAYLOAD_OVERHEAD + (MBEDTLS_SSL_IN_CONTENT_LEN))
Angus Grattond8213d02016-05-25 20:56:48 +1000184
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200185#define MBEDTLS_SSL_OUT_PAYLOAD_LEN \
186 (MBEDTLS_SSL_PAYLOAD_OVERHEAD + (MBEDTLS_SSL_OUT_CONTENT_LEN))
Angus Grattond8213d02016-05-25 20:56:48 +1000187
Hanno Becker0271f962018-08-16 13:23:47 +0100188/* The maximum number of buffered handshake messages. */
Hanno Beckerd488b9e2018-08-16 16:35:37 +0100189#define MBEDTLS_SSL_MAX_BUFFERED_HS 4
Hanno Becker0271f962018-08-16 13:23:47 +0100190
Angus Grattond8213d02016-05-25 20:56:48 +1000191/* Maximum length we can advertise as our max content length for
192 RFC 6066 max_fragment_length extension negotiation purposes
193 (the lesser of both sizes, if they are unequal.)
194 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200195#define MBEDTLS_TLS_EXT_ADV_CONTENT_LEN \
196 ((MBEDTLS_SSL_IN_CONTENT_LEN > MBEDTLS_SSL_OUT_CONTENT_LEN) ? \
197 (MBEDTLS_SSL_OUT_CONTENT_LEN) : \
198 (MBEDTLS_SSL_IN_CONTENT_LEN))
Manuel Pégourié-Gonnard065122c2015-05-26 12:31:46 +0200199
Hanno Beckere131bfe2017-04-12 14:54:42 +0100200/* Maximum size in bytes of list in sig-hash algorithm ext., RFC 5246 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200201#define MBEDTLS_SSL_MAX_SIG_HASH_ALG_LIST_LEN 65534
Hanno Beckere131bfe2017-04-12 14:54:42 +0100202
203/* Maximum size in bytes of list in supported elliptic curve ext., RFC 4492 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200204#define MBEDTLS_SSL_MAX_CURVE_LIST_LEN 65535
Hanno Beckere131bfe2017-04-12 14:54:42 +0100205
Manuel Pégourié-Gonnard065122c2015-05-26 12:31:46 +0200206/*
Hanno Beckera8434e82017-09-18 10:54:39 +0100207 * Check that we obey the standard's message size bounds
208 */
209
David Horstmann95d516f2021-05-04 18:36:56 +0100210#if MBEDTLS_SSL_IN_CONTENT_LEN > 16384
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200211# error "Bad configuration - incoming record content too large."
Hanno Beckera8434e82017-09-18 10:54:39 +0100212#endif
213
David Horstmann95d516f2021-05-04 18:36:56 +0100214#if MBEDTLS_SSL_OUT_CONTENT_LEN > 16384
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200215# error "Bad configuration - outgoing record content too large."
Hanno Beckera8434e82017-09-18 10:54:39 +0100216#endif
217
David Horstmann95d516f2021-05-04 18:36:56 +0100218#if MBEDTLS_SSL_IN_PAYLOAD_LEN > MBEDTLS_SSL_IN_CONTENT_LEN + 2048
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200219# error "Bad configuration - incoming protected record payload too large."
Angus Grattond8213d02016-05-25 20:56:48 +1000220#endif
221
David Horstmann95d516f2021-05-04 18:36:56 +0100222#if MBEDTLS_SSL_OUT_PAYLOAD_LEN > MBEDTLS_SSL_OUT_CONTENT_LEN + 2048
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200223# error "Bad configuration - outgoing protected record payload too large."
Angus Grattond8213d02016-05-25 20:56:48 +1000224#endif
225
226/* Calculate buffer sizes */
227
Hanno Becker25d6d1a2017-12-07 08:22:51 +0000228/* Note: Even though the TLS record header is only 5 bytes
229 long, we're internally using 8 bytes to store the
230 implicit sequence number. */
Hanno Beckerd25d4442017-10-04 13:56:42 +0100231#define MBEDTLS_SSL_HEADER_LEN 13
Hanno Beckera8434e82017-09-18 10:54:39 +0100232
Andrzej Kurek033c42a2020-03-03 05:57:59 -0500233#if !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200234# define MBEDTLS_SSL_IN_BUFFER_LEN \
235 ((MBEDTLS_SSL_HEADER_LEN) + (MBEDTLS_SSL_IN_PAYLOAD_LEN))
Hanno Becker6cbad552019-05-08 15:40:11 +0100236#else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200237# define MBEDTLS_SSL_IN_BUFFER_LEN \
238 ((MBEDTLS_SSL_HEADER_LEN) + (MBEDTLS_SSL_IN_PAYLOAD_LEN) + \
239 (MBEDTLS_SSL_CID_IN_LEN_MAX))
Hanno Becker6cbad552019-05-08 15:40:11 +0100240#endif
Angus Grattond8213d02016-05-25 20:56:48 +1000241
Andrzej Kurek033c42a2020-03-03 05:57:59 -0500242#if !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200243# define MBEDTLS_SSL_OUT_BUFFER_LEN \
244 ((MBEDTLS_SSL_HEADER_LEN) + (MBEDTLS_SSL_OUT_PAYLOAD_LEN))
Hanno Becker6cbad552019-05-08 15:40:11 +0100245#else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200246# define MBEDTLS_SSL_OUT_BUFFER_LEN \
247 ((MBEDTLS_SSL_HEADER_LEN) + (MBEDTLS_SSL_OUT_PAYLOAD_LEN) + \
248 (MBEDTLS_SSL_CID_OUT_LEN_MAX))
Hanno Becker6cbad552019-05-08 15:40:11 +0100249#endif
Angus Grattond8213d02016-05-25 20:56:48 +1000250
Hanno Becker9752aad2021-04-21 05:54:33 +0100251#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
252/**
253 * \brief Return the maximum fragment length (payload, in bytes) for
254 * the output buffer. For the client, this is the configured
255 * value. For the server, it is the minimum of two - the
256 * configured value and the negotiated one.
257 *
258 * \sa mbedtls_ssl_conf_max_frag_len()
259 * \sa mbedtls_ssl_get_max_out_record_payload()
260 *
261 * \param ssl SSL context
262 *
263 * \return Current maximum fragment length for the output buffer.
264 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200265size_t mbedtls_ssl_get_output_max_frag_len(const mbedtls_ssl_context *ssl);
Hanno Becker9752aad2021-04-21 05:54:33 +0100266
267/**
268 * \brief Return the maximum fragment length (payload, in bytes) for
269 * the input buffer. This is the negotiated maximum fragment
Hanno Beckerdf3b8632021-06-08 05:30:45 +0100270 * length, or, if there is none, MBEDTLS_SSL_IN_CONTENT_LEN.
Hanno Becker9752aad2021-04-21 05:54:33 +0100271 * If it is not defined either, the value is 2^14. This function
272 * works as its predecessor, \c mbedtls_ssl_get_max_frag_len().
273 *
274 * \sa mbedtls_ssl_conf_max_frag_len()
275 * \sa mbedtls_ssl_get_max_in_record_payload()
276 *
277 * \param ssl SSL context
278 *
279 * \return Current maximum fragment length for the output buffer.
280 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200281size_t mbedtls_ssl_get_input_max_frag_len(const mbedtls_ssl_context *ssl);
Hanno Becker9752aad2021-04-21 05:54:33 +0100282#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
283
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500284#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200285static inline size_t
286mbedtls_ssl_get_output_buflen(const mbedtls_ssl_context *ctx)
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500287{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200288# if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
289 return mbedtls_ssl_get_output_max_frag_len(ctx) + MBEDTLS_SSL_HEADER_LEN +
290 MBEDTLS_SSL_PAYLOAD_OVERHEAD + MBEDTLS_SSL_CID_OUT_LEN_MAX;
291# else
292 return mbedtls_ssl_get_output_max_frag_len(ctx) + MBEDTLS_SSL_HEADER_LEN +
293 MBEDTLS_SSL_PAYLOAD_OVERHEAD;
294# endif
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500295}
296
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200297static inline size_t
298mbedtls_ssl_get_input_buflen(const mbedtls_ssl_context *ctx)
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500299{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200300# if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
301 return mbedtls_ssl_get_input_max_frag_len(ctx) + MBEDTLS_SSL_HEADER_LEN +
302 MBEDTLS_SSL_PAYLOAD_OVERHEAD + MBEDTLS_SSL_CID_IN_LEN_MAX;
303# else
304 return mbedtls_ssl_get_input_max_frag_len(ctx) + MBEDTLS_SSL_HEADER_LEN +
305 MBEDTLS_SSL_PAYLOAD_OVERHEAD;
306# endif
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500307}
308#endif
309
Hanno Beckera8434e82017-09-18 10:54:39 +0100310/*
Manuel Pégourié-Gonnard065122c2015-05-26 12:31:46 +0200311 * TLS extension flags (for extensions with outgoing ServerHello content
312 * that need it (e.g. for RENEGOTIATION_INFO the server already knows because
313 * of state of the renegotiation flag, so no indicator is required)
314 */
315#define MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT (1 << 0)
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +0200316#define MBEDTLS_TLS_EXT_ECJPAKE_KKPP_OK (1 << 1)
Manuel Pégourié-Gonnard065122c2015-05-26 12:31:46 +0200317
Hanno Becker51018aa2017-04-12 14:54:42 +0100318/**
319 * \brief This function checks if the remaining size in a buffer is
320 * greater or equal than a needed space.
321 *
322 * \param cur Pointer to the current position in the buffer.
323 * \param end Pointer to one past the end of the buffer.
324 * \param need Needed space in bytes.
325 *
Ronald Cronb7b35e12020-06-11 09:50:51 +0200326 * \return Zero if the needed space is available in the buffer, non-zero
Hanno Becker51018aa2017-04-12 14:54:42 +0100327 * otherwise.
328 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200329static inline int
330mbedtls_ssl_chk_buf_ptr(const uint8_t *cur, const uint8_t *end, size_t need)
Hanno Becker51018aa2017-04-12 14:54:42 +0100331{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200332 return ((cur > end) || (need > (size_t)(end - cur)));
Hanno Becker51018aa2017-04-12 14:54:42 +0100333}
334
335/**
336 * \brief This macro checks if the remaining size in a buffer is
337 * greater or equal than a needed space. If it is not the case,
338 * it returns an SSL_BUFFER_TOO_SMALL error.
339 *
340 * \param cur Pointer to the current position in the buffer.
341 * \param end Pointer to one past the end of the buffer.
342 * \param need Needed space in bytes.
343 *
344 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200345#define MBEDTLS_SSL_CHK_BUF_PTR(cur, end, need) \
346 do { \
347 if (mbedtls_ssl_chk_buf_ptr((cur), (end), (need)) != 0) { \
348 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; \
349 } \
350 } while (0)
Hanno Becker51018aa2017-04-12 14:54:42 +0100351
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +0200352#ifdef __cplusplus
353extern "C" {
354#endif
355
Hanno Becker7e5437a2017-04-28 17:15:26 +0100356#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +0100357 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +0100358/*
359 * Abstraction for a grid of allowed signature-hash-algorithm pairs.
360 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200361struct mbedtls_ssl_sig_hash_set_t {
Hanno Becker7e5437a2017-04-28 17:15:26 +0100362 /* At the moment, we only need to remember a single suitable
363 * hash algorithm per signature algorithm. As long as that's
364 * the case - and we don't need a general lookup function -
365 * we can implement the sig-hash-set as a map from signatures
366 * to hash algorithms. */
367 mbedtls_md_type_t rsa;
368 mbedtls_md_type_t ecdsa;
369};
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200370#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && \
Gilles Peskineeccd8882020-03-10 12:19:08 +0100371 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Hanno Becker7e5437a2017-04-28 17:15:26 +0100372
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200373typedef int mbedtls_ssl_tls_prf_cb(const unsigned char *secret,
374 size_t slen,
375 const char *label,
376 const unsigned char *random,
377 size_t rlen,
378 unsigned char *dstbuf,
379 size_t dlen);
Hanno Becker3385a4d2020-08-21 13:03:34 +0100380
Hanno Becker61baae72020-09-16 09:24:14 +0100381/* cipher.h exports the maximum IV, key and block length from
Hanno Becker15889832020-09-08 11:29:11 +0100382 * all ciphers enabled in the config, regardless of whether those
383 * ciphers are actually usable in SSL/TLS. Notably, XTS is enabled
384 * in the default configuration and uses 64 Byte keys, but it is
385 * not used for record protection in SSL/TLS.
386 *
387 * In order to prevent unnecessary inflation of key structures,
388 * we introduce SSL-specific variants of the max-{key,block,IV}
389 * macros here which are meant to only take those ciphers into
390 * account which can be negotiated in SSL/TLS.
391 *
392 * Since the current definitions of MBEDTLS_MAX_{KEY|BLOCK|IV}_LENGTH
393 * in cipher.h are rough overapproximations of the real maxima, here
Hanno Becker9a7a2ac2020-09-09 09:24:54 +0100394 * we content ourselves with replicating those overapproximations
Hanno Becker15889832020-09-08 11:29:11 +0100395 * for the maximum block and IV length, and excluding XTS from the
396 * computation of the maximum key length. */
397#define MBEDTLS_SSL_MAX_BLOCK_LENGTH 16
398#define MBEDTLS_SSL_MAX_IV_LENGTH 16
399#define MBEDTLS_SSL_MAX_KEY_LENGTH 32
400
Hanno Becker3385a4d2020-08-21 13:03:34 +0100401/**
402 * \brief The data structure holding the cryptographic material (key and IV)
403 * used for record protection in TLS 1.3.
404 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200405struct mbedtls_ssl_key_set {
Hanno Becker3385a4d2020-08-21 13:03:34 +0100406 /*! The key for client->server records. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200407 unsigned char client_write_key[MBEDTLS_SSL_MAX_KEY_LENGTH];
Hanno Becker3385a4d2020-08-21 13:03:34 +0100408 /*! The key for server->client records. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200409 unsigned char server_write_key[MBEDTLS_SSL_MAX_KEY_LENGTH];
Hanno Becker3385a4d2020-08-21 13:03:34 +0100410 /*! The IV for client->server records. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200411 unsigned char client_write_iv[MBEDTLS_SSL_MAX_IV_LENGTH];
Hanno Becker3385a4d2020-08-21 13:03:34 +0100412 /*! The IV for server->client records. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200413 unsigned char server_write_iv[MBEDTLS_SSL_MAX_IV_LENGTH];
Hanno Becker3385a4d2020-08-21 13:03:34 +0100414
Hanno Becker493ea7f2020-09-08 11:01:00 +0100415 size_t key_len; /*!< The length of client_write_key and
416 * server_write_key, in Bytes. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200417 size_t iv_len; /*!< The length of client_write_iv and
418 * server_write_iv, in Bytes. */
Hanno Becker3385a4d2020-08-21 13:03:34 +0100419};
420typedef struct mbedtls_ssl_key_set mbedtls_ssl_key_set;
Hanno Becker3385a4d2020-08-21 13:03:34 +0100421
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200422/*
423 * This structure contains the parameters only needed during handshake.
424 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200425struct mbedtls_ssl_handshake_params {
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200426 /*
427 * Handshake specific crypto variables
428 */
Hanno Becker7e5437a2017-04-28 17:15:26 +0100429
430#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +0100431 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200432 mbedtls_ssl_sig_hash_set_t hash_algs; /*!< Set of suitable sig-hash pairs
433 */
Hanno Becker7e5437a2017-04-28 17:15:26 +0100434#endif
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200435#if defined(MBEDTLS_DHM_C)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200436 mbedtls_dhm_context dhm_ctx; /*!< DHM key exchange */
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200437#endif
John Durkop07cc04a2020-11-16 22:08:34 -0800438/* Adding guard for MBEDTLS_ECDSA_C to ensure no compile errors due
439 * to guards also being in ssl_srv.c and ssl_cli.c. There is a gap
440 * in functionality that access to ecdh_ctx structure is needed for
441 * MBEDTLS_ECDSA_C which does not seem correct.
442 */
443#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200444 mbedtls_ecdh_context ecdh_ctx; /*!< ECDH key exchange */
Hanno Beckerdf51dbe2019-02-18 16:41:55 +0000445
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200446# if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine42459802019-12-19 13:31:53 +0100447 psa_key_type_t ecdh_psa_type;
448 uint16_t ecdh_bits;
Ronald Croncf56a0a2020-08-04 09:51:30 +0200449 psa_key_id_t ecdh_psa_privkey;
Hanno Beckerdf51dbe2019-02-18 16:41:55 +0000450 unsigned char ecdh_psa_peerkey[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
451 size_t ecdh_psa_peerkey_len;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200452# endif /* MBEDTLS_USE_PSA_CRYPTO */
John Durkop07cc04a2020-11-16 22:08:34 -0800453#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C */
Hanno Beckerdf51dbe2019-02-18 16:41:55 +0000454
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200455#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200456 mbedtls_ecjpake_context ecjpake_ctx; /*!< EC J-PAKE key exchange */
457# if defined(MBEDTLS_SSL_CLI_C)
458 unsigned char *ecjpake_cache; /*!< Cache for ClientHello ext */
459 size_t ecjpake_cache_len; /*!< Length of cached data */
460# endif
Hanno Becker1aa267c2017-04-28 17:08:27 +0100461#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnardf4721792015-09-15 10:53:51 +0200462#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200463 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200464 const mbedtls_ecp_curve_info **curves; /*!< Supported elliptic curves */
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200465#endif
Gilles Peskineeccd8882020-03-10 12:19:08 +0100466#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200467# if defined(MBEDTLS_USE_PSA_CRYPTO)
468 psa_key_id_t psk_opaque; /*!< Opaque PSK from the callback */
469# endif /* MBEDTLS_USE_PSA_CRYPTO */
470 unsigned char *psk; /*!< PSK from the callback */
471 size_t psk_len; /*!< Length of PSK from callback */
Gilles Peskineeccd8882020-03-10 12:19:08 +0100472#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200473#if defined(MBEDTLS_X509_CRT_PARSE_C)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200474 mbedtls_ssl_key_cert *key_cert; /*!< chosen key/cert pair (server) */
475# if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
476 int sni_authmode; /*!< authmode from SNI callback */
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200477 mbedtls_ssl_key_cert *sni_key_cert; /*!< key/cert list from SNI */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200478 mbedtls_x509_crt *sni_ca_chain; /*!< trusted CAs from SNI callback */
479 mbedtls_x509_crl *sni_ca_crl; /*!< trusted CAs CRLs from SNI */
480# endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200481#endif /* MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskineeccd8882020-03-10 12:19:08 +0100482#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200483 int ecrs_enabled; /*!< Handshake supports EC restart? */
484 mbedtls_x509_crt_restart_ctx ecrs_ctx; /*!< restart context */
485 enum
486 { /* this complements ssl->state with info on intra-state operations */
487 ssl_ecrs_none = 0, /*!< nothing going on (yet) */
488 ssl_ecrs_crt_verify, /*!< Certificate: crt_verify() */
489 ssl_ecrs_ske_start_processing, /*!< ServerKeyExchange: pk_verify() */
490 ssl_ecrs_cke_ecdh_calc_secret, /*!< ClientKeyExchange: ECDH step 2 */
491 ssl_ecrs_crt_vrfy_sign, /*!< CertificateVerify: pk_sign() */
492 } ecrs_state; /*!< current (or last) operation */
493 mbedtls_x509_crt *ecrs_peer_cert; /*!< The peer's CRT chain. */
494 size_t ecrs_n; /*!< place for saving a length */
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +0200495#endif
Hanno Becker75173122019-02-06 16:18:31 +0000496#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
497 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200498 mbedtls_pk_context peer_pubkey; /*!< The public key from the peer. */
Hanno Becker75173122019-02-06 16:18:31 +0000499#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200500#if defined(MBEDTLS_SSL_PROTO_DTLS)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200501 unsigned int out_msg_seq; /*!< Outgoing handshake sequence number */
502 unsigned int in_msg_seq; /*!< Incoming handshake sequence number */
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200503
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200504 unsigned char *verify_cookie; /*!< Cli: HelloVerifyRequest cookie
505 Srv: unused */
506 unsigned char verify_cookie_len; /*!< Cli: cookie length
507 Srv: flag for sending a cookie */
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200508
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200509 uint32_t retransmit_timeout; /*!< Current value of timeout */
510 unsigned char retransmit_state; /*!< Retransmission state */
511 mbedtls_ssl_flight_item *flight; /*!< Current outgoing flight */
512 mbedtls_ssl_flight_item *cur_msg; /*!< Current message in flight */
513 unsigned char *cur_msg_p; /*!< Position in current message */
514 unsigned int in_flight_start_seq; /*!< Minimum message sequence in the
515 flight being received */
516 mbedtls_ssl_transform *alt_transform_out; /*!< Alternative transform for
517 resending messages */
518 unsigned char alt_out_ctr[8]; /*!< Alternative record epoch/counter
519 for resending messages */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +0100520
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200521# if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker2f28c102019-04-25 15:46:59 +0100522 /* The state of CID configuration in this handshake. */
523
524 uint8_t cid_in_use; /*!< This indicates whether the use of the CID extension
Hanno Beckerf1a28082019-05-15 10:17:48 +0100525 * has been negotiated. Possible values are
Hanno Becker2f28c102019-04-25 15:46:59 +0100526 * #MBEDTLS_SSL_CID_ENABLED and
527 * #MBEDTLS_SSL_CID_DISABLED. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200528 unsigned char peer_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX]; /*! The peer's CID */
529 uint8_t peer_cid_len; /*!< The length of
530 * \c peer_cid. */
531# endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker2f28c102019-04-25 15:46:59 +0100532
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200533 struct {
Hanno Beckere0b150f2018-08-21 15:51:03 +0100534 size_t total_bytes_buffered; /*!< Cumulative size of heap allocated
535 * buffers used for message buffering. */
536
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200537 uint8_t seen_ccs; /*!< Indicates if a CCS message has
538 * been seen in the current flight. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +0100539
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200540 struct mbedtls_ssl_hs_buffer {
Hanno Becker98081a02018-08-22 13:32:50 +0100541 unsigned is_valid : 1;
542 unsigned is_fragmented : 1;
543 unsigned is_complete : 1;
Hanno Becker0271f962018-08-16 13:23:47 +0100544 unsigned char *data;
Hanno Beckere0b150f2018-08-21 15:51:03 +0100545 size_t data_len;
Hanno Becker0271f962018-08-16 13:23:47 +0100546 } hs[MBEDTLS_SSL_MAX_BUFFERED_HS];
547
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200548 struct {
Hanno Becker5f066e72018-08-16 14:56:31 +0100549 unsigned char *data;
550 size_t len;
551 unsigned epoch;
552 } future_record;
553
Hanno Beckerd7f8ae22018-08-16 09:45:56 +0100554 } buffering;
Hanno Becker35462012018-08-22 10:25:40 +0100555
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200556 uint16_t mtu; /*!< Handshake mtu, used to fragment outgoing messages */
Hanno Becker1aa267c2017-04-28 17:08:27 +0100557#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200558
559 /*
560 * Checksum contexts
561 */
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200562#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200563# if defined(MBEDTLS_SHA256_C)
564# if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500565 psa_hash_operation_t fin_sha256_psa;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200566# else
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200567 mbedtls_sha256_context fin_sha256;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200568# endif
569# endif
570# if defined(MBEDTLS_SHA384_C)
571# if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -0500572 psa_hash_operation_t fin_sha384_psa;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200573# else
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200574 mbedtls_sha512_context fin_sha512;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200575# endif
576# endif
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200577#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
578
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200579 void (*update_checksum)(mbedtls_ssl_context *,
580 const unsigned char *,
581 size_t);
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +0200582 void (*calc_verify)(const mbedtls_ssl_context *, unsigned char *, size_t *);
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200583 void (*calc_finished)(mbedtls_ssl_context *, unsigned char *, int);
Ron Eldor51d3ab52019-05-12 14:54:30 +0300584 mbedtls_ssl_tls_prf_cb *tls_prf;
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200585
Hanno Beckere694c3e2017-12-27 21:34:08 +0000586 mbedtls_ssl_ciphersuite_t const *ciphersuite_info;
587
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200588 size_t pmslen; /*!< premaster length */
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200589
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200590 unsigned char randbytes[64]; /*!< random bytes */
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200591 unsigned char premaster[MBEDTLS_PREMASTER_SIZE];
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200592 /*!< premaster secret */
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200593
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200594 int resume; /*!< session resume indicator*/
595 int max_major_ver; /*!< max. major version client*/
596 int max_minor_ver; /*!< max. minor version client*/
597 int cli_exts; /*!< client extension presence*/
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200598
599#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200600 int new_session_ticket; /*!< use NewSessionTicket? */
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200601#endif /* MBEDTLS_SSL_SESSION_TICKETS */
602#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200603 int extended_ms; /*!< use Extended Master Secret? */
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200604#endif
Gilles Peskinedf13d5c2018-04-25 20:39:48 +0200605
606#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200607 unsigned int async_in_progress : 1; /*!< an asynchronous operation is in
608 progress */
Gilles Peskinedf13d5c2018-04-25 20:39:48 +0200609#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
610
611#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
612 /** Asynchronous operation context. This field is meant for use by the
613 * asynchronous operation callbacks (mbedtls_ssl_config::f_async_sign_start,
614 * mbedtls_ssl_config::f_async_decrypt_start,
615 * mbedtls_ssl_config::f_async_resume, mbedtls_ssl_config::f_async_cancel).
616 * The library does not use it internally. */
617 void *user_async_ctx;
618#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200619};
620
Hanno Becker0271f962018-08-16 13:23:47 +0100621typedef struct mbedtls_ssl_hs_buffer mbedtls_ssl_hs_buffer;
622
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200623/*
Hanno Beckerd362dc52018-01-03 15:23:11 +0000624 * Representation of decryption/encryption transformations on records
625 *
626 * There are the following general types of record transformations:
TRodziewicz2abf03c2021-06-25 14:40:09 +0200627 * - Stream transformations (TLS versions == 1.2 only)
Hanno Beckerd362dc52018-01-03 15:23:11 +0000628 * Transformation adding a MAC and applying a stream-cipher
629 * to the authenticated message.
TRodziewicz2abf03c2021-06-25 14:40:09 +0200630 * - CBC block cipher transformations ([D]TLS versions == 1.2 only)
631 * For TLS 1.2, no IV is generated at key extraction time, but every
632 * encrypted record is explicitly prefixed by the IV with which it was
633 * encrypted.
634 * - AEAD transformations ([D]TLS versions == 1.2 only)
Hanno Beckerd362dc52018-01-03 15:23:11 +0000635 * These come in two fundamentally different versions, the first one
636 * used in TLS 1.2, excluding ChaChaPoly ciphersuites, and the second
637 * one used for ChaChaPoly ciphersuites in TLS 1.2 as well as for TLS 1.3.
638 * In the first transformation, the IV to be used for a record is obtained
639 * as the concatenation of an explicit, static 4-byte IV and the 8-byte
640 * record sequence number, and explicitly prepending this sequence number
641 * to the encrypted record. In contrast, in the second transformation
642 * the IV is obtained by XOR'ing a static IV obtained at key extraction
643 * time with the 8-byte record sequence number, without prepending the
644 * latter to the encrypted record.
645 *
Hanno Becker7d343ec2020-05-04 12:29:05 +0100646 * Additionally, DTLS 1.2 + CID as well as TLS 1.3 use an inner plaintext
647 * which allows to add flexible length padding and to hide a record's true
648 * content type.
649 *
Hanno Beckerd362dc52018-01-03 15:23:11 +0000650 * In addition to type and version, the following parameters are relevant:
651 * - The symmetric cipher algorithm to be used.
652 * - The (static) encryption/decryption keys for the cipher.
653 * - For stream/CBC, the type of message digest to be used.
654 * - For stream/CBC, (static) encryption/decryption keys for the digest.
Hanno Becker0db7e0c2018-10-18 15:39:53 +0100655 * - For AEAD transformations, the size (potentially 0) of an explicit,
656 * random initialization vector placed in encrypted records.
TRodziewicz299510e2021-07-09 16:55:11 +0200657 * - For some transformations (currently AEAD) an implicit IV. It is static
Hanno Beckerd362dc52018-01-03 15:23:11 +0000658 * and (if present) is combined with the explicit IV in a transformation-
TRodziewicz299510e2021-07-09 16:55:11 +0200659 * -dependent way (e.g. appending in TLS 1.2 and XOR'ing in TLS 1.3).
Hanno Beckerd362dc52018-01-03 15:23:11 +0000660 * - For stream/CBC, a flag determining the order of encryption and MAC.
661 * - The details of the transformation depend on the SSL/TLS version.
662 * - The length of the authentication tag.
663 *
664 * The struct below refines this abstract view as follows:
665 * - The cipher underlying the transformation is managed in
666 * cipher contexts cipher_ctx_{enc/dec}, which must have the
667 * same cipher type. The mode of these cipher contexts determines
668 * the type of the transformation in the sense above: e.g., if
669 * the type is MBEDTLS_CIPHER_AES_256_CBC resp. MBEDTLS_CIPHER_AES_192_GCM
670 * then the transformation has type CBC resp. AEAD.
671 * - The cipher keys are never stored explicitly but
672 * are maintained within cipher_ctx_{enc/dec}.
673 * - For stream/CBC transformations, the message digest contexts
674 * used for the MAC's are stored in md_ctx_{enc/dec}. These contexts
675 * are unused for AEAD transformations.
TRodziewicz2abf03c2021-06-25 14:40:09 +0200676 * - For stream/CBC transformations, the MAC keys are not stored explicitly
677 * but maintained within md_ctx_{enc/dec}.
678 * - The mac_enc and mac_dec fields are unused for EAD transformations.
Hanno Beckerd362dc52018-01-03 15:23:11 +0000679 * - For transformations using an implicit IV maintained within
680 * the transformation context, its contents are stored within
681 * iv_{enc/dec}.
682 * - The value of ivlen indicates the length of the IV.
683 * This is redundant in case of stream/CBC transformations
684 * which always use 0 resp. the cipher's block length as the
685 * IV length, but is needed for AEAD ciphers and may be
686 * different from the underlying cipher's block length
687 * in this case.
688 * - The field fixed_ivlen is nonzero for AEAD transformations only
689 * and indicates the length of the static part of the IV which is
690 * constant throughout the communication, and which is stored in
691 * the first fixed_ivlen bytes of the iv_{enc/dec} arrays.
Hanno Beckerd362dc52018-01-03 15:23:11 +0000692 * - minor_ver denotes the SSL/TLS version
693 * - For stream/CBC transformations, maclen denotes the length of the
694 * authentication tag, while taglen is unused and 0.
695 * - For AEAD transformations, taglen denotes the length of the
696 * authentication tag, while maclen is unused and 0.
697 * - For CBC transformations, encrypt_then_mac determines the
698 * order of encryption and authentication. This field is unused
699 * in other transformations.
700 *
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200701 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200702struct mbedtls_ssl_transform {
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200703 /*
704 * Session specific crypto layer
705 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200706 size_t minlen; /*!< min. ciphertext length */
707 size_t ivlen; /*!< IV length */
708 size_t fixed_ivlen; /*!< Fixed part of IV (AEAD) */
709 size_t maclen; /*!< MAC(CBC) len */
710 size_t taglen; /*!< TAG(AEAD) len */
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200711
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200712 unsigned char iv_enc[16]; /*!< IV (encryption) */
713 unsigned char iv_dec[16]; /*!< IV (decryption) */
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200714
Hanno Beckerfd86ca82020-11-30 08:54:23 +0000715#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Hanno Beckerd56ed242018-01-03 15:32:51 +0000716
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200717 mbedtls_md_context_t md_ctx_enc; /*!< MAC (encryption) */
718 mbedtls_md_context_t md_ctx_dec; /*!< MAC (decryption) */
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200719
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200720# if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
721 int encrypt_then_mac; /*!< flag for EtM activation */
722# endif
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000723
Hanno Beckerfd86ca82020-11-30 08:54:23 +0000724#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Hanno Beckerd56ed242018-01-03 15:32:51 +0000725
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200726 mbedtls_cipher_context_t cipher_ctx_enc; /*!< encryption context */
727 mbedtls_cipher_context_t cipher_ctx_dec; /*!< decryption context */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000728 int minor_ver;
729
Hanno Beckera0e20d02019-05-15 14:03:01 +0100730#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker1327fa72019-04-25 15:54:02 +0100731 uint8_t in_cid_len;
732 uint8_t out_cid_len;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200733 unsigned char in_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX];
734 unsigned char out_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX];
Hanno Beckera0e20d02019-05-15 14:03:01 +0100735#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1327fa72019-04-25 15:54:02 +0100736
Manuel Pégourié-Gonnard96fb0ee2019-07-09 12:54:17 +0200737#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
738 /* We need the Hello random bytes in order to re-derive keys from the
739 * Master Secret and other session info, see ssl_populate_transform() */
740 unsigned char randbytes[64]; /*!< ServerHello.random+ClientHello.random */
741#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200742};
743
Hanno Becker12a3a862018-01-05 15:42:50 +0000744/*
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +0200745 * Return 1 if the transform uses an AEAD cipher, 0 otherwise.
746 * Equivalently, return 0 if a separate MAC is used, 1 otherwise.
747 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200748static inline int
749mbedtls_ssl_transform_uses_aead(const mbedtls_ssl_transform *transform)
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +0200750{
Hanno Beckerfd86ca82020-11-30 08:54:23 +0000751#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200752 return transform->maclen == 0 && transform->taglen != 0;
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +0200753#else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200754 (void)transform;
755 return 1;
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +0200756#endif
757}
758
759/*
Hanno Becker12a3a862018-01-05 15:42:50 +0000760 * Internal representation of record frames
761 *
Hanno Becker12a3a862018-01-05 15:42:50 +0000762 * Instances come in two flavors:
763 * (1) Encrypted
764 * These always have data_offset = 0
765 * (2) Unencrypted
Hanno Beckercd430bc2019-04-04 16:29:48 +0100766 * These have data_offset set to the amount of
767 * pre-expansion during record protection. Concretely,
768 * this is the length of the fixed part of the explicit IV
769 * used for encryption, or 0 if no explicit IV is used
TRodziewicz2abf03c2021-06-25 14:40:09 +0200770 * (e.g. for stream ciphers).
Hanno Becker12a3a862018-01-05 15:42:50 +0000771 *
772 * The reason for the data_offset in the unencrypted case
773 * is to allow for in-place conversion of an unencrypted to
774 * an encrypted record. If the offset wasn't included, the
775 * encrypted content would need to be shifted afterwards to
776 * make space for the fixed IV.
777 *
778 */
Hanno Beckerf2ed4482019-04-29 13:45:54 +0100779#if MBEDTLS_SSL_CID_OUT_LEN_MAX > MBEDTLS_SSL_CID_IN_LEN_MAX
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200780# define MBEDTLS_SSL_CID_LEN_MAX MBEDTLS_SSL_CID_OUT_LEN_MAX
Hanno Beckerf2ed4482019-04-29 13:45:54 +0100781#else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200782# define MBEDTLS_SSL_CID_LEN_MAX MBEDTLS_SSL_CID_IN_LEN_MAX
Hanno Beckerf2ed4482019-04-29 13:45:54 +0100783#endif
784
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200785typedef struct {
786 uint8_t ctr[8]; /* In TLS: The implicit record sequence number.
787 * In DTLS: The 2-byte epoch followed by
788 * the 6-byte sequence number.
789 * This is stored as a raw big endian byte array
790 * as opposed to a uint64_t because we rarely
791 * need to perform arithmetic on this, but do
792 * need it as a Byte array for the purpose of
793 * MAC computations. */
794 uint8_t type; /* The record content type. */
795 uint8_t ver[2]; /* SSL/TLS version as present on the wire.
796 * Convert to internal presentation of versions
797 * using mbedtls_ssl_read_version() and
798 * mbedtls_ssl_write_version().
799 * Keep wire-format for MAC computations. */
Hanno Becker12a3a862018-01-05 15:42:50 +0000800
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200801 unsigned char *buf; /* Memory buffer enclosing the record content */
802 size_t buf_len; /* Buffer length */
803 size_t data_offset; /* Offset of record content */
804 size_t data_len; /* Length of record content */
Hanno Becker12a3a862018-01-05 15:42:50 +0000805
Hanno Beckera0e20d02019-05-15 14:03:01 +0100806#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200807 uint8_t cid_len; /* Length of the CID (0 if not present) */
808 unsigned char cid[MBEDTLS_SSL_CID_LEN_MAX]; /* The CID */
Hanno Beckera0e20d02019-05-15 14:03:01 +0100809#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker12a3a862018-01-05 15:42:50 +0000810} mbedtls_record;
811
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200812#if defined(MBEDTLS_X509_CRT_PARSE_C)
813/*
814 * List of certificate + private key pairs
815 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200816struct mbedtls_ssl_key_cert {
817 mbedtls_x509_crt *cert; /*!< cert */
818 mbedtls_pk_context *key; /*!< private key */
819 mbedtls_ssl_key_cert *next; /*!< next key/cert pair */
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200820};
821#endif /* MBEDTLS_X509_CRT_PARSE_C */
822
823#if defined(MBEDTLS_SSL_PROTO_DTLS)
824/*
825 * List of handshake messages kept around for resending
826 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200827struct mbedtls_ssl_flight_item {
828 unsigned char *p; /*!< message, including handshake headers */
829 size_t len; /*!< length of p */
830 unsigned char type; /*!< type of the message: handshake or CCS */
831 mbedtls_ssl_flight_item *next; /*!< next handshake message(s) */
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200832};
833#endif /* MBEDTLS_SSL_PROTO_DTLS */
834
Hanno Becker7e5437a2017-04-28 17:15:26 +0100835#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +0100836 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +0100837
838/* Find an entry in a signature-hash set matching a given hash algorithm. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200839mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find(mbedtls_ssl_sig_hash_set_t *set,
840 mbedtls_pk_type_t sig_alg);
Hanno Becker7e5437a2017-04-28 17:15:26 +0100841/* Add a signature-hash-pair to a signature-hash set */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200842void mbedtls_ssl_sig_hash_set_add(mbedtls_ssl_sig_hash_set_t *set,
843 mbedtls_pk_type_t sig_alg,
844 mbedtls_md_type_t md_alg);
Hanno Becker7e5437a2017-04-28 17:15:26 +0100845/* Allow exactly one hash algorithm for each signature. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200846void mbedtls_ssl_sig_hash_set_const_hash(mbedtls_ssl_sig_hash_set_t *set,
847 mbedtls_md_type_t md_alg);
Hanno Becker7e5437a2017-04-28 17:15:26 +0100848
849/* Setup an empty signature-hash set */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200850static inline void
851mbedtls_ssl_sig_hash_set_init(mbedtls_ssl_sig_hash_set_t *set)
Hanno Becker7e5437a2017-04-28 17:15:26 +0100852{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200853 mbedtls_ssl_sig_hash_set_const_hash(set, MBEDTLS_MD_NONE);
Hanno Becker7e5437a2017-04-28 17:15:26 +0100854}
855
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200856#endif /* MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +0100857 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200858
859/**
860 * \brief Free referenced items in an SSL transform context and clear
861 * memory
862 *
863 * \param transform SSL transform context
864 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200865void mbedtls_ssl_transform_free(mbedtls_ssl_transform *transform);
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200866
867/**
868 * \brief Free referenced items in an SSL handshake context and clear
869 * memory
870 *
Gilles Peskine9b562d52018-04-25 20:32:43 +0200871 * \param ssl SSL context
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200872 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200873void mbedtls_ssl_handshake_free(mbedtls_ssl_context *ssl);
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200874
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200875int mbedtls_ssl_handshake_client_step(mbedtls_ssl_context *ssl);
876int mbedtls_ssl_handshake_server_step(mbedtls_ssl_context *ssl);
877void mbedtls_ssl_handshake_wrapup(mbedtls_ssl_context *ssl);
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +0200878
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200879int mbedtls_ssl_send_fatal_handshake_failure(mbedtls_ssl_context *ssl);
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +0200880
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200881void mbedtls_ssl_reset_checksum(mbedtls_ssl_context *ssl);
882int mbedtls_ssl_derive_keys(mbedtls_ssl_context *ssl);
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +0200883
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200884int mbedtls_ssl_handle_message_type(mbedtls_ssl_context *ssl);
885int mbedtls_ssl_prepare_handshake_record(mbedtls_ssl_context *ssl);
886void mbedtls_ssl_update_handshake_status(mbedtls_ssl_context *ssl);
Simon Butcher99000142016-10-13 17:21:01 +0100887
Hanno Becker4a810fb2017-05-24 16:27:30 +0100888/**
889 * \brief Update record layer
890 *
891 * This function roughly separates the implementation
892 * of the logic of (D)TLS from the implementation
893 * of the secure transport.
894 *
Hanno Becker3a0aad12018-08-20 09:44:02 +0100895 * \param ssl The SSL context to use.
896 * \param update_hs_digest This indicates if the handshake digest
897 * should be automatically updated in case
898 * a handshake message is found.
Hanno Becker4a810fb2017-05-24 16:27:30 +0100899 *
900 * \return 0 or non-zero error code.
901 *
902 * \note A clarification on what is called 'record layer' here
903 * is in order, as many sensible definitions are possible:
904 *
905 * The record layer takes as input an untrusted underlying
906 * transport (stream or datagram) and transforms it into
907 * a serially multiplexed, secure transport, which
908 * conceptually provides the following:
909 *
910 * (1) Three datagram based, content-agnostic transports
911 * for handshake, alert and CCS messages.
912 * (2) One stream- or datagram-based transport
913 * for application data.
914 * (3) Functionality for changing the underlying transform
915 * securing the contents.
916 *
917 * The interface to this functionality is given as follows:
918 *
919 * a Updating
920 * [Currently implemented by mbedtls_ssl_read_record]
921 *
922 * Check if and on which of the four 'ports' data is pending:
923 * Nothing, a controlling datagram of type (1), or application
924 * data (2). In any case data is present, internal buffers
925 * provide access to the data for the user to process it.
926 * Consumption of type (1) datagrams is done automatically
927 * on the next update, invalidating that the internal buffers
928 * for previous datagrams, while consumption of application
929 * data (2) is user-controlled.
930 *
931 * b Reading of application data
932 * [Currently manual adaption of ssl->in_offt pointer]
933 *
934 * As mentioned in the last paragraph, consumption of data
935 * is different from the automatic consumption of control
936 * datagrams (1) because application data is treated as a stream.
937 *
938 * c Tracking availability of application data
939 * [Currently manually through decreasing ssl->in_msglen]
940 *
941 * For efficiency and to retain datagram semantics for
942 * application data in case of DTLS, the record layer
943 * provides functionality for checking how much application
944 * data is still available in the internal buffer.
945 *
946 * d Changing the transformation securing the communication.
947 *
948 * Given an opaque implementation of the record layer in the
949 * above sense, it should be possible to implement the logic
950 * of (D)TLS on top of it without the need to know anything
951 * about the record layer's internals. This is done e.g.
952 * in all the handshake handling functions, and in the
953 * application data reading function mbedtls_ssl_read.
954 *
955 * \note The above tries to give a conceptual picture of the
956 * record layer, but the current implementation deviates
957 * from it in some places. For example, our implementation of
958 * the update functionality through mbedtls_ssl_read_record
959 * discards datagrams depending on the current state, which
960 * wouldn't fall under the record layer's responsibility
961 * following the above definition.
962 *
963 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200964int mbedtls_ssl_read_record(mbedtls_ssl_context *ssl,
965 unsigned update_hs_digest);
966int mbedtls_ssl_fetch_input(mbedtls_ssl_context *ssl, size_t nb_want);
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +0200967
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200968int mbedtls_ssl_write_handshake_msg(mbedtls_ssl_context *ssl);
969int mbedtls_ssl_write_record(mbedtls_ssl_context *ssl, uint8_t force_flush);
970int mbedtls_ssl_flush_output(mbedtls_ssl_context *ssl);
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +0200971
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200972int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl);
973int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl);
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +0200974
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200975int mbedtls_ssl_parse_change_cipher_spec(mbedtls_ssl_context *ssl);
976int mbedtls_ssl_write_change_cipher_spec(mbedtls_ssl_context *ssl);
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +0200977
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200978int mbedtls_ssl_parse_finished(mbedtls_ssl_context *ssl);
979int mbedtls_ssl_write_finished(mbedtls_ssl_context *ssl);
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +0200980
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200981void mbedtls_ssl_optimize_checksum(
982 mbedtls_ssl_context *ssl,
983 const mbedtls_ssl_ciphersuite_t *ciphersuite_info);
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +0200984
Gilles Peskineeccd8882020-03-10 12:19:08 +0100985#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200986int mbedtls_ssl_psk_derive_premaster(mbedtls_ssl_context *ssl,
987 mbedtls_key_exchange_type_t key_ex);
Guilhem Bryantd511ac32020-03-25 17:06:37 +0000988
Guilhem Bryant8a69ddd2020-03-27 11:13:39 +0000989/**
Guilhem Bryantd511ac32020-03-25 17:06:37 +0000990 * Get the first defined PSK by order of precedence:
991 * 1. handshake PSK set by \c mbedtls_ssl_set_hs_psk() in the PSK callback
992 * 2. static PSK configured by \c mbedtls_ssl_conf_psk()
993 * Return a code and update the pair (PSK, PSK length) passed to this function
994 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200995static inline int mbedtls_ssl_get_psk(const mbedtls_ssl_context *ssl,
996 const unsigned char **psk,
997 size_t *psk_len)
Guilhem Bryantd511ac32020-03-25 17:06:37 +0000998{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200999 if (ssl->handshake->psk != NULL && ssl->handshake->psk_len > 0) {
Guilhem Bryantd511ac32020-03-25 17:06:37 +00001000 *psk = ssl->handshake->psk;
1001 *psk_len = ssl->handshake->psk_len;
1002 }
1003
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001004 else if (ssl->conf->psk != NULL && ssl->conf->psk_len > 0) {
Guilhem Bryantd511ac32020-03-25 17:06:37 +00001005 *psk = ssl->conf->psk;
1006 *psk_len = ssl->conf->psk_len;
1007 }
1008
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001009 else {
Guilhem Bryantb5f04e42020-04-01 11:23:58 +01001010 *psk = NULL;
1011 *psk_len = 0;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001012 return MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED;
Guilhem Bryantd511ac32020-03-25 17:06:37 +00001013 }
1014
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001015 return 0;
Guilhem Bryantd511ac32020-03-25 17:06:37 +00001016}
1017
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001018# if defined(MBEDTLS_USE_PSA_CRYPTO)
Guilhem Bryant8a69ddd2020-03-27 11:13:39 +00001019/**
Guilhem Bryantd511ac32020-03-25 17:06:37 +00001020 * Get the first defined opaque PSK by order of precedence:
1021 * 1. handshake PSK set by \c mbedtls_ssl_set_hs_psk_opaque() in the PSK
1022 * callback
1023 * 2. static PSK configured by \c mbedtls_ssl_conf_psk_opaque()
1024 * Return an opaque PSK
1025 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001026static inline psa_key_id_t
1027mbedtls_ssl_get_opaque_psk(const mbedtls_ssl_context *ssl)
Guilhem Bryantd511ac32020-03-25 17:06:37 +00001028{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001029 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque))
1030 return ssl->handshake->psk_opaque;
Guilhem Bryantd511ac32020-03-25 17:06:37 +00001031
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001032 if (!mbedtls_svc_key_id_is_null(ssl->conf->psk_opaque))
1033 return ssl->conf->psk_opaque;
Guilhem Bryantd511ac32020-03-25 17:06:37 +00001034
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001035 return MBEDTLS_SVC_KEY_ID_INIT;
Guilhem Bryantd511ac32020-03-25 17:06:37 +00001036}
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001037# endif /* MBEDTLS_USE_PSA_CRYPTO */
Guilhem Bryantd511ac32020-03-25 17:06:37 +00001038
1039#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +02001040
1041#if defined(MBEDTLS_PK_C)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001042unsigned char mbedtls_ssl_sig_from_pk(mbedtls_pk_context *pk);
1043unsigned char mbedtls_ssl_sig_from_pk_alg(mbedtls_pk_type_t type);
1044mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig(unsigned char sig);
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +02001045#endif
1046
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001047mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash(unsigned char hash);
1048unsigned char mbedtls_ssl_hash_from_md_alg(int md);
1049int mbedtls_ssl_set_calc_verify_md(mbedtls_ssl_context *ssl, int md);
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +02001050
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02001051#if defined(MBEDTLS_ECP_C)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001052int mbedtls_ssl_check_curve(const mbedtls_ssl_context *ssl,
1053 mbedtls_ecp_group_id grp_id);
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +02001054#endif
1055
Gilles Peskineeccd8882020-03-10 12:19:08 +01001056#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001057int mbedtls_ssl_check_sig_hash(const mbedtls_ssl_context *ssl,
1058 mbedtls_md_type_t md);
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02001059#endif
1060
Ron Eldor089c9fe2018-12-06 17:12:49 +02001061#if defined(MBEDTLS_SSL_DTLS_SRTP)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001062static inline mbedtls_ssl_srtp_profile
1063mbedtls_ssl_check_srtp_profile_value(const uint16_t srtp_profile_value)
Ron Eldor089c9fe2018-12-06 17:12:49 +02001064{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001065 switch (srtp_profile_value) {
Johan Pascal85269572020-08-25 10:01:54 +02001066 case MBEDTLS_TLS_SRTP_AES128_CM_HMAC_SHA1_80:
Johan Pascal85269572020-08-25 10:01:54 +02001067 case MBEDTLS_TLS_SRTP_AES128_CM_HMAC_SHA1_32:
Johan Pascal85269572020-08-25 10:01:54 +02001068 case MBEDTLS_TLS_SRTP_NULL_HMAC_SHA1_80:
Johan Pascal85269572020-08-25 10:01:54 +02001069 case MBEDTLS_TLS_SRTP_NULL_HMAC_SHA1_32:
Johan Pascal43f94902020-09-22 12:25:52 +02001070 return srtp_profile_value;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001071 default:
1072 break;
Ron Eldor089c9fe2018-12-06 17:12:49 +02001073 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001074 return MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor089c9fe2018-12-06 17:12:49 +02001075}
1076#endif
1077
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +02001078#if defined(MBEDTLS_X509_CRT_PARSE_C)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001079static inline mbedtls_pk_context *mbedtls_ssl_own_key(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +02001080{
1081 mbedtls_ssl_key_cert *key_cert;
1082
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001083 if (ssl->handshake != NULL && ssl->handshake->key_cert != NULL)
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +02001084 key_cert = ssl->handshake->key_cert;
1085 else
1086 key_cert = ssl->conf->key_cert;
1087
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001088 return key_cert == NULL ? NULL : key_cert->key;
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +02001089}
1090
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001091static inline mbedtls_x509_crt *mbedtls_ssl_own_cert(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +02001092{
1093 mbedtls_ssl_key_cert *key_cert;
1094
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001095 if (ssl->handshake != NULL && ssl->handshake->key_cert != NULL)
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +02001096 key_cert = ssl->handshake->key_cert;
1097 else
1098 key_cert = ssl->conf->key_cert;
1099
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001100 return key_cert == NULL ? NULL : key_cert->cert;
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +02001101}
1102
1103/*
1104 * Check usage of a certificate wrt extensions:
1105 * keyUsage, extendedKeyUsage (later), and nSCertType (later).
1106 *
1107 * Warning: cert_endpoint is the endpoint of the cert (ie, of our peer when we
1108 * check a cert we received from them)!
1109 *
1110 * Return 0 if everything is OK, -1 if not.
1111 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001112int mbedtls_ssl_check_cert_usage(const mbedtls_x509_crt *cert,
1113 const mbedtls_ssl_ciphersuite_t *ciphersuite,
1114 int cert_endpoint,
1115 uint32_t *flags);
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +02001116#endif /* MBEDTLS_X509_CRT_PARSE_C */
1117
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001118void mbedtls_ssl_write_version(int major,
1119 int minor,
1120 int transport,
1121 unsigned char ver[2]);
1122void mbedtls_ssl_read_version(int *major,
1123 int *minor,
1124 int transport,
1125 const unsigned char ver[2]);
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +02001126
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001127static inline size_t mbedtls_ssl_in_hdr_len(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +02001128{
Hanno Becker47be7682019-07-12 09:55:46 +01001129#if !defined(MBEDTLS_SSL_PROTO_DTLS)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001130 ((void)ssl);
Hanno Becker47be7682019-07-12 09:55:46 +01001131#endif
1132
1133#if defined(MBEDTLS_SSL_PROTO_DTLS)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001134 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1135 return 13;
1136 } else
Hanno Becker47be7682019-07-12 09:55:46 +01001137#endif /* MBEDTLS_SSL_PROTO_DTLS */
1138 {
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001139 return 5;
Hanno Becker47be7682019-07-12 09:55:46 +01001140 }
Hanno Becker5903de42019-05-03 14:46:38 +01001141}
1142
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001143static inline size_t mbedtls_ssl_out_hdr_len(const mbedtls_ssl_context *ssl)
Hanno Becker5903de42019-05-03 14:46:38 +01001144{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001145 return ((size_t)(ssl->out_iv - ssl->out_hdr));
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +02001146}
1147
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001148static inline size_t mbedtls_ssl_hs_hdr_len(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +02001149{
1150#if defined(MBEDTLS_SSL_PROTO_DTLS)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001151 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM)
1152 return 12;
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +02001153#else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001154 ((void)ssl);
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +02001155#endif
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001156 return 4;
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +02001157}
1158
1159#if defined(MBEDTLS_SSL_PROTO_DTLS)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001160void mbedtls_ssl_send_flight_completed(mbedtls_ssl_context *ssl);
1161void mbedtls_ssl_recv_flight_completed(mbedtls_ssl_context *ssl);
1162int mbedtls_ssl_resend(mbedtls_ssl_context *ssl);
1163int mbedtls_ssl_flight_transmit(mbedtls_ssl_context *ssl);
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +02001164#endif
1165
1166/* Visible for testing purposes only */
1167#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001168int mbedtls_ssl_dtls_replay_check(mbedtls_ssl_context const *ssl);
1169void mbedtls_ssl_dtls_replay_update(mbedtls_ssl_context *ssl);
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +02001170#endif
1171
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001172int mbedtls_ssl_session_copy(mbedtls_ssl_session *dst,
1173 const mbedtls_ssl_session *src);
Hanno Becker52055ae2019-02-06 14:30:46 +00001174
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +02001175/* constant-time buffer comparison */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001176static inline int
1177mbedtls_ssl_safer_memcmp(const void *a, const void *b, size_t n)
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +02001178{
1179 size_t i;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001180 volatile const unsigned char *A = (volatile const unsigned char *)a;
1181 volatile const unsigned char *B = (volatile const unsigned char *)b;
Hanno Becker59e69632017-06-26 13:26:58 +01001182 volatile unsigned char diff = 0;
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +02001183
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001184 for (i = 0; i < n; i++) {
Azim Khan45b79cf2018-05-23 16:55:16 +01001185 /* Read volatile data in order before computing diff.
1186 * This avoids IAR compiler warning:
1187 * 'the order of volatile accesses is undefined ..' */
1188 unsigned char x = A[i], y = B[i];
1189 diff |= x ^ y;
1190 }
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +02001191
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001192 return diff;
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +02001193}
1194
TRodziewicz0f82ec62021-05-12 17:49:18 +02001195#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek814feff2019-01-14 04:35:19 -05001196/* The hash buffer must have at least MBEDTLS_MD_MAX_SIZE bytes of length. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001197int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
1198 unsigned char *hash,
1199 size_t *hashlen,
1200 unsigned char *data,
1201 size_t data_len,
1202 mbedtls_md_type_t md_alg);
TRodziewicz0f82ec62021-05-12 17:49:18 +02001203#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01001204
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +02001205#ifdef __cplusplus
1206}
1207#endif
1208
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001209void mbedtls_ssl_transform_init(mbedtls_ssl_transform *transform);
1210int mbedtls_ssl_encrypt_buf(mbedtls_ssl_context *ssl,
1211 mbedtls_ssl_transform *transform,
1212 mbedtls_record *rec,
1213 int (*f_rng)(void *, unsigned char *, size_t),
1214 void *p_rng);
1215int mbedtls_ssl_decrypt_buf(mbedtls_ssl_context const *ssl,
1216 mbedtls_ssl_transform *transform,
1217 mbedtls_record *rec);
Hanno Beckera18d1322018-01-03 14:27:32 +00001218
Hanno Beckerdd772292020-02-05 10:38:31 +00001219/* Length of the "epoch" field in the record header */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001220static inline size_t mbedtls_ssl_ep_len(const mbedtls_ssl_context *ssl)
Hanno Beckerdd772292020-02-05 10:38:31 +00001221{
1222#if defined(MBEDTLS_SSL_PROTO_DTLS)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001223 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM)
1224 return 2;
Hanno Beckerdd772292020-02-05 10:38:31 +00001225#else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001226 ((void)ssl);
Hanno Beckerdd772292020-02-05 10:38:31 +00001227#endif
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001228 return 0;
Hanno Beckerdd772292020-02-05 10:38:31 +00001229}
1230
Hanno Becker08f09132020-02-11 15:40:07 +00001231#if defined(MBEDTLS_SSL_PROTO_DTLS)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001232int mbedtls_ssl_resend_hello_request(mbedtls_ssl_context *ssl);
Hanno Becker08f09132020-02-11 15:40:07 +00001233#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker0f57a652020-02-05 10:37:26 +00001234
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001235void mbedtls_ssl_set_timer(mbedtls_ssl_context *ssl, uint32_t millisecs);
1236int mbedtls_ssl_check_timer(mbedtls_ssl_context *ssl);
Hanno Becker7876d122020-02-05 10:39:31 +00001237
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001238void mbedtls_ssl_reset_in_out_pointers(mbedtls_ssl_context *ssl);
1239void mbedtls_ssl_update_out_pointers(mbedtls_ssl_context *ssl,
1240 mbedtls_ssl_transform *transform);
1241void mbedtls_ssl_update_in_pointers(mbedtls_ssl_context *ssl);
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00001242
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001243int mbedtls_ssl_session_reset_int(mbedtls_ssl_context *ssl, int partial);
Hanno Becker43aefe22020-02-05 10:44:56 +00001244
Hanno Becker7e8e6a62020-02-05 10:45:48 +00001245#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001246void mbedtls_ssl_dtls_replay_reset(mbedtls_ssl_context *ssl);
Hanno Becker7e8e6a62020-02-05 10:45:48 +00001247#endif
1248
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001249void mbedtls_ssl_handshake_wrapup_free_hs_transform(mbedtls_ssl_context *ssl);
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00001250
Hanno Becker08f09132020-02-11 15:40:07 +00001251#if defined(MBEDTLS_SSL_RENEGOTIATION)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001252int mbedtls_ssl_start_renegotiation(mbedtls_ssl_context *ssl);
Hanno Becker08f09132020-02-11 15:40:07 +00001253#endif /* MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker89490712020-02-05 10:50:12 +00001254
Hanno Becker533ab5f2020-02-05 10:49:13 +00001255#if defined(MBEDTLS_SSL_PROTO_DTLS)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001256size_t mbedtls_ssl_get_current_mtu(const mbedtls_ssl_context *ssl);
1257void mbedtls_ssl_buffering_free(mbedtls_ssl_context *ssl);
1258void mbedtls_ssl_flight_free(mbedtls_ssl_flight_item *flight);
Hanno Becker533ab5f2020-02-05 10:49:13 +00001259#endif /* MBEDTLS_SSL_PROTO_DTLS */
1260
Chris Jones84a773f2021-03-05 18:38:47 +00001261#endif /* ssl_misc.h */