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