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