blob: 74716e69108a9ecdf6d989195deeeb0f090ab29e [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
147/*
148 * Allow extra bytes for record, authentication and encryption overhead:
149 * counter (8) + header (5) + IV(16) + MAC (16-48) + padding (0-256)
150 * and allow for a maximum of 1024 of compression expansion if
151 * enabled.
152 */
153#if defined(MBEDTLS_ZLIB_SUPPORT)
154#define MBEDTLS_SSL_COMPRESSION_ADD 1024
155#else
156#define MBEDTLS_SSL_COMPRESSION_ADD 0
157#endif
158
159#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_MODE_CBC)
160/* Ciphersuites using HMAC */
161#if defined(MBEDTLS_SHA512_C)
162#define MBEDTLS_SSL_MAC_ADD 48 /* SHA-384 used for HMAC */
163#elif defined(MBEDTLS_SHA256_C)
164#define MBEDTLS_SSL_MAC_ADD 32 /* SHA-256 used for HMAC */
165#else
166#define MBEDTLS_SSL_MAC_ADD 20 /* SHA-1 used for HMAC */
167#endif
168#else
169/* AEAD ciphersuites: GCM and CCM use a 128 bits tag */
170#define MBEDTLS_SSL_MAC_ADD 16
171#endif
172
173#if defined(MBEDTLS_CIPHER_MODE_CBC)
174#define MBEDTLS_SSL_PADDING_ADD 256
175#else
176#define MBEDTLS_SSL_PADDING_ADD 0
177#endif
178
Hanno Beckera8434e82017-09-18 10:54:39 +0100179#define MBEDTLS_SSL_PAYLOAD_LEN ( MBEDTLS_SSL_MAX_CONTENT_LEN \
180 + MBEDTLS_SSL_COMPRESSION_ADD \
181 + MBEDTLS_MAX_IV_LENGTH \
182 + MBEDTLS_SSL_MAC_ADD \
183 + MBEDTLS_SSL_PADDING_ADD \
Manuel Pégourié-Gonnard065122c2015-05-26 12:31:46 +0200184 )
185
186/*
Hanno Beckera8434e82017-09-18 10:54:39 +0100187 * Check that we obey the standard's message size bounds
188 */
189
190#if MBEDTLS_SSL_MAX_CONTENT_LEN > 16384
191#error Bad configuration - record content too large.
192#endif
193
194#if MBEDTLS_SSL_PAYLOAD_LEN > 16384 + 2048
195#error Bad configuration - protected record payload too large.
196#endif
197
Hanno Becker25d6d1a2017-12-07 08:22:51 +0000198/* Note: Even though the TLS record header is only 5 bytes
199 long, we're internally using 8 bytes to store the
200 implicit sequence number. */
Hanno Beckerd25d4442017-10-04 13:56:42 +0100201#define MBEDTLS_SSL_HEADER_LEN 13
Hanno Beckera8434e82017-09-18 10:54:39 +0100202
Hanno Beckerd25d4442017-10-04 13:56:42 +0100203#define MBEDTLS_SSL_BUFFER_LEN \
204 ( ( MBEDTLS_SSL_HEADER_LEN ) + ( MBEDTLS_SSL_PAYLOAD_LEN ) )
Hanno Beckera8434e82017-09-18 10:54:39 +0100205
206/*
Manuel Pégourié-Gonnard065122c2015-05-26 12:31:46 +0200207 * TLS extension flags (for extensions with outgoing ServerHello content
208 * that need it (e.g. for RENEGOTIATION_INFO the server already knows because
209 * of state of the renegotiation flag, so no indicator is required)
210 */
211#define MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT (1 << 0)
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +0200212#define MBEDTLS_TLS_EXT_ECJPAKE_KKPP_OK (1 << 1)
Manuel Pégourié-Gonnard065122c2015-05-26 12:31:46 +0200213
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +0200214#ifdef __cplusplus
215extern "C" {
216#endif
217
Hanno Becker7e5437a2017-04-28 17:15:26 +0100218#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
219 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
220/*
221 * Abstraction for a grid of allowed signature-hash-algorithm pairs.
222 */
223struct mbedtls_ssl_sig_hash_set_t
224{
225 /* At the moment, we only need to remember a single suitable
226 * hash algorithm per signature algorithm. As long as that's
227 * the case - and we don't need a general lookup function -
228 * we can implement the sig-hash-set as a map from signatures
229 * to hash algorithms. */
230 mbedtls_md_type_t rsa;
231 mbedtls_md_type_t ecdsa;
232};
233#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
234 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
235
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200236/*
237 * This structure contains the parameters only needed during handshake.
238 */
239struct mbedtls_ssl_handshake_params
240{
241 /*
242 * Handshake specific crypto variables
243 */
Hanno Becker7e5437a2017-04-28 17:15:26 +0100244
245#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
246 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
247 mbedtls_ssl_sig_hash_set_t hash_algs; /*!< Set of suitable sig-hash pairs */
248#endif
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200249#if defined(MBEDTLS_DHM_C)
250 mbedtls_dhm_context dhm_ctx; /*!< DHM key exchange */
251#endif
252#if defined(MBEDTLS_ECDH_C)
253 mbedtls_ecdh_context ecdh_ctx; /*!< ECDH key exchange */
254#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200255#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200256 mbedtls_ecjpake_context ecjpake_ctx; /*!< EC J-PAKE key exchange */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +0200257#if defined(MBEDTLS_SSL_CLI_C)
258 unsigned char *ecjpake_cache; /*!< Cache for ClientHello ext */
259 size_t ecjpake_cache_len; /*!< Length of cached data */
260#endif
Hanno Becker1aa267c2017-04-28 17:08:27 +0100261#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnardf4721792015-09-15 10:53:51 +0200262#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200263 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200264 const mbedtls_ecp_curve_info **curves; /*!< Supported elliptic curves */
265#endif
266#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
267 unsigned char *psk; /*!< PSK from the callback */
268 size_t psk_len; /*!< Length of PSK from callback */
269#endif
270#if defined(MBEDTLS_X509_CRT_PARSE_C)
271 mbedtls_ssl_key_cert *key_cert; /*!< chosen key/cert pair (server) */
272#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200273 int sni_authmode; /*!< authmode from SNI callback */
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200274 mbedtls_ssl_key_cert *sni_key_cert; /*!< key/cert list from SNI */
275 mbedtls_x509_crt *sni_ca_chain; /*!< trusted CAs from SNI callback */
276 mbedtls_x509_crl *sni_ca_crl; /*!< trusted CAs CRLs from SNI */
Hanno Becker1aa267c2017-04-28 17:08:27 +0100277#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200278#endif /* MBEDTLS_X509_CRT_PARSE_C */
279#if defined(MBEDTLS_SSL_PROTO_DTLS)
280 unsigned int out_msg_seq; /*!< Outgoing handshake sequence number */
281 unsigned int in_msg_seq; /*!< Incoming handshake sequence number */
282
283 unsigned char *verify_cookie; /*!< Cli: HelloVerifyRequest cookie
284 Srv: unused */
285 unsigned char verify_cookie_len; /*!< Cli: cookie length
286 Srv: flag for sending a cookie */
287
288 unsigned char *hs_msg; /*!< Reassembled handshake message */
289
290 uint32_t retransmit_timeout; /*!< Current value of timeout */
291 unsigned char retransmit_state; /*!< Retransmission state */
292 mbedtls_ssl_flight_item *flight; /*!< Current outgoing flight */
293 mbedtls_ssl_flight_item *cur_msg; /*!< Current message in flight */
294 unsigned int in_flight_start_seq; /*!< Minimum message sequence in the
295 flight being received */
296 mbedtls_ssl_transform *alt_transform_out; /*!< Alternative transform for
297 resending messages */
298 unsigned char alt_out_ctr[8]; /*!< Alternative record epoch/counter
299 for resending messages */
Hanno Becker1aa267c2017-04-28 17:08:27 +0100300#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200301
302 /*
303 * Checksum contexts
304 */
305#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
306 defined(MBEDTLS_SSL_PROTO_TLS1_1)
307 mbedtls_md5_context fin_md5;
308 mbedtls_sha1_context fin_sha1;
309#endif
310#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
311#if defined(MBEDTLS_SHA256_C)
312 mbedtls_sha256_context fin_sha256;
313#endif
314#if defined(MBEDTLS_SHA512_C)
315 mbedtls_sha512_context fin_sha512;
316#endif
317#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
318
319 void (*update_checksum)(mbedtls_ssl_context *, const unsigned char *, size_t);
320 void (*calc_verify)(mbedtls_ssl_context *, unsigned char *);
321 void (*calc_finished)(mbedtls_ssl_context *, unsigned char *, int);
322 int (*tls_prf)(const unsigned char *, size_t, const char *,
323 const unsigned char *, size_t,
324 unsigned char *, size_t);
325
326 size_t pmslen; /*!< premaster length */
327
328 unsigned char randbytes[64]; /*!< random bytes */
329 unsigned char premaster[MBEDTLS_PREMASTER_SIZE];
330 /*!< premaster secret */
331
332 int resume; /*!< session resume indicator*/
333 int max_major_ver; /*!< max. major version client*/
334 int max_minor_ver; /*!< max. minor version client*/
335 int cli_exts; /*!< client extension presence*/
336
337#if defined(MBEDTLS_SSL_SESSION_TICKETS)
338 int new_session_ticket; /*!< use NewSessionTicket? */
339#endif /* MBEDTLS_SSL_SESSION_TICKETS */
340#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
341 int extended_ms; /*!< use Extended Master Secret? */
342#endif
343};
344
345/*
346 * This structure contains a full set of runtime transform parameters
347 * either in negotiation or active.
348 */
349struct mbedtls_ssl_transform
350{
351 /*
352 * Session specific crypto layer
353 */
354 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
355 /*!< Chosen cipersuite_info */
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200356 unsigned int keylen; /*!< symmetric key length (bytes) */
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200357 size_t minlen; /*!< min. ciphertext length */
358 size_t ivlen; /*!< IV length */
359 size_t fixed_ivlen; /*!< Fixed part of IV (AEAD) */
360 size_t maclen; /*!< MAC length */
361
362 unsigned char iv_enc[16]; /*!< IV (encryption) */
363 unsigned char iv_dec[16]; /*!< IV (decryption) */
364
365#if defined(MBEDTLS_SSL_PROTO_SSL3)
366 /* Needed only for SSL v3.0 secret */
367 unsigned char mac_enc[20]; /*!< SSL v3.0 secret (enc) */
368 unsigned char mac_dec[20]; /*!< SSL v3.0 secret (dec) */
369#endif /* MBEDTLS_SSL_PROTO_SSL3 */
370
371 mbedtls_md_context_t md_ctx_enc; /*!< MAC (encryption) */
372 mbedtls_md_context_t md_ctx_dec; /*!< MAC (decryption) */
373
374 mbedtls_cipher_context_t cipher_ctx_enc; /*!< encryption context */
375 mbedtls_cipher_context_t cipher_ctx_dec; /*!< decryption context */
376
377 /*
378 * Session specific compression layer
379 */
380#if defined(MBEDTLS_ZLIB_SUPPORT)
381 z_stream ctx_deflate; /*!< compression context */
382 z_stream ctx_inflate; /*!< decompression context */
383#endif
384};
385
386#if defined(MBEDTLS_X509_CRT_PARSE_C)
387/*
388 * List of certificate + private key pairs
389 */
390struct mbedtls_ssl_key_cert
391{
392 mbedtls_x509_crt *cert; /*!< cert */
393 mbedtls_pk_context *key; /*!< private key */
394 mbedtls_ssl_key_cert *next; /*!< next key/cert pair */
395};
396#endif /* MBEDTLS_X509_CRT_PARSE_C */
397
398#if defined(MBEDTLS_SSL_PROTO_DTLS)
399/*
400 * List of handshake messages kept around for resending
401 */
402struct mbedtls_ssl_flight_item
403{
404 unsigned char *p; /*!< message, including handshake headers */
405 size_t len; /*!< length of p */
406 unsigned char type; /*!< type of the message: handshake or CCS */
407 mbedtls_ssl_flight_item *next; /*!< next handshake message(s) */
408};
409#endif /* MBEDTLS_SSL_PROTO_DTLS */
410
Hanno Becker7e5437a2017-04-28 17:15:26 +0100411#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
412 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
413
414/* Find an entry in a signature-hash set matching a given hash algorithm. */
415mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
416 mbedtls_pk_type_t sig_alg );
417/* Add a signature-hash-pair to a signature-hash set */
418void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
419 mbedtls_pk_type_t sig_alg,
420 mbedtls_md_type_t md_alg );
421/* Allow exactly one hash algorithm for each signature. */
422void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
423 mbedtls_md_type_t md_alg );
424
425/* Setup an empty signature-hash set */
426static inline void mbedtls_ssl_sig_hash_set_init( mbedtls_ssl_sig_hash_set_t *set )
427{
428 mbedtls_ssl_sig_hash_set_const_hash( set, MBEDTLS_MD_NONE );
429}
430
431#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
432 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200433
434/**
435 * \brief Free referenced items in an SSL transform context and clear
436 * memory
437 *
438 * \param transform SSL transform context
439 */
440void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform );
441
442/**
443 * \brief Free referenced items in an SSL handshake context and clear
444 * memory
445 *
446 * \param handshake SSL handshake context
447 */
448void mbedtls_ssl_handshake_free( mbedtls_ssl_handshake_params *handshake );
449
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +0200450int mbedtls_ssl_handshake_client_step( mbedtls_ssl_context *ssl );
451int mbedtls_ssl_handshake_server_step( mbedtls_ssl_context *ssl );
452void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl );
453
454int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl );
455
456void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl );
457int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl );
458
Simon Butcher99000142016-10-13 17:21:01 +0100459int mbedtls_ssl_read_record_layer( mbedtls_ssl_context *ssl );
460int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl );
461int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl );
462void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl );
463
Hanno Becker4a810fb2017-05-24 16:27:30 +0100464/**
465 * \brief Update record layer
466 *
467 * This function roughly separates the implementation
468 * of the logic of (D)TLS from the implementation
469 * of the secure transport.
470 *
471 * \param ssl SSL context to use
472 *
473 * \return 0 or non-zero error code.
474 *
475 * \note A clarification on what is called 'record layer' here
476 * is in order, as many sensible definitions are possible:
477 *
478 * The record layer takes as input an untrusted underlying
479 * transport (stream or datagram) and transforms it into
480 * a serially multiplexed, secure transport, which
481 * conceptually provides the following:
482 *
483 * (1) Three datagram based, content-agnostic transports
484 * for handshake, alert and CCS messages.
485 * (2) One stream- or datagram-based transport
486 * for application data.
487 * (3) Functionality for changing the underlying transform
488 * securing the contents.
489 *
490 * The interface to this functionality is given as follows:
491 *
492 * a Updating
493 * [Currently implemented by mbedtls_ssl_read_record]
494 *
495 * Check if and on which of the four 'ports' data is pending:
496 * Nothing, a controlling datagram of type (1), or application
497 * data (2). In any case data is present, internal buffers
498 * provide access to the data for the user to process it.
499 * Consumption of type (1) datagrams is done automatically
500 * on the next update, invalidating that the internal buffers
501 * for previous datagrams, while consumption of application
502 * data (2) is user-controlled.
503 *
504 * b Reading of application data
505 * [Currently manual adaption of ssl->in_offt pointer]
506 *
507 * As mentioned in the last paragraph, consumption of data
508 * is different from the automatic consumption of control
509 * datagrams (1) because application data is treated as a stream.
510 *
511 * c Tracking availability of application data
512 * [Currently manually through decreasing ssl->in_msglen]
513 *
514 * For efficiency and to retain datagram semantics for
515 * application data in case of DTLS, the record layer
516 * provides functionality for checking how much application
517 * data is still available in the internal buffer.
518 *
519 * d Changing the transformation securing the communication.
520 *
521 * Given an opaque implementation of the record layer in the
522 * above sense, it should be possible to implement the logic
523 * of (D)TLS on top of it without the need to know anything
524 * about the record layer's internals. This is done e.g.
525 * in all the handshake handling functions, and in the
526 * application data reading function mbedtls_ssl_read.
527 *
528 * \note The above tries to give a conceptual picture of the
529 * record layer, but the current implementation deviates
530 * from it in some places. For example, our implementation of
531 * the update functionality through mbedtls_ssl_read_record
532 * discards datagrams depending on the current state, which
533 * wouldn't fall under the record layer's responsibility
534 * following the above definition.
535 *
536 */
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +0200537int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl );
538int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want );
539
540int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl );
541int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl );
542
543int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl );
544int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl );
545
546int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl );
547int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl );
548
549int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl );
550int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl );
551
552void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
553 const mbedtls_ssl_ciphersuite_t *ciphersuite_info );
554
555#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
556int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex );
557#endif
558
559#if defined(MBEDTLS_PK_C)
560unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk );
Hanno Becker7e5437a2017-04-28 17:15:26 +0100561unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type );
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +0200562mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig );
563#endif
564
565mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash );
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +0200566unsigned char mbedtls_ssl_hash_from_md_alg( int md );
Simon Butcher99000142016-10-13 17:21:01 +0100567int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md );
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +0200568
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +0200569#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +0200570int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id );
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +0200571#endif
572
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +0200573#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +0200574int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
575 mbedtls_md_type_t md );
576#endif
577
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +0200578#if defined(MBEDTLS_X509_CRT_PARSE_C)
579static inline mbedtls_pk_context *mbedtls_ssl_own_key( mbedtls_ssl_context *ssl )
580{
581 mbedtls_ssl_key_cert *key_cert;
582
583 if( ssl->handshake != NULL && ssl->handshake->key_cert != NULL )
584 key_cert = ssl->handshake->key_cert;
585 else
586 key_cert = ssl->conf->key_cert;
587
588 return( key_cert == NULL ? NULL : key_cert->key );
589}
590
591static inline mbedtls_x509_crt *mbedtls_ssl_own_cert( mbedtls_ssl_context *ssl )
592{
593 mbedtls_ssl_key_cert *key_cert;
594
595 if( ssl->handshake != NULL && ssl->handshake->key_cert != NULL )
596 key_cert = ssl->handshake->key_cert;
597 else
598 key_cert = ssl->conf->key_cert;
599
600 return( key_cert == NULL ? NULL : key_cert->cert );
601}
602
603/*
604 * Check usage of a certificate wrt extensions:
605 * keyUsage, extendedKeyUsage (later), and nSCertType (later).
606 *
607 * Warning: cert_endpoint is the endpoint of the cert (ie, of our peer when we
608 * check a cert we received from them)!
609 *
610 * Return 0 if everything is OK, -1 if not.
611 */
612int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
613 const mbedtls_ssl_ciphersuite_t *ciphersuite,
614 int cert_endpoint,
615 uint32_t *flags );
616#endif /* MBEDTLS_X509_CRT_PARSE_C */
617
618void mbedtls_ssl_write_version( int major, int minor, int transport,
619 unsigned char ver[2] );
620void mbedtls_ssl_read_version( int *major, int *minor, int transport,
621 const unsigned char ver[2] );
622
623static inline size_t mbedtls_ssl_hdr_len( const mbedtls_ssl_context *ssl )
624{
625#if defined(MBEDTLS_SSL_PROTO_DTLS)
626 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
627 return( 13 );
628#else
629 ((void) ssl);
630#endif
631 return( 5 );
632}
633
634static inline size_t mbedtls_ssl_hs_hdr_len( const mbedtls_ssl_context *ssl )
635{
636#if defined(MBEDTLS_SSL_PROTO_DTLS)
637 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
638 return( 12 );
639#else
640 ((void) ssl);
641#endif
642 return( 4 );
643}
644
645#if defined(MBEDTLS_SSL_PROTO_DTLS)
646void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl );
647void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl );
648int mbedtls_ssl_resend( mbedtls_ssl_context *ssl );
649#endif
650
651/* Visible for testing purposes only */
652#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
653int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl );
654void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl );
655#endif
656
657/* constant-time buffer comparison */
658static inline int mbedtls_ssl_safer_memcmp( const void *a, const void *b, size_t n )
659{
660 size_t i;
Hanno Becker59e69632017-06-26 13:26:58 +0100661 volatile const unsigned char *A = (volatile const unsigned char *) a;
662 volatile const unsigned char *B = (volatile const unsigned char *) b;
663 volatile unsigned char diff = 0;
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +0200664
665 for( i = 0; i < n; i++ )
Azim Khan826cdab2018-05-23 16:55:16 +0100666 {
667 /* Read volatile data in order before computing diff.
668 * This avoids IAR compiler warning:
669 * 'the order of volatile accesses is undefined ..' */
670 unsigned char x = A[i], y = B[i];
671 diff |= x ^ y;
672 }
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +0200673
674 return( diff );
675}
676
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +0100677#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
678 defined(MBEDTLS_SSL_PROTO_TLS1_1)
679int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
680 unsigned char *output,
681 unsigned char *data, size_t data_len );
682#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
683 MBEDTLS_SSL_PROTO_TLS1_1 */
684
685#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
686 defined(MBEDTLS_SSL_PROTO_TLS1_2)
687int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
688 unsigned char *output,
689 unsigned char *data, size_t data_len,
690 mbedtls_md_type_t md_alg );
691#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
692 MBEDTLS_SSL_PROTO_TLS1_2 */
693
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +0200694#ifdef __cplusplus
695}
696#endif
697
698#endif /* ssl_internal.h */