1 /** 2 * \file ssl.h 3 * 4 * \brief SSL/TLS functions. 5 * 6 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 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. 20 * 21 * This file is part of mbed TLS (https://tls.mbed.org) 22 */ 23 #ifndef MBEDTLS_SSL_H 24 #define MBEDTLS_SSL_H 25 26 #if !defined(MBEDTLS_CONFIG_FILE) 27 #include "config.h" 28 #else 29 #include MBEDTLS_CONFIG_FILE 30 #endif 31 32 #include "bignum.h" 33 #include "ecp.h" 34 35 #include "ssl_ciphersuites.h" 36 37 #if defined(MBEDTLS_X509_CRT_PARSE_C) 38 #include "x509_crt.h" 39 #include "x509_crl.h" 40 #endif 41 42 #if defined(MBEDTLS_DHM_C) 43 #include "dhm.h" 44 #endif 45 46 #if defined(MBEDTLS_ECDH_C) 47 #include "ecdh.h" 48 #endif 49 50 #if defined(MBEDTLS_ZLIB_SUPPORT) 51 #include "zlib.h" 52 #endif 53 54 #if defined(MBEDTLS_HAVE_TIME) 55 #include <time.h> 56 #endif 57 58 /* 59 * SSL Error codes 60 */ 61 #define MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE -0x7080 /**< The requested feature is not available. */ 62 #define MBEDTLS_ERR_SSL_BAD_INPUT_DATA -0x7100 /**< Bad input parameters to function. */ 63 #define MBEDTLS_ERR_SSL_INVALID_MAC -0x7180 /**< Verification of the message MAC failed. */ 64 #define MBEDTLS_ERR_SSL_INVALID_RECORD -0x7200 /**< An invalid SSL record was received. */ 65 #define MBEDTLS_ERR_SSL_CONN_EOF -0x7280 /**< The connection indicated an EOF. */ 66 #define MBEDTLS_ERR_SSL_UNKNOWN_CIPHER -0x7300 /**< An unknown cipher was received. */ 67 #define MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN -0x7380 /**< The server has no ciphersuites in common with the client. */ 68 #define MBEDTLS_ERR_SSL_NO_RNG -0x7400 /**< No RNG was provided to the SSL module. */ 69 #define MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE -0x7480 /**< No client certification received from the client, but required by the authentication mode. */ 70 #define MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE -0x7500 /**< Our own certificate(s) is/are too large to send in an SSL message. */ 71 #define MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED -0x7580 /**< The own certificate is not set, but needed by the server. */ 72 #define MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED -0x7600 /**< The own private key or pre-shared key is not set, but needed. */ 73 #define MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED -0x7680 /**< No CA Chain is set, but required to operate. */ 74 #define MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE -0x7700 /**< An unexpected message was received from our peer. */ 75 #define MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE -0x7780 /**< A fatal alert message was received from our peer. */ 76 #define MBEDTLS_ERR_SSL_PEER_VERIFY_FAILED -0x7800 /**< Verification of our peer failed. */ 77 #define MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY -0x7880 /**< The peer notified us that the connection is going to be closed. */ 78 #define MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO -0x7900 /**< Processing of the ClientHello handshake message failed. */ 79 #define MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO -0x7980 /**< Processing of the ServerHello handshake message failed. */ 80 #define MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE -0x7A00 /**< Processing of the Certificate handshake message failed. */ 81 #define MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST -0x7A80 /**< Processing of the CertificateRequest handshake message failed. */ 82 #define MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE -0x7B00 /**< Processing of the ServerKeyExchange handshake message failed. */ 83 #define MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO_DONE -0x7B80 /**< Processing of the ServerHelloDone handshake message failed. */ 84 #define MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE -0x7C00 /**< Processing of the ClientKeyExchange handshake message failed. */ 85 #define MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP -0x7C80 /**< Processing of the ClientKeyExchange handshake message failed in DHM / ECDH Read Public. */ 86 #define MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS -0x7D00 /**< Processing of the ClientKeyExchange handshake message failed in DHM / ECDH Calculate Secret. */ 87 #define MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY -0x7D80 /**< Processing of the CertificateVerify handshake message failed. */ 88 #define MBEDTLS_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC -0x7E00 /**< Processing of the ChangeCipherSpec handshake message failed. */ 89 #define MBEDTLS_ERR_SSL_BAD_HS_FINISHED -0x7E80 /**< Processing of the Finished handshake message failed. */ 90 #define MBEDTLS_ERR_SSL_ALLOC_FAILED -0x7F00 /**< Memory allocation failed */ 91 #define MBEDTLS_ERR_SSL_HW_ACCEL_FAILED -0x7F80 /**< Hardware acceleration function returned with error */ 92 #define MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH -0x6F80 /**< Hardware acceleration function skipped / left alone data */ 93 #define MBEDTLS_ERR_SSL_COMPRESSION_FAILED -0x6F00 /**< Processing of the compression / decompression failed */ 94 #define MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION -0x6E80 /**< Handshake protocol not within min/max boundaries */ 95 #define MBEDTLS_ERR_SSL_BAD_HS_NEW_SESSION_TICKET -0x6E00 /**< Processing of the NewSessionTicket handshake message failed. */ 96 #define MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED -0x6D80 /**< Session ticket has expired. */ 97 #define MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH -0x6D00 /**< Public key type mismatch (eg, asked for RSA key exchange and presented EC key) */ 98 #define MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY -0x6C80 /**< Unknown identity received (eg, PSK identity) */ 99 #define MBEDTLS_ERR_SSL_INTERNAL_ERROR -0x6C00 /**< Internal error (eg, unexpected failure in lower-level module) */ 100 #define MBEDTLS_ERR_SSL_COUNTER_WRAPPING -0x6B80 /**< A counter would wrap (eg, too many messages exchanged). */ 101 #define MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO -0x6B00 /**< Unexpected message at ServerHello in renegotiation. */ 102 #define MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED -0x6A80 /**< DTLS client must retry for hello verification */ 103 #define MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL -0x6A00 /**< A buffer is too small to receive or write a message */ 104 #define MBEDTLS_ERR_SSL_NO_USABLE_CIPHERSUITE -0x6980 /**< None of the common ciphersuites is usable (eg, no suitable certificate, see debug messages). */ 105 #define MBEDTLS_ERR_SSL_WANT_READ -0x6900 /**< Connection requires a read call. */ 106 #define MBEDTLS_ERR_SSL_WANT_WRITE -0x6880 /**< Connection requires a write call. */ 107 #define MBEDTLS_ERR_SSL_TIMEOUT -0x6800 /**< The operation timed out. */ 108 #define MBEDTLS_ERR_SSL_CLIENT_RECONNECT -0x6780 /**< The client initiated a reconnect from the same port. */ 109 #define MBEDTLS_ERR_SSL_UNEXPECTED_RECORD -0x6700 /**< Record header looks valid but is not expected. */ 110 111 /* 112 * Various constants 113 */ 114 #define MBEDTLS_SSL_MAJOR_VERSION_3 3 115 #define MBEDTLS_SSL_MINOR_VERSION_0 0 /*!< SSL v3.0 */ 116 #define MBEDTLS_SSL_MINOR_VERSION_1 1 /*!< TLS v1.0 */ 117 #define MBEDTLS_SSL_MINOR_VERSION_2 2 /*!< TLS v1.1 */ 118 #define MBEDTLS_SSL_MINOR_VERSION_3 3 /*!< TLS v1.2 */ 119 120 #define MBEDTLS_SSL_TRANSPORT_STREAM 0 /*!< TLS */ 121 #define MBEDTLS_SSL_TRANSPORT_DATAGRAM 1 /*!< DTLS */ 122 123 #define MBEDTLS_SSL_MAX_HOST_NAME_LEN 255 /*!< Maximum host name defined in RFC 1035 */ 124 125 /* RFC 6066 section 4, see also mfl_code_to_length in ssl_tls.c 126 * NONE must be zero so that memset()ing structure to zero works */ 127 #define MBEDTLS_SSL_MAX_FRAG_LEN_NONE 0 /*!< don't use this extension */ 128 #define MBEDTLS_SSL_MAX_FRAG_LEN_512 1 /*!< MaxFragmentLength 2^9 */ 129 #define MBEDTLS_SSL_MAX_FRAG_LEN_1024 2 /*!< MaxFragmentLength 2^10 */ 130 #define MBEDTLS_SSL_MAX_FRAG_LEN_2048 3 /*!< MaxFragmentLength 2^11 */ 131 #define MBEDTLS_SSL_MAX_FRAG_LEN_4096 4 /*!< MaxFragmentLength 2^12 */ 132 #define MBEDTLS_SSL_MAX_FRAG_LEN_INVALID 5 /*!< first invalid value */ 133 134 #define MBEDTLS_SSL_IS_CLIENT 0 135 #define MBEDTLS_SSL_IS_SERVER 1 136 137 #define MBEDTLS_SSL_IS_NOT_FALLBACK 0 138 #define MBEDTLS_SSL_IS_FALLBACK 1 139 140 #define MBEDTLS_SSL_EXTENDED_MS_DISABLED 0 141 #define MBEDTLS_SSL_EXTENDED_MS_ENABLED 1 142 143 #define MBEDTLS_SSL_ETM_DISABLED 0 144 #define MBEDTLS_SSL_ETM_ENABLED 1 145 146 #define MBEDTLS_SSL_COMPRESS_NULL 0 147 #define MBEDTLS_SSL_COMPRESS_DEFLATE 1 148 149 #define MBEDTLS_SSL_VERIFY_NONE 0 150 #define MBEDTLS_SSL_VERIFY_OPTIONAL 1 151 #define MBEDTLS_SSL_VERIFY_REQUIRED 2 152 #define MBEDTLS_SSL_VERIFY_UNSET 3 /* Used only for sni_authmode */ 153 154 #define MBEDTLS_SSL_LEGACY_RENEGOTIATION 0 155 #define MBEDTLS_SSL_SECURE_RENEGOTIATION 1 156 157 #define MBEDTLS_SSL_RENEGOTIATION_DISABLED 0 158 #define MBEDTLS_SSL_RENEGOTIATION_ENABLED 1 159 160 #define MBEDTLS_SSL_ANTI_REPLAY_DISABLED 0 161 #define MBEDTLS_SSL_ANTI_REPLAY_ENABLED 1 162 163 #define MBEDTLS_SSL_RENEGOTIATION_NOT_ENFORCED -1 164 #define MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT 16 165 166 #define MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION 0 167 #define MBEDTLS_SSL_LEGACY_ALLOW_RENEGOTIATION 1 168 #define MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE 2 169 170 #define MBEDTLS_SSL_TRUNC_HMAC_DISABLED 0 171 #define MBEDTLS_SSL_TRUNC_HMAC_ENABLED 1 172 #define MBEDTLS_SSL_TRUNCATED_HMAC_LEN 10 /* 80 bits, rfc 6066 section 7 */ 173 174 #define MBEDTLS_SSL_SESSION_TICKETS_DISABLED 0 175 #define MBEDTLS_SSL_SESSION_TICKETS_ENABLED 1 176 177 #define MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED 0 178 #define MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED 1 179 180 #define MBEDTLS_SSL_ARC4_ENABLED 0 181 #define MBEDTLS_SSL_ARC4_DISABLED 1 182 183 #define MBEDTLS_SSL_PRESET_DEFAULT 0 184 #define MBEDTLS_SSL_PRESET_SUITEB 2 185 186 /* 187 * Default range for DTLS retransmission timer value, in milliseconds. 188 * RFC 6347 4.2.4.1 says from 1 second to 60 seconds. 189 */ 190 #define MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN 1000 191 #define MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX 60000 192 193 /** 194 * \name SECTION: Module settings 195 * 196 * The configuration options you can set for this module are in this section. 197 * Either change them in config.h or define them on the compiler command line. 198 * \{ 199 */ 200 201 #if !defined(MBEDTLS_SSL_DEFAULT_TICKET_LIFETIME) 202 #define MBEDTLS_SSL_DEFAULT_TICKET_LIFETIME 86400 /**< Lifetime of session tickets (if enabled) */ 203 #endif 204 205 /* 206 * Maxium fragment length in bytes, 207 * determines the size of each of the two internal I/O buffers. 208 * 209 * Note: the RFC defines the default size of SSL / TLS messages. If you 210 * change the value here, other clients / servers may not be able to 211 * communicate with you anymore. Only change this value if you control 212 * both sides of the connection and have it reduced at both sides, or 213 * if you're using the Max Fragment Length extension and you know all your 214 * peers are using it too! 215 */ 216 #if !defined(MBEDTLS_SSL_MAX_CONTENT_LEN) 217 #define MBEDTLS_SSL_MAX_CONTENT_LEN 16384 /**< Size of the input / output buffer */ 218 #endif 219 220 /* \} name SECTION: Module settings */ 221 222 /* 223 * Length of the verify data for secure renegotiation 224 */ 225 #if defined(MBEDTLS_SSL_PROTO_SSL3) 226 #define MBEDTLS_SSL_VERIFY_DATA_MAX_LEN 36 227 #else 228 #define MBEDTLS_SSL_VERIFY_DATA_MAX_LEN 12 229 #endif 230 231 /* 232 * Signaling ciphersuite values (SCSV) 233 */ 234 #define MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO 0xFF /**< renegotiation info ext */ 235 #define MBEDTLS_SSL_FALLBACK_SCSV_VALUE 0x5600 /**< draft-ietf-tls-downgrade-scsv-00 */ 236 237 /* 238 * Supported Signature and Hash algorithms (For TLS 1.2) 239 * RFC 5246 section 7.4.1.4.1 240 */ 241 #define MBEDTLS_SSL_HASH_NONE 0 242 #define MBEDTLS_SSL_HASH_MD5 1 243 #define MBEDTLS_SSL_HASH_SHA1 2 244 #define MBEDTLS_SSL_HASH_SHA224 3 245 #define MBEDTLS_SSL_HASH_SHA256 4 246 #define MBEDTLS_SSL_HASH_SHA384 5 247 #define MBEDTLS_SSL_HASH_SHA512 6 248 249 #define MBEDTLS_SSL_SIG_ANON 0 250 #define MBEDTLS_SSL_SIG_RSA 1 251 #define MBEDTLS_SSL_SIG_ECDSA 3 252 253 /* 254 * Client Certificate Types 255 * RFC 5246 section 7.4.4 plus RFC 4492 section 5.5 256 */ 257 #define MBEDTLS_SSL_CERT_TYPE_RSA_SIGN 1 258 #define MBEDTLS_SSL_CERT_TYPE_ECDSA_SIGN 64 259 260 /* 261 * Message, alert and handshake types 262 */ 263 #define MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC 20 264 #define MBEDTLS_SSL_MSG_ALERT 21 265 #define MBEDTLS_SSL_MSG_HANDSHAKE 22 266 #define MBEDTLS_SSL_MSG_APPLICATION_DATA 23 267 268 #define MBEDTLS_SSL_ALERT_LEVEL_WARNING 1 269 #define MBEDTLS_SSL_ALERT_LEVEL_FATAL 2 270 271 #define MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY 0 /* 0x00 */ 272 #define MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE 10 /* 0x0A */ 273 #define MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC 20 /* 0x14 */ 274 #define MBEDTLS_SSL_ALERT_MSG_DECRYPTION_FAILED 21 /* 0x15 */ 275 #define MBEDTLS_SSL_ALERT_MSG_RECORD_OVERFLOW 22 /* 0x16 */ 276 #define MBEDTLS_SSL_ALERT_MSG_DECOMPRESSION_FAILURE 30 /* 0x1E */ 277 #define MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE 40 /* 0x28 */ 278 #define MBEDTLS_SSL_ALERT_MSG_NO_CERT 41 /* 0x29 */ 279 #define MBEDTLS_SSL_ALERT_MSG_BAD_CERT 42 /* 0x2A */ 280 #define MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT 43 /* 0x2B */ 281 #define MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED 44 /* 0x2C */ 282 #define MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED 45 /* 0x2D */ 283 #define MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN 46 /* 0x2E */ 284 #define MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER 47 /* 0x2F */ 285 #define MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA 48 /* 0x30 */ 286 #define MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED 49 /* 0x31 */ 287 #define MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR 50 /* 0x32 */ 288 #define MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR 51 /* 0x33 */ 289 #define MBEDTLS_SSL_ALERT_MSG_EXPORT_RESTRICTION 60 /* 0x3C */ 290 #define MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION 70 /* 0x46 */ 291 #define MBEDTLS_SSL_ALERT_MSG_INSUFFICIENT_SECURITY 71 /* 0x47 */ 292 #define MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR 80 /* 0x50 */ 293 #define MBEDTLS_SSL_ALERT_MSG_INAPROPRIATE_FALLBACK 86 /* 0x56 */ 294 #define MBEDTLS_SSL_ALERT_MSG_USER_CANCELED 90 /* 0x5A */ 295 #define MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION 100 /* 0x64 */ 296 #define MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT 110 /* 0x6E */ 297 #define MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME 112 /* 0x70 */ 298 #define MBEDTLS_SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY 115 /* 0x73 */ 299 #define MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL 120 /* 0x78 */ 300 301 #define MBEDTLS_SSL_HS_HELLO_REQUEST 0 302 #define MBEDTLS_SSL_HS_CLIENT_HELLO 1 303 #define MBEDTLS_SSL_HS_SERVER_HELLO 2 304 #define MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST 3 305 #define MBEDTLS_SSL_HS_NEW_SESSION_TICKET 4 306 #define MBEDTLS_SSL_HS_CERTIFICATE 11 307 #define MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE 12 308 #define MBEDTLS_SSL_HS_CERTIFICATE_REQUEST 13 309 #define MBEDTLS_SSL_HS_SERVER_HELLO_DONE 14 310 #define MBEDTLS_SSL_HS_CERTIFICATE_VERIFY 15 311 #define MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE 16 312 #define MBEDTLS_SSL_HS_FINISHED 20 313 314 /* 315 * TLS extensions 316 */ 317 #define MBEDTLS_TLS_EXT_SERVERNAME 0 318 #define MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME 0 319 320 #define MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH 1 321 322 #define MBEDTLS_TLS_EXT_TRUNCATED_HMAC 4 323 324 #define MBEDTLS_TLS_EXT_SUPPORTED_ELLIPTIC_CURVES 10 325 #define MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS 11 326 327 #define MBEDTLS_TLS_EXT_SIG_ALG 13 328 329 #define MBEDTLS_TLS_EXT_ALPN 16 330 331 #define MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC 22 /* 0x16 */ 332 #define MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET 0x0017 /* 23 */ 333 334 #define MBEDTLS_TLS_EXT_SESSION_TICKET 35 335 336 #define MBEDTLS_TLS_EXT_ECJPAKE_KKPP 256 /* experimental */ 337 338 #define MBEDTLS_TLS_EXT_RENEGOTIATION_INFO 0xFF01 339 340 /* 341 * Size defines 342 */ 343 #if !defined(MBEDTLS_PSK_MAX_LEN) 344 #define MBEDTLS_PSK_MAX_LEN 32 /* 256 bits */ 345 #endif 346 347 /* Dummy type used only for its size */ 348 union mbedtls_ssl_premaster_secret 349 { 350 #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) 351 unsigned char _pms_rsa[48]; /* RFC 5246 8.1.1 */ 352 #endif 353 #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) 354 unsigned char _pms_dhm[MBEDTLS_MPI_MAX_SIZE]; /* RFC 5246 8.1.2 */ 355 #endif 356 #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \ 357 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \ 358 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \ 359 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) 360 unsigned char _pms_ecdh[MBEDTLS_ECP_MAX_BYTES]; /* RFC 4492 5.10 */ 361 #endif 362 #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) 363 unsigned char _pms_psk[4 + 2 * MBEDTLS_PSK_MAX_LEN]; /* RFC 4279 2 */ 364 #endif 365 #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) 366 unsigned char _pms_dhe_psk[4 + MBEDTLS_MPI_MAX_SIZE 367 + MBEDTLS_PSK_MAX_LEN]; /* RFC 4279 3 */ 368 #endif 369 #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) 370 unsigned char _pms_rsa_psk[52 + MBEDTLS_PSK_MAX_LEN]; /* RFC 4279 4 */ 371 #endif 372 #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) 373 unsigned char _pms_ecdhe_psk[4 + MBEDTLS_ECP_MAX_BYTES 374 + MBEDTLS_PSK_MAX_LEN]; /* RFC 5489 2 */ 375 #endif 376 #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) 377 unsigned char _pms_ecjpake[32]; /* Thread spec: SHA-256 output */ 378 #endif 379 }; 380 381 #define MBEDTLS_PREMASTER_SIZE sizeof( union mbedtls_ssl_premaster_secret ) 382 383 #ifdef __cplusplus 384 extern "C" { 385 #endif 386 387 /* 388 * SSL state machine 389 */ 390 typedef enum 391 { 392 MBEDTLS_SSL_HELLO_REQUEST, 393 MBEDTLS_SSL_CLIENT_HELLO, 394 MBEDTLS_SSL_SERVER_HELLO, 395 MBEDTLS_SSL_SERVER_CERTIFICATE, 396 MBEDTLS_SSL_SERVER_KEY_EXCHANGE, 397 MBEDTLS_SSL_CERTIFICATE_REQUEST, 398 MBEDTLS_SSL_SERVER_HELLO_DONE, 399 MBEDTLS_SSL_CLIENT_CERTIFICATE, 400 MBEDTLS_SSL_CLIENT_KEY_EXCHANGE, 401 MBEDTLS_SSL_CERTIFICATE_VERIFY, 402 MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC, 403 MBEDTLS_SSL_CLIENT_FINISHED, 404 MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC, 405 MBEDTLS_SSL_SERVER_FINISHED, 406 MBEDTLS_SSL_FLUSH_BUFFERS, 407 MBEDTLS_SSL_HANDSHAKE_WRAPUP, 408 MBEDTLS_SSL_HANDSHAKE_OVER, 409 MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET, 410 MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT, 411 } 412 mbedtls_ssl_states; 413 414 /* Defined below */ 415 typedef struct mbedtls_ssl_session mbedtls_ssl_session; 416 typedef struct mbedtls_ssl_context mbedtls_ssl_context; 417 typedef struct mbedtls_ssl_config mbedtls_ssl_config; 418 419 /* Defined in ssl_internal.h */ 420 typedef struct mbedtls_ssl_transform mbedtls_ssl_transform; 421 typedef struct mbedtls_ssl_handshake_params mbedtls_ssl_handshake_params; 422 #if defined(MBEDTLS_X509_CRT_PARSE_C) 423 typedef struct mbedtls_ssl_key_cert mbedtls_ssl_key_cert; 424 #endif 425 #if defined(MBEDTLS_SSL_PROTO_DTLS) 426 typedef struct mbedtls_ssl_flight_item mbedtls_ssl_flight_item; 427 #endif 428 429 /* 430 * This structure is used for storing current session data. 431 */ 432 struct mbedtls_ssl_session 433 { 434 #if defined(MBEDTLS_HAVE_TIME) 435 time_t start; /*!< starting time */ 436 #endif 437 int ciphersuite; /*!< chosen ciphersuite */ 438 int compression; /*!< chosen compression */ 439 size_t id_len; /*!< session id length */ 440 unsigned char id[32]; /*!< session identifier */ 441 unsigned char master[48]; /*!< the master secret */ 442 443 #if defined(MBEDTLS_X509_CRT_PARSE_C) 444 mbedtls_x509_crt *peer_cert; /*!< peer X.509 cert chain */ 445 #endif /* MBEDTLS_X509_CRT_PARSE_C */ 446 uint32_t verify_result; /*!< verification result */ 447 448 #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) 449 unsigned char *ticket; /*!< RFC 5077 session ticket */ 450 size_t ticket_len; /*!< session ticket length */ 451 uint32_t ticket_lifetime; /*!< ticket lifetime hint */ 452 #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ 453 454 #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) 455 unsigned char mfl_code; /*!< MaxFragmentLength negotiated by peer */ 456 #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ 457 458 #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) 459 int trunc_hmac; /*!< flag for truncated hmac activation */ 460 #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ 461 462 #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) 463 int encrypt_then_mac; /*!< flag for EtM activation */ 464 #endif 465 }; 466 467 /** 468 * SSL/TLS configuration to be shared between mbedtls_ssl_context structures. 469 */ 470 struct mbedtls_ssl_config 471 { 472 /* Group items by size (largest first) to minimize padding overhead */ 473 474 /* 475 * Pointers 476 */ 477 478 const int *ciphersuite_list[4]; /*!< allowed ciphersuites per version */ 479 480 /** Callback for printing debug output */ 481 void (*f_dbg)(void *, int, const char *, int, const char *); 482 void *p_dbg; /*!< context for the debug function */ 483 484 /** Callback for getting (pseudo-)random numbers */ 485 int (*f_rng)(void *, unsigned char *, size_t); 486 void *p_rng; /*!< context for the RNG function */ 487 488 /** Callback to retrieve a session from the cache */ 489 int (*f_get_cache)(void *, mbedtls_ssl_session *); 490 /** Callback to store a session into the cache */ 491 int (*f_set_cache)(void *, const mbedtls_ssl_session *); 492 void *p_cache; /*!< context for cache callbacks */ 493 494 #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) 495 /** Callback for setting cert according to SNI extension */ 496 int (*f_sni)(void *, mbedtls_ssl_context *, const unsigned char *, size_t); 497 void *p_sni; /*!< context for SNI callback */ 498 #endif 499 500 #if defined(MBEDTLS_X509_CRT_PARSE_C) 501 /** Callback to customize X.509 certificate chain verification */ 502 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *); 503 void *p_vrfy; /*!< context for X.509 verify calllback */ 504 #endif 505 506 #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) 507 /** Callback to retrieve PSK key from identity */ 508 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *, size_t); 509 void *p_psk; /*!< context for PSK callback */ 510 #endif 511 512 #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) 513 /** Callback to create & write a cookie for ClientHello veirifcation */ 514 int (*f_cookie_write)( void *, unsigned char **, unsigned char *, 515 const unsigned char *, size_t ); 516 /** Callback to verify validity of a ClientHello cookie */ 517 int (*f_cookie_check)( void *, const unsigned char *, size_t, 518 const unsigned char *, size_t ); 519 void *p_cookie; /*!< context for the cookie callbacks */ 520 #endif 521 522 #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_SRV_C) 523 /** Callback to create & write a session ticket */ 524 int (*f_ticket_write)( void *, const mbedtls_ssl_session *, 525 unsigned char *, const unsigned char *, size_t *, uint32_t * ); 526 /** Callback to parse a session ticket into a session structure */ 527 int (*f_ticket_parse)( void *, mbedtls_ssl_session *, unsigned char *, size_t); 528 void *p_ticket; /*!< context for the ticket callbacks */ 529 #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_SRV_C */ 530 531 #if defined(MBEDTLS_SSL_EXPORT_KEYS) 532 /** Callback to export key block and master secret */ 533 int (*f_export_keys)( void *, const unsigned char *, 534 const unsigned char *, size_t, size_t, size_t ); 535 void *p_export_keys; /*!< context for key export callback */ 536 #endif 537 538 #if defined(MBEDTLS_X509_CRT_PARSE_C) 539 const mbedtls_x509_crt_profile *cert_profile; /*!< verification profile */ 540 mbedtls_ssl_key_cert *key_cert; /*!< own certificate/key pair(s) */ 541 mbedtls_x509_crt *ca_chain; /*!< trusted CAs */ 542 mbedtls_x509_crl *ca_crl; /*!< trusted CAs CRLs */ 543 #endif /* MBEDTLS_X509_CRT_PARSE_C */ 544 545 #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) 546 const int *sig_hashes; /*!< allowed signature hashes */ 547 #endif 548 549 #if defined(MBEDTLS_ECP_C) 550 const mbedtls_ecp_group_id *curve_list; /*!< allowed curves */ 551 #endif 552 553 #if defined(MBEDTLS_DHM_C) 554 mbedtls_mpi dhm_P; /*!< prime modulus for DHM */ 555 mbedtls_mpi dhm_G; /*!< generator for DHM */ 556 #endif 557 558 #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) 559 unsigned char *psk; /*!< pre-shared key */ 560 size_t psk_len; /*!< length of the pre-shared key */ 561 unsigned char *psk_identity; /*!< identity for PSK negotiation */ 562 size_t psk_identity_len;/*!< length of identity */ 563 #endif 564 565 #if defined(MBEDTLS_SSL_ALPN) 566 const char **alpn_list; /*!< ordered list of protocols */ 567 #endif 568 569 /* 570 * Numerical settings (int then char) 571 */ 572 573 uint32_t read_timeout; /*!< timeout for mbedtls_ssl_read (ms) */ 574 575 #if defined(MBEDTLS_SSL_PROTO_DTLS) 576 uint32_t hs_timeout_min; /*!< initial value of the handshake 577 retransmission timeout (ms) */ 578 uint32_t hs_timeout_max; /*!< maximum value of the handshake 579 retransmission timeout (ms) */ 580 #endif 581 582 #if defined(MBEDTLS_SSL_RENEGOTIATION) 583 int renego_max_records; /*!< grace period for renegotiation */ 584 unsigned char renego_period[8]; /*!< value of the record counters 585 that triggers renegotiation */ 586 #endif 587 588 #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) 589 unsigned int badmac_limit; /*!< limit of records with a bad MAC */ 590 #endif 591 592 #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) 593 unsigned int dhm_min_bitlen; /*!< min. bit length of the DHM prime */ 594 #endif 595 596 unsigned char max_major_ver; /*!< max. major version used */ 597 unsigned char max_minor_ver; /*!< max. minor version used */ 598 unsigned char min_major_ver; /*!< min. major version used */ 599 unsigned char min_minor_ver; /*!< min. minor version used */ 600 601 /* 602 * Flags (bitfields) 603 */ 604 605 unsigned int endpoint : 1; /*!< 0: client, 1: server */ 606 unsigned int transport : 1; /*!< stream (TLS) or datagram (DTLS) */ 607 unsigned int authmode : 2; /*!< MBEDTLS_SSL_VERIFY_XXX */ 608 /* needed even with renego disabled for LEGACY_BREAK_HANDSHAKE */ 609 unsigned int allow_legacy_renegotiation : 2 ; /*!< MBEDTLS_LEGACY_XXX */ 610 #if defined(MBEDTLS_ARC4_C) 611 unsigned int arc4_disabled : 1; /*!< blacklist RC4 ciphersuites? */ 612 #endif 613 #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) 614 unsigned int mfl_code : 3; /*!< desired fragment length */ 615 #endif 616 #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) 617 unsigned int encrypt_then_mac : 1 ; /*!< negotiate encrypt-then-mac? */ 618 #endif 619 #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) 620 unsigned int extended_ms : 1; /*!< negotiate extended master secret? */ 621 #endif 622 #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) 623 unsigned int anti_replay : 1; /*!< detect and prevent replay? */ 624 #endif 625 #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) 626 unsigned int cbc_record_splitting : 1; /*!< do cbc record splitting */ 627 #endif 628 #if defined(MBEDTLS_SSL_RENEGOTIATION) 629 unsigned int disable_renegotiation : 1; /*!< disable renegotiation? */ 630 #endif 631 #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) 632 unsigned int trunc_hmac : 1; /*!< negotiate truncated hmac? */ 633 #endif 634 #if defined(MBEDTLS_SSL_SESSION_TICKETS) 635 unsigned int session_tickets : 1; /*!< use session tickets? */ 636 #endif 637 #if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C) 638 unsigned int fallback : 1; /*!< is this a fallback? */ 639 #endif 640 }; 641 642 643 struct mbedtls_ssl_context 644 { 645 const mbedtls_ssl_config *conf; /*!< configuration information */ 646 647 /* 648 * Miscellaneous 649 */ 650 int state; /*!< SSL handshake: current state */ 651 #if defined(MBEDTLS_SSL_RENEGOTIATION) 652 int renego_status; /*!< Initial, in progress, pending? */ 653 int renego_records_seen; /*!< Records since renego request, or with DTLS, 654 number of retransmissions of request if 655 renego_max_records is < 0 */ 656 #endif 657 658 int major_ver; /*!< equal to MBEDTLS_SSL_MAJOR_VERSION_3 */ 659 int minor_ver; /*!< either 0 (SSL3) or 1 (TLS1.0) */ 660 661 #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) 662 unsigned badmac_seen; /*!< records with a bad MAC received */ 663 #endif 664 665 /* 666 * Callbacks 667 */ 668 int (*f_send)(void *, const unsigned char *, size_t); 669 int (*f_recv)(void *, unsigned char *, size_t); 670 int (*f_recv_timeout)(void *, unsigned char *, size_t, uint32_t); 671 void *p_bio; /*!< context for I/O operations */ 672 673 /* 674 * Session layer 675 */ 676 mbedtls_ssl_session *session_in; /*!< current session data (in) */ 677 mbedtls_ssl_session *session_out; /*!< current session data (out) */ 678 mbedtls_ssl_session *session; /*!< negotiated session data */ 679 mbedtls_ssl_session *session_negotiate; /*!< session data in negotiation */ 680 681 mbedtls_ssl_handshake_params *handshake; /*!< params required only during 682 the handshake process */ 683 684 /* 685 * Record layer transformations 686 */ 687 mbedtls_ssl_transform *transform_in; /*!< current transform params (in) */ 688 mbedtls_ssl_transform *transform_out; /*!< current transform params (in) */ 689 mbedtls_ssl_transform *transform; /*!< negotiated transform params */ 690 mbedtls_ssl_transform *transform_negotiate; /*!< transform params in negotiation */ 691 692 /* 693 * Timers 694 */ 695 void *p_timer; /*!< context for the timer callbacks */ 696 void (*f_set_timer)(void *, uint32_t, uint32_t); /*!< set timer callback */ 697 int (*f_get_timer)(void *); /*!< get timer callback */ 698 699 /* 700 * Record layer (incoming data) 701 */ 702 unsigned char *in_buf; /*!< input buffer */ 703 unsigned char *in_ctr; /*!< 64-bit incoming message counter 704 TLS: maintained by us 705 DTLS: read from peer */ 706 unsigned char *in_hdr; /*!< start of record header */ 707 unsigned char *in_len; /*!< two-bytes message length field */ 708 unsigned char *in_iv; /*!< ivlen-byte IV */ 709 unsigned char *in_msg; /*!< message contents (in_iv+ivlen) */ 710 unsigned char *in_offt; /*!< read offset in application data */ 711 712 int in_msgtype; /*!< record header: message type */ 713 size_t in_msglen; /*!< record header: message length */ 714 size_t in_left; /*!< amount of data read so far */ 715 #if defined(MBEDTLS_SSL_PROTO_DTLS) 716 uint16_t in_epoch; /*!< DTLS epoch for incoming records */ 717 size_t next_record_offset; /*!< offset of the next record in datagram 718 (equal to in_left if none) */ 719 #endif 720 #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) 721 uint64_t in_window_top; /*!< last validated record seq_num */ 722 uint64_t in_window; /*!< bitmask for replay detection */ 723 #endif 724 725 size_t in_hslen; /*!< current handshake message length, 726 including the handshake header */ 727 int nb_zero; /*!< # of 0-length encrypted messages */ 728 int record_read; /*!< record is already present */ 729 730 /* 731 * Record layer (outgoing data) 732 */ 733 unsigned char *out_buf; /*!< output buffer */ 734 unsigned char *out_ctr; /*!< 64-bit outgoing message counter */ 735 unsigned char *out_hdr; /*!< start of record header */ 736 unsigned char *out_len; /*!< two-bytes message length field */ 737 unsigned char *out_iv; /*!< ivlen-byte IV */ 738 unsigned char *out_msg; /*!< message contents (out_iv+ivlen) */ 739 740 int out_msgtype; /*!< record header: message type */ 741 size_t out_msglen; /*!< record header: message length */ 742 size_t out_left; /*!< amount of data not yet written */ 743 744 #if defined(MBEDTLS_ZLIB_SUPPORT) 745 unsigned char *compress_buf; /*!< zlib data buffer */ 746 #endif 747 #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) 748 signed char split_done; /*!< current record already splitted? */ 749 #endif 750 751 /* 752 * PKI layer 753 */ 754 int client_auth; /*!< flag for client auth. */ 755 756 /* 757 * User settings 758 */ 759 #if defined(MBEDTLS_X509_CRT_PARSE_C) 760 char *hostname; /*!< expected peer CN for verification 761 (and SNI if available) */ 762 #endif 763 764 #if defined(MBEDTLS_SSL_ALPN) 765 const char *alpn_chosen; /*!< negotiated protocol */ 766 #endif 767 768 /* 769 * Information for DTLS hello verify 770 */ 771 #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) 772 unsigned char *cli_id; /*!< transport-level ID of the client */ 773 size_t cli_id_len; /*!< length of cli_id */ 774 #endif 775 776 /* 777 * Secure renegotiation 778 */ 779 /* needed to know when to send extension on server */ 780 int secure_renegotiation; /*!< does peer support legacy or 781 secure renegotiation */ 782 #if defined(MBEDTLS_SSL_RENEGOTIATION) 783 size_t verify_data_len; /*!< length of verify data stored */ 784 char own_verify_data[MBEDTLS_SSL_VERIFY_DATA_MAX_LEN]; /*!< previous handshake verify data */ 785 char peer_verify_data[MBEDTLS_SSL_VERIFY_DATA_MAX_LEN]; /*!< previous handshake verify data */ 786 #endif 787 }; 788 789 #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) 790 791 #define MBEDTLS_SSL_CHANNEL_OUTBOUND 0 792 #define MBEDTLS_SSL_CHANNEL_INBOUND 1 793 794 extern int (*mbedtls_ssl_hw_record_init)(mbedtls_ssl_context *ssl, 795 const unsigned char *key_enc, const unsigned char *key_dec, 796 size_t keylen, 797 const unsigned char *iv_enc, const unsigned char *iv_dec, 798 size_t ivlen, 799 const unsigned char *mac_enc, const unsigned char *mac_dec, 800 size_t maclen); 801 extern int (*mbedtls_ssl_hw_record_activate)(mbedtls_ssl_context *ssl, int direction); 802 extern int (*mbedtls_ssl_hw_record_reset)(mbedtls_ssl_context *ssl); 803 extern int (*mbedtls_ssl_hw_record_write)(mbedtls_ssl_context *ssl); 804 extern int (*mbedtls_ssl_hw_record_read)(mbedtls_ssl_context *ssl); 805 extern int (*mbedtls_ssl_hw_record_finish)(mbedtls_ssl_context *ssl); 806 #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */ 807 808 /** 809 * \brief Returns the list of ciphersuites supported by the SSL/TLS module. 810 * 811 * \return a statically allocated array of ciphersuites, the last 812 * entry is 0. 813 */ 814 const int *mbedtls_ssl_list_ciphersuites( void ); 815 816 /** 817 * \brief Return the name of the ciphersuite associated with the 818 * given ID 819 * 820 * \param ciphersuite_id SSL ciphersuite ID 821 * 822 * \return a string containing the ciphersuite name 823 */ 824 const char *mbedtls_ssl_get_ciphersuite_name( const int ciphersuite_id ); 825 826 /** 827 * \brief Return the ID of the ciphersuite associated with the 828 * given name 829 * 830 * \param ciphersuite_name SSL ciphersuite name 831 * 832 * \return the ID with the ciphersuite or 0 if not found 833 */ 834 int mbedtls_ssl_get_ciphersuite_id( const char *ciphersuite_name ); 835 836 /** 837 * \brief Initialize an SSL context 838 * Just makes the context ready for mbedtls_ssl_setup() or 839 * mbedtls_ssl_free() 840 * 841 * \param ssl SSL context 842 */ 843 void mbedtls_ssl_init( mbedtls_ssl_context *ssl ); 844 845 /** 846 * \brief Set up an SSL context for use 847 * 848 * \note No copy of the configuration context is made, it can be 849 * shared by many mbedtls_ssl_context structures. 850 * 851 * \warning Modifying the conf structure after is has been used in this 852 * function is unsupported! 853 * 854 * \param ssl SSL context 855 * \param conf SSL configuration to use 856 * 857 * \return 0 if successful, or MBEDTLS_ERR_SSL_ALLOC_FAILED if 858 * memory allocation failed 859 */ 860 int mbedtls_ssl_setup( mbedtls_ssl_context *ssl, 861 const mbedtls_ssl_config *conf ); 862 863 /** 864 * \brief Reset an already initialized SSL context for re-use 865 * while retaining application-set variables, function 866 * pointers and data. 867 * 868 * \param ssl SSL context 869 * \return 0 if successful, or POLASSL_ERR_SSL_MALLOC_FAILED, 870 MBEDTLS_ERR_SSL_HW_ACCEL_FAILED or 871 * MBEDTLS_ERR_SSL_COMPRESSION_FAILED 872 */ 873 int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl ); 874 875 /** 876 * \brief Set the current endpoint type 877 * 878 * \param conf SSL configuration 879 * \param endpoint must be MBEDTLS_SSL_IS_CLIENT or MBEDTLS_SSL_IS_SERVER 880 */ 881 void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint ); 882 883 /** 884 * \brief Set the transport type (TLS or DTLS). 885 * Default: TLS 886 * 887 * \note For DTLS, you must either provide a recv callback that 888 * doesn't block, or one that handles timeouts, see 889 * \c mbedtls_ssl_set_bio(). You also need to provide timer 890 * callbacks with \c mbedtls_ssl_set_timer_cb(). 891 * 892 * \param conf SSL configuration 893 * \param transport transport type: 894 * MBEDTLS_SSL_TRANSPORT_STREAM for TLS, 895 * MBEDTLS_SSL_TRANSPORT_DATAGRAM for DTLS. 896 */ 897 void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport ); 898 899 /** 900 * \brief Set the certificate verification mode 901 * Default: NONE on server, REQUIRED on client 902 * 903 * \param conf SSL configuration 904 * \param authmode can be: 905 * 906 * MBEDTLS_SSL_VERIFY_NONE: peer certificate is not checked 907 * (default on server) 908 * (insecure on client) 909 * 910 * MBEDTLS_SSL_VERIFY_OPTIONAL: peer certificate is checked, however the 911 * handshake continues even if verification failed; 912 * mbedtls_ssl_get_verify_result() can be called after the 913 * handshake is complete. 914 * 915 * MBEDTLS_SSL_VERIFY_REQUIRED: peer *must* present a valid certificate, 916 * handshake is aborted if verification failed. 917 * 918 * \note On client, MBEDTLS_SSL_VERIFY_REQUIRED is the recommended mode. 919 * With MBEDTLS_SSL_VERIFY_OPTIONAL, the user needs to call mbedtls_ssl_get_verify_result() at 920 * the right time(s), which may not be obvious, while REQUIRED always perform 921 * the verification as soon as possible. For example, REQUIRED was protecting 922 * against the "triple handshake" attack even before it was found. 923 */ 924 void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode ); 925 926 #if defined(MBEDTLS_X509_CRT_PARSE_C) 927 /** 928 * \brief Set the verification callback (Optional). 929 * 930 * If set, the verify callback is called for each 931 * certificate in the chain. For implementation 932 * information, please see \c x509parse_verify() 933 * 934 * \param conf SSL configuration 935 * \param f_vrfy verification function 936 * \param p_vrfy verification parameter 937 */ 938 void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf, 939 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), 940 void *p_vrfy ); 941 #endif /* MBEDTLS_X509_CRT_PARSE_C */ 942 943 /** 944 * \brief Set the random number generator callback 945 * 946 * \param conf SSL configuration 947 * \param f_rng RNG function 948 * \param p_rng RNG parameter 949 */ 950 void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf, 951 int (*f_rng)(void *, unsigned char *, size_t), 952 void *p_rng ); 953 954 /** 955 * \brief Set the debug callback 956 * 957 * The callback has the following argument: 958 * void * opaque context for the callback 959 * int debug level 960 * const char * file name 961 * int line number 962 * const char * message 963 * 964 * \param conf SSL configuration 965 * \param f_dbg debug function 966 * \param p_dbg debug parameter 967 */ 968 void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf, 969 void (*f_dbg)(void *, int, const char *, int, const char *), 970 void *p_dbg ); 971 972 /** 973 * \brief Set the underlying BIO callbacks for write, read and 974 * read-with-timeout. 975 * 976 * \param ssl SSL context 977 * \param p_bio parameter (context) shared by BIO callbacks 978 * \param f_send write callback 979 * \param f_recv read callback 980 * \param f_recv_timeout blocking read callback with timeout. 981 * The last argument is the timeout in milliseconds, 982 * 0 means no timeout (block forever until a message comes) 983 * 984 * \note One of f_recv or f_recv_timeout can be NULL, in which case 985 * the other is used. If both are non-NULL, f_recv_timeout is 986 * used and f_recv is ignored (as if it were NULL). 987 * 988 * \note The two most common use cases are: 989 * - non-blocking I/O, f_recv != NULL, f_recv_timeout == NULL 990 * - blocking I/O, f_recv == NULL, f_recv_timout != NULL 991 * 992 * \note For DTLS, you need to provide either a non-NULL 993 * f_recv_timeout callback, or a f_recv that doesn't block. 994 */ 995 void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl, 996 void *p_bio, 997 int (*f_send)(void *, const unsigned char *, size_t), 998 int (*f_recv)(void *, unsigned char *, size_t), 999 int (*f_recv_timeout)(void *, unsigned char *, size_t, uint32_t) ); 1000 1001 /** 1002 * \brief Set the timeout period for mbedtls_ssl_read() 1003 * (Default: no timeout.) 1004 * 1005 * \param conf SSL configuration context 1006 * \param timeout Timeout value in milliseconds. 1007 * Use 0 for no timeout (default). 1008 * 1009 * \note With blocking I/O, this will only work if a non-NULL 1010 * \c f_recv_timeout was set with \c mbedtls_ssl_set_bio(). 1011 * With non-blocking I/O, this will only work if timer 1012 * callbacks were set with \c mbedtls_ssl_set_timer_cb(). 1013 * 1014 * \note With non-blocking I/O, you may also skip this function 1015 * altogether and handle timeouts at the application layer. 1016 */ 1017 void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout ); 1018 1019 /** 1020 * \brief Set the timer callbacks 1021 * (Mandatory for DTLS.) 1022 * 1023 * \param ssl SSL context 1024 * \param p_timer parameter (context) shared by timer callback 1025 * \param f_set_timer set timer callback 1026 * Accepts an intermediate and a final delay in milliseconcs 1027 * If the final delay is 0, cancels the running timer. 1028 * \param f_get_timer get timer callback. Must return: 1029 * -1 if cancelled 1030 * 0 if none of the delays is expired 1031 * 1 if the intermediate delay only is expired 1032 * 2 if the final delay is expired 1033 */ 1034 void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl, 1035 void *p_timer, 1036 void (*f_set_timer)(void *, uint32_t int_ms, uint32_t fin_ms), 1037 int (*f_get_timer)(void *) ); 1038 1039 /** 1040 * \brief Callback type: generate and write session ticket 1041 * 1042 * \note This describes what a callback implementation should do. 1043 * This callback should generate and encrypted and 1044 * authenticated ticket for the session and write it to the 1045 * output buffer. Here, ticket means the opaque ticket part 1046 * of the NewSessionTicket structure of RFC 5077. 1047 * 1048 * \param p_ticket Context for the callback 1049 * \param session SSL session to bo written in the ticket 1050 * \param start Start of the outpur buffer 1051 * \param end End of the output buffer 1052 * \param tlen On exit, holds the length written 1053 * \param lifetime On exit, holds the lifetime of the ticket in seconds 1054 * 1055 * \return 0 if successful, or 1056 * a specific MBEDTLS_ERR_XXX code. 1057 */ 1058 typedef int mbedtls_ssl_ticket_write_t( void *p_ticket, 1059 const mbedtls_ssl_session *session, 1060 unsigned char *start, 1061 const unsigned char *end, 1062 size_t *tlen, 1063 uint32_t *lifetime ); 1064 1065 #if defined(MBEDTLS_SSL_EXPORT_KEYS) 1066 /** 1067 * \brief Callback type: Export key block and master secret 1068 * 1069 * \note This is required for certain uses of TLS, e.g. EAP-TLS 1070 * (RFC 5216) and Thread. The key pointers are ephemeral and 1071 * therefore must not be stored. The master secret and keys 1072 * should not be used directly except as an input to a key 1073 * derivation function. 1074 * 1075 * \param p_expkey Context for the callback 1076 * \param ms Pointer to master secret (fixed length: 48 bytes) 1077 * \param kb Pointer to key block, see RFC 5246 section 6.3 1078 * (variable length: 2 * maclen + 2 * keylen + 2 * ivlen). 1079 * \param maclen MAC length 1080 * \param keylen Key length 1081 * \param ivlen IV length 1082 * 1083 * \return 0 if successful, or 1084 * a specific MBEDTLS_ERR_XXX code. 1085 */ 1086 typedef int mbedtls_ssl_export_keys_t( void *p_expkey, 1087 const unsigned char *ms, 1088 const unsigned char *kb, 1089 size_t maclen, 1090 size_t keylen, 1091 size_t ivlen ); 1092 #endif /* MBEDTLS_SSL_EXPORT_KEYS */ 1093 1094 /** 1095 * \brief Callback type: parse and load session ticket 1096 * 1097 * \note This describes what a callback implementation should do. 1098 * This callback should parse a session ticket as generated 1099 * by the corresponding mbedtls_ssl_ticket_write_t function, 1100 * and, if the ticket is authentic and valid, load the 1101 * session. 1102 * 1103 * \note The implementation is allowed to modify the first len 1104 * bytes of the input buffer, eg to use it as a temporary 1105 * area for the decrypted ticket contents. 1106 * 1107 * \param p_ticket Context for the callback 1108 * \param session SSL session to be loaded 1109 * \param buf Start of the buffer containing the ticket 1110 * \param len Length of the ticket. 1111 * 1112 * \return 0 if successful, or 1113 * MBEDTLS_ERR_SSL_INVALID_MAC if not authentic, or 1114 * MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED if expired, or 1115 * any other non-zero code for other failures. 1116 */ 1117 typedef int mbedtls_ssl_ticket_parse_t( void *p_ticket, 1118 mbedtls_ssl_session *session, 1119 unsigned char *buf, 1120 size_t len ); 1121 1122 #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_SRV_C) 1123 /** 1124 * \brief Configure SSL session ticket callbacks (server only). 1125 * (Default: none.) 1126 * 1127 * \note On server, session tickets are enabled by providing 1128 * non-NULL callbacks. 1129 * 1130 * \note On client, use \c mbedtls_ssl_conf_session_tickets(). 1131 * 1132 * \param conf SSL configuration context 1133 * \param f_ticket_write Callback for writing a ticket 1134 * \param f_ticket_parse Callback for parsing a ticket 1135 * \param p_ticket Context shared by the two callbacks 1136 */ 1137 void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf, 1138 mbedtls_ssl_ticket_write_t *f_ticket_write, 1139 mbedtls_ssl_ticket_parse_t *f_ticket_parse, 1140 void *p_ticket ); 1141 #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_SRV_C */ 1142 1143 #if defined(MBEDTLS_SSL_EXPORT_KEYS) 1144 /** 1145 * \brief Configure key export callback. 1146 * (Default: none.) 1147 * 1148 * \note See \c mbedtls_ssl_export_keys_t. 1149 * 1150 * \param conf SSL configuration context 1151 * \param f_export_keys Callback for exporting keys 1152 * \param p_export_keys Context for the callback 1153 */ 1154 void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf, 1155 mbedtls_ssl_export_keys_t *f_export_keys, 1156 void *p_export_keys ); 1157 #endif /* MBEDTLS_SSL_EXPORT_KEYS */ 1158 1159 /** 1160 * \brief Callback type: generate a cookie 1161 * 1162 * \param ctx Context for the callback 1163 * \param p Buffer to write to, 1164 * must be updated to point right after the cookie 1165 * \param end Pointer to one past the end of the output buffer 1166 * \param info Client ID info that was passed to 1167 * \c mbedtls_ssl_set_client_transport_id() 1168 * \param ilen Length of info in bytes 1169 * 1170 * \return The callback must return 0 on success, 1171 * or a negative error code. 1172 */ 1173 typedef int mbedtls_ssl_cookie_write_t( void *ctx, 1174 unsigned char **p, unsigned char *end, 1175 const unsigned char *info, size_t ilen ); 1176 1177 /** 1178 * \brief Callback type: verify a cookie 1179 * 1180 * \param ctx Context for the callback 1181 * \param cookie Cookie to verify 1182 * \param clen Length of cookie 1183 * \param info Client ID info that was passed to 1184 * \c mbedtls_ssl_set_client_transport_id() 1185 * \param ilen Length of info in bytes 1186 * 1187 * \return The callback must return 0 if cookie is valid, 1188 * or a negative error code. 1189 */ 1190 typedef int mbedtls_ssl_cookie_check_t( void *ctx, 1191 const unsigned char *cookie, size_t clen, 1192 const unsigned char *info, size_t ilen ); 1193 1194 #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) 1195 /** 1196 * \brief Register callbacks for DTLS cookies 1197 * (Server only. DTLS only.) 1198 * 1199 * Default: dummy callbacks that fail, in order to force you to 1200 * register working callbacks (and initialize their context). 1201 * 1202 * To disable HelloVerifyRequest, register NULL callbacks. 1203 * 1204 * \warning Disabling hello verification allows your server to be used 1205 * for amplification in DoS attacks against other hosts. 1206 * Only disable if you known this can't happen in your 1207 * particular environment. 1208 * 1209 * \note See comments on \c mbedtls_ssl_handshake() about handling 1210 * the MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED that is expected 1211 * on the first handshake attempt when this is enabled. 1212 * 1213 * \note This is also necessary to handle client reconnection from 1214 * the same port as described in RFC 6347 section 4.2.8 (only 1215 * the variant with cookies is supported currently). See 1216 * comments on \c mbedtls_ssl_read() for details. 1217 * 1218 * \param conf SSL configuration 1219 * \param f_cookie_write Cookie write callback 1220 * \param f_cookie_check Cookie check callback 1221 * \param p_cookie Context for both callbacks 1222 */ 1223 void mbedtls_ssl_conf_dtls_cookies( mbedtls_ssl_config *conf, 1224 mbedtls_ssl_cookie_write_t *f_cookie_write, 1225 mbedtls_ssl_cookie_check_t *f_cookie_check, 1226 void *p_cookie ); 1227 1228 /** 1229 * \brief Set client's transport-level identification info. 1230 * (Server only. DTLS only.) 1231 * 1232 * This is usually the IP address (and port), but could be 1233 * anything identify the client depending on the underlying 1234 * network stack. Used for HelloVerifyRequest with DTLS. 1235 * This is *not* used to route the actual packets. 1236 * 1237 * \param ssl SSL context 1238 * \param info Transport-level info identifying the client (eg IP + port) 1239 * \param ilen Length of info in bytes 1240 * 1241 * \note An internal copy is made, so the info buffer can be reused. 1242 * 1243 * \return 0 on success, 1244 * MBEDTLS_ERR_SSL_BAD_INPUT_DATA if used on client, 1245 * MBEDTLS_ERR_SSL_ALLOC_FAILED if out of memory. 1246 */ 1247 int mbedtls_ssl_set_client_transport_id( mbedtls_ssl_context *ssl, 1248 const unsigned char *info, 1249 size_t ilen ); 1250 1251 #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */ 1252 1253 #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) 1254 /** 1255 * \brief Enable or disable anti-replay protection for DTLS. 1256 * (DTLS only, no effect on TLS.) 1257 * Default: enabled. 1258 * 1259 * \param conf SSL configuration 1260 * \param mode MBEDTLS_SSL_ANTI_REPLAY_ENABLED or MBEDTLS_SSL_ANTI_REPLAY_DISABLED. 1261 * 1262 * \warning Disabling this is a security risk unless the application 1263 * protocol handles duplicated packets in a safe way. You 1264 * should not disable this without careful consideration. 1265 * However, if your application already detects duplicated 1266 * packets and needs information about them to adjust its 1267 * transmission strategy, then you'll want to disable this. 1268 */ 1269 void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode ); 1270 #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ 1271 1272 #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) 1273 /** 1274 * \brief Set a limit on the number of records with a bad MAC 1275 * before terminating the connection. 1276 * (DTLS only, no effect on TLS.) 1277 * Default: 0 (disabled). 1278 * 1279 * \param conf SSL configuration 1280 * \param limit Limit, or 0 to disable. 1281 * 1282 * \note If the limit is N, then the connection is terminated when 1283 * the Nth non-authentic record is seen. 1284 * 1285 * \note Records with an invalid header are not counted, only the 1286 * ones going through the authentication-decryption phase. 1287 * 1288 * \note This is a security trade-off related to the fact that it's 1289 * often relatively easy for an active attacker ot inject UDP 1290 * datagrams. On one hand, setting a low limit here makes it 1291 * easier for such an attacker to forcibly terminated a 1292 * connection. On the other hand, a high limit or no limit 1293 * might make us waste resources checking authentication on 1294 * many bogus packets. 1295 */ 1296 void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit ); 1297 #endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */ 1298 1299 #if defined(MBEDTLS_SSL_PROTO_DTLS) 1300 /** 1301 * \brief Set retransmit timeout values for the DTLS handshale. 1302 * (DTLS only, no effect on TLS.) 1303 * 1304 * \param conf SSL configuration 1305 * \param min Initial timeout value in milliseconds. 1306 * Default: 1000 (1 second). 1307 * \param max Maximum timeout value in milliseconds. 1308 * Default: 60000 (60 seconds). 1309 * 1310 * \note Default values are from RFC 6347 section 4.2.4.1. 1311 * 1312 * \note Higher values for initial timeout may increase average 1313 * handshake latency. Lower values may increase the risk of 1314 * network congestion by causing more retransmissions. 1315 */ 1316 void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf, uint32_t min, uint32_t max ); 1317 #endif /* MBEDTLS_SSL_PROTO_DTLS */ 1318 1319 #if defined(MBEDTLS_SSL_SRV_C) 1320 /** 1321 * \brief Set the session cache callbacks (server-side only) 1322 * If not set, no session resuming is done (except if session 1323 * tickets are enabled too). 1324 * 1325 * The session cache has the responsibility to check for stale 1326 * entries based on timeout. See RFC 5246 for recommendations. 1327 * 1328 * Warning: session.peer_cert is cleared by the SSL/TLS layer on 1329 * connection shutdown, so do not cache the pointer! Either set 1330 * it to NULL or make a full copy of the certificate. 1331 * 1332 * The get callback is called once during the initial handshake 1333 * to enable session resuming. The get function has the 1334 * following parameters: (void *parameter, mbedtls_ssl_session *session) 1335 * If a valid entry is found, it should fill the master of 1336 * the session object with the cached values and return 0, 1337 * return 1 otherwise. Optionally peer_cert can be set as well 1338 * if it is properly present in cache entry. 1339 * 1340 * The set callback is called once during the initial handshake 1341 * to enable session resuming after the entire handshake has 1342 * been finished. The set function has the following parameters: 1343 * (void *parameter, const mbedtls_ssl_session *session). The function 1344 * should create a cache entry for future retrieval based on 1345 * the data in the session structure and should keep in mind 1346 * that the mbedtls_ssl_session object presented (and all its referenced 1347 * data) is cleared by the SSL/TLS layer when the connection is 1348 * terminated. It is recommended to add metadata to determine if 1349 * an entry is still valid in the future. Return 0 if 1350 * successfully cached, return 1 otherwise. 1351 * 1352 * \param conf SSL configuration 1353 * \param p_cache parmater (context) for both callbacks 1354 * \param f_get_cache session get callback 1355 * \param f_set_cache session set callback 1356 */ 1357 void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf, 1358 void *p_cache, 1359 int (*f_get_cache)(void *, mbedtls_ssl_session *), 1360 int (*f_set_cache)(void *, const mbedtls_ssl_session *) ); 1361 #endif /* MBEDTLS_SSL_SRV_C */ 1362 1363 #if defined(MBEDTLS_SSL_CLI_C) 1364 /** 1365 * \brief Request resumption of session (client-side only) 1366 * Session data is copied from presented session structure. 1367 * 1368 * \param ssl SSL context 1369 * \param session session context 1370 * 1371 * \return 0 if successful, 1372 * MBEDTLS_ERR_SSL_ALLOC_FAILED if memory allocation failed, 1373 * MBEDTLS_ERR_SSL_BAD_INPUT_DATA if used server-side or 1374 * arguments are otherwise invalid 1375 * 1376 * \sa mbedtls_ssl_get_session() 1377 */ 1378 int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session ); 1379 #endif /* MBEDTLS_SSL_CLI_C */ 1380 1381 /** 1382 * \brief Set the list of allowed ciphersuites and the preference 1383 * order. First in the list has the highest preference. 1384 * (Overrides all version specific lists) 1385 * 1386 * The ciphersuites array is not copied, and must remain 1387 * valid for the lifetime of the ssl_config. 1388 * 1389 * Note: The server uses its own preferences 1390 * over the preference of the client unless 1391 * MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE is defined! 1392 * 1393 * \param conf SSL configuration 1394 * \param ciphersuites 0-terminated list of allowed ciphersuites 1395 */ 1396 void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf, 1397 const int *ciphersuites ); 1398 1399 /** 1400 * \brief Set the list of allowed ciphersuites and the 1401 * preference order for a specific version of the protocol. 1402 * (Only useful on the server side) 1403 * 1404 * The ciphersuites array is not copied, and must remain 1405 * valid for the lifetime of the ssl_config. 1406 * 1407 * \param conf SSL configuration 1408 * \param ciphersuites 0-terminated list of allowed ciphersuites 1409 * \param major Major version number (only MBEDTLS_SSL_MAJOR_VERSION_3 1410 * supported) 1411 * \param minor Minor version number (MBEDTLS_SSL_MINOR_VERSION_0, 1412 * MBEDTLS_SSL_MINOR_VERSION_1 and MBEDTLS_SSL_MINOR_VERSION_2, 1413 * MBEDTLS_SSL_MINOR_VERSION_3 supported) 1414 * 1415 * \note With DTLS, use MBEDTLS_SSL_MINOR_VERSION_2 for DTLS 1.0 1416 * and MBEDTLS_SSL_MINOR_VERSION_3 for DTLS 1.2 1417 */ 1418 void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf, 1419 const int *ciphersuites, 1420 int major, int minor ); 1421 1422 #if defined(MBEDTLS_X509_CRT_PARSE_C) 1423 /** 1424 * \brief Set the X.509 security profile used for verification 1425 * 1426 * \note The restrictions are enforced for all certificates in the 1427 * chain. However, signatures in the handshake are not covered 1428 * by this setting but by \b mbedtls_ssl_conf_sig_hashes(). 1429 * 1430 * \param conf SSL configuration 1431 * \param profile Profile to use 1432 */ 1433 void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf, 1434 const mbedtls_x509_crt_profile *profile ); 1435 1436 /** 1437 * \brief Set the data required to verify peer certificate 1438 * 1439 * \param conf SSL configuration 1440 * \param ca_chain trusted CA chain (meaning all fully trusted top-level CAs) 1441 * \param ca_crl trusted CA CRLs 1442 */ 1443 void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf, 1444 mbedtls_x509_crt *ca_chain, 1445 mbedtls_x509_crl *ca_crl ); 1446 1447 /** 1448 * \brief Set own certificate chain and private key 1449 * 1450 * \note own_cert should contain in order from the bottom up your 1451 * certificate chain. The top certificate (self-signed) 1452 * can be omitted. 1453 * 1454 * \note On server, this function can be called multiple times to 1455 * provision more than one cert/key pair (eg one ECDSA, one 1456 * RSA with SHA-256, one RSA with SHA-1). An adequate 1457 * certificate will be selected according to the client's 1458 * advertised capabilities. In case mutliple certificates are 1459 * adequate, preference is given to the one set by the first 1460 * call to this function, then second, etc. 1461 * 1462 * \note On client, only the first call has any effect. 1463 * 1464 * \param conf SSL configuration 1465 * \param own_cert own public certificate chain 1466 * \param pk_key own private key 1467 * 1468 * \return 0 on success or MBEDTLS_ERR_SSL_ALLOC_FAILED 1469 */ 1470 int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf, 1471 mbedtls_x509_crt *own_cert, 1472 mbedtls_pk_context *pk_key ); 1473 #endif /* MBEDTLS_X509_CRT_PARSE_C */ 1474 1475 #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) 1476 /** 1477 * \brief Set the Pre Shared Key (PSK) and the expected identity name 1478 * 1479 * \note This is mainly useful for clients. Servers will usually 1480 * want to use \c mbedtls_ssl_conf_psk_cb() instead. 1481 * 1482 * \param conf SSL configuration 1483 * \param psk pointer to the pre-shared key 1484 * \param psk_len pre-shared key length 1485 * \param psk_identity pointer to the pre-shared key identity 1486 * \param psk_identity_len identity key length 1487 * 1488 * \return 0 if successful or MBEDTLS_ERR_SSL_ALLOC_FAILED 1489 */ 1490 int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf, 1491 const unsigned char *psk, size_t psk_len, 1492 const unsigned char *psk_identity, size_t psk_identity_len ); 1493 1494 1495 /** 1496 * \brief Set the Pre Shared Key (PSK) for the current handshake 1497 * 1498 * \note This should only be called inside the PSK callback, 1499 * ie the function passed to \c mbedtls_ssl_conf_psk_cb(). 1500 * 1501 * \param ssl SSL context 1502 * \param psk pointer to the pre-shared key 1503 * \param psk_len pre-shared key length 1504 * 1505 * \return 0 if successful or MBEDTLS_ERR_SSL_ALLOC_FAILED 1506 */ 1507 int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl, 1508 const unsigned char *psk, size_t psk_len ); 1509 1510 /** 1511 * \brief Set the PSK callback (server-side only). 1512 * 1513 * If set, the PSK callback is called for each 1514 * handshake where a PSK ciphersuite was negotiated. 1515 * The caller provides the identity received and wants to 1516 * receive the actual PSK data and length. 1517 * 1518 * The callback has the following parameters: (void *parameter, 1519 * mbedtls_ssl_context *ssl, const unsigned char *psk_identity, 1520 * size_t identity_len) 1521 * If a valid PSK identity is found, the callback should use 1522 * \c mbedtls_ssl_set_hs_psk() on the ssl context to set the 1523 * correct PSK and return 0. 1524 * Any other return value will result in a denied PSK identity. 1525 * 1526 * \note If you set a PSK callback using this function, then you 1527 * don't need to set a PSK key and identity using 1528 * \c mbedtls_ssl_conf_psk(). 1529 * 1530 * \param conf SSL configuration 1531 * \param f_psk PSK identity function 1532 * \param p_psk PSK identity parameter 1533 */ 1534 void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf, 1535 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *, 1536 size_t), 1537 void *p_psk ); 1538 #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */ 1539 1540 #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) 1541 /** 1542 * \brief Set the Diffie-Hellman public P and G values, 1543 * read as hexadecimal strings (server-side only) 1544 * (Default: MBEDTLS_DHM_RFC5114_MODP_2048_[PG]) 1545 * 1546 * \param conf SSL configuration 1547 * \param dhm_P Diffie-Hellman-Merkle modulus 1548 * \param dhm_G Diffie-Hellman-Merkle generator 1549 * 1550 * \return 0 if successful 1551 */ 1552 int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G ); 1553 1554 /** 1555 * \brief Set the Diffie-Hellman public P and G values, 1556 * read from existing context (server-side only) 1557 * 1558 * \param conf SSL configuration 1559 * \param dhm_ctx Diffie-Hellman-Merkle context 1560 * 1561 * \return 0 if successful 1562 */ 1563 int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx ); 1564 #endif /* MBEDTLS_DHM_C && defined(MBEDTLS_SSL_SRV_C) */ 1565 1566 #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) 1567 /** 1568 * \brief Set the minimum length for Diffie-Hellman parameters. 1569 * (Client-side only.) 1570 * (Default: 1024 bits.) 1571 * 1572 * \param conf SSL configuration 1573 * \param bitlen Minimum bit length of the DHM prime 1574 */ 1575 void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf, 1576 unsigned int bitlen ); 1577 #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */ 1578 1579 #if defined(MBEDTLS_ECP_C) 1580 /** 1581 * \brief Set the allowed curves in order of preference. 1582 * (Default: all defined curves.) 1583 * 1584 * On server: this only affects selection of the ECDHE curve; 1585 * the curves used for ECDH and ECDSA are determined by the 1586 * list of available certificates instead. 1587 * 1588 * On client: this affects the list of curves offered for any 1589 * use. The server can override our preference order. 1590 * 1591 * Both sides: limits the set of curves accepted for use in 1592 * ECDHE and in the peer's end-entity certificate. 1593 * 1594 * \note This has no influence on which curves are allowed inside the 1595 * certificate chains, see \c mbedtls_ssl_conf_cert_profile() 1596 * for that. For the end-entity certificate however, the key 1597 * will be accepted only if it is allowed both by this list 1598 * and by the cert profile. 1599 * 1600 * \note This list should be ordered by decreasing preference 1601 * (preferred curve first). 1602 * 1603 * \param conf SSL configuration 1604 * \param curves Ordered list of allowed curves, 1605 * terminated by MBEDTLS_ECP_DP_NONE. 1606 */ 1607 void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf, 1608 const mbedtls_ecp_group_id *curves ); 1609 #endif /* MBEDTLS_ECP_C */ 1610 1611 #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) 1612 /** 1613 * \brief Set the allowed hashes for signatures during the handshake. 1614 * (Default: all available hashes except MD5.) 1615 * 1616 * \note This only affects which hashes are offered and can be used 1617 * for signatures during the handshake. Hashes for message 1618 * authentication and the TLS PRF are controlled by the 1619 * ciphersuite, see \c mbedtls_ssl_conf_ciphersuites(). Hashes 1620 * used for certificate signature are controlled by the 1621 * verification profile, see \c mbedtls_ssl_conf_cert_profile(). 1622 * 1623 * \note This list should be ordered by decreasing preference 1624 * (preferred hash first). 1625 * 1626 * \param conf SSL configuration 1627 * \param hashes Ordered list of allowed signature hashes, 1628 * terminated by \c MBEDTLS_MD_NONE. 1629 */ 1630 void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf, 1631 const int *hashes ); 1632 #endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ 1633 1634 #if defined(MBEDTLS_X509_CRT_PARSE_C) 1635 /** 1636 * \brief Set hostname for ServerName TLS extension 1637 * (client-side only) 1638 * 1639 * 1640 * \param ssl SSL context 1641 * \param hostname the server hostname 1642 * 1643 * \return 0 if successful or MBEDTLS_ERR_SSL_ALLOC_FAILED 1644 */ 1645 int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname ); 1646 #endif /* MBEDTLS_X509_CRT_PARSE_C */ 1647 1648 #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) 1649 /** 1650 * \brief Set own certificate and key for the current handshake 1651 * 1652 * \note Same as \c mbedtls_ssl_conf_own_cert() but for use within 1653 * the SNI callback. 1654 * 1655 * \param ssl SSL context 1656 * \param own_cert own public certificate chain 1657 * \param pk_key own private key 1658 * 1659 * \return 0 on success or MBEDTLS_ERR_SSL_ALLOC_FAILED 1660 */ 1661 int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl, 1662 mbedtls_x509_crt *own_cert, 1663 mbedtls_pk_context *pk_key ); 1664 1665 /** 1666 * \brief Set the data required to verify peer certificate for the 1667 * current handshake 1668 * 1669 * \note Same as \c mbedtls_ssl_conf_ca_chain() but for use within 1670 * the SNI callback. 1671 * 1672 * \param ssl SSL context 1673 * \param ca_chain trusted CA chain (meaning all fully trusted top-level CAs) 1674 * \param ca_crl trusted CA CRLs 1675 */ 1676 void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl, 1677 mbedtls_x509_crt *ca_chain, 1678 mbedtls_x509_crl *ca_crl ); 1679 1680 /** 1681 * \brief Set authmode for the current handshake. 1682 * 1683 * \note Same as \c mbedtls_ssl_conf_authmode() but for use within 1684 * the SNI callback. 1685 * 1686 * \param ssl SSL context 1687 * \param authmode MBEDTLS_SSL_VERIFY_NONE, MBEDTLS_SSL_VERIFY_OPTIONAL or 1688 * MBEDTLS_SSL_VERIFY_REQUIRED 1689 */ 1690 void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl, 1691 int authmode ); 1692 1693 /** 1694 * \brief Set server side ServerName TLS extension callback 1695 * (optional, server-side only). 1696 * 1697 * If set, the ServerName callback is called whenever the 1698 * server receives a ServerName TLS extension from the client 1699 * during a handshake. The ServerName callback has the 1700 * following parameters: (void *parameter, mbedtls_ssl_context *ssl, 1701 * const unsigned char *hostname, size_t len). If a suitable 1702 * certificate is found, the callback must set the 1703 * certificate(s) and key(s) to use with \c 1704 * mbedtls_ssl_set_hs_own_cert() (can be called repeatedly), 1705 * and may optionally adjust the CA and associated CRL with \c 1706 * mbedtls_ssl_set_hs_ca_chain() as well as the client 1707 * authentication mode with \c mbedtls_ssl_set_hs_authmode(), 1708 * then must return 0. If no matching name is found, the 1709 * callback must either set a default cert, or 1710 * return non-zero to abort the handshake at this point. 1711 * 1712 * \param conf SSL configuration 1713 * \param f_sni verification function 1714 * \param p_sni verification parameter 1715 */ 1716 void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf, 1717 int (*f_sni)(void *, mbedtls_ssl_context *, const unsigned char *, 1718 size_t), 1719 void *p_sni ); 1720 #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ 1721 1722 #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) 1723 /** 1724 * \brief Set the EC J-PAKE password for current handshake. 1725 * 1726 * \note An internal copy is made, and destroyed as soon as the 1727 * handshake is completed, or when the SSL context is reset or 1728 * freed. 1729 * 1730 * \note The SSL context needs to be already set up. The right place 1731 * to call this function is between \c mbedtls_ssl_setup() or 1732 * \c mbedtls_ssl_reset() and \c mbedtls_ssl_handshake(). 1733 * 1734 * \param ssl SSL context 1735 * \param pw EC J-PAKE password (pre-shared secret) 1736 * \param pw_len length of pw in bytes 1737 * 1738 * \return 0 on success, or a negative error code. 1739 */ 1740 int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl, 1741 const unsigned char *pw, 1742 size_t pw_len ); 1743 #endif /*MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ 1744 1745 #if defined(MBEDTLS_SSL_ALPN) 1746 /** 1747 * \brief Set the supported Application Layer Protocols. 1748 * 1749 * \param conf SSL configuration 1750 * \param protos NULL-terminated list of supported protocols, 1751 * in decreasing preference order. 1752 * 1753 * \return 0 on success, or MBEDTLS_ERR_SSL_BAD_INPUT_DATA. 1754 */ 1755 int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos ); 1756 1757 /** 1758 * \brief Get the name of the negotiated Application Layer Protocol. 1759 * This function should be called after the handshake is 1760 * completed. 1761 * 1762 * \param ssl SSL context 1763 * 1764 * \return Protcol name, or NULL if no protocol was negotiated. 1765 */ 1766 const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl ); 1767 #endif /* MBEDTLS_SSL_ALPN */ 1768 1769 /** 1770 * \brief Set the maximum supported version sent from the client side 1771 * and/or accepted at the server side 1772 * (Default: MBEDTLS_SSL_MAX_MAJOR_VERSION, MBEDTLS_SSL_MAX_MINOR_VERSION) 1773 * 1774 * \note This ignores ciphersuites from higher versions. 1775 * 1776 * \note With DTLS, use MBEDTLS_SSL_MINOR_VERSION_2 for DTLS 1.0 and 1777 * MBEDTLS_SSL_MINOR_VERSION_3 for DTLS 1.2 1778 * 1779 * \param conf SSL configuration 1780 * \param major Major version number (only MBEDTLS_SSL_MAJOR_VERSION_3 supported) 1781 * \param minor Minor version number (MBEDTLS_SSL_MINOR_VERSION_0, 1782 * MBEDTLS_SSL_MINOR_VERSION_1 and MBEDTLS_SSL_MINOR_VERSION_2, 1783 * MBEDTLS_SSL_MINOR_VERSION_3 supported) 1784 */ 1785 void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor ); 1786 1787 /** 1788 * \brief Set the minimum accepted SSL/TLS protocol version 1789 * (Default: TLS 1.0) 1790 * 1791 * \note Input outside of the SSL_MAX_XXXXX_VERSION and 1792 * SSL_MIN_XXXXX_VERSION range is ignored. 1793 * 1794 * \note MBEDTLS_SSL_MINOR_VERSION_0 (SSL v3) should be avoided. 1795 * 1796 * \note With DTLS, use MBEDTLS_SSL_MINOR_VERSION_2 for DTLS 1.0 and 1797 * MBEDTLS_SSL_MINOR_VERSION_3 for DTLS 1.2 1798 * 1799 * \param conf SSL configuration 1800 * \param major Major version number (only MBEDTLS_SSL_MAJOR_VERSION_3 supported) 1801 * \param minor Minor version number (MBEDTLS_SSL_MINOR_VERSION_0, 1802 * MBEDTLS_SSL_MINOR_VERSION_1 and MBEDTLS_SSL_MINOR_VERSION_2, 1803 * MBEDTLS_SSL_MINOR_VERSION_3 supported) 1804 */ 1805 void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor ); 1806 1807 #if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C) 1808 /** 1809 * \brief Set the fallback flag (client-side only). 1810 * (Default: MBEDTLS_SSL_IS_NOT_FALLBACK). 1811 * 1812 * \note Set to MBEDTLS_SSL_IS_FALLBACK when preparing a fallback 1813 * connection, that is a connection with max_version set to a 1814 * lower value than the value you're willing to use. Such 1815 * fallback connections are not recommended but are sometimes 1816 * necessary to interoperate with buggy (version-intolerant) 1817 * servers. 1818 * 1819 * \warning You should NOT set this to MBEDTLS_SSL_IS_FALLBACK for 1820 * non-fallback connections! This would appear to work for a 1821 * while, then cause failures when the server is upgraded to 1822 * support a newer TLS version. 1823 * 1824 * \param conf SSL configuration 1825 * \param fallback MBEDTLS_SSL_IS_NOT_FALLBACK or MBEDTLS_SSL_IS_FALLBACK 1826 */ 1827 void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback ); 1828 #endif /* MBEDTLS_SSL_FALLBACK_SCSV && MBEDTLS_SSL_CLI_C */ 1829 1830 #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) 1831 /** 1832 * \brief Enable or disable Encrypt-then-MAC 1833 * (Default: MBEDTLS_SSL_ETM_ENABLED) 1834 * 1835 * \note This should always be enabled, it is a security 1836 * improvement, and should not cause any interoperability 1837 * issue (used only if the peer supports it too). 1838 * 1839 * \param conf SSL configuration 1840 * \param etm MBEDTLS_SSL_ETM_ENABLED or MBEDTLS_SSL_ETM_DISABLED 1841 */ 1842 void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm ); 1843 #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ 1844 1845 #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) 1846 /** 1847 * \brief Enable or disable Extended Master Secret negotiation. 1848 * (Default: MBEDTLS_SSL_EXTENDED_MS_ENABLED) 1849 * 1850 * \note This should always be enabled, it is a security fix to the 1851 * protocol, and should not cause any interoperability issue 1852 * (used only if the peer supports it too). 1853 * 1854 * \param conf SSL configuration 1855 * \param ems MBEDTLS_SSL_EXTENDED_MS_ENABLED or MBEDTLS_SSL_EXTENDED_MS_DISABLED 1856 */ 1857 void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems ); 1858 #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ 1859 1860 #if defined(MBEDTLS_ARC4_C) 1861 /** 1862 * \brief Disable or enable support for RC4 1863 * (Default: MBEDTLS_SSL_ARC4_DISABLED) 1864 * 1865 * \warning Use of RC4 in (D)TLS has been prohibited by RFC ???? 1866 * for security reasons. Use at your own risks. 1867 * 1868 * \note This function will likely be removed in future versions as 1869 * RC4 will then be disabled by default at compile time. 1870 * 1871 * \param conf SSL configuration 1872 * \param arc4 MBEDTLS_SSL_ARC4_ENABLED or MBEDTLS_SSL_ARC4_DISABLED 1873 */ 1874 void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 ); 1875 #endif /* MBEDTLS_ARC4_C */ 1876 1877 #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) 1878 /** 1879 * \brief Set the maximum fragment length to emit and/or negotiate 1880 * (Default: MBEDTLS_SSL_MAX_CONTENT_LEN, usually 2^14 bytes) 1881 * (Server: set maximum fragment length to emit, 1882 * usually negotiated by the client during handshake 1883 * (Client: set maximum fragment length to emit *and* 1884 * negotiate with the server during handshake) 1885 * 1886 * \param conf SSL configuration 1887 * \param mfl_code Code for maximum fragment length (allowed values: 1888 * MBEDTLS_SSL_MAX_FRAG_LEN_512, MBEDTLS_SSL_MAX_FRAG_LEN_1024, 1889 * MBEDTLS_SSL_MAX_FRAG_LEN_2048, MBEDTLS_SSL_MAX_FRAG_LEN_4096) 1890 * 1891 * \return 0 if successful or MBEDTLS_ERR_SSL_BAD_INPUT_DATA 1892 */ 1893 int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code ); 1894 #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ 1895 1896 #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) 1897 /** 1898 * \brief Activate negotiation of truncated HMAC 1899 * (Default: MBEDTLS_SSL_TRUNC_HMAC_DISABLED) 1900 * 1901 * \param conf SSL configuration 1902 * \param truncate Enable or disable (MBEDTLS_SSL_TRUNC_HMAC_ENABLED or 1903 * MBEDTLS_SSL_TRUNC_HMAC_DISABLED) 1904 */ 1905 void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate ); 1906 #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ 1907 1908 #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) 1909 /** 1910 * \brief Enable / Disable 1/n-1 record splitting 1911 * (Default: MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED) 1912 * 1913 * \note Only affects SSLv3 and TLS 1.0, not higher versions. 1914 * Does not affect non-CBC ciphersuites in any version. 1915 * 1916 * \param conf SSL configuration 1917 * \param split MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED or 1918 * MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED 1919 */ 1920 void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split ); 1921 #endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */ 1922 1923 #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) 1924 /** 1925 * \brief Enable / Disable session tickets (client only). 1926 * (Default: MBEDTLS_SSL_SESSION_TICKETS_ENABLED.) 1927 * 1928 * \note On server, use \c mbedtls_ssl_conf_session_tickets_cb(). 1929 * 1930 * \param conf SSL configuration 1931 * \param use_tickets Enable or disable (MBEDTLS_SSL_SESSION_TICKETS_ENABLED or 1932 * MBEDTLS_SSL_SESSION_TICKETS_DISABLED) 1933 */ 1934 void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets ); 1935 #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ 1936 1937 #if defined(MBEDTLS_SSL_RENEGOTIATION) 1938 /** 1939 * \brief Enable / Disable renegotiation support for connection when 1940 * initiated by peer 1941 * (Default: MBEDTLS_SSL_RENEGOTIATION_DISABLED) 1942 * 1943 * \warning It is recommended to always disable renegotation unless you 1944 * know you need it and you know what you're doing. In the 1945 * past, there has been several issues associated with 1946 * renegotiation or a poor understanding of its properties. 1947 * 1948 * \note Server-side, enabling renegotiation also makes the server 1949 * susceptible to a resource DoS by a malicious client. 1950 * 1951 * \param conf SSL configuration 1952 * \param renegotiation Enable or disable (MBEDTLS_SSL_RENEGOTIATION_ENABLED or 1953 * MBEDTLS_SSL_RENEGOTIATION_DISABLED) 1954 */ 1955 void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation ); 1956 #endif /* MBEDTLS_SSL_RENEGOTIATION */ 1957 1958 /** 1959 * \brief Prevent or allow legacy renegotiation. 1960 * (Default: MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION) 1961 * 1962 * MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION allows connections to 1963 * be established even if the peer does not support 1964 * secure renegotiation, but does not allow renegotiation 1965 * to take place if not secure. 1966 * (Interoperable and secure option) 1967 * 1968 * MBEDTLS_SSL_LEGACY_ALLOW_RENEGOTIATION allows renegotiations 1969 * with non-upgraded peers. Allowing legacy renegotiation 1970 * makes the connection vulnerable to specific man in the 1971 * middle attacks. (See RFC 5746) 1972 * (Most interoperable and least secure option) 1973 * 1974 * MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE breaks off connections 1975 * if peer does not support secure renegotiation. Results 1976 * in interoperability issues with non-upgraded peers 1977 * that do not support renegotiation altogether. 1978 * (Most secure option, interoperability issues) 1979 * 1980 * \param conf SSL configuration 1981 * \param allow_legacy Prevent or allow (SSL_NO_LEGACY_RENEGOTIATION, 1982 * SSL_ALLOW_LEGACY_RENEGOTIATION or 1983 * MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE) 1984 */ 1985 void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy ); 1986 1987 #if defined(MBEDTLS_SSL_RENEGOTIATION) 1988 /** 1989 * \brief Enforce renegotiation requests. 1990 * (Default: enforced, max_records = 16) 1991 * 1992 * When we request a renegotiation, the peer can comply or 1993 * ignore the request. This function allows us to decide 1994 * whether to enforce our renegotiation requests by closing 1995 * the connection if the peer doesn't comply. 1996 * 1997 * However, records could already be in transit from the peer 1998 * when the request is emitted. In order to increase 1999 * reliability, we can accept a number of records before the 2000 * expected handshake records. 2001 * 2002 * The optimal value is highly dependent on the specific usage 2003 * scenario. 2004 * 2005 * \note With DTLS and server-initiated renegotiation, the 2006 * HelloRequest is retransmited every time mbedtls_ssl_read() times 2007 * out or receives Application Data, until: 2008 * - max_records records have beens seen, if it is >= 0, or 2009 * - the number of retransmits that would happen during an 2010 * actual handshake has been reached. 2011 * Please remember the request might be lost a few times 2012 * if you consider setting max_records to a really low value. 2013 * 2014 * \warning On client, the grace period can only happen during 2015 * mbedtls_ssl_read(), as opposed to mbedtls_ssl_write() and mbedtls_ssl_renegotiate() 2016 * which always behave as if max_record was 0. The reason is, 2017 * if we receive application data from the server, we need a 2018 * place to write it, which only happens during mbedtls_ssl_read(). 2019 * 2020 * \param conf SSL configuration 2021 * \param max_records Use MBEDTLS_SSL_RENEGOTIATION_NOT_ENFORCED if you don't want to 2022 * enforce renegotiation, or a non-negative value to enforce 2023 * it but allow for a grace period of max_records records. 2024 */ 2025 void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records ); 2026 2027 /** 2028 * \brief Set record counter threshold for periodic renegotiation. 2029 * (Default: 2^64 - 256.) 2030 * 2031 * Renegotiation is automatically triggered when a record 2032 * counter (outgoing or ingoing) crosses the defined 2033 * threshold. The default value is meant to prevent the 2034 * connection from being closed when the counter is about to 2035 * reached its maximal value (it is not allowed to wrap). 2036 * 2037 * Lower values can be used to enforce policies such as "keys 2038 * must be refreshed every N packets with cipher X". 2039 * 2040 * \param conf SSL configuration 2041 * \param period The threshold value: a big-endian 64-bit number. 2042 * Set to 2^64 - 1 to disable periodic renegotiation 2043 */ 2044 void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf, 2045 const unsigned char period[8] ); 2046 #endif /* MBEDTLS_SSL_RENEGOTIATION */ 2047 2048 /** 2049 * \brief Return the number of data bytes available to read 2050 * 2051 * \param ssl SSL context 2052 * 2053 * \return how many bytes are available in the read buffer 2054 */ 2055 size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl ); 2056 2057 /** 2058 * \brief Return the result of the certificate verification 2059 * 2060 * \param ssl SSL context 2061 * 2062 * \return 0 if successful, 2063 * -1 if result is not available (eg because the handshake was 2064 * aborted too early), or 2065 * a combination of BADCERT_xxx and BADCRL_xxx flags, see 2066 * x509.h 2067 */ 2068 uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl ); 2069 2070 /** 2071 * \brief Return the name of the current ciphersuite 2072 * 2073 * \param ssl SSL context 2074 * 2075 * \return a string containing the ciphersuite name 2076 */ 2077 const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl ); 2078 2079 /** 2080 * \brief Return the current SSL version (SSLv3/TLSv1/etc) 2081 * 2082 * \param ssl SSL context 2083 * 2084 * \return a string containing the SSL version 2085 */ 2086 const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl ); 2087 2088 /** 2089 * \brief Return the (maximum) number of bytes added by the record 2090 * layer: header + encryption/MAC overhead (inc. padding) 2091 * 2092 * \param ssl SSL context 2093 * 2094 * \return Current maximum record expansion in bytes, or 2095 * MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE if compression is 2096 * enabled, which makes expansion much less predictable 2097 */ 2098 int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl ); 2099 2100 #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) 2101 /** 2102 * \brief Return the maximum fragment length (payload, in bytes). 2103 * This is the value negotiated with peer if any, 2104 * or the locally configured value. 2105 * 2106 * \note With DTLS, \c mbedtls_ssl_write() will return an error if 2107 * called with a larger length value. 2108 * With TLS, \c mbedtls_ssl_write() will fragment the input if 2109 * necessary and return the number of bytes written; it is up 2110 * to the caller to call \c mbedtls_ssl_write() again in 2111 * order to send the remaining bytes if any. 2112 * 2113 * \param ssl SSL context 2114 * 2115 * \return Current maximum fragment length. 2116 */ 2117 size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl ); 2118 #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ 2119 2120 #if defined(MBEDTLS_X509_CRT_PARSE_C) 2121 /** 2122 * \brief Return the peer certificate from the current connection 2123 * 2124 * Note: Can be NULL in case no certificate was sent during 2125 * the handshake. Different calls for the same connection can 2126 * return the same or different pointers for the same 2127 * certificate and even a different certificate altogether. 2128 * The peer cert CAN change in a single connection if 2129 * renegotiation is performed. 2130 * 2131 * \param ssl SSL context 2132 * 2133 * \return the current peer certificate 2134 */ 2135 const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl ); 2136 #endif /* MBEDTLS_X509_CRT_PARSE_C */ 2137 2138 #if defined(MBEDTLS_SSL_CLI_C) 2139 /** 2140 * \brief Save session in order to resume it later (client-side only) 2141 * Session data is copied to presented session structure. 2142 * 2143 * \warning Currently, peer certificate is lost in the operation. 2144 * 2145 * \param ssl SSL context 2146 * \param session session context 2147 * 2148 * \return 0 if successful, 2149 * MBEDTLS_ERR_SSL_ALLOC_FAILED if memory allocation failed, 2150 * MBEDTLS_ERR_SSL_BAD_INPUT_DATA if used server-side or 2151 * arguments are otherwise invalid 2152 * 2153 * \sa mbedtls_ssl_set_session() 2154 */ 2155 int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *session ); 2156 #endif /* MBEDTLS_SSL_CLI_C */ 2157 2158 /** 2159 * \brief Perform the SSL handshake 2160 * 2161 * \param ssl SSL context 2162 * 2163 * \return 0 if successful, or 2164 * MBEDTLS_ERR_SSL_WANT_READ or MBEDTLS_ERR_SSL_WANT_WRITE, or 2165 * MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED (see below), or 2166 * a specific SSL error code. 2167 * 2168 * \note If this function returns something other than 0 or 2169 * MBEDTLS_ERR_SSL_WANT_READ/WRITE, then the ssl context 2170 * becomes unusable, and you should either free it or call 2171 * \c mbedtls_ssl_session_reset() on it before re-using it for 2172 * a new connection; the current connection must be closed. 2173 * 2174 * \note If DTLS is in use, then you may choose to handle 2175 * MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED specially for logging 2176 * purposes, as it is an expected return value rather than an 2177 * actual error, but you still need to reset/free the context. 2178 */ 2179 int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl ); 2180 2181 /** 2182 * \brief Perform a single step of the SSL handshake 2183 * 2184 * \note The state of the context (ssl->state) will be at 2185 * the following state after execution of this function. 2186 * Do not call this function if state is MBEDTLS_SSL_HANDSHAKE_OVER. 2187 * 2188 * \note If this function returns something other than 0 or 2189 * MBEDTLS_ERR_SSL_WANT_READ/WRITE, then the ssl context 2190 * becomes unusable, and you should either free it or call 2191 * \c mbedtls_ssl_session_reset() on it before re-using it for 2192 * a new connection; the current connection must be closed. 2193 * 2194 * \param ssl SSL context 2195 * 2196 * \return 0 if successful, or 2197 * MBEDTLS_ERR_SSL_WANT_READ or MBEDTLS_ERR_SSL_WANT_WRITE, or 2198 * a specific SSL error code. 2199 */ 2200 int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl ); 2201 2202 #if defined(MBEDTLS_SSL_RENEGOTIATION) 2203 /** 2204 * \brief Initiate an SSL renegotiation on the running connection. 2205 * Client: perform the renegotiation right now. 2206 * Server: request renegotiation, which will be performed 2207 * during the next call to mbedtls_ssl_read() if honored by client. 2208 * 2209 * \param ssl SSL context 2210 * 2211 * \return 0 if successful, or any mbedtls_ssl_handshake() return value. 2212 * 2213 * \note If this function returns something other than 0 or 2214 * MBEDTLS_ERR_SSL_WANT_READ/WRITE, then the ssl context 2215 * becomes unusable, and you should either free it or call 2216 * \c mbedtls_ssl_session_reset() on it before re-using it for 2217 * a new connection; the current connection must be closed. 2218 */ 2219 int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl ); 2220 #endif /* MBEDTLS_SSL_RENEGOTIATION */ 2221 2222 /** 2223 * \brief Read at most 'len' application data bytes 2224 * 2225 * \param ssl SSL context 2226 * \param buf buffer that will hold the data 2227 * \param len maximum number of bytes to read 2228 * 2229 * \return the number of bytes read, or 2230 * 0 for EOF, or 2231 * MBEDTLS_ERR_SSL_WANT_READ or MBEDTLS_ERR_SSL_WANT_WRITE, or 2232 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT (see below), or 2233 * another negative error code. 2234 * 2235 * \note If this function returns something other than a positive 2236 * value or MBEDTLS_ERR_SSL_WANT_READ/WRITE or 2237 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT, then the ssl context 2238 * becomes unusable, and you should either free it or call 2239 * \c mbedtls_ssl_session_reset() on it before re-using it for 2240 * a new connection; the current connection must be closed. 2241 * 2242 * \note When this function return MBEDTLS_ERR_SSL_CLIENT_RECONNECT 2243 * (which can only happen server-side), it means that a client 2244 * is initiating a new connection using the same source port. 2245 * You can either treat that as a connection close and wait 2246 * for the client to resend a ClientHello, or directly 2247 * continue with \c mbedtls_ssl_handshake() with the same 2248 * context (as it has beeen reset internally). Either way, you 2249 * should make sure this is seen by the application as a new 2250 * connection: application state, if any, should be reset, and 2251 * most importantly the identity of the client must be checked 2252 * again. WARNING: not validating the identity of the client 2253 * again, or not transmitting the new identity to the 2254 * application layer, would allow authentication bypass! 2255 */ 2256 int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len ); 2257 2258 /** 2259 * \brief Try to write exactly 'len' application data bytes 2260 * 2261 * \warning This function will do partial writes in some cases. If the 2262 * return value is non-negative but less than length, the 2263 * function must be called again with updated arguments: 2264 * buf + ret, len - ret (if ret is the return value) until 2265 * it returns a value equal to the last 'len' argument. 2266 * 2267 * \param ssl SSL context 2268 * \param buf buffer holding the data 2269 * \param len how many bytes must be written 2270 * 2271 * \return the number of bytes actually written (may be less than len), 2272 * or MBEDTLS_ERR_SSL_WANT_WRITE of MBEDTLS_ERR_SSL_WANT_READ, 2273 * or another negative error code. 2274 * 2275 * \note If this function returns something other than a positive 2276 * value or MBEDTLS_ERR_SSL_WANT_READ/WRITE, the ssl context 2277 * becomes unusable, and you should either free it or call 2278 * \c mbedtls_ssl_session_reset() on it before re-using it for 2279 * a new connection; the current connection must be closed. 2280 * 2281 * \note When this function returns MBEDTLS_ERR_SSL_WANT_WRITE/READ, 2282 * it must be called later with the *same* arguments, 2283 * until it returns a positive value. 2284 * 2285 * \note If the requested length is greater than the maximum 2286 * fragment length (either the built-in limit or the one set 2287 * or negotiated with the peer), then: 2288 * - with TLS, less bytes than requested are written. 2289 * - with DTLS, MBEDTLS_ERR_SSL_BAD_INPUT_DATA is returned. 2290 * \c mbedtls_ssl_get_max_frag_len() may be used to query the 2291 * active maximum fragment length. 2292 */ 2293 int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len ); 2294 2295 /** 2296 * \brief Send an alert message 2297 * 2298 * \param ssl SSL context 2299 * \param level The alert level of the message 2300 * (MBEDTLS_SSL_ALERT_LEVEL_WARNING or MBEDTLS_SSL_ALERT_LEVEL_FATAL) 2301 * \param message The alert message (SSL_ALERT_MSG_*) 2302 * 2303 * \return 0 if successful, or a specific SSL error code. 2304 * 2305 * \note If this function returns something other than 0 or 2306 * MBEDTLS_ERR_SSL_WANT_READ/WRITE, then the ssl context 2307 * becomes unusable, and you should either free it or call 2308 * \c mbedtls_ssl_session_reset() on it before re-using it for 2309 * a new connection; the current connection must be closed. 2310 */ 2311 int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl, 2312 unsigned char level, 2313 unsigned char message ); 2314 /** 2315 * \brief Notify the peer that the connection is being closed 2316 * 2317 * \param ssl SSL context 2318 * 2319 * \return 0 if successful, or a specific SSL error code. 2320 * 2321 * \note If this function returns something other than 0 or 2322 * MBEDTLS_ERR_SSL_WANT_READ/WRITE, then the ssl context 2323 * becomes unusable, and you should either free it or call 2324 * \c mbedtls_ssl_session_reset() on it before re-using it for 2325 * a new connection; the current connection must be closed. 2326 */ 2327 int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl ); 2328 2329 /** 2330 * \brief Free referenced items in an SSL context and clear memory 2331 * 2332 * \param ssl SSL context 2333 */ 2334 void mbedtls_ssl_free( mbedtls_ssl_context *ssl ); 2335 2336 /** 2337 * \brief Initialize an SSL configuration context 2338 * Just makes the context ready for 2339 * mbedtls_ssl_config_defaults() or mbedtls_ssl_config_free(). 2340 * 2341 * \note You need to call mbedtls_ssl_config_defaults() unless you 2342 * manually set all of the relevent fields yourself. 2343 * 2344 * \param conf SSL configuration context 2345 */ 2346 void mbedtls_ssl_config_init( mbedtls_ssl_config *conf ); 2347 2348 /** 2349 * \brief Load reasonnable default SSL configuration values. 2350 * (You need to call mbedtls_ssl_config_init() first.) 2351 * 2352 * \param conf SSL configuration context 2353 * \param endpoint MBEDTLS_SSL_IS_CLIENT or MBEDTLS_SSL_IS_SERVER 2354 * \param transport MBEDTLS_SSL_TRANSPORT_STREAM for TLS, or 2355 * MBEDTLS_SSL_TRANSPORT_DATAGRAM for DTLS 2356 * \param preset a MBEDTLS_SSL_PRESET_XXX value 2357 * (currently unused). 2358 * 2359 * \note See \c mbedtls_ssl_conf_transport() for notes on DTLS. 2360 * 2361 * \return 0 if successful, or 2362 * MBEDTLS_ERR_XXX_ALLOC_FAILED on memory allocation error. 2363 */ 2364 int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf, 2365 int endpoint, int transport, int preset ); 2366 2367 /** 2368 * \brief Free an SSL configuration context 2369 * 2370 * \param conf SSL configuration context 2371 */ 2372 void mbedtls_ssl_config_free( mbedtls_ssl_config *conf ); 2373 2374 /** 2375 * \brief Initialize SSL session structure 2376 * 2377 * \param session SSL session 2378 */ 2379 void mbedtls_ssl_session_init( mbedtls_ssl_session *session ); 2380 2381 /** 2382 * \brief Free referenced items in an SSL session including the 2383 * peer certificate and clear memory 2384 * 2385 * \param session SSL session 2386 */ 2387 void mbedtls_ssl_session_free( mbedtls_ssl_session *session ); 2388 2389 #ifdef __cplusplus 2390 } 2391 #endif 2392 2393 #endif /* ssl.h */ 2394