blob: 35b8eedc04bd366e6a73fef7bf5278f05fc86ea4 [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/*
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02007 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Bence Szépkúti4e9f7122020-06-05 13:02:18 +02008 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
9 *
10 * This file is provided under the Apache License 2.0, or the
11 * GNU General Public License v2.0 or later.
12 *
13 * **********
14 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020015 *
16 * Licensed under the Apache License, Version 2.0 (the "License"); you may
17 * not use this file except in compliance with the License.
18 * You may obtain a copy of the License at
19 *
20 * http://www.apache.org/licenses/LICENSE-2.0
21 *
22 * Unless required by applicable law or agreed to in writing, software
23 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
24 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 * See the License for the specific language governing permissions and
26 * limitations under the License.
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020027 *
Bence Szépkúti4e9f7122020-06-05 13:02:18 +020028 * **********
29 *
30 * **********
31 * GNU General Public License v2.0 or later:
32 *
33 * This program is free software; you can redistribute it and/or modify
34 * it under the terms of the GNU General Public License as published by
35 * the Free Software Foundation; either version 2 of the License, or
36 * (at your option) any later version.
37 *
38 * This program is distributed in the hope that it will be useful,
39 * but WITHOUT ANY WARRANTY; without even the implied warranty of
40 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
41 * GNU General Public License for more details.
42 *
43 * You should have received a copy of the GNU General Public License along
44 * with this program; if not, write to the Free Software Foundation, Inc.,
45 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
46 *
47 * **********
48 *
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020049 * This file is part of mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020050 */
51#ifndef MBEDTLS_SSL_INTERNAL_H
52#define MBEDTLS_SSL_INTERNAL_H
53
Ron Eldor0559c662018-02-14 16:02:41 +020054#if !defined(MBEDTLS_CONFIG_FILE)
55#include "config.h"
56#else
57#include MBEDTLS_CONFIG_FILE
58#endif
59
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020060#include "ssl.h"
Hanno Beckera8434e82017-09-18 10:54:39 +010061#include "cipher.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020062
Manuel Pégourié-Gonnard56273da2015-05-26 12:19:45 +020063#if defined(MBEDTLS_MD5_C)
64#include "md5.h"
65#endif
66
67#if defined(MBEDTLS_SHA1_C)
68#include "sha1.h"
69#endif
70
71#if defined(MBEDTLS_SHA256_C)
72#include "sha256.h"
73#endif
74
75#if defined(MBEDTLS_SHA512_C)
76#include "sha512.h"
77#endif
78
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020079#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020080#include "ecjpake.h"
81#endif
82
Manuel Pégourié-Gonnard0223ab92015-10-05 11:40:01 +010083#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
84 !defined(inline) && !defined(__cplusplus)
Manuel Pégourié-Gonnard065122c2015-05-26 12:31:46 +020085#define inline __inline
Manuel Pégourié-Gonnard20af64d2015-07-07 18:33:39 +020086#endif
Manuel Pégourié-Gonnard065122c2015-05-26 12:31:46 +020087
88/* Determine minimum supported version */
89#define MBEDTLS_SSL_MIN_MAJOR_VERSION MBEDTLS_SSL_MAJOR_VERSION_3
90
91#if defined(MBEDTLS_SSL_PROTO_SSL3)
92#define MBEDTLS_SSL_MIN_MINOR_VERSION MBEDTLS_SSL_MINOR_VERSION_0
93#else
94#if defined(MBEDTLS_SSL_PROTO_TLS1)
95#define MBEDTLS_SSL_MIN_MINOR_VERSION MBEDTLS_SSL_MINOR_VERSION_1
96#else
97#if defined(MBEDTLS_SSL_PROTO_TLS1_1)
98#define MBEDTLS_SSL_MIN_MINOR_VERSION MBEDTLS_SSL_MINOR_VERSION_2
99#else
100#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
101#define MBEDTLS_SSL_MIN_MINOR_VERSION MBEDTLS_SSL_MINOR_VERSION_3
102#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
103#endif /* MBEDTLS_SSL_PROTO_TLS1_1 */
104#endif /* MBEDTLS_SSL_PROTO_TLS1 */
105#endif /* MBEDTLS_SSL_PROTO_SSL3 */
106
Ron Eldor5e9f14d2017-05-28 10:46:38 +0300107#define MBEDTLS_SSL_MIN_VALID_MINOR_VERSION MBEDTLS_SSL_MINOR_VERSION_1
108#define MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION MBEDTLS_SSL_MAJOR_VERSION_3
109
Manuel Pégourié-Gonnard065122c2015-05-26 12:31:46 +0200110/* Determine maximum supported version */
111#define MBEDTLS_SSL_MAX_MAJOR_VERSION MBEDTLS_SSL_MAJOR_VERSION_3
112
113#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
114#define MBEDTLS_SSL_MAX_MINOR_VERSION MBEDTLS_SSL_MINOR_VERSION_3
115#else
116#if defined(MBEDTLS_SSL_PROTO_TLS1_1)
117#define MBEDTLS_SSL_MAX_MINOR_VERSION MBEDTLS_SSL_MINOR_VERSION_2
118#else
119#if defined(MBEDTLS_SSL_PROTO_TLS1)
120#define MBEDTLS_SSL_MAX_MINOR_VERSION MBEDTLS_SSL_MINOR_VERSION_1
121#else
122#if defined(MBEDTLS_SSL_PROTO_SSL3)
123#define MBEDTLS_SSL_MAX_MINOR_VERSION MBEDTLS_SSL_MINOR_VERSION_0
124#endif /* MBEDTLS_SSL_PROTO_SSL3 */
125#endif /* MBEDTLS_SSL_PROTO_TLS1 */
126#endif /* MBEDTLS_SSL_PROTO_TLS1_1 */
127#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
128
129#define MBEDTLS_SSL_INITIAL_HANDSHAKE 0
130#define MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS 1 /* In progress */
131#define MBEDTLS_SSL_RENEGOTIATION_DONE 2 /* Done or aborted */
132#define MBEDTLS_SSL_RENEGOTIATION_PENDING 3 /* Requested (server only) */
133
134/*
135 * DTLS retransmission states, see RFC 6347 4.2.4
136 *
137 * The SENDING state is merged in PREPARING for initial sends,
138 * but is distinct for resends.
139 *
140 * Note: initial state is wrong for server, but is not used anyway.
141 */
142#define MBEDTLS_SSL_RETRANS_PREPARING 0
143#define MBEDTLS_SSL_RETRANS_SENDING 1
144#define MBEDTLS_SSL_RETRANS_WAITING 2
145#define MBEDTLS_SSL_RETRANS_FINISHED 3
146
Manuel Pégourié-Gonnard8ebb88d2020-07-28 09:55:33 +0200147/* For CBC-specific encrypt/decrypt code */
148#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
149 ( defined(MBEDTLS_AES_C) || \
150 defined(MBEDTLS_CAMELLIA_C) || \
151 defined(MBEDTLS_DES_C) )
152#define MBEDTLS_SSL_SOME_SUITES_USE_CBC
153#endif
154
Manuel Pégourié-Gonnard065122c2015-05-26 12:31:46 +0200155/*
156 * Allow extra bytes for record, authentication and encryption overhead:
157 * counter (8) + header (5) + IV(16) + MAC (16-48) + padding (0-256)
158 * and allow for a maximum of 1024 of compression expansion if
159 * enabled.
160 */
161#if defined(MBEDTLS_ZLIB_SUPPORT)
162#define MBEDTLS_SSL_COMPRESSION_ADD 1024
163#else
164#define MBEDTLS_SSL_COMPRESSION_ADD 0
165#endif
166
167#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_MODE_CBC)
168/* Ciphersuites using HMAC */
169#if defined(MBEDTLS_SHA512_C)
170#define MBEDTLS_SSL_MAC_ADD 48 /* SHA-384 used for HMAC */
171#elif defined(MBEDTLS_SHA256_C)
172#define MBEDTLS_SSL_MAC_ADD 32 /* SHA-256 used for HMAC */
173#else
174#define MBEDTLS_SSL_MAC_ADD 20 /* SHA-1 used for HMAC */
175#endif
176#else
177/* AEAD ciphersuites: GCM and CCM use a 128 bits tag */
178#define MBEDTLS_SSL_MAC_ADD 16
179#endif
180
181#if defined(MBEDTLS_CIPHER_MODE_CBC)
182#define MBEDTLS_SSL_PADDING_ADD 256
183#else
184#define MBEDTLS_SSL_PADDING_ADD 0
185#endif
186
Hanno Beckera8434e82017-09-18 10:54:39 +0100187#define MBEDTLS_SSL_PAYLOAD_LEN ( MBEDTLS_SSL_MAX_CONTENT_LEN \
188 + MBEDTLS_SSL_COMPRESSION_ADD \
189 + MBEDTLS_MAX_IV_LENGTH \
190 + MBEDTLS_SSL_MAC_ADD \
191 + MBEDTLS_SSL_PADDING_ADD \
Manuel Pégourié-Gonnard065122c2015-05-26 12:31:46 +0200192 )
193
Hanno Becker20643552017-04-12 14:54:42 +0100194/* Maximum size in bytes of list in sig-hash algorithm ext., RFC 5246 */
195#define MBEDTLS_SSL_MAX_SIG_HASH_ALG_LIST_LEN 65534
196
197/* Maximum size in bytes of list in supported elliptic curve ext., RFC 4492 */
198#define MBEDTLS_SSL_MAX_CURVE_LIST_LEN 65535
199
Manuel Pégourié-Gonnard065122c2015-05-26 12:31:46 +0200200/*
Hanno Beckera8434e82017-09-18 10:54:39 +0100201 * Check that we obey the standard's message size bounds
202 */
203
204#if MBEDTLS_SSL_MAX_CONTENT_LEN > 16384
205#error Bad configuration - record content too large.
206#endif
207
208#if MBEDTLS_SSL_PAYLOAD_LEN > 16384 + 2048
209#error Bad configuration - protected record payload too large.
210#endif
211
Hanno Becker25d6d1a2017-12-07 08:22:51 +0000212/* Note: Even though the TLS record header is only 5 bytes
213 long, we're internally using 8 bytes to store the
214 implicit sequence number. */
Hanno Beckerd25d4442017-10-04 13:56:42 +0100215#define MBEDTLS_SSL_HEADER_LEN 13
Hanno Beckera8434e82017-09-18 10:54:39 +0100216
Hanno Beckerd25d4442017-10-04 13:56:42 +0100217#define MBEDTLS_SSL_BUFFER_LEN \
218 ( ( MBEDTLS_SSL_HEADER_LEN ) + ( MBEDTLS_SSL_PAYLOAD_LEN ) )
Hanno Beckera8434e82017-09-18 10:54:39 +0100219
220/*
Manuel Pégourié-Gonnard065122c2015-05-26 12:31:46 +0200221 * TLS extension flags (for extensions with outgoing ServerHello content
222 * that need it (e.g. for RENEGOTIATION_INFO the server already knows because
223 * of state of the renegotiation flag, so no indicator is required)
224 */
225#define MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT (1 << 0)
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +0200226#define MBEDTLS_TLS_EXT_ECJPAKE_KKPP_OK (1 << 1)
Manuel Pégourié-Gonnard065122c2015-05-26 12:31:46 +0200227
Hanno Beckerdc7b5b92017-04-12 14:54:42 +0100228/**
229 * \brief This function checks if the remaining size in a buffer is
230 * greater or equal than a needed space.
231 *
232 * \param cur Pointer to the current position in the buffer.
233 * \param end Pointer to one past the end of the buffer.
234 * \param need Needed space in bytes.
235 *
Ronald Cron4206bd42020-06-11 09:50:51 +0200236 * \return Zero if the needed space is available in the buffer, non-zero
Hanno Beckerdc7b5b92017-04-12 14:54:42 +0100237 * otherwise.
238 */
239static inline int mbedtls_ssl_chk_buf_ptr( const uint8_t *cur,
240 const uint8_t *end, size_t need )
241{
Ronald Cron4206bd42020-06-11 09:50:51 +0200242 return( ( cur > end ) || ( need > (size_t)( end - cur ) ) );
Hanno Beckerdc7b5b92017-04-12 14:54:42 +0100243}
244
245/**
246 * \brief This macro checks if the remaining size in a buffer is
247 * greater or equal than a needed space. If it is not the case,
248 * it returns an SSL_BUFFER_TOO_SMALL error.
249 *
250 * \param cur Pointer to the current position in the buffer.
251 * \param end Pointer to one past the end of the buffer.
252 * \param need Needed space in bytes.
253 *
254 */
255#define MBEDTLS_SSL_CHK_BUF_PTR( cur, end, need ) \
256 do { \
Ronald Cron4206bd42020-06-11 09:50:51 +0200257 if( mbedtls_ssl_chk_buf_ptr( ( cur ), ( end ), ( need ) ) != 0 ) \
Hanno Beckerdc7b5b92017-04-12 14:54:42 +0100258 { \
259 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); \
260 } \
261 } while( 0 )
262
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +0200263#ifdef __cplusplus
264extern "C" {
265#endif
266
Hanno Becker7e5437a2017-04-28 17:15:26 +0100267#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
268 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
269/*
270 * Abstraction for a grid of allowed signature-hash-algorithm pairs.
271 */
272struct mbedtls_ssl_sig_hash_set_t
273{
274 /* At the moment, we only need to remember a single suitable
275 * hash algorithm per signature algorithm. As long as that's
276 * the case - and we don't need a general lookup function -
277 * we can implement the sig-hash-set as a map from signatures
278 * to hash algorithms. */
279 mbedtls_md_type_t rsa;
280 mbedtls_md_type_t ecdsa;
281};
282#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
283 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
284
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200285/*
286 * This structure contains the parameters only needed during handshake.
287 */
288struct mbedtls_ssl_handshake_params
289{
290 /*
291 * Handshake specific crypto variables
292 */
Hanno Becker7e5437a2017-04-28 17:15:26 +0100293
294#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
295 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
296 mbedtls_ssl_sig_hash_set_t hash_algs; /*!< Set of suitable sig-hash pairs */
297#endif
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200298#if defined(MBEDTLS_DHM_C)
299 mbedtls_dhm_context dhm_ctx; /*!< DHM key exchange */
300#endif
301#if defined(MBEDTLS_ECDH_C)
302 mbedtls_ecdh_context ecdh_ctx; /*!< ECDH key exchange */
303#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200304#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200305 mbedtls_ecjpake_context ecjpake_ctx; /*!< EC J-PAKE key exchange */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +0200306#if defined(MBEDTLS_SSL_CLI_C)
307 unsigned char *ecjpake_cache; /*!< Cache for ClientHello ext */
308 size_t ecjpake_cache_len; /*!< Length of cached data */
309#endif
Hanno Becker1aa267c2017-04-28 17:08:27 +0100310#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnardf4721792015-09-15 10:53:51 +0200311#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200312 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200313 const mbedtls_ecp_curve_info **curves; /*!< Supported elliptic curves */
314#endif
315#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
316 unsigned char *psk; /*!< PSK from the callback */
317 size_t psk_len; /*!< Length of PSK from callback */
318#endif
319#if defined(MBEDTLS_X509_CRT_PARSE_C)
320 mbedtls_ssl_key_cert *key_cert; /*!< chosen key/cert pair (server) */
321#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200322 int sni_authmode; /*!< authmode from SNI callback */
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200323 mbedtls_ssl_key_cert *sni_key_cert; /*!< key/cert list from SNI */
324 mbedtls_x509_crt *sni_ca_chain; /*!< trusted CAs from SNI callback */
325 mbedtls_x509_crl *sni_ca_crl; /*!< trusted CAs CRLs from SNI */
Hanno Becker1aa267c2017-04-28 17:08:27 +0100326#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200327#endif /* MBEDTLS_X509_CRT_PARSE_C */
328#if defined(MBEDTLS_SSL_PROTO_DTLS)
329 unsigned int out_msg_seq; /*!< Outgoing handshake sequence number */
330 unsigned int in_msg_seq; /*!< Incoming handshake sequence number */
331
332 unsigned char *verify_cookie; /*!< Cli: HelloVerifyRequest cookie
333 Srv: unused */
334 unsigned char verify_cookie_len; /*!< Cli: cookie length
335 Srv: flag for sending a cookie */
336
337 unsigned char *hs_msg; /*!< Reassembled handshake message */
338
339 uint32_t retransmit_timeout; /*!< Current value of timeout */
340 unsigned char retransmit_state; /*!< Retransmission state */
341 mbedtls_ssl_flight_item *flight; /*!< Current outgoing flight */
342 mbedtls_ssl_flight_item *cur_msg; /*!< Current message in flight */
343 unsigned int in_flight_start_seq; /*!< Minimum message sequence in the
344 flight being received */
345 mbedtls_ssl_transform *alt_transform_out; /*!< Alternative transform for
346 resending messages */
347 unsigned char alt_out_ctr[8]; /*!< Alternative record epoch/counter
348 for resending messages */
Hanno Becker1aa267c2017-04-28 17:08:27 +0100349#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200350
351 /*
352 * Checksum contexts
353 */
354#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
355 defined(MBEDTLS_SSL_PROTO_TLS1_1)
356 mbedtls_md5_context fin_md5;
357 mbedtls_sha1_context fin_sha1;
358#endif
359#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
360#if defined(MBEDTLS_SHA256_C)
361 mbedtls_sha256_context fin_sha256;
362#endif
363#if defined(MBEDTLS_SHA512_C)
364 mbedtls_sha512_context fin_sha512;
365#endif
366#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
367
368 void (*update_checksum)(mbedtls_ssl_context *, const unsigned char *, size_t);
369 void (*calc_verify)(mbedtls_ssl_context *, unsigned char *);
370 void (*calc_finished)(mbedtls_ssl_context *, unsigned char *, int);
371 int (*tls_prf)(const unsigned char *, size_t, const char *,
372 const unsigned char *, size_t,
373 unsigned char *, size_t);
374
375 size_t pmslen; /*!< premaster length */
376
377 unsigned char randbytes[64]; /*!< random bytes */
378 unsigned char premaster[MBEDTLS_PREMASTER_SIZE];
379 /*!< premaster secret */
380
381 int resume; /*!< session resume indicator*/
382 int max_major_ver; /*!< max. major version client*/
383 int max_minor_ver; /*!< max. minor version client*/
384 int cli_exts; /*!< client extension presence*/
385
386#if defined(MBEDTLS_SSL_SESSION_TICKETS)
387 int new_session_ticket; /*!< use NewSessionTicket? */
388#endif /* MBEDTLS_SSL_SESSION_TICKETS */
389#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
390 int extended_ms; /*!< use Extended Master Secret? */
391#endif
392};
393
394/*
395 * This structure contains a full set of runtime transform parameters
396 * either in negotiation or active.
397 */
398struct mbedtls_ssl_transform
399{
400 /*
401 * Session specific crypto layer
402 */
403 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
404 /*!< Chosen cipersuite_info */
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200405 unsigned int keylen; /*!< symmetric key length (bytes) */
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200406 size_t minlen; /*!< min. ciphertext length */
407 size_t ivlen; /*!< IV length */
408 size_t fixed_ivlen; /*!< Fixed part of IV (AEAD) */
409 size_t maclen; /*!< MAC length */
410
411 unsigned char iv_enc[16]; /*!< IV (encryption) */
412 unsigned char iv_dec[16]; /*!< IV (decryption) */
413
414#if defined(MBEDTLS_SSL_PROTO_SSL3)
415 /* Needed only for SSL v3.0 secret */
416 unsigned char mac_enc[20]; /*!< SSL v3.0 secret (enc) */
417 unsigned char mac_dec[20]; /*!< SSL v3.0 secret (dec) */
418#endif /* MBEDTLS_SSL_PROTO_SSL3 */
419
420 mbedtls_md_context_t md_ctx_enc; /*!< MAC (encryption) */
421 mbedtls_md_context_t md_ctx_dec; /*!< MAC (decryption) */
422
423 mbedtls_cipher_context_t cipher_ctx_enc; /*!< encryption context */
424 mbedtls_cipher_context_t cipher_ctx_dec; /*!< decryption context */
425
426 /*
427 * Session specific compression layer
428 */
429#if defined(MBEDTLS_ZLIB_SUPPORT)
430 z_stream ctx_deflate; /*!< compression context */
431 z_stream ctx_inflate; /*!< decompression context */
432#endif
433};
434
435#if defined(MBEDTLS_X509_CRT_PARSE_C)
436/*
437 * List of certificate + private key pairs
438 */
439struct mbedtls_ssl_key_cert
440{
441 mbedtls_x509_crt *cert; /*!< cert */
442 mbedtls_pk_context *key; /*!< private key */
443 mbedtls_ssl_key_cert *next; /*!< next key/cert pair */
444};
445#endif /* MBEDTLS_X509_CRT_PARSE_C */
446
447#if defined(MBEDTLS_SSL_PROTO_DTLS)
448/*
449 * List of handshake messages kept around for resending
450 */
451struct mbedtls_ssl_flight_item
452{
453 unsigned char *p; /*!< message, including handshake headers */
454 size_t len; /*!< length of p */
455 unsigned char type; /*!< type of the message: handshake or CCS */
456 mbedtls_ssl_flight_item *next; /*!< next handshake message(s) */
457};
458#endif /* MBEDTLS_SSL_PROTO_DTLS */
459
Hanno Becker7e5437a2017-04-28 17:15:26 +0100460#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
461 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
462
463/* Find an entry in a signature-hash set matching a given hash algorithm. */
464mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
465 mbedtls_pk_type_t sig_alg );
466/* Add a signature-hash-pair to a signature-hash set */
467void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
468 mbedtls_pk_type_t sig_alg,
469 mbedtls_md_type_t md_alg );
470/* Allow exactly one hash algorithm for each signature. */
471void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
472 mbedtls_md_type_t md_alg );
473
474/* Setup an empty signature-hash set */
475static inline void mbedtls_ssl_sig_hash_set_init( mbedtls_ssl_sig_hash_set_t *set )
476{
477 mbedtls_ssl_sig_hash_set_const_hash( set, MBEDTLS_MD_NONE );
478}
479
480#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
481 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200482
483/**
484 * \brief Free referenced items in an SSL transform context and clear
485 * memory
486 *
487 * \param transform SSL transform context
488 */
489void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform );
490
491/**
492 * \brief Free referenced items in an SSL handshake context and clear
493 * memory
494 *
495 * \param handshake SSL handshake context
496 */
497void mbedtls_ssl_handshake_free( mbedtls_ssl_handshake_params *handshake );
498
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +0200499int mbedtls_ssl_handshake_client_step( mbedtls_ssl_context *ssl );
500int mbedtls_ssl_handshake_server_step( mbedtls_ssl_context *ssl );
501void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl );
502
503int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl );
504
505void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl );
506int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl );
507
Simon Butcher99000142016-10-13 17:21:01 +0100508int mbedtls_ssl_read_record_layer( mbedtls_ssl_context *ssl );
509int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl );
510int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl );
511void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl );
512
Hanno Becker4a810fb2017-05-24 16:27:30 +0100513/**
514 * \brief Update record layer
515 *
516 * This function roughly separates the implementation
517 * of the logic of (D)TLS from the implementation
518 * of the secure transport.
519 *
520 * \param ssl SSL context to use
521 *
522 * \return 0 or non-zero error code.
523 *
524 * \note A clarification on what is called 'record layer' here
525 * is in order, as many sensible definitions are possible:
526 *
527 * The record layer takes as input an untrusted underlying
528 * transport (stream or datagram) and transforms it into
529 * a serially multiplexed, secure transport, which
530 * conceptually provides the following:
531 *
532 * (1) Three datagram based, content-agnostic transports
533 * for handshake, alert and CCS messages.
534 * (2) One stream- or datagram-based transport
535 * for application data.
536 * (3) Functionality for changing the underlying transform
537 * securing the contents.
538 *
539 * The interface to this functionality is given as follows:
540 *
541 * a Updating
542 * [Currently implemented by mbedtls_ssl_read_record]
543 *
544 * Check if and on which of the four 'ports' data is pending:
545 * Nothing, a controlling datagram of type (1), or application
546 * data (2). In any case data is present, internal buffers
547 * provide access to the data for the user to process it.
548 * Consumption of type (1) datagrams is done automatically
549 * on the next update, invalidating that the internal buffers
550 * for previous datagrams, while consumption of application
551 * data (2) is user-controlled.
552 *
553 * b Reading of application data
554 * [Currently manual adaption of ssl->in_offt pointer]
555 *
556 * As mentioned in the last paragraph, consumption of data
557 * is different from the automatic consumption of control
558 * datagrams (1) because application data is treated as a stream.
559 *
560 * c Tracking availability of application data
561 * [Currently manually through decreasing ssl->in_msglen]
562 *
563 * For efficiency and to retain datagram semantics for
564 * application data in case of DTLS, the record layer
565 * provides functionality for checking how much application
566 * data is still available in the internal buffer.
567 *
568 * d Changing the transformation securing the communication.
569 *
570 * Given an opaque implementation of the record layer in the
571 * above sense, it should be possible to implement the logic
572 * of (D)TLS on top of it without the need to know anything
573 * about the record layer's internals. This is done e.g.
574 * in all the handshake handling functions, and in the
575 * application data reading function mbedtls_ssl_read.
576 *
577 * \note The above tries to give a conceptual picture of the
578 * record layer, but the current implementation deviates
579 * from it in some places. For example, our implementation of
580 * the update functionality through mbedtls_ssl_read_record
581 * discards datagrams depending on the current state, which
582 * wouldn't fall under the record layer's responsibility
583 * following the above definition.
584 *
585 */
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +0200586int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl );
587int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want );
588
589int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl );
590int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl );
591
592int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl );
593int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl );
594
595int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl );
596int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl );
597
598int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl );
599int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl );
600
601void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
602 const mbedtls_ssl_ciphersuite_t *ciphersuite_info );
603
604#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
605int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex );
606#endif
607
608#if defined(MBEDTLS_PK_C)
609unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk );
Hanno Becker7e5437a2017-04-28 17:15:26 +0100610unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type );
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +0200611mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig );
612#endif
613
614mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash );
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +0200615unsigned char mbedtls_ssl_hash_from_md_alg( int md );
Simon Butcher99000142016-10-13 17:21:01 +0100616int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md );
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +0200617
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +0200618#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +0200619int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id );
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +0200620#endif
621
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +0200622#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +0200623int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
624 mbedtls_md_type_t md );
625#endif
626
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +0200627#if defined(MBEDTLS_X509_CRT_PARSE_C)
628static inline mbedtls_pk_context *mbedtls_ssl_own_key( mbedtls_ssl_context *ssl )
629{
630 mbedtls_ssl_key_cert *key_cert;
631
632 if( ssl->handshake != NULL && ssl->handshake->key_cert != NULL )
633 key_cert = ssl->handshake->key_cert;
634 else
635 key_cert = ssl->conf->key_cert;
636
637 return( key_cert == NULL ? NULL : key_cert->key );
638}
639
640static inline mbedtls_x509_crt *mbedtls_ssl_own_cert( mbedtls_ssl_context *ssl )
641{
642 mbedtls_ssl_key_cert *key_cert;
643
644 if( ssl->handshake != NULL && ssl->handshake->key_cert != NULL )
645 key_cert = ssl->handshake->key_cert;
646 else
647 key_cert = ssl->conf->key_cert;
648
649 return( key_cert == NULL ? NULL : key_cert->cert );
650}
651
652/*
653 * Check usage of a certificate wrt extensions:
654 * keyUsage, extendedKeyUsage (later), and nSCertType (later).
655 *
656 * Warning: cert_endpoint is the endpoint of the cert (ie, of our peer when we
657 * check a cert we received from them)!
658 *
659 * Return 0 if everything is OK, -1 if not.
660 */
661int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
662 const mbedtls_ssl_ciphersuite_t *ciphersuite,
663 int cert_endpoint,
664 uint32_t *flags );
665#endif /* MBEDTLS_X509_CRT_PARSE_C */
666
667void mbedtls_ssl_write_version( int major, int minor, int transport,
668 unsigned char ver[2] );
669void mbedtls_ssl_read_version( int *major, int *minor, int transport,
670 const unsigned char ver[2] );
671
672static inline size_t mbedtls_ssl_hdr_len( const mbedtls_ssl_context *ssl )
673{
674#if defined(MBEDTLS_SSL_PROTO_DTLS)
675 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
676 return( 13 );
677#else
678 ((void) ssl);
679#endif
680 return( 5 );
681}
682
683static inline size_t mbedtls_ssl_hs_hdr_len( const mbedtls_ssl_context *ssl )
684{
685#if defined(MBEDTLS_SSL_PROTO_DTLS)
686 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
687 return( 12 );
688#else
689 ((void) ssl);
690#endif
691 return( 4 );
692}
693
694#if defined(MBEDTLS_SSL_PROTO_DTLS)
695void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl );
696void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl );
697int mbedtls_ssl_resend( mbedtls_ssl_context *ssl );
698#endif
699
700/* Visible for testing purposes only */
701#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
702int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl );
703void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl );
704#endif
705
706/* constant-time buffer comparison */
707static inline int mbedtls_ssl_safer_memcmp( const void *a, const void *b, size_t n )
708{
709 size_t i;
Hanno Becker59e69632017-06-26 13:26:58 +0100710 volatile const unsigned char *A = (volatile const unsigned char *) a;
711 volatile const unsigned char *B = (volatile const unsigned char *) b;
712 volatile unsigned char diff = 0;
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +0200713
714 for( i = 0; i < n; i++ )
Azim Khan826cdab2018-05-23 16:55:16 +0100715 {
716 /* Read volatile data in order before computing diff.
717 * This avoids IAR compiler warning:
718 * 'the order of volatile accesses is undefined ..' */
719 unsigned char x = A[i], y = B[i];
720 diff |= x ^ y;
721 }
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +0200722
723 return( diff );
724}
725
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +0100726#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
727 defined(MBEDTLS_SSL_PROTO_TLS1_1)
728int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
729 unsigned char *output,
730 unsigned char *data, size_t data_len );
731#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
732 MBEDTLS_SSL_PROTO_TLS1_1 */
733
734#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
735 defined(MBEDTLS_SSL_PROTO_TLS1_2)
736int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
737 unsigned char *output,
738 unsigned char *data, size_t data_len,
739 mbedtls_md_type_t md_alg );
740#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
741 MBEDTLS_SSL_PROTO_TLS1_2 */
742
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +0200743#ifdef __cplusplus
744}
745#endif
746
747#endif /* ssl_internal.h */