blob: a2b23cb0f6930f3034e707e8b98e51c76431cd1d [file] [log] [blame]
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +02001/**
Darryl Greena40a1012018-01-05 15:33:17 +00002 * \file ssl_internal.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 */
22#ifndef MBEDTLS_SSL_INTERNAL_H
23#define MBEDTLS_SSL_INTERNAL_H
24
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050025#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Amero6609aef2019-07-04 20:01:14 +010026#include "mbedtls/config.h"
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050027#else
28#include MBEDTLS_CONFIG_FILE
29#endif
30
Jaeden Amero6609aef2019-07-04 20:01:14 +010031#include "mbedtls/ssl.h"
32#include "mbedtls/cipher.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020033
Andrzej Kurekeb342242019-01-29 09:14:33 -050034#if defined(MBEDTLS_USE_PSA_CRYPTO)
35#include "psa/crypto.h"
36#endif
37
Manuel Pégourié-Gonnard56273da2015-05-26 12:19:45 +020038#if defined(MBEDTLS_MD5_C)
Jaeden Amero6609aef2019-07-04 20:01:14 +010039#include "mbedtls/md5.h"
Manuel Pégourié-Gonnard56273da2015-05-26 12:19:45 +020040#endif
41
42#if defined(MBEDTLS_SHA1_C)
Jaeden Amero6609aef2019-07-04 20:01:14 +010043#include "mbedtls/sha1.h"
Manuel Pégourié-Gonnard56273da2015-05-26 12:19:45 +020044#endif
45
46#if defined(MBEDTLS_SHA256_C)
Jaeden Amero6609aef2019-07-04 20:01:14 +010047#include "mbedtls/sha256.h"
Manuel Pégourié-Gonnard56273da2015-05-26 12:19:45 +020048#endif
49
50#if defined(MBEDTLS_SHA512_C)
Jaeden Amero6609aef2019-07-04 20:01:14 +010051#include "mbedtls/sha512.h"
Manuel Pégourié-Gonnard56273da2015-05-26 12:19:45 +020052#endif
53
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020054#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Jaeden Amero6609aef2019-07-04 20:01:14 +010055#include "mbedtls/ecjpake.h"
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020056#endif
57
Hanno Beckerdf51dbe2019-02-18 16:41:55 +000058#if defined(MBEDTLS_USE_PSA_CRYPTO)
59#include "psa/crypto.h"
Jaeden Amero6609aef2019-07-04 20:01:14 +010060#include "mbedtls/psa_util.h"
Hanno Beckerdf51dbe2019-02-18 16:41:55 +000061#endif /* MBEDTLS_USE_PSA_CRYPTO */
62
Manuel Pégourié-Gonnard0223ab92015-10-05 11:40:01 +010063#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
64 !defined(inline) && !defined(__cplusplus)
Manuel Pégourié-Gonnard065122c2015-05-26 12:31:46 +020065#define inline __inline
Manuel Pégourié-Gonnard20af64d2015-07-07 18:33:39 +020066#endif
Manuel Pégourié-Gonnard065122c2015-05-26 12:31:46 +020067
68/* Determine minimum supported version */
69#define MBEDTLS_SSL_MIN_MAJOR_VERSION MBEDTLS_SSL_MAJOR_VERSION_3
70
71#if defined(MBEDTLS_SSL_PROTO_SSL3)
72#define MBEDTLS_SSL_MIN_MINOR_VERSION MBEDTLS_SSL_MINOR_VERSION_0
73#else
74#if defined(MBEDTLS_SSL_PROTO_TLS1)
75#define MBEDTLS_SSL_MIN_MINOR_VERSION MBEDTLS_SSL_MINOR_VERSION_1
76#else
77#if defined(MBEDTLS_SSL_PROTO_TLS1_1)
78#define MBEDTLS_SSL_MIN_MINOR_VERSION MBEDTLS_SSL_MINOR_VERSION_2
79#else
80#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
81#define MBEDTLS_SSL_MIN_MINOR_VERSION MBEDTLS_SSL_MINOR_VERSION_3
82#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
83#endif /* MBEDTLS_SSL_PROTO_TLS1_1 */
84#endif /* MBEDTLS_SSL_PROTO_TLS1 */
85#endif /* MBEDTLS_SSL_PROTO_SSL3 */
86
Ron Eldor5e9f14d2017-05-28 10:46:38 +030087#define MBEDTLS_SSL_MIN_VALID_MINOR_VERSION MBEDTLS_SSL_MINOR_VERSION_1
88#define MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION MBEDTLS_SSL_MAJOR_VERSION_3
89
Manuel Pégourié-Gonnard065122c2015-05-26 12:31:46 +020090/* Determine maximum supported version */
91#define MBEDTLS_SSL_MAX_MAJOR_VERSION MBEDTLS_SSL_MAJOR_VERSION_3
92
93#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
94#define MBEDTLS_SSL_MAX_MINOR_VERSION MBEDTLS_SSL_MINOR_VERSION_3
95#else
96#if defined(MBEDTLS_SSL_PROTO_TLS1_1)
97#define MBEDTLS_SSL_MAX_MINOR_VERSION MBEDTLS_SSL_MINOR_VERSION_2
98#else
99#if defined(MBEDTLS_SSL_PROTO_TLS1)
100#define MBEDTLS_SSL_MAX_MINOR_VERSION MBEDTLS_SSL_MINOR_VERSION_1
101#else
102#if defined(MBEDTLS_SSL_PROTO_SSL3)
103#define MBEDTLS_SSL_MAX_MINOR_VERSION MBEDTLS_SSL_MINOR_VERSION_0
104#endif /* MBEDTLS_SSL_PROTO_SSL3 */
105#endif /* MBEDTLS_SSL_PROTO_TLS1 */
106#endif /* MBEDTLS_SSL_PROTO_TLS1_1 */
107#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
108
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +0200109/* Shorthand for restartable ECC */
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +0200110#if defined(MBEDTLS_ECP_RESTARTABLE) && \
111 defined(MBEDTLS_SSL_CLI_C) && \
112 defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
113 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Gilles Peskineeccd8882020-03-10 12:19:08 +0100114#define MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +0200115#endif
116
Manuel Pégourié-Gonnard065122c2015-05-26 12:31:46 +0200117#define MBEDTLS_SSL_INITIAL_HANDSHAKE 0
118#define MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS 1 /* In progress */
119#define MBEDTLS_SSL_RENEGOTIATION_DONE 2 /* Done or aborted */
120#define MBEDTLS_SSL_RENEGOTIATION_PENDING 3 /* Requested (server only) */
121
122/*
123 * DTLS retransmission states, see RFC 6347 4.2.4
124 *
125 * The SENDING state is merged in PREPARING for initial sends,
126 * but is distinct for resends.
127 *
128 * Note: initial state is wrong for server, but is not used anyway.
129 */
130#define MBEDTLS_SSL_RETRANS_PREPARING 0
131#define MBEDTLS_SSL_RETRANS_SENDING 1
132#define MBEDTLS_SSL_RETRANS_WAITING 2
133#define MBEDTLS_SSL_RETRANS_FINISHED 3
134
135/*
136 * Allow extra bytes for record, authentication and encryption overhead:
137 * counter (8) + header (5) + IV(16) + MAC (16-48) + padding (0-256)
138 * and allow for a maximum of 1024 of compression expansion if
139 * enabled.
140 */
141#if defined(MBEDTLS_ZLIB_SUPPORT)
142#define MBEDTLS_SSL_COMPRESSION_ADD 1024
143#else
144#define MBEDTLS_SSL_COMPRESSION_ADD 0
145#endif
146
Manuel Pégourié-Gonnard05579c42020-07-31 12:53:39 +0200147/* This macro determines whether CBC is supported. */
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +0200148#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
149 ( defined(MBEDTLS_AES_C) || \
150 defined(MBEDTLS_CAMELLIA_C) || \
151 defined(MBEDTLS_ARIA_C) || \
Andrzej Kurek15e5dd82022-10-20 06:10:52 -0400152 defined(MBEDTLS_DES_C) || \
153 defined(MBEDTLS_BLOWFISH_C) )
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +0200154#define MBEDTLS_SSL_SOME_SUITES_USE_CBC
155#endif
156
Manuel Pégourié-Gonnard05579c42020-07-31 12:53:39 +0200157/* This macro determines whether the CBC construct used in TLS 1.0-1.2 (as
158 * opposed to the very different CBC construct used in SSLv3) is supported. */
Manuel Pégourié-Gonnarded0e8642020-07-21 11:20:30 +0200159#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC) && \
160 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
161 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
162 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
163#define MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC
164#endif
165
Hanno Becker52344c22018-01-03 15:24:20 +0000166#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) || \
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +0200167 defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC)
Hanno Becker52344c22018-01-03 15:24:20 +0000168#define MBEDTLS_SSL_SOME_MODES_USE_MAC
169#endif
170
171#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard065122c2015-05-26 12:31:46 +0200172/* Ciphersuites using HMAC */
173#if defined(MBEDTLS_SHA512_C)
174#define MBEDTLS_SSL_MAC_ADD 48 /* SHA-384 used for HMAC */
175#elif defined(MBEDTLS_SHA256_C)
176#define MBEDTLS_SSL_MAC_ADD 32 /* SHA-256 used for HMAC */
177#else
178#define MBEDTLS_SSL_MAC_ADD 20 /* SHA-1 used for HMAC */
179#endif
Hanno Becker52344c22018-01-03 15:24:20 +0000180#else /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard065122c2015-05-26 12:31:46 +0200181/* AEAD ciphersuites: GCM and CCM use a 128 bits tag */
182#define MBEDTLS_SSL_MAC_ADD 16
183#endif
184
185#if defined(MBEDTLS_CIPHER_MODE_CBC)
186#define MBEDTLS_SSL_PADDING_ADD 256
187#else
188#define MBEDTLS_SSL_PADDING_ADD 0
189#endif
190
Hanno Beckera0e20d02019-05-15 14:03:01 +0100191#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb1aa1b32019-05-08 17:37:58 +0100192#define MBEDTLS_SSL_MAX_CID_EXPANSION MBEDTLS_SSL_CID_PADDING_GRANULARITY
Hanno Becker6cbad552019-05-08 15:40:11 +0100193#else
194#define MBEDTLS_SSL_MAX_CID_EXPANSION 0
195#endif
196
Angus Grattond8213d02016-05-25 20:56:48 +1000197#define MBEDTLS_SSL_PAYLOAD_OVERHEAD ( MBEDTLS_SSL_COMPRESSION_ADD + \
198 MBEDTLS_MAX_IV_LENGTH + \
199 MBEDTLS_SSL_MAC_ADD + \
Hanno Becker6cbad552019-05-08 15:40:11 +0100200 MBEDTLS_SSL_PADDING_ADD + \
201 MBEDTLS_SSL_MAX_CID_EXPANSION \
Angus Grattond8213d02016-05-25 20:56:48 +1000202 )
203
204#define MBEDTLS_SSL_IN_PAYLOAD_LEN ( MBEDTLS_SSL_PAYLOAD_OVERHEAD + \
205 ( MBEDTLS_SSL_IN_CONTENT_LEN ) )
206
207#define MBEDTLS_SSL_OUT_PAYLOAD_LEN ( MBEDTLS_SSL_PAYLOAD_OVERHEAD + \
208 ( MBEDTLS_SSL_OUT_CONTENT_LEN ) )
209
Hanno Becker0271f962018-08-16 13:23:47 +0100210/* The maximum number of buffered handshake messages. */
Hanno Beckerd488b9e2018-08-16 16:35:37 +0100211#define MBEDTLS_SSL_MAX_BUFFERED_HS 4
Hanno Becker0271f962018-08-16 13:23:47 +0100212
Angus Grattond8213d02016-05-25 20:56:48 +1000213/* Maximum length we can advertise as our max content length for
214 RFC 6066 max_fragment_length extension negotiation purposes
215 (the lesser of both sizes, if they are unequal.)
216 */
217#define MBEDTLS_TLS_EXT_ADV_CONTENT_LEN ( \
218 (MBEDTLS_SSL_IN_CONTENT_LEN > MBEDTLS_SSL_OUT_CONTENT_LEN) \
219 ? ( MBEDTLS_SSL_OUT_CONTENT_LEN ) \
220 : ( MBEDTLS_SSL_IN_CONTENT_LEN ) \
221 )
Manuel Pégourié-Gonnard065122c2015-05-26 12:31:46 +0200222
Hanno Beckere131bfe2017-04-12 14:54:42 +0100223/* Maximum size in bytes of list in sig-hash algorithm ext., RFC 5246 */
224#define MBEDTLS_SSL_MAX_SIG_HASH_ALG_LIST_LEN 65534
225
226/* Maximum size in bytes of list in supported elliptic curve ext., RFC 4492 */
227#define MBEDTLS_SSL_MAX_CURVE_LIST_LEN 65535
228
Manuel Pégourié-Gonnard065122c2015-05-26 12:31:46 +0200229/*
Hanno Beckera8434e82017-09-18 10:54:39 +0100230 * Check that we obey the standard's message size bounds
231 */
232
233#if MBEDTLS_SSL_MAX_CONTENT_LEN > 16384
Angus Grattond8213d02016-05-25 20:56:48 +1000234#error "Bad configuration - record content too large."
Hanno Beckera8434e82017-09-18 10:54:39 +0100235#endif
236
Angus Grattond8213d02016-05-25 20:56:48 +1000237#if MBEDTLS_SSL_IN_CONTENT_LEN > MBEDTLS_SSL_MAX_CONTENT_LEN
238#error "Bad configuration - incoming record content should not be larger than MBEDTLS_SSL_MAX_CONTENT_LEN."
Hanno Beckera8434e82017-09-18 10:54:39 +0100239#endif
240
Angus Grattond8213d02016-05-25 20:56:48 +1000241#if MBEDTLS_SSL_OUT_CONTENT_LEN > MBEDTLS_SSL_MAX_CONTENT_LEN
242#error "Bad configuration - outgoing record content should not be larger than MBEDTLS_SSL_MAX_CONTENT_LEN."
243#endif
244
245#if MBEDTLS_SSL_IN_PAYLOAD_LEN > MBEDTLS_SSL_MAX_CONTENT_LEN + 2048
246#error "Bad configuration - incoming protected record payload too large."
247#endif
248
249#if MBEDTLS_SSL_OUT_PAYLOAD_LEN > MBEDTLS_SSL_MAX_CONTENT_LEN + 2048
250#error "Bad configuration - outgoing protected record payload too large."
251#endif
252
253/* Calculate buffer sizes */
254
Hanno Becker25d6d1a2017-12-07 08:22:51 +0000255/* Note: Even though the TLS record header is only 5 bytes
256 long, we're internally using 8 bytes to store the
257 implicit sequence number. */
Hanno Beckerd25d4442017-10-04 13:56:42 +0100258#define MBEDTLS_SSL_HEADER_LEN 13
Hanno Beckera8434e82017-09-18 10:54:39 +0100259
Andrzej Kurek033c42a2020-03-03 05:57:59 -0500260#if !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Angus Grattond8213d02016-05-25 20:56:48 +1000261#define MBEDTLS_SSL_IN_BUFFER_LEN \
262 ( ( MBEDTLS_SSL_HEADER_LEN ) + ( MBEDTLS_SSL_IN_PAYLOAD_LEN ) )
Hanno Becker6cbad552019-05-08 15:40:11 +0100263#else
264#define MBEDTLS_SSL_IN_BUFFER_LEN \
265 ( ( MBEDTLS_SSL_HEADER_LEN ) + ( MBEDTLS_SSL_IN_PAYLOAD_LEN ) \
266 + ( MBEDTLS_SSL_CID_IN_LEN_MAX ) )
267#endif
Angus Grattond8213d02016-05-25 20:56:48 +1000268
Andrzej Kurek033c42a2020-03-03 05:57:59 -0500269#if !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Angus Grattond8213d02016-05-25 20:56:48 +1000270#define MBEDTLS_SSL_OUT_BUFFER_LEN \
271 ( ( MBEDTLS_SSL_HEADER_LEN ) + ( MBEDTLS_SSL_OUT_PAYLOAD_LEN ) )
Hanno Becker6cbad552019-05-08 15:40:11 +0100272#else
273#define MBEDTLS_SSL_OUT_BUFFER_LEN \
274 ( ( MBEDTLS_SSL_HEADER_LEN ) + ( MBEDTLS_SSL_OUT_PAYLOAD_LEN ) \
275 + ( MBEDTLS_SSL_CID_OUT_LEN_MAX ) )
276#endif
Angus Grattond8213d02016-05-25 20:56:48 +1000277
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500278#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
Andrzej Kurek069fa962021-01-07 08:02:15 -0500279static inline size_t mbedtls_ssl_get_output_buflen( const mbedtls_ssl_context *ctx )
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500280{
281#if defined (MBEDTLS_SSL_DTLS_CONNECTION_ID)
Andrzej Kurek069fa962021-01-07 08:02:15 -0500282 return mbedtls_ssl_get_output_max_frag_len( ctx )
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500283 + MBEDTLS_SSL_HEADER_LEN + MBEDTLS_SSL_PAYLOAD_OVERHEAD
284 + MBEDTLS_SSL_CID_OUT_LEN_MAX;
285#else
Andrzej Kurek069fa962021-01-07 08:02:15 -0500286 return mbedtls_ssl_get_output_max_frag_len( ctx )
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500287 + MBEDTLS_SSL_HEADER_LEN + MBEDTLS_SSL_PAYLOAD_OVERHEAD;
288#endif
289}
290
Andrzej Kurek069fa962021-01-07 08:02:15 -0500291static inline size_t mbedtls_ssl_get_input_buflen( const mbedtls_ssl_context *ctx )
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500292{
293#if defined (MBEDTLS_SSL_DTLS_CONNECTION_ID)
Andrzej Kurek069fa962021-01-07 08:02:15 -0500294 return mbedtls_ssl_get_input_max_frag_len( ctx )
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500295 + MBEDTLS_SSL_HEADER_LEN + MBEDTLS_SSL_PAYLOAD_OVERHEAD
296 + MBEDTLS_SSL_CID_IN_LEN_MAX;
297#else
Andrzej Kurek069fa962021-01-07 08:02:15 -0500298 return mbedtls_ssl_get_input_max_frag_len( ctx )
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500299 + MBEDTLS_SSL_HEADER_LEN + MBEDTLS_SSL_PAYLOAD_OVERHEAD;
300#endif
301}
302#endif
303
Angus Grattond8213d02016-05-25 20:56:48 +1000304#ifdef MBEDTLS_ZLIB_SUPPORT
305/* Compression buffer holds both IN and OUT buffers, so should be size of the larger */
306#define MBEDTLS_SSL_COMPRESS_BUFFER_LEN ( \
307 ( MBEDTLS_SSL_IN_BUFFER_LEN > MBEDTLS_SSL_OUT_BUFFER_LEN ) \
308 ? MBEDTLS_SSL_IN_BUFFER_LEN \
309 : MBEDTLS_SSL_OUT_BUFFER_LEN \
310 )
311#endif
Hanno Beckera8434e82017-09-18 10:54:39 +0100312
313/*
Manuel Pégourié-Gonnard065122c2015-05-26 12:31:46 +0200314 * TLS extension flags (for extensions with outgoing ServerHello content
315 * that need it (e.g. for RENEGOTIATION_INFO the server already knows because
316 * of state of the renegotiation flag, so no indicator is required)
317 */
318#define MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT (1 << 0)
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +0200319#define MBEDTLS_TLS_EXT_ECJPAKE_KKPP_OK (1 << 1)
Manuel Pégourié-Gonnard065122c2015-05-26 12:31:46 +0200320
Hanno Becker51018aa2017-04-12 14:54:42 +0100321/**
322 * \brief This function checks if the remaining size in a buffer is
323 * greater or equal than a needed space.
324 *
325 * \param cur Pointer to the current position in the buffer.
326 * \param end Pointer to one past the end of the buffer.
327 * \param need Needed space in bytes.
328 *
Ronald Cronb7b35e12020-06-11 09:50:51 +0200329 * \return Zero if the needed space is available in the buffer, non-zero
Hanno Becker51018aa2017-04-12 14:54:42 +0100330 * otherwise.
331 */
332static inline int mbedtls_ssl_chk_buf_ptr( const uint8_t *cur,
333 const uint8_t *end, size_t need )
334{
Ronald Cronb7b35e12020-06-11 09:50:51 +0200335 return( ( cur > end ) || ( need > (size_t)( end - cur ) ) );
Hanno Becker51018aa2017-04-12 14:54:42 +0100336}
337
338/**
339 * \brief This macro checks if the remaining size in a buffer is
340 * greater or equal than a needed space. If it is not the case,
341 * it returns an SSL_BUFFER_TOO_SMALL error.
342 *
343 * \param cur Pointer to the current position in the buffer.
344 * \param end Pointer to one past the end of the buffer.
345 * \param need Needed space in bytes.
346 *
347 */
348#define MBEDTLS_SSL_CHK_BUF_PTR( cur, end, need ) \
349 do { \
Ronald Cronb7b35e12020-06-11 09:50:51 +0200350 if( mbedtls_ssl_chk_buf_ptr( ( cur ), ( end ), ( need ) ) != 0 ) \
Hanno Becker51018aa2017-04-12 14:54:42 +0100351 { \
352 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); \
353 } \
354 } while( 0 )
355
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +0200356#ifdef __cplusplus
357extern "C" {
358#endif
359
Hanno Becker7e5437a2017-04-28 17:15:26 +0100360#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +0100361 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +0100362/*
363 * Abstraction for a grid of allowed signature-hash-algorithm pairs.
364 */
365struct mbedtls_ssl_sig_hash_set_t
366{
367 /* At the moment, we only need to remember a single suitable
368 * hash algorithm per signature algorithm. As long as that's
369 * the case - and we don't need a general lookup function -
370 * we can implement the sig-hash-set as a map from signatures
371 * to hash algorithms. */
372 mbedtls_md_type_t rsa;
373 mbedtls_md_type_t ecdsa;
374};
375#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Gilles Peskineeccd8882020-03-10 12:19:08 +0100376 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Hanno Becker7e5437a2017-04-28 17:15:26 +0100377
Ron Eldor51d3ab52019-05-12 14:54:30 +0300378typedef int mbedtls_ssl_tls_prf_cb( const unsigned char *secret, size_t slen,
379 const char *label,
380 const unsigned char *random, size_t rlen,
381 unsigned char *dstbuf, size_t dlen );
Hanno Becker3385a4d2020-08-21 13:03:34 +0100382
Hanno Becker61baae72020-09-16 09:24:14 +0100383/* cipher.h exports the maximum IV, key and block length from
Hanno Becker15889832020-09-08 11:29:11 +0100384 * all ciphers enabled in the config, regardless of whether those
385 * ciphers are actually usable in SSL/TLS. Notably, XTS is enabled
386 * in the default configuration and uses 64 Byte keys, but it is
387 * not used for record protection in SSL/TLS.
388 *
389 * In order to prevent unnecessary inflation of key structures,
390 * we introduce SSL-specific variants of the max-{key,block,IV}
391 * macros here which are meant to only take those ciphers into
392 * account which can be negotiated in SSL/TLS.
393 *
394 * Since the current definitions of MBEDTLS_MAX_{KEY|BLOCK|IV}_LENGTH
395 * in cipher.h are rough overapproximations of the real maxima, here
Hanno Becker9a7a2ac2020-09-09 09:24:54 +0100396 * we content ourselves with replicating those overapproximations
Hanno Becker15889832020-09-08 11:29:11 +0100397 * for the maximum block and IV length, and excluding XTS from the
398 * computation of the maximum key length. */
399#define MBEDTLS_SSL_MAX_BLOCK_LENGTH 16
400#define MBEDTLS_SSL_MAX_IV_LENGTH 16
401#define MBEDTLS_SSL_MAX_KEY_LENGTH 32
402
Hanno Becker3385a4d2020-08-21 13:03:34 +0100403/**
404 * \brief The data structure holding the cryptographic material (key and IV)
405 * used for record protection in TLS 1.3.
406 */
407struct mbedtls_ssl_key_set
408{
409 /*! The key for client->server records. */
Hanno Becker15889832020-09-08 11:29:11 +0100410 unsigned char client_write_key[ MBEDTLS_SSL_MAX_KEY_LENGTH ];
Hanno Becker3385a4d2020-08-21 13:03:34 +0100411 /*! The key for server->client records. */
Hanno Becker15889832020-09-08 11:29:11 +0100412 unsigned char server_write_key[ MBEDTLS_SSL_MAX_KEY_LENGTH ];
Hanno Becker3385a4d2020-08-21 13:03:34 +0100413 /*! The IV for client->server records. */
Hanno Becker15889832020-09-08 11:29:11 +0100414 unsigned char client_write_iv[ MBEDTLS_SSL_MAX_IV_LENGTH ];
Hanno Becker3385a4d2020-08-21 13:03:34 +0100415 /*! The IV for server->client records. */
Hanno Becker15889832020-09-08 11:29:11 +0100416 unsigned char server_write_iv[ MBEDTLS_SSL_MAX_IV_LENGTH ];
Hanno Becker3385a4d2020-08-21 13:03:34 +0100417
Hanno Becker493ea7f2020-09-08 11:01:00 +0100418 size_t key_len; /*!< The length of client_write_key and
419 * server_write_key, in Bytes. */
420 size_t iv_len; /*!< The length of client_write_iv and
421 * server_write_iv, in Bytes. */
Hanno Becker3385a4d2020-08-21 13:03:34 +0100422};
423typedef struct mbedtls_ssl_key_set mbedtls_ssl_key_set;
Hanno Becker3385a4d2020-08-21 13:03:34 +0100424
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200425/*
426 * This structure contains the parameters only needed during handshake.
427 */
428struct mbedtls_ssl_handshake_params
429{
430 /*
431 * Handshake specific crypto variables
432 */
Hanno Becker7e5437a2017-04-28 17:15:26 +0100433
Gilles Peskinebaccfef2021-11-16 17:44:31 +0100434 uint8_t max_major_ver; /*!< max. major version client*/
435 uint8_t max_minor_ver; /*!< max. minor version client*/
436 uint8_t resume; /*!< session resume indicator*/
437 uint8_t cli_exts; /*!< client extension presence*/
438
439#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
440 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
441 uint8_t sni_authmode; /*!< authmode from SNI callback */
442#endif
443
444#if defined(MBEDTLS_SSL_SESSION_TICKETS)
445 uint8_t new_session_ticket; /*!< use NewSessionTicket? */
446#endif /* MBEDTLS_SSL_SESSION_TICKETS */
447
448#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
449 uint8_t extended_ms; /*!< use Extended Master Secret? */
450#endif
451
452#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
453 uint8_t async_in_progress; /*!< an asynchronous operation is in progress */
454#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
455
456#if defined(MBEDTLS_SSL_PROTO_DTLS)
457 unsigned char retransmit_state; /*!< Retransmission state */
458#endif
459
460#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
461 uint8_t ecrs_enabled; /*!< Handshake supports EC restart? */
462 enum { /* this complements ssl->state with info on intra-state operations */
463 ssl_ecrs_none = 0, /*!< nothing going on (yet) */
464 ssl_ecrs_crt_verify, /*!< Certificate: crt_verify() */
465 ssl_ecrs_ske_start_processing, /*!< ServerKeyExchange: pk_verify() */
466 ssl_ecrs_cke_ecdh_calc_secret, /*!< ClientKeyExchange: ECDH step 2 */
467 ssl_ecrs_crt_vrfy_sign, /*!< CertificateVerify: pk_sign() */
468 } ecrs_state; /*!< current (or last) operation */
469 mbedtls_x509_crt *ecrs_peer_cert; /*!< The peer's CRT chain. */
470 size_t ecrs_n; /*!< place for saving a length */
471#endif
472
473#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +0100474 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +0100475 mbedtls_ssl_sig_hash_set_t hash_algs; /*!< Set of suitable sig-hash pairs */
476#endif
Gilles Peskine4a13ebf2021-11-16 15:21:44 +0100477
Gilles Peskinebaccfef2021-11-16 17:44:31 +0100478 size_t pmslen; /*!< premaster length */
479
480 mbedtls_ssl_ciphersuite_t const *ciphersuite_info;
481
482 void (*update_checksum)(mbedtls_ssl_context *, const unsigned char *, size_t);
483 void (*calc_verify)(const mbedtls_ssl_context *, unsigned char *, size_t *);
484 void (*calc_finished)(mbedtls_ssl_context *, unsigned char *, int);
485 mbedtls_ssl_tls_prf_cb *tls_prf;
486
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200487#if defined(MBEDTLS_DHM_C)
488 mbedtls_dhm_context dhm_ctx; /*!< DHM key exchange */
489#endif
Gilles Peskine4a13ebf2021-11-16 15:21:44 +0100490
John Durkop07cc04a2020-11-16 22:08:34 -0800491/* Adding guard for MBEDTLS_ECDSA_C to ensure no compile errors due
492 * to guards also being in ssl_srv.c and ssl_cli.c. There is a gap
493 * in functionality that access to ecdh_ctx structure is needed for
494 * MBEDTLS_ECDSA_C which does not seem correct.
495 */
496#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200497 mbedtls_ecdh_context ecdh_ctx; /*!< ECDH key exchange */
Hanno Beckerdf51dbe2019-02-18 16:41:55 +0000498
499#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine42459802019-12-19 13:31:53 +0100500 psa_key_type_t ecdh_psa_type;
501 uint16_t ecdh_bits;
Ronald Croncf56a0a2020-08-04 09:51:30 +0200502 psa_key_id_t ecdh_psa_privkey;
Hanno Beckerdf51dbe2019-02-18 16:41:55 +0000503 unsigned char ecdh_psa_peerkey[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
504 size_t ecdh_psa_peerkey_len;
505#endif /* MBEDTLS_USE_PSA_CRYPTO */
John Durkop07cc04a2020-11-16 22:08:34 -0800506#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C */
Hanno Beckerdf51dbe2019-02-18 16:41:55 +0000507
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200508#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200509 mbedtls_ecjpake_context ecjpake_ctx; /*!< EC J-PAKE key exchange */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +0200510#if defined(MBEDTLS_SSL_CLI_C)
511 unsigned char *ecjpake_cache; /*!< Cache for ClientHello ext */
512 size_t ecjpake_cache_len; /*!< Length of cached data */
513#endif
Hanno Becker1aa267c2017-04-28 17:08:27 +0100514#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Gilles Peskine4a13ebf2021-11-16 15:21:44 +0100515
516#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200517 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200518 const mbedtls_ecp_curve_info **curves; /*!< Supported elliptic curves */
519#endif
Gilles Peskine4a13ebf2021-11-16 15:21:44 +0100520
Gilles Peskineeccd8882020-03-10 12:19:08 +0100521#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Beckerd9f7d432018-10-22 15:29:46 +0100522#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +0200523 psa_key_id_t psk_opaque; /*!< Opaque PSK from the callback */
Hanno Beckerd9f7d432018-10-22 15:29:46 +0100524#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200525 unsigned char *psk; /*!< PSK from the callback */
526 size_t psk_len; /*!< Length of PSK from callback */
Gilles Peskineeccd8882020-03-10 12:19:08 +0100527#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Gilles Peskine4a13ebf2021-11-16 15:21:44 +0100528
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200529#if defined(MBEDTLS_X509_CRT_PARSE_C)
530 mbedtls_ssl_key_cert *key_cert; /*!< chosen key/cert pair (server) */
531#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
532 mbedtls_ssl_key_cert *sni_key_cert; /*!< key/cert list from SNI */
533 mbedtls_x509_crt *sni_ca_chain; /*!< trusted CAs from SNI callback */
534 mbedtls_x509_crl *sni_ca_crl; /*!< trusted CAs CRLs from SNI */
Hanno Becker1aa267c2017-04-28 17:08:27 +0100535#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200536#endif /* MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine4a13ebf2021-11-16 15:21:44 +0100537
Gilles Peskineeccd8882020-03-10 12:19:08 +0100538#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +0200539 mbedtls_x509_crt_restart_ctx ecrs_ctx; /*!< restart context */
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +0200540#endif
Gilles Peskine4a13ebf2021-11-16 15:21:44 +0100541
542#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
Hanno Becker75173122019-02-06 16:18:31 +0000543 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
544 mbedtls_pk_context peer_pubkey; /*!< The public key from the peer. */
545#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine4a13ebf2021-11-16 15:21:44 +0100546
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200547#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckerd7f8ae22018-08-16 09:45:56 +0100548 struct
549 {
Hanno Beckere0b150f2018-08-21 15:51:03 +0100550 size_t total_bytes_buffered; /*!< Cumulative size of heap allocated
551 * buffers used for message buffering. */
552
Hanno Beckerd7f8ae22018-08-16 09:45:56 +0100553 uint8_t seen_ccs; /*!< Indicates if a CCS message has
Hanno Becker2ed6bcc2018-08-15 15:11:57 +0100554 * been seen in the current flight. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +0100555
Hanno Becker0271f962018-08-16 13:23:47 +0100556 struct mbedtls_ssl_hs_buffer
557 {
Hanno Becker98081a02018-08-22 13:32:50 +0100558 unsigned is_valid : 1;
559 unsigned is_fragmented : 1;
560 unsigned is_complete : 1;
Hanno Becker0271f962018-08-16 13:23:47 +0100561 unsigned char *data;
Hanno Beckere0b150f2018-08-21 15:51:03 +0100562 size_t data_len;
Hanno Becker0271f962018-08-16 13:23:47 +0100563 } hs[MBEDTLS_SSL_MAX_BUFFERED_HS];
564
Hanno Becker5f066e72018-08-16 14:56:31 +0100565 struct
566 {
567 unsigned char *data;
568 size_t len;
569 unsigned epoch;
570 } future_record;
571
Hanno Beckerd7f8ae22018-08-16 09:45:56 +0100572 } buffering;
Hanno Becker35462012018-08-22 10:25:40 +0100573
Gilles Peskinebaccfef2021-11-16 17:44:31 +0100574 unsigned int out_msg_seq; /*!< Outgoing handshake sequence number */
575 unsigned int in_msg_seq; /*!< Incoming handshake sequence number */
576
577 unsigned char *verify_cookie; /*!< Cli: HelloVerifyRequest cookie
578 Srv: unused */
579 unsigned char verify_cookie_len; /*!< Cli: cookie length
580 Srv: flag for sending a cookie */
581
582 uint32_t retransmit_timeout; /*!< Current value of timeout */
583 mbedtls_ssl_flight_item *flight; /*!< Current outgoing flight */
584 mbedtls_ssl_flight_item *cur_msg; /*!< Current message in flight */
585 unsigned char *cur_msg_p; /*!< Position in current message */
586 unsigned int in_flight_start_seq; /*!< Minimum message sequence in the
587 flight being received */
588 mbedtls_ssl_transform *alt_transform_out; /*!< Alternative transform for
589 resending messages */
590 unsigned char alt_out_ctr[8]; /*!< Alternative record epoch/counter
591 for resending messages */
592
593#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
594 /* The state of CID configuration in this handshake. */
595
596 uint8_t cid_in_use; /*!< This indicates whether the use of the CID extension
597 * has been negotiated. Possible values are
598 * #MBEDTLS_SSL_CID_ENABLED and
599 * #MBEDTLS_SSL_CID_DISABLED. */
600 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ]; /*! The peer's CID */
601 uint8_t peer_cid_len; /*!< The length of
602 * \c peer_cid. */
603#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
604
Manuel Pégourié-Gonnardf47a4af2018-08-22 10:38:52 +0200605 uint16_t mtu; /*!< Handshake mtu, used to fragment outgoing messages */
Hanno Becker1aa267c2017-04-28 17:08:27 +0100606#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200607
608 /*
609 * Checksum contexts
610 */
611#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
612 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine4a13ebf2021-11-16 15:21:44 +0100613 mbedtls_md5_context fin_md5;
614 mbedtls_sha1_context fin_sha1;
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200615#endif
616#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
617#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500618#if defined(MBEDTLS_USE_PSA_CRYPTO)
619 psa_hash_operation_t fin_sha256_psa;
620#else
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200621 mbedtls_sha256_context fin_sha256;
622#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500623#endif
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200624#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500625#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -0500626 psa_hash_operation_t fin_sha384_psa;
Andrzej Kurekeb342242019-01-29 09:14:33 -0500627#else
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200628 mbedtls_sha512_context fin_sha512;
629#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500630#endif
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200631#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
632
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200633 unsigned char randbytes[64]; /*!< random bytes */
634 unsigned char premaster[MBEDTLS_PREMASTER_SIZE];
635 /*!< premaster secret */
636
Gilles Peskinedf13d5c2018-04-25 20:39:48 +0200637#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
638 /** Asynchronous operation context. This field is meant for use by the
639 * asynchronous operation callbacks (mbedtls_ssl_config::f_async_sign_start,
640 * mbedtls_ssl_config::f_async_decrypt_start,
641 * mbedtls_ssl_config::f_async_resume, mbedtls_ssl_config::f_async_cancel).
642 * The library does not use it internally. */
643 void *user_async_ctx;
644#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200645};
646
Hanno Becker0271f962018-08-16 13:23:47 +0100647typedef struct mbedtls_ssl_hs_buffer mbedtls_ssl_hs_buffer;
648
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200649/*
Hanno Beckerd362dc52018-01-03 15:23:11 +0000650 * Representation of decryption/encryption transformations on records
651 *
652 * There are the following general types of record transformations:
653 * - Stream transformations (TLS versions <= 1.2 only)
654 * Transformation adding a MAC and applying a stream-cipher
655 * to the authenticated message.
656 * - CBC block cipher transformations ([D]TLS versions <= 1.2 only)
657 * In addition to the distinction of the order of encryption and
658 * authentication, there's a fundamental difference between the
659 * handling in SSL3 & TLS 1.0 and TLS 1.1 and TLS 1.2: For SSL3
660 * and TLS 1.0, the final IV after processing a record is used
661 * as the IV for the next record. No explicit IV is contained
662 * in an encrypted record. The IV for the first record is extracted
663 * at key extraction time. In contrast, for TLS 1.1 and 1.2, no
664 * IV is generated at key extraction time, but every encrypted
665 * record is explicitly prefixed by the IV with which it was encrypted.
666 * - AEAD transformations ([D]TLS versions >= 1.2 only)
667 * These come in two fundamentally different versions, the first one
668 * used in TLS 1.2, excluding ChaChaPoly ciphersuites, and the second
669 * one used for ChaChaPoly ciphersuites in TLS 1.2 as well as for TLS 1.3.
670 * In the first transformation, the IV to be used for a record is obtained
671 * as the concatenation of an explicit, static 4-byte IV and the 8-byte
672 * record sequence number, and explicitly prepending this sequence number
673 * to the encrypted record. In contrast, in the second transformation
674 * the IV is obtained by XOR'ing a static IV obtained at key extraction
675 * time with the 8-byte record sequence number, without prepending the
676 * latter to the encrypted record.
677 *
Hanno Becker7d343ec2020-05-04 12:29:05 +0100678 * Additionally, DTLS 1.2 + CID as well as TLS 1.3 use an inner plaintext
679 * which allows to add flexible length padding and to hide a record's true
680 * content type.
681 *
Hanno Beckerd362dc52018-01-03 15:23:11 +0000682 * In addition to type and version, the following parameters are relevant:
683 * - The symmetric cipher algorithm to be used.
684 * - The (static) encryption/decryption keys for the cipher.
685 * - For stream/CBC, the type of message digest to be used.
686 * - For stream/CBC, (static) encryption/decryption keys for the digest.
Hanno Becker0db7e0c2018-10-18 15:39:53 +0100687 * - For AEAD transformations, the size (potentially 0) of an explicit,
688 * random initialization vector placed in encrypted records.
Hanno Beckerd362dc52018-01-03 15:23:11 +0000689 * - For some transformations (currently AEAD and CBC in SSL3 and TLS 1.0)
690 * an implicit IV. It may be static (e.g. AEAD) or dynamic (e.g. CBC)
691 * and (if present) is combined with the explicit IV in a transformation-
692 * dependent way (e.g. appending in TLS 1.2 and XOR'ing in TLS 1.3).
693 * - For stream/CBC, a flag determining the order of encryption and MAC.
694 * - The details of the transformation depend on the SSL/TLS version.
695 * - The length of the authentication tag.
696 *
Hanno Becker0db7e0c2018-10-18 15:39:53 +0100697 * Note: Except for CBC in SSL3 and TLS 1.0, these parameters are
698 * constant across multiple encryption/decryption operations.
699 * For CBC, the implicit IV needs to be updated after each
700 * operation.
701 *
Hanno Beckerd362dc52018-01-03 15:23:11 +0000702 * The struct below refines this abstract view as follows:
703 * - The cipher underlying the transformation is managed in
704 * cipher contexts cipher_ctx_{enc/dec}, which must have the
705 * same cipher type. The mode of these cipher contexts determines
706 * the type of the transformation in the sense above: e.g., if
707 * the type is MBEDTLS_CIPHER_AES_256_CBC resp. MBEDTLS_CIPHER_AES_192_GCM
708 * then the transformation has type CBC resp. AEAD.
709 * - The cipher keys are never stored explicitly but
710 * are maintained within cipher_ctx_{enc/dec}.
711 * - For stream/CBC transformations, the message digest contexts
712 * used for the MAC's are stored in md_ctx_{enc/dec}. These contexts
713 * are unused for AEAD transformations.
714 * - For stream/CBC transformations and versions > SSL3, the
715 * MAC keys are not stored explicitly but maintained within
716 * md_ctx_{enc/dec}.
717 * - For stream/CBC transformations and version SSL3, the MAC
718 * keys are stored explicitly in mac_enc, mac_dec and have
719 * a fixed size of 20 bytes. These fields are unused for
720 * AEAD transformations or transformations >= TLS 1.0.
721 * - For transformations using an implicit IV maintained within
722 * the transformation context, its contents are stored within
723 * iv_{enc/dec}.
724 * - The value of ivlen indicates the length of the IV.
725 * This is redundant in case of stream/CBC transformations
726 * which always use 0 resp. the cipher's block length as the
727 * IV length, but is needed for AEAD ciphers and may be
728 * different from the underlying cipher's block length
729 * in this case.
730 * - The field fixed_ivlen is nonzero for AEAD transformations only
731 * and indicates the length of the static part of the IV which is
732 * constant throughout the communication, and which is stored in
733 * the first fixed_ivlen bytes of the iv_{enc/dec} arrays.
734 * Note: For CBC in SSL3 and TLS 1.0, the fields iv_{enc/dec}
735 * still store IV's for continued use across multiple transformations,
736 * so it is not true that fixed_ivlen == 0 means that iv_{enc/dec} are
737 * not being used!
738 * - minor_ver denotes the SSL/TLS version
739 * - For stream/CBC transformations, maclen denotes the length of the
740 * authentication tag, while taglen is unused and 0.
741 * - For AEAD transformations, taglen denotes the length of the
742 * authentication tag, while maclen is unused and 0.
743 * - For CBC transformations, encrypt_then_mac determines the
744 * order of encryption and authentication. This field is unused
745 * in other transformations.
746 *
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200747 */
748struct mbedtls_ssl_transform
749{
750 /*
751 * Session specific crypto layer
752 */
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200753 size_t minlen; /*!< min. ciphertext length */
754 size_t ivlen; /*!< IV length */
755 size_t fixed_ivlen; /*!< Fixed part of IV (AEAD) */
Hanno Beckere694c3e2017-12-27 21:34:08 +0000756 size_t maclen; /*!< MAC(CBC) len */
757 size_t taglen; /*!< TAG(AEAD) len */
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200758
759 unsigned char iv_enc[16]; /*!< IV (encryption) */
760 unsigned char iv_dec[16]; /*!< IV (decryption) */
761
Hanno Beckerd56ed242018-01-03 15:32:51 +0000762#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
763
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200764#if defined(MBEDTLS_SSL_PROTO_SSL3)
765 /* Needed only for SSL v3.0 secret */
766 unsigned char mac_enc[20]; /*!< SSL v3.0 secret (enc) */
767 unsigned char mac_dec[20]; /*!< SSL v3.0 secret (dec) */
768#endif /* MBEDTLS_SSL_PROTO_SSL3 */
769
770 mbedtls_md_context_t md_ctx_enc; /*!< MAC (encryption) */
771 mbedtls_md_context_t md_ctx_dec; /*!< MAC (decryption) */
772
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000773#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
774 int encrypt_then_mac; /*!< flag for EtM activation */
775#endif
776
Hanno Beckerd56ed242018-01-03 15:32:51 +0000777#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
778
779 mbedtls_cipher_context_t cipher_ctx_enc; /*!< encryption context */
780 mbedtls_cipher_context_t cipher_ctx_dec; /*!< decryption context */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000781 int minor_ver;
782
Hanno Beckera0e20d02019-05-15 14:03:01 +0100783#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker1327fa72019-04-25 15:54:02 +0100784 uint8_t in_cid_len;
785 uint8_t out_cid_len;
786 unsigned char in_cid [ MBEDTLS_SSL_CID_OUT_LEN_MAX ];
787 unsigned char out_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckera0e20d02019-05-15 14:03:01 +0100788#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1327fa72019-04-25 15:54:02 +0100789
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200790 /*
791 * Session specific compression layer
792 */
793#if defined(MBEDTLS_ZLIB_SUPPORT)
794 z_stream ctx_deflate; /*!< compression context */
795 z_stream ctx_inflate; /*!< decompression context */
796#endif
Manuel Pégourié-Gonnard96fb0ee2019-07-09 12:54:17 +0200797
798#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
799 /* We need the Hello random bytes in order to re-derive keys from the
800 * Master Secret and other session info, see ssl_populate_transform() */
801 unsigned char randbytes[64]; /*!< ServerHello.random+ClientHello.random */
802#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200803};
804
Hanno Becker12a3a862018-01-05 15:42:50 +0000805/*
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +0200806 * Return 1 if the transform uses an AEAD cipher, 0 otherwise.
807 * Equivalently, return 0 if a separate MAC is used, 1 otherwise.
808 */
809static inline int mbedtls_ssl_transform_uses_aead(
810 const mbedtls_ssl_transform *transform )
811{
812#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
813 return( transform->maclen == 0 && transform->taglen != 0 );
814#else
815 (void) transform;
816 return( 1 );
817#endif
818}
819
820/*
Hanno Becker12a3a862018-01-05 15:42:50 +0000821 * Internal representation of record frames
822 *
Hanno Becker12a3a862018-01-05 15:42:50 +0000823 * Instances come in two flavors:
824 * (1) Encrypted
825 * These always have data_offset = 0
826 * (2) Unencrypted
Hanno Beckercd430bc2019-04-04 16:29:48 +0100827 * These have data_offset set to the amount of
828 * pre-expansion during record protection. Concretely,
829 * this is the length of the fixed part of the explicit IV
830 * used for encryption, or 0 if no explicit IV is used
831 * (e.g. for CBC in TLS 1.0, or stream ciphers).
Hanno Becker12a3a862018-01-05 15:42:50 +0000832 *
833 * The reason for the data_offset in the unencrypted case
834 * is to allow for in-place conversion of an unencrypted to
835 * an encrypted record. If the offset wasn't included, the
836 * encrypted content would need to be shifted afterwards to
837 * make space for the fixed IV.
838 *
839 */
Hanno Beckerf2ed4482019-04-29 13:45:54 +0100840#if MBEDTLS_SSL_CID_OUT_LEN_MAX > MBEDTLS_SSL_CID_IN_LEN_MAX
Hanno Becker75f080f2019-04-30 15:01:51 +0100841#define MBEDTLS_SSL_CID_LEN_MAX MBEDTLS_SSL_CID_OUT_LEN_MAX
Hanno Beckerf2ed4482019-04-29 13:45:54 +0100842#else
Hanno Becker75f080f2019-04-30 15:01:51 +0100843#define MBEDTLS_SSL_CID_LEN_MAX MBEDTLS_SSL_CID_IN_LEN_MAX
Hanno Beckerf2ed4482019-04-29 13:45:54 +0100844#endif
845
Hanno Becker12a3a862018-01-05 15:42:50 +0000846typedef struct
847{
Hanno Beckerd840cea2019-07-11 09:24:36 +0100848 uint8_t ctr[8]; /* In TLS: The implicit record sequence number.
849 * In DTLS: The 2-byte epoch followed by
850 * the 6-byte sequence number.
851 * This is stored as a raw big endian byte array
852 * as opposed to a uint64_t because we rarely
853 * need to perform arithmetic on this, but do
854 * need it as a Byte array for the purpose of
855 * MAC computations. */
856 uint8_t type; /* The record content type. */
857 uint8_t ver[2]; /* SSL/TLS version as present on the wire.
858 * Convert to internal presentation of versions
859 * using mbedtls_ssl_read_version() and
860 * mbedtls_ssl_write_version().
861 * Keep wire-format for MAC computations. */
Hanno Becker12a3a862018-01-05 15:42:50 +0000862
Hanno Beckerd840cea2019-07-11 09:24:36 +0100863 unsigned char *buf; /* Memory buffer enclosing the record content */
864 size_t buf_len; /* Buffer length */
865 size_t data_offset; /* Offset of record content */
866 size_t data_len; /* Length of record content */
Hanno Becker12a3a862018-01-05 15:42:50 +0000867
Hanno Beckera0e20d02019-05-15 14:03:01 +0100868#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd840cea2019-07-11 09:24:36 +0100869 uint8_t cid_len; /* Length of the CID (0 if not present) */
870 unsigned char cid[ MBEDTLS_SSL_CID_LEN_MAX ]; /* The CID */
Hanno Beckera0e20d02019-05-15 14:03:01 +0100871#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker12a3a862018-01-05 15:42:50 +0000872} mbedtls_record;
873
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200874#if defined(MBEDTLS_X509_CRT_PARSE_C)
875/*
876 * List of certificate + private key pairs
877 */
878struct mbedtls_ssl_key_cert
879{
880 mbedtls_x509_crt *cert; /*!< cert */
881 mbedtls_pk_context *key; /*!< private key */
882 mbedtls_ssl_key_cert *next; /*!< next key/cert pair */
883};
884#endif /* MBEDTLS_X509_CRT_PARSE_C */
885
886#if defined(MBEDTLS_SSL_PROTO_DTLS)
887/*
888 * List of handshake messages kept around for resending
889 */
890struct mbedtls_ssl_flight_item
891{
892 unsigned char *p; /*!< message, including handshake headers */
893 size_t len; /*!< length of p */
894 unsigned char type; /*!< type of the message: handshake or CCS */
895 mbedtls_ssl_flight_item *next; /*!< next handshake message(s) */
896};
897#endif /* MBEDTLS_SSL_PROTO_DTLS */
898
Hanno Becker7e5437a2017-04-28 17:15:26 +0100899#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +0100900 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +0100901
902/* Find an entry in a signature-hash set matching a given hash algorithm. */
903mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
904 mbedtls_pk_type_t sig_alg );
905/* Add a signature-hash-pair to a signature-hash set */
906void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
907 mbedtls_pk_type_t sig_alg,
908 mbedtls_md_type_t md_alg );
909/* Allow exactly one hash algorithm for each signature. */
910void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
911 mbedtls_md_type_t md_alg );
912
913/* Setup an empty signature-hash set */
914static inline void mbedtls_ssl_sig_hash_set_init( mbedtls_ssl_sig_hash_set_t *set )
915{
916 mbedtls_ssl_sig_hash_set_const_hash( set, MBEDTLS_MD_NONE );
917}
918
919#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
Gilles Peskineeccd8882020-03-10 12:19:08 +0100920 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200921
922/**
923 * \brief Free referenced items in an SSL transform context and clear
924 * memory
925 *
926 * \param transform SSL transform context
927 */
928void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform );
929
930/**
931 * \brief Free referenced items in an SSL handshake context and clear
932 * memory
933 *
Gilles Peskine9b562d52018-04-25 20:32:43 +0200934 * \param ssl SSL context
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200935 */
Gilles Peskine9b562d52018-04-25 20:32:43 +0200936void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200937
Manuel Pégourié-Gonnard57210592022-06-17 10:32:25 +0200938MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +0200939int mbedtls_ssl_handshake_client_step( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnard57210592022-06-17 10:32:25 +0200940MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +0200941int mbedtls_ssl_handshake_server_step( mbedtls_ssl_context *ssl );
942void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl );
943
Manuel Pégourié-Gonnard57210592022-06-17 10:32:25 +0200944MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +0200945int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl );
946
947void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnard57210592022-06-17 10:32:25 +0200948MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +0200949int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl );
950
Manuel Pégourié-Gonnard57210592022-06-17 10:32:25 +0200951MBEDTLS_CHECK_RETURN_CRITICAL
Simon Butcher99000142016-10-13 17:21:01 +0100952int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnard57210592022-06-17 10:32:25 +0200953MBEDTLS_CHECK_RETURN_CRITICAL
Simon Butcher99000142016-10-13 17:21:01 +0100954int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl );
955void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl );
956
Hanno Becker4a810fb2017-05-24 16:27:30 +0100957/**
958 * \brief Update record layer
959 *
960 * This function roughly separates the implementation
961 * of the logic of (D)TLS from the implementation
962 * of the secure transport.
963 *
Hanno Becker3a0aad12018-08-20 09:44:02 +0100964 * \param ssl The SSL context to use.
965 * \param update_hs_digest This indicates if the handshake digest
966 * should be automatically updated in case
967 * a handshake message is found.
Hanno Becker4a810fb2017-05-24 16:27:30 +0100968 *
969 * \return 0 or non-zero error code.
970 *
971 * \note A clarification on what is called 'record layer' here
972 * is in order, as many sensible definitions are possible:
973 *
974 * The record layer takes as input an untrusted underlying
975 * transport (stream or datagram) and transforms it into
976 * a serially multiplexed, secure transport, which
977 * conceptually provides the following:
978 *
979 * (1) Three datagram based, content-agnostic transports
980 * for handshake, alert and CCS messages.
981 * (2) One stream- or datagram-based transport
982 * for application data.
983 * (3) Functionality for changing the underlying transform
984 * securing the contents.
985 *
986 * The interface to this functionality is given as follows:
987 *
988 * a Updating
989 * [Currently implemented by mbedtls_ssl_read_record]
990 *
991 * Check if and on which of the four 'ports' data is pending:
992 * Nothing, a controlling datagram of type (1), or application
993 * data (2). In any case data is present, internal buffers
994 * provide access to the data for the user to process it.
995 * Consumption of type (1) datagrams is done automatically
996 * on the next update, invalidating that the internal buffers
997 * for previous datagrams, while consumption of application
998 * data (2) is user-controlled.
999 *
1000 * b Reading of application data
1001 * [Currently manual adaption of ssl->in_offt pointer]
1002 *
1003 * As mentioned in the last paragraph, consumption of data
1004 * is different from the automatic consumption of control
1005 * datagrams (1) because application data is treated as a stream.
1006 *
1007 * c Tracking availability of application data
1008 * [Currently manually through decreasing ssl->in_msglen]
1009 *
1010 * For efficiency and to retain datagram semantics for
1011 * application data in case of DTLS, the record layer
1012 * provides functionality for checking how much application
1013 * data is still available in the internal buffer.
1014 *
1015 * d Changing the transformation securing the communication.
1016 *
1017 * Given an opaque implementation of the record layer in the
1018 * above sense, it should be possible to implement the logic
1019 * of (D)TLS on top of it without the need to know anything
1020 * about the record layer's internals. This is done e.g.
1021 * in all the handshake handling functions, and in the
1022 * application data reading function mbedtls_ssl_read.
1023 *
1024 * \note The above tries to give a conceptual picture of the
1025 * record layer, but the current implementation deviates
1026 * from it in some places. For example, our implementation of
1027 * the update functionality through mbedtls_ssl_read_record
1028 * discards datagrams depending on the current state, which
1029 * wouldn't fall under the record layer's responsibility
1030 * following the above definition.
1031 *
1032 */
Manuel Pégourié-Gonnard57210592022-06-17 10:32:25 +02001033MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker3a0aad12018-08-20 09:44:02 +01001034int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
1035 unsigned update_hs_digest );
Manuel Pégourié-Gonnard57210592022-06-17 10:32:25 +02001036MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +02001037int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want );
1038
Manuel Pégourié-Gonnard57210592022-06-17 10:32:25 +02001039MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02001040int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnard57210592022-06-17 10:32:25 +02001041MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker67bc7c32018-08-06 11:33:50 +01001042int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush );
Manuel Pégourié-Gonnard57210592022-06-17 10:32:25 +02001043MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +02001044int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl );
1045
Manuel Pégourié-Gonnard57210592022-06-17 10:32:25 +02001046MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +02001047int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnard57210592022-06-17 10:32:25 +02001048MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +02001049int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl );
1050
Manuel Pégourié-Gonnard57210592022-06-17 10:32:25 +02001051MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +02001052int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnard57210592022-06-17 10:32:25 +02001053MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +02001054int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl );
1055
Manuel Pégourié-Gonnard57210592022-06-17 10:32:25 +02001056MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +02001057int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnard57210592022-06-17 10:32:25 +02001058MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +02001059int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl );
1060
1061void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
1062 const mbedtls_ssl_ciphersuite_t *ciphersuite_info );
1063
Gilles Peskineeccd8882020-03-10 12:19:08 +01001064#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnard57210592022-06-17 10:32:25 +02001065MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +02001066int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex );
Guilhem Bryantd511ac32020-03-25 17:06:37 +00001067
Guilhem Bryant8a69ddd2020-03-27 11:13:39 +00001068/**
Guilhem Bryantd511ac32020-03-25 17:06:37 +00001069 * Get the first defined PSK by order of precedence:
1070 * 1. handshake PSK set by \c mbedtls_ssl_set_hs_psk() in the PSK callback
1071 * 2. static PSK configured by \c mbedtls_ssl_conf_psk()
1072 * Return a code and update the pair (PSK, PSK length) passed to this function
1073 */
1074static inline int mbedtls_ssl_get_psk( const mbedtls_ssl_context *ssl,
1075 const unsigned char **psk, size_t *psk_len )
1076{
1077 if( ssl->handshake->psk != NULL && ssl->handshake->psk_len > 0 )
1078 {
1079 *psk = ssl->handshake->psk;
1080 *psk_len = ssl->handshake->psk_len;
1081 }
1082
1083 else if( ssl->conf->psk != NULL && ssl->conf->psk_len > 0 )
1084 {
1085 *psk = ssl->conf->psk;
1086 *psk_len = ssl->conf->psk_len;
1087 }
1088
1089 else
1090 {
Guilhem Bryantb5f04e42020-04-01 11:23:58 +01001091 *psk = NULL;
1092 *psk_len = 0;
Guilhem Bryantd511ac32020-03-25 17:06:37 +00001093 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
1094 }
1095
1096 return( 0 );
1097}
1098
1099#if defined(MBEDTLS_USE_PSA_CRYPTO)
Guilhem Bryant8a69ddd2020-03-27 11:13:39 +00001100/**
Guilhem Bryantd511ac32020-03-25 17:06:37 +00001101 * Get the first defined opaque PSK by order of precedence:
1102 * 1. handshake PSK set by \c mbedtls_ssl_set_hs_psk_opaque() in the PSK
1103 * callback
1104 * 2. static PSK configured by \c mbedtls_ssl_conf_psk_opaque()
1105 * Return an opaque PSK
1106 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001107static inline psa_key_id_t mbedtls_ssl_get_opaque_psk(
Guilhem Bryantd511ac32020-03-25 17:06:37 +00001108 const mbedtls_ssl_context *ssl )
1109{
Ronald Croncf56a0a2020-08-04 09:51:30 +02001110 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
Guilhem Bryantd511ac32020-03-25 17:06:37 +00001111 return( ssl->handshake->psk_opaque );
1112
Ronald Croncf56a0a2020-08-04 09:51:30 +02001113 if( ! mbedtls_svc_key_id_is_null( ssl->conf->psk_opaque ) )
Guilhem Bryantd511ac32020-03-25 17:06:37 +00001114 return( ssl->conf->psk_opaque );
1115
Ronald Croncf56a0a2020-08-04 09:51:30 +02001116 return( MBEDTLS_SVC_KEY_ID_INIT );
Guilhem Bryantd511ac32020-03-25 17:06:37 +00001117}
1118#endif /* MBEDTLS_USE_PSA_CRYPTO */
1119
1120#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +02001121
1122#if defined(MBEDTLS_PK_C)
1123unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk );
Hanno Becker7e5437a2017-04-28 17:15:26 +01001124unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type );
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +02001125mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig );
1126#endif
1127
1128mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash );
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02001129unsigned char mbedtls_ssl_hash_from_md_alg( int md );
Manuel Pégourié-Gonnard57210592022-06-17 10:32:25 +02001130MBEDTLS_CHECK_RETURN_CRITICAL
Simon Butcher99000142016-10-13 17:21:01 +01001131int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md );
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +02001132
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02001133#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard57210592022-06-17 10:32:25 +02001134MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02001135int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id );
Manuel Pégourié-Gonnard57210592022-06-17 10:32:25 +02001136MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard298d6cc2022-02-14 11:34:47 +01001137int mbedtls_ssl_check_curve_tls_id( const mbedtls_ssl_context *ssl, uint16_t tls_id );
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +02001138#endif
1139
Gilles Peskineeccd8882020-03-10 12:19:08 +01001140#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Manuel Pégourié-Gonnard57210592022-06-17 10:32:25 +02001141MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02001142int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
1143 mbedtls_md_type_t md );
1144#endif
1145
Ron Eldor089c9fe2018-12-06 17:12:49 +02001146#if defined(MBEDTLS_SSL_DTLS_SRTP)
Johan Pascal43f94902020-09-22 12:25:52 +02001147static inline mbedtls_ssl_srtp_profile mbedtls_ssl_check_srtp_profile_value
1148 ( const uint16_t srtp_profile_value )
Ron Eldor089c9fe2018-12-06 17:12:49 +02001149{
Johan Pascal43f94902020-09-22 12:25:52 +02001150 switch( srtp_profile_value )
Ron Eldor089c9fe2018-12-06 17:12:49 +02001151 {
Johan Pascal85269572020-08-25 10:01:54 +02001152 case MBEDTLS_TLS_SRTP_AES128_CM_HMAC_SHA1_80:
Johan Pascal85269572020-08-25 10:01:54 +02001153 case MBEDTLS_TLS_SRTP_AES128_CM_HMAC_SHA1_32:
Johan Pascal85269572020-08-25 10:01:54 +02001154 case MBEDTLS_TLS_SRTP_NULL_HMAC_SHA1_80:
Johan Pascal85269572020-08-25 10:01:54 +02001155 case MBEDTLS_TLS_SRTP_NULL_HMAC_SHA1_32:
Johan Pascal43f94902020-09-22 12:25:52 +02001156 return srtp_profile_value;
Ron Eldor089c9fe2018-12-06 17:12:49 +02001157 default: break;
1158 }
Johan Pascal43f94902020-09-22 12:25:52 +02001159 return( MBEDTLS_TLS_SRTP_UNSET );
Ron Eldor089c9fe2018-12-06 17:12:49 +02001160}
1161#endif
1162
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +02001163#if defined(MBEDTLS_X509_CRT_PARSE_C)
1164static inline mbedtls_pk_context *mbedtls_ssl_own_key( mbedtls_ssl_context *ssl )
1165{
1166 mbedtls_ssl_key_cert *key_cert;
1167
1168 if( ssl->handshake != NULL && ssl->handshake->key_cert != NULL )
1169 key_cert = ssl->handshake->key_cert;
1170 else
1171 key_cert = ssl->conf->key_cert;
1172
1173 return( key_cert == NULL ? NULL : key_cert->key );
1174}
1175
1176static inline mbedtls_x509_crt *mbedtls_ssl_own_cert( mbedtls_ssl_context *ssl )
1177{
1178 mbedtls_ssl_key_cert *key_cert;
1179
1180 if( ssl->handshake != NULL && ssl->handshake->key_cert != NULL )
1181 key_cert = ssl->handshake->key_cert;
1182 else
1183 key_cert = ssl->conf->key_cert;
1184
1185 return( key_cert == NULL ? NULL : key_cert->cert );
1186}
1187
1188/*
1189 * Check usage of a certificate wrt extensions:
1190 * keyUsage, extendedKeyUsage (later), and nSCertType (later).
1191 *
1192 * Warning: cert_endpoint is the endpoint of the cert (ie, of our peer when we
1193 * check a cert we received from them)!
1194 *
1195 * Return 0 if everything is OK, -1 if not.
1196 */
Manuel Pégourié-Gonnard57210592022-06-17 10:32:25 +02001197MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +02001198int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
1199 const mbedtls_ssl_ciphersuite_t *ciphersuite,
1200 int cert_endpoint,
1201 uint32_t *flags );
1202#endif /* MBEDTLS_X509_CRT_PARSE_C */
1203
1204void mbedtls_ssl_write_version( int major, int minor, int transport,
1205 unsigned char ver[2] );
1206void mbedtls_ssl_read_version( int *major, int *minor, int transport,
1207 const unsigned char ver[2] );
1208
Hanno Becker5903de42019-05-03 14:46:38 +01001209static inline size_t mbedtls_ssl_in_hdr_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +02001210{
Hanno Becker47be7682019-07-12 09:55:46 +01001211#if !defined(MBEDTLS_SSL_PROTO_DTLS)
1212 ((void) ssl);
1213#endif
1214
1215#if defined(MBEDTLS_SSL_PROTO_DTLS)
1216 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
1217 {
1218 return( 13 );
1219 }
1220 else
1221#endif /* MBEDTLS_SSL_PROTO_DTLS */
1222 {
1223 return( 5 );
1224 }
Hanno Becker5903de42019-05-03 14:46:38 +01001225}
1226
1227static inline size_t mbedtls_ssl_out_hdr_len( const mbedtls_ssl_context *ssl )
1228{
Hanno Becker3b154c12019-05-03 15:05:27 +01001229 return( (size_t) ( ssl->out_iv - ssl->out_hdr ) );
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +02001230}
1231
1232static inline size_t mbedtls_ssl_hs_hdr_len( const mbedtls_ssl_context *ssl )
1233{
1234#if defined(MBEDTLS_SSL_PROTO_DTLS)
1235 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
1236 return( 12 );
1237#else
1238 ((void) ssl);
1239#endif
1240 return( 4 );
1241}
1242
1243#if defined(MBEDTLS_SSL_PROTO_DTLS)
1244void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl );
1245void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnard57210592022-06-17 10:32:25 +02001246MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +02001247int mbedtls_ssl_resend( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnard57210592022-06-17 10:32:25 +02001248MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02001249int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +02001250#endif
1251
1252/* Visible for testing purposes only */
1253#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard57210592022-06-17 10:32:25 +02001254MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker0183d692019-07-12 08:50:37 +01001255int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context const *ssl );
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +02001256void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl );
1257#endif
1258
Manuel Pégourié-Gonnard57210592022-06-17 10:32:25 +02001259MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker52055ae2019-02-06 14:30:46 +00001260int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
1261 const mbedtls_ssl_session *src );
1262
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01001263#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
1264 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard57210592022-06-17 10:32:25 +02001265MBEDTLS_CHECK_RETURN_CRITICAL
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01001266int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
1267 unsigned char *output,
1268 unsigned char *data, size_t data_len );
1269#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
1270 MBEDTLS_SSL_PROTO_TLS1_1 */
1271
1272#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1273 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek814feff2019-01-14 04:35:19 -05001274/* The hash buffer must have at least MBEDTLS_MD_MAX_SIZE bytes of length. */
Manuel Pégourié-Gonnard57210592022-06-17 10:32:25 +02001275MBEDTLS_CHECK_RETURN_CRITICAL
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01001276int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +02001277 unsigned char *hash, size_t *hashlen,
1278 unsigned char *data, size_t data_len,
1279 mbedtls_md_type_t md_alg );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01001280#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
1281 MBEDTLS_SSL_PROTO_TLS1_2 */
1282
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +02001283#ifdef __cplusplus
1284}
1285#endif
1286
Hanno Beckera18d1322018-01-03 14:27:32 +00001287void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform );
Manuel Pégourié-Gonnard57210592022-06-17 10:32:25 +02001288MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckera18d1322018-01-03 14:27:32 +00001289int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
1290 mbedtls_ssl_transform *transform,
1291 mbedtls_record *rec,
1292 int (*f_rng)(void *, unsigned char *, size_t),
1293 void *p_rng );
Manuel Pégourié-Gonnard57210592022-06-17 10:32:25 +02001294MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker605949f2019-07-12 08:23:59 +01001295int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl,
Hanno Beckera18d1322018-01-03 14:27:32 +00001296 mbedtls_ssl_transform *transform,
1297 mbedtls_record *rec );
1298
Hanno Beckerdd772292020-02-05 10:38:31 +00001299/* Length of the "epoch" field in the record header */
1300static inline size_t mbedtls_ssl_ep_len( const mbedtls_ssl_context *ssl )
1301{
1302#if defined(MBEDTLS_SSL_PROTO_DTLS)
1303 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
1304 return( 2 );
1305#else
1306 ((void) ssl);
1307#endif
1308 return( 0 );
1309}
1310
Hanno Becker08f09132020-02-11 15:40:07 +00001311#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard57210592022-06-17 10:32:25 +02001312MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker786300f2020-02-05 10:46:40 +00001313int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl );
Hanno Becker08f09132020-02-11 15:40:07 +00001314#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker0f57a652020-02-05 10:37:26 +00001315
1316void mbedtls_ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs );
Manuel Pégourié-Gonnard57210592022-06-17 10:32:25 +02001317MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker7876d122020-02-05 10:39:31 +00001318int mbedtls_ssl_check_timer( mbedtls_ssl_context *ssl );
1319
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00001320void mbedtls_ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl );
1321void mbedtls_ssl_update_out_pointers( mbedtls_ssl_context *ssl,
1322 mbedtls_ssl_transform *transform );
1323void mbedtls_ssl_update_in_pointers( mbedtls_ssl_context *ssl );
1324
Manuel Pégourié-Gonnard57210592022-06-17 10:32:25 +02001325MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker43aefe22020-02-05 10:44:56 +00001326int mbedtls_ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
1327
Hanno Becker7e8e6a62020-02-05 10:45:48 +00001328#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
1329void mbedtls_ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
1330#endif
1331
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00001332void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
1333
Hanno Becker08f09132020-02-11 15:40:07 +00001334#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard57210592022-06-17 10:32:25 +02001335MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker40cdaa12020-02-05 10:48:27 +00001336int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl );
Hanno Becker08f09132020-02-11 15:40:07 +00001337#endif /* MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker89490712020-02-05 10:50:12 +00001338
Hanno Becker533ab5f2020-02-05 10:49:13 +00001339#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker08f09132020-02-11 15:40:07 +00001340size_t mbedtls_ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker533ab5f2020-02-05 10:49:13 +00001341void mbedtls_ssl_buffering_free( mbedtls_ssl_context *ssl );
1342void mbedtls_ssl_flight_free( mbedtls_ssl_flight_item *flight );
1343#endif /* MBEDTLS_SSL_PROTO_DTLS */
1344
Andrzej Kurek862acb82022-06-06 13:08:23 -04001345#if defined(MBEDTLS_TEST_HOOKS)
Andrzej Kurek33f41a82022-06-08 11:47:33 -04001346int mbedtls_ssl_check_dtls_clihlo_cookie(
Andrzej Kurek862acb82022-06-06 13:08:23 -04001347 mbedtls_ssl_context *ssl,
1348 const unsigned char *cli_id, size_t cli_id_len,
1349 const unsigned char *in, size_t in_len,
1350 unsigned char *obuf, size_t buf_len, size_t *olen );
1351#endif
1352
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +02001353#endif /* ssl_internal.h */