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