4 * \brief SSL/TLS functions.
7 * Copyright The Mbed TLS Contributors
8 * SPDX-License-Identifier: Apache-2.0
10 * Licensed under the Apache License, Version 2.0 (the "License"); you may
11 * not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
25 #if !defined(MBEDTLS_CONFIG_FILE)
26 #include "mbedtls/config.h"
28 #include MBEDTLS_CONFIG_FILE
31 #include "mbedtls/bignum.h"
32 #include "mbedtls/ecp.h"
34 #include "mbedtls/ssl_ciphersuites.h"
36 #if defined(MBEDTLS_X509_CRT_PARSE_C)
37 #include "mbedtls/x509_crt.h"
38 #include "mbedtls/x509_crl.h"
41 #if defined(MBEDTLS_DHM_C)
42 #include "mbedtls/dhm.h"
45 /* Adding guard for MBEDTLS_ECDSA_C to ensure no compile errors due
46 * to guards also being in ssl_srv.c and ssl_cli.c. There is a gap
47 * in functionality that access to ecdh_ctx structure is needed for
48 * MBEDTLS_ECDSA_C which does not seem correct.
50 #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
51 #include "mbedtls/ecdh.h"
54 #if defined(MBEDTLS_ZLIB_SUPPORT)
56 #if defined(MBEDTLS_DEPRECATED_WARNING)
57 #warning "Record compression support via MBEDTLS_ZLIB_SUPPORT is deprecated and will be removed in the next major revision of the library"
60 #if defined(MBEDTLS_DEPRECATED_REMOVED)
61 #error "Record compression support via MBEDTLS_ZLIB_SUPPORT is deprecated and cannot be used if MBEDTLS_DEPRECATED_REMOVED is set"
67 #if defined(MBEDTLS_HAVE_TIME)
68 #include "mbedtls/platform_time.h"
71 #if defined(MBEDTLS_USE_PSA_CRYPTO)
72 #include "psa/crypto.h"
73 #endif /* MBEDTLS_USE_PSA_CRYPTO */
78 #define MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE -0x7080 /**< The requested feature is not available. */
79 #define MBEDTLS_ERR_SSL_BAD_INPUT_DATA -0x7100 /**< Bad input parameters to function. */
80 #define MBEDTLS_ERR_SSL_INVALID_MAC -0x7180 /**< Verification of the message MAC failed. */
81 #define MBEDTLS_ERR_SSL_INVALID_RECORD -0x7200 /**< An invalid SSL record was received. */
82 #define MBEDTLS_ERR_SSL_CONN_EOF -0x7280 /**< The connection indicated an EOF. */
83 #define MBEDTLS_ERR_SSL_UNKNOWN_CIPHER -0x7300 /**< An unknown cipher was received. */
84 #define MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN -0x7380 /**< The server has no ciphersuites in common with the client. */
85 #define MBEDTLS_ERR_SSL_NO_RNG -0x7400 /**< No RNG was provided to the SSL module. */
86 #define MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE -0x7480 /**< No client certification received from the client, but required by the authentication mode. */
87 #define MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE -0x7500 /**< Our own certificate(s) is/are too large to send in an SSL message. */
88 #define MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED -0x7580 /**< The own certificate is not set, but needed by the server. */
89 #define MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED -0x7600 /**< The own private key or pre-shared key is not set, but needed. */
90 #define MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED -0x7680 /**< No CA Chain is set, but required to operate. */
91 #define MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE -0x7700 /**< An unexpected message was received from our peer. */
92 #define MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE -0x7780 /**< A fatal alert message was received from our peer. */
93 #define MBEDTLS_ERR_SSL_PEER_VERIFY_FAILED -0x7800 /**< Verification of our peer failed. */
94 #define MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY -0x7880 /**< The peer notified us that the connection is going to be closed. */
95 #define MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO -0x7900 /**< Processing of the ClientHello handshake message failed. */
96 #define MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO -0x7980 /**< Processing of the ServerHello handshake message failed. */
97 #define MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE -0x7A00 /**< Processing of the Certificate handshake message failed. */
98 #define MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST -0x7A80 /**< Processing of the CertificateRequest handshake message failed. */
99 #define MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE -0x7B00 /**< Processing of the ServerKeyExchange handshake message failed. */
100 #define MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO_DONE -0x7B80 /**< Processing of the ServerHelloDone handshake message failed. */
101 #define MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE -0x7C00 /**< Processing of the ClientKeyExchange handshake message failed. */
102 #define MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP -0x7C80 /**< Processing of the ClientKeyExchange handshake message failed in DHM / ECDH Read Public. */
103 #define MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS -0x7D00 /**< Processing of the ClientKeyExchange handshake message failed in DHM / ECDH Calculate Secret. */
104 #define MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY -0x7D80 /**< Processing of the CertificateVerify handshake message failed. */
105 #define MBEDTLS_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC -0x7E00 /**< Processing of the ChangeCipherSpec handshake message failed. */
106 #define MBEDTLS_ERR_SSL_BAD_HS_FINISHED -0x7E80 /**< Processing of the Finished handshake message failed. */
107 #define MBEDTLS_ERR_SSL_ALLOC_FAILED -0x7F00 /**< Memory allocation failed */
108 #define MBEDTLS_ERR_SSL_HW_ACCEL_FAILED -0x7F80 /**< Hardware acceleration function returned with error */
109 #define MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH -0x6F80 /**< Hardware acceleration function skipped / left alone data */
110 #define MBEDTLS_ERR_SSL_COMPRESSION_FAILED -0x6F00 /**< Processing of the compression / decompression failed */
111 #define MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION -0x6E80 /**< Handshake protocol not within min/max boundaries */
112 #define MBEDTLS_ERR_SSL_BAD_HS_NEW_SESSION_TICKET -0x6E00 /**< Processing of the NewSessionTicket handshake message failed. */
113 #define MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED -0x6D80 /**< Session ticket has expired. */
114 #define MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH -0x6D00 /**< Public key type mismatch (eg, asked for RSA key exchange and presented EC key) */
115 #define MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY -0x6C80 /**< Unknown identity received (eg, PSK identity) */
116 #define MBEDTLS_ERR_SSL_INTERNAL_ERROR -0x6C00 /**< Internal error (eg, unexpected failure in lower-level module) */
117 #define MBEDTLS_ERR_SSL_COUNTER_WRAPPING -0x6B80 /**< A counter would wrap (eg, too many messages exchanged). */
118 #define MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO -0x6B00 /**< Unexpected message at ServerHello in renegotiation. */
119 #define MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED -0x6A80 /**< DTLS client must retry for hello verification */
120 #define MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL -0x6A00 /**< A buffer is too small to receive or write a message */
121 #define MBEDTLS_ERR_SSL_NO_USABLE_CIPHERSUITE -0x6980 /**< None of the common ciphersuites is usable (eg, no suitable certificate, see debug messages). */
122 #define MBEDTLS_ERR_SSL_WANT_READ -0x6900 /**< No data of requested type currently available on underlying transport. */
123 #define MBEDTLS_ERR_SSL_WANT_WRITE -0x6880 /**< Connection requires a write call. */
124 #define MBEDTLS_ERR_SSL_TIMEOUT -0x6800 /**< The operation timed out. */
125 #define MBEDTLS_ERR_SSL_CLIENT_RECONNECT -0x6780 /**< The client initiated a reconnect from the same port. */
126 #define MBEDTLS_ERR_SSL_UNEXPECTED_RECORD -0x6700 /**< Record header looks valid but is not expected. */
127 #define MBEDTLS_ERR_SSL_NON_FATAL -0x6680 /**< The alert message received indicates a non-fatal error. */
128 #define MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH -0x6600 /**< Couldn't set the hash for verifying CertificateVerify */
129 #define MBEDTLS_ERR_SSL_CONTINUE_PROCESSING -0x6580 /**< Internal-only message signaling that further message-processing should be done */
130 #define MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS -0x6500 /**< The asynchronous operation is not completed yet. */
131 #define MBEDTLS_ERR_SSL_EARLY_MESSAGE -0x6480 /**< Internal-only message signaling that a message arrived early. */
132 #define MBEDTLS_ERR_SSL_UNEXPECTED_CID -0x6000 /**< An encrypted DTLS-frame with an unexpected CID was received. */
133 #define MBEDTLS_ERR_SSL_VERSION_MISMATCH -0x5F00 /**< An operation failed due to an unexpected version or configuration. */
134 #define MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS -0x7000 /**< A cryptographic operation is in progress. Try again later. */
135 #define MBEDTLS_ERR_SSL_BAD_CONFIG -0x5E80 /**< Invalid value in SSL config */
140 #define MBEDTLS_SSL_MAJOR_VERSION_3 3
141 #define MBEDTLS_SSL_MINOR_VERSION_0 0 /*!< SSL v3.0 */
142 #define MBEDTLS_SSL_MINOR_VERSION_1 1 /*!< TLS v1.0 */
143 #define MBEDTLS_SSL_MINOR_VERSION_2 2 /*!< TLS v1.1 */
144 #define MBEDTLS_SSL_MINOR_VERSION_3 3 /*!< TLS v1.2 */
145 #define MBEDTLS_SSL_MINOR_VERSION_4 4 /*!< TLS v1.3 (experimental) */
147 #define MBEDTLS_SSL_TRANSPORT_STREAM 0 /*!< TLS */
148 #define MBEDTLS_SSL_TRANSPORT_DATAGRAM 1 /*!< DTLS */
150 #define MBEDTLS_SSL_MAX_HOST_NAME_LEN 255 /*!< Maximum host name defined in RFC 1035 */
151 #define MBEDTLS_SSL_MAX_ALPN_NAME_LEN 255 /*!< Maximum size in bytes of a protocol name in alpn ext., RFC 7301 */
153 #define MBEDTLS_SSL_MAX_ALPN_LIST_LEN 65535 /*!< Maximum size in bytes of list in alpn ext., RFC 7301 */
155 /* RFC 6066 section 4, see also mfl_code_to_length in ssl_tls.c
156 * NONE must be zero so that memset()ing structure to zero works */
157 #define MBEDTLS_SSL_MAX_FRAG_LEN_NONE 0 /*!< don't use this extension */
158 #define MBEDTLS_SSL_MAX_FRAG_LEN_512 1 /*!< MaxFragmentLength 2^9 */
159 #define MBEDTLS_SSL_MAX_FRAG_LEN_1024 2 /*!< MaxFragmentLength 2^10 */
160 #define MBEDTLS_SSL_MAX_FRAG_LEN_2048 3 /*!< MaxFragmentLength 2^11 */
161 #define MBEDTLS_SSL_MAX_FRAG_LEN_4096 4 /*!< MaxFragmentLength 2^12 */
162 #define MBEDTLS_SSL_MAX_FRAG_LEN_INVALID 5 /*!< first invalid value */
164 #define MBEDTLS_SSL_IS_CLIENT 0
165 #define MBEDTLS_SSL_IS_SERVER 1
167 #define MBEDTLS_SSL_IS_NOT_FALLBACK 0
168 #define MBEDTLS_SSL_IS_FALLBACK 1
170 #define MBEDTLS_SSL_EXTENDED_MS_DISABLED 0
171 #define MBEDTLS_SSL_EXTENDED_MS_ENABLED 1
173 #define MBEDTLS_SSL_CID_DISABLED 0
174 #define MBEDTLS_SSL_CID_ENABLED 1
176 #define MBEDTLS_SSL_ETM_DISABLED 0
177 #define MBEDTLS_SSL_ETM_ENABLED 1
179 #define MBEDTLS_SSL_COMPRESS_NULL 0
180 #define MBEDTLS_SSL_COMPRESS_DEFLATE 1
182 #define MBEDTLS_SSL_VERIFY_NONE 0
183 #define MBEDTLS_SSL_VERIFY_OPTIONAL 1
184 #define MBEDTLS_SSL_VERIFY_REQUIRED 2
185 #define MBEDTLS_SSL_VERIFY_UNSET 3 /* Used only for sni_authmode */
187 #define MBEDTLS_SSL_LEGACY_RENEGOTIATION 0
188 #define MBEDTLS_SSL_SECURE_RENEGOTIATION 1
190 #define MBEDTLS_SSL_RENEGOTIATION_DISABLED 0
191 #define MBEDTLS_SSL_RENEGOTIATION_ENABLED 1
193 #define MBEDTLS_SSL_ANTI_REPLAY_DISABLED 0
194 #define MBEDTLS_SSL_ANTI_REPLAY_ENABLED 1
196 #define MBEDTLS_SSL_RENEGOTIATION_NOT_ENFORCED -1
197 #define MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT 16
199 #define MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION 0
200 #define MBEDTLS_SSL_LEGACY_ALLOW_RENEGOTIATION 1
201 #define MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE 2
203 #define MBEDTLS_SSL_TRUNC_HMAC_DISABLED 0
204 #define MBEDTLS_SSL_TRUNC_HMAC_ENABLED 1
205 #define MBEDTLS_SSL_TRUNCATED_HMAC_LEN 10 /* 80 bits, rfc 6066 section 7 */
207 #define MBEDTLS_SSL_SESSION_TICKETS_DISABLED 0
208 #define MBEDTLS_SSL_SESSION_TICKETS_ENABLED 1
210 #define MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED 0
211 #define MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED 1
213 #define MBEDTLS_SSL_ARC4_ENABLED 0
214 #define MBEDTLS_SSL_ARC4_DISABLED 1
216 #define MBEDTLS_SSL_PRESET_DEFAULT 0
217 #define MBEDTLS_SSL_PRESET_SUITEB 2
219 #define MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED 1
220 #define MBEDTLS_SSL_CERT_REQ_CA_LIST_DISABLED 0
222 #define MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED 0
223 #define MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED 1
226 * Default range for DTLS retransmission timer value, in milliseconds.
227 * RFC 6347 4.2.4.1 says from 1 second to 60 seconds.
229 #define MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN 1000
230 #define MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX 60000
233 * \name SECTION: Module settings
235 * The configuration options you can set for this module are in this section.
236 * Either change them in config.h or define them on the compiler command line.
240 #if !defined(MBEDTLS_SSL_DEFAULT_TICKET_LIFETIME)
241 #define MBEDTLS_SSL_DEFAULT_TICKET_LIFETIME 86400 /**< Lifetime of session tickets (if enabled) */
245 * Maximum fragment length in bytes,
246 * determines the size of each of the two internal I/O buffers.
248 * Note: the RFC defines the default size of SSL / TLS messages. If you
249 * change the value here, other clients / servers may not be able to
250 * communicate with you anymore. Only change this value if you control
251 * both sides of the connection and have it reduced at both sides, or
252 * if you're using the Max Fragment Length extension and you know all your
253 * peers are using it too!
255 #if !defined(MBEDTLS_SSL_MAX_CONTENT_LEN)
256 #define MBEDTLS_SSL_MAX_CONTENT_LEN 16384 /**< Size of the input / output buffer */
259 #if !defined(MBEDTLS_SSL_IN_CONTENT_LEN)
260 #define MBEDTLS_SSL_IN_CONTENT_LEN MBEDTLS_SSL_MAX_CONTENT_LEN
263 #if !defined(MBEDTLS_SSL_OUT_CONTENT_LEN)
264 #define MBEDTLS_SSL_OUT_CONTENT_LEN MBEDTLS_SSL_MAX_CONTENT_LEN
268 * Maximum number of heap-allocated bytes for the purpose of
269 * DTLS handshake message reassembly and future message buffering.
271 #if !defined(MBEDTLS_SSL_DTLS_MAX_BUFFERING)
272 #define MBEDTLS_SSL_DTLS_MAX_BUFFERING 32768
276 * Maximum length of CIDs for incoming and outgoing messages.
278 #if !defined(MBEDTLS_SSL_CID_IN_LEN_MAX)
279 #define MBEDTLS_SSL_CID_IN_LEN_MAX 32
282 #if !defined(MBEDTLS_SSL_CID_OUT_LEN_MAX)
283 #define MBEDTLS_SSL_CID_OUT_LEN_MAX 32
286 #if !defined(MBEDTLS_SSL_CID_PADDING_GRANULARITY)
287 #define MBEDTLS_SSL_CID_PADDING_GRANULARITY 16
290 #if !defined(MBEDTLS_SSL_TLS1_3_PADDING_GRANULARITY)
291 #define MBEDTLS_SSL_TLS1_3_PADDING_GRANULARITY 1
294 /* \} name SECTION: Module settings */
297 * Length of the verify data for secure renegotiation
299 #if defined(MBEDTLS_SSL_PROTO_SSL3)
300 #define MBEDTLS_SSL_VERIFY_DATA_MAX_LEN 36
302 #define MBEDTLS_SSL_VERIFY_DATA_MAX_LEN 12
306 * Signaling ciphersuite values (SCSV)
308 #define MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO 0xFF /**< renegotiation info ext */
309 #define MBEDTLS_SSL_FALLBACK_SCSV_VALUE 0x5600 /**< RFC 7507 section 2 */
312 * Supported Signature and Hash algorithms (For TLS 1.2)
313 * RFC 5246 section 7.4.1.4.1
315 #define MBEDTLS_SSL_HASH_NONE 0
316 #define MBEDTLS_SSL_HASH_MD5 1
317 #define MBEDTLS_SSL_HASH_SHA1 2
318 #define MBEDTLS_SSL_HASH_SHA224 3
319 #define MBEDTLS_SSL_HASH_SHA256 4
320 #define MBEDTLS_SSL_HASH_SHA384 5
321 #define MBEDTLS_SSL_HASH_SHA512 6
323 #define MBEDTLS_SSL_SIG_ANON 0
324 #define MBEDTLS_SSL_SIG_RSA 1
325 #define MBEDTLS_SSL_SIG_ECDSA 3
328 * Client Certificate Types
329 * RFC 5246 section 7.4.4 plus RFC 4492 section 5.5
331 #define MBEDTLS_SSL_CERT_TYPE_RSA_SIGN 1
332 #define MBEDTLS_SSL_CERT_TYPE_ECDSA_SIGN 64
335 * Message, alert and handshake types
337 #define MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC 20
338 #define MBEDTLS_SSL_MSG_ALERT 21
339 #define MBEDTLS_SSL_MSG_HANDSHAKE 22
340 #define MBEDTLS_SSL_MSG_APPLICATION_DATA 23
341 #define MBEDTLS_SSL_MSG_CID 25
343 #define MBEDTLS_SSL_ALERT_LEVEL_WARNING 1
344 #define MBEDTLS_SSL_ALERT_LEVEL_FATAL 2
346 #define MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY 0 /* 0x00 */
347 #define MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE 10 /* 0x0A */
348 #define MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC 20 /* 0x14 */
349 #define MBEDTLS_SSL_ALERT_MSG_DECRYPTION_FAILED 21 /* 0x15 */
350 #define MBEDTLS_SSL_ALERT_MSG_RECORD_OVERFLOW 22 /* 0x16 */
351 #define MBEDTLS_SSL_ALERT_MSG_DECOMPRESSION_FAILURE 30 /* 0x1E */
352 #define MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE 40 /* 0x28 */
353 #define MBEDTLS_SSL_ALERT_MSG_NO_CERT 41 /* 0x29 */
354 #define MBEDTLS_SSL_ALERT_MSG_BAD_CERT 42 /* 0x2A */
355 #define MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT 43 /* 0x2B */
356 #define MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED 44 /* 0x2C */
357 #define MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED 45 /* 0x2D */
358 #define MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN 46 /* 0x2E */
359 #define MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER 47 /* 0x2F */
360 #define MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA 48 /* 0x30 */
361 #define MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED 49 /* 0x31 */
362 #define MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR 50 /* 0x32 */
363 #define MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR 51 /* 0x33 */
364 #define MBEDTLS_SSL_ALERT_MSG_EXPORT_RESTRICTION 60 /* 0x3C */
365 #define MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION 70 /* 0x46 */
366 #define MBEDTLS_SSL_ALERT_MSG_INSUFFICIENT_SECURITY 71 /* 0x47 */
367 #define MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR 80 /* 0x50 */
368 #define MBEDTLS_SSL_ALERT_MSG_INAPROPRIATE_FALLBACK 86 /* 0x56 */
369 #define MBEDTLS_SSL_ALERT_MSG_USER_CANCELED 90 /* 0x5A */
370 #define MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION 100 /* 0x64 */
371 #define MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT 110 /* 0x6E */
372 #define MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME 112 /* 0x70 */
373 #define MBEDTLS_SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY 115 /* 0x73 */
374 #define MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL 120 /* 0x78 */
376 #define MBEDTLS_SSL_HS_HELLO_REQUEST 0
377 #define MBEDTLS_SSL_HS_CLIENT_HELLO 1
378 #define MBEDTLS_SSL_HS_SERVER_HELLO 2
379 #define MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST 3
380 #define MBEDTLS_SSL_HS_NEW_SESSION_TICKET 4
381 #define MBEDTLS_SSL_HS_CERTIFICATE 11
382 #define MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE 12
383 #define MBEDTLS_SSL_HS_CERTIFICATE_REQUEST 13
384 #define MBEDTLS_SSL_HS_SERVER_HELLO_DONE 14
385 #define MBEDTLS_SSL_HS_CERTIFICATE_VERIFY 15
386 #define MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE 16
387 #define MBEDTLS_SSL_HS_FINISHED 20
392 #define MBEDTLS_TLS_EXT_SERVERNAME 0
393 #define MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME 0
395 #define MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH 1
397 #define MBEDTLS_TLS_EXT_TRUNCATED_HMAC 4
399 #define MBEDTLS_TLS_EXT_SUPPORTED_ELLIPTIC_CURVES 10
400 #define MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS 11
402 #define MBEDTLS_TLS_EXT_SIG_ALG 13
404 #define MBEDTLS_TLS_EXT_USE_SRTP 14
406 #define MBEDTLS_TLS_EXT_ALPN 16
408 #define MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC 22 /* 0x16 */
409 #define MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET 0x0017 /* 23 */
411 #define MBEDTLS_TLS_EXT_SESSION_TICKET 35
413 /* The value of the CID extension is still TBD as of
414 * draft-ietf-tls-dtls-connection-id-05
415 * (https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05) */
416 #define MBEDTLS_TLS_EXT_CID 254 /* TBD */
418 #define MBEDTLS_TLS_EXT_ECJPAKE_KKPP 256 /* experimental */
420 #define MBEDTLS_TLS_EXT_RENEGOTIATION_INFO 0xFF01
425 #if !defined(MBEDTLS_PSK_MAX_LEN)
426 #define MBEDTLS_PSK_MAX_LEN 32 /* 256 bits */
429 /* Dummy type used only for its size */
430 union mbedtls_ssl_premaster_secret
{
431 #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
432 unsigned char _pms_rsa
[48]; /* RFC 5246 8.1.1 */
434 #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
435 unsigned char _pms_dhm
[MBEDTLS_MPI_MAX_SIZE
]; /* RFC 5246 8.1.2 */
437 #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
438 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
439 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
440 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
441 unsigned char _pms_ecdh
[MBEDTLS_ECP_MAX_BYTES
]; /* RFC 4492 5.10 */
443 #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
444 unsigned char _pms_psk
[4 + 2 * MBEDTLS_PSK_MAX_LEN
]; /* RFC 4279 2 */
446 #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
447 unsigned char _pms_dhe_psk
[4 + MBEDTLS_MPI_MAX_SIZE
448 + MBEDTLS_PSK_MAX_LEN
]; /* RFC 4279 3 */
450 #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
451 unsigned char _pms_rsa_psk
[52 + MBEDTLS_PSK_MAX_LEN
]; /* RFC 4279 4 */
453 #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
454 unsigned char _pms_ecdhe_psk
[4 + MBEDTLS_ECP_MAX_BYTES
455 + MBEDTLS_PSK_MAX_LEN
]; /* RFC 5489 2 */
457 #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
458 unsigned char _pms_ecjpake
[32]; /* Thread spec: SHA-256 output */
462 #define MBEDTLS_PREMASTER_SIZE sizeof( union mbedtls_ssl_premaster_secret )
472 MBEDTLS_SSL_HELLO_REQUEST
,
473 MBEDTLS_SSL_CLIENT_HELLO
,
474 MBEDTLS_SSL_SERVER_HELLO
,
475 MBEDTLS_SSL_SERVER_CERTIFICATE
,
476 MBEDTLS_SSL_SERVER_KEY_EXCHANGE
,
477 MBEDTLS_SSL_CERTIFICATE_REQUEST
,
478 MBEDTLS_SSL_SERVER_HELLO_DONE
,
479 MBEDTLS_SSL_CLIENT_CERTIFICATE
,
480 MBEDTLS_SSL_CLIENT_KEY_EXCHANGE
,
481 MBEDTLS_SSL_CERTIFICATE_VERIFY
,
482 MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC
,
483 MBEDTLS_SSL_CLIENT_FINISHED
,
484 MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC
,
485 MBEDTLS_SSL_SERVER_FINISHED
,
486 MBEDTLS_SSL_FLUSH_BUFFERS
,
487 MBEDTLS_SSL_HANDSHAKE_WRAPUP
,
488 MBEDTLS_SSL_HANDSHAKE_OVER
,
489 MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET
,
490 MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT
,
495 * The tls_prf function types.
498 MBEDTLS_SSL_TLS_PRF_NONE
,
499 MBEDTLS_SSL_TLS_PRF_SSL3
,
500 MBEDTLS_SSL_TLS_PRF_TLS1
,
501 MBEDTLS_SSL_TLS_PRF_SHA384
,
502 MBEDTLS_SSL_TLS_PRF_SHA256
504 mbedtls_tls_prf_types
;
506 * \brief Callback type: send data on the network.
508 * \note That callback may be either blocking or non-blocking.
510 * \param ctx Context for the send callback (typically a file descriptor)
511 * \param buf Buffer holding the data to send
512 * \param len Length of the data to send
514 * \return The callback must return the number of bytes sent if any,
515 * or a non-zero error code.
516 * If performing non-blocking I/O, \c MBEDTLS_ERR_SSL_WANT_WRITE
517 * must be returned when the operation would block.
519 * \note The callback is allowed to send fewer bytes than requested.
520 * It must always return the number of bytes actually sent.
522 typedef int mbedtls_ssl_send_t(void *ctx
,
523 const unsigned char *buf
,
527 * \brief Callback type: receive data from the network.
529 * \note That callback may be either blocking or non-blocking.
531 * \param ctx Context for the receive callback (typically a file
533 * \param buf Buffer to write the received data to
534 * \param len Length of the receive buffer
536 * \return The callback must return the number of bytes received,
537 * or a non-zero error code.
538 * If performing non-blocking I/O, \c MBEDTLS_ERR_SSL_WANT_READ
539 * must be returned when the operation would block.
541 * \note The callback may receive fewer bytes than the length of the
542 * buffer. It must always return the number of bytes actually
543 * received and written to the buffer.
545 typedef int mbedtls_ssl_recv_t(void *ctx
,
550 * \brief Callback type: receive data from the network, with timeout
552 * \note That callback must block until data is received, or the
553 * timeout delay expires, or the operation is interrupted by a
556 * \param ctx Context for the receive callback (typically a file descriptor)
557 * \param buf Buffer to write the received data to
558 * \param len Length of the receive buffer
559 * \param timeout Maximum nomber of millisecondes to wait for data
560 * 0 means no timeout (potentially waiting forever)
562 * \return The callback must return the number of bytes received,
563 * or a non-zero error code:
564 * \c MBEDTLS_ERR_SSL_TIMEOUT if the operation timed out,
565 * \c MBEDTLS_ERR_SSL_WANT_READ if interrupted by a signal.
567 * \note The callback may receive fewer bytes than the length of the
568 * buffer. It must always return the number of bytes actually
569 * received and written to the buffer.
571 typedef int mbedtls_ssl_recv_timeout_t(void *ctx
,
576 * \brief Callback type: set a pair of timers/delays to watch
578 * \param ctx Context pointer
579 * \param int_ms Intermediate delay in milliseconds
580 * \param fin_ms Final delay in milliseconds
581 * 0 cancels the current timer.
583 * \note This callback must at least store the necessary information
584 * for the associated \c mbedtls_ssl_get_timer_t callback to
585 * return correct information.
587 * \note If using a event-driven style of programming, an event must
588 * be generated when the final delay is passed. The event must
589 * cause a call to \c mbedtls_ssl_handshake() with the proper
590 * SSL context to be scheduled. Care must be taken to ensure
591 * that at most one such call happens at a time.
593 * \note Only one timer at a time must be running. Calling this
594 * function while a timer is running must cancel it. Cancelled
595 * timers must not generate any event.
597 typedef void mbedtls_ssl_set_timer_t(void *ctx
,
602 * \brief Callback type: get status of timers/delays
604 * \param ctx Context pointer
606 * \return This callback must return:
607 * -1 if cancelled (fin_ms == 0),
608 * 0 if none of the delays have passed,
609 * 1 if only the intermediate delay has passed,
610 * 2 if the final delay has passed.
612 typedef int mbedtls_ssl_get_timer_t(void *ctx
);
615 typedef struct mbedtls_ssl_session mbedtls_ssl_session
;
616 typedef struct mbedtls_ssl_context mbedtls_ssl_context
;
617 typedef struct mbedtls_ssl_config mbedtls_ssl_config
;
619 /* Defined in ssl_internal.h */
620 typedef struct mbedtls_ssl_transform mbedtls_ssl_transform
;
621 typedef struct mbedtls_ssl_handshake_params mbedtls_ssl_handshake_params
;
622 typedef struct mbedtls_ssl_sig_hash_set_t mbedtls_ssl_sig_hash_set_t
;
623 #if defined(MBEDTLS_X509_CRT_PARSE_C)
624 typedef struct mbedtls_ssl_key_cert mbedtls_ssl_key_cert
;
626 #if defined(MBEDTLS_SSL_PROTO_DTLS)
627 typedef struct mbedtls_ssl_flight_item mbedtls_ssl_flight_item
;
630 #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
631 #if defined(MBEDTLS_X509_CRT_PARSE_C)
633 * \brief Callback type: start external signature operation.
635 * This callback is called during an SSL handshake to start
636 * a signature decryption operation using an
637 * external processor. The parameter \p cert contains
638 * the public key; it is up to the callback function to
639 * determine how to access the associated private key.
641 * This function typically sends or enqueues a request, and
642 * does not wait for the operation to complete. This allows
643 * the handshake step to be non-blocking.
645 * The parameters \p ssl and \p cert are guaranteed to remain
646 * valid throughout the handshake. On the other hand, this
647 * function must save the contents of \p hash if the value
648 * is needed for later processing, because the \p hash buffer
649 * is no longer valid after this function returns.
651 * This function may call mbedtls_ssl_set_async_operation_data()
652 * to store an operation context for later retrieval
653 * by the resume or cancel callback.
655 * \note For RSA signatures, this function must produce output
656 * that is consistent with PKCS#1 v1.5 in the same way as
657 * mbedtls_rsa_pkcs1_sign(). Before the private key operation,
658 * apply the padding steps described in RFC 8017, section 9.2
659 * "EMSA-PKCS1-v1_5" as follows.
660 * - If \p md_alg is #MBEDTLS_MD_NONE, apply the PKCS#1 v1.5
661 * encoding, treating \p hash as the DigestInfo to be
662 * padded. In other words, apply EMSA-PKCS1-v1_5 starting
663 * from step 3, with `T = hash` and `tLen = hash_len`.
664 * - If `md_alg != MBEDTLS_MD_NONE`, apply the PKCS#1 v1.5
665 * encoding, treating \p hash as the hash to be encoded and
666 * padded. In other words, apply EMSA-PKCS1-v1_5 starting
667 * from step 2, with `digestAlgorithm` obtained by calling
668 * mbedtls_oid_get_oid_by_md() on \p md_alg.
670 * \note For ECDSA signatures, the output format is the DER encoding
671 * `Ecdsa-Sig-Value` defined in
672 * [RFC 4492 section 5.4](https://tools.ietf.org/html/rfc4492#section-5.4).
674 * \param ssl The SSL connection instance. It should not be
675 * modified other than via
676 * mbedtls_ssl_set_async_operation_data().
677 * \param cert Certificate containing the public key.
678 * In simple cases, this is one of the pointers passed to
679 * mbedtls_ssl_conf_own_cert() when configuring the SSL
680 * connection. However, if other callbacks are used, this
681 * property may not hold. For example, if an SNI callback
682 * is registered with mbedtls_ssl_conf_sni(), then
683 * this callback determines what certificate is used.
684 * \param md_alg Hash algorithm.
685 * \param hash Buffer containing the hash. This buffer is
686 * no longer valid when the function returns.
687 * \param hash_len Size of the \c hash buffer in bytes.
689 * \return 0 if the operation was started successfully and the SSL
690 * stack should call the resume callback immediately.
691 * \return #MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS if the operation
692 * was started successfully and the SSL stack should return
693 * immediately without calling the resume callback yet.
694 * \return #MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH if the external
695 * processor does not support this key. The SSL stack will
696 * use the private key object instead.
697 * \return Any other error indicates a fatal failure and is
698 * propagated up the call chain. The callback should
699 * use \c MBEDTLS_ERR_PK_xxx error codes, and <b>must not</b>
700 * use \c MBEDTLS_ERR_SSL_xxx error codes except as
701 * directed in the documentation of this callback.
703 typedef int mbedtls_ssl_async_sign_t(mbedtls_ssl_context
*ssl
,
704 mbedtls_x509_crt
*cert
,
705 mbedtls_md_type_t md_alg
,
706 const unsigned char *hash
,
710 * \brief Callback type: start external decryption operation.
712 * This callback is called during an SSL handshake to start
713 * an RSA decryption operation using an
714 * external processor. The parameter \p cert contains
715 * the public key; it is up to the callback function to
716 * determine how to access the associated private key.
718 * This function typically sends or enqueues a request, and
719 * does not wait for the operation to complete. This allows
720 * the handshake step to be non-blocking.
722 * The parameters \p ssl and \p cert are guaranteed to remain
723 * valid throughout the handshake. On the other hand, this
724 * function must save the contents of \p input if the value
725 * is needed for later processing, because the \p input buffer
726 * is no longer valid after this function returns.
728 * This function may call mbedtls_ssl_set_async_operation_data()
729 * to store an operation context for later retrieval
730 * by the resume or cancel callback.
732 * \warning RSA decryption as used in TLS is subject to a potential
733 * timing side channel attack first discovered by Bleichenbacher
734 * in 1998. This attack can be remotely exploitable
735 * in practice. To avoid this attack, you must ensure that
736 * if the callback performs an RSA decryption, the time it
737 * takes to execute and return the result does not depend
738 * on whether the RSA decryption succeeded or reported
741 * \param ssl The SSL connection instance. It should not be
742 * modified other than via
743 * mbedtls_ssl_set_async_operation_data().
744 * \param cert Certificate containing the public key.
745 * In simple cases, this is one of the pointers passed to
746 * mbedtls_ssl_conf_own_cert() when configuring the SSL
747 * connection. However, if other callbacks are used, this
748 * property may not hold. For example, if an SNI callback
749 * is registered with mbedtls_ssl_conf_sni(), then
750 * this callback determines what certificate is used.
751 * \param input Buffer containing the input ciphertext. This buffer
752 * is no longer valid when the function returns.
753 * \param input_len Size of the \p input buffer in bytes.
755 * \return 0 if the operation was started successfully and the SSL
756 * stack should call the resume callback immediately.
757 * \return #MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS if the operation
758 * was started successfully and the SSL stack should return
759 * immediately without calling the resume callback yet.
760 * \return #MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH if the external
761 * processor does not support this key. The SSL stack will
762 * use the private key object instead.
763 * \return Any other error indicates a fatal failure and is
764 * propagated up the call chain. The callback should
765 * use \c MBEDTLS_ERR_PK_xxx error codes, and <b>must not</b>
766 * use \c MBEDTLS_ERR_SSL_xxx error codes except as
767 * directed in the documentation of this callback.
769 typedef int mbedtls_ssl_async_decrypt_t(mbedtls_ssl_context
*ssl
,
770 mbedtls_x509_crt
*cert
,
771 const unsigned char *input
,
773 #endif /* MBEDTLS_X509_CRT_PARSE_C */
776 * \brief Callback type: resume external operation.
778 * This callback is called during an SSL handshake to resume
779 * an external operation started by the
780 * ::mbedtls_ssl_async_sign_t or
781 * ::mbedtls_ssl_async_decrypt_t callback.
783 * This function typically checks the status of a pending
784 * request or causes the request queue to make progress, and
785 * does not wait for the operation to complete. This allows
786 * the handshake step to be non-blocking.
788 * This function may call mbedtls_ssl_get_async_operation_data()
789 * to retrieve an operation context set by the start callback.
790 * It may call mbedtls_ssl_set_async_operation_data() to modify
793 * Note that when this function returns a status other than
794 * #MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS, it must free any
795 * resources associated with the operation.
797 * \param ssl The SSL connection instance. It should not be
798 * modified other than via
799 * mbedtls_ssl_set_async_operation_data().
800 * \param output Buffer containing the output (signature or decrypted
802 * \param output_len On success, number of bytes written to \p output.
803 * \param output_size Size of the \p output buffer in bytes.
805 * \return 0 if output of the operation is available in the
807 * \return #MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS if the operation
808 * is still in progress. Subsequent requests for progress
809 * on the SSL connection will call the resume callback
811 * \return Any other error means that the operation is aborted.
812 * The SSL handshake is aborted. The callback should
813 * use \c MBEDTLS_ERR_PK_xxx error codes, and <b>must not</b>
814 * use \c MBEDTLS_ERR_SSL_xxx error codes except as
815 * directed in the documentation of this callback.
817 typedef int mbedtls_ssl_async_resume_t(mbedtls_ssl_context
*ssl
,
818 unsigned char *output
,
823 * \brief Callback type: cancel external operation.
825 * This callback is called if an SSL connection is closed
826 * while an asynchronous operation is in progress. Note that
827 * this callback is not called if the
828 * ::mbedtls_ssl_async_resume_t callback has run and has
829 * returned a value other than
830 * #MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS, since in that case
831 * the asynchronous operation has already completed.
833 * This function may call mbedtls_ssl_get_async_operation_data()
834 * to retrieve an operation context set by the start callback.
836 * \param ssl The SSL connection instance. It should not be
839 typedef void mbedtls_ssl_async_cancel_t(mbedtls_ssl_context
*ssl
);
840 #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
842 #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) && \
843 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
844 #define MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN 48
845 #if defined(MBEDTLS_SHA256_C)
846 #define MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE MBEDTLS_MD_SHA256
847 #define MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN 32
848 #elif defined(MBEDTLS_SHA512_C)
849 #define MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE MBEDTLS_MD_SHA384
850 #define MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN 48
851 #elif defined(MBEDTLS_SHA1_C)
852 #define MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE MBEDTLS_MD_SHA1
853 #define MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN 20
855 /* This is already checked in check_config.h, but be sure. */
856 #error "Bad configuration - need SHA-1, SHA-256 or SHA-512 enabled to compute digest of peer CRT."
858 #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED &&
859 !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
861 #if defined(MBEDTLS_SSL_DTLS_SRTP)
863 #define MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH 255
864 #define MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH 4
866 * For code readability use a typedef for DTLS-SRTP profiles
868 * Use_srtp extension protection profiles values as defined in
869 * http://www.iana.org/assignments/srtp-protection/srtp-protection.xhtml
871 * Reminder: if this list is expanded mbedtls_ssl_check_srtp_profile_value
872 * must be updated too.
874 #define MBEDTLS_TLS_SRTP_AES128_CM_HMAC_SHA1_80 ( (uint16_t) 0x0001)
875 #define MBEDTLS_TLS_SRTP_AES128_CM_HMAC_SHA1_32 ( (uint16_t) 0x0002)
876 #define MBEDTLS_TLS_SRTP_NULL_HMAC_SHA1_80 ( (uint16_t) 0x0005)
877 #define MBEDTLS_TLS_SRTP_NULL_HMAC_SHA1_32 ( (uint16_t) 0x0006)
878 /* This one is not iana defined, but for code readability. */
879 #define MBEDTLS_TLS_SRTP_UNSET ( (uint16_t) 0x0000)
881 typedef uint16_t mbedtls_ssl_srtp_profile
;
883 typedef struct mbedtls_dtls_srtp_info_t
{
884 /*! The SRTP profile that was negotiated. */
885 mbedtls_ssl_srtp_profile chosen_dtls_srtp_profile
;
886 /*! The length of mki_value. */
888 /*! The mki_value used, with max size of 256 bytes. */
889 unsigned char mki_value
[MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH
];
891 mbedtls_dtls_srtp_info
;
893 #endif /* MBEDTLS_SSL_DTLS_SRTP */
896 * This structure is used for storing current session data.
898 * Note: when changing this definition, we need to check and update:
899 * - in tests/suites/test_suite_ssl.function:
900 * ssl_populate_session() and ssl_serialize_session_save_load()
901 * - in library/ssl_tls.c:
902 * mbedtls_ssl_session_init() and mbedtls_ssl_session_free()
903 * mbedtls_ssl_session_save() and ssl_session_load()
906 struct mbedtls_ssl_session
{
907 #if defined(MBEDTLS_HAVE_TIME)
908 mbedtls_time_t start
; /*!< starting time */
910 int ciphersuite
; /*!< chosen ciphersuite */
911 int compression
; /*!< chosen compression */
912 size_t id_len
; /*!< session id length */
913 unsigned char id
[32]; /*!< session identifier */
914 unsigned char master
[48]; /*!< the master secret */
916 #if defined(MBEDTLS_X509_CRT_PARSE_C)
917 #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
918 mbedtls_x509_crt
*peer_cert
; /*!< peer X.509 cert chain */
919 #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
920 /*! The digest of the peer's end-CRT. This must be kept to detect CRT
921 * changes during renegotiation, mitigating the triple handshake attack. */
922 unsigned char *peer_cert_digest
;
923 size_t peer_cert_digest_len
;
924 mbedtls_md_type_t peer_cert_digest_type
;
925 #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
926 #endif /* MBEDTLS_X509_CRT_PARSE_C */
927 uint32_t verify_result
; /*!< verification result */
929 #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
930 unsigned char *ticket
; /*!< RFC 5077 session ticket */
931 size_t ticket_len
; /*!< session ticket length */
932 uint32_t ticket_lifetime
; /*!< ticket lifetime hint */
933 #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
935 #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
936 unsigned char mfl_code
; /*!< MaxFragmentLength negotiated by peer */
937 #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
939 #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
940 int trunc_hmac
; /*!< flag for truncated hmac activation */
941 #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
943 #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
944 int encrypt_then_mac
; /*!< flag for EtM activation */
949 * SSL/TLS configuration to be shared between mbedtls_ssl_context structures.
951 struct mbedtls_ssl_config
{
952 /* Group items by size (largest first) to minimize padding overhead */
958 const int *ciphersuite_list
[4]; /*!< allowed ciphersuites per version */
960 /** Callback for printing debug output */
961 void (*f_dbg
)(void *, int, const char *, int, const char *);
962 void *p_dbg
; /*!< context for the debug function */
964 /** Callback for getting (pseudo-)random numbers */
965 int (*f_rng
)(void *, unsigned char *, size_t);
966 void *p_rng
; /*!< context for the RNG function */
968 /** Callback to retrieve a session from the cache */
969 int (*f_get_cache
)(void *, mbedtls_ssl_session
*);
970 /** Callback to store a session into the cache */
971 int (*f_set_cache
)(void *, const mbedtls_ssl_session
*);
972 void *p_cache
; /*!< context for cache callbacks */
974 #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
975 /** Callback for setting cert according to SNI extension */
976 int (*f_sni
)(void *, mbedtls_ssl_context
*, const unsigned char *, size_t);
977 void *p_sni
; /*!< context for SNI callback */
980 #if defined(MBEDTLS_X509_CRT_PARSE_C)
981 /** Callback to customize X.509 certificate chain verification */
982 int (*f_vrfy
)(void *, mbedtls_x509_crt
*, int, uint32_t *);
983 void *p_vrfy
; /*!< context for X.509 verify calllback */
986 #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
987 /** Callback to retrieve PSK key from identity */
988 int (*f_psk
)(void *, mbedtls_ssl_context
*, const unsigned char *, size_t);
989 void *p_psk
; /*!< context for PSK callback */
992 #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
993 /** Callback to create & write a cookie for ClientHello veirifcation */
994 int (*f_cookie_write
)(void *, unsigned char **, unsigned char *,
995 const unsigned char *, size_t);
996 /** Callback to verify validity of a ClientHello cookie */
997 int (*f_cookie_check
)(void *, const unsigned char *, size_t,
998 const unsigned char *, size_t);
999 void *p_cookie
; /*!< context for the cookie callbacks */
1002 #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_SRV_C)
1003 /** Callback to create & write a session ticket */
1004 int (*f_ticket_write
)(void *, const mbedtls_ssl_session
*,
1005 unsigned char *, const unsigned char *, size_t *, uint32_t *);
1006 /** Callback to parse a session ticket into a session structure */
1007 int (*f_ticket_parse
)(void *, mbedtls_ssl_session
*, unsigned char *, size_t);
1008 void *p_ticket
; /*!< context for the ticket callbacks */
1009 #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_SRV_C */
1011 #if defined(MBEDTLS_SSL_EXPORT_KEYS)
1012 /** Callback to export key block and master secret */
1013 int (*f_export_keys
)(void *, const unsigned char *,
1014 const unsigned char *, size_t, size_t, size_t);
1015 /** Callback to export key block, master secret,
1016 * tls_prf and random bytes. Should replace f_export_keys */
1017 int (*f_export_keys_ext
)(void *, const unsigned char *,
1018 const unsigned char *, size_t, size_t, size_t,
1019 const unsigned char[32], const unsigned char[32],
1020 mbedtls_tls_prf_types
);
1021 void *p_export_keys
; /*!< context for key export callback */
1024 #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
1025 size_t cid_len
; /*!< The length of CIDs for incoming DTLS records. */
1026 #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
1028 #if defined(MBEDTLS_X509_CRT_PARSE_C)
1029 const mbedtls_x509_crt_profile
*cert_profile
; /*!< verification profile */
1030 mbedtls_ssl_key_cert
*key_cert
; /*!< own certificate/key pair(s) */
1031 mbedtls_x509_crt
*ca_chain
; /*!< trusted CAs */
1032 mbedtls_x509_crl
*ca_crl
; /*!< trusted CAs CRLs */
1033 #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1034 mbedtls_x509_crt_ca_cb_t f_ca_cb
;
1036 #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
1037 #endif /* MBEDTLS_X509_CRT_PARSE_C */
1039 #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
1040 #if defined(MBEDTLS_X509_CRT_PARSE_C)
1041 mbedtls_ssl_async_sign_t
*f_async_sign_start
; /*!< start asynchronous signature operation */
1042 mbedtls_ssl_async_decrypt_t
*f_async_decrypt_start
; /*!< start asynchronous decryption operation */
1043 #endif /* MBEDTLS_X509_CRT_PARSE_C */
1044 mbedtls_ssl_async_resume_t
*f_async_resume
; /*!< resume asynchronous operation */
1045 mbedtls_ssl_async_cancel_t
*f_async_cancel
; /*!< cancel asynchronous operation */
1046 void *p_async_config_data
; /*!< Configuration data set by mbedtls_ssl_conf_async_private_cb(). */
1047 #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
1049 #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
1050 const int *sig_hashes
; /*!< allowed signature hashes */
1053 #if defined(MBEDTLS_ECP_C)
1054 const mbedtls_ecp_group_id
*curve_list
; /*!< allowed curves */
1057 #if defined(MBEDTLS_DHM_C)
1058 mbedtls_mpi dhm_P
; /*!< prime modulus for DHM */
1059 mbedtls_mpi dhm_G
; /*!< generator for DHM */
1062 #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
1064 #if defined(MBEDTLS_USE_PSA_CRYPTO)
1065 psa_key_id_t psk_opaque
; /*!< PSA key slot holding opaque PSK. This field
1066 * should only be set via
1067 * mbedtls_ssl_conf_psk_opaque().
1068 * If either no PSK or a raw PSK have been
1069 * configured, this has value \c 0.
1071 #endif /* MBEDTLS_USE_PSA_CRYPTO */
1073 unsigned char *psk
; /*!< The raw pre-shared key. This field should
1074 * only be set via mbedtls_ssl_conf_psk().
1075 * If either no PSK or an opaque PSK
1076 * have been configured, this has value NULL. */
1077 size_t psk_len
; /*!< The length of the raw pre-shared key.
1078 * This field should only be set via
1079 * mbedtls_ssl_conf_psk().
1080 * Its value is non-zero if and only if
1081 * \c psk is not \c NULL. */
1083 unsigned char *psk_identity
; /*!< The PSK identity for PSK negotiation.
1084 * This field should only be set via
1085 * mbedtls_ssl_conf_psk().
1086 * This is set if and only if either
1087 * \c psk or \c psk_opaque are set. */
1088 size_t psk_identity_len
;/*!< The length of PSK identity.
1089 * This field should only be set via
1090 * mbedtls_ssl_conf_psk().
1091 * Its value is non-zero if and only if
1092 * \c psk is not \c NULL or \c psk_opaque
1094 #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
1096 #if defined(MBEDTLS_SSL_ALPN)
1097 const char **alpn_list
; /*!< ordered list of protocols */
1100 #if defined(MBEDTLS_SSL_DTLS_SRTP)
1101 /*! ordered list of supported srtp profile */
1102 const mbedtls_ssl_srtp_profile
*dtls_srtp_profile_list
;
1103 /*! number of supported profiles */
1104 size_t dtls_srtp_profile_list_len
;
1105 #endif /* MBEDTLS_SSL_DTLS_SRTP */
1108 * Numerical settings (int then char)
1111 uint32_t read_timeout
; /*!< timeout for mbedtls_ssl_read (ms) */
1113 #if defined(MBEDTLS_SSL_PROTO_DTLS)
1114 uint32_t hs_timeout_min
; /*!< initial value of the handshake
1115 retransmission timeout (ms) */
1116 uint32_t hs_timeout_max
; /*!< maximum value of the handshake
1117 retransmission timeout (ms) */
1120 #if defined(MBEDTLS_SSL_RENEGOTIATION)
1121 int renego_max_records
; /*!< grace period for renegotiation */
1122 unsigned char renego_period
[8]; /*!< value of the record counters
1123 that triggers renegotiation */
1126 #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
1127 unsigned int badmac_limit
; /*!< limit of records with a bad MAC */
1130 #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
1131 unsigned int dhm_min_bitlen
; /*!< min. bit length of the DHM prime */
1134 unsigned char max_major_ver
; /*!< max. major version used */
1135 unsigned char max_minor_ver
; /*!< max. minor version used */
1136 unsigned char min_major_ver
; /*!< min. major version used */
1137 unsigned char min_minor_ver
; /*!< min. minor version used */
1143 unsigned int endpoint
: 1; /*!< 0: client, 1: server */
1144 unsigned int transport
: 1; /*!< stream (TLS) or datagram (DTLS) */
1145 unsigned int authmode
: 2; /*!< MBEDTLS_SSL_VERIFY_XXX */
1146 /* needed even with renego disabled for LEGACY_BREAK_HANDSHAKE */
1147 unsigned int allow_legacy_renegotiation
: 2 ; /*!< MBEDTLS_LEGACY_XXX */
1148 #if defined(MBEDTLS_ARC4_C)
1149 unsigned int arc4_disabled
: 1; /*!< blacklist RC4 ciphersuites? */
1151 #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1152 unsigned int mfl_code
: 3; /*!< desired fragment length */
1154 #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1155 unsigned int encrypt_then_mac
: 1 ; /*!< negotiate encrypt-then-mac? */
1157 #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
1158 unsigned int extended_ms
: 1; /*!< negotiate extended master secret? */
1160 #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
1161 unsigned int anti_replay
: 1; /*!< detect and prevent replay? */
1163 #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
1164 unsigned int cbc_record_splitting
: 1; /*!< do cbc record splitting */
1166 #if defined(MBEDTLS_SSL_RENEGOTIATION)
1167 unsigned int disable_renegotiation
: 1; /*!< disable renegotiation? */
1169 #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1170 unsigned int trunc_hmac
: 1; /*!< negotiate truncated hmac? */
1172 #if defined(MBEDTLS_SSL_SESSION_TICKETS)
1173 unsigned int session_tickets
: 1; /*!< use session tickets? */
1175 #if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
1176 unsigned int fallback
: 1; /*!< is this a fallback? */
1178 #if defined(MBEDTLS_SSL_SRV_C)
1179 unsigned int cert_req_ca_list
: 1; /*!< enable sending CA list in
1180 Certificate Request messages? */
1182 #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
1183 unsigned int ignore_unexpected_cid
: 1; /*!< Determines whether DTLS
1184 * record with unexpected CID
1185 * should lead to failure. */
1186 #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
1187 #if defined(MBEDTLS_SSL_DTLS_SRTP)
1188 unsigned int dtls_srtp_mki_support
: 1; /* support having mki_value
1189 in the use_srtp extension */
1193 struct mbedtls_ssl_context
{
1194 const mbedtls_ssl_config
*conf
; /*!< configuration information */
1199 int state
; /*!< SSL handshake: current state */
1200 #if defined(MBEDTLS_SSL_RENEGOTIATION)
1201 int renego_status
; /*!< Initial, in progress, pending? */
1202 int renego_records_seen
; /*!< Records since renego request, or with DTLS,
1203 number of retransmissions of request if
1204 renego_max_records is < 0 */
1205 #endif /* MBEDTLS_SSL_RENEGOTIATION */
1207 int major_ver
; /*!< equal to MBEDTLS_SSL_MAJOR_VERSION_3 */
1208 int minor_ver
; /*!< either 0 (SSL3) or 1 (TLS1.0) */
1210 #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
1211 unsigned badmac_seen
; /*!< records with a bad MAC received */
1212 #endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
1214 #if defined(MBEDTLS_X509_CRT_PARSE_C)
1215 /** Callback to customize X.509 certificate chain verification */
1216 int (*f_vrfy
)(void *, mbedtls_x509_crt
*, int, uint32_t *);
1217 void *p_vrfy
; /*!< context for X.509 verify callback */
1220 mbedtls_ssl_send_t
*f_send
; /*!< Callback for network send */
1221 mbedtls_ssl_recv_t
*f_recv
; /*!< Callback for network receive */
1222 mbedtls_ssl_recv_timeout_t
*f_recv_timeout
;
1223 /*!< Callback for network receive with timeout */
1225 void *p_bio
; /*!< context for I/O operations */
1230 mbedtls_ssl_session
*session_in
; /*!< current session data (in) */
1231 mbedtls_ssl_session
*session_out
; /*!< current session data (out) */
1232 mbedtls_ssl_session
*session
; /*!< negotiated session data */
1233 mbedtls_ssl_session
*session_negotiate
; /*!< session data in negotiation */
1235 mbedtls_ssl_handshake_params
*handshake
; /*!< params required only during
1236 the handshake process */
1239 * Record layer transformations
1241 mbedtls_ssl_transform
*transform_in
; /*!< current transform params (in) */
1242 mbedtls_ssl_transform
*transform_out
; /*!< current transform params (in) */
1243 mbedtls_ssl_transform
*transform
; /*!< negotiated transform params */
1244 mbedtls_ssl_transform
*transform_negotiate
; /*!< transform params in negotiation */
1249 void *p_timer
; /*!< context for the timer callbacks */
1251 mbedtls_ssl_set_timer_t
*f_set_timer
; /*!< set timer callback */
1252 mbedtls_ssl_get_timer_t
*f_get_timer
; /*!< get timer callback */
1255 * Record layer (incoming data)
1257 unsigned char *in_buf
; /*!< input buffer */
1258 unsigned char *in_ctr
; /*!< 64-bit incoming message counter
1259 TLS: maintained by us
1260 DTLS: read from peer */
1261 unsigned char *in_hdr
; /*!< start of record header */
1262 #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
1263 unsigned char *in_cid
; /*!< The start of the CID;
1264 * (the end is marked by in_len). */
1265 #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
1266 unsigned char *in_len
; /*!< two-bytes message length field */
1267 unsigned char *in_iv
; /*!< ivlen-byte IV */
1268 unsigned char *in_msg
; /*!< message contents (in_iv+ivlen) */
1269 unsigned char *in_offt
; /*!< read offset in application data */
1271 int in_msgtype
; /*!< record header: message type */
1272 size_t in_msglen
; /*!< record header: message length */
1273 size_t in_left
; /*!< amount of data read so far */
1274 #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1275 size_t in_buf_len
; /*!< length of input buffer */
1277 #if defined(MBEDTLS_SSL_PROTO_DTLS)
1278 uint16_t in_epoch
; /*!< DTLS epoch for incoming records */
1279 size_t next_record_offset
; /*!< offset of the next record in datagram
1280 (equal to in_left if none) */
1281 #endif /* MBEDTLS_SSL_PROTO_DTLS */
1282 #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
1283 uint64_t in_window_top
; /*!< last validated record seq_num */
1284 uint64_t in_window
; /*!< bitmask for replay detection */
1285 #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
1287 size_t in_hslen
; /*!< current handshake message length,
1288 including the handshake header */
1289 int nb_zero
; /*!< # of 0-length encrypted messages */
1291 int keep_current_message
; /*!< drop or reuse current message
1292 on next call to record layer? */
1294 #if defined(MBEDTLS_SSL_PROTO_DTLS)
1295 uint8_t disable_datagram_packing
; /*!< Disable packing multiple records
1296 * within a single datagram. */
1297 #endif /* MBEDTLS_SSL_PROTO_DTLS */
1300 * Record layer (outgoing data)
1302 unsigned char *out_buf
; /*!< output buffer */
1303 unsigned char *out_ctr
; /*!< 64-bit outgoing message counter */
1304 unsigned char *out_hdr
; /*!< start of record header */
1305 #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
1306 unsigned char *out_cid
; /*!< The start of the CID;
1307 * (the end is marked by in_len). */
1308 #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
1309 unsigned char *out_len
; /*!< two-bytes message length field */
1310 unsigned char *out_iv
; /*!< ivlen-byte IV */
1311 unsigned char *out_msg
; /*!< message contents (out_iv+ivlen) */
1313 int out_msgtype
; /*!< record header: message type */
1314 size_t out_msglen
; /*!< record header: message length */
1315 size_t out_left
; /*!< amount of data not yet written */
1316 #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1317 size_t out_buf_len
; /*!< length of output buffer */
1320 unsigned char cur_out_ctr
[8]; /*!< Outgoing record sequence number. */
1322 #if defined(MBEDTLS_SSL_PROTO_DTLS)
1323 uint16_t mtu
; /*!< path mtu, used to fragment outgoing messages */
1324 #endif /* MBEDTLS_SSL_PROTO_DTLS */
1326 #if defined(MBEDTLS_ZLIB_SUPPORT)
1327 unsigned char *compress_buf
; /*!< zlib data buffer */
1328 #endif /* MBEDTLS_ZLIB_SUPPORT */
1329 #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
1330 signed char split_done
; /*!< current record already splitted? */
1331 #endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
1336 int client_auth
; /*!< flag for client auth. */
1341 #if defined(MBEDTLS_X509_CRT_PARSE_C)
1342 char *hostname
; /*!< expected peer CN for verification
1343 (and SNI if available) */
1344 #endif /* MBEDTLS_X509_CRT_PARSE_C */
1346 #if defined(MBEDTLS_SSL_ALPN)
1347 const char *alpn_chosen
; /*!< negotiated protocol */
1348 #endif /* MBEDTLS_SSL_ALPN */
1350 #if defined(MBEDTLS_SSL_DTLS_SRTP)
1352 * use_srtp extension
1354 mbedtls_dtls_srtp_info dtls_srtp_info
;
1355 #endif /* MBEDTLS_SSL_DTLS_SRTP */
1358 * Information for DTLS hello verify
1360 #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
1361 unsigned char *cli_id
; /*!< transport-level ID of the client */
1362 size_t cli_id_len
; /*!< length of cli_id */
1363 #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
1366 * Secure renegotiation
1368 /* needed to know when to send extension on server */
1369 int secure_renegotiation
; /*!< does peer support legacy or
1370 secure renegotiation */
1371 #if defined(MBEDTLS_SSL_RENEGOTIATION)
1372 size_t verify_data_len
; /*!< length of verify data stored */
1373 char own_verify_data
[MBEDTLS_SSL_VERIFY_DATA_MAX_LEN
]; /*!< previous handshake verify data */
1374 char peer_verify_data
[MBEDTLS_SSL_VERIFY_DATA_MAX_LEN
]; /*!< previous handshake verify data */
1375 #endif /* MBEDTLS_SSL_RENEGOTIATION */
1377 #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
1378 /* CID configuration to use in subsequent handshakes. */
1380 /*! The next incoming CID, chosen by the user and applying to
1381 * all subsequent handshakes. This may be different from the
1382 * CID currently used in case the user has re-configured the CID
1383 * after an initial handshake. */
1384 unsigned char own_cid
[ MBEDTLS_SSL_CID_IN_LEN_MAX
];
1385 uint8_t own_cid_len
; /*!< The length of \c own_cid. */
1386 uint8_t negotiate_cid
; /*!< This indicates whether the CID extension should
1387 * be negotiated in the next handshake or not.
1388 * Possible values are #MBEDTLS_SSL_CID_ENABLED
1389 * and #MBEDTLS_SSL_CID_DISABLED. */
1390 #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
1393 #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1395 #if !defined(MBEDTLS_DEPRECATED_REMOVED)
1397 #define MBEDTLS_SSL_CHANNEL_OUTBOUND MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( 0 )
1398 #define MBEDTLS_SSL_CHANNEL_INBOUND MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( 1 )
1400 #if defined(MBEDTLS_DEPRECATED_WARNING)
1401 #define MBEDTLS_DEPRECATED __attribute__((deprecated))
1403 #define MBEDTLS_DEPRECATED
1404 #endif /* MBEDTLS_DEPRECATED_WARNING */
1406 MBEDTLS_DEPRECATED
extern int (*mbedtls_ssl_hw_record_init
)(
1407 mbedtls_ssl_context
*ssl
,
1408 const unsigned char *key_enc
, const unsigned char *key_dec
,
1410 const unsigned char *iv_enc
, const unsigned char *iv_dec
,
1412 const unsigned char *mac_enc
, const unsigned char *mac_dec
,
1414 MBEDTLS_DEPRECATED
extern int (*mbedtls_ssl_hw_record_activate
)(
1415 mbedtls_ssl_context
*ssl
,
1417 MBEDTLS_DEPRECATED
extern int (*mbedtls_ssl_hw_record_reset
)(
1418 mbedtls_ssl_context
*ssl
);
1419 MBEDTLS_DEPRECATED
extern int (*mbedtls_ssl_hw_record_write
)(
1420 mbedtls_ssl_context
*ssl
);
1421 MBEDTLS_DEPRECATED
extern int (*mbedtls_ssl_hw_record_read
)(
1422 mbedtls_ssl_context
*ssl
);
1423 MBEDTLS_DEPRECATED
extern int (*mbedtls_ssl_hw_record_finish
)(
1424 mbedtls_ssl_context
*ssl
);
1426 #undef MBEDTLS_DEPRECATED
1427 #endif /* !MBEDTLS_DEPRECATED_REMOVED */
1429 #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
1432 * \brief Return the name of the ciphersuite associated with the
1435 * \param ciphersuite_id SSL ciphersuite ID
1437 * \return a string containing the ciphersuite name
1439 const char *mbedtls_ssl_get_ciphersuite_name(const int ciphersuite_id
);
1442 * \brief Return the ID of the ciphersuite associated with the
1445 * \param ciphersuite_name SSL ciphersuite name
1447 * \return the ID with the ciphersuite or 0 if not found
1449 int mbedtls_ssl_get_ciphersuite_id(const char *ciphersuite_name
);
1452 * \brief Initialize an SSL context
1453 * Just makes the context ready for mbedtls_ssl_setup() or
1454 * mbedtls_ssl_free()
1456 * \param ssl SSL context
1458 void mbedtls_ssl_init(mbedtls_ssl_context
*ssl
);
1461 * \brief Set up an SSL context for use
1463 * \note No copy of the configuration context is made, it can be
1464 * shared by many mbedtls_ssl_context structures.
1466 * \warning The conf structure will be accessed during the session.
1467 * It must not be modified or freed as long as the session
1470 * \warning This function must be called exactly once per context.
1471 * Calling mbedtls_ssl_setup again is not supported, even
1472 * if no session is active.
1474 * \param ssl SSL context
1475 * \param conf SSL configuration to use
1477 * \return 0 if successful, or MBEDTLS_ERR_SSL_ALLOC_FAILED if
1478 * memory allocation failed
1480 int mbedtls_ssl_setup(mbedtls_ssl_context
*ssl
,
1481 const mbedtls_ssl_config
*conf
);
1484 * \brief Reset an already initialized SSL context for re-use
1485 * while retaining application-set variables, function
1486 * pointers and data.
1488 * \param ssl SSL context
1489 * \return 0 if successful, or MBEDTLS_ERR_SSL_ALLOC_FAILED,
1490 MBEDTLS_ERR_SSL_HW_ACCEL_FAILED or
1491 * MBEDTLS_ERR_SSL_COMPRESSION_FAILED
1493 int mbedtls_ssl_session_reset(mbedtls_ssl_context
*ssl
);
1496 * \brief Set the current endpoint type
1498 * \param conf SSL configuration
1499 * \param endpoint must be MBEDTLS_SSL_IS_CLIENT or MBEDTLS_SSL_IS_SERVER
1501 void mbedtls_ssl_conf_endpoint(mbedtls_ssl_config
*conf
, int endpoint
);
1504 * \brief Set the transport type (TLS or DTLS).
1507 * \note For DTLS, you must either provide a recv callback that
1508 * doesn't block, or one that handles timeouts, see
1509 * \c mbedtls_ssl_set_bio(). You also need to provide timer
1510 * callbacks with \c mbedtls_ssl_set_timer_cb().
1512 * \param conf SSL configuration
1513 * \param transport transport type:
1514 * MBEDTLS_SSL_TRANSPORT_STREAM for TLS,
1515 * MBEDTLS_SSL_TRANSPORT_DATAGRAM for DTLS.
1517 void mbedtls_ssl_conf_transport(mbedtls_ssl_config
*conf
, int transport
);
1520 * \brief Set the certificate verification mode
1521 * Default: NONE on server, REQUIRED on client
1523 * \param conf SSL configuration
1524 * \param authmode can be:
1526 * MBEDTLS_SSL_VERIFY_NONE: peer certificate is not checked
1527 * (default on server)
1528 * (insecure on client)
1530 * MBEDTLS_SSL_VERIFY_OPTIONAL: peer certificate is checked, however the
1531 * handshake continues even if verification failed;
1532 * mbedtls_ssl_get_verify_result() can be called after the
1533 * handshake is complete.
1535 * MBEDTLS_SSL_VERIFY_REQUIRED: peer *must* present a valid certificate,
1536 * handshake is aborted if verification failed.
1537 * (default on client)
1539 * \note On client, MBEDTLS_SSL_VERIFY_REQUIRED is the recommended mode.
1540 * With MBEDTLS_SSL_VERIFY_OPTIONAL, the user needs to call mbedtls_ssl_get_verify_result() at
1541 * the right time(s), which may not be obvious, while REQUIRED always perform
1542 * the verification as soon as possible. For example, REQUIRED was protecting
1543 * against the "triple handshake" attack even before it was found.
1545 void mbedtls_ssl_conf_authmode(mbedtls_ssl_config
*conf
, int authmode
);
1547 #if defined(MBEDTLS_X509_CRT_PARSE_C)
1549 * \brief Set the verification callback (Optional).
1551 * If set, the provided verify callback is called for each
1552 * certificate in the peer's CRT chain, including the trusted
1553 * root. For more information, please see the documentation of
1554 * \c mbedtls_x509_crt_verify().
1556 * \note For per context callbacks and contexts, please use
1557 * mbedtls_ssl_set_verify() instead.
1559 * \param conf The SSL configuration to use.
1560 * \param f_vrfy The verification callback to use during CRT verification.
1561 * \param p_vrfy The opaque context to be passed to the callback.
1563 void mbedtls_ssl_conf_verify(mbedtls_ssl_config
*conf
,
1564 int (*f_vrfy
)(void *, mbedtls_x509_crt
*, int, uint32_t *),
1566 #endif /* MBEDTLS_X509_CRT_PARSE_C */
1569 * \brief Set the random number generator callback
1571 * \param conf SSL configuration
1572 * \param f_rng RNG function
1573 * \param p_rng RNG parameter
1575 void mbedtls_ssl_conf_rng(mbedtls_ssl_config
*conf
,
1576 int (*f_rng
)(void *, unsigned char *, size_t),
1580 * \brief Set the debug callback
1582 * The callback has the following argument:
1583 * void * opaque context for the callback
1585 * const char * file name
1587 * const char * message
1589 * \param conf SSL configuration
1590 * \param f_dbg debug function
1591 * \param p_dbg debug parameter
1593 void mbedtls_ssl_conf_dbg(mbedtls_ssl_config
*conf
,
1594 void (*f_dbg
)(void *, int, const char *, int, const char *),
1598 * \brief Set the underlying BIO callbacks for write, read and
1599 * read-with-timeout.
1601 * \param ssl SSL context
1602 * \param p_bio parameter (context) shared by BIO callbacks
1603 * \param f_send write callback
1604 * \param f_recv read callback
1605 * \param f_recv_timeout blocking read callback with timeout.
1607 * \note One of f_recv or f_recv_timeout can be NULL, in which case
1608 * the other is used. If both are non-NULL, f_recv_timeout is
1609 * used and f_recv is ignored (as if it were NULL).
1611 * \note The two most common use cases are:
1612 * - non-blocking I/O, f_recv != NULL, f_recv_timeout == NULL
1613 * - blocking I/O, f_recv == NULL, f_recv_timout != NULL
1615 * \note For DTLS, you need to provide either a non-NULL
1616 * f_recv_timeout callback, or a f_recv that doesn't block.
1618 * \note See the documentations of \c mbedtls_ssl_send_t,
1619 * \c mbedtls_ssl_recv_t and \c mbedtls_ssl_recv_timeout_t for
1620 * the conventions those callbacks must follow.
1622 * \note On some platforms, net_sockets.c provides
1623 * \c mbedtls_net_send(), \c mbedtls_net_recv() and
1624 * \c mbedtls_net_recv_timeout() that are suitable to be used
1627 void mbedtls_ssl_set_bio(mbedtls_ssl_context
*ssl
,
1629 mbedtls_ssl_send_t
*f_send
,
1630 mbedtls_ssl_recv_t
*f_recv
,
1631 mbedtls_ssl_recv_timeout_t
*f_recv_timeout
);
1633 #if defined(MBEDTLS_SSL_PROTO_DTLS)
1635 #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
1639 * \brief Configure the use of the Connection ID (CID)
1640 * extension in the next handshake.
1642 * Reference: draft-ietf-tls-dtls-connection-id-05
1643 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05
1645 * The DTLS CID extension allows the reliable association of
1646 * DTLS records to DTLS connections across changes in the
1647 * underlying transport (changed IP and Port metadata) by
1648 * adding explicit connection identifiers (CIDs) to the
1649 * headers of encrypted DTLS records. The desired CIDs are
1650 * configured by the application layer and are exchanged in
1651 * new `ClientHello` / `ServerHello` extensions during the
1652 * handshake, where each side indicates the CID it wants the
1653 * peer to use when writing encrypted messages. The CIDs are
1654 * put to use once records get encrypted: the stack discards
1655 * any incoming records that don't include the configured CID
1656 * in their header, and adds the peer's requested CID to the
1657 * headers of outgoing messages.
1659 * This API enables or disables the use of the CID extension
1660 * in the next handshake and sets the value of the CID to
1661 * be used for incoming messages.
1663 * \param ssl The SSL context to configure. This must be initialized.
1664 * \param enable This value determines whether the CID extension should
1665 * be used or not. Possible values are:
1666 * - MBEDTLS_SSL_CID_ENABLED to enable the use of the CID.
1667 * - MBEDTLS_SSL_CID_DISABLED (default) to disable the use
1669 * \param own_cid The address of the readable buffer holding the CID we want
1670 * the peer to use when sending encrypted messages to us.
1671 * This may be \c NULL if \p own_cid_len is \c 0.
1672 * This parameter is unused if \p enabled is set to
1673 * MBEDTLS_SSL_CID_DISABLED.
1674 * \param own_cid_len The length of \p own_cid.
1675 * This parameter is unused if \p enabled is set to
1676 * MBEDTLS_SSL_CID_DISABLED.
1678 * \note The value of \p own_cid_len must match the value of the
1679 * \c len parameter passed to mbedtls_ssl_conf_cid()
1680 * when configuring the ::mbedtls_ssl_config that \p ssl
1683 * \note This CID configuration applies to subsequent handshakes
1684 * performed on the SSL context \p ssl, but does not trigger
1685 * one. You still have to call `mbedtls_ssl_handshake()`
1686 * (for the initial handshake) or `mbedtls_ssl_renegotiate()`
1687 * (for a renegotiation handshake) explicitly after a
1688 * successful call to this function to run the handshake.
1690 * \note This call cannot guarantee that the use of the CID
1691 * will be successfully negotiated in the next handshake,
1692 * because the peer might not support it. Specifically:
1693 * - On the Client, enabling the use of the CID through
1694 * this call implies that the `ClientHello` in the next
1695 * handshake will include the CID extension, thereby
1696 * offering the use of the CID to the server. Only if
1697 * the `ServerHello` contains the CID extension, too,
1698 * the CID extension will actually be put to use.
1699 * - On the Server, enabling the use of the CID through
1700 * this call implies that that the server will look for
1701 * the CID extension in a `ClientHello` from the client,
1702 * and, if present, reply with a CID extension in its
1705 * \note To check whether the use of the CID was negotiated
1706 * after the subsequent handshake has completed, please
1707 * use the API mbedtls_ssl_get_peer_cid().
1709 * \warning If the use of the CID extension is enabled in this call
1710 * and the subsequent handshake negotiates its use, Mbed TLS
1711 * will silently drop every packet whose CID does not match
1712 * the CID configured in \p own_cid. It is the responsibility
1713 * of the user to adapt the underlying transport to take care
1714 * of CID-based demultiplexing before handing datagrams to
1717 * \return \c 0 on success. In this case, the CID configuration
1718 * applies to the next handshake.
1719 * \return A negative error code on failure.
1721 int mbedtls_ssl_set_cid(mbedtls_ssl_context
*ssl
,
1723 unsigned char const *own_cid
,
1724 size_t own_cid_len
);
1727 * \brief Get information about the use of the CID extension
1728 * in the current connection.
1730 * \param ssl The SSL context to query.
1731 * \param enabled The address at which to store whether the CID extension
1732 * is currently in use or not. If the CID is in use,
1733 * `*enabled` is set to MBEDTLS_SSL_CID_ENABLED;
1734 * otherwise, it is set to MBEDTLS_SSL_CID_DISABLED.
1735 * \param peer_cid The address of the buffer in which to store the CID
1736 * chosen by the peer (if the CID extension is used).
1737 * This may be \c NULL in case the value of peer CID
1738 * isn't needed. If it is not \c NULL, \p peer_cid_len
1739 * must not be \c NULL.
1740 * \param peer_cid_len The address at which to store the size of the CID
1741 * chosen by the peer (if the CID extension is used).
1742 * This is also the number of Bytes in \p peer_cid that
1743 * have been written.
1744 * This may be \c NULL in case the length of the peer CID
1745 * isn't needed. If it is \c NULL, \p peer_cid must be
1748 * \note This applies to the state of the CID negotiated in
1749 * the last complete handshake. If a handshake is in
1750 * progress, this function will attempt to complete
1751 * the handshake first.
1753 * \note If CID extensions have been exchanged but both client
1754 * and server chose to use an empty CID, this function
1755 * sets `*enabled` to #MBEDTLS_SSL_CID_DISABLED
1756 * (the rationale for this is that the resulting
1757 * communication is the same as if the CID extensions
1758 * hadn't been used).
1760 * \return \c 0 on success.
1761 * \return A negative error code on failure.
1763 int mbedtls_ssl_get_peer_cid(mbedtls_ssl_context
*ssl
,
1765 unsigned char peer_cid
[ MBEDTLS_SSL_CID_OUT_LEN_MAX
],
1766 size_t *peer_cid_len
);
1768 #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
1771 * \brief Set the Maximum Tranport Unit (MTU).
1772 * Special value: 0 means unset (no limit).
1773 * This represents the maximum size of a datagram payload
1774 * handled by the transport layer (usually UDP) as determined
1775 * by the network link and stack. In practice, this controls
1776 * the maximum size datagram the DTLS layer will pass to the
1777 * \c f_send() callback set using \c mbedtls_ssl_set_bio().
1779 * \note The limit on datagram size is converted to a limit on
1780 * record payload by subtracting the current overhead of
1781 * encapsulation and encryption/authentication if any.
1783 * \note This can be called at any point during the connection, for
1784 * example when a Path Maximum Transfer Unit (PMTU)
1785 * estimate becomes available from other sources,
1786 * such as lower (or higher) protocol layers.
1788 * \note This setting only controls the size of the packets we send,
1789 * and does not restrict the size of the datagrams we're
1790 * willing to receive. Client-side, you can request the
1791 * server to use smaller records with \c
1792 * mbedtls_ssl_conf_max_frag_len().
1794 * \note If both a MTU and a maximum fragment length have been
1795 * configured (or negotiated with the peer), the resulting
1796 * lower limit on record payload (see first note) is used.
1798 * \note This can only be used to decrease the maximum size
1799 * of datagrams (hence records, see first note) sent. It
1800 * cannot be used to increase the maximum size of records over
1801 * the limit set by #MBEDTLS_SSL_OUT_CONTENT_LEN.
1803 * \note Values lower than the current record layer expansion will
1804 * result in an error when trying to send data.
1806 * \note Using record compression together with a non-zero MTU value
1807 * will result in an error when trying to send data.
1809 * \param ssl SSL context
1810 * \param mtu Value of the path MTU in bytes
1812 void mbedtls_ssl_set_mtu(mbedtls_ssl_context
*ssl
, uint16_t mtu
);
1813 #endif /* MBEDTLS_SSL_PROTO_DTLS */
1815 #if defined(MBEDTLS_X509_CRT_PARSE_C)
1817 * \brief Set a connection-specific verification callback (optional).
1819 * If set, the provided verify callback is called for each
1820 * certificate in the peer's CRT chain, including the trusted
1821 * root. For more information, please see the documentation of
1822 * \c mbedtls_x509_crt_verify().
1824 * \note This call is analogous to mbedtls_ssl_conf_verify() but
1825 * binds the verification callback and context to an SSL context
1826 * as opposed to an SSL configuration.
1827 * If mbedtls_ssl_conf_verify() and mbedtls_ssl_set_verify()
1828 * are both used, mbedtls_ssl_set_verify() takes precedence.
1830 * \param ssl The SSL context to use.
1831 * \param f_vrfy The verification callback to use during CRT verification.
1832 * \param p_vrfy The opaque context to be passed to the callback.
1834 void mbedtls_ssl_set_verify(mbedtls_ssl_context
*ssl
,
1835 int (*f_vrfy
)(void *, mbedtls_x509_crt
*, int, uint32_t *),
1837 #endif /* MBEDTLS_X509_CRT_PARSE_C */
1840 * \brief Set the timeout period for mbedtls_ssl_read()
1841 * (Default: no timeout.)
1843 * \param conf SSL configuration context
1844 * \param timeout Timeout value in milliseconds.
1845 * Use 0 for no timeout (default).
1847 * \note With blocking I/O, this will only work if a non-NULL
1848 * \c f_recv_timeout was set with \c mbedtls_ssl_set_bio().
1849 * With non-blocking I/O, this will only work if timer
1850 * callbacks were set with \c mbedtls_ssl_set_timer_cb().
1852 * \note With non-blocking I/O, you may also skip this function
1853 * altogether and handle timeouts at the application layer.
1855 void mbedtls_ssl_conf_read_timeout(mbedtls_ssl_config
*conf
, uint32_t timeout
);
1857 #if defined(MBEDTLS_SSL_RECORD_CHECKING)
1859 * \brief Check whether a buffer contains a valid and authentic record
1860 * that has not been seen before. (DTLS only).
1862 * This function does not change the user-visible state
1863 * of the SSL context. Its sole purpose is to provide
1864 * an indication of the legitimacy of an incoming record.
1866 * This can be useful e.g. in distributed server environments
1867 * using the DTLS Connection ID feature, in which connections
1868 * might need to be passed between service instances on a change
1869 * of peer address, but where such disruptive operations should
1870 * only happen after the validity of incoming records has been
1873 * \param ssl The SSL context to use.
1874 * \param buf The address of the buffer holding the record to be checked.
1875 * This must be a read/write buffer of length \p buflen Bytes.
1876 * \param buflen The length of \p buf in Bytes.
1878 * \note This routine only checks whether the provided buffer begins
1879 * with a valid and authentic record that has not been seen
1880 * before, but does not check potential data following the
1881 * initial record. In particular, it is possible to pass DTLS
1882 * datagrams containing multiple records, in which case only
1883 * the first record is checked.
1885 * \note This function modifies the input buffer \p buf. If you need
1886 * to preserve the original record, you have to maintain a copy.
1888 * \return \c 0 if the record is valid and authentic and has not been
1890 * \return MBEDTLS_ERR_SSL_INVALID_MAC if the check completed
1891 * successfully but the record was found to be not authentic.
1892 * \return MBEDTLS_ERR_SSL_INVALID_RECORD if the check completed
1893 * successfully but the record was found to be invalid for
1894 * a reason different from authenticity checking.
1895 * \return MBEDTLS_ERR_SSL_UNEXPECTED_RECORD if the check completed
1896 * successfully but the record was found to be unexpected
1897 * in the state of the SSL context, including replayed records.
1898 * \return Another negative error code on different kinds of failure.
1899 * In this case, the SSL context becomes unusable and needs
1900 * to be freed or reset before reuse.
1902 int mbedtls_ssl_check_record(mbedtls_ssl_context
const *ssl
,
1905 #endif /* MBEDTLS_SSL_RECORD_CHECKING */
1908 * \brief Set the timer callbacks (Mandatory for DTLS.)
1910 * \param ssl SSL context
1911 * \param p_timer parameter (context) shared by timer callbacks
1912 * \param f_set_timer set timer callback
1913 * \param f_get_timer get timer callback. Must return:
1915 * \note See the documentation of \c mbedtls_ssl_set_timer_t and
1916 * \c mbedtls_ssl_get_timer_t for the conventions this pair of
1917 * callbacks must follow.
1919 * \note On some platforms, timing.c provides
1920 * \c mbedtls_timing_set_delay() and
1921 * \c mbedtls_timing_get_delay() that are suitable for using
1922 * here, except if using an event-driven style.
1924 * \note See also the "DTLS tutorial" article in our knowledge base.
1925 * https://tls.mbed.org/kb/how-to/dtls-tutorial
1927 void mbedtls_ssl_set_timer_cb(mbedtls_ssl_context
*ssl
,
1929 mbedtls_ssl_set_timer_t
*f_set_timer
,
1930 mbedtls_ssl_get_timer_t
*f_get_timer
);
1933 * \brief Callback type: generate and write session ticket
1935 * \note This describes what a callback implementation should do.
1936 * This callback should generate an encrypted and
1937 * authenticated ticket for the session and write it to the
1938 * output buffer. Here, ticket means the opaque ticket part
1939 * of the NewSessionTicket structure of RFC 5077.
1941 * \param p_ticket Context for the callback
1942 * \param session SSL session to be written in the ticket
1943 * \param start Start of the output buffer
1944 * \param end End of the output buffer
1945 * \param tlen On exit, holds the length written
1946 * \param lifetime On exit, holds the lifetime of the ticket in seconds
1948 * \return 0 if successful, or
1949 * a specific MBEDTLS_ERR_XXX code.
1951 typedef int mbedtls_ssl_ticket_write_t(void *p_ticket
,
1952 const mbedtls_ssl_session
*session
,
1953 unsigned char *start
,
1954 const unsigned char *end
,
1956 uint32_t *lifetime
);
1958 #if defined(MBEDTLS_SSL_EXPORT_KEYS)
1960 * \brief Callback type: Export key block and master secret
1962 * \note This is required for certain uses of TLS, e.g. EAP-TLS
1963 * (RFC 5216) and Thread. The key pointers are ephemeral and
1964 * therefore must not be stored. The master secret and keys
1965 * should not be used directly except as an input to a key
1966 * derivation function.
1968 * \param p_expkey Context for the callback
1969 * \param ms Pointer to master secret (fixed length: 48 bytes)
1970 * \param kb Pointer to key block, see RFC 5246 section 6.3
1971 * (variable length: 2 * maclen + 2 * keylen + 2 * ivlen).
1972 * \param maclen MAC length
1973 * \param keylen Key length
1974 * \param ivlen IV length
1976 * \return 0 if successful, or
1977 * a specific MBEDTLS_ERR_XXX code.
1979 typedef int mbedtls_ssl_export_keys_t(void *p_expkey
,
1980 const unsigned char *ms
,
1981 const unsigned char *kb
,
1987 * \brief Callback type: Export key block, master secret,
1988 * handshake randbytes and the tls_prf function
1989 * used to derive keys.
1991 * \note This is required for certain uses of TLS, e.g. EAP-TLS
1992 * (RFC 5216) and Thread. The key pointers are ephemeral and
1993 * therefore must not be stored. The master secret and keys
1994 * should not be used directly except as an input to a key
1995 * derivation function.
1997 * \param p_expkey Context for the callback.
1998 * \param ms Pointer to master secret (fixed length: 48 bytes).
1999 * \param kb Pointer to key block, see RFC 5246 section 6.3.
2000 * (variable length: 2 * maclen + 2 * keylen + 2 * ivlen).
2001 * \param maclen MAC length.
2002 * \param keylen Key length.
2003 * \param ivlen IV length.
2004 * \param client_random The client random bytes.
2005 * \param server_random The server random bytes.
2006 * \param tls_prf_type The tls_prf enum type.
2008 * \return 0 if successful, or
2009 * a specific MBEDTLS_ERR_XXX code.
2011 typedef int mbedtls_ssl_export_keys_ext_t(void *p_expkey
,
2012 const unsigned char *ms
,
2013 const unsigned char *kb
,
2017 const unsigned char client_random
[32],
2018 const unsigned char server_random
[32],
2019 mbedtls_tls_prf_types tls_prf_type
);
2020 #endif /* MBEDTLS_SSL_EXPORT_KEYS */
2023 * \brief Callback type: parse and load session ticket
2025 * \note This describes what a callback implementation should do.
2026 * This callback should parse a session ticket as generated
2027 * by the corresponding mbedtls_ssl_ticket_write_t function,
2028 * and, if the ticket is authentic and valid, load the
2031 * \note The implementation is allowed to modify the first len
2032 * bytes of the input buffer, eg to use it as a temporary
2033 * area for the decrypted ticket contents.
2035 * \param p_ticket Context for the callback
2036 * \param session SSL session to be loaded
2037 * \param buf Start of the buffer containing the ticket
2038 * \param len Length of the ticket.
2040 * \return 0 if successful, or
2041 * MBEDTLS_ERR_SSL_INVALID_MAC if not authentic, or
2042 * MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED if expired, or
2043 * any other non-zero code for other failures.
2045 typedef int mbedtls_ssl_ticket_parse_t(void *p_ticket
,
2046 mbedtls_ssl_session
*session
,
2050 #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_SRV_C)
2052 * \brief Configure SSL session ticket callbacks (server only).
2055 * \note On server, session tickets are enabled by providing
2056 * non-NULL callbacks.
2058 * \note On client, use \c mbedtls_ssl_conf_session_tickets().
2060 * \param conf SSL configuration context
2061 * \param f_ticket_write Callback for writing a ticket
2062 * \param f_ticket_parse Callback for parsing a ticket
2063 * \param p_ticket Context shared by the two callbacks
2065 void mbedtls_ssl_conf_session_tickets_cb(mbedtls_ssl_config
*conf
,
2066 mbedtls_ssl_ticket_write_t
*f_ticket_write
,
2067 mbedtls_ssl_ticket_parse_t
*f_ticket_parse
,
2069 #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_SRV_C */
2071 #if defined(MBEDTLS_SSL_EXPORT_KEYS)
2073 * \brief Configure key export callback.
2076 * \note See \c mbedtls_ssl_export_keys_t.
2078 * \param conf SSL configuration context
2079 * \param f_export_keys Callback for exporting keys
2080 * \param p_export_keys Context for the callback
2082 void mbedtls_ssl_conf_export_keys_cb(mbedtls_ssl_config
*conf
,
2083 mbedtls_ssl_export_keys_t
*f_export_keys
,
2084 void *p_export_keys
);
2087 * \brief Configure extended key export callback.
2090 * \note See \c mbedtls_ssl_export_keys_ext_t.
2091 * \warning Exported key material must not be used for any purpose
2092 * before the (D)TLS handshake is completed
2094 * \param conf SSL configuration context
2095 * \param f_export_keys_ext Callback for exporting keys
2096 * \param p_export_keys Context for the callback
2098 void mbedtls_ssl_conf_export_keys_ext_cb(mbedtls_ssl_config
*conf
,
2099 mbedtls_ssl_export_keys_ext_t
*f_export_keys_ext
,
2100 void *p_export_keys
);
2101 #endif /* MBEDTLS_SSL_EXPORT_KEYS */
2103 #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
2105 * \brief Configure asynchronous private key operation callbacks.
2107 * \param conf SSL configuration context
2108 * \param f_async_sign Callback to start a signature operation. See
2109 * the description of ::mbedtls_ssl_async_sign_t
2110 * for more information. This may be \c NULL if the
2111 * external processor does not support any signature
2112 * operation; in this case the private key object
2113 * associated with the certificate will be used.
2114 * \param f_async_decrypt Callback to start a decryption operation. See
2115 * the description of ::mbedtls_ssl_async_decrypt_t
2116 * for more information. This may be \c NULL if the
2117 * external processor does not support any decryption
2118 * operation; in this case the private key object
2119 * associated with the certificate will be used.
2120 * \param f_async_resume Callback to resume an asynchronous operation. See
2121 * the description of ::mbedtls_ssl_async_resume_t
2122 * for more information. This may not be \c NULL unless
2123 * \p f_async_sign and \p f_async_decrypt are both
2125 * \param f_async_cancel Callback to cancel an asynchronous operation. See
2126 * the description of ::mbedtls_ssl_async_cancel_t
2127 * for more information. This may be \c NULL if
2128 * no cleanup is needed.
2129 * \param config_data A pointer to configuration data which can be
2131 * mbedtls_ssl_conf_get_async_config_data(). The
2132 * library stores this value without dereferencing it.
2134 void mbedtls_ssl_conf_async_private_cb(mbedtls_ssl_config
*conf
,
2135 mbedtls_ssl_async_sign_t
*f_async_sign
,
2136 mbedtls_ssl_async_decrypt_t
*f_async_decrypt
,
2137 mbedtls_ssl_async_resume_t
*f_async_resume
,
2138 mbedtls_ssl_async_cancel_t
*f_async_cancel
,
2142 * \brief Retrieve the configuration data set by
2143 * mbedtls_ssl_conf_async_private_cb().
2145 * \param conf SSL configuration context
2146 * \return The configuration data set by
2147 * mbedtls_ssl_conf_async_private_cb().
2149 void *mbedtls_ssl_conf_get_async_config_data(const mbedtls_ssl_config
*conf
);
2152 * \brief Retrieve the asynchronous operation user context.
2154 * \note This function may only be called while a handshake
2157 * \param ssl The SSL context to access.
2159 * \return The asynchronous operation user context that was last
2160 * set during the current handshake. If
2161 * mbedtls_ssl_set_async_operation_data() has not yet been
2162 * called during the current handshake, this function returns
2165 void *mbedtls_ssl_get_async_operation_data(const mbedtls_ssl_context
*ssl
);
2168 * \brief Retrieve the asynchronous operation user context.
2170 * \note This function may only be called while a handshake
2173 * \param ssl The SSL context to access.
2174 * \param ctx The new value of the asynchronous operation user context.
2175 * Call mbedtls_ssl_get_async_operation_data() later during the
2176 * same handshake to retrieve this value.
2178 void mbedtls_ssl_set_async_operation_data(mbedtls_ssl_context
*ssl
,
2180 #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
2183 * \brief Callback type: generate a cookie
2185 * \param ctx Context for the callback
2186 * \param p Buffer to write to,
2187 * must be updated to point right after the cookie
2188 * \param end Pointer to one past the end of the output buffer
2189 * \param info Client ID info that was passed to
2190 * \c mbedtls_ssl_set_client_transport_id()
2191 * \param ilen Length of info in bytes
2193 * \return The callback must return 0 on success,
2194 * or a negative error code.
2196 typedef int mbedtls_ssl_cookie_write_t(void *ctx
,
2197 unsigned char **p
, unsigned char *end
,
2198 const unsigned char *info
, size_t ilen
);
2201 * \brief Callback type: verify a cookie
2203 * \param ctx Context for the callback
2204 * \param cookie Cookie to verify
2205 * \param clen Length of cookie
2206 * \param info Client ID info that was passed to
2207 * \c mbedtls_ssl_set_client_transport_id()
2208 * \param ilen Length of info in bytes
2210 * \return The callback must return 0 if cookie is valid,
2211 * or a negative error code.
2213 typedef int mbedtls_ssl_cookie_check_t(void *ctx
,
2214 const unsigned char *cookie
, size_t clen
,
2215 const unsigned char *info
, size_t ilen
);
2217 #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
2219 * \brief Register callbacks for DTLS cookies
2220 * (Server only. DTLS only.)
2222 * Default: dummy callbacks that fail, in order to force you to
2223 * register working callbacks (and initialize their context).
2225 * To disable HelloVerifyRequest, register NULL callbacks.
2227 * \warning Disabling hello verification allows your server to be used
2228 * for amplification in DoS attacks against other hosts.
2229 * Only disable if you known this can't happen in your
2230 * particular environment.
2232 * \note See comments on \c mbedtls_ssl_handshake() about handling
2233 * the MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED that is expected
2234 * on the first handshake attempt when this is enabled.
2236 * \note This is also necessary to handle client reconnection from
2237 * the same port as described in RFC 6347 section 4.2.8 (only
2238 * the variant with cookies is supported currently). See
2239 * comments on \c mbedtls_ssl_read() for details.
2241 * \param conf SSL configuration
2242 * \param f_cookie_write Cookie write callback
2243 * \param f_cookie_check Cookie check callback
2244 * \param p_cookie Context for both callbacks
2246 void mbedtls_ssl_conf_dtls_cookies(mbedtls_ssl_config
*conf
,
2247 mbedtls_ssl_cookie_write_t
*f_cookie_write
,
2248 mbedtls_ssl_cookie_check_t
*f_cookie_check
,
2252 * \brief Set client's transport-level identification info.
2253 * (Server only. DTLS only.)
2255 * This is usually the IP address (and port), but could be
2256 * anything identify the client depending on the underlying
2257 * network stack. Used for HelloVerifyRequest with DTLS.
2258 * This is *not* used to route the actual packets.
2260 * \param ssl SSL context
2261 * \param info Transport-level info identifying the client (eg IP + port)
2262 * \param ilen Length of info in bytes
2264 * \note An internal copy is made, so the info buffer can be reused.
2266 * \return 0 on success,
2267 * MBEDTLS_ERR_SSL_BAD_INPUT_DATA if used on client,
2268 * MBEDTLS_ERR_SSL_ALLOC_FAILED if out of memory.
2270 int mbedtls_ssl_set_client_transport_id(mbedtls_ssl_context
*ssl
,
2271 const unsigned char *info
,
2274 #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
2276 #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
2278 * \brief Enable or disable anti-replay protection for DTLS.
2279 * (DTLS only, no effect on TLS.)
2282 * \param conf SSL configuration
2283 * \param mode MBEDTLS_SSL_ANTI_REPLAY_ENABLED or MBEDTLS_SSL_ANTI_REPLAY_DISABLED.
2285 * \warning Disabling this is a security risk unless the application
2286 * protocol handles duplicated packets in a safe way. You
2287 * should not disable this without careful consideration.
2288 * However, if your application already detects duplicated
2289 * packets and needs information about them to adjust its
2290 * transmission strategy, then you'll want to disable this.
2292 void mbedtls_ssl_conf_dtls_anti_replay(mbedtls_ssl_config
*conf
, char mode
);
2293 #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
2295 #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
2297 * \brief Set a limit on the number of records with a bad MAC
2298 * before terminating the connection.
2299 * (DTLS only, no effect on TLS.)
2300 * Default: 0 (disabled).
2302 * \param conf SSL configuration
2303 * \param limit Limit, or 0 to disable.
2305 * \note If the limit is N, then the connection is terminated when
2306 * the Nth non-authentic record is seen.
2308 * \note Records with an invalid header are not counted, only the
2309 * ones going through the authentication-decryption phase.
2311 * \note This is a security trade-off related to the fact that it's
2312 * often relatively easy for an active attacker ot inject UDP
2313 * datagrams. On one hand, setting a low limit here makes it
2314 * easier for such an attacker to forcibly terminated a
2315 * connection. On the other hand, a high limit or no limit
2316 * might make us waste resources checking authentication on
2317 * many bogus packets.
2319 void mbedtls_ssl_conf_dtls_badmac_limit(mbedtls_ssl_config
*conf
, unsigned limit
);
2320 #endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
2322 #if defined(MBEDTLS_SSL_PROTO_DTLS)
2325 * \brief Allow or disallow packing of multiple handshake records
2326 * within a single datagram.
2328 * \param ssl The SSL context to configure.
2329 * \param allow_packing This determines whether datagram packing may
2330 * be used or not. A value of \c 0 means that every
2331 * record will be sent in a separate datagram; a
2332 * value of \c 1 means that, if space permits,
2333 * multiple handshake messages (including CCS) belonging to
2334 * a single flight may be packed within a single datagram.
2336 * \note This is enabled by default and should only be disabled
2337 * for test purposes, or if datagram packing causes
2338 * interoperability issues with peers that don't support it.
2340 * \note Allowing datagram packing reduces the network load since
2341 * there's less overhead if multiple messages share the same
2342 * datagram. Also, it increases the handshake efficiency
2343 * since messages belonging to a single datagram will not
2344 * be reordered in transit, and so future message buffering
2345 * or flight retransmission (if no buffering is used) as
2346 * means to deal with reordering are needed less frequently.
2348 * \note Application records are not affected by this option and
2349 * are currently always sent in separate datagrams.
2352 void mbedtls_ssl_set_datagram_packing(mbedtls_ssl_context
*ssl
,
2353 unsigned allow_packing
);
2356 * \brief Set retransmit timeout values for the DTLS handshake.
2357 * (DTLS only, no effect on TLS.)
2359 * \param conf SSL configuration
2360 * \param min Initial timeout value in milliseconds.
2361 * Default: 1000 (1 second).
2362 * \param max Maximum timeout value in milliseconds.
2363 * Default: 60000 (60 seconds).
2365 * \note Default values are from RFC 6347 section 4.2.4.1.
2367 * \note The 'min' value should typically be slightly above the
2368 * expected round-trip time to your peer, plus whatever time
2369 * it takes for the peer to process the message. For example,
2370 * if your RTT is about 600ms and you peer needs up to 1s to
2371 * do the cryptographic operations in the handshake, then you
2372 * should set 'min' slightly above 1600. Lower values of 'min'
2373 * might cause spurious resends which waste network resources,
2374 * while larger value of 'min' will increase overall latency
2375 * on unreliable network links.
2377 * \note The more unreliable your network connection is, the larger
2378 * your max / min ratio needs to be in order to achieve
2379 * reliable handshakes.
2381 * \note Messages are retransmitted up to log2(ceil(max/min)) times.
2382 * For example, if min = 1s and max = 5s, the retransmit plan
2383 * goes: send ... 1s -> resend ... 2s -> resend ... 4s ->
2384 * resend ... 5s -> give up and return a timeout error.
2386 void mbedtls_ssl_conf_handshake_timeout(mbedtls_ssl_config
*conf
, uint32_t min
, uint32_t max
);
2387 #endif /* MBEDTLS_SSL_PROTO_DTLS */
2389 #if defined(MBEDTLS_SSL_SRV_C)
2391 * \brief Set the session cache callbacks (server-side only)
2392 * If not set, no session resuming is done (except if session
2393 * tickets are enabled too).
2395 * The session cache has the responsibility to check for stale
2396 * entries based on timeout. See RFC 5246 for recommendations.
2398 * Warning: session.peer_cert is cleared by the SSL/TLS layer on
2399 * connection shutdown, so do not cache the pointer! Either set
2400 * it to NULL or make a full copy of the certificate.
2402 * The get callback is called once during the initial handshake
2403 * to enable session resuming. The get function has the
2404 * following parameters: (void *parameter, mbedtls_ssl_session *session)
2405 * If a valid entry is found, it should fill the master of
2406 * the session object with the cached values and return 0,
2407 * return 1 otherwise. Optionally peer_cert can be set as well
2408 * if it is properly present in cache entry.
2410 * The set callback is called once during the initial handshake
2411 * to enable session resuming after the entire handshake has
2412 * been finished. The set function has the following parameters:
2413 * (void *parameter, const mbedtls_ssl_session *session). The function
2414 * should create a cache entry for future retrieval based on
2415 * the data in the session structure and should keep in mind
2416 * that the mbedtls_ssl_session object presented (and all its referenced
2417 * data) is cleared by the SSL/TLS layer when the connection is
2418 * terminated. It is recommended to add metadata to determine if
2419 * an entry is still valid in the future. Return 0 if
2420 * successfully cached, return 1 otherwise.
2422 * \param conf SSL configuration
2423 * \param p_cache parmater (context) for both callbacks
2424 * \param f_get_cache session get callback
2425 * \param f_set_cache session set callback
2427 void mbedtls_ssl_conf_session_cache(mbedtls_ssl_config
*conf
,
2429 int (*f_get_cache
)(void *, mbedtls_ssl_session
*),
2430 int (*f_set_cache
)(void *, const mbedtls_ssl_session
*));
2431 #endif /* MBEDTLS_SSL_SRV_C */
2433 #if defined(MBEDTLS_SSL_CLI_C)
2435 * \brief Request resumption of session (client-side only)
2436 * Session data is copied from presented session structure.
2438 * \param ssl SSL context
2439 * \param session session context
2441 * \return 0 if successful,
2442 * MBEDTLS_ERR_SSL_ALLOC_FAILED if memory allocation failed,
2443 * MBEDTLS_ERR_SSL_BAD_INPUT_DATA if used server-side or
2444 * arguments are otherwise invalid
2446 * \sa mbedtls_ssl_get_session()
2448 int mbedtls_ssl_set_session(mbedtls_ssl_context
*ssl
, const mbedtls_ssl_session
*session
);
2449 #endif /* MBEDTLS_SSL_CLI_C */
2452 * \brief Load serialized session data into a session structure.
2453 * On client, this can be used for loading saved sessions
2454 * before resuming them with mbedstls_ssl_set_session().
2455 * On server, this can be used for alternative implementations
2456 * of session cache or session tickets.
2458 * \warning If a peer certificate chain is associated with the session,
2459 * the serialized state will only contain the peer's
2460 * end-entity certificate and the result of the chain
2461 * verification (unless verification was disabled), but not
2462 * the rest of the chain.
2464 * \see mbedtls_ssl_session_save()
2465 * \see mbedtls_ssl_set_session()
2467 * \param session The session structure to be populated. It must have been
2468 * initialised with mbedtls_ssl_session_init() but not
2470 * \param buf The buffer holding the serialized session data. It must be a
2471 * readable buffer of at least \p len bytes.
2472 * \param len The size of the serialized data in bytes.
2474 * \return \c 0 if successful.
2475 * \return #MBEDTLS_ERR_SSL_ALLOC_FAILED if memory allocation failed.
2476 * \return #MBEDTLS_ERR_SSL_BAD_INPUT_DATA if input data is invalid.
2477 * \return #MBEDTLS_ERR_SSL_VERSION_MISMATCH if the serialized data
2478 * was generated in a different version or configuration of
2480 * \return Another negative value for other kinds of errors (for
2481 * example, unsupported features in the embedded certificate).
2483 int mbedtls_ssl_session_load(mbedtls_ssl_session
*session
,
2484 const unsigned char *buf
,
2488 * \brief Save session structure as serialized data in a buffer.
2489 * On client, this can be used for saving session data,
2490 * potentially in non-volatile storage, for resuming later.
2491 * On server, this can be used for alternative implementations
2492 * of session cache or session tickets.
2494 * \see mbedtls_ssl_session_load()
2495 * \see mbedtls_ssl_get_session_pointer()
2497 * \param session The session structure to be saved.
2498 * \param buf The buffer to write the serialized data to. It must be a
2499 * writeable buffer of at least \p len bytes, or may be \c
2500 * NULL if \p len is \c 0.
2501 * \param buf_len The number of bytes available for writing in \p buf.
2502 * \param olen The size in bytes of the data that has been or would have
2503 * been written. It must point to a valid \c size_t.
2505 * \note \p olen is updated to the correct value regardless of
2506 * whether \p buf_len was large enough. This makes it possible
2507 * to determine the necessary size by calling this function
2508 * with \p buf set to \c NULL and \p buf_len to \c 0.
2510 * \return \c 0 if successful.
2511 * \return #MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL if \p buf is too small.
2513 int mbedtls_ssl_session_save(const mbedtls_ssl_session
*session
,
2519 * \brief Get a pointer to the current session structure, for example
2522 * \warning Ownership of the session remains with the SSL context, and
2523 * the returned pointer is only guaranteed to be valid until
2524 * the next API call operating on the same \p ssl context.
2526 * \see mbedtls_ssl_session_save()
2528 * \param ssl The SSL context.
2530 * \return A pointer to the current session if successful.
2531 * \return \c NULL if no session is active.
2533 const mbedtls_ssl_session
*mbedtls_ssl_get_session_pointer(const mbedtls_ssl_context
*ssl
);
2536 * \brief Set the list of allowed ciphersuites and the preference
2537 * order. First in the list has the highest preference.
2538 * (Overrides all version-specific lists)
2540 * The ciphersuites array is not copied, and must remain
2541 * valid for the lifetime of the ssl_config.
2543 * Note: The server uses its own preferences
2544 * over the preference of the client unless
2545 * MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE is defined!
2547 * \param conf SSL configuration
2548 * \param ciphersuites 0-terminated list of allowed ciphersuites
2550 void mbedtls_ssl_conf_ciphersuites(mbedtls_ssl_config
*conf
,
2551 const int *ciphersuites
);
2553 #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
2554 #define MBEDTLS_SSL_UNEXPECTED_CID_IGNORE 0
2555 #define MBEDTLS_SSL_UNEXPECTED_CID_FAIL 1
2557 * \brief Specify the length of Connection IDs for incoming
2558 * encrypted DTLS records, as well as the behaviour
2559 * on unexpected CIDs.
2561 * By default, the CID length is set to \c 0,
2562 * and unexpected CIDs are silently ignored.
2564 * \param conf The SSL configuration to modify.
2565 * \param len The length in Bytes of the CID fields in encrypted
2566 * DTLS records using the CID mechanism. This must
2567 * not be larger than #MBEDTLS_SSL_CID_OUT_LEN_MAX.
2568 * \param ignore_other_cids This determines the stack's behaviour when
2569 * receiving a record with an unexpected CID.
2570 * Possible values are:
2571 * - #MBEDTLS_SSL_UNEXPECTED_CID_IGNORE
2572 * In this case, the record is silently ignored.
2573 * - #MBEDTLS_SSL_UNEXPECTED_CID_FAIL
2574 * In this case, the stack fails with the specific
2575 * error code #MBEDTLS_ERR_SSL_UNEXPECTED_CID.
2577 * \note The CID specification allows implementations to either
2578 * use a common length for all incoming connection IDs or
2579 * allow variable-length incoming IDs. Mbed TLS currently
2580 * requires a common length for all connections sharing the
2581 * same SSL configuration; this allows simpler parsing of
2584 * \return \c 0 on success.
2585 * \return #MBEDTLS_ERR_SSL_BAD_INPUT_DATA if \p own_cid_len
2588 int mbedtls_ssl_conf_cid(mbedtls_ssl_config
*conf
, size_t len
,
2589 int ignore_other_cids
);
2590 #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
2593 * \brief Set the list of allowed ciphersuites and the
2594 * preference order for a specific version of the protocol.
2595 * (Only useful on the server side)
2597 * The ciphersuites array is not copied, and must remain
2598 * valid for the lifetime of the ssl_config.
2600 * \param conf SSL configuration
2601 * \param ciphersuites 0-terminated list of allowed ciphersuites
2602 * \param major Major version number (only MBEDTLS_SSL_MAJOR_VERSION_3
2604 * \param minor Minor version number (MBEDTLS_SSL_MINOR_VERSION_0,
2605 * MBEDTLS_SSL_MINOR_VERSION_1 and MBEDTLS_SSL_MINOR_VERSION_2,
2606 * MBEDTLS_SSL_MINOR_VERSION_3 supported)
2608 * \note With DTLS, use MBEDTLS_SSL_MINOR_VERSION_2 for DTLS 1.0
2609 * and MBEDTLS_SSL_MINOR_VERSION_3 for DTLS 1.2
2611 void mbedtls_ssl_conf_ciphersuites_for_version(mbedtls_ssl_config
*conf
,
2612 const int *ciphersuites
,
2613 int major
, int minor
);
2615 #if defined(MBEDTLS_X509_CRT_PARSE_C)
2617 * \brief Set the X.509 security profile used for verification
2619 * \note The restrictions are enforced for all certificates in the
2620 * chain. However, signatures in the handshake are not covered
2621 * by this setting but by \b mbedtls_ssl_conf_sig_hashes().
2623 * \param conf SSL configuration
2624 * \param profile Profile to use
2626 void mbedtls_ssl_conf_cert_profile(mbedtls_ssl_config
*conf
,
2627 const mbedtls_x509_crt_profile
*profile
);
2630 * \brief Set the data required to verify peer certificate
2632 * \note See \c mbedtls_x509_crt_verify() for notes regarding the
2633 * parameters ca_chain (maps to trust_ca for that function)
2636 * \param conf SSL configuration
2637 * \param ca_chain trusted CA chain (meaning all fully trusted top-level CAs)
2638 * \param ca_crl trusted CA CRLs
2640 void mbedtls_ssl_conf_ca_chain(mbedtls_ssl_config
*conf
,
2641 mbedtls_x509_crt
*ca_chain
,
2642 mbedtls_x509_crl
*ca_crl
);
2644 #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
2646 * \brief Set the trusted certificate callback.
2648 * This API allows to register the set of trusted certificates
2649 * through a callback, instead of a linked list as configured
2650 * by mbedtls_ssl_conf_ca_chain().
2652 * This is useful for example in contexts where a large number
2653 * of CAs are used, and the inefficiency of maintaining them
2654 * in a linked list cannot be tolerated. It is also useful when
2655 * the set of trusted CAs needs to be modified frequently.
2657 * See the documentation of `mbedtls_x509_crt_ca_cb_t` for
2660 * \param conf The SSL configuration to register the callback with.
2661 * \param f_ca_cb The trusted certificate callback to use when verifying
2662 * certificate chains.
2663 * \param p_ca_cb The context to be passed to \p f_ca_cb (for example,
2664 * a reference to a trusted CA database).
2666 * \note This API is incompatible with mbedtls_ssl_conf_ca_chain():
2667 * Any call to this function overwrites the values set through
2668 * earlier calls to mbedtls_ssl_conf_ca_chain() or
2669 * mbedtls_ssl_conf_ca_cb().
2671 * \note This API is incompatible with CA indication in
2672 * CertificateRequest messages: A server-side SSL context which
2673 * is bound to an SSL configuration that uses a CA callback
2674 * configured via mbedtls_ssl_conf_ca_cb(), and which requires
2675 * client authentication, will send an empty CA list in the
2676 * corresponding CertificateRequest message.
2678 * \note This API is incompatible with mbedtls_ssl_set_hs_ca_chain():
2679 * If an SSL context is bound to an SSL configuration which uses
2680 * CA callbacks configured via mbedtls_ssl_conf_ca_cb(), then
2681 * calls to mbedtls_ssl_set_hs_ca_chain() have no effect.
2683 * \note The use of this API disables the use of restartable ECC
2684 * during X.509 CRT signature verification (but doesn't affect
2687 * \warning This API is incompatible with the use of CRLs. Any call to
2688 * mbedtls_ssl_conf_ca_cb() unsets CRLs configured through
2689 * earlier calls to mbedtls_ssl_conf_ca_chain().
2691 * \warning In multi-threaded environments, the callback \p f_ca_cb
2692 * must be thread-safe, and it is the user's responsibility
2693 * to guarantee this (for example through a mutex
2694 * contained in the callback context pointed to by \p p_ca_cb).
2696 void mbedtls_ssl_conf_ca_cb(mbedtls_ssl_config
*conf
,
2697 mbedtls_x509_crt_ca_cb_t f_ca_cb
,
2699 #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2702 * \brief Set own certificate chain and private key
2704 * \note own_cert should contain in order from the bottom up your
2705 * certificate chain. The top certificate (self-signed)
2708 * \note On server, this function can be called multiple times to
2709 * provision more than one cert/key pair (eg one ECDSA, one
2710 * RSA with SHA-256, one RSA with SHA-1). An adequate
2711 * certificate will be selected according to the client's
2712 * advertised capabilities. In case multiple certificates are
2713 * adequate, preference is given to the one set by the first
2714 * call to this function, then second, etc.
2716 * \note On client, only the first call has any effect. That is,
2717 * only one client certificate can be provisioned. The
2718 * server's preferences in its CertficateRequest message will
2719 * be ignored and our only cert will be sent regardless of
2720 * whether it matches those preferences - the server can then
2721 * decide what it wants to do with it.
2723 * \note The provided \p pk_key needs to match the public key in the
2724 * first certificate in \p own_cert, or all handshakes using
2725 * that certificate will fail. It is your responsibility
2726 * to ensure that; this function will not perform any check.
2727 * You may use mbedtls_pk_check_pair() in order to perform
2728 * this check yourself, but be aware that this function can
2729 * be computationally expensive on some key types.
2731 * \param conf SSL configuration
2732 * \param own_cert own public certificate chain
2733 * \param pk_key own private key
2735 * \return 0 on success or MBEDTLS_ERR_SSL_ALLOC_FAILED
2737 int mbedtls_ssl_conf_own_cert(mbedtls_ssl_config
*conf
,
2738 mbedtls_x509_crt
*own_cert
,
2739 mbedtls_pk_context
*pk_key
);
2740 #endif /* MBEDTLS_X509_CRT_PARSE_C */
2742 #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
2744 * \brief Configure a pre-shared key (PSK) and identity
2745 * to be used in PSK-based ciphersuites.
2747 * \note This is mainly useful for clients. Servers will usually
2748 * want to use \c mbedtls_ssl_conf_psk_cb() instead.
2750 * \note A PSK set by \c mbedtls_ssl_set_hs_psk() in the PSK callback
2751 * takes precedence over a PSK configured by this function.
2753 * \warning Currently, clients can only register a single pre-shared key.
2754 * Calling this function or mbedtls_ssl_conf_psk_opaque() more
2755 * than once will overwrite values configured in previous calls.
2756 * Support for setting multiple PSKs on clients and selecting
2757 * one based on the identity hint is not a planned feature,
2758 * but feedback is welcomed.
2760 * \param conf The SSL configuration to register the PSK with.
2761 * \param psk The pointer to the pre-shared key to use.
2762 * \param psk_len The length of the pre-shared key in bytes.
2763 * \param psk_identity The pointer to the pre-shared key identity.
2764 * \param psk_identity_len The length of the pre-shared key identity
2767 * \note The PSK and its identity are copied internally and
2768 * hence need not be preserved by the caller for the lifetime
2769 * of the SSL configuration.
2771 * \return \c 0 if successful.
2772 * \return An \c MBEDTLS_ERR_SSL_XXX error code on failure.
2774 int mbedtls_ssl_conf_psk(mbedtls_ssl_config
*conf
,
2775 const unsigned char *psk
, size_t psk_len
,
2776 const unsigned char *psk_identity
, size_t psk_identity_len
);
2778 #if defined(MBEDTLS_USE_PSA_CRYPTO)
2780 * \brief Configure an opaque pre-shared key (PSK) and identity
2781 * to be used in PSK-based ciphersuites.
2783 * \note This is mainly useful for clients. Servers will usually
2784 * want to use \c mbedtls_ssl_conf_psk_cb() instead.
2786 * \note An opaque PSK set by \c mbedtls_ssl_set_hs_psk_opaque() in
2787 * the PSK callback takes precedence over an opaque PSK
2788 * configured by this function.
2790 * \warning Currently, clients can only register a single pre-shared key.
2791 * Calling this function or mbedtls_ssl_conf_psk() more than
2792 * once will overwrite values configured in previous calls.
2793 * Support for setting multiple PSKs on clients and selecting
2794 * one based on the identity hint is not a planned feature,
2795 * but feedback is welcomed.
2797 * \param conf The SSL configuration to register the PSK with.
2798 * \param psk The identifier of the key slot holding the PSK.
2799 * Until \p conf is destroyed or this function is successfully
2800 * called again, the key slot \p psk must be populated with a
2801 * key of type PSA_ALG_CATEGORY_KEY_DERIVATION whose policy
2802 * allows its use for the key derivation algorithm applied
2804 * \param psk_identity The pointer to the pre-shared key identity.
2805 * \param psk_identity_len The length of the pre-shared key identity
2808 * \note The PSK identity hint is copied internally and hence need
2809 * not be preserved by the caller for the lifetime of the
2810 * SSL configuration.
2812 * \return \c 0 if successful.
2813 * \return An \c MBEDTLS_ERR_SSL_XXX error code on failure.
2815 int mbedtls_ssl_conf_psk_opaque(mbedtls_ssl_config
*conf
,
2817 const unsigned char *psk_identity
,
2818 size_t psk_identity_len
);
2819 #endif /* MBEDTLS_USE_PSA_CRYPTO */
2822 * \brief Set the pre-shared Key (PSK) for the current handshake.
2824 * \note This should only be called inside the PSK callback,
2825 * i.e. the function passed to \c mbedtls_ssl_conf_psk_cb().
2827 * \note A PSK set by this function takes precedence over a PSK
2828 * configured by \c mbedtls_ssl_conf_psk().
2830 * \param ssl The SSL context to configure a PSK for.
2831 * \param psk The pointer to the pre-shared key.
2832 * \param psk_len The length of the pre-shared key in bytes.
2834 * \return \c 0 if successful.
2835 * \return An \c MBEDTLS_ERR_SSL_XXX error code on failure.
2837 int mbedtls_ssl_set_hs_psk(mbedtls_ssl_context
*ssl
,
2838 const unsigned char *psk
, size_t psk_len
);
2840 #if defined(MBEDTLS_USE_PSA_CRYPTO)
2842 * \brief Set an opaque pre-shared Key (PSK) for the current handshake.
2844 * \note This should only be called inside the PSK callback,
2845 * i.e. the function passed to \c mbedtls_ssl_conf_psk_cb().
2847 * \note An opaque PSK set by this function takes precedence over an
2848 * opaque PSK configured by \c mbedtls_ssl_conf_psk_opaque().
2850 * \param ssl The SSL context to configure a PSK for.
2851 * \param psk The identifier of the key slot holding the PSK.
2852 * For the duration of the current handshake, the key slot
2853 * must be populated with a key of type
2854 * PSA_ALG_CATEGORY_KEY_DERIVATION whose policy allows its
2855 * use for the key derivation algorithm
2856 * applied in the handshake.
2858 * \return \c 0 if successful.
2859 * \return An \c MBEDTLS_ERR_SSL_XXX error code on failure.
2861 int mbedtls_ssl_set_hs_psk_opaque(mbedtls_ssl_context
*ssl
,
2863 #endif /* MBEDTLS_USE_PSA_CRYPTO */
2866 * \brief Set the PSK callback (server-side only).
2868 * If set, the PSK callback is called for each
2869 * handshake where a PSK-based ciphersuite was negotiated.
2870 * The caller provides the identity received and wants to
2871 * receive the actual PSK data and length.
2873 * The callback has the following parameters:
2874 * - \c void*: The opaque pointer \p p_psk.
2875 * - \c mbedtls_ssl_context*: The SSL context to which
2876 * the operation applies.
2877 * - \c const unsigned char*: The PSK identity
2878 * selected by the client.
2879 * - \c size_t: The length of the PSK identity
2880 * selected by the client.
2882 * If a valid PSK identity is found, the callback should use
2883 * \c mbedtls_ssl_set_hs_psk() or
2884 * \c mbedtls_ssl_set_hs_psk_opaque()
2885 * on the SSL context to set the correct PSK and return \c 0.
2886 * Any other return value will result in a denied PSK identity.
2888 * \note A dynamic PSK (i.e. set by the PSK callback) takes
2889 * precedence over a static PSK (i.e. set by
2890 * \c mbedtls_ssl_conf_psk() or
2891 * \c mbedtls_ssl_conf_psk_opaque()).
2892 * This means that if you set a PSK callback using this
2893 * function, you don't need to set a PSK using
2894 * \c mbedtls_ssl_conf_psk() or
2895 * \c mbedtls_ssl_conf_psk_opaque()).
2897 * \param conf The SSL configuration to register the callback with.
2898 * \param f_psk The callback for selecting and setting the PSK based
2899 * in the PSK identity chosen by the client.
2900 * \param p_psk A pointer to an opaque structure to be passed to
2901 * the callback, for example a PSK store.
2903 void mbedtls_ssl_conf_psk_cb(mbedtls_ssl_config
*conf
,
2904 int (*f_psk
)(void *, mbedtls_ssl_context
*, const unsigned char *,
2907 #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
2909 #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
2911 #if !defined(MBEDTLS_DEPRECATED_REMOVED)
2913 #if defined(MBEDTLS_DEPRECATED_WARNING)
2914 #define MBEDTLS_DEPRECATED __attribute__((deprecated))
2916 #define MBEDTLS_DEPRECATED
2920 * \brief Set the Diffie-Hellman public P and G values,
2921 * read as hexadecimal strings (server-side only)
2922 * (Default values: MBEDTLS_DHM_RFC3526_MODP_2048_[PG])
2924 * \param conf SSL configuration
2925 * \param dhm_P Diffie-Hellman-Merkle modulus
2926 * \param dhm_G Diffie-Hellman-Merkle generator
2928 * \deprecated Superseded by \c mbedtls_ssl_conf_dh_param_bin.
2930 * \return 0 if successful
2932 MBEDTLS_DEPRECATED
int mbedtls_ssl_conf_dh_param(mbedtls_ssl_config
*conf
,
2936 #endif /* MBEDTLS_DEPRECATED_REMOVED */
2939 * \brief Set the Diffie-Hellman public P and G values
2940 * from big-endian binary presentations.
2941 * (Default values: MBEDTLS_DHM_RFC3526_MODP_2048_[PG]_BIN)
2943 * \param conf SSL configuration
2944 * \param dhm_P Diffie-Hellman-Merkle modulus in big-endian binary form
2945 * \param P_len Length of DHM modulus
2946 * \param dhm_G Diffie-Hellman-Merkle generator in big-endian binary form
2947 * \param G_len Length of DHM generator
2949 * \return 0 if successful
2951 int mbedtls_ssl_conf_dh_param_bin(mbedtls_ssl_config
*conf
,
2952 const unsigned char *dhm_P
, size_t P_len
,
2953 const unsigned char *dhm_G
, size_t G_len
);
2956 * \brief Set the Diffie-Hellman public P and G values,
2957 * read from existing context (server-side only)
2959 * \param conf SSL configuration
2960 * \param dhm_ctx Diffie-Hellman-Merkle context
2962 * \return 0 if successful
2964 int mbedtls_ssl_conf_dh_param_ctx(mbedtls_ssl_config
*conf
, mbedtls_dhm_context
*dhm_ctx
);
2965 #endif /* MBEDTLS_DHM_C && defined(MBEDTLS_SSL_SRV_C) */
2967 #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2969 * \brief Set the minimum length for Diffie-Hellman parameters.
2970 * (Client-side only.)
2971 * (Default: 1024 bits.)
2973 * \param conf SSL configuration
2974 * \param bitlen Minimum bit length of the DHM prime
2976 void mbedtls_ssl_conf_dhm_min_bitlen(mbedtls_ssl_config
*conf
,
2977 unsigned int bitlen
);
2978 #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2980 #if defined(MBEDTLS_ECP_C)
2982 * \brief Set the allowed curves in order of preference.
2983 * (Default: all defined curves.)
2985 * On server: this only affects selection of the ECDHE curve;
2986 * the curves used for ECDH and ECDSA are determined by the
2987 * list of available certificates instead.
2989 * On client: this affects the list of curves offered for any
2990 * use. The server can override our preference order.
2992 * Both sides: limits the set of curves accepted for use in
2993 * ECDHE and in the peer's end-entity certificate.
2995 * \note This has no influence on which curves are allowed inside the
2996 * certificate chains, see \c mbedtls_ssl_conf_cert_profile()
2997 * for that. For the end-entity certificate however, the key
2998 * will be accepted only if it is allowed both by this list
2999 * and by the cert profile.
3001 * \note This list should be ordered by decreasing preference
3002 * (preferred curve first).
3004 * \param conf SSL configuration
3005 * \param curves Ordered list of allowed curves,
3006 * terminated by MBEDTLS_ECP_DP_NONE.
3008 void mbedtls_ssl_conf_curves(mbedtls_ssl_config
*conf
,
3009 const mbedtls_ecp_group_id
*curves
);
3010 #endif /* MBEDTLS_ECP_C */
3012 #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
3014 * \brief Set the allowed hashes for signatures during the handshake.
3015 * (Default: all available hashes except MD5.)
3017 * \note This only affects which hashes are offered and can be used
3018 * for signatures during the handshake. Hashes for message
3019 * authentication and the TLS PRF are controlled by the
3020 * ciphersuite, see \c mbedtls_ssl_conf_ciphersuites(). Hashes
3021 * used for certificate signature are controlled by the
3022 * verification profile, see \c mbedtls_ssl_conf_cert_profile().
3024 * \note This list should be ordered by decreasing preference
3025 * (preferred hash first).
3027 * \param conf SSL configuration
3028 * \param hashes Ordered list of allowed signature hashes,
3029 * terminated by \c MBEDTLS_MD_NONE.
3031 void mbedtls_ssl_conf_sig_hashes(mbedtls_ssl_config
*conf
,
3033 #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
3035 #if defined(MBEDTLS_X509_CRT_PARSE_C)
3037 * \brief Set or reset the hostname to check against the received
3038 * server certificate. It sets the ServerName TLS extension,
3039 * too, if that extension is enabled. (client-side only)
3041 * \param ssl SSL context
3042 * \param hostname the server hostname, may be NULL to clear hostname
3044 * \note Maximum hostname length MBEDTLS_SSL_MAX_HOST_NAME_LEN.
3046 * \return 0 if successful, MBEDTLS_ERR_SSL_ALLOC_FAILED on
3047 * allocation failure, MBEDTLS_ERR_SSL_BAD_INPUT_DATA on
3048 * too long input hostname.
3050 * Hostname set to the one provided on success (cleared
3051 * when NULL). On allocation failure hostname is cleared.
3052 * On too long input failure, old hostname is unchanged.
3054 int mbedtls_ssl_set_hostname(mbedtls_ssl_context
*ssl
, const char *hostname
);
3055 #endif /* MBEDTLS_X509_CRT_PARSE_C */
3057 #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3059 * \brief Set own certificate and key for the current handshake
3061 * \note Same as \c mbedtls_ssl_conf_own_cert() but for use within
3064 * \param ssl SSL context
3065 * \param own_cert own public certificate chain
3066 * \param pk_key own private key
3068 * \return 0 on success or MBEDTLS_ERR_SSL_ALLOC_FAILED
3070 int mbedtls_ssl_set_hs_own_cert(mbedtls_ssl_context
*ssl
,
3071 mbedtls_x509_crt
*own_cert
,
3072 mbedtls_pk_context
*pk_key
);
3075 * \brief Set the data required to verify peer certificate for the
3078 * \note Same as \c mbedtls_ssl_conf_ca_chain() but for use within
3081 * \param ssl SSL context
3082 * \param ca_chain trusted CA chain (meaning all fully trusted top-level CAs)
3083 * \param ca_crl trusted CA CRLs
3085 void mbedtls_ssl_set_hs_ca_chain(mbedtls_ssl_context
*ssl
,
3086 mbedtls_x509_crt
*ca_chain
,
3087 mbedtls_x509_crl
*ca_crl
);
3090 * \brief Set authmode for the current handshake.
3092 * \note Same as \c mbedtls_ssl_conf_authmode() but for use within
3095 * \param ssl SSL context
3096 * \param authmode MBEDTLS_SSL_VERIFY_NONE, MBEDTLS_SSL_VERIFY_OPTIONAL or
3097 * MBEDTLS_SSL_VERIFY_REQUIRED
3099 void mbedtls_ssl_set_hs_authmode(mbedtls_ssl_context
*ssl
,
3103 * \brief Set server side ServerName TLS extension callback
3104 * (optional, server-side only).
3106 * If set, the ServerName callback is called whenever the
3107 * server receives a ServerName TLS extension from the client
3108 * during a handshake. The ServerName callback has the
3109 * following parameters: (void *parameter, mbedtls_ssl_context *ssl,
3110 * const unsigned char *hostname, size_t len). If a suitable
3111 * certificate is found, the callback must set the
3112 * certificate(s) and key(s) to use with \c
3113 * mbedtls_ssl_set_hs_own_cert() (can be called repeatedly),
3114 * and may optionally adjust the CA and associated CRL with \c
3115 * mbedtls_ssl_set_hs_ca_chain() as well as the client
3116 * authentication mode with \c mbedtls_ssl_set_hs_authmode(),
3117 * then must return 0. If no matching name is found, the
3118 * callback must either set a default cert, or
3119 * return non-zero to abort the handshake at this point.
3121 * \param conf SSL configuration
3122 * \param f_sni verification function
3123 * \param p_sni verification parameter
3125 void mbedtls_ssl_conf_sni(mbedtls_ssl_config
*conf
,
3126 int (*f_sni
)(void *, mbedtls_ssl_context
*, const unsigned char *,
3129 #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
3131 #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
3133 * \brief Set the EC J-PAKE password for current handshake.
3135 * \note An internal copy is made, and destroyed as soon as the
3136 * handshake is completed, or when the SSL context is reset or
3139 * \note The SSL context needs to be already set up. The right place
3140 * to call this function is between \c mbedtls_ssl_setup() or
3141 * \c mbedtls_ssl_reset() and \c mbedtls_ssl_handshake().
3143 * \param ssl SSL context
3144 * \param pw EC J-PAKE password (pre-shared secret)
3145 * \param pw_len length of pw in bytes
3147 * \return 0 on success, or a negative error code.
3149 int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context
*ssl
,
3150 const unsigned char *pw
,
3152 #endif /*MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
3154 #if defined(MBEDTLS_SSL_ALPN)
3156 * \brief Set the supported Application Layer Protocols.
3158 * \param conf SSL configuration
3159 * \param protos Pointer to a NULL-terminated list of supported protocols,
3160 * in decreasing preference order. The pointer to the list is
3161 * recorded by the library for later reference as required, so
3162 * the lifetime of the table must be atleast as long as the
3163 * lifetime of the SSL configuration structure.
3165 * \return 0 on success, or MBEDTLS_ERR_SSL_BAD_INPUT_DATA.
3167 int mbedtls_ssl_conf_alpn_protocols(mbedtls_ssl_config
*conf
, const char **protos
);
3170 * \brief Get the name of the negotiated Application Layer Protocol.
3171 * This function should be called after the handshake is
3174 * \param ssl SSL context
3176 * \return Protcol name, or NULL if no protocol was negotiated.
3178 const char *mbedtls_ssl_get_alpn_protocol(const mbedtls_ssl_context
*ssl
);
3179 #endif /* MBEDTLS_SSL_ALPN */
3181 #if defined(MBEDTLS_SSL_DTLS_SRTP)
3182 #if defined(MBEDTLS_DEBUG_C)
3183 static inline const char *mbedtls_ssl_get_srtp_profile_as_string(mbedtls_ssl_srtp_profile profile
) {
3185 case MBEDTLS_TLS_SRTP_AES128_CM_HMAC_SHA1_80
:
3186 return ("MBEDTLS_TLS_SRTP_AES128_CM_HMAC_SHA1_80");
3187 case MBEDTLS_TLS_SRTP_AES128_CM_HMAC_SHA1_32
:
3188 return ("MBEDTLS_TLS_SRTP_AES128_CM_HMAC_SHA1_32");
3189 case MBEDTLS_TLS_SRTP_NULL_HMAC_SHA1_80
:
3190 return ("MBEDTLS_TLS_SRTP_NULL_HMAC_SHA1_80");
3191 case MBEDTLS_TLS_SRTP_NULL_HMAC_SHA1_32
:
3192 return ("MBEDTLS_TLS_SRTP_NULL_HMAC_SHA1_32");
3198 #endif /* MBEDTLS_DEBUG_C */
3200 * \brief Manage support for mki(master key id) value
3201 * in use_srtp extension.
3202 * MKI is an optional part of SRTP used for key management
3203 * and re-keying. See RFC3711 section 3.1 for details.
3204 * The default value is
3205 * #MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED.
3207 * \param conf The SSL configuration to manage mki support.
3208 * \param support_mki_value Enable or disable mki usage. Values are
3209 * #MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED
3210 * or #MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED.
3212 void mbedtls_ssl_conf_srtp_mki_value_supported(mbedtls_ssl_config
*conf
,
3213 int support_mki_value
);
3216 * \brief Set the supported DTLS-SRTP protection profiles.
3218 * \param conf SSL configuration
3219 * \param profiles Pointer to a List of MBEDTLS_TLS_SRTP_UNSET terminated
3220 * supported protection profiles
3221 * in decreasing preference order.
3222 * The pointer to the list is recorded by the library
3223 * for later reference as required, so the lifetime
3224 * of the table must be at least as long as the lifetime
3225 * of the SSL configuration structure.
3226 * The list must not hold more than
3227 * MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH elements
3228 * (excluding the terminating MBEDTLS_TLS_SRTP_UNSET).
3230 * \return 0 on success
3231 * \return #MBEDTLS_ERR_SSL_BAD_INPUT_DATA when the list of
3232 * protection profiles is incorrect.
3234 int mbedtls_ssl_conf_dtls_srtp_protection_profiles
3235 (mbedtls_ssl_config
*conf
,
3236 const mbedtls_ssl_srtp_profile
*profiles
);
3239 * \brief Set the mki_value for the current DTLS-SRTP session.
3241 * \param ssl SSL context to use.
3242 * \param mki_value The MKI value to set.
3243 * \param mki_len The length of the MKI value.
3245 * \note This function is relevant on client side only.
3246 * The server discovers the mki value during handshake.
3247 * A mki value set on server side using this function
3250 * \return 0 on success
3251 * \return #MBEDTLS_ERR_SSL_BAD_INPUT_DATA
3252 * \return #MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE
3254 int mbedtls_ssl_dtls_srtp_set_mki_value(mbedtls_ssl_context
*ssl
,
3255 unsigned char *mki_value
,
3258 * \brief Get the negotiated DTLS-SRTP informations:
3259 * Protection profile and MKI value.
3261 * \warning This function must be called after the handshake is
3262 * completed. The value returned by this function must
3263 * not be trusted or acted upon before the handshake completes.
3265 * \param ssl The SSL context to query.
3266 * \param dtls_srtp_info The negotiated DTLS-SRTP informations:
3267 * - Protection profile in use.
3268 * A direct mapping of the iana defined value for protection
3269 * profile on an uint16_t.
3270 http://www.iana.org/assignments/srtp-protection/srtp-protection.xhtml
3271 * #MBEDTLS_TLS_SRTP_UNSET if the use of SRTP was not negotiated
3272 * or peer's Hello packet was not parsed yet.
3273 * - mki size and value( if size is > 0 ).
3275 void mbedtls_ssl_get_dtls_srtp_negotiation_result(const mbedtls_ssl_context
*ssl
,
3276 mbedtls_dtls_srtp_info
*dtls_srtp_info
);
3277 #endif /* MBEDTLS_SSL_DTLS_SRTP */
3280 * \brief Set the maximum supported version sent from the client side
3281 * and/or accepted at the server side
3282 * (Default: MBEDTLS_SSL_MAX_MAJOR_VERSION, MBEDTLS_SSL_MAX_MINOR_VERSION)
3284 * \note This ignores ciphersuites from higher versions.
3286 * \note With DTLS, use MBEDTLS_SSL_MINOR_VERSION_2 for DTLS 1.0 and
3287 * MBEDTLS_SSL_MINOR_VERSION_3 for DTLS 1.2
3289 * \param conf SSL configuration
3290 * \param major Major version number (only MBEDTLS_SSL_MAJOR_VERSION_3 supported)
3291 * \param minor Minor version number (MBEDTLS_SSL_MINOR_VERSION_0,
3292 * MBEDTLS_SSL_MINOR_VERSION_1 and MBEDTLS_SSL_MINOR_VERSION_2,
3293 * MBEDTLS_SSL_MINOR_VERSION_3 supported)
3295 void mbedtls_ssl_conf_max_version(mbedtls_ssl_config
*conf
, int major
, int minor
);
3298 * \brief Set the minimum accepted SSL/TLS protocol version
3299 * (Default: TLS 1.0)
3301 * \note Input outside of the SSL_MAX_XXXXX_VERSION and
3302 * SSL_MIN_XXXXX_VERSION range is ignored.
3304 * \note MBEDTLS_SSL_MINOR_VERSION_0 (SSL v3) should be avoided.
3306 * \note With DTLS, use MBEDTLS_SSL_MINOR_VERSION_2 for DTLS 1.0 and
3307 * MBEDTLS_SSL_MINOR_VERSION_3 for DTLS 1.2
3309 * \param conf SSL configuration
3310 * \param major Major version number (only MBEDTLS_SSL_MAJOR_VERSION_3 supported)
3311 * \param minor Minor version number (MBEDTLS_SSL_MINOR_VERSION_0,
3312 * MBEDTLS_SSL_MINOR_VERSION_1 and MBEDTLS_SSL_MINOR_VERSION_2,
3313 * MBEDTLS_SSL_MINOR_VERSION_3 supported)
3315 void mbedtls_ssl_conf_min_version(mbedtls_ssl_config
*conf
, int major
, int minor
);
3317 #if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
3319 * \brief Set the fallback flag (client-side only).
3320 * (Default: MBEDTLS_SSL_IS_NOT_FALLBACK).
3322 * \note Set to MBEDTLS_SSL_IS_FALLBACK when preparing a fallback
3323 * connection, that is a connection with max_version set to a
3324 * lower value than the value you're willing to use. Such
3325 * fallback connections are not recommended but are sometimes
3326 * necessary to interoperate with buggy (version-intolerant)
3329 * \warning You should NOT set this to MBEDTLS_SSL_IS_FALLBACK for
3330 * non-fallback connections! This would appear to work for a
3331 * while, then cause failures when the server is upgraded to
3332 * support a newer TLS version.
3334 * \param conf SSL configuration
3335 * \param fallback MBEDTLS_SSL_IS_NOT_FALLBACK or MBEDTLS_SSL_IS_FALLBACK
3337 void mbedtls_ssl_conf_fallback(mbedtls_ssl_config
*conf
, char fallback
);
3338 #endif /* MBEDTLS_SSL_FALLBACK_SCSV && MBEDTLS_SSL_CLI_C */
3340 #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
3342 * \brief Enable or disable Encrypt-then-MAC
3343 * (Default: MBEDTLS_SSL_ETM_ENABLED)
3345 * \note This should always be enabled, it is a security
3346 * improvement, and should not cause any interoperability
3347 * issue (used only if the peer supports it too).
3349 * \param conf SSL configuration
3350 * \param etm MBEDTLS_SSL_ETM_ENABLED or MBEDTLS_SSL_ETM_DISABLED
3352 void mbedtls_ssl_conf_encrypt_then_mac(mbedtls_ssl_config
*conf
, char etm
);
3353 #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
3355 #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
3357 * \brief Enable or disable Extended Master Secret negotiation.
3358 * (Default: MBEDTLS_SSL_EXTENDED_MS_ENABLED)
3360 * \note This should always be enabled, it is a security fix to the
3361 * protocol, and should not cause any interoperability issue
3362 * (used only if the peer supports it too).
3364 * \param conf SSL configuration
3365 * \param ems MBEDTLS_SSL_EXTENDED_MS_ENABLED or MBEDTLS_SSL_EXTENDED_MS_DISABLED
3367 void mbedtls_ssl_conf_extended_master_secret(mbedtls_ssl_config
*conf
, char ems
);
3368 #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
3370 #if defined(MBEDTLS_ARC4_C)
3372 * \brief Disable or enable support for RC4
3373 * (Default: MBEDTLS_SSL_ARC4_DISABLED)
3375 * \warning Use of RC4 in DTLS/TLS has been prohibited by RFC 7465
3376 * for security reasons. Use at your own risk.
3378 * \note This function is deprecated and will be removed in
3379 * a future version of the library.
3380 * RC4 is disabled by default at compile time and needs to be
3381 * actively enabled for use with legacy systems.
3383 * \param conf SSL configuration
3384 * \param arc4 MBEDTLS_SSL_ARC4_ENABLED or MBEDTLS_SSL_ARC4_DISABLED
3386 void mbedtls_ssl_conf_arc4_support(mbedtls_ssl_config
*conf
, char arc4
);
3387 #endif /* MBEDTLS_ARC4_C */
3389 #if defined(MBEDTLS_SSL_SRV_C)
3391 * \brief Whether to send a list of acceptable CAs in
3392 * CertificateRequest messages.
3393 * (Default: do send)
3395 * \param conf SSL configuration
3396 * \param cert_req_ca_list MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED or
3397 * MBEDTLS_SSL_CERT_REQ_CA_LIST_DISABLED
3399 void mbedtls_ssl_conf_cert_req_ca_list(mbedtls_ssl_config
*conf
,
3400 char cert_req_ca_list
);
3401 #endif /* MBEDTLS_SSL_SRV_C */
3403 #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3405 * \brief Set the maximum fragment length to emit and/or negotiate.
3406 * (Typical: the smaller of #MBEDTLS_SSL_IN_CONTENT_LEN and
3407 * #MBEDTLS_SSL_OUT_CONTENT_LEN, usually `2^14` bytes)
3408 * (Server: set maximum fragment length to emit,
3409 * usually negotiated by the client during handshake)
3410 * (Client: set maximum fragment length to emit *and*
3411 * negotiate with the server during handshake)
3412 * (Default: #MBEDTLS_SSL_MAX_FRAG_LEN_NONE)
3414 * \note On the client side, the maximum fragment length extension
3415 * *will not* be used, unless the maximum fragment length has
3416 * been set via this function to a value different than
3417 * #MBEDTLS_SSL_MAX_FRAG_LEN_NONE.
3419 * \note With TLS, this currently only affects ApplicationData (sent
3420 * with \c mbedtls_ssl_read()), not handshake messages.
3421 * With DTLS, this affects both ApplicationData and handshake.
3423 * \note This sets the maximum length for a record's payload,
3424 * excluding record overhead that will be added to it, see
3425 * \c mbedtls_ssl_get_record_expansion().
3427 * \note For DTLS, it is also possible to set a limit for the total
3428 * size of daragrams passed to the transport layer, including
3429 * record overhead, see \c mbedtls_ssl_set_mtu().
3431 * \param conf SSL configuration
3432 * \param mfl_code Code for maximum fragment length (allowed values:
3433 * MBEDTLS_SSL_MAX_FRAG_LEN_512, MBEDTLS_SSL_MAX_FRAG_LEN_1024,
3434 * MBEDTLS_SSL_MAX_FRAG_LEN_2048, MBEDTLS_SSL_MAX_FRAG_LEN_4096)
3436 * \return 0 if successful or MBEDTLS_ERR_SSL_BAD_INPUT_DATA
3438 int mbedtls_ssl_conf_max_frag_len(mbedtls_ssl_config
*conf
, unsigned char mfl_code
);
3439 #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3441 #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
3443 * \brief Activate negotiation of truncated HMAC
3444 * (Default: MBEDTLS_SSL_TRUNC_HMAC_DISABLED)
3446 * \param conf SSL configuration
3447 * \param truncate Enable or disable (MBEDTLS_SSL_TRUNC_HMAC_ENABLED or
3448 * MBEDTLS_SSL_TRUNC_HMAC_DISABLED)
3450 void mbedtls_ssl_conf_truncated_hmac(mbedtls_ssl_config
*conf
, int truncate
);
3451 #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
3453 #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
3455 * \brief Enable / Disable 1/n-1 record splitting
3456 * (Default: MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED)
3458 * \note Only affects SSLv3 and TLS 1.0, not higher versions.
3459 * Does not affect non-CBC ciphersuites in any version.
3461 * \param conf SSL configuration
3462 * \param split MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED or
3463 * MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED
3465 void mbedtls_ssl_conf_cbc_record_splitting(mbedtls_ssl_config
*conf
, char split
);
3466 #endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
3468 #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
3470 * \brief Enable / Disable session tickets (client only).
3471 * (Default: MBEDTLS_SSL_SESSION_TICKETS_ENABLED.)
3473 * \note On server, use \c mbedtls_ssl_conf_session_tickets_cb().
3475 * \param conf SSL configuration
3476 * \param use_tickets Enable or disable (MBEDTLS_SSL_SESSION_TICKETS_ENABLED or
3477 * MBEDTLS_SSL_SESSION_TICKETS_DISABLED)
3479 void mbedtls_ssl_conf_session_tickets(mbedtls_ssl_config
*conf
, int use_tickets
);
3480 #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
3482 #if defined(MBEDTLS_SSL_RENEGOTIATION)
3484 * \brief Enable / Disable renegotiation support for connection when
3486 * (Default: MBEDTLS_SSL_RENEGOTIATION_DISABLED)
3488 * \warning It is recommended to always disable renegotation unless you
3489 * know you need it and you know what you're doing. In the
3490 * past, there have been several issues associated with
3491 * renegotiation or a poor understanding of its properties.
3493 * \note Server-side, enabling renegotiation also makes the server
3494 * susceptible to a resource DoS by a malicious client.
3496 * \param conf SSL configuration
3497 * \param renegotiation Enable or disable (MBEDTLS_SSL_RENEGOTIATION_ENABLED or
3498 * MBEDTLS_SSL_RENEGOTIATION_DISABLED)
3500 void mbedtls_ssl_conf_renegotiation(mbedtls_ssl_config
*conf
, int renegotiation
);
3501 #endif /* MBEDTLS_SSL_RENEGOTIATION */
3504 * \brief Prevent or allow legacy renegotiation.
3505 * (Default: MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION)
3507 * MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION allows connections to
3508 * be established even if the peer does not support
3509 * secure renegotiation, but does not allow renegotiation
3510 * to take place if not secure.
3511 * (Interoperable and secure option)
3513 * MBEDTLS_SSL_LEGACY_ALLOW_RENEGOTIATION allows renegotiations
3514 * with non-upgraded peers. Allowing legacy renegotiation
3515 * makes the connection vulnerable to specific man in the
3516 * middle attacks. (See RFC 5746)
3517 * (Most interoperable and least secure option)
3519 * MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE breaks off connections
3520 * if peer does not support secure renegotiation. Results
3521 * in interoperability issues with non-upgraded peers
3522 * that do not support renegotiation altogether.
3523 * (Most secure option, interoperability issues)
3525 * \param conf SSL configuration
3526 * \param allow_legacy Prevent or allow (SSL_NO_LEGACY_RENEGOTIATION,
3527 * SSL_ALLOW_LEGACY_RENEGOTIATION or
3528 * MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE)
3530 void mbedtls_ssl_conf_legacy_renegotiation(mbedtls_ssl_config
*conf
, int allow_legacy
);
3532 #if defined(MBEDTLS_SSL_RENEGOTIATION)
3534 * \brief Enforce renegotiation requests.
3535 * (Default: enforced, max_records = 16)
3537 * When we request a renegotiation, the peer can comply or
3538 * ignore the request. This function allows us to decide
3539 * whether to enforce our renegotiation requests by closing
3540 * the connection if the peer doesn't comply.
3542 * However, records could already be in transit from the peer
3543 * when the request is emitted. In order to increase
3544 * reliability, we can accept a number of records before the
3545 * expected handshake records.
3547 * The optimal value is highly dependent on the specific usage
3550 * \note With DTLS and server-initiated renegotiation, the
3551 * HelloRequest is retransmited every time mbedtls_ssl_read() times
3552 * out or receives Application Data, until:
3553 * - max_records records have beens seen, if it is >= 0, or
3554 * - the number of retransmits that would happen during an
3555 * actual handshake has been reached.
3556 * Please remember the request might be lost a few times
3557 * if you consider setting max_records to a really low value.
3559 * \warning On client, the grace period can only happen during
3560 * mbedtls_ssl_read(), as opposed to mbedtls_ssl_write() and mbedtls_ssl_renegotiate()
3561 * which always behave as if max_record was 0. The reason is,
3562 * if we receive application data from the server, we need a
3563 * place to write it, which only happens during mbedtls_ssl_read().
3565 * \param conf SSL configuration
3566 * \param max_records Use MBEDTLS_SSL_RENEGOTIATION_NOT_ENFORCED if you don't want to
3567 * enforce renegotiation, or a non-negative value to enforce
3568 * it but allow for a grace period of max_records records.
3570 void mbedtls_ssl_conf_renegotiation_enforced(mbedtls_ssl_config
*conf
, int max_records
);
3573 * \brief Set record counter threshold for periodic renegotiation.
3574 * (Default: 2^48 - 1)
3576 * Renegotiation is automatically triggered when a record
3577 * counter (outgoing or incoming) crosses the defined
3578 * threshold. The default value is meant to prevent the
3579 * connection from being closed when the counter is about to
3580 * reached its maximal value (it is not allowed to wrap).
3582 * Lower values can be used to enforce policies such as "keys
3583 * must be refreshed every N packets with cipher X".
3585 * The renegotiation period can be disabled by setting
3586 * conf->disable_renegotiation to
3587 * MBEDTLS_SSL_RENEGOTIATION_DISABLED.
3589 * \note When the configured transport is
3590 * MBEDTLS_SSL_TRANSPORT_DATAGRAM the maximum renegotiation
3591 * period is 2^48 - 1, and for MBEDTLS_SSL_TRANSPORT_STREAM,
3592 * the maximum renegotiation period is 2^64 - 1.
3594 * \param conf SSL configuration
3595 * \param period The threshold value: a big-endian 64-bit number.
3597 void mbedtls_ssl_conf_renegotiation_period(mbedtls_ssl_config
*conf
,
3598 const unsigned char period
[8]);
3599 #endif /* MBEDTLS_SSL_RENEGOTIATION */
3602 * \brief Check if there is data already read from the
3603 * underlying transport but not yet processed.
3605 * \param ssl SSL context
3607 * \return 0 if nothing's pending, 1 otherwise.
3609 * \note This is different in purpose and behaviour from
3610 * \c mbedtls_ssl_get_bytes_avail in that it considers
3611 * any kind of unprocessed data, not only unread
3612 * application data. If \c mbedtls_ssl_get_bytes
3613 * returns a non-zero value, this function will
3614 * also signal pending data, but the converse does
3615 * not hold. For example, in DTLS there might be
3616 * further records waiting to be processed from
3617 * the current underlying transport's datagram.
3619 * \note If this function returns 1 (data pending), this
3620 * does not imply that a subsequent call to
3621 * \c mbedtls_ssl_read will provide any data;
3622 * e.g., the unprocessed data might turn out
3623 * to be an alert or a handshake message.
3625 * \note This function is useful in the following situation:
3626 * If the SSL/TLS module successfully returns from an
3627 * operation - e.g. a handshake or an application record
3628 * read - and you're awaiting incoming data next, you
3629 * must not immediately idle on the underlying transport
3630 * to have data ready, but you need to check the value
3631 * of this function first. The reason is that the desired
3632 * data might already be read but not yet processed.
3633 * If, in contrast, a previous call to the SSL/TLS module
3634 * returned MBEDTLS_ERR_SSL_WANT_READ, it is not necessary
3635 * to call this function, as the latter error code entails
3636 * that all internal data has been processed.
3639 int mbedtls_ssl_check_pending(const mbedtls_ssl_context
*ssl
);
3642 * \brief Return the number of application data bytes
3643 * remaining to be read from the current record.
3645 * \param ssl SSL context
3647 * \return How many bytes are available in the application
3648 * data record read buffer.
3650 * \note When working over a datagram transport, this is
3651 * useful to detect the current datagram's boundary
3652 * in case \c mbedtls_ssl_read has written the maximal
3653 * amount of data fitting into the input buffer.
3656 size_t mbedtls_ssl_get_bytes_avail(const mbedtls_ssl_context
*ssl
);
3659 * \brief Return the result of the certificate verification
3661 * \param ssl The SSL context to use.
3663 * \return \c 0 if the certificate verification was successful.
3664 * \return \c -1u if the result is not available. This may happen
3665 * e.g. if the handshake aborts early, or a verification
3666 * callback returned a fatal error.
3667 * \return A bitwise combination of \c MBEDTLS_X509_BADCERT_XXX
3668 * and \c MBEDTLS_X509_BADCRL_XXX failure flags; see x509.h.
3670 uint32_t mbedtls_ssl_get_verify_result(const mbedtls_ssl_context
*ssl
);
3673 * \brief Return the name of the current ciphersuite
3675 * \param ssl SSL context
3677 * \return a string containing the ciphersuite name
3679 const char *mbedtls_ssl_get_ciphersuite(const mbedtls_ssl_context
*ssl
);
3682 * \brief Return the current SSL version (SSLv3/TLSv1/etc)
3684 * \param ssl SSL context
3686 * \return a string containing the SSL version
3688 const char *mbedtls_ssl_get_version(const mbedtls_ssl_context
*ssl
);
3691 * \brief Return the (maximum) number of bytes added by the record
3692 * layer: header + encryption/MAC overhead (inc. padding)
3694 * \note This function is not available (always returns an error)
3695 * when record compression is enabled.
3697 * \param ssl SSL context
3699 * \return Current maximum record expansion in bytes, or
3700 * MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE if compression is
3701 * enabled, which makes expansion much less predictable
3703 int mbedtls_ssl_get_record_expansion(const mbedtls_ssl_context
*ssl
);
3705 #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3707 * \brief Return the maximum fragment length (payload, in bytes) for
3708 * the output buffer. For the client, this is the configured
3709 * value. For the server, it is the minimum of two - the
3710 * configured value and the negotiated one.
3712 * \sa mbedtls_ssl_conf_max_frag_len()
3713 * \sa mbedtls_ssl_get_max_record_payload()
3715 * \param ssl SSL context
3717 * \return Current maximum fragment length for the output buffer.
3719 size_t mbedtls_ssl_get_output_max_frag_len(const mbedtls_ssl_context
*ssl
);
3722 * \brief Return the maximum fragment length (payload, in bytes) for
3723 * the input buffer. This is the negotiated maximum fragment
3724 * length, or, if there is none, MBEDTLS_SSL_MAX_CONTENT_LEN.
3725 * If it is not defined either, the value is 2^14. This function
3726 * works as its predecessor, \c mbedtls_ssl_get_max_frag_len().
3728 * \sa mbedtls_ssl_conf_max_frag_len()
3729 * \sa mbedtls_ssl_get_max_record_payload()
3731 * \param ssl SSL context
3733 * \return Current maximum fragment length for the output buffer.
3735 size_t mbedtls_ssl_get_input_max_frag_len(const mbedtls_ssl_context
*ssl
);
3737 #if !defined(MBEDTLS_DEPRECATED_REMOVED)
3739 #if defined(MBEDTLS_DEPRECATED_WARNING)
3740 #define MBEDTLS_DEPRECATED __attribute__((deprecated))
3742 #define MBEDTLS_DEPRECATED
3746 * \brief This function is a deprecated approach to getting the max
3747 * fragment length. Its an alias for
3748 * \c mbedtls_ssl_get_output_max_frag_len(), as the behaviour
3749 * is the same. See \c mbedtls_ssl_get_output_max_frag_len() for
3752 * \sa mbedtls_ssl_get_input_max_frag_len()
3753 * \sa mbedtls_ssl_get_output_max_frag_len()
3755 * \param ssl SSL context
3757 * \return Current maximum fragment length for the output buffer.
3759 MBEDTLS_DEPRECATED
size_t mbedtls_ssl_get_max_frag_len(
3760 const mbedtls_ssl_context
*ssl
);
3761 #endif /* MBEDTLS_DEPRECATED_REMOVED */
3762 #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3765 * \brief Return the current maximum outgoing record payload in bytes.
3766 * This takes into account the config.h setting \c
3767 * MBEDTLS_SSL_OUT_CONTENT_LEN, the configured and negotiated
3768 * max fragment length extension if used, and for DTLS the
3769 * path MTU as configured and current record expansion.
3771 * \note With DTLS, \c mbedtls_ssl_write() will return an error if
3772 * called with a larger length value.
3773 * With TLS, \c mbedtls_ssl_write() will fragment the input if
3774 * necessary and return the number of bytes written; it is up
3775 * to the caller to call \c mbedtls_ssl_write() again in
3776 * order to send the remaining bytes if any.
3778 * \note This function is not available (always returns an error)
3779 * when record compression is enabled.
3781 * \sa mbedtls_ssl_set_mtu()
3782 * \sa mbedtls_ssl_get_output_max_frag_len()
3783 * \sa mbedtls_ssl_get_input_max_frag_len()
3784 * \sa mbedtls_ssl_get_record_expansion()
3786 * \param ssl SSL context
3788 * \return Current maximum payload for an outgoing record,
3789 * or a negative error code.
3791 int mbedtls_ssl_get_max_out_record_payload(const mbedtls_ssl_context
*ssl
);
3793 #if defined(MBEDTLS_X509_CRT_PARSE_C)
3795 * \brief Return the peer certificate from the current connection.
3797 * \param ssl The SSL context to use. This must be initialized and setup.
3799 * \return The current peer certificate, if available.
3800 * The returned certificate is owned by the SSL context and
3801 * is valid only until the next call to the SSL API.
3802 * \return \c NULL if no peer certificate is available. This might
3803 * be because the chosen ciphersuite doesn't use CRTs
3804 * (PSK-based ciphersuites, for example), or because
3805 * #MBEDTLS_SSL_KEEP_PEER_CERTIFICATE has been disabled,
3806 * allowing the stack to free the peer's CRT to save memory.
3808 * \note For one-time inspection of the peer's certificate during
3809 * the handshake, consider registering an X.509 CRT verification
3810 * callback through mbedtls_ssl_conf_verify() instead of calling
3811 * this function. Using mbedtls_ssl_conf_verify() also comes at
3812 * the benefit of allowing you to influence the verification
3813 * process, for example by masking expected and tolerated
3814 * verification failures.
3816 * \warning You must not use the pointer returned by this function
3817 * after any further call to the SSL API, including
3818 * mbedtls_ssl_read() and mbedtls_ssl_write(); this is
3819 * because the pointer might change during renegotiation,
3820 * which happens transparently to the user.
3821 * If you want to use the certificate across API calls,
3822 * you must make a copy.
3824 const mbedtls_x509_crt
*mbedtls_ssl_get_peer_cert(const mbedtls_ssl_context
*ssl
);
3825 #endif /* MBEDTLS_X509_CRT_PARSE_C */
3827 #if defined(MBEDTLS_SSL_CLI_C)
3829 * \brief Save session in order to resume it later (client-side only)
3830 * Session data is copied to presented session structure.
3833 * \param ssl SSL context
3834 * \param session session context
3836 * \return 0 if successful,
3837 * MBEDTLS_ERR_SSL_ALLOC_FAILED if memory allocation failed,
3838 * MBEDTLS_ERR_SSL_BAD_INPUT_DATA if used server-side or
3839 * arguments are otherwise invalid.
3841 * \note Only the server certificate is copied, and not the full chain,
3842 * so you should not attempt to validate the certificate again
3843 * by calling \c mbedtls_x509_crt_verify() on it.
3844 * Instead, you should use the results from the verification
3845 * in the original handshake by calling \c mbedtls_ssl_get_verify_result()
3846 * after loading the session again into a new SSL context
3847 * using \c mbedtls_ssl_set_session().
3849 * \note Once the session object is not needed anymore, you should
3850 * free it by calling \c mbedtls_ssl_session_free().
3852 * \sa mbedtls_ssl_set_session()
3854 int mbedtls_ssl_get_session(const mbedtls_ssl_context
*ssl
, mbedtls_ssl_session
*session
);
3855 #endif /* MBEDTLS_SSL_CLI_C */
3858 * \brief Perform the SSL handshake
3860 * \param ssl SSL context
3862 * \return \c 0 if successful.
3863 * \return #MBEDTLS_ERR_SSL_WANT_READ or #MBEDTLS_ERR_SSL_WANT_WRITE
3864 * if the handshake is incomplete and waiting for data to
3865 * be available for reading from or writing to the underlying
3866 * transport - in this case you must call this function again
3867 * when the underlying transport is ready for the operation.
3868 * \return #MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS if an asynchronous
3869 * operation is in progress (see
3870 * mbedtls_ssl_conf_async_private_cb()) - in this case you
3871 * must call this function again when the operation is ready.
3872 * \return #MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS if a cryptographic
3873 * operation is in progress (see mbedtls_ecp_set_max_ops()) -
3874 * in this case you must call this function again to complete
3875 * the handshake when you're done attending other tasks.
3876 * \return #MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED if DTLS is in use
3877 * and the client did not demonstrate reachability yet - in
3878 * this case you must stop using the context (see below).
3879 * \return Another SSL error code - in this case you must stop using
3880 * the context (see below).
3882 * \warning If this function returns something other than
3884 * #MBEDTLS_ERR_SSL_WANT_READ,
3885 * #MBEDTLS_ERR_SSL_WANT_WRITE,
3886 * #MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS or
3887 * #MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS,
3888 * you must stop using the SSL context for reading or writing,
3889 * and either free it or call \c mbedtls_ssl_session_reset()
3890 * on it before re-using it for a new connection; the current
3891 * connection must be closed.
3893 * \note If DTLS is in use, then you may choose to handle
3894 * #MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED specially for logging
3895 * purposes, as it is an expected return value rather than an
3896 * actual error, but you still need to reset/free the context.
3898 * \note Remarks regarding event-driven DTLS:
3899 * If the function returns #MBEDTLS_ERR_SSL_WANT_READ, no datagram
3900 * from the underlying transport layer is currently being processed,
3901 * and it is safe to idle until the timer or the underlying transport
3902 * signal a new event. This is not true for a successful handshake,
3903 * in which case the datagram of the underlying transport that is
3904 * currently being processed might or might not contain further
3907 int mbedtls_ssl_handshake(mbedtls_ssl_context
*ssl
);
3910 * \brief Perform a single step of the SSL handshake
3912 * \note The state of the context (ssl->state) will be at
3913 * the next state after this function returns \c 0. Do not
3914 * call this function if state is MBEDTLS_SSL_HANDSHAKE_OVER.
3916 * \param ssl SSL context
3918 * \return See mbedtls_ssl_handshake().
3920 * \warning If this function returns something other than \c 0,
3921 * #MBEDTLS_ERR_SSL_WANT_READ, #MBEDTLS_ERR_SSL_WANT_WRITE,
3922 * #MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS or
3923 * #MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS, you must stop using
3924 * the SSL context for reading or writing, and either free it
3925 * or call \c mbedtls_ssl_session_reset() on it before
3926 * re-using it for a new connection; the current connection
3929 int mbedtls_ssl_handshake_step(mbedtls_ssl_context
*ssl
);
3931 #if defined(MBEDTLS_SSL_RENEGOTIATION)
3933 * \brief Initiate an SSL renegotiation on the running connection.
3934 * Client: perform the renegotiation right now.
3935 * Server: request renegotiation, which will be performed
3936 * during the next call to mbedtls_ssl_read() if honored by
3939 * \param ssl SSL context
3941 * \return 0 if successful, or any mbedtls_ssl_handshake() return
3942 * value except #MBEDTLS_ERR_SSL_CLIENT_RECONNECT that can't
3943 * happen during a renegotiation.
3945 * \warning If this function returns something other than \c 0,
3946 * #MBEDTLS_ERR_SSL_WANT_READ, #MBEDTLS_ERR_SSL_WANT_WRITE,
3947 * #MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS or
3948 * #MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS, you must stop using
3949 * the SSL context for reading or writing, and either free it
3950 * or call \c mbedtls_ssl_session_reset() on it before
3951 * re-using it for a new connection; the current connection
3955 int mbedtls_ssl_renegotiate(mbedtls_ssl_context
*ssl
);
3956 #endif /* MBEDTLS_SSL_RENEGOTIATION */
3959 * \brief Read at most 'len' application data bytes
3961 * \param ssl SSL context
3962 * \param buf buffer that will hold the data
3963 * \param len maximum number of bytes to read
3965 * \return The (positive) number of bytes read if successful.
3966 * \return \c 0 if the read end of the underlying transport was closed
3967 * without sending a CloseNotify beforehand, which might happen
3968 * because of various reasons (internal error of an underlying
3969 * stack, non-conformant peer not sending a CloseNotify and
3970 * such) - in this case you must stop using the context
3972 * \return #MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY if the underlying
3973 * transport is still functional, but the peer has
3974 * acknowledged to not send anything anymore.
3975 * \return #MBEDTLS_ERR_SSL_WANT_READ or #MBEDTLS_ERR_SSL_WANT_WRITE
3976 * if the handshake is incomplete and waiting for data to
3977 * be available for reading from or writing to the underlying
3978 * transport - in this case you must call this function again
3979 * when the underlying transport is ready for the operation.
3980 * \return #MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS if an asynchronous
3981 * operation is in progress (see
3982 * mbedtls_ssl_conf_async_private_cb()) - in this case you
3983 * must call this function again when the operation is ready.
3984 * \return #MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS if a cryptographic
3985 * operation is in progress (see mbedtls_ecp_set_max_ops()) -
3986 * in this case you must call this function again to complete
3987 * the handshake when you're done attending other tasks.
3988 * \return #MBEDTLS_ERR_SSL_CLIENT_RECONNECT if we're at the server
3989 * side of a DTLS connection and the client is initiating a
3990 * new connection using the same source port. See below.
3991 * \return Another SSL error code - in this case you must stop using
3992 * the context (see below).
3994 * \warning If this function returns something other than
3996 * #MBEDTLS_ERR_SSL_WANT_READ,
3997 * #MBEDTLS_ERR_SSL_WANT_WRITE,
3998 * #MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS,
3999 * #MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS or
4000 * #MBEDTLS_ERR_SSL_CLIENT_RECONNECT,
4001 * you must stop using the SSL context for reading or writing,
4002 * and either free it or call \c mbedtls_ssl_session_reset()
4003 * on it before re-using it for a new connection; the current
4004 * connection must be closed.
4006 * \note When this function returns #MBEDTLS_ERR_SSL_CLIENT_RECONNECT
4007 * (which can only happen server-side), it means that a client
4008 * is initiating a new connection using the same source port.
4009 * You can either treat that as a connection close and wait
4010 * for the client to resend a ClientHello, or directly
4011 * continue with \c mbedtls_ssl_handshake() with the same
4012 * context (as it has been reset internally). Either way, you
4013 * must make sure this is seen by the application as a new
4014 * connection: application state, if any, should be reset, and
4015 * most importantly the identity of the client must be checked
4016 * again. WARNING: not validating the identity of the client
4017 * again, or not transmitting the new identity to the
4018 * application layer, would allow authentication bypass!
4020 * \note Remarks regarding event-driven DTLS:
4021 * - If the function returns #MBEDTLS_ERR_SSL_WANT_READ, no datagram
4022 * from the underlying transport layer is currently being processed,
4023 * and it is safe to idle until the timer or the underlying transport
4024 * signal a new event.
4025 * - This function may return MBEDTLS_ERR_SSL_WANT_READ even if data was
4026 * initially available on the underlying transport, as this data may have
4027 * been only e.g. duplicated messages or a renegotiation request.
4028 * Therefore, you must be prepared to receive MBEDTLS_ERR_SSL_WANT_READ even
4029 * when reacting to an incoming-data event from the underlying transport.
4030 * - On success, the datagram of the underlying transport that is currently
4031 * being processed may contain further DTLS records. You should call
4032 * \c mbedtls_ssl_check_pending to check for remaining records.
4035 int mbedtls_ssl_read(mbedtls_ssl_context
*ssl
, unsigned char *buf
, size_t len
);
4038 * \brief Try to write exactly 'len' application data bytes
4040 * \warning This function will do partial writes in some cases. If the
4041 * return value is non-negative but less than length, the
4042 * function must be called again with updated arguments:
4043 * buf + ret, len - ret (if ret is the return value) until
4044 * it returns a value equal to the last 'len' argument.
4046 * \param ssl SSL context
4047 * \param buf buffer holding the data
4048 * \param len how many bytes must be written
4050 * \return The (non-negative) number of bytes actually written if
4051 * successful (may be less than \p len).
4052 * \return #MBEDTLS_ERR_SSL_WANT_READ or #MBEDTLS_ERR_SSL_WANT_WRITE
4053 * if the handshake is incomplete and waiting for data to
4054 * be available for reading from or writing to the underlying
4055 * transport - in this case you must call this function again
4056 * when the underlying transport is ready for the operation.
4057 * \return #MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS if an asynchronous
4058 * operation is in progress (see
4059 * mbedtls_ssl_conf_async_private_cb()) - in this case you
4060 * must call this function again when the operation is ready.
4061 * \return #MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS if a cryptographic
4062 * operation is in progress (see mbedtls_ecp_set_max_ops()) -
4063 * in this case you must call this function again to complete
4064 * the handshake when you're done attending other tasks.
4065 * \return Another SSL error code - in this case you must stop using
4066 * the context (see below).
4068 * \warning If this function returns something other than
4069 * a non-negative value,
4070 * #MBEDTLS_ERR_SSL_WANT_READ,
4071 * #MBEDTLS_ERR_SSL_WANT_WRITE,
4072 * #MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS or
4073 * #MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS,
4074 * you must stop using the SSL context for reading or writing,
4075 * and either free it or call \c mbedtls_ssl_session_reset()
4076 * on it before re-using it for a new connection; the current
4077 * connection must be closed.
4079 * \note When this function returns #MBEDTLS_ERR_SSL_WANT_WRITE/READ,
4080 * it must be called later with the *same* arguments,
4081 * until it returns a value greater that or equal to 0. When
4082 * the function returns #MBEDTLS_ERR_SSL_WANT_WRITE there may be
4083 * some partial data in the output buffer, however this is not
4086 * \note If the requested length is greater than the maximum
4087 * fragment length (either the built-in limit or the one set
4088 * or negotiated with the peer), then:
4089 * - with TLS, less bytes than requested are written.
4090 * - with DTLS, MBEDTLS_ERR_SSL_BAD_INPUT_DATA is returned.
4091 * \c mbedtls_ssl_get_output_max_frag_len() may be used to
4092 * query the active maximum fragment length.
4094 * \note Attempting to write 0 bytes will result in an empty TLS
4095 * application record being sent.
4097 int mbedtls_ssl_write(mbedtls_ssl_context
*ssl
, const unsigned char *buf
, size_t len
);
4100 * \brief Send an alert message
4102 * \param ssl SSL context
4103 * \param level The alert level of the message
4104 * (MBEDTLS_SSL_ALERT_LEVEL_WARNING or MBEDTLS_SSL_ALERT_LEVEL_FATAL)
4105 * \param message The alert message (SSL_ALERT_MSG_*)
4107 * \return 0 if successful, or a specific SSL error code.
4109 * \note If this function returns something other than 0 or
4110 * MBEDTLS_ERR_SSL_WANT_READ/WRITE, you must stop using
4111 * the SSL context for reading or writing, and either free it or
4112 * call \c mbedtls_ssl_session_reset() on it before re-using it
4113 * for a new connection; the current connection must be closed.
4115 int mbedtls_ssl_send_alert_message(mbedtls_ssl_context
*ssl
,
4116 unsigned char level
,
4117 unsigned char message
);
4119 * \brief Notify the peer that the connection is being closed
4121 * \param ssl SSL context
4123 * \return 0 if successful, or a specific SSL error code.
4125 * \note If this function returns something other than 0 or
4126 * MBEDTLS_ERR_SSL_WANT_READ/WRITE, you must stop using
4127 * the SSL context for reading or writing, and either free it or
4128 * call \c mbedtls_ssl_session_reset() on it before re-using it
4129 * for a new connection; the current connection must be closed.
4131 int mbedtls_ssl_close_notify(mbedtls_ssl_context
*ssl
);
4134 * \brief Free referenced items in an SSL context and clear memory
4136 * \param ssl SSL context
4138 void mbedtls_ssl_free(mbedtls_ssl_context
*ssl
);
4140 #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
4142 * \brief Save an active connection as serialized data in a buffer.
4143 * This allows the freeing or re-using of the SSL context
4144 * while still picking up the connection later in a way that
4145 * it entirely transparent to the peer.
4147 * \see mbedtls_ssl_context_load()
4149 * \note This feature is currently only available under certain
4150 * conditions, see the documentation of the return value
4151 * #MBEDTLS_ERR_SSL_BAD_INPUT_DATA for details.
4153 * \note When this function succeeds, it calls
4154 * mbedtls_ssl_session_reset() on \p ssl which as a result is
4155 * no longer associated with the connection that has been
4156 * serialized. This avoids creating copies of the connection
4157 * state. You're then free to either re-use the context
4158 * structure for a different connection, or call
4159 * mbedtls_ssl_free() on it. See the documentation of
4160 * mbedtls_ssl_session_reset() for more details.
4162 * \param ssl The SSL context to save. On success, it is no longer
4163 * associated with the connection that has been serialized.
4164 * \param buf The buffer to write the serialized data to. It must be a
4165 * writeable buffer of at least \p buf_len bytes, or may be \c
4166 * NULL if \p buf_len is \c 0.
4167 * \param buf_len The number of bytes available for writing in \p buf.
4168 * \param olen The size in bytes of the data that has been or would have
4169 * been written. It must point to a valid \c size_t.
4171 * \note \p olen is updated to the correct value regardless of
4172 * whether \p buf_len was large enough. This makes it possible
4173 * to determine the necessary size by calling this function
4174 * with \p buf set to \c NULL and \p buf_len to \c 0. However,
4175 * the value of \p olen is only guaranteed to be correct when
4176 * the function returns #MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL or
4177 * \c 0. If the return value is different, then the value of
4178 * \p olen is undefined.
4180 * \return \c 0 if successful.
4181 * \return #MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL if \p buf is too small.
4182 * \return #MBEDTLS_ERR_SSL_ALLOC_FAILED if memory allocation failed
4183 * while reseting the context.
4184 * \return #MBEDTLS_ERR_SSL_BAD_INPUT_DATA if a handshake is in
4185 * progress, or there is pending data for reading or sending,
4186 * or the connection does not use DTLS 1.2 with an AEAD
4187 * ciphersuite, or renegotiation is enabled.
4189 int mbedtls_ssl_context_save(mbedtls_ssl_context
*ssl
,
4195 * \brief Load serialized connection data to an SSL context.
4197 * \see mbedtls_ssl_context_save()
4199 * \warning The same serialized data must never be loaded into more
4200 * that one context. In order to ensure that, after
4201 * successfully loading serialized data to an SSL context, you
4202 * should immediately destroy or invalidate all copies of the
4203 * serialized data that was loaded. Loading the same data in
4204 * more than one context would cause severe security failures
4205 * including but not limited to loss of confidentiality.
4207 * \note Before calling this function, the SSL context must be
4208 * prepared in one of the two following ways. The first way is
4209 * to take a context freshly initialised with
4210 * mbedtls_ssl_init() and call mbedtls_ssl_setup() on it with
4211 * the same ::mbedtls_ssl_config structure that was used in
4212 * the original connection. The second way is to
4213 * call mbedtls_ssl_session_reset() on a context that was
4214 * previously prepared as above but used in the meantime.
4215 * Either way, you must not use the context to perform a
4216 * handshake between calling mbedtls_ssl_setup() or
4217 * mbedtls_ssl_session_reset() and calling this function. You
4218 * may however call other setter functions in that time frame
4219 * as indicated in the note below.
4221 * \note Before or after calling this function successfully, you
4222 * also need to configure some connection-specific callbacks
4223 * and settings before you can use the connection again
4224 * (unless they were already set before calling
4225 * mbedtls_ssl_session_reset() and the values are suitable for
4226 * the present connection). Specifically, you want to call
4227 * at least mbedtls_ssl_set_bio() and
4228 * mbedtls_ssl_set_timer_cb(). All other SSL setter functions
4229 * are not necessary to call, either because they're only used
4230 * in handshakes, or because the setting is already saved. You
4231 * might choose to call them anyway, for example in order to
4232 * share code between the cases of establishing a new
4233 * connection and the case of loading an already-established
4236 * \note If you have new information about the path MTU, you want to
4237 * call mbedtls_ssl_set_mtu() after calling this function, as
4238 * otherwise this function would overwrite your
4239 * newly-configured value with the value that was active when
4240 * the context was saved.
4242 * \note When this function returns an error code, it calls
4243 * mbedtls_ssl_free() on \p ssl. In this case, you need to
4244 * prepare the context with the usual sequence starting with a
4245 * call to mbedtls_ssl_init() if you want to use it again.
4247 * \param ssl The SSL context structure to be populated. It must have
4248 * been prepared as described in the note above.
4249 * \param buf The buffer holding the serialized connection data. It must
4250 * be a readable buffer of at least \p len bytes.
4251 * \param len The size of the serialized data in bytes.
4253 * \return \c 0 if successful.
4254 * \return #MBEDTLS_ERR_SSL_ALLOC_FAILED if memory allocation failed.
4255 * \return #MBEDTLS_ERR_SSL_VERSION_MISMATCH if the serialized data
4256 * comes from a different Mbed TLS version or build.
4257 * \return #MBEDTLS_ERR_SSL_BAD_INPUT_DATA if input data is invalid.
4259 int mbedtls_ssl_context_load(mbedtls_ssl_context
*ssl
,
4260 const unsigned char *buf
,
4262 #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
4265 * \brief Initialize an SSL configuration context
4266 * Just makes the context ready for
4267 * mbedtls_ssl_config_defaults() or mbedtls_ssl_config_free().
4269 * \note You need to call mbedtls_ssl_config_defaults() unless you
4270 * manually set all of the relevant fields yourself.
4272 * \param conf SSL configuration context
4274 void mbedtls_ssl_config_init(mbedtls_ssl_config
*conf
);
4277 * \brief Load reasonnable default SSL configuration values.
4278 * (You need to call mbedtls_ssl_config_init() first.)
4280 * \param conf SSL configuration context
4281 * \param endpoint MBEDTLS_SSL_IS_CLIENT or MBEDTLS_SSL_IS_SERVER
4282 * \param transport MBEDTLS_SSL_TRANSPORT_STREAM for TLS, or
4283 * MBEDTLS_SSL_TRANSPORT_DATAGRAM for DTLS
4284 * \param preset a MBEDTLS_SSL_PRESET_XXX value
4286 * \note See \c mbedtls_ssl_conf_transport() for notes on DTLS.
4288 * \return 0 if successful, or
4289 * MBEDTLS_ERR_XXX_ALLOC_FAILED on memory allocation error.
4291 int mbedtls_ssl_config_defaults(mbedtls_ssl_config
*conf
,
4292 int endpoint
, int transport
, int preset
);
4295 * \brief Free an SSL configuration context
4297 * \param conf SSL configuration context
4299 void mbedtls_ssl_config_free(mbedtls_ssl_config
*conf
);
4302 * \brief Initialize SSL session structure
4304 * \param session SSL session
4306 void mbedtls_ssl_session_init(mbedtls_ssl_session
*session
);
4309 * \brief Free referenced items in an SSL session including the
4310 * peer certificate and clear memory
4312 * \note A session object can be freed even if the SSL context
4313 * that was used to retrieve the session is still in use.
4315 * \param session SSL session
4317 void mbedtls_ssl_session_free(mbedtls_ssl_session
*session
);
4320 * \brief TLS-PRF function for key derivation.
4322 * \param prf The tls_prf type funtion type to be used.
4323 * \param secret Secret for the key derivation function.
4324 * \param slen Length of the secret.
4325 * \param label String label for the key derivation function,
4326 * terminated with null character.
4327 * \param random Random bytes.
4328 * \param rlen Length of the random bytes buffer.
4329 * \param dstbuf The buffer holding the derived key.
4330 * \param dlen Length of the output buffer.
4332 * \return 0 on sucess. An SSL specific error on failure.
4334 int mbedtls_ssl_tls_prf(const mbedtls_tls_prf_types prf
,
4335 const unsigned char *secret
, size_t slen
,
4337 const unsigned char *random
, size_t rlen
,
4338 unsigned char *dstbuf
, size_t dlen
);