mbed TLS v2.1.0
ssl_internal.h
Go to the documentation of this file.
1 
23 #ifndef MBEDTLS_SSL_INTERNAL_H
24 #define MBEDTLS_SSL_INTERNAL_H
25 
26 #include "ssl.h"
27 
28 #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 
44 #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && !defined(inline)
45 #define inline __inline
46 #endif
47 
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 
150 #ifdef __cplusplus
151 extern "C" {
152 #endif
153 
154 /*
155  * This structure contains the parameters only needed during handshake.
156  */
158 {
159  /*
160  * Handshake specific crypto variables
161  */
162  int sig_alg;
163  int cert_type;
165 #if defined(MBEDTLS_DHM_C)
167 #endif
168 #if defined(MBEDTLS_ECDH_C)
170 #endif
171 #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
173 #endif
174 #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
175  unsigned char *psk;
176  size_t psk_len;
177 #endif
178 #if defined(MBEDTLS_X509_CRT_PARSE_C)
180 #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
185 #endif
186 #endif /* MBEDTLS_X509_CRT_PARSE_C */
187 #if defined(MBEDTLS_SSL_PROTO_DTLS)
188  unsigned int out_msg_seq;
189  unsigned int in_msg_seq;
191  unsigned char *verify_cookie;
193  unsigned char verify_cookie_len;
196  unsigned char *hs_msg;
199  unsigned char retransmit_state;
202  unsigned int in_flight_start_seq;
206  unsigned char alt_out_ctr[8];
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)
217 #endif
218 #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
219 #if defined(MBEDTLS_SHA256_C)
221 #endif
222 #if defined(MBEDTLS_SHA512_C)
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;
236  unsigned char randbytes[64];
240  int resume;
243  int cli_exts;
245 #if defined(MBEDTLS_SSL_SESSION_TICKETS)
247 #endif /* MBEDTLS_SSL_SESSION_TICKETS */
248 #if defined(MBEDTLS_SSL_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  */
258 {
259  /*
260  * Session specific crypto layer
261  */
264  unsigned int keylen;
265  size_t minlen;
266  size_t ivlen;
267  size_t fixed_ivlen;
268  size_t maclen;
270  unsigned char iv_enc[16];
271  unsigned char iv_dec[16];
273 #if defined(MBEDTLS_SSL_PROTO_SSL3)
274  /* Needed only for SSL v3.0 secret */
275  unsigned char mac_enc[20];
276  unsigned char mac_dec[20];
277 #endif /* MBEDTLS_SSL_PROTO_SSL3 */
278 
285  /*
286  * Session specific compression layer
287  */
288 #if defined(MBEDTLS_ZLIB_SUPPORT)
289  z_stream ctx_deflate;
290  z_stream ctx_inflate;
291 #endif
292 };
293 
294 #if defined(MBEDTLS_X509_CRT_PARSE_C)
295 /*
296  * List of certificate + private key pairs
297  */
299 {
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  */
311 {
312  unsigned char *p;
313  size_t len;
314  unsigned char type;
316 };
317 #endif /* MBEDTLS_SSL_PROTO_DTLS */
318 
319 
327 
335 
339 
341 
344 
346 int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want );
347 
350 
353 
356 
359 
361  const mbedtls_ssl_ciphersuite_t *ciphersuite_info );
362 
363 #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
365 #endif
366 
367 #if defined(MBEDTLS_PK_C)
368 unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk );
370 #endif
371 
372 mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash );
373 unsigned char mbedtls_ssl_hash_from_md_alg( int md );
374 
375 #if defined(MBEDTLS_ECP_C)
377 #endif
378 
379 #if defined(MBEDTLS_KEY_EXCHANGE__SOME__SIGNATURE_ENABLED)
382 #endif
383 
384 #if defined(MBEDTLS_X509_CRT_PARSE_C)
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 
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  */
419  const mbedtls_ssl_ciphersuite_t *ciphersuite,
420  int cert_endpoint,
421  uint32_t *flags );
422 #endif /* MBEDTLS_X509_CRT_PARSE_C */
423 
424 void mbedtls_ssl_write_version( int major, int minor, int transport,
425  unsigned char ver[2] );
426 void mbedtls_ssl_read_version( int *major, int *minor, int transport,
427  const unsigned char ver[2] );
428 
429 static inline size_t mbedtls_ssl_hdr_len( const mbedtls_ssl_context *ssl )
430 {
431 #if defined(MBEDTLS_SSL_PROTO_DTLS)
433  return( 13 );
434 #else
435  ((void) ssl);
436 #endif
437  return( 5 );
438 }
439 
440 static inline size_t mbedtls_ssl_hs_hdr_len( const mbedtls_ssl_context *ssl )
441 {
442 #if defined(MBEDTLS_SSL_PROTO_DTLS)
444  return( 12 );
445 #else
446  ((void) ssl);
447 #endif
448  return( 4 );
449 }
450 
451 #if defined(MBEDTLS_SSL_PROTO_DTLS)
455 #endif
456 
457 /* Visible for testing purposes only */
458 #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
461 #endif
462 
463 /* constant-time buffer comparison */
464 static 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 */
void mbedtls_ssl_send_flight_completed(mbedtls_ssl_context *ssl)
unsigned int transport
Definition: ssl.h:610
int mbedtls_ssl_parse_finished(mbedtls_ssl_context *ssl)
unsigned char mbedtls_ssl_hash_from_md_alg(int md)
Public key container.
Definition: pk.h:122
mbedtls_sha1_context fin_sha1
Definition: ssl_internal.h:216
void(* update_checksum)(mbedtls_ssl_context *, const unsigned char *, size_t)
Definition: ssl_internal.h:227
void mbedtls_ssl_read_version(int *major, int *minor, int transport, const unsigned char ver[2])
mbedtls_ssl_key_cert * key_cert
Definition: ssl_internal.h:179
int mbedtls_ssl_handshake_server_step(mbedtls_ssl_context *ssl)
unsigned char alt_out_ctr[8]
Definition: ssl_internal.h:206
unsigned char randbytes[64]
Definition: ssl_internal.h:236
int mbedtls_ssl_write_change_cipher_spec(mbedtls_ssl_context *ssl)
mbedtls_ssl_flight_item * cur_msg
Definition: ssl_internal.h:201
unsigned char mac_enc[20]
Definition: ssl_internal.h:275
mbedtls_sha256_context fin_sha256
Definition: ssl_internal.h:220
int mbedtls_ssl_write_finished(mbedtls_ssl_context *ssl)
Certificate revocation list structure.
Definition: x509_crl.h:69
mbedtls_ecdh_context ecdh_ctx
Definition: ssl_internal.h:169
Generic cipher context.
Definition: cipher.h:212
static int mbedtls_ssl_safer_memcmp(const void *a, const void *b, size_t n)
Definition: ssl_internal.h:464
mbedtls_sha512_context fin_sha512
Definition: ssl_internal.h:223
mbedtls_pk_type_t
Public key types.
Definition: pk.h:70
int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
mbedtls_cipher_context_t cipher_ctx_enc
Definition: ssl_internal.h:282
static size_t mbedtls_ssl_hs_hdr_len(const mbedtls_ssl_context *ssl)
Definition: ssl_internal.h:440
#define MBEDTLS_SSL_TRANSPORT_DATAGRAM
Definition: ssl.h:139
Curve information for use by other modules.
Definition: ecp.h:80
int mbedtls_ssl_resend(mbedtls_ssl_context *ssl)
unsigned char mbedtls_ssl_sig_from_pk(mbedtls_pk_context *pk)
int mbedtls_ssl_derive_keys(mbedtls_ssl_context *ssl)
Generic message digest context.
Definition: md.h:70
mbedtls_x509_crt * sni_ca_chain
Definition: ssl_internal.h:183
mbedtls_md_context_t md_ctx_dec
Definition: ssl_internal.h:280
void mbedtls_ssl_handshake_free(mbedtls_ssl_handshake_params *handshake)
Free referenced items in an SSL handshake context and clear memory.
const mbedtls_ssl_ciphersuite_t * ciphersuite_info
Definition: ssl_internal.h:262
mbedtls_md5_context fin_md5
Definition: ssl_internal.h:215
unsigned char iv_dec[16]
Definition: ssl_internal.h:271
int mbedtls_ssl_fetch_input(mbedtls_ssl_context *ssl, size_t nb_want)
mbedtls_ssl_handshake_params * handshake
Definition: ssl.h:685
int mbedtls_ssl_dtls_replay_check(mbedtls_ssl_context *ssl)
const mbedtls_ecp_curve_info ** curves
Definition: ssl_internal.h:172
mbedtls_ssl_transform * alt_transform_out
Definition: ssl_internal.h:204
int mbedtls_ssl_read_record(mbedtls_ssl_context *ssl)
unsigned char iv_enc[16]
Definition: ssl_internal.h:270
mbedtls_ssl_flight_item * next
Definition: ssl_internal.h:315
mbedtls_ssl_key_cert * key_cert
Definition: ssl.h:544
void mbedtls_ssl_transform_free(mbedtls_ssl_transform *transform)
Free referenced items in an SSL transform context and clear memory.
SHA-512 context structure.
Definition: sha512.h:46
int mbedtls_ssl_check_cert_usage(const mbedtls_x509_crt *cert, const mbedtls_ssl_ciphersuite_t *ciphersuite, int cert_endpoint, uint32_t *flags)
unsigned char mac_dec[20]
Definition: ssl_internal.h:276
int mbedtls_ssl_psk_derive_premaster(mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex)
int mbedtls_ssl_check_curve(const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id)
mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash(unsigned char hash)
void(* calc_verify)(mbedtls_ssl_context *, unsigned char *)
Definition: ssl_internal.h:228
int mbedtls_ssl_send_fatal_handshake_failure(mbedtls_ssl_context *ssl)
mbedtls_key_exchange_type_t
unsigned char * verify_cookie
Definition: ssl_internal.h:191
static mbedtls_x509_crt * mbedtls_ssl_own_cert(mbedtls_ssl_context *ssl)
Definition: ssl_internal.h:397
mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig(unsigned char sig)
mbedtls_ecp_group_id
Domain parameters (curve, subgroup and generator) identifiers.
Definition: ecp.h:53
DHM context structure.
Definition: dhm.h:149
const mbedtls_ssl_config * conf
Definition: ssl.h:649
SHA-256 context structure.
Definition: sha256.h:46
ECDH context structure.
Definition: ecdh.h:44
int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
This structure is used for storing ciphersuite information.
#define MBEDTLS_PREMASTER_SIZE
Definition: ssl.h:392
int mbedtls_ssl_parse_change_cipher_spec(mbedtls_ssl_context *ssl)
void mbedtls_ssl_write_version(int major, int minor, int transport, unsigned char ver[2])
mbedtls_cipher_context_t cipher_ctx_dec
Definition: ssl_internal.h:283
mbedtls_ssl_key_cert * next
Definition: ssl_internal.h:302
void mbedtls_ssl_recv_flight_completed(mbedtls_ssl_context *ssl)
int mbedtls_ssl_flush_output(mbedtls_ssl_context *ssl)
MD5 context structure.
Definition: md5.h:46
void mbedtls_ssl_optimize_checksum(mbedtls_ssl_context *ssl, const mbedtls_ssl_ciphersuite_t *ciphersuite_info)
int mbedtls_ssl_write_record(mbedtls_ssl_context *ssl)
void mbedtls_ssl_reset_checksum(mbedtls_ssl_context *ssl)
Container for an X.509 certificate.
Definition: x509_crt.h:52
SHA-1 context structure.
Definition: sha1.h:46
int mbedtls_ssl_check_sig_hash(const mbedtls_ssl_context *ssl, mbedtls_md_type_t md)
mbedtls_ssl_key_cert * sni_key_cert
Definition: ssl_internal.h:182
SSL/TLS functions.
mbedtls_pk_context * key
Definition: ssl_internal.h:301
int mbedtls_ssl_handshake_client_step(mbedtls_ssl_context *ssl)
unsigned char premaster[MBEDTLS_PREMASTER_SIZE]
Definition: ssl_internal.h:237
void mbedtls_ssl_handshake_wrapup(mbedtls_ssl_context *ssl)
void(* calc_finished)(mbedtls_ssl_context *, unsigned char *, int)
Definition: ssl_internal.h:229
mbedtls_ssl_flight_item * flight
Definition: ssl_internal.h:200
mbedtls_x509_crl * sni_ca_crl
Definition: ssl_internal.h:184
int(* tls_prf)(const unsigned char *, size_t, const char *, const unsigned char *, size_t, unsigned char *, size_t)
Definition: ssl_internal.h:230
void mbedtls_ssl_dtls_replay_update(mbedtls_ssl_context *ssl)
mbedtls_md_type_t
Definition: md.h:43
mbedtls_md_context_t md_ctx_enc
Definition: ssl_internal.h:279
#define md
Definition: compat-1.3.h:2030
mbedtls_x509_crt * cert
Definition: ssl_internal.h:300
mbedtls_dhm_context dhm_ctx
Definition: ssl_internal.h:166
static mbedtls_pk_context * mbedtls_ssl_own_key(mbedtls_ssl_context *ssl)
Definition: ssl_internal.h:385
static size_t mbedtls_ssl_hdr_len(const mbedtls_ssl_context *ssl)
Definition: ssl_internal.h:429