mbed TLS Build

Dependents:   Slave-prot-prod

Committer:
williequesada
Date:
Tue Jun 04 16:03:38 2019 +0000
Revision:
1:1a219dea6cb5
Parent:
0:cdf462088d13
compartir a Pablo

Who changed what in which revision?

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