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