blob: 6dbec1bd422d6361fe85dfbf924f48771e1733e4 [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úti44bfbe32020-08-19 16:54:51 +02007 * Copyright The Mbed TLS Contributors
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 * **********
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020048 */
49#ifndef MBEDTLS_SSL_INTERNAL_H
50#define MBEDTLS_SSL_INTERNAL_H
51
Ron Eldor0559c662018-02-14 16:02:41 +020052#if !defined(MBEDTLS_CONFIG_FILE)
53#include "config.h"
54#else
55#include MBEDTLS_CONFIG_FILE
56#endif
57
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020058#include "ssl.h"
Hanno Beckera8434e82017-09-18 10:54:39 +010059#include "cipher.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020060
Manuel Pégourié-Gonnard56273da2015-05-26 12:19:45 +020061#if defined(MBEDTLS_MD5_C)
62#include "md5.h"
63#endif
64
65#if defined(MBEDTLS_SHA1_C)
66#include "sha1.h"
67#endif
68
69#if defined(MBEDTLS_SHA256_C)
70#include "sha256.h"
71#endif
72
73#if defined(MBEDTLS_SHA512_C)
74#include "sha512.h"
75#endif
76
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020077#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020078#include "ecjpake.h"
79#endif
80
Manuel Pégourié-Gonnard0223ab92015-10-05 11:40:01 +010081#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
82 !defined(inline) && !defined(__cplusplus)
Manuel Pégourié-Gonnard065122c2015-05-26 12:31:46 +020083#define inline __inline
Manuel Pégourié-Gonnard20af64d2015-07-07 18:33:39 +020084#endif
Manuel Pégourié-Gonnard065122c2015-05-26 12:31:46 +020085
86/* Determine minimum supported version */
87#define MBEDTLS_SSL_MIN_MAJOR_VERSION MBEDTLS_SSL_MAJOR_VERSION_3
88
89#if defined(MBEDTLS_SSL_PROTO_SSL3)
90#define MBEDTLS_SSL_MIN_MINOR_VERSION MBEDTLS_SSL_MINOR_VERSION_0
91#else
92#if defined(MBEDTLS_SSL_PROTO_TLS1)
93#define MBEDTLS_SSL_MIN_MINOR_VERSION MBEDTLS_SSL_MINOR_VERSION_1
94#else
95#if defined(MBEDTLS_SSL_PROTO_TLS1_1)
96#define MBEDTLS_SSL_MIN_MINOR_VERSION MBEDTLS_SSL_MINOR_VERSION_2
97#else
98#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
99#define MBEDTLS_SSL_MIN_MINOR_VERSION MBEDTLS_SSL_MINOR_VERSION_3
100#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
101#endif /* MBEDTLS_SSL_PROTO_TLS1_1 */
102#endif /* MBEDTLS_SSL_PROTO_TLS1 */
103#endif /* MBEDTLS_SSL_PROTO_SSL3 */
104
Ron Eldor5e9f14d2017-05-28 10:46:38 +0300105#define MBEDTLS_SSL_MIN_VALID_MINOR_VERSION MBEDTLS_SSL_MINOR_VERSION_1
106#define MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION MBEDTLS_SSL_MAJOR_VERSION_3
107
Manuel Pégourié-Gonnard065122c2015-05-26 12:31:46 +0200108/* Determine maximum supported version */
109#define MBEDTLS_SSL_MAX_MAJOR_VERSION MBEDTLS_SSL_MAJOR_VERSION_3
110
111#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
112#define MBEDTLS_SSL_MAX_MINOR_VERSION MBEDTLS_SSL_MINOR_VERSION_3
113#else
114#if defined(MBEDTLS_SSL_PROTO_TLS1_1)
115#define MBEDTLS_SSL_MAX_MINOR_VERSION MBEDTLS_SSL_MINOR_VERSION_2
116#else
117#if defined(MBEDTLS_SSL_PROTO_TLS1)
118#define MBEDTLS_SSL_MAX_MINOR_VERSION MBEDTLS_SSL_MINOR_VERSION_1
119#else
120#if defined(MBEDTLS_SSL_PROTO_SSL3)
121#define MBEDTLS_SSL_MAX_MINOR_VERSION MBEDTLS_SSL_MINOR_VERSION_0
122#endif /* MBEDTLS_SSL_PROTO_SSL3 */
123#endif /* MBEDTLS_SSL_PROTO_TLS1 */
124#endif /* MBEDTLS_SSL_PROTO_TLS1_1 */
125#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
126
127#define MBEDTLS_SSL_INITIAL_HANDSHAKE 0
128#define MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS 1 /* In progress */
129#define MBEDTLS_SSL_RENEGOTIATION_DONE 2 /* Done or aborted */
130#define MBEDTLS_SSL_RENEGOTIATION_PENDING 3 /* Requested (server only) */
131
132/*
133 * DTLS retransmission states, see RFC 6347 4.2.4
134 *
135 * The SENDING state is merged in PREPARING for initial sends,
136 * but is distinct for resends.
137 *
138 * Note: initial state is wrong for server, but is not used anyway.
139 */
140#define MBEDTLS_SSL_RETRANS_PREPARING 0
141#define MBEDTLS_SSL_RETRANS_SENDING 1
142#define MBEDTLS_SSL_RETRANS_WAITING 2
143#define MBEDTLS_SSL_RETRANS_FINISHED 3
144
145/*
146 * Allow extra bytes for record, authentication and encryption overhead:
147 * counter (8) + header (5) + IV(16) + MAC (16-48) + padding (0-256)
148 * and allow for a maximum of 1024 of compression expansion if
149 * enabled.
150 */
151#if defined(MBEDTLS_ZLIB_SUPPORT)
152#define MBEDTLS_SSL_COMPRESSION_ADD 1024
153#else
154#define MBEDTLS_SSL_COMPRESSION_ADD 0
155#endif
156
157#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_MODE_CBC)
158/* Ciphersuites using HMAC */
159#if defined(MBEDTLS_SHA512_C)
160#define MBEDTLS_SSL_MAC_ADD 48 /* SHA-384 used for HMAC */
161#elif defined(MBEDTLS_SHA256_C)
162#define MBEDTLS_SSL_MAC_ADD 32 /* SHA-256 used for HMAC */
163#else
164#define MBEDTLS_SSL_MAC_ADD 20 /* SHA-1 used for HMAC */
165#endif
166#else
167/* AEAD ciphersuites: GCM and CCM use a 128 bits tag */
168#define MBEDTLS_SSL_MAC_ADD 16
169#endif
170
171#if defined(MBEDTLS_CIPHER_MODE_CBC)
172#define MBEDTLS_SSL_PADDING_ADD 256
173#else
174#define MBEDTLS_SSL_PADDING_ADD 0
175#endif
176
Hanno Beckera8434e82017-09-18 10:54:39 +0100177#define MBEDTLS_SSL_PAYLOAD_LEN ( MBEDTLS_SSL_MAX_CONTENT_LEN \
178 + MBEDTLS_SSL_COMPRESSION_ADD \
179 + MBEDTLS_MAX_IV_LENGTH \
180 + MBEDTLS_SSL_MAC_ADD \
181 + MBEDTLS_SSL_PADDING_ADD \
Manuel Pégourié-Gonnard065122c2015-05-26 12:31:46 +0200182 )
183
Hanno Becker20643552017-04-12 14:54:42 +0100184/* Maximum size in bytes of list in sig-hash algorithm ext., RFC 5246 */
185#define MBEDTLS_SSL_MAX_SIG_HASH_ALG_LIST_LEN 65534
186
187/* Maximum size in bytes of list in supported elliptic curve ext., RFC 4492 */
188#define MBEDTLS_SSL_MAX_CURVE_LIST_LEN 65535
189
Manuel Pégourié-Gonnard065122c2015-05-26 12:31:46 +0200190/*
Hanno Beckera8434e82017-09-18 10:54:39 +0100191 * Check that we obey the standard's message size bounds
192 */
193
194#if MBEDTLS_SSL_MAX_CONTENT_LEN > 16384
195#error Bad configuration - record content too large.
196#endif
197
198#if MBEDTLS_SSL_PAYLOAD_LEN > 16384 + 2048
199#error Bad configuration - protected record payload too large.
200#endif
201
Hanno Becker25d6d1a2017-12-07 08:22:51 +0000202/* Note: Even though the TLS record header is only 5 bytes
203 long, we're internally using 8 bytes to store the
204 implicit sequence number. */
Hanno Beckerd25d4442017-10-04 13:56:42 +0100205#define MBEDTLS_SSL_HEADER_LEN 13
Hanno Beckera8434e82017-09-18 10:54:39 +0100206
Hanno Beckerd25d4442017-10-04 13:56:42 +0100207#define MBEDTLS_SSL_BUFFER_LEN \
208 ( ( MBEDTLS_SSL_HEADER_LEN ) + ( MBEDTLS_SSL_PAYLOAD_LEN ) )
Hanno Beckera8434e82017-09-18 10:54:39 +0100209
210/*
Manuel Pégourié-Gonnard065122c2015-05-26 12:31:46 +0200211 * TLS extension flags (for extensions with outgoing ServerHello content
212 * that need it (e.g. for RENEGOTIATION_INFO the server already knows because
213 * of state of the renegotiation flag, so no indicator is required)
214 */
215#define MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT (1 << 0)
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +0200216#define MBEDTLS_TLS_EXT_ECJPAKE_KKPP_OK (1 << 1)
Manuel Pégourié-Gonnard065122c2015-05-26 12:31:46 +0200217
Hanno Beckerdc7b5b92017-04-12 14:54:42 +0100218/**
219 * \brief This function checks if the remaining size in a buffer is
220 * greater or equal than a needed space.
221 *
222 * \param cur Pointer to the current position in the buffer.
223 * \param end Pointer to one past the end of the buffer.
224 * \param need Needed space in bytes.
225 *
Ronald Cron4206bd42020-06-11 09:50:51 +0200226 * \return Zero if the needed space is available in the buffer, non-zero
Hanno Beckerdc7b5b92017-04-12 14:54:42 +0100227 * otherwise.
228 */
229static inline int mbedtls_ssl_chk_buf_ptr( const uint8_t *cur,
230 const uint8_t *end, size_t need )
231{
Ronald Cron4206bd42020-06-11 09:50:51 +0200232 return( ( cur > end ) || ( need > (size_t)( end - cur ) ) );
Hanno Beckerdc7b5b92017-04-12 14:54:42 +0100233}
234
235/**
236 * \brief This macro checks if the remaining size in a buffer is
237 * greater or equal than a needed space. If it is not the case,
238 * it returns an SSL_BUFFER_TOO_SMALL error.
239 *
240 * \param cur Pointer to the current position in the buffer.
241 * \param end Pointer to one past the end of the buffer.
242 * \param need Needed space in bytes.
243 *
244 */
245#define MBEDTLS_SSL_CHK_BUF_PTR( cur, end, need ) \
246 do { \
Ronald Cron4206bd42020-06-11 09:50:51 +0200247 if( mbedtls_ssl_chk_buf_ptr( ( cur ), ( end ), ( need ) ) != 0 ) \
Hanno Beckerdc7b5b92017-04-12 14:54:42 +0100248 { \
249 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); \
250 } \
251 } while( 0 )
252
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +0200253#ifdef __cplusplus
254extern "C" {
255#endif
256
Hanno Becker7e5437a2017-04-28 17:15:26 +0100257#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
258 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
259/*
260 * Abstraction for a grid of allowed signature-hash-algorithm pairs.
261 */
262struct mbedtls_ssl_sig_hash_set_t
263{
264 /* At the moment, we only need to remember a single suitable
265 * hash algorithm per signature algorithm. As long as that's
266 * the case - and we don't need a general lookup function -
267 * we can implement the sig-hash-set as a map from signatures
268 * to hash algorithms. */
269 mbedtls_md_type_t rsa;
270 mbedtls_md_type_t ecdsa;
271};
272#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
273 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
274
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200275/*
276 * This structure contains the parameters only needed during handshake.
277 */
278struct mbedtls_ssl_handshake_params
279{
280 /*
281 * Handshake specific crypto variables
282 */
Hanno Becker7e5437a2017-04-28 17:15:26 +0100283
284#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
285 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
286 mbedtls_ssl_sig_hash_set_t hash_algs; /*!< Set of suitable sig-hash pairs */
287#endif
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200288#if defined(MBEDTLS_DHM_C)
289 mbedtls_dhm_context dhm_ctx; /*!< DHM key exchange */
290#endif
291#if defined(MBEDTLS_ECDH_C)
292 mbedtls_ecdh_context ecdh_ctx; /*!< ECDH key exchange */
293#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200294#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200295 mbedtls_ecjpake_context ecjpake_ctx; /*!< EC J-PAKE key exchange */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +0200296#if defined(MBEDTLS_SSL_CLI_C)
297 unsigned char *ecjpake_cache; /*!< Cache for ClientHello ext */
298 size_t ecjpake_cache_len; /*!< Length of cached data */
299#endif
Hanno Becker1aa267c2017-04-28 17:08:27 +0100300#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnardf4721792015-09-15 10:53:51 +0200301#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200302 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200303 const mbedtls_ecp_curve_info **curves; /*!< Supported elliptic curves */
304#endif
305#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
306 unsigned char *psk; /*!< PSK from the callback */
307 size_t psk_len; /*!< Length of PSK from callback */
308#endif
309#if defined(MBEDTLS_X509_CRT_PARSE_C)
310 mbedtls_ssl_key_cert *key_cert; /*!< chosen key/cert pair (server) */
311#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200312 int sni_authmode; /*!< authmode from SNI callback */
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200313 mbedtls_ssl_key_cert *sni_key_cert; /*!< key/cert list from SNI */
314 mbedtls_x509_crt *sni_ca_chain; /*!< trusted CAs from SNI callback */
315 mbedtls_x509_crl *sni_ca_crl; /*!< trusted CAs CRLs from SNI */
Hanno Becker1aa267c2017-04-28 17:08:27 +0100316#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200317#endif /* MBEDTLS_X509_CRT_PARSE_C */
318#if defined(MBEDTLS_SSL_PROTO_DTLS)
319 unsigned int out_msg_seq; /*!< Outgoing handshake sequence number */
320 unsigned int in_msg_seq; /*!< Incoming handshake sequence number */
321
322 unsigned char *verify_cookie; /*!< Cli: HelloVerifyRequest cookie
323 Srv: unused */
324 unsigned char verify_cookie_len; /*!< Cli: cookie length
325 Srv: flag for sending a cookie */
326
327 unsigned char *hs_msg; /*!< Reassembled handshake message */
328
329 uint32_t retransmit_timeout; /*!< Current value of timeout */
330 unsigned char retransmit_state; /*!< Retransmission state */
331 mbedtls_ssl_flight_item *flight; /*!< Current outgoing flight */
332 mbedtls_ssl_flight_item *cur_msg; /*!< Current message in flight */
333 unsigned int in_flight_start_seq; /*!< Minimum message sequence in the
334 flight being received */
335 mbedtls_ssl_transform *alt_transform_out; /*!< Alternative transform for
336 resending messages */
337 unsigned char alt_out_ctr[8]; /*!< Alternative record epoch/counter
338 for resending messages */
Hanno Becker1aa267c2017-04-28 17:08:27 +0100339#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200340
341 /*
342 * Checksum contexts
343 */
344#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
345 defined(MBEDTLS_SSL_PROTO_TLS1_1)
346 mbedtls_md5_context fin_md5;
347 mbedtls_sha1_context fin_sha1;
348#endif
349#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
350#if defined(MBEDTLS_SHA256_C)
351 mbedtls_sha256_context fin_sha256;
352#endif
353#if defined(MBEDTLS_SHA512_C)
354 mbedtls_sha512_context fin_sha512;
355#endif
356#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
357
358 void (*update_checksum)(mbedtls_ssl_context *, const unsigned char *, size_t);
359 void (*calc_verify)(mbedtls_ssl_context *, unsigned char *);
360 void (*calc_finished)(mbedtls_ssl_context *, unsigned char *, int);
361 int (*tls_prf)(const unsigned char *, size_t, const char *,
362 const unsigned char *, size_t,
363 unsigned char *, size_t);
364
365 size_t pmslen; /*!< premaster length */
366
367 unsigned char randbytes[64]; /*!< random bytes */
368 unsigned char premaster[MBEDTLS_PREMASTER_SIZE];
369 /*!< premaster secret */
370
371 int resume; /*!< session resume indicator*/
372 int max_major_ver; /*!< max. major version client*/
373 int max_minor_ver; /*!< max. minor version client*/
374 int cli_exts; /*!< client extension presence*/
375
376#if defined(MBEDTLS_SSL_SESSION_TICKETS)
377 int new_session_ticket; /*!< use NewSessionTicket? */
378#endif /* MBEDTLS_SSL_SESSION_TICKETS */
379#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
380 int extended_ms; /*!< use Extended Master Secret? */
381#endif
382};
383
384/*
385 * This structure contains a full set of runtime transform parameters
386 * either in negotiation or active.
387 */
388struct mbedtls_ssl_transform
389{
390 /*
391 * Session specific crypto layer
392 */
393 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
394 /*!< Chosen cipersuite_info */
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200395 unsigned int keylen; /*!< symmetric key length (bytes) */
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200396 size_t minlen; /*!< min. ciphertext length */
397 size_t ivlen; /*!< IV length */
398 size_t fixed_ivlen; /*!< Fixed part of IV (AEAD) */
399 size_t maclen; /*!< MAC length */
400
401 unsigned char iv_enc[16]; /*!< IV (encryption) */
402 unsigned char iv_dec[16]; /*!< IV (decryption) */
403
404#if defined(MBEDTLS_SSL_PROTO_SSL3)
405 /* Needed only for SSL v3.0 secret */
406 unsigned char mac_enc[20]; /*!< SSL v3.0 secret (enc) */
407 unsigned char mac_dec[20]; /*!< SSL v3.0 secret (dec) */
408#endif /* MBEDTLS_SSL_PROTO_SSL3 */
409
410 mbedtls_md_context_t md_ctx_enc; /*!< MAC (encryption) */
411 mbedtls_md_context_t md_ctx_dec; /*!< MAC (decryption) */
412
413 mbedtls_cipher_context_t cipher_ctx_enc; /*!< encryption context */
414 mbedtls_cipher_context_t cipher_ctx_dec; /*!< decryption context */
415
416 /*
417 * Session specific compression layer
418 */
419#if defined(MBEDTLS_ZLIB_SUPPORT)
420 z_stream ctx_deflate; /*!< compression context */
421 z_stream ctx_inflate; /*!< decompression context */
422#endif
423};
424
425#if defined(MBEDTLS_X509_CRT_PARSE_C)
426/*
427 * List of certificate + private key pairs
428 */
429struct mbedtls_ssl_key_cert
430{
431 mbedtls_x509_crt *cert; /*!< cert */
432 mbedtls_pk_context *key; /*!< private key */
433 mbedtls_ssl_key_cert *next; /*!< next key/cert pair */
434};
435#endif /* MBEDTLS_X509_CRT_PARSE_C */
436
437#if defined(MBEDTLS_SSL_PROTO_DTLS)
438/*
439 * List of handshake messages kept around for resending
440 */
441struct mbedtls_ssl_flight_item
442{
443 unsigned char *p; /*!< message, including handshake headers */
444 size_t len; /*!< length of p */
445 unsigned char type; /*!< type of the message: handshake or CCS */
446 mbedtls_ssl_flight_item *next; /*!< next handshake message(s) */
447};
448#endif /* MBEDTLS_SSL_PROTO_DTLS */
449
Hanno Becker7e5437a2017-04-28 17:15:26 +0100450#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
451 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
452
453/* Find an entry in a signature-hash set matching a given hash algorithm. */
454mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
455 mbedtls_pk_type_t sig_alg );
456/* Add a signature-hash-pair to a signature-hash set */
457void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
458 mbedtls_pk_type_t sig_alg,
459 mbedtls_md_type_t md_alg );
460/* Allow exactly one hash algorithm for each signature. */
461void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
462 mbedtls_md_type_t md_alg );
463
464/* Setup an empty signature-hash set */
465static inline void mbedtls_ssl_sig_hash_set_init( mbedtls_ssl_sig_hash_set_t *set )
466{
467 mbedtls_ssl_sig_hash_set_const_hash( set, MBEDTLS_MD_NONE );
468}
469
470#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
471 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200472
473/**
474 * \brief Free referenced items in an SSL transform context and clear
475 * memory
476 *
477 * \param transform SSL transform context
478 */
479void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform );
480
481/**
482 * \brief Free referenced items in an SSL handshake context and clear
483 * memory
484 *
485 * \param handshake SSL handshake context
486 */
487void mbedtls_ssl_handshake_free( mbedtls_ssl_handshake_params *handshake );
488
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +0200489int mbedtls_ssl_handshake_client_step( mbedtls_ssl_context *ssl );
490int mbedtls_ssl_handshake_server_step( mbedtls_ssl_context *ssl );
491void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl );
492
493int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl );
494
495void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl );
496int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl );
497
Simon Butcher99000142016-10-13 17:21:01 +0100498int mbedtls_ssl_read_record_layer( mbedtls_ssl_context *ssl );
499int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl );
500int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl );
501void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl );
502
Hanno Becker4a810fb2017-05-24 16:27:30 +0100503/**
504 * \brief Update record layer
505 *
506 * This function roughly separates the implementation
507 * of the logic of (D)TLS from the implementation
508 * of the secure transport.
509 *
510 * \param ssl SSL context to use
511 *
512 * \return 0 or non-zero error code.
513 *
514 * \note A clarification on what is called 'record layer' here
515 * is in order, as many sensible definitions are possible:
516 *
517 * The record layer takes as input an untrusted underlying
518 * transport (stream or datagram) and transforms it into
519 * a serially multiplexed, secure transport, which
520 * conceptually provides the following:
521 *
522 * (1) Three datagram based, content-agnostic transports
523 * for handshake, alert and CCS messages.
524 * (2) One stream- or datagram-based transport
525 * for application data.
526 * (3) Functionality for changing the underlying transform
527 * securing the contents.
528 *
529 * The interface to this functionality is given as follows:
530 *
531 * a Updating
532 * [Currently implemented by mbedtls_ssl_read_record]
533 *
534 * Check if and on which of the four 'ports' data is pending:
535 * Nothing, a controlling datagram of type (1), or application
536 * data (2). In any case data is present, internal buffers
537 * provide access to the data for the user to process it.
538 * Consumption of type (1) datagrams is done automatically
539 * on the next update, invalidating that the internal buffers
540 * for previous datagrams, while consumption of application
541 * data (2) is user-controlled.
542 *
543 * b Reading of application data
544 * [Currently manual adaption of ssl->in_offt pointer]
545 *
546 * As mentioned in the last paragraph, consumption of data
547 * is different from the automatic consumption of control
548 * datagrams (1) because application data is treated as a stream.
549 *
550 * c Tracking availability of application data
551 * [Currently manually through decreasing ssl->in_msglen]
552 *
553 * For efficiency and to retain datagram semantics for
554 * application data in case of DTLS, the record layer
555 * provides functionality for checking how much application
556 * data is still available in the internal buffer.
557 *
558 * d Changing the transformation securing the communication.
559 *
560 * Given an opaque implementation of the record layer in the
561 * above sense, it should be possible to implement the logic
562 * of (D)TLS on top of it without the need to know anything
563 * about the record layer's internals. This is done e.g.
564 * in all the handshake handling functions, and in the
565 * application data reading function mbedtls_ssl_read.
566 *
567 * \note The above tries to give a conceptual picture of the
568 * record layer, but the current implementation deviates
569 * from it in some places. For example, our implementation of
570 * the update functionality through mbedtls_ssl_read_record
571 * discards datagrams depending on the current state, which
572 * wouldn't fall under the record layer's responsibility
573 * following the above definition.
574 *
575 */
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +0200576int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl );
577int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want );
578
579int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl );
580int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl );
581
582int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl );
583int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl );
584
585int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl );
586int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl );
587
588int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl );
589int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl );
590
591void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
592 const mbedtls_ssl_ciphersuite_t *ciphersuite_info );
593
594#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
595int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex );
596#endif
597
598#if defined(MBEDTLS_PK_C)
599unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk );
Hanno Becker7e5437a2017-04-28 17:15:26 +0100600unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type );
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +0200601mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig );
602#endif
603
604mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash );
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +0200605unsigned char mbedtls_ssl_hash_from_md_alg( int md );
Simon Butcher99000142016-10-13 17:21:01 +0100606int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md );
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +0200607
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +0200608#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +0200609int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id );
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +0200610#endif
611
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +0200612#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +0200613int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
614 mbedtls_md_type_t md );
615#endif
616
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +0200617#if defined(MBEDTLS_X509_CRT_PARSE_C)
618static inline mbedtls_pk_context *mbedtls_ssl_own_key( mbedtls_ssl_context *ssl )
619{
620 mbedtls_ssl_key_cert *key_cert;
621
622 if( ssl->handshake != NULL && ssl->handshake->key_cert != NULL )
623 key_cert = ssl->handshake->key_cert;
624 else
625 key_cert = ssl->conf->key_cert;
626
627 return( key_cert == NULL ? NULL : key_cert->key );
628}
629
630static inline mbedtls_x509_crt *mbedtls_ssl_own_cert( mbedtls_ssl_context *ssl )
631{
632 mbedtls_ssl_key_cert *key_cert;
633
634 if( ssl->handshake != NULL && ssl->handshake->key_cert != NULL )
635 key_cert = ssl->handshake->key_cert;
636 else
637 key_cert = ssl->conf->key_cert;
638
639 return( key_cert == NULL ? NULL : key_cert->cert );
640}
641
642/*
643 * Check usage of a certificate wrt extensions:
644 * keyUsage, extendedKeyUsage (later), and nSCertType (later).
645 *
646 * Warning: cert_endpoint is the endpoint of the cert (ie, of our peer when we
647 * check a cert we received from them)!
648 *
649 * Return 0 if everything is OK, -1 if not.
650 */
651int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
652 const mbedtls_ssl_ciphersuite_t *ciphersuite,
653 int cert_endpoint,
654 uint32_t *flags );
655#endif /* MBEDTLS_X509_CRT_PARSE_C */
656
657void mbedtls_ssl_write_version( int major, int minor, int transport,
658 unsigned char ver[2] );
659void mbedtls_ssl_read_version( int *major, int *minor, int transport,
660 const unsigned char ver[2] );
661
662static inline size_t mbedtls_ssl_hdr_len( const mbedtls_ssl_context *ssl )
663{
664#if defined(MBEDTLS_SSL_PROTO_DTLS)
665 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
666 return( 13 );
667#else
668 ((void) ssl);
669#endif
670 return( 5 );
671}
672
673static inline size_t mbedtls_ssl_hs_hdr_len( const mbedtls_ssl_context *ssl )
674{
675#if defined(MBEDTLS_SSL_PROTO_DTLS)
676 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
677 return( 12 );
678#else
679 ((void) ssl);
680#endif
681 return( 4 );
682}
683
684#if defined(MBEDTLS_SSL_PROTO_DTLS)
685void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl );
686void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl );
687int mbedtls_ssl_resend( mbedtls_ssl_context *ssl );
688#endif
689
690/* Visible for testing purposes only */
691#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
692int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl );
693void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl );
694#endif
695
696/* constant-time buffer comparison */
697static inline int mbedtls_ssl_safer_memcmp( const void *a, const void *b, size_t n )
698{
699 size_t i;
Hanno Becker59e69632017-06-26 13:26:58 +0100700 volatile const unsigned char *A = (volatile const unsigned char *) a;
701 volatile const unsigned char *B = (volatile const unsigned char *) b;
702 volatile unsigned char diff = 0;
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +0200703
704 for( i = 0; i < n; i++ )
Azim Khan826cdab2018-05-23 16:55:16 +0100705 {
706 /* Read volatile data in order before computing diff.
707 * This avoids IAR compiler warning:
708 * 'the order of volatile accesses is undefined ..' */
709 unsigned char x = A[i], y = B[i];
710 diff |= x ^ y;
711 }
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +0200712
713 return( diff );
714}
715
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +0100716#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
717 defined(MBEDTLS_SSL_PROTO_TLS1_1)
718int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
719 unsigned char *output,
720 unsigned char *data, size_t data_len );
721#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
722 MBEDTLS_SSL_PROTO_TLS1_1 */
723
724#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
725 defined(MBEDTLS_SSL_PROTO_TLS1_2)
726int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
727 unsigned char *output,
728 unsigned char *data, size_t data_len,
729 mbedtls_md_type_t md_alg );
730#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
731 MBEDTLS_SSL_PROTO_TLS1_2 */
732
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +0200733#ifdef __cplusplus
734}
735#endif
736
737#endif /* ssl_internal.h */