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