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