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