Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /**
  2.  * \file ssl.h
  3.  *
  4.  * \brief SSL/TLS functions.
  5.  */
  6. /*
  7.  *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  8.  *  SPDX-License-Identifier: GPL-2.0
  9.  *
  10.  *  This program is free software; you can redistribute it and/or modify
  11.  *  it under the terms of the GNU General Public License as published by
  12.  *  the Free Software Foundation; either version 2 of the License, or
  13.  *  (at your option) any later version.
  14.  *
  15.  *  This program is distributed in the hope that it will be useful,
  16.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.  *  GNU General Public License for more details.
  19.  *
  20.  *  You should have received a copy of the GNU General Public License along
  21.  *  with this program; if not, write to the Free Software Foundation, Inc.,
  22.  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  23.  *
  24.  *  This file is part of mbed TLS (https://tls.mbed.org)
  25.  */
  26. #ifndef MBEDTLS_SSL_H
  27. #define MBEDTLS_SSL_H
  28.  
  29. #if !defined(MBEDTLS_CONFIG_FILE)
  30. #include "config.h"
  31. #else
  32. #include MBEDTLS_CONFIG_FILE
  33. #endif
  34.  
  35. #include "bignum.h"
  36. #include "ecp.h"
  37.  
  38. #include "ssl_ciphersuites.h"
  39.  
  40. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  41. #include "x509_crt.h"
  42. #include "x509_crl.h"
  43. #endif
  44.  
  45. #if defined(MBEDTLS_DHM_C)
  46. #include "dhm.h"
  47. #endif
  48.  
  49. #if defined(MBEDTLS_ECDH_C)
  50. #include "ecdh.h"
  51. #endif
  52.  
  53. #if defined(MBEDTLS_ZLIB_SUPPORT)
  54.  
  55. #if defined(MBEDTLS_DEPRECATED_WARNING)
  56. #warning "Record compression support via MBEDTLS_ZLIB_SUPPORT is deprecated and will be removed in the next major revision of the library"
  57. #endif
  58.  
  59. #if defined(MBEDTLS_DEPRECATED_REMOVED)
  60. #error "Record compression support via MBEDTLS_ZLIB_SUPPORT is deprecated and cannot be used if MBEDTLS_DEPRECATED_REMOVED is set"
  61. #endif
  62.  
  63. #include "zlib.h"
  64. #endif
  65.  
  66. #if defined(MBEDTLS_HAVE_TIME)
  67. #include "platform_time.h"
  68. #endif
  69.  
  70. /*
  71.  * SSL Error codes
  72.  */
  73. #define MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE               -0x7080  /**< The requested feature is not available. */
  74. #define MBEDTLS_ERR_SSL_BAD_INPUT_DATA                    -0x7100  /**< Bad input parameters to function. */
  75. #define MBEDTLS_ERR_SSL_INVALID_MAC                       -0x7180  /**< Verification of the message MAC failed. */
  76. #define MBEDTLS_ERR_SSL_INVALID_RECORD                    -0x7200  /**< An invalid SSL record was received. */
  77. #define MBEDTLS_ERR_SSL_CONN_EOF                          -0x7280  /**< The connection indicated an EOF. */
  78. #define MBEDTLS_ERR_SSL_UNKNOWN_CIPHER                    -0x7300  /**< An unknown cipher was received. */
  79. #define MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN                  -0x7380  /**< The server has no ciphersuites in common with the client. */
  80. #define MBEDTLS_ERR_SSL_NO_RNG                            -0x7400  /**< No RNG was provided to the SSL module. */
  81. #define MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE             -0x7480  /**< No client certification received from the client, but required by the authentication mode. */
  82. #define MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE             -0x7500  /**< Our own certificate(s) is/are too large to send in an SSL message. */
  83. #define MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED              -0x7580  /**< The own certificate is not set, but needed by the server. */
  84. #define MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED              -0x7600  /**< The own private key or pre-shared key is not set, but needed. */
  85. #define MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED                 -0x7680  /**< No CA Chain is set, but required to operate. */
  86. #define MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE                -0x7700  /**< An unexpected message was received from our peer. */
  87. #define MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE               -0x7780  /**< A fatal alert message was received from our peer. */
  88. #define MBEDTLS_ERR_SSL_PEER_VERIFY_FAILED                -0x7800  /**< Verification of our peer failed. */
  89. #define MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY                 -0x7880  /**< The peer notified us that the connection is going to be closed. */
  90. #define MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO               -0x7900  /**< Processing of the ClientHello handshake message failed. */
  91. #define MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO               -0x7980  /**< Processing of the ServerHello handshake message failed. */
  92. #define MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE                -0x7A00  /**< Processing of the Certificate handshake message failed. */
  93. #define MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST        -0x7A80  /**< Processing of the CertificateRequest handshake message failed. */
  94. #define MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE        -0x7B00  /**< Processing of the ServerKeyExchange handshake message failed. */
  95. #define MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO_DONE          -0x7B80  /**< Processing of the ServerHelloDone handshake message failed. */
  96. #define MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE        -0x7C00  /**< Processing of the ClientKeyExchange handshake message failed. */
  97. #define MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP     -0x7C80  /**< Processing of the ClientKeyExchange handshake message failed in DHM / ECDH Read Public. */
  98. #define MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS     -0x7D00  /**< Processing of the ClientKeyExchange handshake message failed in DHM / ECDH Calculate Secret. */
  99. #define MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY         -0x7D80  /**< Processing of the CertificateVerify handshake message failed. */
  100. #define MBEDTLS_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC         -0x7E00  /**< Processing of the ChangeCipherSpec handshake message failed. */
  101. #define MBEDTLS_ERR_SSL_BAD_HS_FINISHED                   -0x7E80  /**< Processing of the Finished handshake message failed. */
  102. #define MBEDTLS_ERR_SSL_ALLOC_FAILED                      -0x7F00  /**< Memory allocation failed */
  103. #define MBEDTLS_ERR_SSL_HW_ACCEL_FAILED                   -0x7F80  /**< Hardware acceleration function returned with error */
  104. #define MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH              -0x6F80  /**< Hardware acceleration function skipped / left alone data */
  105. #define MBEDTLS_ERR_SSL_COMPRESSION_FAILED                -0x6F00  /**< Processing of the compression / decompression failed */
  106. #define MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION           -0x6E80  /**< Handshake protocol not within min/max boundaries */
  107. #define MBEDTLS_ERR_SSL_BAD_HS_NEW_SESSION_TICKET         -0x6E00  /**< Processing of the NewSessionTicket handshake message failed. */
  108. #define MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED            -0x6D80  /**< Session ticket has expired. */
  109. #define MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH                  -0x6D00  /**< Public key type mismatch (eg, asked for RSA key exchange and presented EC key) */
  110. #define MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY                  -0x6C80  /**< Unknown identity received (eg, PSK identity) */
  111. #define MBEDTLS_ERR_SSL_INTERNAL_ERROR                    -0x6C00  /**< Internal error (eg, unexpected failure in lower-level module) */
  112. #define MBEDTLS_ERR_SSL_COUNTER_WRAPPING                  -0x6B80  /**< A counter would wrap (eg, too many messages exchanged). */
  113. #define MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO       -0x6B00  /**< Unexpected message at ServerHello in renegotiation. */
  114. #define MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED             -0x6A80  /**< DTLS client must retry for hello verification */
  115. #define MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL                  -0x6A00  /**< A buffer is too small to receive or write a message */
  116. #define MBEDTLS_ERR_SSL_NO_USABLE_CIPHERSUITE             -0x6980  /**< None of the common ciphersuites is usable (eg, no suitable certificate, see debug messages). */
  117. #define MBEDTLS_ERR_SSL_WANT_READ                         -0x6900  /**< No data of requested type currently available on underlying transport. */
  118. #define MBEDTLS_ERR_SSL_WANT_WRITE                        -0x6880  /**< Connection requires a write call. */
  119. #define MBEDTLS_ERR_SSL_TIMEOUT                           -0x6800  /**< The operation timed out. */
  120. #define MBEDTLS_ERR_SSL_CLIENT_RECONNECT                  -0x6780  /**< The client initiated a reconnect from the same port. */
  121. #define MBEDTLS_ERR_SSL_UNEXPECTED_RECORD                 -0x6700  /**< Record header looks valid but is not expected. */
  122. #define MBEDTLS_ERR_SSL_NON_FATAL                         -0x6680  /**< The alert message received indicates a non-fatal error. */
  123. #define MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH               -0x6600  /**< Couldn't set the hash for verifying CertificateVerify */
  124. #define MBEDTLS_ERR_SSL_CONTINUE_PROCESSING               -0x6580  /**< Internal-only message signaling that further message-processing should be done */
  125. #define MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS                 -0x6500  /**< The asynchronous operation is not completed yet. */
  126. #define MBEDTLS_ERR_SSL_EARLY_MESSAGE                     -0x6480  /**< Internal-only message signaling that a message arrived early. */
  127. #define MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS                -0x7000  /**< A cryptographic operation is in progress. Try again later. */
  128.  
  129. /*
  130.  * Various constants
  131.  */
  132. #define MBEDTLS_SSL_MAJOR_VERSION_3             3
  133. #define MBEDTLS_SSL_MINOR_VERSION_0             0   /*!< SSL v3.0 */
  134. #define MBEDTLS_SSL_MINOR_VERSION_1             1   /*!< TLS v1.0 */
  135. #define MBEDTLS_SSL_MINOR_VERSION_2             2   /*!< TLS v1.1 */
  136. #define MBEDTLS_SSL_MINOR_VERSION_3             3   /*!< TLS v1.2 */
  137.  
  138. #define MBEDTLS_SSL_TRANSPORT_STREAM            0   /*!< TLS      */
  139. #define MBEDTLS_SSL_TRANSPORT_DATAGRAM          1   /*!< DTLS     */
  140.  
  141. #define MBEDTLS_SSL_MAX_HOST_NAME_LEN           255 /*!< Maximum host name defined in RFC 1035 */
  142.  
  143. /* RFC 6066 section 4, see also mfl_code_to_length in ssl_tls.c
  144.  * NONE must be zero so that memset()ing structure to zero works */
  145. #define MBEDTLS_SSL_MAX_FRAG_LEN_NONE           0   /*!< don't use this extension   */
  146. #define MBEDTLS_SSL_MAX_FRAG_LEN_512            1   /*!< MaxFragmentLength 2^9      */
  147. #define MBEDTLS_SSL_MAX_FRAG_LEN_1024           2   /*!< MaxFragmentLength 2^10     */
  148. #define MBEDTLS_SSL_MAX_FRAG_LEN_2048           3   /*!< MaxFragmentLength 2^11     */
  149. #define MBEDTLS_SSL_MAX_FRAG_LEN_4096           4   /*!< MaxFragmentLength 2^12     */
  150. #define MBEDTLS_SSL_MAX_FRAG_LEN_INVALID        5   /*!< first invalid value        */
  151.  
  152. #define MBEDTLS_SSL_IS_CLIENT                   0
  153. #define MBEDTLS_SSL_IS_SERVER                   1
  154.  
  155. #define MBEDTLS_SSL_IS_NOT_FALLBACK             0
  156. #define MBEDTLS_SSL_IS_FALLBACK                 1
  157.  
  158. #define MBEDTLS_SSL_EXTENDED_MS_DISABLED        0
  159. #define MBEDTLS_SSL_EXTENDED_MS_ENABLED         1
  160.  
  161. #define MBEDTLS_SSL_ETM_DISABLED                0
  162. #define MBEDTLS_SSL_ETM_ENABLED                 1
  163.  
  164. #define MBEDTLS_SSL_COMPRESS_NULL               0
  165. #define MBEDTLS_SSL_COMPRESS_DEFLATE            1
  166.  
  167. #define MBEDTLS_SSL_VERIFY_NONE                 0
  168. #define MBEDTLS_SSL_VERIFY_OPTIONAL             1
  169. #define MBEDTLS_SSL_VERIFY_REQUIRED             2
  170. #define MBEDTLS_SSL_VERIFY_UNSET                3 /* Used only for sni_authmode */
  171.  
  172. #define MBEDTLS_SSL_LEGACY_RENEGOTIATION        0
  173. #define MBEDTLS_SSL_SECURE_RENEGOTIATION        1
  174.  
  175. #define MBEDTLS_SSL_RENEGOTIATION_DISABLED      0
  176. #define MBEDTLS_SSL_RENEGOTIATION_ENABLED       1
  177.  
  178. #define MBEDTLS_SSL_ANTI_REPLAY_DISABLED        0
  179. #define MBEDTLS_SSL_ANTI_REPLAY_ENABLED         1
  180.  
  181. #define MBEDTLS_SSL_RENEGOTIATION_NOT_ENFORCED  -1
  182. #define MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT  16
  183.  
  184. #define MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION     0
  185. #define MBEDTLS_SSL_LEGACY_ALLOW_RENEGOTIATION  1
  186. #define MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE      2
  187.  
  188. #define MBEDTLS_SSL_TRUNC_HMAC_DISABLED         0
  189. #define MBEDTLS_SSL_TRUNC_HMAC_ENABLED          1
  190. #define MBEDTLS_SSL_TRUNCATED_HMAC_LEN          10  /* 80 bits, rfc 6066 section 7 */
  191.  
  192. #define MBEDTLS_SSL_SESSION_TICKETS_DISABLED     0
  193. #define MBEDTLS_SSL_SESSION_TICKETS_ENABLED      1
  194.  
  195. #define MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED    0
  196. #define MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED     1
  197.  
  198. #define MBEDTLS_SSL_ARC4_ENABLED                0
  199. #define MBEDTLS_SSL_ARC4_DISABLED               1
  200.  
  201. #define MBEDTLS_SSL_PRESET_DEFAULT              0
  202. #define MBEDTLS_SSL_PRESET_SUITEB               2
  203.  
  204. #define MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED       1
  205. #define MBEDTLS_SSL_CERT_REQ_CA_LIST_DISABLED      0
  206.  
  207. /*
  208.  * Default range for DTLS retransmission timer value, in milliseconds.
  209.  * RFC 6347 4.2.4.1 says from 1 second to 60 seconds.
  210.  */
  211. #define MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN    1000
  212. #define MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX   60000
  213.  
  214. /**
  215.  * \name SECTION: Module settings
  216.  *
  217.  * The configuration options you can set for this module are in this section.
  218.  * Either change them in config.h or define them on the compiler command line.
  219.  * \{
  220.  */
  221.  
  222. #if !defined(MBEDTLS_SSL_DEFAULT_TICKET_LIFETIME)
  223. #define MBEDTLS_SSL_DEFAULT_TICKET_LIFETIME     86400 /**< Lifetime of session tickets (if enabled) */
  224. #endif
  225.  
  226. /*
  227.  * Maximum fragment length in bytes,
  228.  * determines the size of each of the two internal I/O buffers.
  229.  *
  230.  * Note: the RFC defines the default size of SSL / TLS messages. If you
  231.  * change the value here, other clients / servers may not be able to
  232.  * communicate with you anymore. Only change this value if you control
  233.  * both sides of the connection and have it reduced at both sides, or
  234.  * if you're using the Max Fragment Length extension and you know all your
  235.  * peers are using it too!
  236.  */
  237. #if !defined(MBEDTLS_SSL_MAX_CONTENT_LEN)
  238. #define MBEDTLS_SSL_MAX_CONTENT_LEN         16384   /**< Size of the input / output buffer */
  239. #endif
  240.  
  241. #if !defined(MBEDTLS_SSL_IN_CONTENT_LEN)
  242. #define MBEDTLS_SSL_IN_CONTENT_LEN MBEDTLS_SSL_MAX_CONTENT_LEN
  243. #endif
  244.  
  245. #if !defined(MBEDTLS_SSL_OUT_CONTENT_LEN)
  246. #define MBEDTLS_SSL_OUT_CONTENT_LEN MBEDTLS_SSL_MAX_CONTENT_LEN
  247. #endif
  248.  
  249. /*
  250.  * Maximum number of heap-allocated bytes for the purpose of
  251.  * DTLS handshake message reassembly and future message buffering.
  252.  */
  253. #if !defined(MBEDTLS_SSL_DTLS_MAX_BUFFERING)
  254. #define MBEDTLS_SSL_DTLS_MAX_BUFFERING 32768
  255. #endif
  256.  
  257. /* \} name SECTION: Module settings */
  258.  
  259. /*
  260.  * Length of the verify data for secure renegotiation
  261.  */
  262. #if defined(MBEDTLS_SSL_PROTO_SSL3)
  263. #define MBEDTLS_SSL_VERIFY_DATA_MAX_LEN 36
  264. #else
  265. #define MBEDTLS_SSL_VERIFY_DATA_MAX_LEN 12
  266. #endif
  267.  
  268. /*
  269.  * Signaling ciphersuite values (SCSV)
  270.  */
  271. #define MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO    0xFF   /**< renegotiation info ext */
  272. #define MBEDTLS_SSL_FALLBACK_SCSV_VALUE         0x5600 /**< RFC 7507 section 2 */
  273.  
  274. /*
  275.  * Supported Signature and Hash algorithms (For TLS 1.2)
  276.  * RFC 5246 section 7.4.1.4.1
  277.  */
  278. #define MBEDTLS_SSL_HASH_NONE                0
  279. #define MBEDTLS_SSL_HASH_MD5                 1
  280. #define MBEDTLS_SSL_HASH_SHA1                2
  281. #define MBEDTLS_SSL_HASH_SHA224              3
  282. #define MBEDTLS_SSL_HASH_SHA256              4
  283. #define MBEDTLS_SSL_HASH_SHA384              5
  284. #define MBEDTLS_SSL_HASH_SHA512              6
  285.  
  286. #define MBEDTLS_SSL_SIG_ANON                 0
  287. #define MBEDTLS_SSL_SIG_RSA                  1
  288. #define MBEDTLS_SSL_SIG_ECDSA                3
  289.  
  290. /*
  291.  * Client Certificate Types
  292.  * RFC 5246 section 7.4.4 plus RFC 4492 section 5.5
  293.  */
  294. #define MBEDTLS_SSL_CERT_TYPE_RSA_SIGN       1
  295. #define MBEDTLS_SSL_CERT_TYPE_ECDSA_SIGN    64
  296.  
  297. /*
  298.  * Message, alert and handshake types
  299.  */
  300. #define MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC     20
  301. #define MBEDTLS_SSL_MSG_ALERT                  21
  302. #define MBEDTLS_SSL_MSG_HANDSHAKE              22
  303. #define MBEDTLS_SSL_MSG_APPLICATION_DATA       23
  304.  
  305. #define MBEDTLS_SSL_ALERT_LEVEL_WARNING         1
  306. #define MBEDTLS_SSL_ALERT_LEVEL_FATAL           2
  307.  
  308. #define MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY           0  /* 0x00 */
  309. #define MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE    10  /* 0x0A */
  310. #define MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC        20  /* 0x14 */
  311. #define MBEDTLS_SSL_ALERT_MSG_DECRYPTION_FAILED     21  /* 0x15 */
  312. #define MBEDTLS_SSL_ALERT_MSG_RECORD_OVERFLOW       22  /* 0x16 */
  313. #define MBEDTLS_SSL_ALERT_MSG_DECOMPRESSION_FAILURE 30  /* 0x1E */
  314. #define MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE     40  /* 0x28 */
  315. #define MBEDTLS_SSL_ALERT_MSG_NO_CERT               41  /* 0x29 */
  316. #define MBEDTLS_SSL_ALERT_MSG_BAD_CERT              42  /* 0x2A */
  317. #define MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT      43  /* 0x2B */
  318. #define MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED          44  /* 0x2C */
  319. #define MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED          45  /* 0x2D */
  320. #define MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN          46  /* 0x2E */
  321. #define MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER     47  /* 0x2F */
  322. #define MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA            48  /* 0x30 */
  323. #define MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED         49  /* 0x31 */
  324. #define MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR          50  /* 0x32 */
  325. #define MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR         51  /* 0x33 */
  326. #define MBEDTLS_SSL_ALERT_MSG_EXPORT_RESTRICTION    60  /* 0x3C */
  327. #define MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION      70  /* 0x46 */
  328. #define MBEDTLS_SSL_ALERT_MSG_INSUFFICIENT_SECURITY 71  /* 0x47 */
  329. #define MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR        80  /* 0x50 */
  330. #define MBEDTLS_SSL_ALERT_MSG_INAPROPRIATE_FALLBACK 86  /* 0x56 */
  331. #define MBEDTLS_SSL_ALERT_MSG_USER_CANCELED         90  /* 0x5A */
  332. #define MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION     100  /* 0x64 */
  333. #define MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT      110  /* 0x6E */
  334. #define MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME    112  /* 0x70 */
  335. #define MBEDTLS_SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY 115  /* 0x73 */
  336. #define MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL 120 /* 0x78 */
  337.  
  338. #define MBEDTLS_SSL_HS_HELLO_REQUEST            0
  339. #define MBEDTLS_SSL_HS_CLIENT_HELLO             1
  340. #define MBEDTLS_SSL_HS_SERVER_HELLO             2
  341. #define MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST     3
  342. #define MBEDTLS_SSL_HS_NEW_SESSION_TICKET       4
  343. #define MBEDTLS_SSL_HS_CERTIFICATE             11
  344. #define MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE     12
  345. #define MBEDTLS_SSL_HS_CERTIFICATE_REQUEST     13
  346. #define MBEDTLS_SSL_HS_SERVER_HELLO_DONE       14
  347. #define MBEDTLS_SSL_HS_CERTIFICATE_VERIFY      15
  348. #define MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE     16
  349. #define MBEDTLS_SSL_HS_FINISHED                20
  350.  
  351. /*
  352.  * TLS extensions
  353.  */
  354. #define MBEDTLS_TLS_EXT_SERVERNAME                   0
  355. #define MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME          0
  356.  
  357. #define MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH          1
  358.  
  359. #define MBEDTLS_TLS_EXT_TRUNCATED_HMAC               4
  360.  
  361. #define MBEDTLS_TLS_EXT_SUPPORTED_ELLIPTIC_CURVES   10
  362. #define MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS     11
  363.  
  364. #define MBEDTLS_TLS_EXT_SIG_ALG                     13
  365.  
  366. #define MBEDTLS_TLS_EXT_ALPN                        16
  367.  
  368. #define MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC            22 /* 0x16 */
  369. #define MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET  0x0017 /* 23 */
  370.  
  371. #define MBEDTLS_TLS_EXT_SESSION_TICKET              35
  372.  
  373. #define MBEDTLS_TLS_EXT_ECJPAKE_KKPP               256 /* experimental */
  374.  
  375. #define MBEDTLS_TLS_EXT_RENEGOTIATION_INFO      0xFF01
  376.  
  377. /*
  378.  * Size defines
  379.  */
  380. #if !defined(MBEDTLS_PSK_MAX_LEN)
  381. #define MBEDTLS_PSK_MAX_LEN            32 /* 256 bits */
  382. #endif
  383.  
  384. /* Dummy type used only for its size */
  385. union mbedtls_ssl_premaster_secret
  386. {
  387. #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
  388.     unsigned char _pms_rsa[48];                         /* RFC 5246 8.1.1 */
  389. #endif
  390. #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
  391.     unsigned char _pms_dhm[MBEDTLS_MPI_MAX_SIZE];      /* RFC 5246 8.1.2 */
  392. #endif
  393. #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED)    || \
  394.     defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)  || \
  395.     defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED)     || \
  396.     defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
  397.     unsigned char _pms_ecdh[MBEDTLS_ECP_MAX_BYTES];    /* RFC 4492 5.10 */
  398. #endif
  399. #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
  400.     unsigned char _pms_psk[4 + 2 * MBEDTLS_PSK_MAX_LEN];       /* RFC 4279 2 */
  401. #endif
  402. #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
  403.     unsigned char _pms_dhe_psk[4 + MBEDTLS_MPI_MAX_SIZE
  404.                                  + MBEDTLS_PSK_MAX_LEN];       /* RFC 4279 3 */
  405. #endif
  406. #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
  407.     unsigned char _pms_rsa_psk[52 + MBEDTLS_PSK_MAX_LEN];      /* RFC 4279 4 */
  408. #endif
  409. #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
  410.     unsigned char _pms_ecdhe_psk[4 + MBEDTLS_ECP_MAX_BYTES
  411.                                    + MBEDTLS_PSK_MAX_LEN];     /* RFC 5489 2 */
  412. #endif
  413. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  414.     unsigned char _pms_ecjpake[32];     /* Thread spec: SHA-256 output */
  415. #endif
  416. };
  417.  
  418. #define MBEDTLS_PREMASTER_SIZE     sizeof( union mbedtls_ssl_premaster_secret )
  419.  
  420. #ifdef __cplusplus
  421. extern "C" {
  422. #endif
  423.  
  424. /*
  425.  * SSL state machine
  426.  */
  427. typedef enum
  428. {
  429.     MBEDTLS_SSL_HELLO_REQUEST,
  430.     MBEDTLS_SSL_CLIENT_HELLO,
  431.     MBEDTLS_SSL_SERVER_HELLO,
  432.     MBEDTLS_SSL_SERVER_CERTIFICATE,
  433.     MBEDTLS_SSL_SERVER_KEY_EXCHANGE,
  434.     MBEDTLS_SSL_CERTIFICATE_REQUEST,
  435.     MBEDTLS_SSL_SERVER_HELLO_DONE,
  436.     MBEDTLS_SSL_CLIENT_CERTIFICATE,
  437.     MBEDTLS_SSL_CLIENT_KEY_EXCHANGE,
  438.     MBEDTLS_SSL_CERTIFICATE_VERIFY,
  439.     MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC,
  440.     MBEDTLS_SSL_CLIENT_FINISHED,
  441.     MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC,
  442.     MBEDTLS_SSL_SERVER_FINISHED,
  443.     MBEDTLS_SSL_FLUSH_BUFFERS,
  444.     MBEDTLS_SSL_HANDSHAKE_WRAPUP,
  445.     MBEDTLS_SSL_HANDSHAKE_OVER,
  446.     MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET,
  447.     MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT,
  448. }
  449. mbedtls_ssl_states;
  450.  
  451. /**
  452.  * \brief          Callback type: send data on the network.
  453.  *
  454.  * \note           That callback may be either blocking or non-blocking.
  455.  *
  456.  * \param ctx      Context for the send callback (typically a file descriptor)
  457.  * \param buf      Buffer holding the data to send
  458.  * \param len      Length of the data to send
  459.  *
  460.  * \return         The callback must return the number of bytes sent if any,
  461.  *                 or a non-zero error code.
  462.  *                 If performing non-blocking I/O, \c MBEDTLS_ERR_SSL_WANT_WRITE
  463.  *                 must be returned when the operation would block.
  464.  *
  465.  * \note           The callback is allowed to send fewer bytes than requested.
  466.  *                 It must always return the number of bytes actually sent.
  467.  */
  468. typedef int mbedtls_ssl_send_t( void *ctx,
  469.                                 const unsigned char *buf,
  470.                                 size_t len );
  471.  
  472. /**
  473.  * \brief          Callback type: receive data from the network.
  474.  *
  475.  * \note           That callback may be either blocking or non-blocking.
  476.  *
  477.  * \param ctx      Context for the receive callback (typically a file
  478.  *                 descriptor)
  479.  * \param buf      Buffer to write the received data to
  480.  * \param len      Length of the receive buffer
  481.  *
  482.  * \return         The callback must return the number of bytes received,
  483.  *                 or a non-zero error code.
  484.  *                 If performing non-blocking I/O, \c MBEDTLS_ERR_SSL_WANT_READ
  485.  *                 must be returned when the operation would block.
  486.  *
  487.  * \note           The callback may receive fewer bytes than the length of the
  488.  *                 buffer. It must always return the number of bytes actually
  489.  *                 received and written to the buffer.
  490.  */
  491. typedef int mbedtls_ssl_recv_t( void *ctx,
  492.                                 unsigned char *buf,
  493.                                 size_t len );
  494.  
  495. /**
  496.  * \brief          Callback type: receive data from the network, with timeout
  497.  *
  498.  * \note           That callback must block until data is received, or the
  499.  *                 timeout delay expires, or the operation is interrupted by a
  500.  *                 signal.
  501.  *
  502.  * \param ctx      Context for the receive callback (typically a file descriptor)
  503.  * \param buf      Buffer to write the received data to
  504.  * \param len      Length of the receive buffer
  505.  * \param timeout  Maximum nomber of millisecondes to wait for data
  506.  *                 0 means no timeout (potentially waiting forever)
  507.  *
  508.  * \return         The callback must return the number of bytes received,
  509.  *                 or a non-zero error code:
  510.  *                 \c MBEDTLS_ERR_SSL_TIMEOUT if the operation timed out,
  511.  *                 \c MBEDTLS_ERR_SSL_WANT_READ if interrupted by a signal.
  512.  *
  513.  * \note           The callback may receive fewer bytes than the length of the
  514.  *                 buffer. It must always return the number of bytes actually
  515.  *                 received and written to the buffer.
  516.  */
  517. typedef int mbedtls_ssl_recv_timeout_t( void *ctx,
  518.                                         unsigned char *buf,
  519.                                         size_t len,
  520.                                         uint32_t timeout );
  521. /**
  522.  * \brief          Callback type: set a pair of timers/delays to watch
  523.  *
  524.  * \param ctx      Context pointer
  525.  * \param int_ms   Intermediate delay in milliseconds
  526.  * \param fin_ms   Final delay in milliseconds
  527.  *                 0 cancels the current timer.
  528.  *
  529.  * \note           This callback must at least store the necessary information
  530.  *                 for the associated \c mbedtls_ssl_get_timer_t callback to
  531.  *                 return correct information.
  532.  *
  533.  * \note           If using a event-driven style of programming, an event must
  534.  *                 be generated when the final delay is passed. The event must
  535.  *                 cause a call to \c mbedtls_ssl_handshake() with the proper
  536.  *                 SSL context to be scheduled. Care must be taken to ensure
  537.  *                 that at most one such call happens at a time.
  538.  *
  539.  * \note           Only one timer at a time must be running. Calling this
  540.  *                 function while a timer is running must cancel it. Cancelled
  541.  *                 timers must not generate any event.
  542.  */
  543. typedef void mbedtls_ssl_set_timer_t( void * ctx,
  544.                                       uint32_t int_ms,
  545.                                       uint32_t fin_ms );
  546.  
  547. /**
  548.  * \brief          Callback type: get status of timers/delays
  549.  *
  550.  * \param ctx      Context pointer
  551.  *
  552.  * \return         This callback must return:
  553.  *                 -1 if cancelled (fin_ms == 0),
  554.  *                  0 if none of the delays have passed,
  555.  *                  1 if only the intermediate delay has passed,
  556.  *                  2 if the final delay has passed.
  557.  */
  558. typedef int mbedtls_ssl_get_timer_t( void * ctx );
  559.  
  560. /* Defined below */
  561. typedef struct mbedtls_ssl_session mbedtls_ssl_session;
  562. typedef struct mbedtls_ssl_context mbedtls_ssl_context;
  563. typedef struct mbedtls_ssl_config  mbedtls_ssl_config;
  564.  
  565. /* Defined in ssl_internal.h */
  566. typedef struct mbedtls_ssl_transform mbedtls_ssl_transform;
  567. typedef struct mbedtls_ssl_handshake_params mbedtls_ssl_handshake_params;
  568. typedef struct mbedtls_ssl_sig_hash_set_t mbedtls_ssl_sig_hash_set_t;
  569. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  570. typedef struct mbedtls_ssl_key_cert mbedtls_ssl_key_cert;
  571. #endif
  572. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  573. typedef struct mbedtls_ssl_flight_item mbedtls_ssl_flight_item;
  574. #endif
  575.  
  576. #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
  577. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  578. /**
  579.  * \brief           Callback type: start external signature operation.
  580.  *
  581.  *                  This callback is called during an SSL handshake to start
  582.  *                  a signature decryption operation using an
  583.  *                  external processor. The parameter \p cert contains
  584.  *                  the public key; it is up to the callback function to
  585.  *                  determine how to access the associated private key.
  586.  *
  587.  *                  This function typically sends or enqueues a request, and
  588.  *                  does not wait for the operation to complete. This allows
  589.  *                  the handshake step to be non-blocking.
  590.  *
  591.  *                  The parameters \p ssl and \p cert are guaranteed to remain
  592.  *                  valid throughout the handshake. On the other hand, this
  593.  *                  function must save the contents of \p hash if the value
  594.  *                  is needed for later processing, because the \p hash buffer
  595.  *                  is no longer valid after this function returns.
  596.  *
  597.  *                  This function may call mbedtls_ssl_set_async_operation_data()
  598.  *                  to store an operation context for later retrieval
  599.  *                  by the resume or cancel callback.
  600.  *
  601.  * \note            For RSA signatures, this function must produce output
  602.  *                  that is consistent with PKCS#1 v1.5 in the same way as
  603.  *                  mbedtls_rsa_pkcs1_sign(). Before the private key operation,
  604.  *                  apply the padding steps described in RFC 8017, section 9.2
  605.  *                  "EMSA-PKCS1-v1_5" as follows.
  606.  *                  - If \p md_alg is #MBEDTLS_MD_NONE, apply the PKCS#1 v1.5
  607.  *                    encoding, treating \p hash as the DigestInfo to be
  608.  *                    padded. In other words, apply EMSA-PKCS1-v1_5 starting
  609.  *                    from step 3, with `T = hash` and `tLen = hash_len`.
  610.  *                  - If `md_alg != MBEDTLS_MD_NONE`, apply the PKCS#1 v1.5
  611.  *                    encoding, treating \p hash as the hash to be encoded and
  612.  *                    padded. In other words, apply EMSA-PKCS1-v1_5 starting
  613.  *                    from step 2, with `digestAlgorithm` obtained by calling
  614.  *                    mbedtls_oid_get_oid_by_md() on \p md_alg.
  615.  *
  616.  * \note            For ECDSA signatures, the output format is the DER encoding
  617.  *                  `Ecdsa-Sig-Value` defined in
  618.  *                  [RFC 4492 section 5.4](https://tools.ietf.org/html/rfc4492#section-5.4).
  619.  *
  620.  * \param ssl             The SSL connection instance. It should not be
  621.  *                        modified other than via
  622.  *                        mbedtls_ssl_set_async_operation_data().
  623.  * \param cert            Certificate containing the public key.
  624.  *                        In simple cases, this is one of the pointers passed to
  625.  *                        mbedtls_ssl_conf_own_cert() when configuring the SSL
  626.  *                        connection. However, if other callbacks are used, this
  627.  *                        property may not hold. For example, if an SNI callback
  628.  *                        is registered with mbedtls_ssl_conf_sni(), then
  629.  *                        this callback determines what certificate is used.
  630.  * \param md_alg          Hash algorithm.
  631.  * \param hash            Buffer containing the hash. This buffer is
  632.  *                        no longer valid when the function returns.
  633.  * \param hash_len        Size of the \c hash buffer in bytes.
  634.  *
  635.  * \return          0 if the operation was started successfully and the SSL
  636.  *                  stack should call the resume callback immediately.
  637.  * \return          #MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS if the operation
  638.  *                  was started successfully and the SSL stack should return
  639.  *                  immediately without calling the resume callback yet.
  640.  * \return          #MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH if the external
  641.  *                  processor does not support this key. The SSL stack will
  642.  *                  use the private key object instead.
  643.  * \return          Any other error indicates a fatal failure and is
  644.  *                  propagated up the call chain. The callback should
  645.  *                  use \c MBEDTLS_ERR_PK_xxx error codes, and <b>must not</b>
  646.  *                  use \c MBEDTLS_ERR_SSL_xxx error codes except as
  647.  *                  directed in the documentation of this callback.
  648.  */
  649. typedef int mbedtls_ssl_async_sign_t( mbedtls_ssl_context *ssl,
  650.                                       mbedtls_x509_crt *cert,
  651.                                       mbedtls_md_type_t md_alg,
  652.                                       const unsigned char *hash,
  653.                                       size_t hash_len );
  654.  
  655. /**
  656.  * \brief           Callback type: start external decryption operation.
  657.  *
  658.  *                  This callback is called during an SSL handshake to start
  659.  *                  an RSA decryption operation using an
  660.  *                  external processor. The parameter \p cert contains
  661.  *                  the public key; it is up to the callback function to
  662.  *                  determine how to access the associated private key.
  663.  *
  664.  *                  This function typically sends or enqueues a request, and
  665.  *                  does not wait for the operation to complete. This allows
  666.  *                  the handshake step to be non-blocking.
  667.  *
  668.  *                  The parameters \p ssl and \p cert are guaranteed to remain
  669.  *                  valid throughout the handshake. On the other hand, this
  670.  *                  function must save the contents of \p input if the value
  671.  *                  is needed for later processing, because the \p input buffer
  672.  *                  is no longer valid after this function returns.
  673.  *
  674.  *                  This function may call mbedtls_ssl_set_async_operation_data()
  675.  *                  to store an operation context for later retrieval
  676.  *                  by the resume or cancel callback.
  677.  *
  678.  * \warning         RSA decryption as used in TLS is subject to a potential
  679.  *                  timing side channel attack first discovered by Bleichenbacher
  680.  *                  in 1998. This attack can be remotely exploitable
  681.  *                  in practice. To avoid this attack, you must ensure that
  682.  *                  if the callback performs an RSA decryption, the time it
  683.  *                  takes to execute and return the result does not depend
  684.  *                  on whether the RSA decryption succeeded or reported
  685.  *                  invalid padding.
  686.  *
  687.  * \param ssl             The SSL connection instance. It should not be
  688.  *                        modified other than via
  689.  *                        mbedtls_ssl_set_async_operation_data().
  690.  * \param cert            Certificate containing the public key.
  691.  *                        In simple cases, this is one of the pointers passed to
  692.  *                        mbedtls_ssl_conf_own_cert() when configuring the SSL
  693.  *                        connection. However, if other callbacks are used, this
  694.  *                        property may not hold. For example, if an SNI callback
  695.  *                        is registered with mbedtls_ssl_conf_sni(), then
  696.  *                        this callback determines what certificate is used.
  697.  * \param input           Buffer containing the input ciphertext. This buffer
  698.  *                        is no longer valid when the function returns.
  699.  * \param input_len       Size of the \p input buffer in bytes.
  700.  *
  701.  * \return          0 if the operation was started successfully and the SSL
  702.  *                  stack should call the resume callback immediately.
  703.  * \return          #MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS if the operation
  704.  *                  was started successfully and the SSL stack should return
  705.  *                  immediately without calling the resume callback yet.
  706.  * \return          #MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH if the external
  707.  *                  processor does not support this key. The SSL stack will
  708.  *                  use the private key object instead.
  709.  * \return          Any other error indicates a fatal failure and is
  710.  *                  propagated up the call chain. The callback should
  711.  *                  use \c MBEDTLS_ERR_PK_xxx error codes, and <b>must not</b>
  712.  *                  use \c MBEDTLS_ERR_SSL_xxx error codes except as
  713.  *                  directed in the documentation of this callback.
  714.  */
  715. typedef int mbedtls_ssl_async_decrypt_t( mbedtls_ssl_context *ssl,
  716.                                          mbedtls_x509_crt *cert,
  717.                                          const unsigned char *input,
  718.                                          size_t input_len );
  719. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  720.  
  721. /**
  722.  * \brief           Callback type: resume external operation.
  723.  *
  724.  *                  This callback is called during an SSL handshake to resume
  725.  *                  an external operation started by the
  726.  *                  ::mbedtls_ssl_async_sign_t or
  727.  *                  ::mbedtls_ssl_async_decrypt_t callback.
  728.  *
  729.  *                  This function typically checks the status of a pending
  730.  *                  request or causes the request queue to make progress, and
  731.  *                  does not wait for the operation to complete. This allows
  732.  *                  the handshake step to be non-blocking.
  733.  *
  734.  *                  This function may call mbedtls_ssl_get_async_operation_data()
  735.  *                  to retrieve an operation context set by the start callback.
  736.  *                  It may call mbedtls_ssl_set_async_operation_data() to modify
  737.  *                  this context.
  738.  *
  739.  *                  Note that when this function returns a status other than
  740.  *                  #MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS, it must free any
  741.  *                  resources associated with the operation.
  742.  *
  743.  * \param ssl             The SSL connection instance. It should not be
  744.  *                        modified other than via
  745.  *                        mbedtls_ssl_set_async_operation_data().
  746.  * \param output          Buffer containing the output (signature or decrypted
  747.  *                        data) on success.
  748.  * \param output_len      On success, number of bytes written to \p output.
  749.  * \param output_size     Size of the \p output buffer in bytes.
  750.  *
  751.  * \return          0 if output of the operation is available in the
  752.  *                  \p output buffer.
  753.  * \return          #MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS if the operation
  754.  *                  is still in progress. Subsequent requests for progress
  755.  *                  on the SSL connection will call the resume callback
  756.  *                  again.
  757.  * \return          Any other error means that the operation is aborted.
  758.  *                  The SSL handshake is aborted. The callback should
  759.  *                  use \c MBEDTLS_ERR_PK_xxx error codes, and <b>must not</b>
  760.  *                  use \c MBEDTLS_ERR_SSL_xxx error codes except as
  761.  *                  directed in the documentation of this callback.
  762.  */
  763. typedef int mbedtls_ssl_async_resume_t( mbedtls_ssl_context *ssl,
  764.                                         unsigned char *output,
  765.                                         size_t *output_len,
  766.                                         size_t output_size );
  767.  
  768. /**
  769.  * \brief           Callback type: cancel external operation.
  770.  *
  771.  *                  This callback is called if an SSL connection is closed
  772.  *                  while an asynchronous operation is in progress. Note that
  773.  *                  this callback is not called if the
  774.  *                  ::mbedtls_ssl_async_resume_t callback has run and has
  775.  *                  returned a value other than
  776.  *                  #MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS, since in that case
  777.  *                  the asynchronous operation has already completed.
  778.  *
  779.  *                  This function may call mbedtls_ssl_get_async_operation_data()
  780.  *                  to retrieve an operation context set by the start callback.
  781.  *
  782.  * \param ssl             The SSL connection instance. It should not be
  783.  *                        modified.
  784.  */
  785. typedef void mbedtls_ssl_async_cancel_t( mbedtls_ssl_context *ssl );
  786. #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
  787.  
  788. /*
  789.  * This structure is used for storing current session data.
  790.  */
  791. struct mbedtls_ssl_session
  792. {
  793. #if defined(MBEDTLS_HAVE_TIME)
  794.     mbedtls_time_t start;       /*!< starting time      */
  795. #endif
  796.     int ciphersuite;            /*!< chosen ciphersuite */
  797.     int compression;            /*!< chosen compression */
  798.     size_t id_len;              /*!< session id length  */
  799.     unsigned char id[32];       /*!< session identifier */
  800.     unsigned char master[48];   /*!< the master secret  */
  801.  
  802. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  803.     mbedtls_x509_crt *peer_cert;        /*!< peer X.509 cert chain */
  804. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  805.     uint32_t verify_result;          /*!<  verification result     */
  806.  
  807. #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
  808.     unsigned char *ticket;      /*!< RFC 5077 session ticket */
  809.     size_t ticket_len;          /*!< session ticket length   */
  810.     uint32_t ticket_lifetime;   /*!< ticket lifetime hint    */
  811. #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
  812.  
  813. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  814.     unsigned char mfl_code;     /*!< MaxFragmentLength negotiated by peer */
  815. #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
  816.  
  817. #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
  818.     int trunc_hmac;             /*!< flag for truncated hmac activation   */
  819. #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
  820.  
  821. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  822.     int encrypt_then_mac;       /*!< flag for EtM activation                */
  823. #endif
  824. };
  825.  
  826. /**
  827.  * SSL/TLS configuration to be shared between mbedtls_ssl_context structures.
  828.  */
  829. struct mbedtls_ssl_config
  830. {
  831.     /* Group items by size (largest first) to minimize padding overhead */
  832.  
  833.     /*
  834.      * Pointers
  835.      */
  836.  
  837.     const int *ciphersuite_list[4]; /*!< allowed ciphersuites per version   */
  838.  
  839.     /** Callback for printing debug output                                  */
  840.     void (*f_dbg)(void *, int, const char *, int, const char *);
  841.     void *p_dbg;                    /*!< context for the debug function     */
  842.  
  843.     /** Callback for getting (pseudo-)random numbers                        */
  844.     int  (*f_rng)(void *, unsigned char *, size_t);
  845.     void *p_rng;                    /*!< context for the RNG function       */
  846.  
  847.     /** Callback to retrieve a session from the cache                       */
  848.     int (*f_get_cache)(void *, mbedtls_ssl_session *);
  849.     /** Callback to store a session into the cache                          */
  850.     int (*f_set_cache)(void *, const mbedtls_ssl_session *);
  851.     void *p_cache;                  /*!< context for cache callbacks        */
  852.  
  853. #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
  854.     /** Callback for setting cert according to SNI extension                */
  855.     int (*f_sni)(void *, mbedtls_ssl_context *, const unsigned char *, size_t);
  856.     void *p_sni;                    /*!< context for SNI callback           */
  857. #endif
  858.  
  859. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  860.     /** Callback to customize X.509 certificate chain verification          */
  861.     int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
  862.     void *p_vrfy;                   /*!< context for X.509 verify calllback */
  863. #endif
  864.  
  865. #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
  866.     /** Callback to retrieve PSK key from identity                          */
  867.     int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *, size_t);
  868.     void *p_psk;                    /*!< context for PSK callback           */
  869. #endif
  870.  
  871. #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
  872.     /** Callback to create & write a cookie for ClientHello veirifcation    */
  873.     int (*f_cookie_write)( void *, unsigned char **, unsigned char *,
  874.                            const unsigned char *, size_t );
  875.     /** Callback to verify validity of a ClientHello cookie                 */
  876.     int (*f_cookie_check)( void *, const unsigned char *, size_t,
  877.                            const unsigned char *, size_t );
  878.     void *p_cookie;                 /*!< context for the cookie callbacks   */
  879. #endif
  880.  
  881. #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_SRV_C)
  882.     /** Callback to create & write a session ticket                         */
  883.     int (*f_ticket_write)( void *, const mbedtls_ssl_session *,
  884.             unsigned char *, const unsigned char *, size_t *, uint32_t * );
  885.     /** Callback to parse a session ticket into a session structure         */
  886.     int (*f_ticket_parse)( void *, mbedtls_ssl_session *, unsigned char *, size_t);
  887.     void *p_ticket;                 /*!< context for the ticket callbacks   */
  888. #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_SRV_C */
  889.  
  890. #if defined(MBEDTLS_SSL_EXPORT_KEYS)
  891.     /** Callback to export key block and master secret                      */
  892.     int (*f_export_keys)( void *, const unsigned char *,
  893.             const unsigned char *, size_t, size_t, size_t );
  894.     void *p_export_keys;            /*!< context for key export callback    */
  895. #endif
  896.  
  897. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  898.     const mbedtls_x509_crt_profile *cert_profile; /*!< verification profile */
  899.     mbedtls_ssl_key_cert *key_cert; /*!< own certificate/key pair(s)        */
  900.     mbedtls_x509_crt *ca_chain;     /*!< trusted CAs                        */
  901.     mbedtls_x509_crl *ca_crl;       /*!< trusted CAs CRLs                   */
  902. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  903.  
  904. #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
  905. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  906.     mbedtls_ssl_async_sign_t *f_async_sign_start; /*!< start asynchronous signature operation */
  907.     mbedtls_ssl_async_decrypt_t *f_async_decrypt_start; /*!< start asynchronous decryption operation */
  908. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  909.     mbedtls_ssl_async_resume_t *f_async_resume; /*!< resume asynchronous operation */
  910.     mbedtls_ssl_async_cancel_t *f_async_cancel; /*!< cancel asynchronous operation */
  911.     void *p_async_config_data; /*!< Configuration data set by mbedtls_ssl_conf_async_private_cb(). */
  912. #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
  913.  
  914. #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
  915.     const int *sig_hashes;          /*!< allowed signature hashes           */
  916. #endif
  917.  
  918. #if defined(MBEDTLS_ECP_C)
  919.     const mbedtls_ecp_group_id *curve_list; /*!< allowed curves             */
  920. #endif
  921.  
  922. #if defined(MBEDTLS_DHM_C)
  923.     mbedtls_mpi dhm_P;              /*!< prime modulus for DHM              */
  924.     mbedtls_mpi dhm_G;              /*!< generator for DHM                  */
  925. #endif
  926.  
  927. #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
  928.     unsigned char *psk;             /*!< pre-shared key. This field should
  929.                                          only be set via
  930.                                          mbedtls_ssl_conf_psk() */
  931.     size_t         psk_len;         /*!< length of the pre-shared key. This
  932.                                          field should only be set via
  933.                                          mbedtls_ssl_conf_psk() */
  934.     unsigned char *psk_identity;    /*!< identity for PSK negotiation. This
  935.                                          field should only be set via
  936.                                          mbedtls_ssl_conf_psk() */
  937.     size_t         psk_identity_len;/*!< length of identity. This field should
  938.                                          only be set via
  939.                                          mbedtls_ssl_conf_psk() */
  940. #endif
  941.  
  942. #if defined(MBEDTLS_SSL_ALPN)
  943.     const char **alpn_list;         /*!< ordered list of protocols          */
  944. #endif
  945.  
  946.     /*
  947.      * Numerical settings (int then char)
  948.      */
  949.  
  950.     uint32_t read_timeout;          /*!< timeout for mbedtls_ssl_read (ms)  */
  951.  
  952. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  953.     uint32_t hs_timeout_min;        /*!< initial value of the handshake
  954.                                          retransmission timeout (ms)        */
  955.     uint32_t hs_timeout_max;        /*!< maximum value of the handshake
  956.                                          retransmission timeout (ms)        */
  957. #endif
  958.  
  959. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  960.     int renego_max_records;         /*!< grace period for renegotiation     */
  961.     unsigned char renego_period[8]; /*!< value of the record counters
  962.                                          that triggers renegotiation        */
  963. #endif
  964.  
  965. #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
  966.     unsigned int badmac_limit;      /*!< limit of records with a bad MAC    */
  967. #endif
  968.  
  969. #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
  970.     unsigned int dhm_min_bitlen;    /*!< min. bit length of the DHM prime   */
  971. #endif
  972.  
  973.     unsigned char max_major_ver;    /*!< max. major version used            */
  974.     unsigned char max_minor_ver;    /*!< max. minor version used            */
  975.     unsigned char min_major_ver;    /*!< min. major version used            */
  976.     unsigned char min_minor_ver;    /*!< min. minor version used            */
  977.  
  978.     /*
  979.      * Flags (bitfields)
  980.      */
  981.  
  982.     unsigned int endpoint : 1;      /*!< 0: client, 1: server               */
  983.     unsigned int transport : 1;     /*!< stream (TLS) or datagram (DTLS)    */
  984.     unsigned int authmode : 2;      /*!< MBEDTLS_SSL_VERIFY_XXX             */
  985.     /* needed even with renego disabled for LEGACY_BREAK_HANDSHAKE          */
  986.     unsigned int allow_legacy_renegotiation : 2 ; /*!< MBEDTLS_LEGACY_XXX   */
  987. #if defined(MBEDTLS_ARC4_C)
  988.     unsigned int arc4_disabled : 1; /*!< blacklist RC4 ciphersuites?        */
  989. #endif
  990. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  991.     unsigned int mfl_code : 3;      /*!< desired fragment length            */
  992. #endif
  993. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  994.     unsigned int encrypt_then_mac : 1 ; /*!< negotiate encrypt-then-mac?    */
  995. #endif
  996. #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
  997.     unsigned int extended_ms : 1;   /*!< negotiate extended master secret?  */
  998. #endif
  999. #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
  1000.     unsigned int anti_replay : 1;   /*!< detect and prevent replay?         */
  1001. #endif
  1002. #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
  1003.     unsigned int cbc_record_splitting : 1;  /*!< do cbc record splitting    */
  1004. #endif
  1005. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  1006.     unsigned int disable_renegotiation : 1; /*!< disable renegotiation?     */
  1007. #endif
  1008. #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
  1009.     unsigned int trunc_hmac : 1;    /*!< negotiate truncated hmac?          */
  1010. #endif
  1011. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  1012.     unsigned int session_tickets : 1;   /*!< use session tickets?           */
  1013. #endif
  1014. #if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
  1015.     unsigned int fallback : 1;      /*!< is this a fallback?                */
  1016. #endif
  1017. #if defined(MBEDTLS_SSL_SRV_C)
  1018.     unsigned int cert_req_ca_list : 1;  /*!< enable sending CA list in
  1019.                                           Certificate Request messages?     */
  1020. #endif
  1021. };
  1022.  
  1023.  
  1024. struct mbedtls_ssl_context
  1025. {
  1026.     const mbedtls_ssl_config *conf; /*!< configuration information          */
  1027.  
  1028.     /*
  1029.      * Miscellaneous
  1030.      */
  1031.     int state;                  /*!< SSL handshake: current state     */
  1032. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  1033.     int renego_status;          /*!< Initial, in progress, pending?   */
  1034.     int renego_records_seen;    /*!< Records since renego request, or with DTLS,
  1035.                                   number of retransmissions of request if
  1036.                                   renego_max_records is < 0           */
  1037. #endif /* MBEDTLS_SSL_RENEGOTIATION */
  1038.  
  1039.     int major_ver;              /*!< equal to  MBEDTLS_SSL_MAJOR_VERSION_3    */
  1040.     int minor_ver;              /*!< either 0 (SSL3) or 1 (TLS1.0)    */
  1041.  
  1042. #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
  1043.     unsigned badmac_seen;       /*!< records with a bad MAC received    */
  1044. #endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
  1045.  
  1046.     mbedtls_ssl_send_t *f_send; /*!< Callback for network send */
  1047.     mbedtls_ssl_recv_t *f_recv; /*!< Callback for network receive */
  1048.     mbedtls_ssl_recv_timeout_t *f_recv_timeout;
  1049.                                 /*!< Callback for network receive with timeout */
  1050.  
  1051.     void *p_bio;                /*!< context for I/O operations   */
  1052.  
  1053.     /*
  1054.      * Session layer
  1055.      */
  1056.     mbedtls_ssl_session *session_in;            /*!<  current session data (in)   */
  1057.     mbedtls_ssl_session *session_out;           /*!<  current session data (out)  */
  1058.     mbedtls_ssl_session *session;               /*!<  negotiated session data     */
  1059.     mbedtls_ssl_session *session_negotiate;     /*!<  session data in negotiation */
  1060.  
  1061.     mbedtls_ssl_handshake_params *handshake;    /*!<  params required only during
  1062.                                               the handshake process        */
  1063.  
  1064.     /*
  1065.      * Record layer transformations
  1066.      */
  1067.     mbedtls_ssl_transform *transform_in;        /*!<  current transform params (in)   */
  1068.     mbedtls_ssl_transform *transform_out;       /*!<  current transform params (in)   */
  1069.     mbedtls_ssl_transform *transform;           /*!<  negotiated transform params     */
  1070.     mbedtls_ssl_transform *transform_negotiate; /*!<  transform params in negotiation */
  1071.  
  1072.     /*
  1073.      * Timers
  1074.      */
  1075.     void *p_timer;              /*!< context for the timer callbacks */
  1076.  
  1077.     mbedtls_ssl_set_timer_t *f_set_timer;       /*!< set timer callback */
  1078.     mbedtls_ssl_get_timer_t *f_get_timer;       /*!< get timer callback */
  1079.  
  1080.     /*
  1081.      * Record layer (incoming data)
  1082.      */
  1083.     unsigned char *in_buf;      /*!< input buffer                     */
  1084.     unsigned char *in_ctr;      /*!< 64-bit incoming message counter
  1085.                                      TLS: maintained by us
  1086.                                      DTLS: read from peer             */
  1087.     unsigned char *in_hdr;      /*!< start of record header           */
  1088.     unsigned char *in_len;      /*!< two-bytes message length field   */
  1089.     unsigned char *in_iv;       /*!< ivlen-byte IV                    */
  1090.     unsigned char *in_msg;      /*!< message contents (in_iv+ivlen)   */
  1091.     unsigned char *in_offt;     /*!< read offset in application data  */
  1092.  
  1093.     int in_msgtype;             /*!< record header: message type      */
  1094.     size_t in_msglen;           /*!< record header: message length    */
  1095.     size_t in_left;             /*!< amount of data read so far       */
  1096. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  1097.     uint16_t in_epoch;          /*!< DTLS epoch for incoming records  */
  1098.     size_t next_record_offset;  /*!< offset of the next record in datagram
  1099.                                      (equal to in_left if none)       */
  1100. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  1101. #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
  1102.     uint64_t in_window_top;     /*!< last validated record seq_num    */
  1103.     uint64_t in_window;         /*!< bitmask for replay detection     */
  1104. #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
  1105.  
  1106.     size_t in_hslen;            /*!< current handshake message length,
  1107.                                      including the handshake header   */
  1108.     int nb_zero;                /*!< # of 0-length encrypted messages */
  1109.  
  1110.     int keep_current_message;   /*!< drop or reuse current message
  1111.                                      on next call to record layer? */
  1112.  
  1113. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  1114.     uint8_t disable_datagram_packing;  /*!< Disable packing multiple records
  1115.                                         *   within a single datagram.  */
  1116. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  1117.  
  1118.     /*
  1119.      * Record layer (outgoing data)
  1120.      */
  1121.     unsigned char *out_buf;     /*!< output buffer                    */
  1122.     unsigned char *out_ctr;     /*!< 64-bit outgoing message counter  */
  1123.     unsigned char *out_hdr;     /*!< start of record header           */
  1124.     unsigned char *out_len;     /*!< two-bytes message length field   */
  1125.     unsigned char *out_iv;      /*!< ivlen-byte IV                    */
  1126.     unsigned char *out_msg;     /*!< message contents (out_iv+ivlen)  */
  1127.  
  1128.     int out_msgtype;            /*!< record header: message type      */
  1129.     size_t out_msglen;          /*!< record header: message length    */
  1130.     size_t out_left;            /*!< amount of data not yet written   */
  1131.  
  1132.     unsigned char cur_out_ctr[8]; /*!<  Outgoing record sequence  number. */
  1133.  
  1134. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  1135.     uint16_t mtu;               /*!< path mtu, used to fragment outgoing messages */
  1136. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  1137.  
  1138. #if defined(MBEDTLS_ZLIB_SUPPORT)
  1139.     unsigned char *compress_buf;        /*!<  zlib data buffer        */
  1140. #endif /* MBEDTLS_ZLIB_SUPPORT */
  1141. #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
  1142.     signed char split_done;     /*!< current record already splitted? */
  1143. #endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
  1144.  
  1145.     /*
  1146.      * PKI layer
  1147.      */
  1148.     int client_auth;                    /*!<  flag for client auth.   */
  1149.  
  1150.     /*
  1151.      * User settings
  1152.      */
  1153. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  1154.     char *hostname;             /*!< expected peer CN for verification
  1155.                                      (and SNI if available)                 */
  1156. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  1157.  
  1158. #if defined(MBEDTLS_SSL_ALPN)
  1159.     const char *alpn_chosen;    /*!<  negotiated protocol                   */
  1160. #endif /* MBEDTLS_SSL_ALPN */
  1161.  
  1162.     /*
  1163.      * Information for DTLS hello verify
  1164.      */
  1165. #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
  1166.     unsigned char  *cli_id;         /*!<  transport-level ID of the client  */
  1167.     size_t          cli_id_len;     /*!<  length of cli_id                  */
  1168. #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
  1169.  
  1170.     /*
  1171.      * Secure renegotiation
  1172.      */
  1173.     /* needed to know when to send extension on server */
  1174.     int secure_renegotiation;           /*!<  does peer support legacy or
  1175.                                               secure renegotiation           */
  1176. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  1177.     size_t verify_data_len;             /*!<  length of verify data stored   */
  1178.     char own_verify_data[MBEDTLS_SSL_VERIFY_DATA_MAX_LEN]; /*!<  previous handshake verify data */
  1179.     char peer_verify_data[MBEDTLS_SSL_VERIFY_DATA_MAX_LEN]; /*!<  previous handshake verify data */
  1180. #endif /* MBEDTLS_SSL_RENEGOTIATION */
  1181. };
  1182.  
  1183. #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
  1184.  
  1185. #define MBEDTLS_SSL_CHANNEL_OUTBOUND    0
  1186. #define MBEDTLS_SSL_CHANNEL_INBOUND     1
  1187.  
  1188. extern int (*mbedtls_ssl_hw_record_init)(mbedtls_ssl_context *ssl,
  1189.                 const unsigned char *key_enc, const unsigned char *key_dec,
  1190.                 size_t keylen,
  1191.                 const unsigned char *iv_enc,  const unsigned char *iv_dec,
  1192.                 size_t ivlen,
  1193.                 const unsigned char *mac_enc, const unsigned char *mac_dec,
  1194.                 size_t maclen);
  1195. extern int (*mbedtls_ssl_hw_record_activate)(mbedtls_ssl_context *ssl, int direction);
  1196. extern int (*mbedtls_ssl_hw_record_reset)(mbedtls_ssl_context *ssl);
  1197. extern int (*mbedtls_ssl_hw_record_write)(mbedtls_ssl_context *ssl);
  1198. extern int (*mbedtls_ssl_hw_record_read)(mbedtls_ssl_context *ssl);
  1199. extern int (*mbedtls_ssl_hw_record_finish)(mbedtls_ssl_context *ssl);
  1200. #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
  1201.  
  1202. /**
  1203.  * \brief               Return the name of the ciphersuite associated with the
  1204.  *                      given ID
  1205.  *
  1206.  * \param ciphersuite_id SSL ciphersuite ID
  1207.  *
  1208.  * \return              a string containing the ciphersuite name
  1209.  */
  1210. const char *mbedtls_ssl_get_ciphersuite_name( const int ciphersuite_id );
  1211.  
  1212. /**
  1213.  * \brief               Return the ID of the ciphersuite associated with the
  1214.  *                      given name
  1215.  *
  1216.  * \param ciphersuite_name SSL ciphersuite name
  1217.  *
  1218.  * \return              the ID with the ciphersuite or 0 if not found
  1219.  */
  1220. int mbedtls_ssl_get_ciphersuite_id( const char *ciphersuite_name );
  1221.  
  1222. /**
  1223.  * \brief          Initialize an SSL context
  1224.  *                 Just makes the context ready for mbedtls_ssl_setup() or
  1225.  *                 mbedtls_ssl_free()
  1226.  *
  1227.  * \param ssl      SSL context
  1228.  */
  1229. void mbedtls_ssl_init( mbedtls_ssl_context *ssl );
  1230.  
  1231. /**
  1232.  * \brief          Set up an SSL context for use
  1233.  *
  1234.  * \note           No copy of the configuration context is made, it can be
  1235.  *                 shared by many mbedtls_ssl_context structures.
  1236.  *
  1237.  * \warning        The conf structure will be accessed during the session.
  1238.  *                 It must not be modified or freed as long as the session
  1239.  *                 is active.
  1240.  *
  1241.  * \warning        This function must be called exactly once per context.
  1242.  *                 Calling mbedtls_ssl_setup again is not supported, even
  1243.  *                 if no session is active.
  1244.  *
  1245.  * \param ssl      SSL context
  1246.  * \param conf     SSL configuration to use
  1247.  *
  1248.  * \return         0 if successful, or MBEDTLS_ERR_SSL_ALLOC_FAILED if
  1249.  *                 memory allocation failed
  1250.  */
  1251. int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
  1252.                        const mbedtls_ssl_config *conf );
  1253.  
  1254. /**
  1255.  * \brief          Reset an already initialized SSL context for re-use
  1256.  *                 while retaining application-set variables, function
  1257.  *                 pointers and data.
  1258.  *
  1259.  * \param ssl      SSL context
  1260.  * \return         0 if successful, or MBEDTLS_ERR_SSL_ALLOC_FAILED,
  1261.                    MBEDTLS_ERR_SSL_HW_ACCEL_FAILED or
  1262.  *                 MBEDTLS_ERR_SSL_COMPRESSION_FAILED
  1263.  */
  1264. int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl );
  1265.  
  1266. /**
  1267.  * \brief          Set the current endpoint type
  1268.  *
  1269.  * \param conf     SSL configuration
  1270.  * \param endpoint must be MBEDTLS_SSL_IS_CLIENT or MBEDTLS_SSL_IS_SERVER
  1271.  */
  1272. void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint );
  1273.  
  1274. /**
  1275.  * \brief           Set the transport type (TLS or DTLS).
  1276.  *                  Default: TLS
  1277.  *
  1278.  * \note            For DTLS, you must either provide a recv callback that
  1279.  *                  doesn't block, or one that handles timeouts, see
  1280.  *                  \c mbedtls_ssl_set_bio(). You also need to provide timer
  1281.  *                  callbacks with \c mbedtls_ssl_set_timer_cb().
  1282.  *
  1283.  * \param conf      SSL configuration
  1284.  * \param transport transport type:
  1285.  *                  MBEDTLS_SSL_TRANSPORT_STREAM for TLS,
  1286.  *                  MBEDTLS_SSL_TRANSPORT_DATAGRAM for DTLS.
  1287.  */
  1288. void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport );
  1289.  
  1290. /**
  1291.  * \brief          Set the certificate verification mode
  1292.  *                 Default: NONE on server, REQUIRED on client
  1293.  *
  1294.  * \param conf     SSL configuration
  1295.  * \param authmode can be:
  1296.  *
  1297.  *  MBEDTLS_SSL_VERIFY_NONE:      peer certificate is not checked
  1298.  *                        (default on server)
  1299.  *                        (insecure on client)
  1300.  *
  1301.  *  MBEDTLS_SSL_VERIFY_OPTIONAL:  peer certificate is checked, however the
  1302.  *                        handshake continues even if verification failed;
  1303.  *                        mbedtls_ssl_get_verify_result() can be called after the
  1304.  *                        handshake is complete.
  1305.  *
  1306.  *  MBEDTLS_SSL_VERIFY_REQUIRED:  peer *must* present a valid certificate,
  1307.  *                        handshake is aborted if verification failed.
  1308.  *                        (default on client)
  1309.  *
  1310.  * \note On client, MBEDTLS_SSL_VERIFY_REQUIRED is the recommended mode.
  1311.  * With MBEDTLS_SSL_VERIFY_OPTIONAL, the user needs to call mbedtls_ssl_get_verify_result() at
  1312.  * the right time(s), which may not be obvious, while REQUIRED always perform
  1313.  * the verification as soon as possible. For example, REQUIRED was protecting
  1314.  * against the "triple handshake" attack even before it was found.
  1315.  */
  1316. void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode );
  1317.  
  1318. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  1319. /**
  1320.  * \brief          Set the verification callback (Optional).
  1321.  *
  1322.  *                 If set, the verify callback is called for each
  1323.  *                 certificate in the chain. For implementation
  1324.  *                 information, please see \c mbedtls_x509_crt_verify()
  1325.  *
  1326.  * \param conf     SSL configuration
  1327.  * \param f_vrfy   verification function
  1328.  * \param p_vrfy   verification parameter
  1329.  */
  1330. void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
  1331.                      int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
  1332.                      void *p_vrfy );
  1333. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  1334.  
  1335. /**
  1336.  * \brief          Set the random number generator callback
  1337.  *
  1338.  * \param conf     SSL configuration
  1339.  * \param f_rng    RNG function
  1340.  * \param p_rng    RNG parameter
  1341.  */
  1342. void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
  1343.                   int (*f_rng)(void *, unsigned char *, size_t),
  1344.                   void *p_rng );
  1345.  
  1346. /**
  1347.  * \brief          Set the debug callback
  1348.  *
  1349.  *                 The callback has the following argument:
  1350.  *                 void *           opaque context for the callback
  1351.  *                 int              debug level
  1352.  *                 const char *     file name
  1353.  *                 int              line number
  1354.  *                 const char *     message
  1355.  *
  1356.  * \param conf     SSL configuration
  1357.  * \param f_dbg    debug function
  1358.  * \param p_dbg    debug parameter
  1359.  */
  1360. void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
  1361.                   void (*f_dbg)(void *, int, const char *, int, const char *),
  1362.                   void  *p_dbg );
  1363.  
  1364. /**
  1365.  * \brief          Set the underlying BIO callbacks for write, read and
  1366.  *                 read-with-timeout.
  1367.  *
  1368.  * \param ssl      SSL context
  1369.  * \param p_bio    parameter (context) shared by BIO callbacks
  1370.  * \param f_send   write callback
  1371.  * \param f_recv   read callback
  1372.  * \param f_recv_timeout blocking read callback with timeout.
  1373.  *
  1374.  * \note           One of f_recv or f_recv_timeout can be NULL, in which case
  1375.  *                 the other is used. If both are non-NULL, f_recv_timeout is
  1376.  *                 used and f_recv is ignored (as if it were NULL).
  1377.  *
  1378.  * \note           The two most common use cases are:
  1379.  *                 - non-blocking I/O, f_recv != NULL, f_recv_timeout == NULL
  1380.  *                 - blocking I/O, f_recv == NULL, f_recv_timout != NULL
  1381.  *
  1382.  * \note           For DTLS, you need to provide either a non-NULL
  1383.  *                 f_recv_timeout callback, or a f_recv that doesn't block.
  1384.  *
  1385.  * \note           See the documentations of \c mbedtls_ssl_sent_t,
  1386.  *                 \c mbedtls_ssl_recv_t and \c mbedtls_ssl_recv_timeout_t for
  1387.  *                 the conventions those callbacks must follow.
  1388.  *
  1389.  * \note           On some platforms, net_sockets.c provides
  1390.  *                 \c mbedtls_net_send(), \c mbedtls_net_recv() and
  1391.  *                 \c mbedtls_net_recv_timeout() that are suitable to be used
  1392.  *                 here.
  1393.  */
  1394. void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
  1395.                           void *p_bio,
  1396.                           mbedtls_ssl_send_t *f_send,
  1397.                           mbedtls_ssl_recv_t *f_recv,
  1398.                           mbedtls_ssl_recv_timeout_t *f_recv_timeout );
  1399.  
  1400. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  1401. /**
  1402.  * \brief          Set the Maximum Tranport Unit (MTU).
  1403.  *                 Special value: 0 means unset (no limit).
  1404.  *                 This represents the maximum size of a datagram payload
  1405.  *                 handled by the transport layer (usually UDP) as determined
  1406.  *                 by the network link and stack. In practice, this controls
  1407.  *                 the maximum size datagram the DTLS layer will pass to the
  1408.  *                 \c f_send() callback set using \c mbedtls_ssl_set_bio().
  1409.  *
  1410.  * \note           The limit on datagram size is converted to a limit on
  1411.  *                 record payload by subtracting the current overhead of
  1412.  *                 encapsulation and encryption/authentication if any.
  1413.  *
  1414.  * \note           This can be called at any point during the connection, for
  1415.  *                 example when a Path Maximum Transfer Unit (PMTU)
  1416.  *                 estimate becomes available from other sources,
  1417.  *                 such as lower (or higher) protocol layers.
  1418.  *
  1419.  * \note           This setting only controls the size of the packets we send,
  1420.  *                 and does not restrict the size of the datagrams we're
  1421.  *                 willing to receive. Client-side, you can request the
  1422.  *                 server to use smaller records with \c
  1423.  *                 mbedtls_ssl_conf_max_frag_len().
  1424.  *
  1425.  * \note           If both a MTU and a maximum fragment length have been
  1426.  *                 configured (or negotiated with the peer), the resulting
  1427.  *                 lower limit on record payload (see first note) is used.
  1428.  *
  1429.  * \note           This can only be used to decrease the maximum size
  1430.  *                 of datagrams (hence records, see first note) sent. It
  1431.  *                 cannot be used to increase the maximum size of records over
  1432.  *                 the limit set by #MBEDTLS_SSL_OUT_CONTENT_LEN.
  1433.  *
  1434.  * \note           Values lower than the current record layer expansion will
  1435.  *                 result in an error when trying to send data.
  1436.  *
  1437.  * \note           Using record compression together with a non-zero MTU value
  1438.  *                 will result in an error when trying to send data.
  1439.  *
  1440.  * \param ssl      SSL context
  1441.  * \param mtu      Value of the path MTU in bytes
  1442.  */
  1443. void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu );
  1444. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  1445.  
  1446. /**
  1447.  * \brief          Set the timeout period for mbedtls_ssl_read()
  1448.  *                 (Default: no timeout.)
  1449.  *
  1450.  * \param conf     SSL configuration context
  1451.  * \param timeout  Timeout value in milliseconds.
  1452.  *                 Use 0 for no timeout (default).
  1453.  *
  1454.  * \note           With blocking I/O, this will only work if a non-NULL
  1455.  *                 \c f_recv_timeout was set with \c mbedtls_ssl_set_bio().
  1456.  *                 With non-blocking I/O, this will only work if timer
  1457.  *                 callbacks were set with \c mbedtls_ssl_set_timer_cb().
  1458.  *
  1459.  * \note           With non-blocking I/O, you may also skip this function
  1460.  *                 altogether and handle timeouts at the application layer.
  1461.  */
  1462. void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout );
  1463.  
  1464. /**
  1465.  * \brief          Set the timer callbacks (Mandatory for DTLS.)
  1466.  *
  1467.  * \param ssl      SSL context
  1468.  * \param p_timer  parameter (context) shared by timer callbacks
  1469.  * \param f_set_timer   set timer callback
  1470.  * \param f_get_timer   get timer callback. Must return:
  1471.  *
  1472.  * \note           See the documentation of \c mbedtls_ssl_set_timer_t and
  1473.  *                 \c mbedtls_ssl_get_timer_t for the conventions this pair of
  1474.  *                 callbacks must follow.
  1475.  *
  1476.  * \note           On some platforms, timing.c provides
  1477.  *                 \c mbedtls_timing_set_delay() and
  1478.  *                 \c mbedtls_timing_get_delay() that are suitable for using
  1479.  *                 here, except if using an event-driven style.
  1480.  *
  1481.  * \note           See also the "DTLS tutorial" article in our knowledge base.
  1482.  *                 https://tls.mbed.org/kb/how-to/dtls-tutorial
  1483.  */
  1484. void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
  1485.                                void *p_timer,
  1486.                                mbedtls_ssl_set_timer_t *f_set_timer,
  1487.                                mbedtls_ssl_get_timer_t *f_get_timer );
  1488.  
  1489. /**
  1490.  * \brief           Callback type: generate and write session ticket
  1491.  *
  1492.  * \note            This describes what a callback implementation should do.
  1493.  *                  This callback should generate an encrypted and
  1494.  *                  authenticated ticket for the session and write it to the
  1495.  *                  output buffer. Here, ticket means the opaque ticket part
  1496.  *                  of the NewSessionTicket structure of RFC 5077.
  1497.  *
  1498.  * \param p_ticket  Context for the callback
  1499.  * \param session   SSL session to be written in the ticket
  1500.  * \param start     Start of the output buffer
  1501.  * \param end       End of the output buffer
  1502.  * \param tlen      On exit, holds the length written
  1503.  * \param lifetime  On exit, holds the lifetime of the ticket in seconds
  1504.  *
  1505.  * \return          0 if successful, or
  1506.  *                  a specific MBEDTLS_ERR_XXX code.
  1507.  */
  1508. typedef int mbedtls_ssl_ticket_write_t( void *p_ticket,
  1509.                                         const mbedtls_ssl_session *session,
  1510.                                         unsigned char *start,
  1511.                                         const unsigned char *end,
  1512.                                         size_t *tlen,
  1513.                                         uint32_t *lifetime );
  1514.  
  1515. #if defined(MBEDTLS_SSL_EXPORT_KEYS)
  1516. /**
  1517.  * \brief           Callback type: Export key block and master secret
  1518.  *
  1519.  * \note            This is required for certain uses of TLS, e.g. EAP-TLS
  1520.  *                  (RFC 5216) and Thread. The key pointers are ephemeral and
  1521.  *                  therefore must not be stored. The master secret and keys
  1522.  *                  should not be used directly except as an input to a key
  1523.  *                  derivation function.
  1524.  *
  1525.  * \param p_expkey  Context for the callback
  1526.  * \param ms        Pointer to master secret (fixed length: 48 bytes)
  1527.  * \param kb        Pointer to key block, see RFC 5246 section 6.3
  1528.  *                  (variable length: 2 * maclen + 2 * keylen + 2 * ivlen).
  1529.  * \param maclen    MAC length
  1530.  * \param keylen    Key length
  1531.  * \param ivlen     IV length
  1532.  *
  1533.  * \return          0 if successful, or
  1534.  *                  a specific MBEDTLS_ERR_XXX code.
  1535.  */
  1536. typedef int mbedtls_ssl_export_keys_t( void *p_expkey,
  1537.                                 const unsigned char *ms,
  1538.                                 const unsigned char *kb,
  1539.                                 size_t maclen,
  1540.                                 size_t keylen,
  1541.                                 size_t ivlen );
  1542. #endif /* MBEDTLS_SSL_EXPORT_KEYS */
  1543.  
  1544. /**
  1545.  * \brief           Callback type: parse and load session ticket
  1546.  *
  1547.  * \note            This describes what a callback implementation should do.
  1548.  *                  This callback should parse a session ticket as generated
  1549.  *                  by the corresponding mbedtls_ssl_ticket_write_t function,
  1550.  *                  and, if the ticket is authentic and valid, load the
  1551.  *                  session.
  1552.  *
  1553.  * \note            The implementation is allowed to modify the first len
  1554.  *                  bytes of the input buffer, eg to use it as a temporary
  1555.  *                  area for the decrypted ticket contents.
  1556.  *
  1557.  * \param p_ticket  Context for the callback
  1558.  * \param session   SSL session to be loaded
  1559.  * \param buf       Start of the buffer containing the ticket
  1560.  * \param len       Length of the ticket.
  1561.  *
  1562.  * \return          0 if successful, or
  1563.  *                  MBEDTLS_ERR_SSL_INVALID_MAC if not authentic, or
  1564.  *                  MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED if expired, or
  1565.  *                  any other non-zero code for other failures.
  1566.  */
  1567. typedef int mbedtls_ssl_ticket_parse_t( void *p_ticket,
  1568.                                         mbedtls_ssl_session *session,
  1569.                                         unsigned char *buf,
  1570.                                         size_t len );
  1571.  
  1572. #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_SRV_C)
  1573. /**
  1574.  * \brief           Configure SSL session ticket callbacks (server only).
  1575.  *                  (Default: none.)
  1576.  *
  1577.  * \note            On server, session tickets are enabled by providing
  1578.  *                  non-NULL callbacks.
  1579.  *
  1580.  * \note            On client, use \c mbedtls_ssl_conf_session_tickets().
  1581.  *
  1582.  * \param conf      SSL configuration context
  1583.  * \param f_ticket_write    Callback for writing a ticket
  1584.  * \param f_ticket_parse    Callback for parsing a ticket
  1585.  * \param p_ticket          Context shared by the two callbacks
  1586.  */
  1587. void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
  1588.         mbedtls_ssl_ticket_write_t *f_ticket_write,
  1589.         mbedtls_ssl_ticket_parse_t *f_ticket_parse,
  1590.         void *p_ticket );
  1591. #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_SRV_C */
  1592.  
  1593. #if defined(MBEDTLS_SSL_EXPORT_KEYS)
  1594. /**
  1595.  * \brief           Configure key export callback.
  1596.  *                  (Default: none.)
  1597.  *
  1598.  * \note            See \c mbedtls_ssl_export_keys_t.
  1599.  *
  1600.  * \param conf      SSL configuration context
  1601.  * \param f_export_keys     Callback for exporting keys
  1602.  * \param p_export_keys     Context for the callback
  1603.  */
  1604. void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
  1605.         mbedtls_ssl_export_keys_t *f_export_keys,
  1606.         void *p_export_keys );
  1607. #endif /* MBEDTLS_SSL_EXPORT_KEYS */
  1608.  
  1609. #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
  1610. /**
  1611.  * \brief           Configure asynchronous private key operation callbacks.
  1612.  *
  1613.  * \param conf              SSL configuration context
  1614.  * \param f_async_sign      Callback to start a signature operation. See
  1615.  *                          the description of ::mbedtls_ssl_async_sign_t
  1616.  *                          for more information. This may be \c NULL if the
  1617.  *                          external processor does not support any signature
  1618.  *                          operation; in this case the private key object
  1619.  *                          associated with the certificate will be used.
  1620.  * \param f_async_decrypt   Callback to start a decryption operation. See
  1621.  *                          the description of ::mbedtls_ssl_async_decrypt_t
  1622.  *                          for more information. This may be \c NULL if the
  1623.  *                          external processor does not support any decryption
  1624.  *                          operation; in this case the private key object
  1625.  *                          associated with the certificate will be used.
  1626.  * \param f_async_resume    Callback to resume an asynchronous operation. See
  1627.  *                          the description of ::mbedtls_ssl_async_resume_t
  1628.  *                          for more information. This may not be \c NULL unless
  1629.  *                          \p f_async_sign and \p f_async_decrypt are both
  1630.  *                          \c NULL.
  1631.  * \param f_async_cancel    Callback to cancel an asynchronous operation. See
  1632.  *                          the description of ::mbedtls_ssl_async_cancel_t
  1633.  *                          for more information. This may be \c NULL if
  1634.  *                          no cleanup is needed.
  1635.  * \param config_data       A pointer to configuration data which can be
  1636.  *                          retrieved with
  1637.  *                          mbedtls_ssl_conf_get_async_config_data(). The
  1638.  *                          library stores this value without dereferencing it.
  1639.  */
  1640. void mbedtls_ssl_conf_async_private_cb( mbedtls_ssl_config *conf,
  1641.                                         mbedtls_ssl_async_sign_t *f_async_sign,
  1642.                                         mbedtls_ssl_async_decrypt_t *f_async_decrypt,
  1643.                                         mbedtls_ssl_async_resume_t *f_async_resume,
  1644.                                         mbedtls_ssl_async_cancel_t *f_async_cancel,
  1645.                                         void *config_data );
  1646.  
  1647. /**
  1648.  * \brief           Retrieve the configuration data set by
  1649.  *                  mbedtls_ssl_conf_async_private_cb().
  1650.  *
  1651.  * \param conf      SSL configuration context
  1652.  * \return          The configuration data set by
  1653.  *                  mbedtls_ssl_conf_async_private_cb().
  1654.  */
  1655. void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf );
  1656.  
  1657. /**
  1658.  * \brief           Retrieve the asynchronous operation user context.
  1659.  *
  1660.  * \note            This function may only be called while a handshake
  1661.  *                  is in progress.
  1662.  *
  1663.  * \param ssl       The SSL context to access.
  1664.  *
  1665.  * \return          The asynchronous operation user context that was last
  1666.  *                  set during the current handshake. If
  1667.  *                  mbedtls_ssl_set_async_operation_data() has not yet been
  1668.  *                  called during the current handshake, this function returns
  1669.  *                  \c NULL.
  1670.  */
  1671. void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl );
  1672.  
  1673. /**
  1674.  * \brief           Retrieve the asynchronous operation user context.
  1675.  *
  1676.  * \note            This function may only be called while a handshake
  1677.  *                  is in progress.
  1678.  *
  1679.  * \param ssl       The SSL context to access.
  1680.  * \param ctx       The new value of the asynchronous operation user context.
  1681.  *                  Call mbedtls_ssl_get_async_operation_data() later during the
  1682.  *                  same handshake to retrieve this value.
  1683.  */
  1684. void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
  1685.                                  void *ctx );
  1686. #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
  1687.  
  1688. /**
  1689.  * \brief          Callback type: generate a cookie
  1690.  *
  1691.  * \param ctx      Context for the callback
  1692.  * \param p        Buffer to write to,
  1693.  *                 must be updated to point right after the cookie
  1694.  * \param end      Pointer to one past the end of the output buffer
  1695.  * \param info     Client ID info that was passed to
  1696.  *                 \c mbedtls_ssl_set_client_transport_id()
  1697.  * \param ilen     Length of info in bytes
  1698.  *
  1699.  * \return         The callback must return 0 on success,
  1700.  *                 or a negative error code.
  1701.  */
  1702. typedef int mbedtls_ssl_cookie_write_t( void *ctx,
  1703.                                 unsigned char **p, unsigned char *end,
  1704.                                 const unsigned char *info, size_t ilen );
  1705.  
  1706. /**
  1707.  * \brief          Callback type: verify a cookie
  1708.  *
  1709.  * \param ctx      Context for the callback
  1710.  * \param cookie   Cookie to verify
  1711.  * \param clen     Length of cookie
  1712.  * \param info     Client ID info that was passed to
  1713.  *                 \c mbedtls_ssl_set_client_transport_id()
  1714.  * \param ilen     Length of info in bytes
  1715.  *
  1716.  * \return         The callback must return 0 if cookie is valid,
  1717.  *                 or a negative error code.
  1718.  */
  1719. typedef int mbedtls_ssl_cookie_check_t( void *ctx,
  1720.                                 const unsigned char *cookie, size_t clen,
  1721.                                 const unsigned char *info, size_t ilen );
  1722.  
  1723. #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
  1724. /**
  1725.  * \brief           Register callbacks for DTLS cookies
  1726.  *                  (Server only. DTLS only.)
  1727.  *
  1728.  *                  Default: dummy callbacks that fail, in order to force you to
  1729.  *                  register working callbacks (and initialize their context).
  1730.  *
  1731.  *                  To disable HelloVerifyRequest, register NULL callbacks.
  1732.  *
  1733.  * \warning         Disabling hello verification allows your server to be used
  1734.  *                  for amplification in DoS attacks against other hosts.
  1735.  *                  Only disable if you known this can't happen in your
  1736.  *                  particular environment.
  1737.  *
  1738.  * \note            See comments on \c mbedtls_ssl_handshake() about handling
  1739.  *                  the MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED that is expected
  1740.  *                  on the first handshake attempt when this is enabled.
  1741.  *
  1742.  * \note            This is also necessary to handle client reconnection from
  1743.  *                  the same port as described in RFC 6347 section 4.2.8 (only
  1744.  *                  the variant with cookies is supported currently). See
  1745.  *                  comments on \c mbedtls_ssl_read() for details.
  1746.  *
  1747.  * \param conf              SSL configuration
  1748.  * \param f_cookie_write    Cookie write callback
  1749.  * \param f_cookie_check    Cookie check callback
  1750.  * \param p_cookie          Context for both callbacks
  1751.  */
  1752. void mbedtls_ssl_conf_dtls_cookies( mbedtls_ssl_config *conf,
  1753.                            mbedtls_ssl_cookie_write_t *f_cookie_write,
  1754.                            mbedtls_ssl_cookie_check_t *f_cookie_check,
  1755.                            void *p_cookie );
  1756.  
  1757. /**
  1758.  * \brief          Set client's transport-level identification info.
  1759.  *                 (Server only. DTLS only.)
  1760.  *
  1761.  *                 This is usually the IP address (and port), but could be
  1762.  *                 anything identify the client depending on the underlying
  1763.  *                 network stack. Used for HelloVerifyRequest with DTLS.
  1764.  *                 This is *not* used to route the actual packets.
  1765.  *
  1766.  * \param ssl      SSL context
  1767.  * \param info     Transport-level info identifying the client (eg IP + port)
  1768.  * \param ilen     Length of info in bytes
  1769.  *
  1770.  * \note           An internal copy is made, so the info buffer can be reused.
  1771.  *
  1772.  * \return         0 on success,
  1773.  *                 MBEDTLS_ERR_SSL_BAD_INPUT_DATA if used on client,
  1774.  *                 MBEDTLS_ERR_SSL_ALLOC_FAILED if out of memory.
  1775.  */
  1776. int mbedtls_ssl_set_client_transport_id( mbedtls_ssl_context *ssl,
  1777.                                  const unsigned char *info,
  1778.                                  size_t ilen );
  1779.  
  1780. #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
  1781.  
  1782. #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
  1783. /**
  1784.  * \brief          Enable or disable anti-replay protection for DTLS.
  1785.  *                 (DTLS only, no effect on TLS.)
  1786.  *                 Default: enabled.
  1787.  *
  1788.  * \param conf     SSL configuration
  1789.  * \param mode     MBEDTLS_SSL_ANTI_REPLAY_ENABLED or MBEDTLS_SSL_ANTI_REPLAY_DISABLED.
  1790.  *
  1791.  * \warning        Disabling this is a security risk unless the application
  1792.  *                 protocol handles duplicated packets in a safe way. You
  1793.  *                 should not disable this without careful consideration.
  1794.  *                 However, if your application already detects duplicated
  1795.  *                 packets and needs information about them to adjust its
  1796.  *                 transmission strategy, then you'll want to disable this.
  1797.  */
  1798. void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode );
  1799. #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
  1800.  
  1801. #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
  1802. /**
  1803.  * \brief          Set a limit on the number of records with a bad MAC
  1804.  *                 before terminating the connection.
  1805.  *                 (DTLS only, no effect on TLS.)
  1806.  *                 Default: 0 (disabled).
  1807.  *
  1808.  * \param conf     SSL configuration
  1809.  * \param limit    Limit, or 0 to disable.
  1810.  *
  1811.  * \note           If the limit is N, then the connection is terminated when
  1812.  *                 the Nth non-authentic record is seen.
  1813.  *
  1814.  * \note           Records with an invalid header are not counted, only the
  1815.  *                 ones going through the authentication-decryption phase.
  1816.  *
  1817.  * \note           This is a security trade-off related to the fact that it's
  1818.  *                 often relatively easy for an active attacker ot inject UDP
  1819.  *                 datagrams. On one hand, setting a low limit here makes it
  1820.  *                 easier for such an attacker to forcibly terminated a
  1821.  *                 connection. On the other hand, a high limit or no limit
  1822.  *                 might make us waste resources checking authentication on
  1823.  *                 many bogus packets.
  1824.  */
  1825. void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit );
  1826. #endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
  1827.  
  1828. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  1829.  
  1830. /**
  1831.  * \brief          Allow or disallow packing of multiple handshake records
  1832.  *                 within a single datagram.
  1833.  *
  1834.  * \param ssl           The SSL context to configure.
  1835.  * \param allow_packing This determines whether datagram packing may
  1836.  *                      be used or not. A value of \c 0 means that every
  1837.  *                      record will be sent in a separate datagram; a
  1838.  *                      value of \c 1 means that, if space permits,
  1839.  *                      multiple handshake messages (including CCS) belonging to
  1840.  *                      a single flight may be packed within a single datagram.
  1841.  *
  1842.  * \note           This is enabled by default and should only be disabled
  1843.  *                 for test purposes, or if datagram packing causes
  1844.  *                 interoperability issues with peers that don't support it.
  1845.  *
  1846.  * \note           Allowing datagram packing reduces the network load since
  1847.  *                 there's less overhead if multiple messages share the same
  1848.  *                 datagram. Also, it increases the handshake efficiency
  1849.  *                 since messages belonging to a single datagram will not
  1850.  *                 be reordered in transit, and so future message buffering
  1851.  *                 or flight retransmission (if no buffering is used) as
  1852.  *                 means to deal with reordering are needed less frequently.
  1853.  *
  1854.  * \note           Application records are not affected by this option and
  1855.  *                 are currently always sent in separate datagrams.
  1856.  *
  1857.  */
  1858. void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
  1859.                                        unsigned allow_packing );
  1860.  
  1861. /**
  1862.  * \brief          Set retransmit timeout values for the DTLS handshake.
  1863.  *                 (DTLS only, no effect on TLS.)
  1864.  *
  1865.  * \param conf     SSL configuration
  1866.  * \param min      Initial timeout value in milliseconds.
  1867.  *                 Default: 1000 (1 second).
  1868.  * \param max      Maximum timeout value in milliseconds.
  1869.  *                 Default: 60000 (60 seconds).
  1870.  *
  1871.  * \note           Default values are from RFC 6347 section 4.2.4.1.
  1872.  *
  1873.  * \note           The 'min' value should typically be slightly above the
  1874.  *                 expected round-trip time to your peer, plus whatever time
  1875.  *                 it takes for the peer to process the message. For example,
  1876.  *                 if your RTT is about 600ms and you peer needs up to 1s to
  1877.  *                 do the cryptographic operations in the handshake, then you
  1878.  *                 should set 'min' slightly above 1600. Lower values of 'min'
  1879.  *                 might cause spurious resends which waste network resources,
  1880.  *                 while larger value of 'min' will increase overall latency
  1881.  *                 on unreliable network links.
  1882.  *
  1883.  * \note           The more unreliable your network connection is, the larger
  1884.  *                 your max / min ratio needs to be in order to achieve
  1885.  *                 reliable handshakes.
  1886.  *
  1887.  * \note           Messages are retransmitted up to log2(ceil(max/min)) times.
  1888.  *                 For example, if min = 1s and max = 5s, the retransmit plan
  1889.  *                 goes: send ... 1s -> resend ... 2s -> resend ... 4s ->
  1890.  *                 resend ... 5s -> give up and return a timeout error.
  1891.  */
  1892. void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf, uint32_t min, uint32_t max );
  1893. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  1894.  
  1895. #if defined(MBEDTLS_SSL_SRV_C)
  1896. /**
  1897.  * \brief          Set the session cache callbacks (server-side only)
  1898.  *                 If not set, no session resuming is done (except if session
  1899.  *                 tickets are enabled too).
  1900.  *
  1901.  *                 The session cache has the responsibility to check for stale
  1902.  *                 entries based on timeout. See RFC 5246 for recommendations.
  1903.  *
  1904.  *                 Warning: session.peer_cert is cleared by the SSL/TLS layer on
  1905.  *                 connection shutdown, so do not cache the pointer! Either set
  1906.  *                 it to NULL or make a full copy of the certificate.
  1907.  *
  1908.  *                 The get callback is called once during the initial handshake
  1909.  *                 to enable session resuming. The get function has the
  1910.  *                 following parameters: (void *parameter, mbedtls_ssl_session *session)
  1911.  *                 If a valid entry is found, it should fill the master of
  1912.  *                 the session object with the cached values and return 0,
  1913.  *                 return 1 otherwise. Optionally peer_cert can be set as well
  1914.  *                 if it is properly present in cache entry.
  1915.  *
  1916.  *                 The set callback is called once during the initial handshake
  1917.  *                 to enable session resuming after the entire handshake has
  1918.  *                 been finished. The set function has the following parameters:
  1919.  *                 (void *parameter, const mbedtls_ssl_session *session). The function
  1920.  *                 should create a cache entry for future retrieval based on
  1921.  *                 the data in the session structure and should keep in mind
  1922.  *                 that the mbedtls_ssl_session object presented (and all its referenced
  1923.  *                 data) is cleared by the SSL/TLS layer when the connection is
  1924.  *                 terminated. It is recommended to add metadata to determine if
  1925.  *                 an entry is still valid in the future. Return 0 if
  1926.  *                 successfully cached, return 1 otherwise.
  1927.  *
  1928.  * \param conf           SSL configuration
  1929.  * \param p_cache        parmater (context) for both callbacks
  1930.  * \param f_get_cache    session get callback
  1931.  * \param f_set_cache    session set callback
  1932.  */
  1933. void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
  1934.         void *p_cache,
  1935.         int (*f_get_cache)(void *, mbedtls_ssl_session *),
  1936.         int (*f_set_cache)(void *, const mbedtls_ssl_session *) );
  1937. #endif /* MBEDTLS_SSL_SRV_C */
  1938.  
  1939. #if defined(MBEDTLS_SSL_CLI_C)
  1940. /**
  1941.  * \brief          Request resumption of session (client-side only)
  1942.  *                 Session data is copied from presented session structure.
  1943.  *
  1944.  * \param ssl      SSL context
  1945.  * \param session  session context
  1946.  *
  1947.  * \return         0 if successful,
  1948.  *                 MBEDTLS_ERR_SSL_ALLOC_FAILED if memory allocation failed,
  1949.  *                 MBEDTLS_ERR_SSL_BAD_INPUT_DATA if used server-side or
  1950.  *                 arguments are otherwise invalid
  1951.  *
  1952.  * \sa             mbedtls_ssl_get_session()
  1953.  */
  1954. int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session );
  1955. #endif /* MBEDTLS_SSL_CLI_C */
  1956.  
  1957. /**
  1958.  * \brief               Set the list of allowed ciphersuites and the preference
  1959.  *                      order. First in the list has the highest preference.
  1960.  *                      (Overrides all version-specific lists)
  1961.  *
  1962.  *                      The ciphersuites array is not copied, and must remain
  1963.  *                      valid for the lifetime of the ssl_config.
  1964.  *
  1965.  *                      Note: The server uses its own preferences
  1966.  *                      over the preference of the client unless
  1967.  *                      MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE is defined!
  1968.  *
  1969.  * \param conf          SSL configuration
  1970.  * \param ciphersuites  0-terminated list of allowed ciphersuites
  1971.  */
  1972. void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
  1973.                                    const int *ciphersuites );
  1974.  
  1975. /**
  1976.  * \brief               Set the list of allowed ciphersuites and the
  1977.  *                      preference order for a specific version of the protocol.
  1978.  *                      (Only useful on the server side)
  1979.  *
  1980.  *                      The ciphersuites array is not copied, and must remain
  1981.  *                      valid for the lifetime of the ssl_config.
  1982.  *
  1983.  * \param conf          SSL configuration
  1984.  * \param ciphersuites  0-terminated list of allowed ciphersuites
  1985.  * \param major         Major version number (only MBEDTLS_SSL_MAJOR_VERSION_3
  1986.  *                      supported)
  1987.  * \param minor         Minor version number (MBEDTLS_SSL_MINOR_VERSION_0,
  1988.  *                      MBEDTLS_SSL_MINOR_VERSION_1 and MBEDTLS_SSL_MINOR_VERSION_2,
  1989.  *                      MBEDTLS_SSL_MINOR_VERSION_3 supported)
  1990.  *
  1991.  * \note                With DTLS, use MBEDTLS_SSL_MINOR_VERSION_2 for DTLS 1.0
  1992.  *                      and MBEDTLS_SSL_MINOR_VERSION_3 for DTLS 1.2
  1993.  */
  1994. void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
  1995.                                        const int *ciphersuites,
  1996.                                        int major, int minor );
  1997.  
  1998. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  1999. /**
  2000.  * \brief          Set the X.509 security profile used for verification
  2001.  *
  2002.  * \note           The restrictions are enforced for all certificates in the
  2003.  *                 chain. However, signatures in the handshake are not covered
  2004.  *                 by this setting but by \b mbedtls_ssl_conf_sig_hashes().
  2005.  *
  2006.  * \param conf     SSL configuration
  2007.  * \param profile  Profile to use
  2008.  */
  2009. void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
  2010.                                     const mbedtls_x509_crt_profile *profile );
  2011.  
  2012. /**
  2013.  * \brief          Set the data required to verify peer certificate
  2014.  *
  2015.  * \note           See \c mbedtls_x509_crt_verify() for notes regarding the
  2016.  *                 parameters ca_chain (maps to trust_ca for that function)
  2017.  *                 and ca_crl.
  2018.  *
  2019.  * \param conf     SSL configuration
  2020.  * \param ca_chain trusted CA chain (meaning all fully trusted top-level CAs)
  2021.  * \param ca_crl   trusted CA CRLs
  2022.  */
  2023. void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
  2024.                                mbedtls_x509_crt *ca_chain,
  2025.                                mbedtls_x509_crl *ca_crl );
  2026.  
  2027. /**
  2028.  * \brief          Set own certificate chain and private key
  2029.  *
  2030.  * \note           own_cert should contain in order from the bottom up your
  2031.  *                 certificate chain. The top certificate (self-signed)
  2032.  *                 can be omitted.
  2033.  *
  2034.  * \note           On server, this function can be called multiple times to
  2035.  *                 provision more than one cert/key pair (eg one ECDSA, one
  2036.  *                 RSA with SHA-256, one RSA with SHA-1). An adequate
  2037.  *                 certificate will be selected according to the client's
  2038.  *                 advertised capabilities. In case multiple certificates are
  2039.  *                 adequate, preference is given to the one set by the first
  2040.  *                 call to this function, then second, etc.
  2041.  *
  2042.  * \note           On client, only the first call has any effect. That is,
  2043.  *                 only one client certificate can be provisioned. The
  2044.  *                 server's preferences in its CertficateRequest message will
  2045.  *                 be ignored and our only cert will be sent regardless of
  2046.  *                 whether it matches those preferences - the server can then
  2047.  *                 decide what it wants to do with it.
  2048.  *
  2049.  * \note           The provided \p pk_key needs to match the public key in the
  2050.  *                 first certificate in \p own_cert, or all handshakes using
  2051.  *                 that certificate will fail. It is your responsibility
  2052.  *                 to ensure that; this function will not perform any check.
  2053.  *                 You may use mbedtls_pk_check_pair() in order to perform
  2054.  *                 this check yourself, but be aware that this function can
  2055.  *                 be computationally expensive on some key types.
  2056.  *
  2057.  * \param conf     SSL configuration
  2058.  * \param own_cert own public certificate chain
  2059.  * \param pk_key   own private key
  2060.  *
  2061.  * \return         0 on success or MBEDTLS_ERR_SSL_ALLOC_FAILED
  2062.  */
  2063. int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
  2064.                               mbedtls_x509_crt *own_cert,
  2065.                               mbedtls_pk_context *pk_key );
  2066. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  2067.  
  2068. #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
  2069. /**
  2070.  * \brief          Set the Pre Shared Key (PSK) and the expected identity name
  2071.  *
  2072.  * \note           This is mainly useful for clients. Servers will usually
  2073.  *                 want to use \c mbedtls_ssl_conf_psk_cb() instead.
  2074.  *
  2075.  * \note           Currently clients can only register one pre-shared key.
  2076.  *                 In other words, the servers' identity hint is ignored.
  2077.  *                 Support for setting multiple PSKs on clients and selecting
  2078.  *                 one based on the identity hint is not a planned feature but
  2079.  *                 feedback is welcomed.
  2080.  *
  2081.  * \param conf     SSL configuration
  2082.  * \param psk      pointer to the pre-shared key
  2083.  * \param psk_len  pre-shared key length
  2084.  * \param psk_identity      pointer to the pre-shared key identity
  2085.  * \param psk_identity_len  identity key length
  2086.  *
  2087.  * \return         0 if successful or MBEDTLS_ERR_SSL_ALLOC_FAILED
  2088.  */
  2089. int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
  2090.                 const unsigned char *psk, size_t psk_len,
  2091.                 const unsigned char *psk_identity, size_t psk_identity_len );
  2092.  
  2093.  
  2094. /**
  2095.  * \brief          Set the Pre Shared Key (PSK) for the current handshake
  2096.  *
  2097.  * \note           This should only be called inside the PSK callback,
  2098.  *                 ie the function passed to \c mbedtls_ssl_conf_psk_cb().
  2099.  *
  2100.  * \param ssl      SSL context
  2101.  * \param psk      pointer to the pre-shared key
  2102.  * \param psk_len  pre-shared key length
  2103.  *
  2104.  * \return         0 if successful or MBEDTLS_ERR_SSL_ALLOC_FAILED
  2105.  */
  2106. int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
  2107.                             const unsigned char *psk, size_t psk_len );
  2108.  
  2109. /**
  2110.  * \brief          Set the PSK callback (server-side only).
  2111.  *
  2112.  *                 If set, the PSK callback is called for each
  2113.  *                 handshake where a PSK ciphersuite was negotiated.
  2114.  *                 The caller provides the identity received and wants to
  2115.  *                 receive the actual PSK data and length.
  2116.  *
  2117.  *                 The callback has the following parameters: (void *parameter,
  2118.  *                 mbedtls_ssl_context *ssl, const unsigned char *psk_identity,
  2119.  *                 size_t identity_len)
  2120.  *                 If a valid PSK identity is found, the callback should use
  2121.  *                 \c mbedtls_ssl_set_hs_psk() on the ssl context to set the
  2122.  *                 correct PSK and return 0.
  2123.  *                 Any other return value will result in a denied PSK identity.
  2124.  *
  2125.  * \note           If you set a PSK callback using this function, then you
  2126.  *                 don't need to set a PSK key and identity using
  2127.  *                 \c mbedtls_ssl_conf_psk().
  2128.  *
  2129.  * \param conf     SSL configuration
  2130.  * \param f_psk    PSK identity function
  2131.  * \param p_psk    PSK identity parameter
  2132.  */
  2133. void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
  2134.                      int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
  2135.                                   size_t),
  2136.                      void *p_psk );
  2137. #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
  2138.  
  2139. #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
  2140.  
  2141. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  2142.  
  2143. #if defined(MBEDTLS_DEPRECATED_WARNING)
  2144. #define MBEDTLS_DEPRECATED    __attribute__((deprecated))
  2145. #else
  2146. #define MBEDTLS_DEPRECATED
  2147. #endif
  2148.  
  2149. /**
  2150.  * \brief          Set the Diffie-Hellman public P and G values,
  2151.  *                 read as hexadecimal strings (server-side only)
  2152.  *                 (Default values: MBEDTLS_DHM_RFC3526_MODP_2048_[PG])
  2153.  *
  2154.  * \param conf     SSL configuration
  2155.  * \param dhm_P    Diffie-Hellman-Merkle modulus
  2156.  * \param dhm_G    Diffie-Hellman-Merkle generator
  2157.  *
  2158.  * \deprecated     Superseded by \c mbedtls_ssl_conf_dh_param_bin.
  2159.  *
  2160.  * \return         0 if successful
  2161.  */
  2162. MBEDTLS_DEPRECATED int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf,
  2163.                                                   const char *dhm_P,
  2164.                                                   const char *dhm_G );
  2165.  
  2166. #endif /* MBEDTLS_DEPRECATED_REMOVED */
  2167.  
  2168. /**
  2169.  * \brief          Set the Diffie-Hellman public P and G values
  2170.  *                 from big-endian binary presentations.
  2171.  *                 (Default values: MBEDTLS_DHM_RFC3526_MODP_2048_[PG]_BIN)
  2172.  *
  2173.  * \param conf     SSL configuration
  2174.  * \param dhm_P    Diffie-Hellman-Merkle modulus in big-endian binary form
  2175.  * \param P_len    Length of DHM modulus
  2176.  * \param dhm_G    Diffie-Hellman-Merkle generator in big-endian binary form
  2177.  * \param G_len    Length of DHM generator
  2178.  *
  2179.  * \return         0 if successful
  2180.  */
  2181. int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
  2182.                                    const unsigned char *dhm_P, size_t P_len,
  2183.                                    const unsigned char *dhm_G,  size_t G_len );
  2184.  
  2185. /**
  2186.  * \brief          Set the Diffie-Hellman public P and G values,
  2187.  *                 read from existing context (server-side only)
  2188.  *
  2189.  * \param conf     SSL configuration
  2190.  * \param dhm_ctx  Diffie-Hellman-Merkle context
  2191.  *
  2192.  * \return         0 if successful
  2193.  */
  2194. int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx );
  2195. #endif /* MBEDTLS_DHM_C && defined(MBEDTLS_SSL_SRV_C) */
  2196.  
  2197. #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
  2198. /**
  2199.  * \brief          Set the minimum length for Diffie-Hellman parameters.
  2200.  *                 (Client-side only.)
  2201.  *                 (Default: 1024 bits.)
  2202.  *
  2203.  * \param conf     SSL configuration
  2204.  * \param bitlen   Minimum bit length of the DHM prime
  2205.  */
  2206. void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
  2207.                                       unsigned int bitlen );
  2208. #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
  2209.  
  2210. #if defined(MBEDTLS_ECP_C)
  2211. /**
  2212.  * \brief          Set the allowed curves in order of preference.
  2213.  *                 (Default: all defined curves.)
  2214.  *
  2215.  *                 On server: this only affects selection of the ECDHE curve;
  2216.  *                 the curves used for ECDH and ECDSA are determined by the
  2217.  *                 list of available certificates instead.
  2218.  *
  2219.  *                 On client: this affects the list of curves offered for any
  2220.  *                 use. The server can override our preference order.
  2221.  *
  2222.  *                 Both sides: limits the set of curves accepted for use in
  2223.  *                 ECDHE and in the peer's end-entity certificate.
  2224.  *
  2225.  * \note           This has no influence on which curves are allowed inside the
  2226.  *                 certificate chains, see \c mbedtls_ssl_conf_cert_profile()
  2227.  *                 for that. For the end-entity certificate however, the key
  2228.  *                 will be accepted only if it is allowed both by this list
  2229.  *                 and by the cert profile.
  2230.  *
  2231.  * \note           This list should be ordered by decreasing preference
  2232.  *                 (preferred curve first).
  2233.  *
  2234.  * \param conf     SSL configuration
  2235.  * \param curves   Ordered list of allowed curves,
  2236.  *                 terminated by MBEDTLS_ECP_DP_NONE.
  2237.  */
  2238. void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
  2239.                               const mbedtls_ecp_group_id *curves );
  2240. #endif /* MBEDTLS_ECP_C */
  2241.  
  2242. #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
  2243. /**
  2244.  * \brief          Set the allowed hashes for signatures during the handshake.
  2245.  *                 (Default: all available hashes except MD5.)
  2246.  *
  2247.  * \note           This only affects which hashes are offered and can be used
  2248.  *                 for signatures during the handshake. Hashes for message
  2249.  *                 authentication and the TLS PRF are controlled by the
  2250.  *                 ciphersuite, see \c mbedtls_ssl_conf_ciphersuites(). Hashes
  2251.  *                 used for certificate signature are controlled by the
  2252.  *                 verification profile, see \c mbedtls_ssl_conf_cert_profile().
  2253.  *
  2254.  * \note           This list should be ordered by decreasing preference
  2255.  *                 (preferred hash first).
  2256.  *
  2257.  * \param conf     SSL configuration
  2258.  * \param hashes   Ordered list of allowed signature hashes,
  2259.  *                 terminated by \c MBEDTLS_MD_NONE.
  2260.  */
  2261. void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
  2262.                                   const int *hashes );
  2263. #endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
  2264.  
  2265. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  2266. /**
  2267.  * \brief          Set or reset the hostname to check against the received
  2268.  *                 server certificate. It sets the ServerName TLS extension,
  2269.  *                 too, if that extension is enabled. (client-side only)
  2270.  *
  2271.  * \param ssl      SSL context
  2272.  * \param hostname the server hostname, may be NULL to clear hostname
  2273.  
  2274.  * \note           Maximum hostname length MBEDTLS_SSL_MAX_HOST_NAME_LEN.
  2275.  *
  2276.  * \return         0 if successful, MBEDTLS_ERR_SSL_ALLOC_FAILED on
  2277.  *                 allocation failure, MBEDTLS_ERR_SSL_BAD_INPUT_DATA on
  2278.  *                 too long input hostname.
  2279.  *
  2280.  *                 Hostname set to the one provided on success (cleared
  2281.  *                 when NULL). On allocation failure hostname is cleared.
  2282.  *                 On too long input failure, old hostname is unchanged.
  2283.  */
  2284. int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname );
  2285. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  2286.  
  2287. #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
  2288. /**
  2289.  * \brief          Set own certificate and key for the current handshake
  2290.  *
  2291.  * \note           Same as \c mbedtls_ssl_conf_own_cert() but for use within
  2292.  *                 the SNI callback.
  2293.  *
  2294.  * \param ssl      SSL context
  2295.  * \param own_cert own public certificate chain
  2296.  * \param pk_key   own private key
  2297.  *
  2298.  * \return         0 on success or MBEDTLS_ERR_SSL_ALLOC_FAILED
  2299.  */
  2300. int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
  2301.                                  mbedtls_x509_crt *own_cert,
  2302.                                  mbedtls_pk_context *pk_key );
  2303.  
  2304. /**
  2305.  * \brief          Set the data required to verify peer certificate for the
  2306.  *                 current handshake
  2307.  *
  2308.  * \note           Same as \c mbedtls_ssl_conf_ca_chain() but for use within
  2309.  *                 the SNI callback.
  2310.  *
  2311.  * \param ssl      SSL context
  2312.  * \param ca_chain trusted CA chain (meaning all fully trusted top-level CAs)
  2313.  * \param ca_crl   trusted CA CRLs
  2314.  */
  2315. void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
  2316.                                   mbedtls_x509_crt *ca_chain,
  2317.                                   mbedtls_x509_crl *ca_crl );
  2318.  
  2319. /**
  2320.  * \brief          Set authmode for the current handshake.
  2321.  *
  2322.  * \note           Same as \c mbedtls_ssl_conf_authmode() but for use within
  2323.  *                 the SNI callback.
  2324.  *
  2325.  * \param ssl      SSL context
  2326.  * \param authmode MBEDTLS_SSL_VERIFY_NONE, MBEDTLS_SSL_VERIFY_OPTIONAL or
  2327.  *                 MBEDTLS_SSL_VERIFY_REQUIRED
  2328.  */
  2329. void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
  2330.                                   int authmode );
  2331.  
  2332. /**
  2333.  * \brief          Set server side ServerName TLS extension callback
  2334.  *                 (optional, server-side only).
  2335.  *
  2336.  *                 If set, the ServerName callback is called whenever the
  2337.  *                 server receives a ServerName TLS extension from the client
  2338.  *                 during a handshake. The ServerName callback has the
  2339.  *                 following parameters: (void *parameter, mbedtls_ssl_context *ssl,
  2340.  *                 const unsigned char *hostname, size_t len). If a suitable
  2341.  *                 certificate is found, the callback must set the
  2342.  *                 certificate(s) and key(s) to use with \c
  2343.  *                 mbedtls_ssl_set_hs_own_cert() (can be called repeatedly),
  2344.  *                 and may optionally adjust the CA and associated CRL with \c
  2345.  *                 mbedtls_ssl_set_hs_ca_chain() as well as the client
  2346.  *                 authentication mode with \c mbedtls_ssl_set_hs_authmode(),
  2347.  *                 then must return 0. If no matching name is found, the
  2348.  *                 callback must either set a default cert, or
  2349.  *                 return non-zero to abort the handshake at this point.
  2350.  *
  2351.  * \param conf     SSL configuration
  2352.  * \param f_sni    verification function
  2353.  * \param p_sni    verification parameter
  2354.  */
  2355. void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
  2356.                   int (*f_sni)(void *, mbedtls_ssl_context *, const unsigned char *,
  2357.                                size_t),
  2358.                   void *p_sni );
  2359. #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
  2360.  
  2361. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  2362. /**
  2363.  * \brief          Set the EC J-PAKE password for current handshake.
  2364.  *
  2365.  * \note           An internal copy is made, and destroyed as soon as the
  2366.  *                 handshake is completed, or when the SSL context is reset or
  2367.  *                 freed.
  2368.  *
  2369.  * \note           The SSL context needs to be already set up. The right place
  2370.  *                 to call this function is between \c mbedtls_ssl_setup() or
  2371.  *                 \c mbedtls_ssl_reset() and \c mbedtls_ssl_handshake().
  2372.  *
  2373.  * \param ssl      SSL context
  2374.  * \param pw       EC J-PAKE password (pre-shared secret)
  2375.  * \param pw_len   length of pw in bytes
  2376.  *
  2377.  * \return         0 on success, or a negative error code.
  2378.  */
  2379. int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
  2380.                                          const unsigned char *pw,
  2381.                                          size_t pw_len );
  2382. #endif /*MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  2383.  
  2384. #if defined(MBEDTLS_SSL_ALPN)
  2385. /**
  2386.  * \brief          Set the supported Application Layer Protocols.
  2387.  *
  2388.  * \param conf     SSL configuration
  2389.  * \param protos   Pointer to a NULL-terminated list of supported protocols,
  2390.  *                 in decreasing preference order. The pointer to the list is
  2391.  *                 recorded by the library for later reference as required, so
  2392.  *                 the lifetime of the table must be atleast as long as the
  2393.  *                 lifetime of the SSL configuration structure.
  2394.  *
  2395.  * \return         0 on success, or MBEDTLS_ERR_SSL_BAD_INPUT_DATA.
  2396.  */
  2397. int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos );
  2398.  
  2399. /**
  2400.  * \brief          Get the name of the negotiated Application Layer Protocol.
  2401.  *                 This function should be called after the handshake is
  2402.  *                 completed.
  2403.  *
  2404.  * \param ssl      SSL context
  2405.  *
  2406.  * \return         Protcol name, or NULL if no protocol was negotiated.
  2407.  */
  2408. const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl );
  2409. #endif /* MBEDTLS_SSL_ALPN */
  2410.  
  2411. /**
  2412.  * \brief          Set the maximum supported version sent from the client side
  2413.  *                 and/or accepted at the server side
  2414.  *                 (Default: MBEDTLS_SSL_MAX_MAJOR_VERSION, MBEDTLS_SSL_MAX_MINOR_VERSION)
  2415.  *
  2416.  * \note           This ignores ciphersuites from higher versions.
  2417.  *
  2418.  * \note           With DTLS, use MBEDTLS_SSL_MINOR_VERSION_2 for DTLS 1.0 and
  2419.  *                 MBEDTLS_SSL_MINOR_VERSION_3 for DTLS 1.2
  2420.  *
  2421.  * \param conf     SSL configuration
  2422.  * \param major    Major version number (only MBEDTLS_SSL_MAJOR_VERSION_3 supported)
  2423.  * \param minor    Minor version number (MBEDTLS_SSL_MINOR_VERSION_0,
  2424.  *                 MBEDTLS_SSL_MINOR_VERSION_1 and MBEDTLS_SSL_MINOR_VERSION_2,
  2425.  *                 MBEDTLS_SSL_MINOR_VERSION_3 supported)
  2426.  */
  2427. void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor );
  2428.  
  2429. /**
  2430.  * \brief          Set the minimum accepted SSL/TLS protocol version
  2431.  *                 (Default: TLS 1.0)
  2432.  *
  2433.  * \note           Input outside of the SSL_MAX_XXXXX_VERSION and
  2434.  *                 SSL_MIN_XXXXX_VERSION range is ignored.
  2435.  *
  2436.  * \note           MBEDTLS_SSL_MINOR_VERSION_0 (SSL v3) should be avoided.
  2437.  *
  2438.  * \note           With DTLS, use MBEDTLS_SSL_MINOR_VERSION_2 for DTLS 1.0 and
  2439.  *                 MBEDTLS_SSL_MINOR_VERSION_3 for DTLS 1.2
  2440.  *
  2441.  * \param conf     SSL configuration
  2442.  * \param major    Major version number (only MBEDTLS_SSL_MAJOR_VERSION_3 supported)
  2443.  * \param minor    Minor version number (MBEDTLS_SSL_MINOR_VERSION_0,
  2444.  *                 MBEDTLS_SSL_MINOR_VERSION_1 and MBEDTLS_SSL_MINOR_VERSION_2,
  2445.  *                 MBEDTLS_SSL_MINOR_VERSION_3 supported)
  2446.  */
  2447. void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor );
  2448.  
  2449. #if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
  2450. /**
  2451.  * \brief          Set the fallback flag (client-side only).
  2452.  *                 (Default: MBEDTLS_SSL_IS_NOT_FALLBACK).
  2453.  *
  2454.  * \note           Set to MBEDTLS_SSL_IS_FALLBACK when preparing a fallback
  2455.  *                 connection, that is a connection with max_version set to a
  2456.  *                 lower value than the value you're willing to use. Such
  2457.  *                 fallback connections are not recommended but are sometimes
  2458.  *                 necessary to interoperate with buggy (version-intolerant)
  2459.  *                 servers.
  2460.  *
  2461.  * \warning        You should NOT set this to MBEDTLS_SSL_IS_FALLBACK for
  2462.  *                 non-fallback connections! This would appear to work for a
  2463.  *                 while, then cause failures when the server is upgraded to
  2464.  *                 support a newer TLS version.
  2465.  *
  2466.  * \param conf     SSL configuration
  2467.  * \param fallback MBEDTLS_SSL_IS_NOT_FALLBACK or MBEDTLS_SSL_IS_FALLBACK
  2468.  */
  2469. void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback );
  2470. #endif /* MBEDTLS_SSL_FALLBACK_SCSV && MBEDTLS_SSL_CLI_C */
  2471.  
  2472. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  2473. /**
  2474.  * \brief           Enable or disable Encrypt-then-MAC
  2475.  *                  (Default: MBEDTLS_SSL_ETM_ENABLED)
  2476.  *
  2477.  * \note            This should always be enabled, it is a security
  2478.  *                  improvement, and should not cause any interoperability
  2479.  *                  issue (used only if the peer supports it too).
  2480.  *
  2481.  * \param conf      SSL configuration
  2482.  * \param etm       MBEDTLS_SSL_ETM_ENABLED or MBEDTLS_SSL_ETM_DISABLED
  2483.  */
  2484. void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm );
  2485. #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
  2486.  
  2487. #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
  2488. /**
  2489.  * \brief           Enable or disable Extended Master Secret negotiation.
  2490.  *                  (Default: MBEDTLS_SSL_EXTENDED_MS_ENABLED)
  2491.  *
  2492.  * \note            This should always be enabled, it is a security fix to the
  2493.  *                  protocol, and should not cause any interoperability issue
  2494.  *                  (used only if the peer supports it too).
  2495.  *
  2496.  * \param conf      SSL configuration
  2497.  * \param ems       MBEDTLS_SSL_EXTENDED_MS_ENABLED or MBEDTLS_SSL_EXTENDED_MS_DISABLED
  2498.  */
  2499. void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems );
  2500. #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
  2501.  
  2502. #if defined(MBEDTLS_ARC4_C)
  2503. /**
  2504.  * \brief          Disable or enable support for RC4
  2505.  *                 (Default: MBEDTLS_SSL_ARC4_DISABLED)
  2506.  *
  2507.  * \warning        Use of RC4 in DTLS/TLS has been prohibited by RFC 7465
  2508.  *                 for security reasons. Use at your own risk.
  2509.  *
  2510.  * \note           This function is deprecated and will likely be removed in
  2511.  *                 a future version of the library.
  2512.  *                 RC4 is disabled by default at compile time and needs to be
  2513.  *                 actively enabled for use with legacy systems.
  2514.  *
  2515.  * \param conf     SSL configuration
  2516.  * \param arc4     MBEDTLS_SSL_ARC4_ENABLED or MBEDTLS_SSL_ARC4_DISABLED
  2517.  */
  2518. void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 );
  2519. #endif /* MBEDTLS_ARC4_C */
  2520.  
  2521. #if defined(MBEDTLS_SSL_SRV_C)
  2522. /**
  2523.  * \brief          Whether to send a list of acceptable CAs in
  2524.  *                 CertificateRequest messages.
  2525.  *                 (Default: do send)
  2526.  *
  2527.  * \param conf     SSL configuration
  2528.  * \param cert_req_ca_list   MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED or
  2529.  *                          MBEDTLS_SSL_CERT_REQ_CA_LIST_DISABLED
  2530.  */
  2531. void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
  2532.                                           char cert_req_ca_list );
  2533. #endif /* MBEDTLS_SSL_SRV_C */
  2534.  
  2535. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  2536. /**
  2537.  * \brief          Set the maximum fragment length to emit and/or negotiate.
  2538.  *                 (Typical: the smaller of #MBEDTLS_SSL_IN_CONTENT_LEN and
  2539.  *                 #MBEDTLS_SSL_OUT_CONTENT_LEN, usually `2^14` bytes)
  2540.  *                 (Server: set maximum fragment length to emit,
  2541.  *                 usually negotiated by the client during handshake)
  2542.  *                 (Client: set maximum fragment length to emit *and*
  2543.  *                 negotiate with the server during handshake)
  2544.  *                 (Default: #MBEDTLS_SSL_MAX_FRAG_LEN_NONE)
  2545.  *
  2546.  * \note           On the client side, the maximum fragment length extension
  2547.  *                 *will not* be used, unless the maximum fragment length has
  2548.  *                 been set via this function to a value different than
  2549.  *                 #MBEDTLS_SSL_MAX_FRAG_LEN_NONE.
  2550.  *
  2551.  * \note           This sets the maximum length for a record's payload,
  2552.  *                 excluding record overhead that will be added to it, see
  2553.  *                 \c mbedtls_ssl_get_record_expansion().
  2554.  *
  2555.  * \note           With TLS, this currently only affects ApplicationData (sent
  2556.  *                 with \c mbedtls_ssl_read()), not handshake messages.
  2557.  *                 With DTLS, this affects both ApplicationData and handshake.
  2558.  *
  2559.  * \note           For DTLS, it is also possible to set a limit for the total
  2560.  *                 size of daragrams passed to the transport layer, including
  2561.  *                 record overhead, see \c mbedtls_ssl_set_mtu().
  2562.  *
  2563.  * \param conf     SSL configuration
  2564.  * \param mfl_code Code for maximum fragment length (allowed values:
  2565.  *                 MBEDTLS_SSL_MAX_FRAG_LEN_512,  MBEDTLS_SSL_MAX_FRAG_LEN_1024,
  2566.  *                 MBEDTLS_SSL_MAX_FRAG_LEN_2048, MBEDTLS_SSL_MAX_FRAG_LEN_4096)
  2567.  *
  2568.  * \return         0 if successful or MBEDTLS_ERR_SSL_BAD_INPUT_DATA
  2569.  */
  2570. int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code );
  2571. #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
  2572.  
  2573. #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
  2574. /**
  2575.  * \brief          Activate negotiation of truncated HMAC
  2576.  *                 (Default: MBEDTLS_SSL_TRUNC_HMAC_DISABLED)
  2577.  *
  2578.  * \param conf     SSL configuration
  2579.  * \param truncate Enable or disable (MBEDTLS_SSL_TRUNC_HMAC_ENABLED or
  2580.  *                                    MBEDTLS_SSL_TRUNC_HMAC_DISABLED)
  2581.  */
  2582. void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate );
  2583. #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
  2584.  
  2585. #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
  2586. /**
  2587.  * \brief          Enable / Disable 1/n-1 record splitting
  2588.  *                 (Default: MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED)
  2589.  *
  2590.  * \note           Only affects SSLv3 and TLS 1.0, not higher versions.
  2591.  *                 Does not affect non-CBC ciphersuites in any version.
  2592.  *
  2593.  * \param conf     SSL configuration
  2594.  * \param split    MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED or
  2595.  *                 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED
  2596.  */
  2597. void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split );
  2598. #endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
  2599.  
  2600. #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
  2601. /**
  2602.  * \brief          Enable / Disable session tickets (client only).
  2603.  *                 (Default: MBEDTLS_SSL_SESSION_TICKETS_ENABLED.)
  2604.  *
  2605.  * \note           On server, use \c mbedtls_ssl_conf_session_tickets_cb().
  2606.  *
  2607.  * \param conf     SSL configuration
  2608.  * \param use_tickets   Enable or disable (MBEDTLS_SSL_SESSION_TICKETS_ENABLED or
  2609.  *                                         MBEDTLS_SSL_SESSION_TICKETS_DISABLED)
  2610.  */
  2611. void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets );
  2612. #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
  2613.  
  2614. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  2615. /**
  2616.  * \brief          Enable / Disable renegotiation support for connection when
  2617.  *                 initiated by peer
  2618.  *                 (Default: MBEDTLS_SSL_RENEGOTIATION_DISABLED)
  2619.  *
  2620.  * \warning        It is recommended to always disable renegotation unless you
  2621.  *                 know you need it and you know what you're doing. In the
  2622.  *                 past, there have been several issues associated with
  2623.  *                 renegotiation or a poor understanding of its properties.
  2624.  *
  2625.  * \note           Server-side, enabling renegotiation also makes the server
  2626.  *                 susceptible to a resource DoS by a malicious client.
  2627.  *
  2628.  * \param conf    SSL configuration
  2629.  * \param renegotiation     Enable or disable (MBEDTLS_SSL_RENEGOTIATION_ENABLED or
  2630.  *                                             MBEDTLS_SSL_RENEGOTIATION_DISABLED)
  2631.  */
  2632. void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation );
  2633. #endif /* MBEDTLS_SSL_RENEGOTIATION */
  2634.  
  2635. /**
  2636.  * \brief          Prevent or allow legacy renegotiation.
  2637.  *                 (Default: MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION)
  2638.  *
  2639.  *                 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION allows connections to
  2640.  *                 be established even if the peer does not support
  2641.  *                 secure renegotiation, but does not allow renegotiation
  2642.  *                 to take place if not secure.
  2643.  *                 (Interoperable and secure option)
  2644.  *
  2645.  *                 MBEDTLS_SSL_LEGACY_ALLOW_RENEGOTIATION allows renegotiations
  2646.  *                 with non-upgraded peers. Allowing legacy renegotiation
  2647.  *                 makes the connection vulnerable to specific man in the
  2648.  *                 middle attacks. (See RFC 5746)
  2649.  *                 (Most interoperable and least secure option)
  2650.  *
  2651.  *                 MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE breaks off connections
  2652.  *                 if peer does not support secure renegotiation. Results
  2653.  *                 in interoperability issues with non-upgraded peers
  2654.  *                 that do not support renegotiation altogether.
  2655.  *                 (Most secure option, interoperability issues)
  2656.  *
  2657.  * \param conf     SSL configuration
  2658.  * \param allow_legacy  Prevent or allow (SSL_NO_LEGACY_RENEGOTIATION,
  2659.  *                                        SSL_ALLOW_LEGACY_RENEGOTIATION or
  2660.  *                                        MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE)
  2661.  */
  2662. void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy );
  2663.  
  2664. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  2665. /**
  2666.  * \brief          Enforce renegotiation requests.
  2667.  *                 (Default: enforced, max_records = 16)
  2668.  *
  2669.  *                 When we request a renegotiation, the peer can comply or
  2670.  *                 ignore the request. This function allows us to decide
  2671.  *                 whether to enforce our renegotiation requests by closing
  2672.  *                 the connection if the peer doesn't comply.
  2673.  *
  2674.  *                 However, records could already be in transit from the peer
  2675.  *                 when the request is emitted. In order to increase
  2676.  *                 reliability, we can accept a number of records before the
  2677.  *                 expected handshake records.
  2678.  *
  2679.  *                 The optimal value is highly dependent on the specific usage
  2680.  *                 scenario.
  2681.  *
  2682.  * \note           With DTLS and server-initiated renegotiation, the
  2683.  *                 HelloRequest is retransmited every time mbedtls_ssl_read() times
  2684.  *                 out or receives Application Data, until:
  2685.  *                 - max_records records have beens seen, if it is >= 0, or
  2686.  *                 - the number of retransmits that would happen during an
  2687.  *                 actual handshake has been reached.
  2688.  *                 Please remember the request might be lost a few times
  2689.  *                 if you consider setting max_records to a really low value.
  2690.  *
  2691.  * \warning        On client, the grace period can only happen during
  2692.  *                 mbedtls_ssl_read(), as opposed to mbedtls_ssl_write() and mbedtls_ssl_renegotiate()
  2693.  *                 which always behave as if max_record was 0. The reason is,
  2694.  *                 if we receive application data from the server, we need a
  2695.  *                 place to write it, which only happens during mbedtls_ssl_read().
  2696.  *
  2697.  * \param conf     SSL configuration
  2698.  * \param max_records Use MBEDTLS_SSL_RENEGOTIATION_NOT_ENFORCED if you don't want to
  2699.  *                 enforce renegotiation, or a non-negative value to enforce
  2700.  *                 it but allow for a grace period of max_records records.
  2701.  */
  2702. void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records );
  2703.  
  2704. /**
  2705.  * \brief          Set record counter threshold for periodic renegotiation.
  2706.  *                 (Default: 2^48 - 1)
  2707.  *
  2708.  *                 Renegotiation is automatically triggered when a record
  2709.  *                 counter (outgoing or ingoing) crosses the defined
  2710.  *                 threshold. The default value is meant to prevent the
  2711.  *                 connection from being closed when the counter is about to
  2712.  *                 reached its maximal value (it is not allowed to wrap).
  2713.  *
  2714.  *                 Lower values can be used to enforce policies such as "keys
  2715.  *                 must be refreshed every N packets with cipher X".
  2716.  *
  2717.  *                 The renegotiation period can be disabled by setting
  2718.  *                 conf->disable_renegotiation to
  2719.  *                 MBEDTLS_SSL_RENEGOTIATION_DISABLED.
  2720.  *
  2721.  * \note           When the configured transport is
  2722.  *                 MBEDTLS_SSL_TRANSPORT_DATAGRAM the maximum renegotiation
  2723.  *                 period is 2^48 - 1, and for MBEDTLS_SSL_TRANSPORT_STREAM,
  2724.  *                 the maximum renegotiation period is 2^64 - 1.
  2725.  *
  2726.  * \param conf     SSL configuration
  2727.  * \param period   The threshold value: a big-endian 64-bit number.
  2728.  */
  2729. void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
  2730.                                    const unsigned char period[8] );
  2731. #endif /* MBEDTLS_SSL_RENEGOTIATION */
  2732.  
  2733. /**
  2734.  * \brief          Check if there is data already read from the
  2735.  *                 underlying transport but not yet processed.
  2736.  *
  2737.  * \param ssl      SSL context
  2738.  *
  2739.  * \return         0 if nothing's pending, 1 otherwise.
  2740.  *
  2741.  * \note           This is different in purpose and behaviour from
  2742.  *                 \c mbedtls_ssl_get_bytes_avail in that it considers
  2743.  *                 any kind of unprocessed data, not only unread
  2744.  *                 application data. If \c mbedtls_ssl_get_bytes
  2745.  *                 returns a non-zero value, this function will
  2746.  *                 also signal pending data, but the converse does
  2747.  *                 not hold. For example, in DTLS there might be
  2748.  *                 further records waiting to be processed from
  2749.  *                 the current underlying transport's datagram.
  2750.  *
  2751.  * \note           If this function returns 1 (data pending), this
  2752.  *                 does not imply that a subsequent call to
  2753.  *                 \c mbedtls_ssl_read will provide any data;
  2754.  *                 e.g., the unprocessed data might turn out
  2755.  *                 to be an alert or a handshake message.
  2756.  *
  2757.  * \note           This function is useful in the following situation:
  2758.  *                 If the SSL/TLS module successfully returns from an
  2759.  *                 operation - e.g. a handshake or an application record
  2760.  *                 read - and you're awaiting incoming data next, you
  2761.  *                 must not immediately idle on the underlying transport
  2762.  *                 to have data ready, but you need to check the value
  2763.  *                 of this function first. The reason is that the desired
  2764.  *                 data might already be read but not yet processed.
  2765.  *                 If, in contrast, a previous call to the SSL/TLS module
  2766.  *                 returned MBEDTLS_ERR_SSL_WANT_READ, it is not necessary
  2767.  *                 to call this function, as the latter error code entails
  2768.  *                 that all internal data has been processed.
  2769.  *
  2770.  */
  2771. int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl );
  2772.  
  2773. /**
  2774.  * \brief          Return the number of application data bytes
  2775.  *                 remaining to be read from the current record.
  2776.  *
  2777.  * \param ssl      SSL context
  2778.  *
  2779.  * \return         How many bytes are available in the application
  2780.  *                 data record read buffer.
  2781.  *
  2782.  * \note           When working over a datagram transport, this is
  2783.  *                 useful to detect the current datagram's boundary
  2784.  *                 in case \c mbedtls_ssl_read has written the maximal
  2785.  *                 amount of data fitting into the input buffer.
  2786.  *
  2787.  */
  2788. size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl );
  2789.  
  2790. /**
  2791.  * \brief          Return the result of the certificate verification
  2792.  *
  2793.  * \param ssl      The SSL context to use.
  2794.  *
  2795.  * \return         \c 0 if the certificate verification was successful.
  2796.  * \return         \c -1u if the result is not available. This may happen
  2797.  *                 e.g. if the handshake aborts early, or a verification
  2798.  *                 callback returned a fatal error.
  2799.  * \return         A bitwise combination of \c MBEDTLS_X509_BADCERT_XXX
  2800.  *                 and \c MBEDTLS_X509_BADCRL_XXX failure flags; see x509.h.
  2801.  */
  2802. uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl );
  2803.  
  2804. /**
  2805.  * \brief          Return the name of the current ciphersuite
  2806.  *
  2807.  * \param ssl      SSL context
  2808.  *
  2809.  * \return         a string containing the ciphersuite name
  2810.  */
  2811. const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl );
  2812.  
  2813. /**
  2814.  * \brief          Return the current SSL version (SSLv3/TLSv1/etc)
  2815.  *
  2816.  * \param ssl      SSL context
  2817.  *
  2818.  * \return         a string containing the SSL version
  2819.  */
  2820. const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl );
  2821.  
  2822. /**
  2823.  * \brief          Return the (maximum) number of bytes added by the record
  2824.  *                 layer: header + encryption/MAC overhead (inc. padding)
  2825.  *
  2826.  * \note           This function is not available (always returns an error)
  2827.  *                 when record compression is enabled.
  2828.  *
  2829.  * \param ssl      SSL context
  2830.  *
  2831.  * \return         Current maximum record expansion in bytes, or
  2832.  *                 MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE if compression is
  2833.  *                 enabled, which makes expansion much less predictable
  2834.  */
  2835. int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl );
  2836.  
  2837. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  2838. /**
  2839.  * \brief          Return the maximum fragment length (payload, in bytes).
  2840.  *                 This is the value negotiated with peer if any,
  2841.  *                 or the locally configured value.
  2842.  *
  2843.  * \sa             mbedtls_ssl_conf_max_frag_len()
  2844.  * \sa             mbedtls_ssl_get_max_record_payload()
  2845.  *
  2846.  * \param ssl      SSL context
  2847.  *
  2848.  * \return         Current maximum fragment length.
  2849.  */
  2850. size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl );
  2851. #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
  2852.  
  2853. /**
  2854.  * \brief          Return the current maximum outgoing record payload in bytes.
  2855.  *                 This takes into account the config.h setting \c
  2856.  *                 MBEDTLS_SSL_OUT_CONTENT_LEN, the configured and negotiated
  2857.  *                 max fragment length extension if used, and for DTLS the
  2858.  *                 path MTU as configured and current record expansion.
  2859.  *
  2860.  * \note           With DTLS, \c mbedtls_ssl_write() will return an error if
  2861.  *                 called with a larger length value.
  2862.  *                 With TLS, \c mbedtls_ssl_write() will fragment the input if
  2863.  *                 necessary and return the number of bytes written; it is up
  2864.  *                 to the caller to call \c mbedtls_ssl_write() again in
  2865.  *                 order to send the remaining bytes if any.
  2866.  *
  2867.  * \note           This function is not available (always returns an error)
  2868.  *                 when record compression is enabled.
  2869.  *
  2870.  * \sa             mbedtls_ssl_set_mtu()
  2871.  * \sa             mbedtls_ssl_get_max_frag_len()
  2872.  * \sa             mbedtls_ssl_get_record_expansion()
  2873.  *
  2874.  * \param ssl      SSL context
  2875.  *
  2876.  * \return         Current maximum payload for an outgoing record,
  2877.  *                 or a negative error code.
  2878.  */
  2879. int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl );
  2880.  
  2881. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  2882. /**
  2883.  * \brief          Return the peer certificate from the current connection
  2884.  *
  2885.  *                 Note: Can be NULL in case no certificate was sent during
  2886.  *                 the handshake. Different calls for the same connection can
  2887.  *                 return the same or different pointers for the same
  2888.  *                 certificate and even a different certificate altogether.
  2889.  *                 The peer cert CAN change in a single connection if
  2890.  *                 renegotiation is performed.
  2891.  *
  2892.  * \param ssl      SSL context
  2893.  *
  2894.  * \return         the current peer certificate
  2895.  */
  2896. const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl );
  2897. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  2898.  
  2899. #if defined(MBEDTLS_SSL_CLI_C)
  2900. /**
  2901.  * \brief          Save session in order to resume it later (client-side only)
  2902.  *                 Session data is copied to presented session structure.
  2903.  *
  2904.  *
  2905.  * \param ssl      SSL context
  2906.  * \param session  session context
  2907.  *
  2908.  * \return         0 if successful,
  2909.  *                 MBEDTLS_ERR_SSL_ALLOC_FAILED if memory allocation failed,
  2910.  *                 MBEDTLS_ERR_SSL_BAD_INPUT_DATA if used server-side or
  2911.  *                 arguments are otherwise invalid.
  2912.  *
  2913.  * \note           Only the server certificate is copied, and not the full chain,
  2914.  *                 so you should not attempt to validate the certificate again
  2915.  *                 by calling \c mbedtls_x509_crt_verify() on it.
  2916.  *                 Instead, you should use the results from the verification
  2917.  *                 in the original handshake by calling \c mbedtls_ssl_get_verify_result()
  2918.  *                 after loading the session again into a new SSL context
  2919.  *                 using \c mbedtls_ssl_set_session().
  2920.  *
  2921.  * \note           Once the session object is not needed anymore, you should
  2922.  *                 free it by calling \c mbedtls_ssl_session_free().
  2923.  *
  2924.  * \sa             mbedtls_ssl_set_session()
  2925.  */
  2926. int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *session );
  2927. #endif /* MBEDTLS_SSL_CLI_C */
  2928.  
  2929. /**
  2930.  * \brief          Perform the SSL handshake
  2931.  *
  2932.  * \param ssl      SSL context
  2933.  *
  2934.  * \return         \c 0 if successful.
  2935.  * \return         #MBEDTLS_ERR_SSL_WANT_READ or #MBEDTLS_ERR_SSL_WANT_WRITE
  2936.  *                 if the handshake is incomplete and waiting for data to
  2937.  *                 be available for reading from or writing to the underlying
  2938.  *                 transport - in this case you must call this function again
  2939.  *                 when the underlying transport is ready for the operation.
  2940.  * \return         #MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS if an asynchronous
  2941.  *                 operation is in progress (see
  2942.  *                 mbedtls_ssl_conf_async_private_cb()) - in this case you
  2943.  *                 must call this function again when the operation is ready.
  2944.  * \return         #MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS if a cryptographic
  2945.  *                 operation is in progress (see mbedtls_ecp_set_max_ops()) -
  2946.  *                 in this case you must call this function again to complete
  2947.  *                 the handshake when you're done attending other tasks.
  2948.  * \return         #MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED if DTLS is in use
  2949.  *                 and the client did not demonstrate reachability yet - in
  2950.  *                 this case you must stop using the context (see below).
  2951.  * \return         Another SSL error code - in this case you must stop using
  2952.  *                 the context (see below).
  2953.  *
  2954.  * \warning        If this function returns something other than
  2955.  *                 \c 0,
  2956.  *                 #MBEDTLS_ERR_SSL_WANT_READ,
  2957.  *                 #MBEDTLS_ERR_SSL_WANT_WRITE,
  2958.  *                 #MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS or
  2959.  *                 #MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS,
  2960.  *                 you must stop using the SSL context for reading or writing,
  2961.  *                 and either free it or call \c mbedtls_ssl_session_reset()
  2962.  *                 on it before re-using it for a new connection; the current
  2963.  *                 connection must be closed.
  2964.  *
  2965.  * \note           If DTLS is in use, then you may choose to handle
  2966.  *                 #MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED specially for logging
  2967.  *                 purposes, as it is an expected return value rather than an
  2968.  *                 actual error, but you still need to reset/free the context.
  2969.  *
  2970.  * \note           Remarks regarding event-driven DTLS:
  2971.  *                 If the function returns #MBEDTLS_ERR_SSL_WANT_READ, no datagram
  2972.  *                 from the underlying transport layer is currently being processed,
  2973.  *                 and it is safe to idle until the timer or the underlying transport
  2974.  *                 signal a new event. This is not true for a successful handshake,
  2975.  *                 in which case the datagram of the underlying transport that is
  2976.  *                 currently being processed might or might not contain further
  2977.  *                 DTLS records.
  2978.  */
  2979. int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl );
  2980.  
  2981. /**
  2982.  * \brief          Perform a single step of the SSL handshake
  2983.  *
  2984.  * \note           The state of the context (ssl->state) will be at
  2985.  *                 the next state after this function returns \c 0. Do not
  2986.  *                 call this function if state is MBEDTLS_SSL_HANDSHAKE_OVER.
  2987.  *
  2988.  * \param ssl      SSL context
  2989.  *
  2990.  * \return         See mbedtls_ssl_handshake().
  2991.  *
  2992.  * \warning        If this function returns something other than \c 0,
  2993.  *                 #MBEDTLS_ERR_SSL_WANT_READ, #MBEDTLS_ERR_SSL_WANT_WRITE,
  2994.  *                 #MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS or
  2995.  *                 #MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS, you must stop using
  2996.  *                 the SSL context for reading or writing, and either free it
  2997.  *                 or call \c mbedtls_ssl_session_reset() on it before
  2998.  *                 re-using it for a new connection; the current connection
  2999.  *                 must be closed.
  3000.  */
  3001. int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl );
  3002.  
  3003. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  3004. /**
  3005.  * \brief          Initiate an SSL renegotiation on the running connection.
  3006.  *                 Client: perform the renegotiation right now.
  3007.  *                 Server: request renegotiation, which will be performed
  3008.  *                 during the next call to mbedtls_ssl_read() if honored by
  3009.  *                 client.
  3010.  *
  3011.  * \param ssl      SSL context
  3012.  *
  3013.  * \return         0 if successful, or any mbedtls_ssl_handshake() return
  3014.  *                 value except #MBEDTLS_ERR_SSL_CLIENT_RECONNECT that can't
  3015.  *                 happen during a renegotiation.
  3016.  *
  3017.  * \warning        If this function returns something other than \c 0,
  3018.  *                 #MBEDTLS_ERR_SSL_WANT_READ, #MBEDTLS_ERR_SSL_WANT_WRITE,
  3019.  *                 #MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS or
  3020.  *                 #MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS, you must stop using
  3021.  *                 the SSL context for reading or writing, and either free it
  3022.  *                 or call \c mbedtls_ssl_session_reset() on it before
  3023.  *                 re-using it for a new connection; the current connection
  3024.  *                 must be closed.
  3025.  *
  3026.  */
  3027. int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl );
  3028. #endif /* MBEDTLS_SSL_RENEGOTIATION */
  3029.  
  3030. /**
  3031.  * \brief          Read at most 'len' application data bytes
  3032.  *
  3033.  * \param ssl      SSL context
  3034.  * \param buf      buffer that will hold the data
  3035.  * \param len      maximum number of bytes to read
  3036.  *
  3037.  * \return         The (positive) number of bytes read if successful.
  3038.  * \return         \c 0 if the read end of the underlying transport was closed
  3039.  *                 - in this case you must stop using the context (see below).
  3040.  * \return         #MBEDTLS_ERR_SSL_WANT_READ or #MBEDTLS_ERR_SSL_WANT_WRITE
  3041.  *                 if the handshake is incomplete and waiting for data to
  3042.  *                 be available for reading from or writing to the underlying
  3043.  *                 transport - in this case you must call this function again
  3044.  *                 when the underlying transport is ready for the operation.
  3045.  * \return         #MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS if an asynchronous
  3046.  *                 operation is in progress (see
  3047.  *                 mbedtls_ssl_conf_async_private_cb()) - in this case you
  3048.  *                 must call this function again when the operation is ready.
  3049.  * \return         #MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS if a cryptographic
  3050.  *                 operation is in progress (see mbedtls_ecp_set_max_ops()) -
  3051.  *                 in this case you must call this function again to complete
  3052.  *                 the handshake when you're done attending other tasks.
  3053.  * \return         #MBEDTLS_ERR_SSL_CLIENT_RECONNECT if we're at the server
  3054.  *                 side of a DTLS connection and the client is initiating a
  3055.  *                 new connection using the same source port. See below.
  3056.  * \return         Another SSL error code - in this case you must stop using
  3057.  *                 the context (see below).
  3058.  *
  3059.  * \warning        If this function returns something other than
  3060.  *                 a positive value,
  3061.  *                 #MBEDTLS_ERR_SSL_WANT_READ,
  3062.  *                 #MBEDTLS_ERR_SSL_WANT_WRITE,
  3063.  *                 #MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS,
  3064.  *                 #MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS or
  3065.  *                 #MBEDTLS_ERR_SSL_CLIENT_RECONNECT,
  3066.  *                 you must stop using the SSL context for reading or writing,
  3067.  *                 and either free it or call \c mbedtls_ssl_session_reset()
  3068.  *                 on it before re-using it for a new connection; the current
  3069.  *                 connection must be closed.
  3070.  *
  3071.  * \note           When this function returns #MBEDTLS_ERR_SSL_CLIENT_RECONNECT
  3072.  *                 (which can only happen server-side), it means that a client
  3073.  *                 is initiating a new connection using the same source port.
  3074.  *                 You can either treat that as a connection close and wait
  3075.  *                 for the client to resend a ClientHello, or directly
  3076.  *                 continue with \c mbedtls_ssl_handshake() with the same
  3077.  *                 context (as it has been reset internally). Either way, you
  3078.  *                 must make sure this is seen by the application as a new
  3079.  *                 connection: application state, if any, should be reset, and
  3080.  *                 most importantly the identity of the client must be checked
  3081.  *                 again. WARNING: not validating the identity of the client
  3082.  *                 again, or not transmitting the new identity to the
  3083.  *                 application layer, would allow authentication bypass!
  3084.  *
  3085.  * \note           Remarks regarding event-driven DTLS:
  3086.  *                 - If the function returns #MBEDTLS_ERR_SSL_WANT_READ, no datagram
  3087.  *                   from the underlying transport layer is currently being processed,
  3088.  *                   and it is safe to idle until the timer or the underlying transport
  3089.  *                   signal a new event.
  3090.  *                 - This function may return MBEDTLS_ERR_SSL_WANT_READ even if data was
  3091.  *                   initially available on the underlying transport, as this data may have
  3092.  *                   been only e.g. duplicated messages or a renegotiation request.
  3093.  *                   Therefore, you must be prepared to receive MBEDTLS_ERR_SSL_WANT_READ even
  3094.  *                   when reacting to an incoming-data event from the underlying transport.
  3095.  *                 - On success, the datagram of the underlying transport that is currently
  3096.  *                   being processed may contain further DTLS records. You should call
  3097.  *                   \c mbedtls_ssl_check_pending to check for remaining records.
  3098.  *
  3099.  */
  3100. int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len );
  3101.  
  3102. /**
  3103.  * \brief          Try to write exactly 'len' application data bytes
  3104.  *
  3105.  * \warning        This function will do partial writes in some cases. If the
  3106.  *                 return value is non-negative but less than length, the
  3107.  *                 function must be called again with updated arguments:
  3108.  *                 buf + ret, len - ret (if ret is the return value) until
  3109.  *                 it returns a value equal to the last 'len' argument.
  3110.  *
  3111.  * \param ssl      SSL context
  3112.  * \param buf      buffer holding the data
  3113.  * \param len      how many bytes must be written
  3114.  *
  3115.  * \return         The (non-negative) number of bytes actually written if
  3116.  *                 successful (may be less than \p len).
  3117.  * \return         #MBEDTLS_ERR_SSL_WANT_READ or #MBEDTLS_ERR_SSL_WANT_WRITE
  3118.  *                 if the handshake is incomplete and waiting for data to
  3119.  *                 be available for reading from or writing to the underlying
  3120.  *                 transport - in this case you must call this function again
  3121.  *                 when the underlying transport is ready for the operation.
  3122.  * \return         #MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS if an asynchronous
  3123.  *                 operation is in progress (see
  3124.  *                 mbedtls_ssl_conf_async_private_cb()) - in this case you
  3125.  *                 must call this function again when the operation is ready.
  3126.  * \return         #MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS if a cryptographic
  3127.  *                 operation is in progress (see mbedtls_ecp_set_max_ops()) -
  3128.  *                 in this case you must call this function again to complete
  3129.  *                 the handshake when you're done attending other tasks.
  3130.  * \return         Another SSL error code - in this case you must stop using
  3131.  *                 the context (see below).
  3132.  *
  3133.  * \warning        If this function returns something other than
  3134.  *                 a non-negative value,
  3135.  *                 #MBEDTLS_ERR_SSL_WANT_READ,
  3136.  *                 #MBEDTLS_ERR_SSL_WANT_WRITE,
  3137.  *                 #MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS or
  3138.  *                 #MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS,
  3139.  *                 you must stop using the SSL context for reading or writing,
  3140.  *                 and either free it or call \c mbedtls_ssl_session_reset()
  3141.  *                 on it before re-using it for a new connection; the current
  3142.  *                 connection must be closed.
  3143.  *
  3144.  * \note           When this function returns #MBEDTLS_ERR_SSL_WANT_WRITE/READ,
  3145.  *                 it must be called later with the *same* arguments,
  3146.  *                 until it returns a value greater that or equal to 0. When
  3147.  *                 the function returns #MBEDTLS_ERR_SSL_WANT_WRITE there may be
  3148.  *                 some partial data in the output buffer, however this is not
  3149.  *                 yet sent.
  3150.  *
  3151.  * \note           If the requested length is greater than the maximum
  3152.  *                 fragment length (either the built-in limit or the one set
  3153.  *                 or negotiated with the peer), then:
  3154.  *                 - with TLS, less bytes than requested are written.
  3155.  *                 - with DTLS, MBEDTLS_ERR_SSL_BAD_INPUT_DATA is returned.
  3156.  *                 \c mbedtls_ssl_get_max_frag_len() may be used to query the
  3157.  *                 active maximum fragment length.
  3158.  *
  3159.  * \note           Attempting to write 0 bytes will result in an empty TLS
  3160.  *                 application record being sent.
  3161.  */
  3162. int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len );
  3163.  
  3164. /**
  3165.  * \brief           Send an alert message
  3166.  *
  3167.  * \param ssl       SSL context
  3168.  * \param level     The alert level of the message
  3169.  *                  (MBEDTLS_SSL_ALERT_LEVEL_WARNING or MBEDTLS_SSL_ALERT_LEVEL_FATAL)
  3170.  * \param message   The alert message (SSL_ALERT_MSG_*)
  3171.  *
  3172.  * \return          0 if successful, or a specific SSL error code.
  3173.  *
  3174.  * \note           If this function returns something other than 0 or
  3175.  *                 MBEDTLS_ERR_SSL_WANT_READ/WRITE, you must stop using
  3176.  *                 the SSL context for reading or writing, and either free it or
  3177.  *                 call \c mbedtls_ssl_session_reset() on it before re-using it
  3178.  *                 for a new connection; the current connection must be closed.
  3179.  */
  3180. int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
  3181.                             unsigned char level,
  3182.                             unsigned char message );
  3183. /**
  3184.  * \brief          Notify the peer that the connection is being closed
  3185.  *
  3186.  * \param ssl      SSL context
  3187.  *
  3188.  * \return          0 if successful, or a specific SSL error code.
  3189.  *
  3190.  * \note           If this function returns something other than 0 or
  3191.  *                 MBEDTLS_ERR_SSL_WANT_READ/WRITE, you must stop using
  3192.  *                 the SSL context for reading or writing, and either free it or
  3193.  *                 call \c mbedtls_ssl_session_reset() on it before re-using it
  3194.  *                 for a new connection; the current connection must be closed.
  3195.  */
  3196. int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl );
  3197.  
  3198. /**
  3199.  * \brief          Free referenced items in an SSL context and clear memory
  3200.  *
  3201.  * \param ssl      SSL context
  3202.  */
  3203. void mbedtls_ssl_free( mbedtls_ssl_context *ssl );
  3204.  
  3205. /**
  3206.  * \brief          Initialize an SSL configuration context
  3207.  *                 Just makes the context ready for
  3208.  *                 mbedtls_ssl_config_defaults() or mbedtls_ssl_config_free().
  3209.  *
  3210.  * \note           You need to call mbedtls_ssl_config_defaults() unless you
  3211.  *                 manually set all of the relevant fields yourself.
  3212.  *
  3213.  * \param conf     SSL configuration context
  3214.  */
  3215. void mbedtls_ssl_config_init( mbedtls_ssl_config *conf );
  3216.  
  3217. /**
  3218.  * \brief          Load reasonnable default SSL configuration values.
  3219.  *                 (You need to call mbedtls_ssl_config_init() first.)
  3220.  *
  3221.  * \param conf     SSL configuration context
  3222.  * \param endpoint MBEDTLS_SSL_IS_CLIENT or MBEDTLS_SSL_IS_SERVER
  3223.  * \param transport MBEDTLS_SSL_TRANSPORT_STREAM for TLS, or
  3224.  *                  MBEDTLS_SSL_TRANSPORT_DATAGRAM for DTLS
  3225.  * \param preset   a MBEDTLS_SSL_PRESET_XXX value
  3226.  *
  3227.  * \note           See \c mbedtls_ssl_conf_transport() for notes on DTLS.
  3228.  *
  3229.  * \return         0 if successful, or
  3230.  *                 MBEDTLS_ERR_XXX_ALLOC_FAILED on memory allocation error.
  3231.  */
  3232. int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
  3233.                                  int endpoint, int transport, int preset );
  3234.  
  3235. /**
  3236.  * \brief          Free an SSL configuration context
  3237.  *
  3238.  * \param conf     SSL configuration context
  3239.  */
  3240. void mbedtls_ssl_config_free( mbedtls_ssl_config *conf );
  3241.  
  3242. /**
  3243.  * \brief          Initialize SSL session structure
  3244.  *
  3245.  * \param session  SSL session
  3246.  */
  3247. void mbedtls_ssl_session_init( mbedtls_ssl_session *session );
  3248.  
  3249. /**
  3250.  * \brief          Free referenced items in an SSL session including the
  3251.  *                 peer certificate and clear memory
  3252.  *
  3253.  * \note           A session object can be freed even if the SSL context
  3254.  *                 that was used to retrieve the session is still in use.
  3255.  *
  3256.  * \param session  SSL session
  3257.  */
  3258. void mbedtls_ssl_session_free( mbedtls_ssl_session *session );
  3259.  
  3260. #ifdef __cplusplus
  3261. }
  3262. #endif
  3263.  
  3264. #endif /* ssl.h */
  3265.