| Manuel Pégourié-Gonnard | 5e94dde | 2015-05-26 11:57:05 +0200 | [diff] [blame] | 1 | /** | 
|  | 2 | * \file ssl_ticket.h | 
|  | 3 | * | 
|  | 4 | * \brief Internal functions shared by the SSL modules | 
|  | 5 | * | 
| Manuel Pégourié-Gonnard | 6fb8187 | 2015-07-27 11:11:48 +0200 | [diff] [blame] | 6 | *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved | 
| Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 7 | *  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é-Gonnard | 5e94dde | 2015-05-26 11:57:05 +0200 | [diff] [blame] | 20 | * | 
|  | 21 | *  This file is part of mbed TLS (https://tls.mbed.org) | 
| Manuel Pégourié-Gonnard | 5e94dde | 2015-05-26 11:57:05 +0200 | [diff] [blame] | 22 | */ | 
|  | 23 | #ifndef MBEDTLS_SSL_INTERNAL_H | 
|  | 24 | #define MBEDTLS_SSL_INTERNAL_H | 
|  | 25 |  | 
|  | 26 | #include "ssl.h" | 
|  | 27 |  | 
| Manuel Pégourié-Gonnard | 56273da | 2015-05-26 12:19:45 +0200 | [diff] [blame] | 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 |  | 
| Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 44 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
| Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 45 | #include "ecjpake.h" | 
|  | 46 | #endif | 
|  | 47 |  | 
| Manuel Pégourié-Gonnard | 0223ab9 | 2015-10-05 11:40:01 +0100 | [diff] [blame] | 48 | #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \ | 
|  | 49 | !defined(inline) && !defined(__cplusplus) | 
| Manuel Pégourié-Gonnard | 065122c | 2015-05-26 12:31:46 +0200 | [diff] [blame] | 50 | #define inline __inline | 
| Manuel Pégourié-Gonnard | 20af64d | 2015-07-07 18:33:39 +0200 | [diff] [blame] | 51 | #endif | 
| Manuel Pégourié-Gonnard | 065122c | 2015-05-26 12:31:46 +0200 | [diff] [blame] | 52 |  | 
|  | 53 | /* Determine minimum supported version */ | 
|  | 54 | #define MBEDTLS_SSL_MIN_MAJOR_VERSION           MBEDTLS_SSL_MAJOR_VERSION_3 | 
|  | 55 |  | 
|  | 56 | #if defined(MBEDTLS_SSL_PROTO_SSL3) | 
|  | 57 | #define MBEDTLS_SSL_MIN_MINOR_VERSION           MBEDTLS_SSL_MINOR_VERSION_0 | 
|  | 58 | #else | 
|  | 59 | #if defined(MBEDTLS_SSL_PROTO_TLS1) | 
|  | 60 | #define MBEDTLS_SSL_MIN_MINOR_VERSION           MBEDTLS_SSL_MINOR_VERSION_1 | 
|  | 61 | #else | 
|  | 62 | #if defined(MBEDTLS_SSL_PROTO_TLS1_1) | 
|  | 63 | #define MBEDTLS_SSL_MIN_MINOR_VERSION           MBEDTLS_SSL_MINOR_VERSION_2 | 
|  | 64 | #else | 
|  | 65 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 66 | #define MBEDTLS_SSL_MIN_MINOR_VERSION           MBEDTLS_SSL_MINOR_VERSION_3 | 
|  | 67 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
|  | 68 | #endif /* MBEDTLS_SSL_PROTO_TLS1_1 */ | 
|  | 69 | #endif /* MBEDTLS_SSL_PROTO_TLS1   */ | 
|  | 70 | #endif /* MBEDTLS_SSL_PROTO_SSL3   */ | 
|  | 71 |  | 
|  | 72 | /* 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 |  | 
|  | 141 | #define MBEDTLS_SSL_BUFFER_LEN  ( MBEDTLS_SSL_MAX_CONTENT_LEN               \ | 
|  | 142 | + MBEDTLS_SSL_COMPRESSION_ADD               \ | 
|  | 143 | + 29 /* counter + header + IV */    \ | 
|  | 144 | + MBEDTLS_SSL_MAC_ADD                       \ | 
|  | 145 | + MBEDTLS_SSL_PADDING_ADD                   \ | 
|  | 146 | ) | 
|  | 147 |  | 
|  | 148 | /* | 
|  | 149 | * TLS extension flags (for extensions with outgoing ServerHello content | 
|  | 150 | * that need it (e.g. for RENEGOTIATION_INFO the server already knows because | 
|  | 151 | * of state of the renegotiation flag, so no indicator is required) | 
|  | 152 | */ | 
|  | 153 | #define MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT (1 << 0) | 
| Manuel Pégourié-Gonnard | bf57be6 | 2015-09-16 15:04:01 +0200 | [diff] [blame] | 154 | #define MBEDTLS_TLS_EXT_ECJPAKE_KKPP_OK                 (1 << 1) | 
| Manuel Pégourié-Gonnard | 065122c | 2015-05-26 12:31:46 +0200 | [diff] [blame] | 155 |  | 
| Manuel Pégourié-Gonnard | 5e94dde | 2015-05-26 11:57:05 +0200 | [diff] [blame] | 156 | #ifdef __cplusplus | 
|  | 157 | extern "C" { | 
|  | 158 | #endif | 
|  | 159 |  | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 160 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ | 
|  | 161 | defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) | 
|  | 162 | /* | 
|  | 163 | * Abstraction for a grid of allowed signature-hash-algorithm pairs. | 
|  | 164 | */ | 
|  | 165 | struct mbedtls_ssl_sig_hash_set_t | 
|  | 166 | { | 
|  | 167 | /* At the moment, we only need to remember a single suitable | 
|  | 168 | * hash algorithm per signature algorithm. As long as that's | 
|  | 169 | * the case - and we don't need a general lookup function - | 
|  | 170 | * we can implement the sig-hash-set as a map from signatures | 
|  | 171 | * to hash algorithms. */ | 
|  | 172 | mbedtls_md_type_t rsa; | 
|  | 173 | mbedtls_md_type_t ecdsa; | 
|  | 174 | }; | 
|  | 175 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 && | 
|  | 176 | MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ | 
|  | 177 |  | 
| Manuel Pégourié-Gonnard | cd4fcc6 | 2015-05-26 12:11:48 +0200 | [diff] [blame] | 178 | /* | 
|  | 179 | * This structure contains the parameters only needed during handshake. | 
|  | 180 | */ | 
|  | 181 | struct mbedtls_ssl_handshake_params | 
|  | 182 | { | 
|  | 183 | /* | 
|  | 184 | * Handshake specific crypto variables | 
|  | 185 | */ | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 186 |  | 
|  | 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 | 
| Manuel Pégourié-Gonnard | cd4fcc6 | 2015-05-26 12:11:48 +0200 | [diff] [blame] | 191 | #if defined(MBEDTLS_DHM_C) | 
|  | 192 | mbedtls_dhm_context dhm_ctx;                /*!<  DHM key exchange        */ | 
|  | 193 | #endif | 
|  | 194 | #if defined(MBEDTLS_ECDH_C) | 
|  | 195 | mbedtls_ecdh_context ecdh_ctx;              /*!<  ECDH key exchange       */ | 
|  | 196 | #endif | 
| Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 197 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
| Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 198 | mbedtls_ecjpake_context ecjpake_ctx;        /*!< EC J-PAKE key exchange */ | 
| Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 199 | #if defined(MBEDTLS_SSL_CLI_C) | 
|  | 200 | unsigned char *ecjpake_cache;               /*!< Cache for ClientHello ext */ | 
|  | 201 | size_t ecjpake_cache_len;                   /*!< Length of cached data */ | 
|  | 202 | #endif | 
| Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 203 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ | 
| Manuel Pégourié-Gonnard | f472179 | 2015-09-15 10:53:51 +0200 | [diff] [blame] | 204 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ | 
| Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 205 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
| Manuel Pégourié-Gonnard | cd4fcc6 | 2015-05-26 12:11:48 +0200 | [diff] [blame] | 206 | const mbedtls_ecp_curve_info **curves;      /*!<  Supported elliptic curves */ | 
|  | 207 | #endif | 
|  | 208 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) | 
|  | 209 | unsigned char *psk;                 /*!<  PSK from the callback         */ | 
|  | 210 | size_t psk_len;                     /*!<  Length of PSK from callback   */ | 
|  | 211 | #endif | 
|  | 212 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
|  | 213 | mbedtls_ssl_key_cert *key_cert;     /*!< chosen key/cert pair (server)  */ | 
|  | 214 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) | 
| Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 215 | int sni_authmode;                   /*!< authmode from SNI callback     */ | 
| Manuel Pégourié-Gonnard | cd4fcc6 | 2015-05-26 12:11:48 +0200 | [diff] [blame] | 216 | mbedtls_ssl_key_cert *sni_key_cert; /*!< key/cert list from SNI         */ | 
|  | 217 | mbedtls_x509_crt *sni_ca_chain;     /*!< trusted CAs from SNI callback  */ | 
|  | 218 | mbedtls_x509_crl *sni_ca_crl;       /*!< trusted CAs CRLs from SNI      */ | 
| Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 219 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ | 
| Manuel Pégourié-Gonnard | cd4fcc6 | 2015-05-26 12:11:48 +0200 | [diff] [blame] | 220 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
|  | 221 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 222 | unsigned int out_msg_seq;           /*!<  Outgoing handshake sequence number */ | 
|  | 223 | unsigned int in_msg_seq;            /*!<  Incoming handshake sequence number */ | 
|  | 224 |  | 
|  | 225 | unsigned char *verify_cookie;       /*!<  Cli: HelloVerifyRequest cookie | 
|  | 226 | Srv: unused                    */ | 
|  | 227 | unsigned char verify_cookie_len;    /*!<  Cli: cookie length | 
|  | 228 | Srv: flag for sending a cookie */ | 
|  | 229 |  | 
|  | 230 | unsigned char *hs_msg;              /*!<  Reassembled handshake message  */ | 
|  | 231 |  | 
|  | 232 | uint32_t retransmit_timeout;        /*!<  Current value of timeout       */ | 
|  | 233 | unsigned char retransmit_state;     /*!<  Retransmission state           */ | 
|  | 234 | mbedtls_ssl_flight_item *flight;            /*!<  Current outgoing flight        */ | 
|  | 235 | mbedtls_ssl_flight_item *cur_msg;           /*!<  Current message in flight      */ | 
|  | 236 | unsigned int in_flight_start_seq;   /*!<  Minimum message sequence in the | 
|  | 237 | flight being received          */ | 
|  | 238 | mbedtls_ssl_transform *alt_transform_out;   /*!<  Alternative transform for | 
|  | 239 | resending messages             */ | 
|  | 240 | unsigned char alt_out_ctr[8];       /*!<  Alternative record epoch/counter | 
|  | 241 | for resending messages         */ | 
| Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 242 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
| Manuel Pégourié-Gonnard | cd4fcc6 | 2015-05-26 12:11:48 +0200 | [diff] [blame] | 243 |  | 
|  | 244 | /* | 
|  | 245 | * Checksum contexts | 
|  | 246 | */ | 
|  | 247 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ | 
|  | 248 | defined(MBEDTLS_SSL_PROTO_TLS1_1) | 
|  | 249 | mbedtls_md5_context fin_md5; | 
|  | 250 | mbedtls_sha1_context fin_sha1; | 
|  | 251 | #endif | 
|  | 252 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 253 | #if defined(MBEDTLS_SHA256_C) | 
|  | 254 | mbedtls_sha256_context fin_sha256; | 
|  | 255 | #endif | 
|  | 256 | #if defined(MBEDTLS_SHA512_C) | 
|  | 257 | mbedtls_sha512_context fin_sha512; | 
|  | 258 | #endif | 
|  | 259 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
|  | 260 |  | 
|  | 261 | void (*update_checksum)(mbedtls_ssl_context *, const unsigned char *, size_t); | 
|  | 262 | void (*calc_verify)(mbedtls_ssl_context *, unsigned char *); | 
|  | 263 | void (*calc_finished)(mbedtls_ssl_context *, unsigned char *, int); | 
|  | 264 | int  (*tls_prf)(const unsigned char *, size_t, const char *, | 
|  | 265 | const unsigned char *, size_t, | 
|  | 266 | unsigned char *, size_t); | 
|  | 267 |  | 
|  | 268 | size_t pmslen;                      /*!<  premaster length        */ | 
|  | 269 |  | 
|  | 270 | unsigned char randbytes[64];        /*!<  random bytes            */ | 
|  | 271 | unsigned char premaster[MBEDTLS_PREMASTER_SIZE]; | 
|  | 272 | /*!<  premaster secret        */ | 
|  | 273 |  | 
|  | 274 | int resume;                         /*!<  session resume indicator*/ | 
|  | 275 | int max_major_ver;                  /*!< max. major version client*/ | 
|  | 276 | int max_minor_ver;                  /*!< max. minor version client*/ | 
|  | 277 | int cli_exts;                       /*!< client extension presence*/ | 
|  | 278 |  | 
|  | 279 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) | 
|  | 280 | int new_session_ticket;             /*!< use NewSessionTicket?    */ | 
|  | 281 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ | 
|  | 282 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) | 
|  | 283 | int extended_ms;                    /*!< use Extended Master Secret? */ | 
|  | 284 | #endif | 
|  | 285 | }; | 
|  | 286 |  | 
|  | 287 | /* | 
|  | 288 | * This structure contains a full set of runtime transform parameters | 
|  | 289 | * either in negotiation or active. | 
|  | 290 | */ | 
|  | 291 | struct mbedtls_ssl_transform | 
|  | 292 | { | 
|  | 293 | /* | 
|  | 294 | * Session specific crypto layer | 
|  | 295 | */ | 
|  | 296 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info; | 
|  | 297 | /*!<  Chosen cipersuite_info  */ | 
| Manuel Pégourié-Gonnard | 39a48f4 | 2015-06-18 16:06:55 +0200 | [diff] [blame] | 298 | unsigned int keylen;                /*!<  symmetric key length (bytes)  */ | 
| Manuel Pégourié-Gonnard | cd4fcc6 | 2015-05-26 12:11:48 +0200 | [diff] [blame] | 299 | size_t minlen;                      /*!<  min. ciphertext length  */ | 
|  | 300 | size_t ivlen;                       /*!<  IV length               */ | 
|  | 301 | size_t fixed_ivlen;                 /*!<  Fixed part of IV (AEAD) */ | 
|  | 302 | size_t maclen;                      /*!<  MAC length              */ | 
|  | 303 |  | 
|  | 304 | unsigned char iv_enc[16];           /*!<  IV (encryption)         */ | 
|  | 305 | unsigned char iv_dec[16];           /*!<  IV (decryption)         */ | 
|  | 306 |  | 
|  | 307 | #if defined(MBEDTLS_SSL_PROTO_SSL3) | 
|  | 308 | /* Needed only for SSL v3.0 secret */ | 
|  | 309 | unsigned char mac_enc[20];          /*!<  SSL v3.0 secret (enc)   */ | 
|  | 310 | unsigned char mac_dec[20];          /*!<  SSL v3.0 secret (dec)   */ | 
|  | 311 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ | 
|  | 312 |  | 
|  | 313 | mbedtls_md_context_t md_ctx_enc;            /*!<  MAC (encryption)        */ | 
|  | 314 | mbedtls_md_context_t md_ctx_dec;            /*!<  MAC (decryption)        */ | 
|  | 315 |  | 
|  | 316 | mbedtls_cipher_context_t cipher_ctx_enc;    /*!<  encryption context      */ | 
|  | 317 | mbedtls_cipher_context_t cipher_ctx_dec;    /*!<  decryption context      */ | 
|  | 318 |  | 
|  | 319 | /* | 
|  | 320 | * Session specific compression layer | 
|  | 321 | */ | 
|  | 322 | #if defined(MBEDTLS_ZLIB_SUPPORT) | 
|  | 323 | z_stream ctx_deflate;               /*!<  compression context     */ | 
|  | 324 | z_stream ctx_inflate;               /*!<  decompression context   */ | 
|  | 325 | #endif | 
|  | 326 | }; | 
|  | 327 |  | 
|  | 328 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
|  | 329 | /* | 
|  | 330 | * List of certificate + private key pairs | 
|  | 331 | */ | 
|  | 332 | struct mbedtls_ssl_key_cert | 
|  | 333 | { | 
|  | 334 | mbedtls_x509_crt *cert;                 /*!< cert                       */ | 
|  | 335 | mbedtls_pk_context *key;                /*!< private key                */ | 
|  | 336 | mbedtls_ssl_key_cert *next;             /*!< next key/cert pair         */ | 
|  | 337 | }; | 
|  | 338 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
|  | 339 |  | 
|  | 340 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 341 | /* | 
|  | 342 | * List of handshake messages kept around for resending | 
|  | 343 | */ | 
|  | 344 | struct mbedtls_ssl_flight_item | 
|  | 345 | { | 
|  | 346 | unsigned char *p;       /*!< message, including handshake headers   */ | 
|  | 347 | size_t len;             /*!< length of p                            */ | 
|  | 348 | unsigned char type;     /*!< type of the message: handshake or CCS  */ | 
|  | 349 | mbedtls_ssl_flight_item *next;  /*!< next handshake message(s)              */ | 
|  | 350 | }; | 
|  | 351 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
|  | 352 |  | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 353 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ | 
|  | 354 | defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) | 
|  | 355 |  | 
|  | 356 | /* Find an entry in a signature-hash set matching a given hash algorithm. */ | 
|  | 357 | mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set, | 
|  | 358 | mbedtls_pk_type_t sig_alg ); | 
|  | 359 | /* Add a signature-hash-pair to a signature-hash set */ | 
|  | 360 | void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set, | 
|  | 361 | mbedtls_pk_type_t sig_alg, | 
|  | 362 | mbedtls_md_type_t md_alg ); | 
|  | 363 | /* Allow exactly one hash algorithm for each signature. */ | 
|  | 364 | void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set, | 
|  | 365 | mbedtls_md_type_t md_alg ); | 
|  | 366 |  | 
|  | 367 | /* Setup an empty signature-hash set */ | 
|  | 368 | static inline void mbedtls_ssl_sig_hash_set_init( mbedtls_ssl_sig_hash_set_t *set ) | 
|  | 369 | { | 
|  | 370 | mbedtls_ssl_sig_hash_set_const_hash( set, MBEDTLS_MD_NONE ); | 
|  | 371 | } | 
|  | 372 |  | 
|  | 373 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2) && | 
|  | 374 | MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ | 
| Manuel Pégourié-Gonnard | cd4fcc6 | 2015-05-26 12:11:48 +0200 | [diff] [blame] | 375 |  | 
|  | 376 | /** | 
|  | 377 | * \brief           Free referenced items in an SSL transform context and clear | 
|  | 378 | *                  memory | 
|  | 379 | * | 
|  | 380 | * \param transform SSL transform context | 
|  | 381 | */ | 
|  | 382 | void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform ); | 
|  | 383 |  | 
|  | 384 | /** | 
|  | 385 | * \brief           Free referenced items in an SSL handshake context and clear | 
|  | 386 | *                  memory | 
|  | 387 | * | 
|  | 388 | * \param handshake SSL handshake context | 
|  | 389 | */ | 
|  | 390 | void mbedtls_ssl_handshake_free( mbedtls_ssl_handshake_params *handshake ); | 
|  | 391 |  | 
| Manuel Pégourié-Gonnard | 5e94dde | 2015-05-26 11:57:05 +0200 | [diff] [blame] | 392 | int mbedtls_ssl_handshake_client_step( mbedtls_ssl_context *ssl ); | 
|  | 393 | int mbedtls_ssl_handshake_server_step( mbedtls_ssl_context *ssl ); | 
|  | 394 | void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl ); | 
|  | 395 |  | 
|  | 396 | int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl ); | 
|  | 397 |  | 
|  | 398 | void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl ); | 
|  | 399 | int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl ); | 
|  | 400 |  | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 401 | int mbedtls_ssl_read_record_layer( mbedtls_ssl_context *ssl ); | 
|  | 402 | int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl ); | 
|  | 403 | int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl ); | 
|  | 404 | void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl ); | 
|  | 405 |  | 
| Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 406 | /** | 
|  | 407 | * \brief       Update record layer | 
|  | 408 | * | 
|  | 409 | *              This function roughly separates the implementation | 
|  | 410 | *              of the logic of (D)TLS from the implementation | 
|  | 411 | *              of the secure transport. | 
|  | 412 | * | 
|  | 413 | * \param  ssl  SSL context to use | 
|  | 414 | * | 
|  | 415 | * \return      0 or non-zero error code. | 
|  | 416 | * | 
|  | 417 | * \note        A clarification on what is called 'record layer' here | 
|  | 418 | *              is in order, as many sensible definitions are possible: | 
|  | 419 | * | 
|  | 420 | *              The record layer takes as input an untrusted underlying | 
|  | 421 | *              transport (stream or datagram) and transforms it into | 
|  | 422 | *              a serially multiplexed, secure transport, which | 
|  | 423 | *              conceptually provides the following: | 
|  | 424 | * | 
|  | 425 | *              (1) Three datagram based, content-agnostic transports | 
|  | 426 | *                  for handshake, alert and CCS messages. | 
|  | 427 | *              (2) One stream- or datagram-based transport | 
|  | 428 | *                  for application data. | 
|  | 429 | *              (3) Functionality for changing the underlying transform | 
|  | 430 | *                  securing the contents. | 
|  | 431 | * | 
|  | 432 | *              The interface to this functionality is given as follows: | 
|  | 433 | * | 
|  | 434 | *              a Updating | 
|  | 435 | *                [Currently implemented by mbedtls_ssl_read_record] | 
|  | 436 | * | 
|  | 437 | *                Check if and on which of the four 'ports' data is pending: | 
|  | 438 | *                Nothing, a controlling datagram of type (1), or application | 
|  | 439 | *                data (2). In any case data is present, internal buffers | 
|  | 440 | *                provide access to the data for the user to process it. | 
|  | 441 | *                Consumption of type (1) datagrams is done automatically | 
|  | 442 | *                on the next update, invalidating that the internal buffers | 
|  | 443 | *                for previous datagrams, while consumption of application | 
|  | 444 | *                data (2) is user-controlled. | 
|  | 445 | * | 
|  | 446 | *              b Reading of application data | 
|  | 447 | *                [Currently manual adaption of ssl->in_offt pointer] | 
|  | 448 | * | 
|  | 449 | *                As mentioned in the last paragraph, consumption of data | 
|  | 450 | *                is different from the automatic consumption of control | 
|  | 451 | *                datagrams (1) because application data is treated as a stream. | 
|  | 452 | * | 
|  | 453 | *              c Tracking availability of application data | 
|  | 454 | *                [Currently manually through decreasing ssl->in_msglen] | 
|  | 455 | * | 
|  | 456 | *                For efficiency and to retain datagram semantics for | 
|  | 457 | *                application data in case of DTLS, the record layer | 
|  | 458 | *                provides functionality for checking how much application | 
|  | 459 | *                data is still available in the internal buffer. | 
|  | 460 | * | 
|  | 461 | *              d Changing the transformation securing the communication. | 
|  | 462 | * | 
|  | 463 | *              Given an opaque implementation of the record layer in the | 
|  | 464 | *              above sense, it should be possible to implement the logic | 
|  | 465 | *              of (D)TLS on top of it without the need to know anything | 
|  | 466 | *              about the record layer's internals. This is done e.g. | 
|  | 467 | *              in all the handshake handling functions, and in the | 
|  | 468 | *              application data reading function mbedtls_ssl_read. | 
|  | 469 | * | 
|  | 470 | * \note        The above tries to give a conceptual picture of the | 
|  | 471 | *              record layer, but the current implementation deviates | 
|  | 472 | *              from it in some places. For example, our implementation of | 
|  | 473 | *              the update functionality through mbedtls_ssl_read_record | 
|  | 474 | *              discards datagrams depending on the current state, which | 
|  | 475 | *              wouldn't fall under the record layer's responsibility | 
|  | 476 | *              following the above definition. | 
|  | 477 | * | 
|  | 478 | */ | 
| Manuel Pégourié-Gonnard | 5e94dde | 2015-05-26 11:57:05 +0200 | [diff] [blame] | 479 | int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl ); | 
|  | 480 | int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want ); | 
|  | 481 |  | 
|  | 482 | int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl ); | 
|  | 483 | int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl ); | 
|  | 484 |  | 
|  | 485 | int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ); | 
|  | 486 | int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ); | 
|  | 487 |  | 
|  | 488 | int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl ); | 
|  | 489 | int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl ); | 
|  | 490 |  | 
|  | 491 | int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl ); | 
|  | 492 | int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl ); | 
|  | 493 |  | 
|  | 494 | void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl, | 
|  | 495 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info ); | 
|  | 496 |  | 
|  | 497 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) | 
|  | 498 | int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex ); | 
|  | 499 | #endif | 
|  | 500 |  | 
|  | 501 | #if defined(MBEDTLS_PK_C) | 
|  | 502 | unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk ); | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 503 | unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type ); | 
| Manuel Pégourié-Gonnard | 5e94dde | 2015-05-26 11:57:05 +0200 | [diff] [blame] | 504 | mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig ); | 
|  | 505 | #endif | 
|  | 506 |  | 
|  | 507 | mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash ); | 
| Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 508 | unsigned char mbedtls_ssl_hash_from_md_alg( int md ); | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 509 | int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md ); | 
| Manuel Pégourié-Gonnard | 5e94dde | 2015-05-26 11:57:05 +0200 | [diff] [blame] | 510 |  | 
| Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 511 | #if defined(MBEDTLS_ECP_C) | 
| Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 512 | int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id ); | 
| Manuel Pégourié-Gonnard | 5e94dde | 2015-05-26 11:57:05 +0200 | [diff] [blame] | 513 | #endif | 
|  | 514 |  | 
| Manuel Pégourié-Gonnard | e5f3072 | 2015-10-22 17:01:15 +0200 | [diff] [blame] | 515 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) | 
| Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 516 | int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl, | 
|  | 517 | mbedtls_md_type_t md ); | 
|  | 518 | #endif | 
|  | 519 |  | 
| Manuel Pégourié-Gonnard | 5e94dde | 2015-05-26 11:57:05 +0200 | [diff] [blame] | 520 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
|  | 521 | static inline mbedtls_pk_context *mbedtls_ssl_own_key( mbedtls_ssl_context *ssl ) | 
|  | 522 | { | 
|  | 523 | mbedtls_ssl_key_cert *key_cert; | 
|  | 524 |  | 
|  | 525 | if( ssl->handshake != NULL && ssl->handshake->key_cert != NULL ) | 
|  | 526 | key_cert = ssl->handshake->key_cert; | 
|  | 527 | else | 
|  | 528 | key_cert = ssl->conf->key_cert; | 
|  | 529 |  | 
|  | 530 | return( key_cert == NULL ? NULL : key_cert->key ); | 
|  | 531 | } | 
|  | 532 |  | 
|  | 533 | static inline mbedtls_x509_crt *mbedtls_ssl_own_cert( mbedtls_ssl_context *ssl ) | 
|  | 534 | { | 
|  | 535 | mbedtls_ssl_key_cert *key_cert; | 
|  | 536 |  | 
|  | 537 | if( ssl->handshake != NULL && ssl->handshake->key_cert != NULL ) | 
|  | 538 | key_cert = ssl->handshake->key_cert; | 
|  | 539 | else | 
|  | 540 | key_cert = ssl->conf->key_cert; | 
|  | 541 |  | 
|  | 542 | return( key_cert == NULL ? NULL : key_cert->cert ); | 
|  | 543 | } | 
|  | 544 |  | 
|  | 545 | /* | 
|  | 546 | * Check usage of a certificate wrt extensions: | 
|  | 547 | * keyUsage, extendedKeyUsage (later), and nSCertType (later). | 
|  | 548 | * | 
|  | 549 | * Warning: cert_endpoint is the endpoint of the cert (ie, of our peer when we | 
|  | 550 | * check a cert we received from them)! | 
|  | 551 | * | 
|  | 552 | * Return 0 if everything is OK, -1 if not. | 
|  | 553 | */ | 
|  | 554 | int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert, | 
|  | 555 | const mbedtls_ssl_ciphersuite_t *ciphersuite, | 
|  | 556 | int cert_endpoint, | 
|  | 557 | uint32_t *flags ); | 
|  | 558 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
|  | 559 |  | 
|  | 560 | void mbedtls_ssl_write_version( int major, int minor, int transport, | 
|  | 561 | unsigned char ver[2] ); | 
|  | 562 | void mbedtls_ssl_read_version( int *major, int *minor, int transport, | 
|  | 563 | const unsigned char ver[2] ); | 
|  | 564 |  | 
|  | 565 | static inline size_t mbedtls_ssl_hdr_len( const mbedtls_ssl_context *ssl ) | 
|  | 566 | { | 
|  | 567 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 568 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
|  | 569 | return( 13 ); | 
|  | 570 | #else | 
|  | 571 | ((void) ssl); | 
|  | 572 | #endif | 
|  | 573 | return( 5 ); | 
|  | 574 | } | 
|  | 575 |  | 
|  | 576 | static inline size_t mbedtls_ssl_hs_hdr_len( const mbedtls_ssl_context *ssl ) | 
|  | 577 | { | 
|  | 578 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 579 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
|  | 580 | return( 12 ); | 
|  | 581 | #else | 
|  | 582 | ((void) ssl); | 
|  | 583 | #endif | 
|  | 584 | return( 4 ); | 
|  | 585 | } | 
|  | 586 |  | 
|  | 587 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 588 | void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl ); | 
|  | 589 | void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl ); | 
|  | 590 | int mbedtls_ssl_resend( mbedtls_ssl_context *ssl ); | 
|  | 591 | #endif | 
|  | 592 |  | 
|  | 593 | /* Visible for testing purposes only */ | 
|  | 594 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) | 
|  | 595 | int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl ); | 
|  | 596 | void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl ); | 
|  | 597 | #endif | 
|  | 598 |  | 
|  | 599 | /* constant-time buffer comparison */ | 
|  | 600 | static inline int mbedtls_ssl_safer_memcmp( const void *a, const void *b, size_t n ) | 
|  | 601 | { | 
|  | 602 | size_t i; | 
|  | 603 | const unsigned char *A = (const unsigned char *) a; | 
|  | 604 | const unsigned char *B = (const unsigned char *) b; | 
|  | 605 | unsigned char diff = 0; | 
|  | 606 |  | 
|  | 607 | for( i = 0; i < n; i++ ) | 
|  | 608 | diff |= A[i] ^ B[i]; | 
|  | 609 |  | 
|  | 610 | return( diff ); | 
|  | 611 | } | 
|  | 612 |  | 
|  | 613 | #ifdef __cplusplus | 
|  | 614 | } | 
|  | 615 | #endif | 
|  | 616 |  | 
|  | 617 | #endif /* ssl_internal.h */ |