4 * \brief SSL/TLS functions.
6 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
7 * SPDX-License-Identifier: GPL-2.0
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 * This file is part of mbed TLS (https://tls.mbed.org)
28 #if !defined(MBEDTLS_CONFIG_FILE)
31 #include MBEDTLS_CONFIG_FILE
37 #include "ssl_ciphersuites.h"
39 #if defined(MBEDTLS_X509_CRT_PARSE_C)
44 #if defined(MBEDTLS_DHM_C)
48 #if defined(MBEDTLS_ECDH_C)
52 #if defined(MBEDTLS_ZLIB_SUPPORT)
56 #if defined(MBEDTLS_HAVE_TIME)
57 #include "mbedtls/platform_time.h"
63 #define MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE -0x7080 /**< The requested feature is not available. */
64 #define MBEDTLS_ERR_SSL_BAD_INPUT_DATA -0x7100 /**< Bad input parameters to function. */
65 #define MBEDTLS_ERR_SSL_INVALID_MAC -0x7180 /**< Verification of the message MAC failed. */
66 #define MBEDTLS_ERR_SSL_INVALID_RECORD -0x7200 /**< An invalid SSL record was received. */
67 #define MBEDTLS_ERR_SSL_CONN_EOF -0x7280 /**< The connection indicated an EOF. */
68 #define MBEDTLS_ERR_SSL_UNKNOWN_CIPHER -0x7300 /**< An unknown cipher was received. */
69 #define MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN -0x7380 /**< The server has no ciphersuites in common with the client. */
70 #define MBEDTLS_ERR_SSL_NO_RNG -0x7400 /**< No RNG was provided to the SSL module. */
71 #define MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE -0x7480 /**< No client certification received from the client, but required by the authentication mode. */
72 #define MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE -0x7500 /**< Our own certificate(s) is/are too large to send in an SSL message. */
73 #define MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED -0x7580 /**< The own certificate is not set, but needed by the server. */
74 #define MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED -0x7600 /**< The own private key or pre-shared key is not set, but needed. */
75 #define MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED -0x7680 /**< No CA Chain is set, but required to operate. */
76 #define MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE -0x7700 /**< An unexpected message was received from our peer. */
77 #define MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE -0x7780 /**< A fatal alert message was received from our peer. */
78 #define MBEDTLS_ERR_SSL_PEER_VERIFY_FAILED -0x7800 /**< Verification of our peer failed. */
79 #define MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY -0x7880 /**< The peer notified us that the connection is going to be closed. */
80 #define MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO -0x7900 /**< Processing of the ClientHello handshake message failed. */
81 #define MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO -0x7980 /**< Processing of the ServerHello handshake message failed. */
82 #define MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE -0x7A00 /**< Processing of the Certificate handshake message failed. */
83 #define MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST -0x7A80 /**< Processing of the CertificateRequest handshake message failed. */
84 #define MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE -0x7B00 /**< Processing of the ServerKeyExchange handshake message failed. */
85 #define MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO_DONE -0x7B80 /**< Processing of the ServerHelloDone handshake message failed. */
86 #define MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE -0x7C00 /**< Processing of the ClientKeyExchange handshake message failed. */
87 #define MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP -0x7C80 /**< Processing of the ClientKeyExchange handshake message failed in DHM / ECDH Read Public. */
88 #define MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS -0x7D00 /**< Processing of the ClientKeyExchange handshake message failed in DHM / ECDH Calculate Secret. */
89 #define MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY -0x7D80 /**< Processing of the CertificateVerify handshake message failed. */
90 #define MBEDTLS_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC -0x7E00 /**< Processing of the ChangeCipherSpec handshake message failed. */
91 #define MBEDTLS_ERR_SSL_BAD_HS_FINISHED -0x7E80 /**< Processing of the Finished handshake message failed. */
92 #define MBEDTLS_ERR_SSL_ALLOC_FAILED -0x7F00 /**< Memory allocation failed */
93 #define MBEDTLS_ERR_SSL_HW_ACCEL_FAILED -0x7F80 /**< Hardware acceleration function returned with error */
94 #define MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH -0x6F80 /**< Hardware acceleration function skipped / left alone data */
95 #define MBEDTLS_ERR_SSL_COMPRESSION_FAILED -0x6F00 /**< Processing of the compression / decompression failed */
96 #define MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION -0x6E80 /**< Handshake protocol not within min/max boundaries */
97 #define MBEDTLS_ERR_SSL_BAD_HS_NEW_SESSION_TICKET -0x6E00 /**< Processing of the NewSessionTicket handshake message failed. */
98 #define MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED -0x6D80 /**< Session ticket has expired. */
99 #define MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH -0x6D00 /**< Public key type mismatch (eg, asked for RSA key exchange and presented EC key) */
100 #define MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY -0x6C80 /**< Unknown identity received (eg, PSK identity) */
101 #define MBEDTLS_ERR_SSL_INTERNAL_ERROR -0x6C00 /**< Internal error (eg, unexpected failure in lower-level module) */
102 #define MBEDTLS_ERR_SSL_COUNTER_WRAPPING -0x6B80 /**< A counter would wrap (eg, too many messages exchanged). */
103 #define MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO -0x6B00 /**< Unexpected message at ServerHello in renegotiation. */
104 #define MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED -0x6A80 /**< DTLS client must retry for hello verification */
105 #define MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL -0x6A00 /**< A buffer is too small to receive or write a message */
106 #define MBEDTLS_ERR_SSL_NO_USABLE_CIPHERSUITE -0x6980 /**< None of the common ciphersuites is usable (eg, no suitable certificate, see debug messages). */
107 #define MBEDTLS_ERR_SSL_WANT_READ -0x6900 /**< Connection requires a read call. */
108 #define MBEDTLS_ERR_SSL_WANT_WRITE -0x6880 /**< Connection requires a write call. */
109 #define MBEDTLS_ERR_SSL_TIMEOUT -0x6800 /**< The operation timed out. */
110 #define MBEDTLS_ERR_SSL_CLIENT_RECONNECT -0x6780 /**< The client initiated a reconnect from the same port. */
111 #define MBEDTLS_ERR_SSL_UNEXPECTED_RECORD -0x6700 /**< Record header looks valid but is not expected. */
112 #define MBEDTLS_ERR_SSL_NON_FATAL -0x6680 /**< The alert message received indicates a non-fatal error. */
113 #define MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH -0x6600 /**< Couldn't set the hash for verifying CertificateVerify */
118 #define MBEDTLS_SSL_MAJOR_VERSION_3 3
119 #define MBEDTLS_SSL_MINOR_VERSION_0 0 /*!< SSL v3.0 */
120 #define MBEDTLS_SSL_MINOR_VERSION_1 1 /*!< TLS v1.0 */
121 #define MBEDTLS_SSL_MINOR_VERSION_2 2 /*!< TLS v1.1 */
122 #define MBEDTLS_SSL_MINOR_VERSION_3 3 /*!< TLS v1.2 */
124 #define MBEDTLS_SSL_TRANSPORT_STREAM 0 /*!< TLS */
125 #define MBEDTLS_SSL_TRANSPORT_DATAGRAM 1 /*!< DTLS */
127 #define MBEDTLS_SSL_MAX_HOST_NAME_LEN 255 /*!< Maximum host name defined in RFC 1035 */
129 /* RFC 6066 section 4, see also mfl_code_to_length in ssl_tls.c
130 * NONE must be zero so that memset()ing structure to zero works */
131 #define MBEDTLS_SSL_MAX_FRAG_LEN_NONE 0 /*!< don't use this extension */
132 #define MBEDTLS_SSL_MAX_FRAG_LEN_512 1 /*!< MaxFragmentLength 2^9 */
133 #define MBEDTLS_SSL_MAX_FRAG_LEN_1024 2 /*!< MaxFragmentLength 2^10 */
134 #define MBEDTLS_SSL_MAX_FRAG_LEN_2048 3 /*!< MaxFragmentLength 2^11 */
135 #define MBEDTLS_SSL_MAX_FRAG_LEN_4096 4 /*!< MaxFragmentLength 2^12 */
136 #define MBEDTLS_SSL_MAX_FRAG_LEN_INVALID 5 /*!< first invalid value */
138 #define MBEDTLS_SSL_IS_CLIENT 0
139 #define MBEDTLS_SSL_IS_SERVER 1
141 #define MBEDTLS_SSL_IS_NOT_FALLBACK 0
142 #define MBEDTLS_SSL_IS_FALLBACK 1
144 #define MBEDTLS_SSL_EXTENDED_MS_DISABLED 0
145 #define MBEDTLS_SSL_EXTENDED_MS_ENABLED 1
147 #define MBEDTLS_SSL_ETM_DISABLED 0
148 #define MBEDTLS_SSL_ETM_ENABLED 1
150 #define MBEDTLS_SSL_COMPRESS_NULL 0
151 #define MBEDTLS_SSL_COMPRESS_DEFLATE 1
153 #define MBEDTLS_SSL_VERIFY_NONE 0
154 #define MBEDTLS_SSL_VERIFY_OPTIONAL 1
155 #define MBEDTLS_SSL_VERIFY_REQUIRED 2
156 #define MBEDTLS_SSL_VERIFY_UNSET 3 /* Used only for sni_authmode */
158 #define MBEDTLS_SSL_LEGACY_RENEGOTIATION 0
159 #define MBEDTLS_SSL_SECURE_RENEGOTIATION 1
161 #define MBEDTLS_SSL_RENEGOTIATION_DISABLED 0
162 #define MBEDTLS_SSL_RENEGOTIATION_ENABLED 1
164 #define MBEDTLS_SSL_ANTI_REPLAY_DISABLED 0
165 #define MBEDTLS_SSL_ANTI_REPLAY_ENABLED 1
167 #define MBEDTLS_SSL_RENEGOTIATION_NOT_ENFORCED -1
168 #define MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT 16
170 #define MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION 0
171 #define MBEDTLS_SSL_LEGACY_ALLOW_RENEGOTIATION 1
172 #define MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE 2
174 #define MBEDTLS_SSL_TRUNC_HMAC_DISABLED 0
175 #define MBEDTLS_SSL_TRUNC_HMAC_ENABLED 1
176 #define MBEDTLS_SSL_TRUNCATED_HMAC_LEN 10 /* 80 bits, rfc 6066 section 7 */
178 #define MBEDTLS_SSL_SESSION_TICKETS_DISABLED 0
179 #define MBEDTLS_SSL_SESSION_TICKETS_ENABLED 1
181 #define MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED 0
182 #define MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED 1
184 #define MBEDTLS_SSL_ARC4_ENABLED 0
185 #define MBEDTLS_SSL_ARC4_DISABLED 1
187 #define MBEDTLS_SSL_PRESET_DEFAULT 0
188 #define MBEDTLS_SSL_PRESET_SUITEB 2
191 * Default range for DTLS retransmission timer value, in milliseconds.
192 * RFC 6347 4.2.4.1 says from 1 second to 60 seconds.
194 #define MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN 1000
195 #define MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX 60000
198 * \name SECTION: Module settings
200 * The configuration options you can set for this module are in this section.
201 * Either change them in config.h or define them on the compiler command line.
205 #if !defined(MBEDTLS_SSL_DEFAULT_TICKET_LIFETIME)
206 #define MBEDTLS_SSL_DEFAULT_TICKET_LIFETIME 86400 /**< Lifetime of session tickets (if enabled) */
210 * Maxium fragment length in bytes,
211 * determines the size of each of the two internal I/O buffers.
213 * Note: the RFC defines the default size of SSL / TLS messages. If you
214 * change the value here, other clients / servers may not be able to
215 * communicate with you anymore. Only change this value if you control
216 * both sides of the connection and have it reduced at both sides, or
217 * if you're using the Max Fragment Length extension and you know all your
218 * peers are using it too!
220 #if !defined(MBEDTLS_SSL_MAX_CONTENT_LEN)
221 #define MBEDTLS_SSL_MAX_CONTENT_LEN 16384 /**< Size of the input / output buffer */
224 /* \} name SECTION: Module settings */
227 * Length of the verify data for secure renegotiation
229 #if defined(MBEDTLS_SSL_PROTO_SSL3)
230 #define MBEDTLS_SSL_VERIFY_DATA_MAX_LEN 36
232 #define MBEDTLS_SSL_VERIFY_DATA_MAX_LEN 12
236 * Signaling ciphersuite values (SCSV)
238 #define MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO 0xFF /**< renegotiation info ext */
239 #define MBEDTLS_SSL_FALLBACK_SCSV_VALUE 0x5600 /**< RFC 7507 section 2 */
242 * Supported Signature and Hash algorithms (For TLS 1.2)
243 * RFC 5246 section 7.4.1.4.1
245 #define MBEDTLS_SSL_HASH_NONE 0
246 #define MBEDTLS_SSL_HASH_MD5 1
247 #define MBEDTLS_SSL_HASH_SHA1 2
248 #define MBEDTLS_SSL_HASH_SHA224 3
249 #define MBEDTLS_SSL_HASH_SHA256 4
250 #define MBEDTLS_SSL_HASH_SHA384 5
251 #define MBEDTLS_SSL_HASH_SHA512 6
253 #define MBEDTLS_SSL_SIG_ANON 0
254 #define MBEDTLS_SSL_SIG_RSA 1
255 #define MBEDTLS_SSL_SIG_ECDSA 3
258 * Client Certificate Types
259 * RFC 5246 section 7.4.4 plus RFC 4492 section 5.5
261 #define MBEDTLS_SSL_CERT_TYPE_RSA_SIGN 1
262 #define MBEDTLS_SSL_CERT_TYPE_ECDSA_SIGN 64
265 * Message, alert and handshake types
267 #define MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC 20
268 #define MBEDTLS_SSL_MSG_ALERT 21
269 #define MBEDTLS_SSL_MSG_HANDSHAKE 22
270 #define MBEDTLS_SSL_MSG_APPLICATION_DATA 23
272 #define MBEDTLS_SSL_ALERT_LEVEL_WARNING 1
273 #define MBEDTLS_SSL_ALERT_LEVEL_FATAL 2
275 #define MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY 0 /* 0x00 */
276 #define MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE 10 /* 0x0A */
277 #define MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC 20 /* 0x14 */
278 #define MBEDTLS_SSL_ALERT_MSG_DECRYPTION_FAILED 21 /* 0x15 */
279 #define MBEDTLS_SSL_ALERT_MSG_RECORD_OVERFLOW 22 /* 0x16 */
280 #define MBEDTLS_SSL_ALERT_MSG_DECOMPRESSION_FAILURE 30 /* 0x1E */
281 #define MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE 40 /* 0x28 */
282 #define MBEDTLS_SSL_ALERT_MSG_NO_CERT 41 /* 0x29 */
283 #define MBEDTLS_SSL_ALERT_MSG_BAD_CERT 42 /* 0x2A */
284 #define MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT 43 /* 0x2B */
285 #define MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED 44 /* 0x2C */
286 #define MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED 45 /* 0x2D */
287 #define MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN 46 /* 0x2E */
288 #define MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER 47 /* 0x2F */
289 #define MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA 48 /* 0x30 */
290 #define MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED 49 /* 0x31 */
291 #define MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR 50 /* 0x32 */
292 #define MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR 51 /* 0x33 */
293 #define MBEDTLS_SSL_ALERT_MSG_EXPORT_RESTRICTION 60 /* 0x3C */
294 #define MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION 70 /* 0x46 */
295 #define MBEDTLS_SSL_ALERT_MSG_INSUFFICIENT_SECURITY 71 /* 0x47 */
296 #define MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR 80 /* 0x50 */
297 #define MBEDTLS_SSL_ALERT_MSG_INAPROPRIATE_FALLBACK 86 /* 0x56 */
298 #define MBEDTLS_SSL_ALERT_MSG_USER_CANCELED 90 /* 0x5A */
299 #define MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION 100 /* 0x64 */
300 #define MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT 110 /* 0x6E */
301 #define MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME 112 /* 0x70 */
302 #define MBEDTLS_SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY 115 /* 0x73 */
303 #define MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL 120 /* 0x78 */
305 #define MBEDTLS_SSL_HS_HELLO_REQUEST 0
306 #define MBEDTLS_SSL_HS_CLIENT_HELLO 1
307 #define MBEDTLS_SSL_HS_SERVER_HELLO 2
308 #define MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST 3
309 #define MBEDTLS_SSL_HS_NEW_SESSION_TICKET 4
310 #define MBEDTLS_SSL_HS_CERTIFICATE 11
311 #define MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE 12
312 #define MBEDTLS_SSL_HS_CERTIFICATE_REQUEST 13
313 #define MBEDTLS_SSL_HS_SERVER_HELLO_DONE 14
314 #define MBEDTLS_SSL_HS_CERTIFICATE_VERIFY 15
315 #define MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE 16
316 #define MBEDTLS_SSL_HS_FINISHED 20
321 #define MBEDTLS_TLS_EXT_SERVERNAME 0
322 #define MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME 0
324 #define MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH 1
326 #define MBEDTLS_TLS_EXT_TRUNCATED_HMAC 4
328 #define MBEDTLS_TLS_EXT_SUPPORTED_ELLIPTIC_CURVES 10
329 #define MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS 11
331 #define MBEDTLS_TLS_EXT_SIG_ALG 13
333 #define MBEDTLS_TLS_EXT_ALPN 16
335 #define MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC 22 /* 0x16 */
336 #define MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET 0x0017 /* 23 */
338 #define MBEDTLS_TLS_EXT_SESSION_TICKET 35
340 #define MBEDTLS_TLS_EXT_ECJPAKE_KKPP 256 /* experimental */
342 #define MBEDTLS_TLS_EXT_RENEGOTIATION_INFO 0xFF01
347 #if !defined(MBEDTLS_PSK_MAX_LEN)
348 #define MBEDTLS_PSK_MAX_LEN 32 /* 256 bits */
351 /* Dummy type used only for its size */
352 union mbedtls_ssl_premaster_secret
354 #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
355 unsigned char _pms_rsa
[48]; /* RFC 5246 8.1.1 */
357 #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
358 unsigned char _pms_dhm
[MBEDTLS_MPI_MAX_SIZE
]; /* RFC 5246 8.1.2 */
360 #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
361 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
362 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
363 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
364 unsigned char _pms_ecdh
[MBEDTLS_ECP_MAX_BYTES
]; /* RFC 4492 5.10 */
366 #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
367 unsigned char _pms_psk
[4 + 2 * MBEDTLS_PSK_MAX_LEN
]; /* RFC 4279 2 */
369 #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
370 unsigned char _pms_dhe_psk
[4 + MBEDTLS_MPI_MAX_SIZE
371 + MBEDTLS_PSK_MAX_LEN
]; /* RFC 4279 3 */
373 #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
374 unsigned char _pms_rsa_psk
[52 + MBEDTLS_PSK_MAX_LEN
]; /* RFC 4279 4 */
376 #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
377 unsigned char _pms_ecdhe_psk
[4 + MBEDTLS_ECP_MAX_BYTES
378 + MBEDTLS_PSK_MAX_LEN
]; /* RFC 5489 2 */
380 #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
381 unsigned char _pms_ecjpake
[32]; /* Thread spec: SHA-256 output */
385 #define MBEDTLS_PREMASTER_SIZE sizeof( union mbedtls_ssl_premaster_secret )
396 MBEDTLS_SSL_HELLO_REQUEST
,
397 MBEDTLS_SSL_CLIENT_HELLO
,
398 MBEDTLS_SSL_SERVER_HELLO
,
399 MBEDTLS_SSL_SERVER_CERTIFICATE
,
400 MBEDTLS_SSL_SERVER_KEY_EXCHANGE
,
401 MBEDTLS_SSL_CERTIFICATE_REQUEST
,
402 MBEDTLS_SSL_SERVER_HELLO_DONE
,
403 MBEDTLS_SSL_CLIENT_CERTIFICATE
,
404 MBEDTLS_SSL_CLIENT_KEY_EXCHANGE
,
405 MBEDTLS_SSL_CERTIFICATE_VERIFY
,
406 MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC
,
407 MBEDTLS_SSL_CLIENT_FINISHED
,
408 MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC
,
409 MBEDTLS_SSL_SERVER_FINISHED
,
410 MBEDTLS_SSL_FLUSH_BUFFERS
,
411 MBEDTLS_SSL_HANDSHAKE_WRAPUP
,
412 MBEDTLS_SSL_HANDSHAKE_OVER
,
413 MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET
,
414 MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT
,
419 * \brief Callback type: send data on the network.
421 * \note That callback may be either blocking or non-blocking.
423 * \param ctx Context for the send callback (typically a file descriptor)
424 * \param buf Buffer holding the data to send
425 * \param len Length of the data to send
427 * \return The callback must return the number of bytes sent if any,
428 * or a non-zero error code.
429 * If performing non-blocking I/O, \c MBEDTLS_ERR_SSL_WANT_WRITE
430 * must be returned when the operation would block.
432 * \note The callback is allowed to send fewer bytes than requested.
433 * It must always return the number of bytes actually sent.
435 typedef int mbedtls_ssl_send_t( void *ctx
,
436 const unsigned char *buf
,
440 * \brief Callback type: receive data from the network.
442 * \note That callback may be either blocking or non-blocking.
444 * \param ctx Context for the receive callback (typically a file
446 * \param buf Buffer to write the received data to
447 * \param len Length of the receive buffer
449 * \return The callback must return the number of bytes received,
450 * or a non-zero error code.
451 * If performing non-blocking I/O, \c MBEDTLS_ERR_SSL_WANT_READ
452 * must be returned when the operation would block.
454 * \note The callback may receive fewer bytes than the length of the
455 * buffer. It must always return the number of bytes actually
456 * received and written to the buffer.
458 typedef int mbedtls_ssl_recv_t( void *ctx
,
463 * \brief Callback type: receive data from the network, with timeout
465 * \note That callback must block until data is received, or the
466 * timeout delay expires, or the operation is interrupted by a
469 * \param ctx Context for the receive callback (typically a file descriptor)
470 * \param buf Buffer to write the received data to
471 * \param len Length of the receive buffer
472 * \param timeout Maximum nomber of millisecondes to wait for data
473 * 0 means no timeout (potentially waiting forever)
475 * \return The callback must return the number of bytes received,
476 * or a non-zero error code:
477 * \c MBEDTLS_ERR_SSL_TIMEOUT if the operation timed out,
478 * \c MBEDTLS_ERR_SSL_WANT_READ if interrupted by a signal.
480 * \note The callback may receive fewer bytes than the length of the
481 * buffer. It must always return the number of bytes actually
482 * received and written to the buffer.
484 typedef int mbedtls_ssl_recv_timeout_t( void *ctx
,
489 * \brief Callback type: set a pair of timers/delays to watch
491 * \param ctx Context pointer
492 * \param int_ms Intermediate delay in milliseconds
493 * \param fin_ms Final delay in milliseconds
494 * 0 cancels the current timer.
496 * \note This callback must at least store the necessary information
497 * for the associated \c mbedtls_ssl_get_timer_t callback to
498 * return correct information.
500 * \note If using a event-driven style of programming, an event must
501 * be generated when the final delay is passed. The event must
502 * cause a call to \c mbedtls_ssl_handshake() with the proper
503 * SSL context to be scheduled. Care must be taken to ensure
504 * that at most one such call happens at a time.
506 * \note Only one timer at a time must be running. Calling this
507 * function while a timer is running must cancel it. Cancelled
508 * timers must not generate any event.
510 typedef void mbedtls_ssl_set_timer_t( void * ctx
,
515 * \brief Callback type: get status of timers/delays
517 * \param ctx Context pointer
519 * \return This callback must return:
520 * -1 if cancelled (fin_ms == 0),
521 * 0 if none of the delays have passed,
522 * 1 if only the intermediate delay has passed,
523 * 2 if the final delay has passed.
525 typedef int mbedtls_ssl_get_timer_t( void * ctx
);
529 typedef struct mbedtls_ssl_session mbedtls_ssl_session
;
530 typedef struct mbedtls_ssl_context mbedtls_ssl_context
;
531 typedef struct mbedtls_ssl_config mbedtls_ssl_config
;
533 /* Defined in ssl_internal.h */
534 typedef struct mbedtls_ssl_transform mbedtls_ssl_transform
;
535 typedef struct mbedtls_ssl_handshake_params mbedtls_ssl_handshake_params
;
536 #if defined(MBEDTLS_X509_CRT_PARSE_C)
537 typedef struct mbedtls_ssl_key_cert mbedtls_ssl_key_cert
;
539 #if defined(MBEDTLS_SSL_PROTO_DTLS)
540 typedef struct mbedtls_ssl_flight_item mbedtls_ssl_flight_item
;
544 * This structure is used for storing current session data.
546 struct mbedtls_ssl_session
548 #if defined(MBEDTLS_HAVE_TIME)
549 mbedtls_time_t start
; /*!< starting time */
551 int ciphersuite
; /*!< chosen ciphersuite */
552 int compression
; /*!< chosen compression */
553 size_t id_len
; /*!< session id length */
554 unsigned char id
[32]; /*!< session identifier */
555 unsigned char master
[48]; /*!< the master secret */
557 #if defined(MBEDTLS_X509_CRT_PARSE_C)
558 mbedtls_x509_crt
*peer_cert
; /*!< peer X.509 cert chain */
559 #endif /* MBEDTLS_X509_CRT_PARSE_C */
560 uint32_t verify_result
; /*!< verification result */
562 #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
563 unsigned char *ticket
; /*!< RFC 5077 session ticket */
564 size_t ticket_len
; /*!< session ticket length */
565 uint32_t ticket_lifetime
; /*!< ticket lifetime hint */
566 #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
568 #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
569 unsigned char mfl_code
; /*!< MaxFragmentLength negotiated by peer */
570 #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
572 #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
573 int trunc_hmac
; /*!< flag for truncated hmac activation */
574 #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
576 #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
577 int encrypt_then_mac
; /*!< flag for EtM activation */
582 * SSL/TLS configuration to be shared between mbedtls_ssl_context structures.
584 struct mbedtls_ssl_config
586 /* Group items by size (largest first) to minimize padding overhead */
592 const int *ciphersuite_list
[4]; /*!< allowed ciphersuites per version */
594 /** Callback for printing debug output */
595 void (*f_dbg
)(void *, int, const char *, int, const char *);
596 void *p_dbg
; /*!< context for the debug function */
598 /** Callback for getting (pseudo-)random numbers */
599 int (*f_rng
)(void *, unsigned char *, size_t);
600 void *p_rng
; /*!< context for the RNG function */
602 /** Callback to retrieve a session from the cache */
603 int (*f_get_cache
)(void *, mbedtls_ssl_session
*);
604 /** Callback to store a session into the cache */
605 int (*f_set_cache
)(void *, const mbedtls_ssl_session
*);
606 void *p_cache
; /*!< context for cache callbacks */
608 #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
609 /** Callback for setting cert according to SNI extension */
610 int (*f_sni
)(void *, mbedtls_ssl_context
*, const unsigned char *, size_t);
611 void *p_sni
; /*!< context for SNI callback */
614 #if defined(MBEDTLS_X509_CRT_PARSE_C)
615 /** Callback to customize X.509 certificate chain verification */
616 int (*f_vrfy
)(void *, mbedtls_x509_crt
*, int, uint32_t *);
617 void *p_vrfy
; /*!< context for X.509 verify calllback */
620 #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
621 /** Callback to retrieve PSK key from identity */
622 int (*f_psk
)(void *, mbedtls_ssl_context
*, const unsigned char *, size_t);
623 void *p_psk
; /*!< context for PSK callback */
626 #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
627 /** Callback to create & write a cookie for ClientHello veirifcation */
628 int (*f_cookie_write
)( void *, unsigned char **, unsigned char *,
629 const unsigned char *, size_t );
630 /** Callback to verify validity of a ClientHello cookie */
631 int (*f_cookie_check
)( void *, const unsigned char *, size_t,
632 const unsigned char *, size_t );
633 void *p_cookie
; /*!< context for the cookie callbacks */
636 #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_SRV_C)
637 /** Callback to create & write a session ticket */
638 int (*f_ticket_write
)( void *, const mbedtls_ssl_session
*,
639 unsigned char *, const unsigned char *, size_t *, uint32_t * );
640 /** Callback to parse a session ticket into a session structure */
641 int (*f_ticket_parse
)( void *, mbedtls_ssl_session
*, unsigned char *, size_t);
642 void *p_ticket
; /*!< context for the ticket callbacks */
643 #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_SRV_C */
645 #if defined(MBEDTLS_SSL_EXPORT_KEYS)
646 /** Callback to export key block and master secret */
647 int (*f_export_keys
)( void *, const unsigned char *,
648 const unsigned char *, size_t, size_t, size_t );
649 void *p_export_keys
; /*!< context for key export callback */
652 #if defined(MBEDTLS_X509_CRT_PARSE_C)
653 const mbedtls_x509_crt_profile
*cert_profile
; /*!< verification profile */
654 mbedtls_ssl_key_cert
*key_cert
; /*!< own certificate/key pair(s) */
655 mbedtls_x509_crt
*ca_chain
; /*!< trusted CAs */
656 mbedtls_x509_crl
*ca_crl
; /*!< trusted CAs CRLs */
657 #endif /* MBEDTLS_X509_CRT_PARSE_C */
659 #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
660 const int *sig_hashes
; /*!< allowed signature hashes */
663 #if defined(MBEDTLS_ECP_C)
664 const mbedtls_ecp_group_id
*curve_list
; /*!< allowed curves */
667 #if defined(MBEDTLS_DHM_C)
668 mbedtls_mpi dhm_P
; /*!< prime modulus for DHM */
669 mbedtls_mpi dhm_G
; /*!< generator for DHM */
672 #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
673 unsigned char *psk
; /*!< pre-shared key */
674 size_t psk_len
; /*!< length of the pre-shared key */
675 unsigned char *psk_identity
; /*!< identity for PSK negotiation */
676 size_t psk_identity_len
;/*!< length of identity */
679 #if defined(MBEDTLS_SSL_ALPN)
680 const char **alpn_list
; /*!< ordered list of protocols */
684 * Numerical settings (int then char)
687 uint32_t read_timeout
; /*!< timeout for mbedtls_ssl_read (ms) */
689 #if defined(MBEDTLS_SSL_PROTO_DTLS)
690 uint32_t hs_timeout_min
; /*!< initial value of the handshake
691 retransmission timeout (ms) */
692 uint32_t hs_timeout_max
; /*!< maximum value of the handshake
693 retransmission timeout (ms) */
696 #if defined(MBEDTLS_SSL_RENEGOTIATION)
697 int renego_max_records
; /*!< grace period for renegotiation */
698 unsigned char renego_period
[8]; /*!< value of the record counters
699 that triggers renegotiation */
702 #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
703 unsigned int badmac_limit
; /*!< limit of records with a bad MAC */
706 #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
707 unsigned int dhm_min_bitlen
; /*!< min. bit length of the DHM prime */
710 unsigned char max_major_ver
; /*!< max. major version used */
711 unsigned char max_minor_ver
; /*!< max. minor version used */
712 unsigned char min_major_ver
; /*!< min. major version used */
713 unsigned char min_minor_ver
; /*!< min. minor version used */
719 unsigned int endpoint
: 1; /*!< 0: client, 1: server */
720 unsigned int transport
: 1; /*!< stream (TLS) or datagram (DTLS) */
721 unsigned int authmode
: 2; /*!< MBEDTLS_SSL_VERIFY_XXX */
722 /* needed even with renego disabled for LEGACY_BREAK_HANDSHAKE */
723 unsigned int allow_legacy_renegotiation
: 2 ; /*!< MBEDTLS_LEGACY_XXX */
724 #if defined(MBEDTLS_ARC4_C)
725 unsigned int arc4_disabled
: 1; /*!< blacklist RC4 ciphersuites? */
727 #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
728 unsigned int mfl_code
: 3; /*!< desired fragment length */
730 #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
731 unsigned int encrypt_then_mac
: 1 ; /*!< negotiate encrypt-then-mac? */
733 #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
734 unsigned int extended_ms
: 1; /*!< negotiate extended master secret? */
736 #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
737 unsigned int anti_replay
: 1; /*!< detect and prevent replay? */
739 #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
740 unsigned int cbc_record_splitting
: 1; /*!< do cbc record splitting */
742 #if defined(MBEDTLS_SSL_RENEGOTIATION)
743 unsigned int disable_renegotiation
: 1; /*!< disable renegotiation? */
745 #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
746 unsigned int trunc_hmac
: 1; /*!< negotiate truncated hmac? */
748 #if defined(MBEDTLS_SSL_SESSION_TICKETS)
749 unsigned int session_tickets
: 1; /*!< use session tickets? */
751 #if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
752 unsigned int fallback
: 1; /*!< is this a fallback? */
757 struct mbedtls_ssl_context
759 const mbedtls_ssl_config
*conf
; /*!< configuration information */
764 int state
; /*!< SSL handshake: current state */
765 #if defined(MBEDTLS_SSL_RENEGOTIATION)
766 int renego_status
; /*!< Initial, in progress, pending? */
767 int renego_records_seen
; /*!< Records since renego request, or with DTLS,
768 number of retransmissions of request if
769 renego_max_records is < 0 */
772 int major_ver
; /*!< equal to MBEDTLS_SSL_MAJOR_VERSION_3 */
773 int minor_ver
; /*!< either 0 (SSL3) or 1 (TLS1.0) */
775 #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
776 unsigned badmac_seen
; /*!< records with a bad MAC received */
779 mbedtls_ssl_send_t
*f_send
; /*!< Callback for network send */
780 mbedtls_ssl_recv_t
*f_recv
; /*!< Callback for network receive */
781 mbedtls_ssl_recv_timeout_t
*f_recv_timeout
;
782 /*!< Callback for network receive with timeout */
784 void *p_bio
; /*!< context for I/O operations */
789 mbedtls_ssl_session
*session_in
; /*!< current session data (in) */
790 mbedtls_ssl_session
*session_out
; /*!< current session data (out) */
791 mbedtls_ssl_session
*session
; /*!< negotiated session data */
792 mbedtls_ssl_session
*session_negotiate
; /*!< session data in negotiation */
794 mbedtls_ssl_handshake_params
*handshake
; /*!< params required only during
795 the handshake process */
798 * Record layer transformations
800 mbedtls_ssl_transform
*transform_in
; /*!< current transform params (in) */
801 mbedtls_ssl_transform
*transform_out
; /*!< current transform params (in) */
802 mbedtls_ssl_transform
*transform
; /*!< negotiated transform params */
803 mbedtls_ssl_transform
*transform_negotiate
; /*!< transform params in negotiation */
808 void *p_timer
; /*!< context for the timer callbacks */
810 mbedtls_ssl_set_timer_t
*f_set_timer
; /*!< set timer callback */
811 mbedtls_ssl_get_timer_t
*f_get_timer
; /*!< get timer callback */
814 * Record layer (incoming data)
816 unsigned char *in_buf
; /*!< input buffer */
817 unsigned char *in_ctr
; /*!< 64-bit incoming message counter
818 TLS: maintained by us
819 DTLS: read from peer */
820 unsigned char *in_hdr
; /*!< start of record header */
821 unsigned char *in_len
; /*!< two-bytes message length field */
822 unsigned char *in_iv
; /*!< ivlen-byte IV */
823 unsigned char *in_msg
; /*!< message contents (in_iv+ivlen) */
824 unsigned char *in_offt
; /*!< read offset in application data */
826 int in_msgtype
; /*!< record header: message type */
827 size_t in_msglen
; /*!< record header: message length */
828 size_t in_left
; /*!< amount of data read so far */
829 #if defined(MBEDTLS_SSL_PROTO_DTLS)
830 uint16_t in_epoch
; /*!< DTLS epoch for incoming records */
831 size_t next_record_offset
; /*!< offset of the next record in datagram
832 (equal to in_left if none) */
834 #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
835 uint64_t in_window_top
; /*!< last validated record seq_num */
836 uint64_t in_window
; /*!< bitmask for replay detection */
839 size_t in_hslen
; /*!< current handshake message length,
840 including the handshake header */
841 int nb_zero
; /*!< # of 0-length encrypted messages */
842 int record_read
; /*!< record is already present */
845 * Record layer (outgoing data)
847 unsigned char *out_buf
; /*!< output buffer */
848 unsigned char *out_ctr
; /*!< 64-bit outgoing message counter */
849 unsigned char *out_hdr
; /*!< start of record header */
850 unsigned char *out_len
; /*!< two-bytes message length field */
851 unsigned char *out_iv
; /*!< ivlen-byte IV */
852 unsigned char *out_msg
; /*!< message contents (out_iv+ivlen) */
854 int out_msgtype
; /*!< record header: message type */
855 size_t out_msglen
; /*!< record header: message length */
856 size_t out_left
; /*!< amount of data not yet written */
858 #if defined(MBEDTLS_ZLIB_SUPPORT)
859 unsigned char *compress_buf
; /*!< zlib data buffer */
861 #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
862 signed char split_done
; /*!< current record already splitted? */
868 int client_auth
; /*!< flag for client auth. */
873 #if defined(MBEDTLS_X509_CRT_PARSE_C)
874 char *hostname
; /*!< expected peer CN for verification
875 (and SNI if available) */
878 #if defined(MBEDTLS_SSL_ALPN)
879 const char *alpn_chosen
; /*!< negotiated protocol */
883 * Information for DTLS hello verify
885 #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
886 unsigned char *cli_id
; /*!< transport-level ID of the client */
887 size_t cli_id_len
; /*!< length of cli_id */
891 * Secure renegotiation
893 /* needed to know when to send extension on server */
894 int secure_renegotiation
; /*!< does peer support legacy or
895 secure renegotiation */
896 #if defined(MBEDTLS_SSL_RENEGOTIATION)
897 size_t verify_data_len
; /*!< length of verify data stored */
898 char own_verify_data
[MBEDTLS_SSL_VERIFY_DATA_MAX_LEN
]; /*!< previous handshake verify data */
899 char peer_verify_data
[MBEDTLS_SSL_VERIFY_DATA_MAX_LEN
]; /*!< previous handshake verify data */
903 #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
905 #define MBEDTLS_SSL_CHANNEL_OUTBOUND 0
906 #define MBEDTLS_SSL_CHANNEL_INBOUND 1
908 extern int (*mbedtls_ssl_hw_record_init
)(mbedtls_ssl_context
*ssl
,
909 const unsigned char *key_enc
, const unsigned char *key_dec
,
911 const unsigned char *iv_enc
, const unsigned char *iv_dec
,
913 const unsigned char *mac_enc
, const unsigned char *mac_dec
,
915 extern int (*mbedtls_ssl_hw_record_activate
)(mbedtls_ssl_context
*ssl
, int direction
);
916 extern int (*mbedtls_ssl_hw_record_reset
)(mbedtls_ssl_context
*ssl
);
917 extern int (*mbedtls_ssl_hw_record_write
)(mbedtls_ssl_context
*ssl
);
918 extern int (*mbedtls_ssl_hw_record_read
)(mbedtls_ssl_context
*ssl
);
919 extern int (*mbedtls_ssl_hw_record_finish
)(mbedtls_ssl_context
*ssl
);
920 #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
923 * \brief Returns the list of ciphersuites supported by the SSL/TLS module.
925 * \return a statically allocated array of ciphersuites, the last
928 const int *mbedtls_ssl_list_ciphersuites( void );
931 * \brief Return the name of the ciphersuite associated with the
934 * \param ciphersuite_id SSL ciphersuite ID
936 * \return a string containing the ciphersuite name
938 const char *mbedtls_ssl_get_ciphersuite_name( const int ciphersuite_id
);
941 * \brief Return the ID of the ciphersuite associated with the
944 * \param ciphersuite_name SSL ciphersuite name
946 * \return the ID with the ciphersuite or 0 if not found
948 int mbedtls_ssl_get_ciphersuite_id( const char *ciphersuite_name
);
951 * \brief Initialize an SSL context
952 * Just makes the context ready for mbedtls_ssl_setup() or
955 * \param ssl SSL context
957 void mbedtls_ssl_init( mbedtls_ssl_context
*ssl
);
960 * \brief Set up an SSL context for use
962 * \note No copy of the configuration context is made, it can be
963 * shared by many mbedtls_ssl_context structures.
965 * \warning Modifying the conf structure after it has been used in this
966 * function is unsupported!
968 * \param ssl SSL context
969 * \param conf SSL configuration to use
971 * \return 0 if successful, or MBEDTLS_ERR_SSL_ALLOC_FAILED if
972 * memory allocation failed
974 int mbedtls_ssl_setup( mbedtls_ssl_context
*ssl
,
975 const mbedtls_ssl_config
*conf
);
978 * \brief Reset an already initialized SSL context for re-use
979 * while retaining application-set variables, function
982 * \param ssl SSL context
983 * \return 0 if successful, or MBEDTLS_ERR_SSL_ALLOC_FAILED,
984 MBEDTLS_ERR_SSL_HW_ACCEL_FAILED or
985 * MBEDTLS_ERR_SSL_COMPRESSION_FAILED
987 int mbedtls_ssl_session_reset( mbedtls_ssl_context
*ssl
);
990 * \brief Set the current endpoint type
992 * \param conf SSL configuration
993 * \param endpoint must be MBEDTLS_SSL_IS_CLIENT or MBEDTLS_SSL_IS_SERVER
995 void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config
*conf
, int endpoint
);
998 * \brief Set the transport type (TLS or DTLS).
1001 * \note For DTLS, you must either provide a recv callback that
1002 * doesn't block, or one that handles timeouts, see
1003 * \c mbedtls_ssl_set_bio(). You also need to provide timer
1004 * callbacks with \c mbedtls_ssl_set_timer_cb().
1006 * \param conf SSL configuration
1007 * \param transport transport type:
1008 * MBEDTLS_SSL_TRANSPORT_STREAM for TLS,
1009 * MBEDTLS_SSL_TRANSPORT_DATAGRAM for DTLS.
1011 void mbedtls_ssl_conf_transport( mbedtls_ssl_config
*conf
, int transport
);
1014 * \brief Set the certificate verification mode
1015 * Default: NONE on server, REQUIRED on client
1017 * \param conf SSL configuration
1018 * \param authmode can be:
1020 * MBEDTLS_SSL_VERIFY_NONE: peer certificate is not checked
1021 * (default on server)
1022 * (insecure on client)
1024 * MBEDTLS_SSL_VERIFY_OPTIONAL: peer certificate is checked, however the
1025 * handshake continues even if verification failed;
1026 * mbedtls_ssl_get_verify_result() can be called after the
1027 * handshake is complete.
1029 * MBEDTLS_SSL_VERIFY_REQUIRED: peer *must* present a valid certificate,
1030 * handshake is aborted if verification failed.
1031 * (default on client)
1033 * \note On client, MBEDTLS_SSL_VERIFY_REQUIRED is the recommended mode.
1034 * With MBEDTLS_SSL_VERIFY_OPTIONAL, the user needs to call mbedtls_ssl_get_verify_result() at
1035 * the right time(s), which may not be obvious, while REQUIRED always perform
1036 * the verification as soon as possible. For example, REQUIRED was protecting
1037 * against the "triple handshake" attack even before it was found.
1039 void mbedtls_ssl_conf_authmode( mbedtls_ssl_config
*conf
, int authmode
);
1041 #if defined(MBEDTLS_X509_CRT_PARSE_C)
1043 * \brief Set the verification callback (Optional).
1045 * If set, the verify callback is called for each
1046 * certificate in the chain. For implementation
1047 * information, please see \c x509parse_verify()
1049 * \param conf SSL configuration
1050 * \param f_vrfy verification function
1051 * \param p_vrfy verification parameter
1053 void mbedtls_ssl_conf_verify( mbedtls_ssl_config
*conf
,
1054 int (*f_vrfy
)(void *, mbedtls_x509_crt
*, int, uint32_t *),
1056 #endif /* MBEDTLS_X509_CRT_PARSE_C */
1059 * \brief Set the random number generator callback
1061 * \param conf SSL configuration
1062 * \param f_rng RNG function
1063 * \param p_rng RNG parameter
1065 void mbedtls_ssl_conf_rng( mbedtls_ssl_config
*conf
,
1066 int (*f_rng
)(void *, unsigned char *, size_t),
1070 * \brief Set the debug callback
1072 * The callback has the following argument:
1073 * void * opaque context for the callback
1075 * const char * file name
1077 * const char * message
1079 * \param conf SSL configuration
1080 * \param f_dbg debug function
1081 * \param p_dbg debug parameter
1083 void mbedtls_ssl_conf_dbg( mbedtls_ssl_config
*conf
,
1084 void (*f_dbg
)(void *, int, const char *, int, const char *),
1088 * \brief Set the underlying BIO callbacks for write, read and
1089 * read-with-timeout.
1091 * \param ssl SSL context
1092 * \param p_bio parameter (context) shared by BIO callbacks
1093 * \param f_send write callback
1094 * \param f_recv read callback
1095 * \param f_recv_timeout blocking read callback with timeout.
1097 * \note One of f_recv or f_recv_timeout can be NULL, in which case
1098 * the other is used. If both are non-NULL, f_recv_timeout is
1099 * used and f_recv is ignored (as if it were NULL).
1101 * \note The two most common use cases are:
1102 * - non-blocking I/O, f_recv != NULL, f_recv_timeout == NULL
1103 * - blocking I/O, f_recv == NULL, f_recv_timout != NULL
1105 * \note For DTLS, you need to provide either a non-NULL
1106 * f_recv_timeout callback, or a f_recv that doesn't block.
1108 * \note See the documentations of \c mbedtls_ssl_sent_t,
1109 * \c mbedtls_ssl_recv_t and \c mbedtls_ssl_recv_timeout_t for
1110 * the conventions those callbacks must follow.
1112 * \note On some platforms, net_sockets.c provides
1113 * \c mbedtls_net_send(), \c mbedtls_net_recv() and
1114 * \c mbedtls_net_recv_timeout() that are suitable to be used
1117 void mbedtls_ssl_set_bio( mbedtls_ssl_context
*ssl
,
1119 mbedtls_ssl_send_t
*f_send
,
1120 mbedtls_ssl_recv_t
*f_recv
,
1121 mbedtls_ssl_recv_timeout_t
*f_recv_timeout
);
1124 * \brief Set the timeout period for mbedtls_ssl_read()
1125 * (Default: no timeout.)
1127 * \param conf SSL configuration context
1128 * \param timeout Timeout value in milliseconds.
1129 * Use 0 for no timeout (default).
1131 * \note With blocking I/O, this will only work if a non-NULL
1132 * \c f_recv_timeout was set with \c mbedtls_ssl_set_bio().
1133 * With non-blocking I/O, this will only work if timer
1134 * callbacks were set with \c mbedtls_ssl_set_timer_cb().
1136 * \note With non-blocking I/O, you may also skip this function
1137 * altogether and handle timeouts at the application layer.
1139 void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config
*conf
, uint32_t timeout
);
1142 * \brief Set the timer callbacks (Mandatory for DTLS.)
1144 * \param ssl SSL context
1145 * \param p_timer parameter (context) shared by timer callbacks
1146 * \param f_set_timer set timer callback
1147 * \param f_get_timer get timer callback. Must return:
1149 * \note See the documentation of \c mbedtls_ssl_set_timer_t and
1150 * \c mbedtls_ssl_get_timer_t for the conventions this pair of
1151 * callbacks must fallow.
1153 * \note On some platforms, timing.c provides
1154 * \c mbedtls_timing_set_delay() and
1155 * \c mbedtls_timing_get_delay() that are suitable for using
1156 * here, except if using an event-driven style.
1158 * \note See also the "DTLS tutorial" article in our knowledge base.
1159 * https://tls.mbed.org/kb/how-to/dtls-tutorial
1161 void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context
*ssl
,
1163 mbedtls_ssl_set_timer_t
*f_set_timer
,
1164 mbedtls_ssl_get_timer_t
*f_get_timer
);
1167 * \brief Callback type: generate and write session ticket
1169 * \note This describes what a callback implementation should do.
1170 * This callback should generate an encrypted and
1171 * authenticated ticket for the session and write it to the
1172 * output buffer. Here, ticket means the opaque ticket part
1173 * of the NewSessionTicket structure of RFC 5077.
1175 * \param p_ticket Context for the callback
1176 * \param session SSL session to be written in the ticket
1177 * \param start Start of the output buffer
1178 * \param end End of the output buffer
1179 * \param tlen On exit, holds the length written
1180 * \param lifetime On exit, holds the lifetime of the ticket in seconds
1182 * \return 0 if successful, or
1183 * a specific MBEDTLS_ERR_XXX code.
1185 typedef int mbedtls_ssl_ticket_write_t( void *p_ticket
,
1186 const mbedtls_ssl_session
*session
,
1187 unsigned char *start
,
1188 const unsigned char *end
,
1190 uint32_t *lifetime
);
1192 #if defined(MBEDTLS_SSL_EXPORT_KEYS)
1194 * \brief Callback type: Export key block and master secret
1196 * \note This is required for certain uses of TLS, e.g. EAP-TLS
1197 * (RFC 5216) and Thread. The key pointers are ephemeral and
1198 * therefore must not be stored. The master secret and keys
1199 * should not be used directly except as an input to a key
1200 * derivation function.
1202 * \param p_expkey Context for the callback
1203 * \param ms Pointer to master secret (fixed length: 48 bytes)
1204 * \param kb Pointer to key block, see RFC 5246 section 6.3
1205 * (variable length: 2 * maclen + 2 * keylen + 2 * ivlen).
1206 * \param maclen MAC length
1207 * \param keylen Key length
1208 * \param ivlen IV length
1210 * \return 0 if successful, or
1211 * a specific MBEDTLS_ERR_XXX code.
1213 typedef int mbedtls_ssl_export_keys_t( void *p_expkey
,
1214 const unsigned char *ms
,
1215 const unsigned char *kb
,
1219 #endif /* MBEDTLS_SSL_EXPORT_KEYS */
1222 * \brief Callback type: parse and load session ticket
1224 * \note This describes what a callback implementation should do.
1225 * This callback should parse a session ticket as generated
1226 * by the corresponding mbedtls_ssl_ticket_write_t function,
1227 * and, if the ticket is authentic and valid, load the
1230 * \note The implementation is allowed to modify the first len
1231 * bytes of the input buffer, eg to use it as a temporary
1232 * area for the decrypted ticket contents.
1234 * \param p_ticket Context for the callback
1235 * \param session SSL session to be loaded
1236 * \param buf Start of the buffer containing the ticket
1237 * \param len Length of the ticket.
1239 * \return 0 if successful, or
1240 * MBEDTLS_ERR_SSL_INVALID_MAC if not authentic, or
1241 * MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED if expired, or
1242 * any other non-zero code for other failures.
1244 typedef int mbedtls_ssl_ticket_parse_t( void *p_ticket
,
1245 mbedtls_ssl_session
*session
,
1249 #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_SRV_C)
1251 * \brief Configure SSL session ticket callbacks (server only).
1254 * \note On server, session tickets are enabled by providing
1255 * non-NULL callbacks.
1257 * \note On client, use \c mbedtls_ssl_conf_session_tickets().
1259 * \param conf SSL configuration context
1260 * \param f_ticket_write Callback for writing a ticket
1261 * \param f_ticket_parse Callback for parsing a ticket
1262 * \param p_ticket Context shared by the two callbacks
1264 void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config
*conf
,
1265 mbedtls_ssl_ticket_write_t
*f_ticket_write
,
1266 mbedtls_ssl_ticket_parse_t
*f_ticket_parse
,
1268 #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_SRV_C */
1270 #if defined(MBEDTLS_SSL_EXPORT_KEYS)
1272 * \brief Configure key export callback.
1275 * \note See \c mbedtls_ssl_export_keys_t.
1277 * \param conf SSL configuration context
1278 * \param f_export_keys Callback for exporting keys
1279 * \param p_export_keys Context for the callback
1281 void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config
*conf
,
1282 mbedtls_ssl_export_keys_t
*f_export_keys
,
1283 void *p_export_keys
);
1284 #endif /* MBEDTLS_SSL_EXPORT_KEYS */
1287 * \brief Callback type: generate a cookie
1289 * \param ctx Context for the callback
1290 * \param p Buffer to write to,
1291 * must be updated to point right after the cookie
1292 * \param end Pointer to one past the end of the output buffer
1293 * \param info Client ID info that was passed to
1294 * \c mbedtls_ssl_set_client_transport_id()
1295 * \param ilen Length of info in bytes
1297 * \return The callback must return 0 on success,
1298 * or a negative error code.
1300 typedef int mbedtls_ssl_cookie_write_t( void *ctx
,
1301 unsigned char **p
, unsigned char *end
,
1302 const unsigned char *info
, size_t ilen
);
1305 * \brief Callback type: verify a cookie
1307 * \param ctx Context for the callback
1308 * \param cookie Cookie to verify
1309 * \param clen Length of cookie
1310 * \param info Client ID info that was passed to
1311 * \c mbedtls_ssl_set_client_transport_id()
1312 * \param ilen Length of info in bytes
1314 * \return The callback must return 0 if cookie is valid,
1315 * or a negative error code.
1317 typedef int mbedtls_ssl_cookie_check_t( void *ctx
,
1318 const unsigned char *cookie
, size_t clen
,
1319 const unsigned char *info
, size_t ilen
);
1321 #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
1323 * \brief Register callbacks for DTLS cookies
1324 * (Server only. DTLS only.)
1326 * Default: dummy callbacks that fail, in order to force you to
1327 * register working callbacks (and initialize their context).
1329 * To disable HelloVerifyRequest, register NULL callbacks.
1331 * \warning Disabling hello verification allows your server to be used
1332 * for amplification in DoS attacks against other hosts.
1333 * Only disable if you known this can't happen in your
1334 * particular environment.
1336 * \note See comments on \c mbedtls_ssl_handshake() about handling
1337 * the MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED that is expected
1338 * on the first handshake attempt when this is enabled.
1340 * \note This is also necessary to handle client reconnection from
1341 * the same port as described in RFC 6347 section 4.2.8 (only
1342 * the variant with cookies is supported currently). See
1343 * comments on \c mbedtls_ssl_read() for details.
1345 * \param conf SSL configuration
1346 * \param f_cookie_write Cookie write callback
1347 * \param f_cookie_check Cookie check callback
1348 * \param p_cookie Context for both callbacks
1350 void mbedtls_ssl_conf_dtls_cookies( mbedtls_ssl_config
*conf
,
1351 mbedtls_ssl_cookie_write_t
*f_cookie_write
,
1352 mbedtls_ssl_cookie_check_t
*f_cookie_check
,
1356 * \brief Set client's transport-level identification info.
1357 * (Server only. DTLS only.)
1359 * This is usually the IP address (and port), but could be
1360 * anything identify the client depending on the underlying
1361 * network stack. Used for HelloVerifyRequest with DTLS.
1362 * This is *not* used to route the actual packets.
1364 * \param ssl SSL context
1365 * \param info Transport-level info identifying the client (eg IP + port)
1366 * \param ilen Length of info in bytes
1368 * \note An internal copy is made, so the info buffer can be reused.
1370 * \return 0 on success,
1371 * MBEDTLS_ERR_SSL_BAD_INPUT_DATA if used on client,
1372 * MBEDTLS_ERR_SSL_ALLOC_FAILED if out of memory.
1374 int mbedtls_ssl_set_client_transport_id( mbedtls_ssl_context
*ssl
,
1375 const unsigned char *info
,
1378 #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
1380 #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
1382 * \brief Enable or disable anti-replay protection for DTLS.
1383 * (DTLS only, no effect on TLS.)
1386 * \param conf SSL configuration
1387 * \param mode MBEDTLS_SSL_ANTI_REPLAY_ENABLED or MBEDTLS_SSL_ANTI_REPLAY_DISABLED.
1389 * \warning Disabling this is a security risk unless the application
1390 * protocol handles duplicated packets in a safe way. You
1391 * should not disable this without careful consideration.
1392 * However, if your application already detects duplicated
1393 * packets and needs information about them to adjust its
1394 * transmission strategy, then you'll want to disable this.
1396 void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config
*conf
, char mode
);
1397 #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
1399 #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
1401 * \brief Set a limit on the number of records with a bad MAC
1402 * before terminating the connection.
1403 * (DTLS only, no effect on TLS.)
1404 * Default: 0 (disabled).
1406 * \param conf SSL configuration
1407 * \param limit Limit, or 0 to disable.
1409 * \note If the limit is N, then the connection is terminated when
1410 * the Nth non-authentic record is seen.
1412 * \note Records with an invalid header are not counted, only the
1413 * ones going through the authentication-decryption phase.
1415 * \note This is a security trade-off related to the fact that it's
1416 * often relatively easy for an active attacker ot inject UDP
1417 * datagrams. On one hand, setting a low limit here makes it
1418 * easier for such an attacker to forcibly terminated a
1419 * connection. On the other hand, a high limit or no limit
1420 * might make us waste resources checking authentication on
1421 * many bogus packets.
1423 void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config
*conf
, unsigned limit
);
1424 #endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
1426 #if defined(MBEDTLS_SSL_PROTO_DTLS)
1428 * \brief Set retransmit timeout values for the DTLS handshake.
1429 * (DTLS only, no effect on TLS.)
1431 * \param conf SSL configuration
1432 * \param min Initial timeout value in milliseconds.
1433 * Default: 1000 (1 second).
1434 * \param max Maximum timeout value in milliseconds.
1435 * Default: 60000 (60 seconds).
1437 * \note Default values are from RFC 6347 section 4.2.4.1.
1439 * \note The 'min' value should typically be slightly above the
1440 * expected round-trip time to your peer, plus whatever time
1441 * it takes for the peer to process the message. For example,
1442 * if your RTT is about 600ms and you peer needs up to 1s to
1443 * do the cryptographic operations in the handshake, then you
1444 * should set 'min' slightly above 1600. Lower values of 'min'
1445 * might cause spurious resends which waste network resources,
1446 * while larger value of 'min' will increase overall latency
1447 * on unreliable network links.
1449 * \note The more unreliable your network connection is, the larger
1450 * your max / min ratio needs to be in order to achieve
1451 * reliable handshakes.
1453 * \note Messages are retransmitted up to log2(ceil(max/min)) times.
1454 * For example, if min = 1s and max = 5s, the retransmit plan
1455 * goes: send ... 1s -> resend ... 2s -> resend ... 4s ->
1456 * resend ... 5s -> give up and return a timeout error.
1458 void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config
*conf
, uint32_t min
, uint32_t max
);
1459 #endif /* MBEDTLS_SSL_PROTO_DTLS */
1461 #if defined(MBEDTLS_SSL_SRV_C)
1463 * \brief Set the session cache callbacks (server-side only)
1464 * If not set, no session resuming is done (except if session
1465 * tickets are enabled too).
1467 * The session cache has the responsibility to check for stale
1468 * entries based on timeout. See RFC 5246 for recommendations.
1470 * Warning: session.peer_cert is cleared by the SSL/TLS layer on
1471 * connection shutdown, so do not cache the pointer! Either set
1472 * it to NULL or make a full copy of the certificate.
1474 * The get callback is called once during the initial handshake
1475 * to enable session resuming. The get function has the
1476 * following parameters: (void *parameter, mbedtls_ssl_session *session)
1477 * If a valid entry is found, it should fill the master of
1478 * the session object with the cached values and return 0,
1479 * return 1 otherwise. Optionally peer_cert can be set as well
1480 * if it is properly present in cache entry.
1482 * The set callback is called once during the initial handshake
1483 * to enable session resuming after the entire handshake has
1484 * been finished. The set function has the following parameters:
1485 * (void *parameter, const mbedtls_ssl_session *session). The function
1486 * should create a cache entry for future retrieval based on
1487 * the data in the session structure and should keep in mind
1488 * that the mbedtls_ssl_session object presented (and all its referenced
1489 * data) is cleared by the SSL/TLS layer when the connection is
1490 * terminated. It is recommended to add metadata to determine if
1491 * an entry is still valid in the future. Return 0 if
1492 * successfully cached, return 1 otherwise.
1494 * \param conf SSL configuration
1495 * \param p_cache parmater (context) for both callbacks
1496 * \param f_get_cache session get callback
1497 * \param f_set_cache session set callback
1499 void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config
*conf
,
1501 int (*f_get_cache
)(void *, mbedtls_ssl_session
*),
1502 int (*f_set_cache
)(void *, const mbedtls_ssl_session
*) );
1503 #endif /* MBEDTLS_SSL_SRV_C */
1505 #if defined(MBEDTLS_SSL_CLI_C)
1507 * \brief Request resumption of session (client-side only)
1508 * Session data is copied from presented session structure.
1510 * \param ssl SSL context
1511 * \param session session context
1513 * \return 0 if successful,
1514 * MBEDTLS_ERR_SSL_ALLOC_FAILED if memory allocation failed,
1515 * MBEDTLS_ERR_SSL_BAD_INPUT_DATA if used server-side or
1516 * arguments are otherwise invalid
1518 * \sa mbedtls_ssl_get_session()
1520 int mbedtls_ssl_set_session( mbedtls_ssl_context
*ssl
, const mbedtls_ssl_session
*session
);
1521 #endif /* MBEDTLS_SSL_CLI_C */
1524 * \brief Set the list of allowed ciphersuites and the preference
1525 * order. First in the list has the highest preference.
1526 * (Overrides all version-specific lists)
1528 * The ciphersuites array is not copied, and must remain
1529 * valid for the lifetime of the ssl_config.
1531 * Note: The server uses its own preferences
1532 * over the preference of the client unless
1533 * MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE is defined!
1535 * \param conf SSL configuration
1536 * \param ciphersuites 0-terminated list of allowed ciphersuites
1538 void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config
*conf
,
1539 const int *ciphersuites
);
1542 * \brief Set the list of allowed ciphersuites and the
1543 * preference order for a specific version of the protocol.
1544 * (Only useful on the server side)
1546 * The ciphersuites array is not copied, and must remain
1547 * valid for the lifetime of the ssl_config.
1549 * \param conf SSL configuration
1550 * \param ciphersuites 0-terminated list of allowed ciphersuites
1551 * \param major Major version number (only MBEDTLS_SSL_MAJOR_VERSION_3
1553 * \param minor Minor version number (MBEDTLS_SSL_MINOR_VERSION_0,
1554 * MBEDTLS_SSL_MINOR_VERSION_1 and MBEDTLS_SSL_MINOR_VERSION_2,
1555 * MBEDTLS_SSL_MINOR_VERSION_3 supported)
1557 * \note With DTLS, use MBEDTLS_SSL_MINOR_VERSION_2 for DTLS 1.0
1558 * and MBEDTLS_SSL_MINOR_VERSION_3 for DTLS 1.2
1560 void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config
*conf
,
1561 const int *ciphersuites
,
1562 int major
, int minor
);
1564 #if defined(MBEDTLS_X509_CRT_PARSE_C)
1566 * \brief Set the X.509 security profile used for verification
1568 * \note The restrictions are enforced for all certificates in the
1569 * chain. However, signatures in the handshake are not covered
1570 * by this setting but by \b mbedtls_ssl_conf_sig_hashes().
1572 * \param conf SSL configuration
1573 * \param profile Profile to use
1575 void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config
*conf
,
1576 const mbedtls_x509_crt_profile
*profile
);
1579 * \brief Set the data required to verify peer certificate
1581 * \param conf SSL configuration
1582 * \param ca_chain trusted CA chain (meaning all fully trusted top-level CAs)
1583 * \param ca_crl trusted CA CRLs
1585 void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config
*conf
,
1586 mbedtls_x509_crt
*ca_chain
,
1587 mbedtls_x509_crl
*ca_crl
);
1590 * \brief Set own certificate chain and private key
1592 * \note own_cert should contain in order from the bottom up your
1593 * certificate chain. The top certificate (self-signed)
1596 * \note On server, this function can be called multiple times to
1597 * provision more than one cert/key pair (eg one ECDSA, one
1598 * RSA with SHA-256, one RSA with SHA-1). An adequate
1599 * certificate will be selected according to the client's
1600 * advertised capabilities. In case mutliple certificates are
1601 * adequate, preference is given to the one set by the first
1602 * call to this function, then second, etc.
1604 * \note On client, only the first call has any effect. That is,
1605 * only one client certificate can be provisioned. The
1606 * server's preferences in its CertficateRequest message will
1607 * be ignored and our only cert will be sent regardless of
1608 * whether it matches those preferences - the server can then
1609 * decide what it wants to do with it.
1611 * \param conf SSL configuration
1612 * \param own_cert own public certificate chain
1613 * \param pk_key own private key
1615 * \return 0 on success or MBEDTLS_ERR_SSL_ALLOC_FAILED
1617 int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config
*conf
,
1618 mbedtls_x509_crt
*own_cert
,
1619 mbedtls_pk_context
*pk_key
);
1620 #endif /* MBEDTLS_X509_CRT_PARSE_C */
1622 #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1624 * \brief Set the Pre Shared Key (PSK) and the expected identity name
1626 * \note This is mainly useful for clients. Servers will usually
1627 * want to use \c mbedtls_ssl_conf_psk_cb() instead.
1629 * \note Currently clients can only register one pre-shared key.
1630 * In other words, the servers' identity hint is ignored.
1631 * Support for setting multiple PSKs on clients and selecting
1632 * one based on the identity hint is not a planned feature but
1633 * feedback is welcomed.
1635 * \param conf SSL configuration
1636 * \param psk pointer to the pre-shared key
1637 * \param psk_len pre-shared key length
1638 * \param psk_identity pointer to the pre-shared key identity
1639 * \param psk_identity_len identity key length
1641 * \return 0 if successful or MBEDTLS_ERR_SSL_ALLOC_FAILED
1643 int mbedtls_ssl_conf_psk( mbedtls_ssl_config
*conf
,
1644 const unsigned char *psk
, size_t psk_len
,
1645 const unsigned char *psk_identity
, size_t psk_identity_len
);
1649 * \brief Set the Pre Shared Key (PSK) for the current handshake
1651 * \note This should only be called inside the PSK callback,
1652 * ie the function passed to \c mbedtls_ssl_conf_psk_cb().
1654 * \param ssl SSL context
1655 * \param psk pointer to the pre-shared key
1656 * \param psk_len pre-shared key length
1658 * \return 0 if successful or MBEDTLS_ERR_SSL_ALLOC_FAILED
1660 int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context
*ssl
,
1661 const unsigned char *psk
, size_t psk_len
);
1664 * \brief Set the PSK callback (server-side only).
1666 * If set, the PSK callback is called for each
1667 * handshake where a PSK ciphersuite was negotiated.
1668 * The caller provides the identity received and wants to
1669 * receive the actual PSK data and length.
1671 * The callback has the following parameters: (void *parameter,
1672 * mbedtls_ssl_context *ssl, const unsigned char *psk_identity,
1673 * size_t identity_len)
1674 * If a valid PSK identity is found, the callback should use
1675 * \c mbedtls_ssl_set_hs_psk() on the ssl context to set the
1676 * correct PSK and return 0.
1677 * Any other return value will result in a denied PSK identity.
1679 * \note If you set a PSK callback using this function, then you
1680 * don't need to set a PSK key and identity using
1681 * \c mbedtls_ssl_conf_psk().
1683 * \param conf SSL configuration
1684 * \param f_psk PSK identity function
1685 * \param p_psk PSK identity parameter
1687 void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config
*conf
,
1688 int (*f_psk
)(void *, mbedtls_ssl_context
*, const unsigned char *,
1691 #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
1693 #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
1695 * \brief Set the Diffie-Hellman public P and G values,
1696 * read as hexadecimal strings (server-side only)
1697 * (Default: MBEDTLS_DHM_RFC5114_MODP_2048_[PG])
1699 * \param conf SSL configuration
1700 * \param dhm_P Diffie-Hellman-Merkle modulus
1701 * \param dhm_G Diffie-Hellman-Merkle generator
1703 * \return 0 if successful
1705 int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config
*conf
, const char *dhm_P
, const char *dhm_G
);
1708 * \brief Set the Diffie-Hellman public P and G values,
1709 * read from existing context (server-side only)
1711 * \param conf SSL configuration
1712 * \param dhm_ctx Diffie-Hellman-Merkle context
1714 * \return 0 if successful
1716 int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config
*conf
, mbedtls_dhm_context
*dhm_ctx
);
1717 #endif /* MBEDTLS_DHM_C && defined(MBEDTLS_SSL_SRV_C) */
1719 #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
1721 * \brief Set the minimum length for Diffie-Hellman parameters.
1722 * (Client-side only.)
1723 * (Default: 1024 bits.)
1725 * \param conf SSL configuration
1726 * \param bitlen Minimum bit length of the DHM prime
1728 void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config
*conf
,
1729 unsigned int bitlen
);
1730 #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
1732 #if defined(MBEDTLS_ECP_C)
1734 * \brief Set the allowed curves in order of preference.
1735 * (Default: all defined curves.)
1737 * On server: this only affects selection of the ECDHE curve;
1738 * the curves used for ECDH and ECDSA are determined by the
1739 * list of available certificates instead.
1741 * On client: this affects the list of curves offered for any
1742 * use. The server can override our preference order.
1744 * Both sides: limits the set of curves accepted for use in
1745 * ECDHE and in the peer's end-entity certificate.
1747 * \note This has no influence on which curves are allowed inside the
1748 * certificate chains, see \c mbedtls_ssl_conf_cert_profile()
1749 * for that. For the end-entity certificate however, the key
1750 * will be accepted only if it is allowed both by this list
1751 * and by the cert profile.
1753 * \note This list should be ordered by decreasing preference
1754 * (preferred curve first).
1756 * \param conf SSL configuration
1757 * \param curves Ordered list of allowed curves,
1758 * terminated by MBEDTLS_ECP_DP_NONE.
1760 void mbedtls_ssl_conf_curves( mbedtls_ssl_config
*conf
,
1761 const mbedtls_ecp_group_id
*curves
);
1762 #endif /* MBEDTLS_ECP_C */
1764 #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
1766 * \brief Set the allowed hashes for signatures during the handshake.
1767 * (Default: all available hashes except MD5.)
1769 * \note This only affects which hashes are offered and can be used
1770 * for signatures during the handshake. Hashes for message
1771 * authentication and the TLS PRF are controlled by the
1772 * ciphersuite, see \c mbedtls_ssl_conf_ciphersuites(). Hashes
1773 * used for certificate signature are controlled by the
1774 * verification profile, see \c mbedtls_ssl_conf_cert_profile().
1776 * \note This list should be ordered by decreasing preference
1777 * (preferred hash first).
1779 * \param conf SSL configuration
1780 * \param hashes Ordered list of allowed signature hashes,
1781 * terminated by \c MBEDTLS_MD_NONE.
1783 void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config
*conf
,
1784 const int *hashes
);
1785 #endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
1787 #if defined(MBEDTLS_X509_CRT_PARSE_C)
1789 * \brief Set the hostname to check against the received server
1790 * certificate. It sets the ServerName TLS extension too,
1791 * if the extension is enabled.
1792 * (client-side only)
1794 * \param ssl SSL context
1795 * \param hostname the server hostname
1797 * \return 0 if successful or MBEDTLS_ERR_SSL_ALLOC_FAILED
1799 int mbedtls_ssl_set_hostname( mbedtls_ssl_context
*ssl
, const char *hostname
);
1800 #endif /* MBEDTLS_X509_CRT_PARSE_C */
1802 #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1804 * \brief Set own certificate and key for the current handshake
1806 * \note Same as \c mbedtls_ssl_conf_own_cert() but for use within
1809 * \param ssl SSL context
1810 * \param own_cert own public certificate chain
1811 * \param pk_key own private key
1813 * \return 0 on success or MBEDTLS_ERR_SSL_ALLOC_FAILED
1815 int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context
*ssl
,
1816 mbedtls_x509_crt
*own_cert
,
1817 mbedtls_pk_context
*pk_key
);
1820 * \brief Set the data required to verify peer certificate for the
1823 * \note Same as \c mbedtls_ssl_conf_ca_chain() but for use within
1826 * \param ssl SSL context
1827 * \param ca_chain trusted CA chain (meaning all fully trusted top-level CAs)
1828 * \param ca_crl trusted CA CRLs
1830 void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context
*ssl
,
1831 mbedtls_x509_crt
*ca_chain
,
1832 mbedtls_x509_crl
*ca_crl
);
1835 * \brief Set authmode for the current handshake.
1837 * \note Same as \c mbedtls_ssl_conf_authmode() but for use within
1840 * \param ssl SSL context
1841 * \param authmode MBEDTLS_SSL_VERIFY_NONE, MBEDTLS_SSL_VERIFY_OPTIONAL or
1842 * MBEDTLS_SSL_VERIFY_REQUIRED
1844 void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context
*ssl
,
1848 * \brief Set server side ServerName TLS extension callback
1849 * (optional, server-side only).
1851 * If set, the ServerName callback is called whenever the
1852 * server receives a ServerName TLS extension from the client
1853 * during a handshake. The ServerName callback has the
1854 * following parameters: (void *parameter, mbedtls_ssl_context *ssl,
1855 * const unsigned char *hostname, size_t len). If a suitable
1856 * certificate is found, the callback must set the
1857 * certificate(s) and key(s) to use with \c
1858 * mbedtls_ssl_set_hs_own_cert() (can be called repeatedly),
1859 * and may optionally adjust the CA and associated CRL with \c
1860 * mbedtls_ssl_set_hs_ca_chain() as well as the client
1861 * authentication mode with \c mbedtls_ssl_set_hs_authmode(),
1862 * then must return 0. If no matching name is found, the
1863 * callback must either set a default cert, or
1864 * return non-zero to abort the handshake at this point.
1866 * \param conf SSL configuration
1867 * \param f_sni verification function
1868 * \param p_sni verification parameter
1870 void mbedtls_ssl_conf_sni( mbedtls_ssl_config
*conf
,
1871 int (*f_sni
)(void *, mbedtls_ssl_context
*, const unsigned char *,
1874 #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1876 #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1878 * \brief Set the EC J-PAKE password for current handshake.
1880 * \note An internal copy is made, and destroyed as soon as the
1881 * handshake is completed, or when the SSL context is reset or
1884 * \note The SSL context needs to be already set up. The right place
1885 * to call this function is between \c mbedtls_ssl_setup() or
1886 * \c mbedtls_ssl_reset() and \c mbedtls_ssl_handshake().
1888 * \param ssl SSL context
1889 * \param pw EC J-PAKE password (pre-shared secret)
1890 * \param pw_len length of pw in bytes
1892 * \return 0 on success, or a negative error code.
1894 int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context
*ssl
,
1895 const unsigned char *pw
,
1897 #endif /*MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
1899 #if defined(MBEDTLS_SSL_ALPN)
1901 * \brief Set the supported Application Layer Protocols.
1903 * \param conf SSL configuration
1904 * \param protos Pointer to a NULL-terminated list of supported protocols,
1905 * in decreasing preference order. The pointer to the list is
1906 * recorded by the library for later reference as required, so
1907 * the lifetime of the table must be atleast as long as the
1908 * lifetime of the SSL configuration structure.
1910 * \return 0 on success, or MBEDTLS_ERR_SSL_BAD_INPUT_DATA.
1912 int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config
*conf
, const char **protos
);
1915 * \brief Get the name of the negotiated Application Layer Protocol.
1916 * This function should be called after the handshake is
1919 * \param ssl SSL context
1921 * \return Protcol name, or NULL if no protocol was negotiated.
1923 const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context
*ssl
);
1924 #endif /* MBEDTLS_SSL_ALPN */
1927 * \brief Set the maximum supported version sent from the client side
1928 * and/or accepted at the server side
1929 * (Default: MBEDTLS_SSL_MAX_MAJOR_VERSION, MBEDTLS_SSL_MAX_MINOR_VERSION)
1931 * \note This ignores ciphersuites from higher versions.
1933 * \note With DTLS, use MBEDTLS_SSL_MINOR_VERSION_2 for DTLS 1.0 and
1934 * MBEDTLS_SSL_MINOR_VERSION_3 for DTLS 1.2
1936 * \param conf SSL configuration
1937 * \param major Major version number (only MBEDTLS_SSL_MAJOR_VERSION_3 supported)
1938 * \param minor Minor version number (MBEDTLS_SSL_MINOR_VERSION_0,
1939 * MBEDTLS_SSL_MINOR_VERSION_1 and MBEDTLS_SSL_MINOR_VERSION_2,
1940 * MBEDTLS_SSL_MINOR_VERSION_3 supported)
1942 void mbedtls_ssl_conf_max_version( mbedtls_ssl_config
*conf
, int major
, int minor
);
1945 * \brief Set the minimum accepted SSL/TLS protocol version
1946 * (Default: TLS 1.0)
1948 * \note Input outside of the SSL_MAX_XXXXX_VERSION and
1949 * SSL_MIN_XXXXX_VERSION range is ignored.
1951 * \note MBEDTLS_SSL_MINOR_VERSION_0 (SSL v3) should be avoided.
1953 * \note With DTLS, use MBEDTLS_SSL_MINOR_VERSION_2 for DTLS 1.0 and
1954 * MBEDTLS_SSL_MINOR_VERSION_3 for DTLS 1.2
1956 * \param conf SSL configuration
1957 * \param major Major version number (only MBEDTLS_SSL_MAJOR_VERSION_3 supported)
1958 * \param minor Minor version number (MBEDTLS_SSL_MINOR_VERSION_0,
1959 * MBEDTLS_SSL_MINOR_VERSION_1 and MBEDTLS_SSL_MINOR_VERSION_2,
1960 * MBEDTLS_SSL_MINOR_VERSION_3 supported)
1962 void mbedtls_ssl_conf_min_version( mbedtls_ssl_config
*conf
, int major
, int minor
);
1964 #if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
1966 * \brief Set the fallback flag (client-side only).
1967 * (Default: MBEDTLS_SSL_IS_NOT_FALLBACK).
1969 * \note Set to MBEDTLS_SSL_IS_FALLBACK when preparing a fallback
1970 * connection, that is a connection with max_version set to a
1971 * lower value than the value you're willing to use. Such
1972 * fallback connections are not recommended but are sometimes
1973 * necessary to interoperate with buggy (version-intolerant)
1976 * \warning You should NOT set this to MBEDTLS_SSL_IS_FALLBACK for
1977 * non-fallback connections! This would appear to work for a
1978 * while, then cause failures when the server is upgraded to
1979 * support a newer TLS version.
1981 * \param conf SSL configuration
1982 * \param fallback MBEDTLS_SSL_IS_NOT_FALLBACK or MBEDTLS_SSL_IS_FALLBACK
1984 void mbedtls_ssl_conf_fallback( mbedtls_ssl_config
*conf
, char fallback
);
1985 #endif /* MBEDTLS_SSL_FALLBACK_SCSV && MBEDTLS_SSL_CLI_C */
1987 #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1989 * \brief Enable or disable Encrypt-then-MAC
1990 * (Default: MBEDTLS_SSL_ETM_ENABLED)
1992 * \note This should always be enabled, it is a security
1993 * improvement, and should not cause any interoperability
1994 * issue (used only if the peer supports it too).
1996 * \param conf SSL configuration
1997 * \param etm MBEDTLS_SSL_ETM_ENABLED or MBEDTLS_SSL_ETM_DISABLED
1999 void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config
*conf
, char etm
);
2000 #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
2002 #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
2004 * \brief Enable or disable Extended Master Secret negotiation.
2005 * (Default: MBEDTLS_SSL_EXTENDED_MS_ENABLED)
2007 * \note This should always be enabled, it is a security fix to the
2008 * protocol, and should not cause any interoperability issue
2009 * (used only if the peer supports it too).
2011 * \param conf SSL configuration
2012 * \param ems MBEDTLS_SSL_EXTENDED_MS_ENABLED or MBEDTLS_SSL_EXTENDED_MS_DISABLED
2014 void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config
*conf
, char ems
);
2015 #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
2017 #if defined(MBEDTLS_ARC4_C)
2019 * \brief Disable or enable support for RC4
2020 * (Default: MBEDTLS_SSL_ARC4_DISABLED)
2022 * \warning Use of RC4 in DTLS/TLS has been prohibited by RFC 7465
2023 * for security reasons. Use at your own risk.
2025 * \note This function is deprecated and will likely be removed in
2026 * a future version of the library.
2027 * RC4 is disabled by default at compile time and needs to be
2028 * actively enabled for use with legacy systems.
2030 * \param conf SSL configuration
2031 * \param arc4 MBEDTLS_SSL_ARC4_ENABLED or MBEDTLS_SSL_ARC4_DISABLED
2033 void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config
*conf
, char arc4
);
2034 #endif /* MBEDTLS_ARC4_C */
2036 #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2038 * \brief Set the maximum fragment length to emit and/or negotiate
2039 * (Default: MBEDTLS_SSL_MAX_CONTENT_LEN, usually 2^14 bytes)
2040 * (Server: set maximum fragment length to emit,
2041 * usually negotiated by the client during handshake
2042 * (Client: set maximum fragment length to emit *and*
2043 * negotiate with the server during handshake)
2045 * \param conf SSL configuration
2046 * \param mfl_code Code for maximum fragment length (allowed values:
2047 * MBEDTLS_SSL_MAX_FRAG_LEN_512, MBEDTLS_SSL_MAX_FRAG_LEN_1024,
2048 * MBEDTLS_SSL_MAX_FRAG_LEN_2048, MBEDTLS_SSL_MAX_FRAG_LEN_4096)
2050 * \return 0 if successful or MBEDTLS_ERR_SSL_BAD_INPUT_DATA
2052 int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config
*conf
, unsigned char mfl_code
);
2053 #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2055 #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
2057 * \brief Activate negotiation of truncated HMAC
2058 * (Default: MBEDTLS_SSL_TRUNC_HMAC_DISABLED)
2060 * \param conf SSL configuration
2061 * \param truncate Enable or disable (MBEDTLS_SSL_TRUNC_HMAC_ENABLED or
2062 * MBEDTLS_SSL_TRUNC_HMAC_DISABLED)
2064 void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config
*conf
, int truncate
);
2065 #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
2067 #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
2069 * \brief Enable / Disable 1/n-1 record splitting
2070 * (Default: MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED)
2072 * \note Only affects SSLv3 and TLS 1.0, not higher versions.
2073 * Does not affect non-CBC ciphersuites in any version.
2075 * \param conf SSL configuration
2076 * \param split MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED or
2077 * MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED
2079 void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config
*conf
, char split
);
2080 #endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
2082 #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
2084 * \brief Enable / Disable session tickets (client only).
2085 * (Default: MBEDTLS_SSL_SESSION_TICKETS_ENABLED.)
2087 * \note On server, use \c mbedtls_ssl_conf_session_tickets_cb().
2089 * \param conf SSL configuration
2090 * \param use_tickets Enable or disable (MBEDTLS_SSL_SESSION_TICKETS_ENABLED or
2091 * MBEDTLS_SSL_SESSION_TICKETS_DISABLED)
2093 void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config
*conf
, int use_tickets
);
2094 #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
2096 #if defined(MBEDTLS_SSL_RENEGOTIATION)
2098 * \brief Enable / Disable renegotiation support for connection when
2100 * (Default: MBEDTLS_SSL_RENEGOTIATION_DISABLED)
2102 * \warning It is recommended to always disable renegotation unless you
2103 * know you need it and you know what you're doing. In the
2104 * past, there have been several issues associated with
2105 * renegotiation or a poor understanding of its properties.
2107 * \note Server-side, enabling renegotiation also makes the server
2108 * susceptible to a resource DoS by a malicious client.
2110 * \param conf SSL configuration
2111 * \param renegotiation Enable or disable (MBEDTLS_SSL_RENEGOTIATION_ENABLED or
2112 * MBEDTLS_SSL_RENEGOTIATION_DISABLED)
2114 void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config
*conf
, int renegotiation
);
2115 #endif /* MBEDTLS_SSL_RENEGOTIATION */
2118 * \brief Prevent or allow legacy renegotiation.
2119 * (Default: MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION)
2121 * MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION allows connections to
2122 * be established even if the peer does not support
2123 * secure renegotiation, but does not allow renegotiation
2124 * to take place if not secure.
2125 * (Interoperable and secure option)
2127 * MBEDTLS_SSL_LEGACY_ALLOW_RENEGOTIATION allows renegotiations
2128 * with non-upgraded peers. Allowing legacy renegotiation
2129 * makes the connection vulnerable to specific man in the
2130 * middle attacks. (See RFC 5746)
2131 * (Most interoperable and least secure option)
2133 * MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE breaks off connections
2134 * if peer does not support secure renegotiation. Results
2135 * in interoperability issues with non-upgraded peers
2136 * that do not support renegotiation altogether.
2137 * (Most secure option, interoperability issues)
2139 * \param conf SSL configuration
2140 * \param allow_legacy Prevent or allow (SSL_NO_LEGACY_RENEGOTIATION,
2141 * SSL_ALLOW_LEGACY_RENEGOTIATION or
2142 * MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE)
2144 void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config
*conf
, int allow_legacy
);
2146 #if defined(MBEDTLS_SSL_RENEGOTIATION)
2148 * \brief Enforce renegotiation requests.
2149 * (Default: enforced, max_records = 16)
2151 * When we request a renegotiation, the peer can comply or
2152 * ignore the request. This function allows us to decide
2153 * whether to enforce our renegotiation requests by closing
2154 * the connection if the peer doesn't comply.
2156 * However, records could already be in transit from the peer
2157 * when the request is emitted. In order to increase
2158 * reliability, we can accept a number of records before the
2159 * expected handshake records.
2161 * The optimal value is highly dependent on the specific usage
2164 * \note With DTLS and server-initiated renegotiation, the
2165 * HelloRequest is retransmited every time mbedtls_ssl_read() times
2166 * out or receives Application Data, until:
2167 * - max_records records have beens seen, if it is >= 0, or
2168 * - the number of retransmits that would happen during an
2169 * actual handshake has been reached.
2170 * Please remember the request might be lost a few times
2171 * if you consider setting max_records to a really low value.
2173 * \warning On client, the grace period can only happen during
2174 * mbedtls_ssl_read(), as opposed to mbedtls_ssl_write() and mbedtls_ssl_renegotiate()
2175 * which always behave as if max_record was 0. The reason is,
2176 * if we receive application data from the server, we need a
2177 * place to write it, which only happens during mbedtls_ssl_read().
2179 * \param conf SSL configuration
2180 * \param max_records Use MBEDTLS_SSL_RENEGOTIATION_NOT_ENFORCED if you don't want to
2181 * enforce renegotiation, or a non-negative value to enforce
2182 * it but allow for a grace period of max_records records.
2184 void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config
*conf
, int max_records
);
2187 * \brief Set record counter threshold for periodic renegotiation.
2188 * (Default: 2^64 - 256.)
2190 * Renegotiation is automatically triggered when a record
2191 * counter (outgoing or ingoing) crosses the defined
2192 * threshold. The default value is meant to prevent the
2193 * connection from being closed when the counter is about to
2194 * reached its maximal value (it is not allowed to wrap).
2196 * Lower values can be used to enforce policies such as "keys
2197 * must be refreshed every N packets with cipher X".
2199 * \param conf SSL configuration
2200 * \param period The threshold value: a big-endian 64-bit number.
2201 * Set to 2^64 - 1 to disable periodic renegotiation
2203 void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config
*conf
,
2204 const unsigned char period
[8] );
2205 #endif /* MBEDTLS_SSL_RENEGOTIATION */
2208 * \brief Return the number of data bytes available to read
2210 * \param ssl SSL context
2212 * \return how many bytes are available in the read buffer
2214 size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context
*ssl
);
2217 * \brief Return the result of the certificate verification
2219 * \param ssl SSL context
2221 * \return 0 if successful,
2222 * -1 if result is not available (eg because the handshake was
2223 * aborted too early), or
2224 * a combination of BADCERT_xxx and BADCRL_xxx flags, see
2227 uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context
*ssl
);
2230 * \brief Return the name of the current ciphersuite
2232 * \param ssl SSL context
2234 * \return a string containing the ciphersuite name
2236 const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context
*ssl
);
2239 * \brief Return the current SSL version (SSLv3/TLSv1/etc)
2241 * \param ssl SSL context
2243 * \return a string containing the SSL version
2245 const char *mbedtls_ssl_get_version( const mbedtls_ssl_context
*ssl
);
2248 * \brief Return the (maximum) number of bytes added by the record
2249 * layer: header + encryption/MAC overhead (inc. padding)
2251 * \param ssl SSL context
2253 * \return Current maximum record expansion in bytes, or
2254 * MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE if compression is
2255 * enabled, which makes expansion much less predictable
2257 int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context
*ssl
);
2259 #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2261 * \brief Return the maximum fragment length (payload, in bytes).
2262 * This is the value negotiated with peer if any,
2263 * or the locally configured value.
2265 * \note With DTLS, \c mbedtls_ssl_write() will return an error if
2266 * called with a larger length value.
2267 * With TLS, \c mbedtls_ssl_write() will fragment the input if
2268 * necessary and return the number of bytes written; it is up
2269 * to the caller to call \c mbedtls_ssl_write() again in
2270 * order to send the remaining bytes if any.
2272 * \param ssl SSL context
2274 * \return Current maximum fragment length.
2276 size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context
*ssl
);
2277 #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2279 #if defined(MBEDTLS_X509_CRT_PARSE_C)
2281 * \brief Return the peer certificate from the current connection
2283 * Note: Can be NULL in case no certificate was sent during
2284 * the handshake. Different calls for the same connection can
2285 * return the same or different pointers for the same
2286 * certificate and even a different certificate altogether.
2287 * The peer cert CAN change in a single connection if
2288 * renegotiation is performed.
2290 * \param ssl SSL context
2292 * \return the current peer certificate
2294 const mbedtls_x509_crt
*mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context
*ssl
);
2295 #endif /* MBEDTLS_X509_CRT_PARSE_C */
2297 #if defined(MBEDTLS_SSL_CLI_C)
2299 * \brief Save session in order to resume it later (client-side only)
2300 * Session data is copied to presented session structure.
2302 * \warning Currently, peer certificate is lost in the operation.
2304 * \param ssl SSL context
2305 * \param session session context
2307 * \return 0 if successful,
2308 * MBEDTLS_ERR_SSL_ALLOC_FAILED if memory allocation failed,
2309 * MBEDTLS_ERR_SSL_BAD_INPUT_DATA if used server-side or
2310 * arguments are otherwise invalid
2312 * \sa mbedtls_ssl_set_session()
2314 int mbedtls_ssl_get_session( const mbedtls_ssl_context
*ssl
, mbedtls_ssl_session
*session
);
2315 #endif /* MBEDTLS_SSL_CLI_C */
2318 * \brief Perform the SSL handshake
2320 * \param ssl SSL context
2322 * \return 0 if successful, or
2323 * MBEDTLS_ERR_SSL_WANT_READ or MBEDTLS_ERR_SSL_WANT_WRITE, or
2324 * MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED (see below), or
2325 * a specific SSL error code.
2327 * \note If this function returns something other than 0 or
2328 * MBEDTLS_ERR_SSL_WANT_READ/WRITE, then the ssl context
2329 * becomes unusable, and you should either free it or call
2330 * \c mbedtls_ssl_session_reset() on it before re-using it for
2331 * a new connection; the current connection must be closed.
2333 * \note If DTLS is in use, then you may choose to handle
2334 * MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED specially for logging
2335 * purposes, as it is an expected return value rather than an
2336 * actual error, but you still need to reset/free the context.
2338 int mbedtls_ssl_handshake( mbedtls_ssl_context
*ssl
);
2341 * \brief Perform a single step of the SSL handshake
2343 * \note The state of the context (ssl->state) will be at
2344 * the next state after execution of this function. Do not
2345 * call this function if state is MBEDTLS_SSL_HANDSHAKE_OVER.
2347 * \note If this function returns something other than 0 or
2348 * MBEDTLS_ERR_SSL_WANT_READ/WRITE, then the ssl context
2349 * becomes unusable, and you should either free it or call
2350 * \c mbedtls_ssl_session_reset() on it before re-using it for
2351 * a new connection; the current connection must be closed.
2353 * \param ssl SSL context
2355 * \return 0 if successful, or
2356 * MBEDTLS_ERR_SSL_WANT_READ or MBEDTLS_ERR_SSL_WANT_WRITE, or
2357 * a specific SSL error code.
2359 int mbedtls_ssl_handshake_step( mbedtls_ssl_context
*ssl
);
2361 #if defined(MBEDTLS_SSL_RENEGOTIATION)
2363 * \brief Initiate an SSL renegotiation on the running connection.
2364 * Client: perform the renegotiation right now.
2365 * Server: request renegotiation, which will be performed
2366 * during the next call to mbedtls_ssl_read() if honored by
2369 * \param ssl SSL context
2371 * \return 0 if successful, or any mbedtls_ssl_handshake() return
2374 * \note If this function returns something other than 0 or
2375 * MBEDTLS_ERR_SSL_WANT_READ/WRITE, then the ssl context
2376 * becomes unusable, and you should either free it or call
2377 * \c mbedtls_ssl_session_reset() on it before re-using it for
2378 * a new connection; the current connection must be closed.
2380 int mbedtls_ssl_renegotiate( mbedtls_ssl_context
*ssl
);
2381 #endif /* MBEDTLS_SSL_RENEGOTIATION */
2384 * \brief Read at most 'len' application data bytes
2386 * \param ssl SSL context
2387 * \param buf buffer that will hold the data
2388 * \param len maximum number of bytes to read
2390 * \return the number of bytes read, or
2392 * MBEDTLS_ERR_SSL_WANT_READ or MBEDTLS_ERR_SSL_WANT_WRITE, or
2393 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT (see below), or
2394 * another negative error code.
2396 * \note If this function returns something other than a positive
2397 * value or MBEDTLS_ERR_SSL_WANT_READ/WRITE or
2398 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT, then the ssl context
2399 * becomes unusable, and you should either free it or call
2400 * \c mbedtls_ssl_session_reset() on it before re-using it for
2401 * a new connection; the current connection must be closed.
2403 * \note When this function return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
2404 * (which can only happen server-side), it means that a client
2405 * is initiating a new connection using the same source port.
2406 * You can either treat that as a connection close and wait
2407 * for the client to resend a ClientHello, or directly
2408 * continue with \c mbedtls_ssl_handshake() with the same
2409 * context (as it has beeen reset internally). Either way, you
2410 * should make sure this is seen by the application as a new
2411 * connection: application state, if any, should be reset, and
2412 * most importantly the identity of the client must be checked
2413 * again. WARNING: not validating the identity of the client
2414 * again, or not transmitting the new identity to the
2415 * application layer, would allow authentication bypass!
2417 int mbedtls_ssl_read( mbedtls_ssl_context
*ssl
, unsigned char *buf
, size_t len
);
2420 * \brief Try to write exactly 'len' application data bytes
2422 * \warning This function will do partial writes in some cases. If the
2423 * return value is non-negative but less than length, the
2424 * function must be called again with updated arguments:
2425 * buf + ret, len - ret (if ret is the return value) until
2426 * it returns a value equal to the last 'len' argument.
2428 * \param ssl SSL context
2429 * \param buf buffer holding the data
2430 * \param len how many bytes must be written
2432 * \return the number of bytes actually written (may be less than len),
2433 * or MBEDTLS_ERR_SSL_WANT_WRITE of MBEDTLS_ERR_SSL_WANT_READ,
2434 * or another negative error code.
2436 * \note If this function returns something other than a positive
2437 * value or MBEDTLS_ERR_SSL_WANT_READ/WRITE, the ssl context
2438 * becomes unusable, and you should either free it or call
2439 * \c mbedtls_ssl_session_reset() on it before re-using it for
2440 * a new connection; the current connection must be closed.
2442 * \note When this function returns MBEDTLS_ERR_SSL_WANT_WRITE/READ,
2443 * it must be called later with the *same* arguments,
2444 * until it returns a positive value.
2446 * \note If the requested length is greater than the maximum
2447 * fragment length (either the built-in limit or the one set
2448 * or negotiated with the peer), then:
2449 * - with TLS, less bytes than requested are written.
2450 * - with DTLS, MBEDTLS_ERR_SSL_BAD_INPUT_DATA is returned.
2451 * \c mbedtls_ssl_get_max_frag_len() may be used to query the
2452 * active maximum fragment length.
2454 int mbedtls_ssl_write( mbedtls_ssl_context
*ssl
, const unsigned char *buf
, size_t len
);
2457 * \brief Send an alert message
2459 * \param ssl SSL context
2460 * \param level The alert level of the message
2461 * (MBEDTLS_SSL_ALERT_LEVEL_WARNING or MBEDTLS_SSL_ALERT_LEVEL_FATAL)
2462 * \param message The alert message (SSL_ALERT_MSG_*)
2464 * \return 0 if successful, or a specific SSL error code.
2466 * \note If this function returns something other than 0 or
2467 * MBEDTLS_ERR_SSL_WANT_READ/WRITE, then the ssl context
2468 * becomes unusable, and you should either free it or call
2469 * \c mbedtls_ssl_session_reset() on it before re-using it for
2470 * a new connection; the current connection must be closed.
2472 int mbedtls_ssl_send_alert_message( mbedtls_ssl_context
*ssl
,
2473 unsigned char level
,
2474 unsigned char message
);
2476 * \brief Notify the peer that the connection is being closed
2478 * \param ssl SSL context
2480 * \return 0 if successful, or a specific SSL error code.
2482 * \note If this function returns something other than 0 or
2483 * MBEDTLS_ERR_SSL_WANT_READ/WRITE, then the ssl context
2484 * becomes unusable, and you should either free it or call
2485 * \c mbedtls_ssl_session_reset() on it before re-using it for
2486 * a new connection; the current connection must be closed.
2488 int mbedtls_ssl_close_notify( mbedtls_ssl_context
*ssl
);
2491 * \brief Free referenced items in an SSL context and clear memory
2493 * \param ssl SSL context
2495 void mbedtls_ssl_free( mbedtls_ssl_context
*ssl
);
2498 * \brief Initialize an SSL configuration context
2499 * Just makes the context ready for
2500 * mbedtls_ssl_config_defaults() or mbedtls_ssl_config_free().
2502 * \note You need to call mbedtls_ssl_config_defaults() unless you
2503 * manually set all of the relevent fields yourself.
2505 * \param conf SSL configuration context
2507 void mbedtls_ssl_config_init( mbedtls_ssl_config
*conf
);
2510 * \brief Load reasonnable default SSL configuration values.
2511 * (You need to call mbedtls_ssl_config_init() first.)
2513 * \param conf SSL configuration context
2514 * \param endpoint MBEDTLS_SSL_IS_CLIENT or MBEDTLS_SSL_IS_SERVER
2515 * \param transport MBEDTLS_SSL_TRANSPORT_STREAM for TLS, or
2516 * MBEDTLS_SSL_TRANSPORT_DATAGRAM for DTLS
2517 * \param preset a MBEDTLS_SSL_PRESET_XXX value
2518 * (currently unused).
2520 * \note See \c mbedtls_ssl_conf_transport() for notes on DTLS.
2522 * \return 0 if successful, or
2523 * MBEDTLS_ERR_XXX_ALLOC_FAILED on memory allocation error.
2525 int mbedtls_ssl_config_defaults( mbedtls_ssl_config
*conf
,
2526 int endpoint
, int transport
, int preset
);
2529 * \brief Free an SSL configuration context
2531 * \param conf SSL configuration context
2533 void mbedtls_ssl_config_free( mbedtls_ssl_config
*conf
);
2536 * \brief Initialize SSL session structure
2538 * \param session SSL session
2540 void mbedtls_ssl_session_init( mbedtls_ssl_session
*session
);
2543 * \brief Free referenced items in an SSL session including the
2544 * peer certificate and clear memory
2546 * \param session SSL session
2548 void mbedtls_ssl_session_free( mbedtls_ssl_session
*session
);