blob: 509927ad94dcf3ddf15c52316d96a755cc16f794 [file] [log] [blame]
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +02001/**
Darryl Greena40a1012018-01-05 15:33:17 +00002 * \file ssl_internal.h
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +02003 *
4 * \brief Internal functions shared by the SSL modules
Darryl Greena40a1012018-01-05 15:33:17 +00005 */
6/*
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02007 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02008 * SPDX-License-Identifier: Apache-2.0
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License"); you may
11 * not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020021 *
22 * This file is part of mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020023 */
24#ifndef MBEDTLS_SSL_INTERNAL_H
25#define MBEDTLS_SSL_INTERNAL_H
26
27#include "ssl.h"
28
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
142#define MBEDTLS_SSL_BUFFER_LEN ( MBEDTLS_SSL_MAX_CONTENT_LEN \
143 + MBEDTLS_SSL_COMPRESSION_ADD \
144 + 29 /* counter + header + IV */ \
145 + MBEDTLS_SSL_MAC_ADD \
146 + MBEDTLS_SSL_PADDING_ADD \
147 )
148
149/*
150 * TLS extension flags (for extensions with outgoing ServerHello content
151 * that need it (e.g. for RENEGOTIATION_INFO the server already knows because
152 * of state of the renegotiation flag, so no indicator is required)
153 */
154#define MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT (1 << 0)
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +0200155#define MBEDTLS_TLS_EXT_ECJPAKE_KKPP_OK (1 << 1)
Manuel Pégourié-Gonnard065122c2015-05-26 12:31:46 +0200156
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +0200157#ifdef __cplusplus
158extern "C" {
159#endif
160
Hanno Becker7e5437a2017-04-28 17:15:26 +0100161#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
162 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
163/*
164 * Abstraction for a grid of allowed signature-hash-algorithm pairs.
165 */
166struct mbedtls_ssl_sig_hash_set_t
167{
168 /* At the moment, we only need to remember a single suitable
169 * hash algorithm per signature algorithm. As long as that's
170 * the case - and we don't need a general lookup function -
171 * we can implement the sig-hash-set as a map from signatures
172 * to hash algorithms. */
173 mbedtls_md_type_t rsa;
174 mbedtls_md_type_t ecdsa;
175};
176#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
177 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
178
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200179/*
180 * This structure contains the parameters only needed during handshake.
181 */
182struct mbedtls_ssl_handshake_params
183{
184 /*
185 * Handshake specific crypto variables
186 */
Hanno Becker7e5437a2017-04-28 17:15:26 +0100187
188#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
189 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
190 mbedtls_ssl_sig_hash_set_t hash_algs; /*!< Set of suitable sig-hash pairs */
191#endif
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200192#if defined(MBEDTLS_DHM_C)
193 mbedtls_dhm_context dhm_ctx; /*!< DHM key exchange */
194#endif
195#if defined(MBEDTLS_ECDH_C)
196 mbedtls_ecdh_context ecdh_ctx; /*!< ECDH key exchange */
197#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200198#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200199 mbedtls_ecjpake_context ecjpake_ctx; /*!< EC J-PAKE key exchange */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +0200200#if defined(MBEDTLS_SSL_CLI_C)
201 unsigned char *ecjpake_cache; /*!< Cache for ClientHello ext */
202 size_t ecjpake_cache_len; /*!< Length of cached data */
203#endif
Hanno Becker1aa267c2017-04-28 17:08:27 +0100204#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnardf4721792015-09-15 10:53:51 +0200205#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200206 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200207 const mbedtls_ecp_curve_info **curves; /*!< Supported elliptic curves */
208#endif
209#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
210 unsigned char *psk; /*!< PSK from the callback */
211 size_t psk_len; /*!< Length of PSK from callback */
212#endif
213#if defined(MBEDTLS_X509_CRT_PARSE_C)
214 mbedtls_ssl_key_cert *key_cert; /*!< chosen key/cert pair (server) */
215#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200216 int sni_authmode; /*!< authmode from SNI callback */
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200217 mbedtls_ssl_key_cert *sni_key_cert; /*!< key/cert list from SNI */
218 mbedtls_x509_crt *sni_ca_chain; /*!< trusted CAs from SNI callback */
219 mbedtls_x509_crl *sni_ca_crl; /*!< trusted CAs CRLs from SNI */
Hanno Becker1aa267c2017-04-28 17:08:27 +0100220#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200221#endif /* MBEDTLS_X509_CRT_PARSE_C */
222#if defined(MBEDTLS_SSL_PROTO_DTLS)
223 unsigned int out_msg_seq; /*!< Outgoing handshake sequence number */
224 unsigned int in_msg_seq; /*!< Incoming handshake sequence number */
225
226 unsigned char *verify_cookie; /*!< Cli: HelloVerifyRequest cookie
227 Srv: unused */
228 unsigned char verify_cookie_len; /*!< Cli: cookie length
229 Srv: flag for sending a cookie */
230
231 unsigned char *hs_msg; /*!< Reassembled handshake message */
232
233 uint32_t retransmit_timeout; /*!< Current value of timeout */
234 unsigned char retransmit_state; /*!< Retransmission state */
235 mbedtls_ssl_flight_item *flight; /*!< Current outgoing flight */
236 mbedtls_ssl_flight_item *cur_msg; /*!< Current message in flight */
237 unsigned int in_flight_start_seq; /*!< Minimum message sequence in the
238 flight being received */
239 mbedtls_ssl_transform *alt_transform_out; /*!< Alternative transform for
240 resending messages */
241 unsigned char alt_out_ctr[8]; /*!< Alternative record epoch/counter
242 for resending messages */
Hanno Becker1aa267c2017-04-28 17:08:27 +0100243#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200244
245 /*
246 * Checksum contexts
247 */
248#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
249 defined(MBEDTLS_SSL_PROTO_TLS1_1)
250 mbedtls_md5_context fin_md5;
251 mbedtls_sha1_context fin_sha1;
252#endif
253#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
254#if defined(MBEDTLS_SHA256_C)
255 mbedtls_sha256_context fin_sha256;
256#endif
257#if defined(MBEDTLS_SHA512_C)
258 mbedtls_sha512_context fin_sha512;
259#endif
260#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
261
262 void (*update_checksum)(mbedtls_ssl_context *, const unsigned char *, size_t);
263 void (*calc_verify)(mbedtls_ssl_context *, unsigned char *);
264 void (*calc_finished)(mbedtls_ssl_context *, unsigned char *, int);
265 int (*tls_prf)(const unsigned char *, size_t, const char *,
266 const unsigned char *, size_t,
267 unsigned char *, size_t);
268
269 size_t pmslen; /*!< premaster length */
270
271 unsigned char randbytes[64]; /*!< random bytes */
272 unsigned char premaster[MBEDTLS_PREMASTER_SIZE];
273 /*!< premaster secret */
274
275 int resume; /*!< session resume indicator*/
276 int max_major_ver; /*!< max. major version client*/
277 int max_minor_ver; /*!< max. minor version client*/
278 int cli_exts; /*!< client extension presence*/
279
280#if defined(MBEDTLS_SSL_SESSION_TICKETS)
281 int new_session_ticket; /*!< use NewSessionTicket? */
282#endif /* MBEDTLS_SSL_SESSION_TICKETS */
283#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
284 int extended_ms; /*!< use Extended Master Secret? */
285#endif
286};
287
288/*
289 * This structure contains a full set of runtime transform parameters
290 * either in negotiation or active.
291 */
292struct mbedtls_ssl_transform
293{
294 /*
295 * Session specific crypto layer
296 */
297 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
298 /*!< Chosen cipersuite_info */
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200299 unsigned int keylen; /*!< symmetric key length (bytes) */
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200300 size_t minlen; /*!< min. ciphertext length */
301 size_t ivlen; /*!< IV length */
302 size_t fixed_ivlen; /*!< Fixed part of IV (AEAD) */
303 size_t maclen; /*!< MAC length */
304
305 unsigned char iv_enc[16]; /*!< IV (encryption) */
306 unsigned char iv_dec[16]; /*!< IV (decryption) */
307
308#if defined(MBEDTLS_SSL_PROTO_SSL3)
309 /* Needed only for SSL v3.0 secret */
310 unsigned char mac_enc[20]; /*!< SSL v3.0 secret (enc) */
311 unsigned char mac_dec[20]; /*!< SSL v3.0 secret (dec) */
312#endif /* MBEDTLS_SSL_PROTO_SSL3 */
313
314 mbedtls_md_context_t md_ctx_enc; /*!< MAC (encryption) */
315 mbedtls_md_context_t md_ctx_dec; /*!< MAC (decryption) */
316
317 mbedtls_cipher_context_t cipher_ctx_enc; /*!< encryption context */
318 mbedtls_cipher_context_t cipher_ctx_dec; /*!< decryption context */
319
320 /*
321 * Session specific compression layer
322 */
323#if defined(MBEDTLS_ZLIB_SUPPORT)
324 z_stream ctx_deflate; /*!< compression context */
325 z_stream ctx_inflate; /*!< decompression context */
326#endif
327};
328
329#if defined(MBEDTLS_X509_CRT_PARSE_C)
330/*
331 * List of certificate + private key pairs
332 */
333struct mbedtls_ssl_key_cert
334{
335 mbedtls_x509_crt *cert; /*!< cert */
336 mbedtls_pk_context *key; /*!< private key */
337 mbedtls_ssl_key_cert *next; /*!< next key/cert pair */
338};
339#endif /* MBEDTLS_X509_CRT_PARSE_C */
340
341#if defined(MBEDTLS_SSL_PROTO_DTLS)
342/*
343 * List of handshake messages kept around for resending
344 */
345struct mbedtls_ssl_flight_item
346{
347 unsigned char *p; /*!< message, including handshake headers */
348 size_t len; /*!< length of p */
349 unsigned char type; /*!< type of the message: handshake or CCS */
350 mbedtls_ssl_flight_item *next; /*!< next handshake message(s) */
351};
352#endif /* MBEDTLS_SSL_PROTO_DTLS */
353
Hanno Becker7e5437a2017-04-28 17:15:26 +0100354#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
355 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
356
357/* Find an entry in a signature-hash set matching a given hash algorithm. */
358mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
359 mbedtls_pk_type_t sig_alg );
360/* Add a signature-hash-pair to a signature-hash set */
361void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
362 mbedtls_pk_type_t sig_alg,
363 mbedtls_md_type_t md_alg );
364/* Allow exactly one hash algorithm for each signature. */
365void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
366 mbedtls_md_type_t md_alg );
367
368/* Setup an empty signature-hash set */
369static inline void mbedtls_ssl_sig_hash_set_init( mbedtls_ssl_sig_hash_set_t *set )
370{
371 mbedtls_ssl_sig_hash_set_const_hash( set, MBEDTLS_MD_NONE );
372}
373
374#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
375 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200376
377/**
378 * \brief Free referenced items in an SSL transform context and clear
379 * memory
380 *
381 * \param transform SSL transform context
382 */
383void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform );
384
385/**
386 * \brief Free referenced items in an SSL handshake context and clear
387 * memory
388 *
389 * \param handshake SSL handshake context
390 */
391void mbedtls_ssl_handshake_free( mbedtls_ssl_handshake_params *handshake );
392
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +0200393int mbedtls_ssl_handshake_client_step( mbedtls_ssl_context *ssl );
394int mbedtls_ssl_handshake_server_step( mbedtls_ssl_context *ssl );
395void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl );
396
397int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl );
398
399void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl );
400int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl );
401
Simon Butcher99000142016-10-13 17:21:01 +0100402int mbedtls_ssl_read_record_layer( mbedtls_ssl_context *ssl );
403int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl );
404int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl );
405void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl );
406
Hanno Becker4a810fb2017-05-24 16:27:30 +0100407/**
408 * \brief Update record layer
409 *
410 * This function roughly separates the implementation
411 * of the logic of (D)TLS from the implementation
412 * of the secure transport.
413 *
414 * \param ssl SSL context to use
415 *
416 * \return 0 or non-zero error code.
417 *
418 * \note A clarification on what is called 'record layer' here
419 * is in order, as many sensible definitions are possible:
420 *
421 * The record layer takes as input an untrusted underlying
422 * transport (stream or datagram) and transforms it into
423 * a serially multiplexed, secure transport, which
424 * conceptually provides the following:
425 *
426 * (1) Three datagram based, content-agnostic transports
427 * for handshake, alert and CCS messages.
428 * (2) One stream- or datagram-based transport
429 * for application data.
430 * (3) Functionality for changing the underlying transform
431 * securing the contents.
432 *
433 * The interface to this functionality is given as follows:
434 *
435 * a Updating
436 * [Currently implemented by mbedtls_ssl_read_record]
437 *
438 * Check if and on which of the four 'ports' data is pending:
439 * Nothing, a controlling datagram of type (1), or application
440 * data (2). In any case data is present, internal buffers
441 * provide access to the data for the user to process it.
442 * Consumption of type (1) datagrams is done automatically
443 * on the next update, invalidating that the internal buffers
444 * for previous datagrams, while consumption of application
445 * data (2) is user-controlled.
446 *
447 * b Reading of application data
448 * [Currently manual adaption of ssl->in_offt pointer]
449 *
450 * As mentioned in the last paragraph, consumption of data
451 * is different from the automatic consumption of control
452 * datagrams (1) because application data is treated as a stream.
453 *
454 * c Tracking availability of application data
455 * [Currently manually through decreasing ssl->in_msglen]
456 *
457 * For efficiency and to retain datagram semantics for
458 * application data in case of DTLS, the record layer
459 * provides functionality for checking how much application
460 * data is still available in the internal buffer.
461 *
462 * d Changing the transformation securing the communication.
463 *
464 * Given an opaque implementation of the record layer in the
465 * above sense, it should be possible to implement the logic
466 * of (D)TLS on top of it without the need to know anything
467 * about the record layer's internals. This is done e.g.
468 * in all the handshake handling functions, and in the
469 * application data reading function mbedtls_ssl_read.
470 *
471 * \note The above tries to give a conceptual picture of the
472 * record layer, but the current implementation deviates
473 * from it in some places. For example, our implementation of
474 * the update functionality through mbedtls_ssl_read_record
475 * discards datagrams depending on the current state, which
476 * wouldn't fall under the record layer's responsibility
477 * following the above definition.
478 *
479 */
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +0200480int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl );
481int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want );
482
483int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl );
484int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl );
485
486int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl );
487int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl );
488
489int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl );
490int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl );
491
492int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl );
493int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl );
494
495void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
496 const mbedtls_ssl_ciphersuite_t *ciphersuite_info );
497
498#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
499int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex );
500#endif
501
502#if defined(MBEDTLS_PK_C)
503unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk );
Hanno Becker7e5437a2017-04-28 17:15:26 +0100504unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type );
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +0200505mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig );
506#endif
507
508mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash );
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +0200509unsigned char mbedtls_ssl_hash_from_md_alg( int md );
Simon Butcher99000142016-10-13 17:21:01 +0100510int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md );
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +0200511
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +0200512#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +0200513int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id );
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +0200514#endif
515
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +0200516#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +0200517int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
518 mbedtls_md_type_t md );
519#endif
520
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +0200521#if defined(MBEDTLS_X509_CRT_PARSE_C)
522static inline mbedtls_pk_context *mbedtls_ssl_own_key( mbedtls_ssl_context *ssl )
523{
524 mbedtls_ssl_key_cert *key_cert;
525
526 if( ssl->handshake != NULL && ssl->handshake->key_cert != NULL )
527 key_cert = ssl->handshake->key_cert;
528 else
529 key_cert = ssl->conf->key_cert;
530
531 return( key_cert == NULL ? NULL : key_cert->key );
532}
533
534static inline mbedtls_x509_crt *mbedtls_ssl_own_cert( mbedtls_ssl_context *ssl )
535{
536 mbedtls_ssl_key_cert *key_cert;
537
538 if( ssl->handshake != NULL && ssl->handshake->key_cert != NULL )
539 key_cert = ssl->handshake->key_cert;
540 else
541 key_cert = ssl->conf->key_cert;
542
543 return( key_cert == NULL ? NULL : key_cert->cert );
544}
545
546/*
547 * Check usage of a certificate wrt extensions:
548 * keyUsage, extendedKeyUsage (later), and nSCertType (later).
549 *
550 * Warning: cert_endpoint is the endpoint of the cert (ie, of our peer when we
551 * check a cert we received from them)!
552 *
553 * Return 0 if everything is OK, -1 if not.
554 */
555int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
556 const mbedtls_ssl_ciphersuite_t *ciphersuite,
557 int cert_endpoint,
558 uint32_t *flags );
559#endif /* MBEDTLS_X509_CRT_PARSE_C */
560
561void mbedtls_ssl_write_version( int major, int minor, int transport,
562 unsigned char ver[2] );
563void mbedtls_ssl_read_version( int *major, int *minor, int transport,
564 const unsigned char ver[2] );
565
566static inline size_t mbedtls_ssl_hdr_len( const mbedtls_ssl_context *ssl )
567{
568#if defined(MBEDTLS_SSL_PROTO_DTLS)
569 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
570 return( 13 );
571#else
572 ((void) ssl);
573#endif
574 return( 5 );
575}
576
577static inline size_t mbedtls_ssl_hs_hdr_len( const mbedtls_ssl_context *ssl )
578{
579#if defined(MBEDTLS_SSL_PROTO_DTLS)
580 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
581 return( 12 );
582#else
583 ((void) ssl);
584#endif
585 return( 4 );
586}
587
588#if defined(MBEDTLS_SSL_PROTO_DTLS)
589void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl );
590void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl );
591int mbedtls_ssl_resend( mbedtls_ssl_context *ssl );
592#endif
593
594/* Visible for testing purposes only */
595#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
596int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl );
597void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl );
598#endif
599
600/* constant-time buffer comparison */
601static inline int mbedtls_ssl_safer_memcmp( const void *a, const void *b, size_t n )
602{
603 size_t i;
604 const unsigned char *A = (const unsigned char *) a;
605 const unsigned char *B = (const unsigned char *) b;
606 unsigned char diff = 0;
607
608 for( i = 0; i < n; i++ )
609 diff |= A[i] ^ B[i];
610
611 return( diff );
612}
613
614#ifdef __cplusplus
615}
616#endif
617
618#endif /* ssl_internal.h */