blob: 036b60a0653a5fae872e8a4d8a6f3e258427919a [file] [log] [blame]
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +02001/**
2 * \file ssl_ticket.h
3 *
4 * \brief Internal functions shared by the SSL modules
5 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02006 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02007 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020020 *
21 * This file is part of mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020022 */
23#ifndef MBEDTLS_SSL_INTERNAL_H
24#define MBEDTLS_SSL_INTERNAL_H
25
26#include "ssl.h"
27
Manuel Pégourié-Gonnard56273da2015-05-26 12:19:45 +020028#if defined(MBEDTLS_MD5_C)
29#include "md5.h"
30#endif
31
32#if defined(MBEDTLS_SHA1_C)
33#include "sha1.h"
34#endif
35
36#if defined(MBEDTLS_SHA256_C)
37#include "sha256.h"
38#endif
39
40#if defined(MBEDTLS_SHA512_C)
41#include "sha512.h"
42#endif
43
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020044#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020045#include "ecjpake.h"
46#endif
47
Manuel Pégourié-Gonnard0223ab92015-10-05 11:40:01 +010048#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
49 !defined(inline) && !defined(__cplusplus)
Manuel Pégourié-Gonnard065122c2015-05-26 12:31:46 +020050#define inline __inline
Manuel Pégourié-Gonnard20af64d2015-07-07 18:33:39 +020051#endif
Manuel Pégourié-Gonnard065122c2015-05-26 12:31:46 +020052
53/* Determine minimum supported version */
54#define MBEDTLS_SSL_MIN_MAJOR_VERSION MBEDTLS_SSL_MAJOR_VERSION_3
55
56#if defined(MBEDTLS_SSL_PROTO_SSL3)
57#define MBEDTLS_SSL_MIN_MINOR_VERSION MBEDTLS_SSL_MINOR_VERSION_0
58#else
59#if defined(MBEDTLS_SSL_PROTO_TLS1)
60#define MBEDTLS_SSL_MIN_MINOR_VERSION MBEDTLS_SSL_MINOR_VERSION_1
61#else
62#if defined(MBEDTLS_SSL_PROTO_TLS1_1)
63#define MBEDTLS_SSL_MIN_MINOR_VERSION MBEDTLS_SSL_MINOR_VERSION_2
64#else
65#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
66#define MBEDTLS_SSL_MIN_MINOR_VERSION MBEDTLS_SSL_MINOR_VERSION_3
67#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
68#endif /* MBEDTLS_SSL_PROTO_TLS1_1 */
69#endif /* MBEDTLS_SSL_PROTO_TLS1 */
70#endif /* MBEDTLS_SSL_PROTO_SSL3 */
71
72/* Determine maximum supported version */
73#define MBEDTLS_SSL_MAX_MAJOR_VERSION MBEDTLS_SSL_MAJOR_VERSION_3
74
75#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
76#define MBEDTLS_SSL_MAX_MINOR_VERSION MBEDTLS_SSL_MINOR_VERSION_3
77#else
78#if defined(MBEDTLS_SSL_PROTO_TLS1_1)
79#define MBEDTLS_SSL_MAX_MINOR_VERSION MBEDTLS_SSL_MINOR_VERSION_2
80#else
81#if defined(MBEDTLS_SSL_PROTO_TLS1)
82#define MBEDTLS_SSL_MAX_MINOR_VERSION MBEDTLS_SSL_MINOR_VERSION_1
83#else
84#if defined(MBEDTLS_SSL_PROTO_SSL3)
85#define MBEDTLS_SSL_MAX_MINOR_VERSION MBEDTLS_SSL_MINOR_VERSION_0
86#endif /* MBEDTLS_SSL_PROTO_SSL3 */
87#endif /* MBEDTLS_SSL_PROTO_TLS1 */
88#endif /* MBEDTLS_SSL_PROTO_TLS1_1 */
89#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
90
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020091/* Shorthand for restartable ECC */
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +020092#if defined(MBEDTLS_ECP_RESTARTABLE) && \
93 defined(MBEDTLS_SSL_CLI_C) && \
94 defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
95 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
96#define MBEDTLS_SSL__ECP_RESTARTABLE
97#endif
98
Manuel Pégourié-Gonnard065122c2015-05-26 12:31:46 +020099#define MBEDTLS_SSL_INITIAL_HANDSHAKE 0
100#define MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS 1 /* In progress */
101#define MBEDTLS_SSL_RENEGOTIATION_DONE 2 /* Done or aborted */
102#define MBEDTLS_SSL_RENEGOTIATION_PENDING 3 /* Requested (server only) */
103
104/*
105 * DTLS retransmission states, see RFC 6347 4.2.4
106 *
107 * The SENDING state is merged in PREPARING for initial sends,
108 * but is distinct for resends.
109 *
110 * Note: initial state is wrong for server, but is not used anyway.
111 */
112#define MBEDTLS_SSL_RETRANS_PREPARING 0
113#define MBEDTLS_SSL_RETRANS_SENDING 1
114#define MBEDTLS_SSL_RETRANS_WAITING 2
115#define MBEDTLS_SSL_RETRANS_FINISHED 3
116
117/*
118 * Allow extra bytes for record, authentication and encryption overhead:
119 * counter (8) + header (5) + IV(16) + MAC (16-48) + padding (0-256)
120 * and allow for a maximum of 1024 of compression expansion if
121 * enabled.
122 */
123#if defined(MBEDTLS_ZLIB_SUPPORT)
124#define MBEDTLS_SSL_COMPRESSION_ADD 1024
125#else
126#define MBEDTLS_SSL_COMPRESSION_ADD 0
127#endif
128
129#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_MODE_CBC)
130/* Ciphersuites using HMAC */
131#if defined(MBEDTLS_SHA512_C)
132#define MBEDTLS_SSL_MAC_ADD 48 /* SHA-384 used for HMAC */
133#elif defined(MBEDTLS_SHA256_C)
134#define MBEDTLS_SSL_MAC_ADD 32 /* SHA-256 used for HMAC */
135#else
136#define MBEDTLS_SSL_MAC_ADD 20 /* SHA-1 used for HMAC */
137#endif
138#else
139/* AEAD ciphersuites: GCM and CCM use a 128 bits tag */
140#define MBEDTLS_SSL_MAC_ADD 16
141#endif
142
143#if defined(MBEDTLS_CIPHER_MODE_CBC)
144#define MBEDTLS_SSL_PADDING_ADD 256
145#else
146#define MBEDTLS_SSL_PADDING_ADD 0
147#endif
148
149#define MBEDTLS_SSL_BUFFER_LEN ( MBEDTLS_SSL_MAX_CONTENT_LEN \
150 + MBEDTLS_SSL_COMPRESSION_ADD \
151 + 29 /* counter + header + IV */ \
152 + MBEDTLS_SSL_MAC_ADD \
153 + MBEDTLS_SSL_PADDING_ADD \
154 )
155
156/*
157 * TLS extension flags (for extensions with outgoing ServerHello content
158 * that need it (e.g. for RENEGOTIATION_INFO the server already knows because
159 * of state of the renegotiation flag, so no indicator is required)
160 */
161#define MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT (1 << 0)
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +0200162#define MBEDTLS_TLS_EXT_ECJPAKE_KKPP_OK (1 << 1)
Manuel Pégourié-Gonnard065122c2015-05-26 12:31:46 +0200163
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +0200164#ifdef __cplusplus
165extern "C" {
166#endif
167
Hanno Becker7e5437a2017-04-28 17:15:26 +0100168#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
169 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
170/*
171 * Abstraction for a grid of allowed signature-hash-algorithm pairs.
172 */
173struct mbedtls_ssl_sig_hash_set_t
174{
175 /* At the moment, we only need to remember a single suitable
176 * hash algorithm per signature algorithm. As long as that's
177 * the case - and we don't need a general lookup function -
178 * we can implement the sig-hash-set as a map from signatures
179 * to hash algorithms. */
180 mbedtls_md_type_t rsa;
181 mbedtls_md_type_t ecdsa;
182};
183#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
184 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
185
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200186/*
187 * This structure contains the parameters only needed during handshake.
188 */
189struct mbedtls_ssl_handshake_params
190{
191 /*
192 * Handshake specific crypto variables
193 */
Hanno Becker7e5437a2017-04-28 17:15:26 +0100194
195#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
196 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
197 mbedtls_ssl_sig_hash_set_t hash_algs; /*!< Set of suitable sig-hash pairs */
198#endif
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200199#if defined(MBEDTLS_DHM_C)
200 mbedtls_dhm_context dhm_ctx; /*!< DHM key exchange */
201#endif
202#if defined(MBEDTLS_ECDH_C)
203 mbedtls_ecdh_context ecdh_ctx; /*!< ECDH key exchange */
204#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200205#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200206 mbedtls_ecjpake_context ecjpake_ctx; /*!< EC J-PAKE key exchange */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +0200207#if defined(MBEDTLS_SSL_CLI_C)
208 unsigned char *ecjpake_cache; /*!< Cache for ClientHello ext */
209 size_t ecjpake_cache_len; /*!< Length of cached data */
210#endif
Hanno Becker1aa267c2017-04-28 17:08:27 +0100211#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnardf4721792015-09-15 10:53:51 +0200212#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200213 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200214 const mbedtls_ecp_curve_info **curves; /*!< Supported elliptic curves */
215#endif
216#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
217 unsigned char *psk; /*!< PSK from the callback */
218 size_t psk_len; /*!< Length of PSK from callback */
219#endif
220#if defined(MBEDTLS_X509_CRT_PARSE_C)
221 mbedtls_ssl_key_cert *key_cert; /*!< chosen key/cert pair (server) */
222#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200223 int sni_authmode; /*!< authmode from SNI callback */
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200224 mbedtls_ssl_key_cert *sni_key_cert; /*!< key/cert list from SNI */
225 mbedtls_x509_crt *sni_ca_chain; /*!< trusted CAs from SNI callback */
226 mbedtls_x509_crl *sni_ca_crl; /*!< trusted CAs CRLs from SNI */
Hanno Becker1aa267c2017-04-28 17:08:27 +0100227#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200228#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +0200229#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +0200230 int ecrs_enabled; /*!< Handshake supports EC restart? */
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +0200231 mbedtls_x509_crt_restart_ctx ecrs_ctx; /*!< restart context */
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +0200232 enum { /* this complements ssl->state with info on intra-state operations */
233 ssl_ecrs_none = 0, /*!< nothing going on (yet) */
234 ssl_ecrs_crt_verify, /*!< Certificate: crt_verify() */
235 ssl_ecrs_ske_start_processing, /*!< ServerKeyExchange: step 1 */
236 ssl_ecrs_ske_ecdh_calc_secret, /*!< ServerKeyExchange: ECDH step 2 */
237 ssl_ecrs_crt_vrfy_sign, /*!< CertificateVerify: pk_sign() */
238 } ecrs_state; /*!< current (or last) operation */
239 size_t ecrs_n; /*!< place for saving a length */
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +0200240#endif
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200241#if defined(MBEDTLS_SSL_PROTO_DTLS)
242 unsigned int out_msg_seq; /*!< Outgoing handshake sequence number */
243 unsigned int in_msg_seq; /*!< Incoming handshake sequence number */
244
245 unsigned char *verify_cookie; /*!< Cli: HelloVerifyRequest cookie
246 Srv: unused */
247 unsigned char verify_cookie_len; /*!< Cli: cookie length
248 Srv: flag for sending a cookie */
249
250 unsigned char *hs_msg; /*!< Reassembled handshake message */
251
252 uint32_t retransmit_timeout; /*!< Current value of timeout */
253 unsigned char retransmit_state; /*!< Retransmission state */
254 mbedtls_ssl_flight_item *flight; /*!< Current outgoing flight */
255 mbedtls_ssl_flight_item *cur_msg; /*!< Current message in flight */
256 unsigned int in_flight_start_seq; /*!< Minimum message sequence in the
257 flight being received */
258 mbedtls_ssl_transform *alt_transform_out; /*!< Alternative transform for
259 resending messages */
260 unsigned char alt_out_ctr[8]; /*!< Alternative record epoch/counter
261 for resending messages */
Hanno Becker1aa267c2017-04-28 17:08:27 +0100262#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200263
264 /*
265 * Checksum contexts
266 */
267#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
268 defined(MBEDTLS_SSL_PROTO_TLS1_1)
269 mbedtls_md5_context fin_md5;
270 mbedtls_sha1_context fin_sha1;
271#endif
272#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
273#if defined(MBEDTLS_SHA256_C)
274 mbedtls_sha256_context fin_sha256;
275#endif
276#if defined(MBEDTLS_SHA512_C)
277 mbedtls_sha512_context fin_sha512;
278#endif
279#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
280
281 void (*update_checksum)(mbedtls_ssl_context *, const unsigned char *, size_t);
282 void (*calc_verify)(mbedtls_ssl_context *, unsigned char *);
283 void (*calc_finished)(mbedtls_ssl_context *, unsigned char *, int);
284 int (*tls_prf)(const unsigned char *, size_t, const char *,
285 const unsigned char *, size_t,
286 unsigned char *, size_t);
287
288 size_t pmslen; /*!< premaster length */
289
290 unsigned char randbytes[64]; /*!< random bytes */
291 unsigned char premaster[MBEDTLS_PREMASTER_SIZE];
292 /*!< premaster secret */
293
294 int resume; /*!< session resume indicator*/
295 int max_major_ver; /*!< max. major version client*/
296 int max_minor_ver; /*!< max. minor version client*/
297 int cli_exts; /*!< client extension presence*/
298
299#if defined(MBEDTLS_SSL_SESSION_TICKETS)
300 int new_session_ticket; /*!< use NewSessionTicket? */
301#endif /* MBEDTLS_SSL_SESSION_TICKETS */
302#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
303 int extended_ms; /*!< use Extended Master Secret? */
304#endif
305};
306
307/*
308 * This structure contains a full set of runtime transform parameters
309 * either in negotiation or active.
310 */
311struct mbedtls_ssl_transform
312{
313 /*
314 * Session specific crypto layer
315 */
316 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
317 /*!< Chosen cipersuite_info */
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200318 unsigned int keylen; /*!< symmetric key length (bytes) */
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200319 size_t minlen; /*!< min. ciphertext length */
320 size_t ivlen; /*!< IV length */
321 size_t fixed_ivlen; /*!< Fixed part of IV (AEAD) */
322 size_t maclen; /*!< MAC length */
323
324 unsigned char iv_enc[16]; /*!< IV (encryption) */
325 unsigned char iv_dec[16]; /*!< IV (decryption) */
326
327#if defined(MBEDTLS_SSL_PROTO_SSL3)
328 /* Needed only for SSL v3.0 secret */
329 unsigned char mac_enc[20]; /*!< SSL v3.0 secret (enc) */
330 unsigned char mac_dec[20]; /*!< SSL v3.0 secret (dec) */
331#endif /* MBEDTLS_SSL_PROTO_SSL3 */
332
333 mbedtls_md_context_t md_ctx_enc; /*!< MAC (encryption) */
334 mbedtls_md_context_t md_ctx_dec; /*!< MAC (decryption) */
335
336 mbedtls_cipher_context_t cipher_ctx_enc; /*!< encryption context */
337 mbedtls_cipher_context_t cipher_ctx_dec; /*!< decryption context */
338
339 /*
340 * Session specific compression layer
341 */
342#if defined(MBEDTLS_ZLIB_SUPPORT)
343 z_stream ctx_deflate; /*!< compression context */
344 z_stream ctx_inflate; /*!< decompression context */
345#endif
346};
347
348#if defined(MBEDTLS_X509_CRT_PARSE_C)
349/*
350 * List of certificate + private key pairs
351 */
352struct mbedtls_ssl_key_cert
353{
354 mbedtls_x509_crt *cert; /*!< cert */
355 mbedtls_pk_context *key; /*!< private key */
356 mbedtls_ssl_key_cert *next; /*!< next key/cert pair */
357};
358#endif /* MBEDTLS_X509_CRT_PARSE_C */
359
360#if defined(MBEDTLS_SSL_PROTO_DTLS)
361/*
362 * List of handshake messages kept around for resending
363 */
364struct mbedtls_ssl_flight_item
365{
366 unsigned char *p; /*!< message, including handshake headers */
367 size_t len; /*!< length of p */
368 unsigned char type; /*!< type of the message: handshake or CCS */
369 mbedtls_ssl_flight_item *next; /*!< next handshake message(s) */
370};
371#endif /* MBEDTLS_SSL_PROTO_DTLS */
372
Hanno Becker7e5437a2017-04-28 17:15:26 +0100373#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
374 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
375
376/* Find an entry in a signature-hash set matching a given hash algorithm. */
377mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
378 mbedtls_pk_type_t sig_alg );
379/* Add a signature-hash-pair to a signature-hash set */
380void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
381 mbedtls_pk_type_t sig_alg,
382 mbedtls_md_type_t md_alg );
383/* Allow exactly one hash algorithm for each signature. */
384void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
385 mbedtls_md_type_t md_alg );
386
387/* Setup an empty signature-hash set */
388static inline void mbedtls_ssl_sig_hash_set_init( mbedtls_ssl_sig_hash_set_t *set )
389{
390 mbedtls_ssl_sig_hash_set_const_hash( set, MBEDTLS_MD_NONE );
391}
392
393#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
394 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200395
396/**
397 * \brief Free referenced items in an SSL transform context and clear
398 * memory
399 *
400 * \param transform SSL transform context
401 */
402void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform );
403
404/**
405 * \brief Free referenced items in an SSL handshake context and clear
406 * memory
407 *
408 * \param handshake SSL handshake context
409 */
410void mbedtls_ssl_handshake_free( mbedtls_ssl_handshake_params *handshake );
411
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +0200412int mbedtls_ssl_handshake_client_step( mbedtls_ssl_context *ssl );
413int mbedtls_ssl_handshake_server_step( mbedtls_ssl_context *ssl );
414void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl );
415
416int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl );
417
418void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl );
419int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl );
420
Simon Butcher99000142016-10-13 17:21:01 +0100421int mbedtls_ssl_read_record_layer( mbedtls_ssl_context *ssl );
422int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl );
423int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl );
424void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl );
425
Hanno Becker4a810fb2017-05-24 16:27:30 +0100426/**
427 * \brief Update record layer
428 *
429 * This function roughly separates the implementation
430 * of the logic of (D)TLS from the implementation
431 * of the secure transport.
432 *
433 * \param ssl SSL context to use
434 *
435 * \return 0 or non-zero error code.
436 *
437 * \note A clarification on what is called 'record layer' here
438 * is in order, as many sensible definitions are possible:
439 *
440 * The record layer takes as input an untrusted underlying
441 * transport (stream or datagram) and transforms it into
442 * a serially multiplexed, secure transport, which
443 * conceptually provides the following:
444 *
445 * (1) Three datagram based, content-agnostic transports
446 * for handshake, alert and CCS messages.
447 * (2) One stream- or datagram-based transport
448 * for application data.
449 * (3) Functionality for changing the underlying transform
450 * securing the contents.
451 *
452 * The interface to this functionality is given as follows:
453 *
454 * a Updating
455 * [Currently implemented by mbedtls_ssl_read_record]
456 *
457 * Check if and on which of the four 'ports' data is pending:
458 * Nothing, a controlling datagram of type (1), or application
459 * data (2). In any case data is present, internal buffers
460 * provide access to the data for the user to process it.
461 * Consumption of type (1) datagrams is done automatically
462 * on the next update, invalidating that the internal buffers
463 * for previous datagrams, while consumption of application
464 * data (2) is user-controlled.
465 *
466 * b Reading of application data
467 * [Currently manual adaption of ssl->in_offt pointer]
468 *
469 * As mentioned in the last paragraph, consumption of data
470 * is different from the automatic consumption of control
471 * datagrams (1) because application data is treated as a stream.
472 *
473 * c Tracking availability of application data
474 * [Currently manually through decreasing ssl->in_msglen]
475 *
476 * For efficiency and to retain datagram semantics for
477 * application data in case of DTLS, the record layer
478 * provides functionality for checking how much application
479 * data is still available in the internal buffer.
480 *
481 * d Changing the transformation securing the communication.
482 *
483 * Given an opaque implementation of the record layer in the
484 * above sense, it should be possible to implement the logic
485 * of (D)TLS on top of it without the need to know anything
486 * about the record layer's internals. This is done e.g.
487 * in all the handshake handling functions, and in the
488 * application data reading function mbedtls_ssl_read.
489 *
490 * \note The above tries to give a conceptual picture of the
491 * record layer, but the current implementation deviates
492 * from it in some places. For example, our implementation of
493 * the update functionality through mbedtls_ssl_read_record
494 * discards datagrams depending on the current state, which
495 * wouldn't fall under the record layer's responsibility
496 * following the above definition.
497 *
498 */
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +0200499int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl );
500int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want );
501
502int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl );
503int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl );
504
505int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl );
506int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl );
507
508int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl );
509int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl );
510
511int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl );
512int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl );
513
514void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
515 const mbedtls_ssl_ciphersuite_t *ciphersuite_info );
516
517#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
518int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex );
519#endif
520
521#if defined(MBEDTLS_PK_C)
522unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk );
Hanno Becker7e5437a2017-04-28 17:15:26 +0100523unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type );
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +0200524mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig );
525#endif
526
527mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash );
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +0200528unsigned char mbedtls_ssl_hash_from_md_alg( int md );
Simon Butcher99000142016-10-13 17:21:01 +0100529int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md );
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +0200530
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +0200531#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +0200532int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id );
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +0200533#endif
534
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +0200535#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +0200536int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
537 mbedtls_md_type_t md );
538#endif
539
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +0200540#if defined(MBEDTLS_X509_CRT_PARSE_C)
541static inline mbedtls_pk_context *mbedtls_ssl_own_key( mbedtls_ssl_context *ssl )
542{
543 mbedtls_ssl_key_cert *key_cert;
544
545 if( ssl->handshake != NULL && ssl->handshake->key_cert != NULL )
546 key_cert = ssl->handshake->key_cert;
547 else
548 key_cert = ssl->conf->key_cert;
549
550 return( key_cert == NULL ? NULL : key_cert->key );
551}
552
553static inline mbedtls_x509_crt *mbedtls_ssl_own_cert( mbedtls_ssl_context *ssl )
554{
555 mbedtls_ssl_key_cert *key_cert;
556
557 if( ssl->handshake != NULL && ssl->handshake->key_cert != NULL )
558 key_cert = ssl->handshake->key_cert;
559 else
560 key_cert = ssl->conf->key_cert;
561
562 return( key_cert == NULL ? NULL : key_cert->cert );
563}
564
565/*
566 * Check usage of a certificate wrt extensions:
567 * keyUsage, extendedKeyUsage (later), and nSCertType (later).
568 *
569 * Warning: cert_endpoint is the endpoint of the cert (ie, of our peer when we
570 * check a cert we received from them)!
571 *
572 * Return 0 if everything is OK, -1 if not.
573 */
574int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
575 const mbedtls_ssl_ciphersuite_t *ciphersuite,
576 int cert_endpoint,
577 uint32_t *flags );
578#endif /* MBEDTLS_X509_CRT_PARSE_C */
579
580void mbedtls_ssl_write_version( int major, int minor, int transport,
581 unsigned char ver[2] );
582void mbedtls_ssl_read_version( int *major, int *minor, int transport,
583 const unsigned char ver[2] );
584
585static inline size_t mbedtls_ssl_hdr_len( const mbedtls_ssl_context *ssl )
586{
587#if defined(MBEDTLS_SSL_PROTO_DTLS)
588 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
589 return( 13 );
590#else
591 ((void) ssl);
592#endif
593 return( 5 );
594}
595
596static inline size_t mbedtls_ssl_hs_hdr_len( const mbedtls_ssl_context *ssl )
597{
598#if defined(MBEDTLS_SSL_PROTO_DTLS)
599 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
600 return( 12 );
601#else
602 ((void) ssl);
603#endif
604 return( 4 );
605}
606
607#if defined(MBEDTLS_SSL_PROTO_DTLS)
608void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl );
609void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl );
610int mbedtls_ssl_resend( mbedtls_ssl_context *ssl );
611#endif
612
613/* Visible for testing purposes only */
614#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
615int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl );
616void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl );
617#endif
618
619/* constant-time buffer comparison */
620static inline int mbedtls_ssl_safer_memcmp( const void *a, const void *b, size_t n )
621{
622 size_t i;
623 const unsigned char *A = (const unsigned char *) a;
624 const unsigned char *B = (const unsigned char *) b;
625 unsigned char diff = 0;
626
627 for( i = 0; i < n; i++ )
628 diff |= A[i] ^ B[i];
629
630 return( diff );
631}
632
633#ifdef __cplusplus
634}
635#endif
636
637#endif /* ssl_internal.h */