blob: 73279a5ae27b6c747cb2dcf5a0d89c09da5a270d [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é-Gonnard20af64d2015-07-07 18:33:39 +020044#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && !defined(inline)
Manuel Pégourié-Gonnard065122c2015-05-26 12:31:46 +020045#define inline __inline
Manuel Pégourié-Gonnard20af64d2015-07-07 18:33:39 +020046#endif
Manuel Pégourié-Gonnard065122c2015-05-26 12:31:46 +020047
48/* Determine minimum supported version */
49#define MBEDTLS_SSL_MIN_MAJOR_VERSION MBEDTLS_SSL_MAJOR_VERSION_3
50
51#if defined(MBEDTLS_SSL_PROTO_SSL3)
52#define MBEDTLS_SSL_MIN_MINOR_VERSION MBEDTLS_SSL_MINOR_VERSION_0
53#else
54#if defined(MBEDTLS_SSL_PROTO_TLS1)
55#define MBEDTLS_SSL_MIN_MINOR_VERSION MBEDTLS_SSL_MINOR_VERSION_1
56#else
57#if defined(MBEDTLS_SSL_PROTO_TLS1_1)
58#define MBEDTLS_SSL_MIN_MINOR_VERSION MBEDTLS_SSL_MINOR_VERSION_2
59#else
60#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
61#define MBEDTLS_SSL_MIN_MINOR_VERSION MBEDTLS_SSL_MINOR_VERSION_3
62#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
63#endif /* MBEDTLS_SSL_PROTO_TLS1_1 */
64#endif /* MBEDTLS_SSL_PROTO_TLS1 */
65#endif /* MBEDTLS_SSL_PROTO_SSL3 */
66
67/* Determine maximum supported version */
68#define MBEDTLS_SSL_MAX_MAJOR_VERSION MBEDTLS_SSL_MAJOR_VERSION_3
69
70#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
71#define MBEDTLS_SSL_MAX_MINOR_VERSION MBEDTLS_SSL_MINOR_VERSION_3
72#else
73#if defined(MBEDTLS_SSL_PROTO_TLS1_1)
74#define MBEDTLS_SSL_MAX_MINOR_VERSION MBEDTLS_SSL_MINOR_VERSION_2
75#else
76#if defined(MBEDTLS_SSL_PROTO_TLS1)
77#define MBEDTLS_SSL_MAX_MINOR_VERSION MBEDTLS_SSL_MINOR_VERSION_1
78#else
79#if defined(MBEDTLS_SSL_PROTO_SSL3)
80#define MBEDTLS_SSL_MAX_MINOR_VERSION MBEDTLS_SSL_MINOR_VERSION_0
81#endif /* MBEDTLS_SSL_PROTO_SSL3 */
82#endif /* MBEDTLS_SSL_PROTO_TLS1 */
83#endif /* MBEDTLS_SSL_PROTO_TLS1_1 */
84#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
85
86#define MBEDTLS_SSL_INITIAL_HANDSHAKE 0
87#define MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS 1 /* In progress */
88#define MBEDTLS_SSL_RENEGOTIATION_DONE 2 /* Done or aborted */
89#define MBEDTLS_SSL_RENEGOTIATION_PENDING 3 /* Requested (server only) */
90
91/*
92 * DTLS retransmission states, see RFC 6347 4.2.4
93 *
94 * The SENDING state is merged in PREPARING for initial sends,
95 * but is distinct for resends.
96 *
97 * Note: initial state is wrong for server, but is not used anyway.
98 */
99#define MBEDTLS_SSL_RETRANS_PREPARING 0
100#define MBEDTLS_SSL_RETRANS_SENDING 1
101#define MBEDTLS_SSL_RETRANS_WAITING 2
102#define MBEDTLS_SSL_RETRANS_FINISHED 3
103
104/*
105 * Allow extra bytes for record, authentication and encryption overhead:
106 * counter (8) + header (5) + IV(16) + MAC (16-48) + padding (0-256)
107 * and allow for a maximum of 1024 of compression expansion if
108 * enabled.
109 */
110#if defined(MBEDTLS_ZLIB_SUPPORT)
111#define MBEDTLS_SSL_COMPRESSION_ADD 1024
112#else
113#define MBEDTLS_SSL_COMPRESSION_ADD 0
114#endif
115
116#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_MODE_CBC)
117/* Ciphersuites using HMAC */
118#if defined(MBEDTLS_SHA512_C)
119#define MBEDTLS_SSL_MAC_ADD 48 /* SHA-384 used for HMAC */
120#elif defined(MBEDTLS_SHA256_C)
121#define MBEDTLS_SSL_MAC_ADD 32 /* SHA-256 used for HMAC */
122#else
123#define MBEDTLS_SSL_MAC_ADD 20 /* SHA-1 used for HMAC */
124#endif
125#else
126/* AEAD ciphersuites: GCM and CCM use a 128 bits tag */
127#define MBEDTLS_SSL_MAC_ADD 16
128#endif
129
130#if defined(MBEDTLS_CIPHER_MODE_CBC)
131#define MBEDTLS_SSL_PADDING_ADD 256
132#else
133#define MBEDTLS_SSL_PADDING_ADD 0
134#endif
135
136#define MBEDTLS_SSL_BUFFER_LEN ( MBEDTLS_SSL_MAX_CONTENT_LEN \
137 + MBEDTLS_SSL_COMPRESSION_ADD \
138 + 29 /* counter + header + IV */ \
139 + MBEDTLS_SSL_MAC_ADD \
140 + MBEDTLS_SSL_PADDING_ADD \
141 )
142
143/*
144 * TLS extension flags (for extensions with outgoing ServerHello content
145 * that need it (e.g. for RENEGOTIATION_INFO the server already knows because
146 * of state of the renegotiation flag, so no indicator is required)
147 */
148#define MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT (1 << 0)
149
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +0200150#ifdef __cplusplus
151extern "C" {
152#endif
153
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200154/*
155 * This structure contains the parameters only needed during handshake.
156 */
157struct mbedtls_ssl_handshake_params
158{
159 /*
160 * Handshake specific crypto variables
161 */
162 int sig_alg; /*!< Hash algorithm for signature */
163 int cert_type; /*!< Requested cert type */
164 int verify_sig_alg; /*!< Signature algorithm for verify */
165#if defined(MBEDTLS_DHM_C)
166 mbedtls_dhm_context dhm_ctx; /*!< DHM key exchange */
167#endif
168#if defined(MBEDTLS_ECDH_C)
169 mbedtls_ecdh_context ecdh_ctx; /*!< ECDH key exchange */
170#endif
171#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
172 const mbedtls_ecp_curve_info **curves; /*!< Supported elliptic curves */
173#endif
174#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
175 unsigned char *psk; /*!< PSK from the callback */
176 size_t psk_len; /*!< Length of PSK from callback */
177#endif
178#if defined(MBEDTLS_X509_CRT_PARSE_C)
179 mbedtls_ssl_key_cert *key_cert; /*!< chosen key/cert pair (server) */
180#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200181 int sni_authmode; /*!< authmode from SNI callback */
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200182 mbedtls_ssl_key_cert *sni_key_cert; /*!< key/cert list from SNI */
183 mbedtls_x509_crt *sni_ca_chain; /*!< trusted CAs from SNI callback */
184 mbedtls_x509_crl *sni_ca_crl; /*!< trusted CAs CRLs from SNI */
185#endif
186#endif /* MBEDTLS_X509_CRT_PARSE_C */
187#if defined(MBEDTLS_SSL_PROTO_DTLS)
188 unsigned int out_msg_seq; /*!< Outgoing handshake sequence number */
189 unsigned int in_msg_seq; /*!< Incoming handshake sequence number */
190
191 unsigned char *verify_cookie; /*!< Cli: HelloVerifyRequest cookie
192 Srv: unused */
193 unsigned char verify_cookie_len; /*!< Cli: cookie length
194 Srv: flag for sending a cookie */
195
196 unsigned char *hs_msg; /*!< Reassembled handshake message */
197
198 uint32_t retransmit_timeout; /*!< Current value of timeout */
199 unsigned char retransmit_state; /*!< Retransmission state */
200 mbedtls_ssl_flight_item *flight; /*!< Current outgoing flight */
201 mbedtls_ssl_flight_item *cur_msg; /*!< Current message in flight */
202 unsigned int in_flight_start_seq; /*!< Minimum message sequence in the
203 flight being received */
204 mbedtls_ssl_transform *alt_transform_out; /*!< Alternative transform for
205 resending messages */
206 unsigned char alt_out_ctr[8]; /*!< Alternative record epoch/counter
207 for resending messages */
208#endif
209
210 /*
211 * Checksum contexts
212 */
213#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
214 defined(MBEDTLS_SSL_PROTO_TLS1_1)
215 mbedtls_md5_context fin_md5;
216 mbedtls_sha1_context fin_sha1;
217#endif
218#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
219#if defined(MBEDTLS_SHA256_C)
220 mbedtls_sha256_context fin_sha256;
221#endif
222#if defined(MBEDTLS_SHA512_C)
223 mbedtls_sha512_context fin_sha512;
224#endif
225#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
226
227 void (*update_checksum)(mbedtls_ssl_context *, const unsigned char *, size_t);
228 void (*calc_verify)(mbedtls_ssl_context *, unsigned char *);
229 void (*calc_finished)(mbedtls_ssl_context *, unsigned char *, int);
230 int (*tls_prf)(const unsigned char *, size_t, const char *,
231 const unsigned char *, size_t,
232 unsigned char *, size_t);
233
234 size_t pmslen; /*!< premaster length */
235
236 unsigned char randbytes[64]; /*!< random bytes */
237 unsigned char premaster[MBEDTLS_PREMASTER_SIZE];
238 /*!< premaster secret */
239
240 int resume; /*!< session resume indicator*/
241 int max_major_ver; /*!< max. major version client*/
242 int max_minor_ver; /*!< max. minor version client*/
243 int cli_exts; /*!< client extension presence*/
244
245#if defined(MBEDTLS_SSL_SESSION_TICKETS)
246 int new_session_ticket; /*!< use NewSessionTicket? */
247#endif /* MBEDTLS_SSL_SESSION_TICKETS */
248#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
249 int extended_ms; /*!< use Extended Master Secret? */
250#endif
251};
252
253/*
254 * This structure contains a full set of runtime transform parameters
255 * either in negotiation or active.
256 */
257struct mbedtls_ssl_transform
258{
259 /*
260 * Session specific crypto layer
261 */
262 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
263 /*!< Chosen cipersuite_info */
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200264 unsigned int keylen; /*!< symmetric key length (bytes) */
Manuel Pégourié-Gonnardcd4fcc62015-05-26 12:11:48 +0200265 size_t minlen; /*!< min. ciphertext length */
266 size_t ivlen; /*!< IV length */
267 size_t fixed_ivlen; /*!< Fixed part of IV (AEAD) */
268 size_t maclen; /*!< MAC length */
269
270 unsigned char iv_enc[16]; /*!< IV (encryption) */
271 unsigned char iv_dec[16]; /*!< IV (decryption) */
272
273#if defined(MBEDTLS_SSL_PROTO_SSL3)
274 /* Needed only for SSL v3.0 secret */
275 unsigned char mac_enc[20]; /*!< SSL v3.0 secret (enc) */
276 unsigned char mac_dec[20]; /*!< SSL v3.0 secret (dec) */
277#endif /* MBEDTLS_SSL_PROTO_SSL3 */
278
279 mbedtls_md_context_t md_ctx_enc; /*!< MAC (encryption) */
280 mbedtls_md_context_t md_ctx_dec; /*!< MAC (decryption) */
281
282 mbedtls_cipher_context_t cipher_ctx_enc; /*!< encryption context */
283 mbedtls_cipher_context_t cipher_ctx_dec; /*!< decryption context */
284
285 /*
286 * Session specific compression layer
287 */
288#if defined(MBEDTLS_ZLIB_SUPPORT)
289 z_stream ctx_deflate; /*!< compression context */
290 z_stream ctx_inflate; /*!< decompression context */
291#endif
292};
293
294#if defined(MBEDTLS_X509_CRT_PARSE_C)
295/*
296 * List of certificate + private key pairs
297 */
298struct mbedtls_ssl_key_cert
299{
300 mbedtls_x509_crt *cert; /*!< cert */
301 mbedtls_pk_context *key; /*!< private key */
302 mbedtls_ssl_key_cert *next; /*!< next key/cert pair */
303};
304#endif /* MBEDTLS_X509_CRT_PARSE_C */
305
306#if defined(MBEDTLS_SSL_PROTO_DTLS)
307/*
308 * List of handshake messages kept around for resending
309 */
310struct mbedtls_ssl_flight_item
311{
312 unsigned char *p; /*!< message, including handshake headers */
313 size_t len; /*!< length of p */
314 unsigned char type; /*!< type of the message: handshake or CCS */
315 mbedtls_ssl_flight_item *next; /*!< next handshake message(s) */
316};
317#endif /* MBEDTLS_SSL_PROTO_DTLS */
318
319
320/**
321 * \brief Free referenced items in an SSL transform context and clear
322 * memory
323 *
324 * \param transform SSL transform context
325 */
326void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform );
327
328/**
329 * \brief Free referenced items in an SSL handshake context and clear
330 * memory
331 *
332 * \param handshake SSL handshake context
333 */
334void mbedtls_ssl_handshake_free( mbedtls_ssl_handshake_params *handshake );
335
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +0200336int mbedtls_ssl_handshake_client_step( mbedtls_ssl_context *ssl );
337int mbedtls_ssl_handshake_server_step( mbedtls_ssl_context *ssl );
338void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl );
339
340int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl );
341
342void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl );
343int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl );
344
345int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl );
346int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want );
347
348int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl );
349int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl );
350
351int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl );
352int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl );
353
354int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl );
355int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl );
356
357int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl );
358int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl );
359
360void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
361 const mbedtls_ssl_ciphersuite_t *ciphersuite_info );
362
363#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
364int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex );
365#endif
366
367#if defined(MBEDTLS_PK_C)
368unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk );
369mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig );
370#endif
371
372mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash );
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +0200373unsigned char mbedtls_ssl_hash_from_md_alg( int md );
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +0200374
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +0200375#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +0200376int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id );
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +0200377#endif
378
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +0200379#if defined(MBEDTLS_KEY_EXCHANGE__SOME__SIGNATURE_ENABLED)
380int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
381 mbedtls_md_type_t md );
382#endif
383
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +0200384#if defined(MBEDTLS_X509_CRT_PARSE_C)
385static inline mbedtls_pk_context *mbedtls_ssl_own_key( mbedtls_ssl_context *ssl )
386{
387 mbedtls_ssl_key_cert *key_cert;
388
389 if( ssl->handshake != NULL && ssl->handshake->key_cert != NULL )
390 key_cert = ssl->handshake->key_cert;
391 else
392 key_cert = ssl->conf->key_cert;
393
394 return( key_cert == NULL ? NULL : key_cert->key );
395}
396
397static inline mbedtls_x509_crt *mbedtls_ssl_own_cert( mbedtls_ssl_context *ssl )
398{
399 mbedtls_ssl_key_cert *key_cert;
400
401 if( ssl->handshake != NULL && ssl->handshake->key_cert != NULL )
402 key_cert = ssl->handshake->key_cert;
403 else
404 key_cert = ssl->conf->key_cert;
405
406 return( key_cert == NULL ? NULL : key_cert->cert );
407}
408
409/*
410 * Check usage of a certificate wrt extensions:
411 * keyUsage, extendedKeyUsage (later), and nSCertType (later).
412 *
413 * Warning: cert_endpoint is the endpoint of the cert (ie, of our peer when we
414 * check a cert we received from them)!
415 *
416 * Return 0 if everything is OK, -1 if not.
417 */
418int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
419 const mbedtls_ssl_ciphersuite_t *ciphersuite,
420 int cert_endpoint,
421 uint32_t *flags );
422#endif /* MBEDTLS_X509_CRT_PARSE_C */
423
424void mbedtls_ssl_write_version( int major, int minor, int transport,
425 unsigned char ver[2] );
426void mbedtls_ssl_read_version( int *major, int *minor, int transport,
427 const unsigned char ver[2] );
428
429static inline size_t mbedtls_ssl_hdr_len( const mbedtls_ssl_context *ssl )
430{
431#if defined(MBEDTLS_SSL_PROTO_DTLS)
432 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
433 return( 13 );
434#else
435 ((void) ssl);
436#endif
437 return( 5 );
438}
439
440static inline size_t mbedtls_ssl_hs_hdr_len( const mbedtls_ssl_context *ssl )
441{
442#if defined(MBEDTLS_SSL_PROTO_DTLS)
443 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
444 return( 12 );
445#else
446 ((void) ssl);
447#endif
448 return( 4 );
449}
450
451#if defined(MBEDTLS_SSL_PROTO_DTLS)
452void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl );
453void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl );
454int mbedtls_ssl_resend( mbedtls_ssl_context *ssl );
455#endif
456
457/* Visible for testing purposes only */
458#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
459int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl );
460void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl );
461#endif
462
463/* constant-time buffer comparison */
464static inline int mbedtls_ssl_safer_memcmp( const void *a, const void *b, size_t n )
465{
466 size_t i;
467 const unsigned char *A = (const unsigned char *) a;
468 const unsigned char *B = (const unsigned char *) b;
469 unsigned char diff = 0;
470
471 for( i = 0; i < n; i++ )
472 diff |= A[i] ^ B[i];
473
474 return( diff );
475}
476
477#ifdef __cplusplus
478}
479#endif
480
481#endif /* ssl_internal.h */