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