esp_tls.h 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  1. /*
  2. * SPDX-FileCopyrightText: 2017-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #ifndef _ESP_TLS_H_
  7. #define _ESP_TLS_H_
  8. #include <stdbool.h>
  9. #include "esp_err.h"
  10. #include "esp_tls_errors.h"
  11. #include "sdkconfig.h"
  12. #ifdef CONFIG_ESP_TLS_USING_MBEDTLS
  13. #include "mbedtls/ssl.h"
  14. #include "mbedtls/x509_crt.h"
  15. #ifdef CONFIG_ESP_TLS_SERVER_SESSION_TICKETS
  16. #include "mbedtls/ssl_ticket.h"
  17. #include "mbedtls/entropy.h"
  18. #include "mbedtls/ctr_drbg.h"
  19. #endif
  20. #elif CONFIG_ESP_TLS_USING_WOLFSSL
  21. #include "wolfssl/wolfcrypt/settings.h"
  22. #include "wolfssl/ssl.h"
  23. #endif
  24. #ifdef __cplusplus
  25. extern "C" {
  26. #endif
  27. /**
  28. * @brief ESP-TLS Connection State
  29. */
  30. typedef enum esp_tls_conn_state {
  31. ESP_TLS_INIT = 0,
  32. ESP_TLS_CONNECTING,
  33. ESP_TLS_HANDSHAKE,
  34. ESP_TLS_FAIL,
  35. ESP_TLS_DONE,
  36. } esp_tls_conn_state_t;
  37. typedef enum esp_tls_role {
  38. ESP_TLS_CLIENT = 0,
  39. ESP_TLS_SERVER,
  40. } esp_tls_role_t;
  41. /**
  42. * @brief ESP-TLS preshared key and hint structure
  43. */
  44. typedef struct psk_key_hint {
  45. const uint8_t* key; /*!< key in PSK authentication mode in binary format */
  46. const size_t key_size; /*!< length of the key */
  47. const char* hint; /*!< hint in PSK authentication mode in string format */
  48. } psk_hint_key_t;
  49. /**
  50. * @brief esp-tls client session ticket ctx
  51. */
  52. #ifdef CONFIG_ESP_TLS_CLIENT_SESSION_TICKETS
  53. typedef struct esp_tls_client_session {
  54. mbedtls_ssl_session saved_session;
  55. } esp_tls_client_session_t;
  56. #endif /* CONFIG_ESP_TLS_CLIENT_SESSION_TICKETS */
  57. /**
  58. * @brief Keep alive parameters structure
  59. */
  60. typedef struct tls_keep_alive_cfg {
  61. bool keep_alive_enable; /*!< Enable keep-alive timeout */
  62. int keep_alive_idle; /*!< Keep-alive idle time (second) */
  63. int keep_alive_interval; /*!< Keep-alive interval time (second) */
  64. int keep_alive_count; /*!< Keep-alive packet retry send count */
  65. } tls_keep_alive_cfg_t;
  66. /*
  67. * @brief ESP-TLS Address families
  68. */
  69. typedef enum esp_tls_addr_family {
  70. ESP_TLS_AF_UNSPEC = 0, /**< Unspecified address family. */
  71. ESP_TLS_AF_INET, /**< IPv4 address family. */
  72. ESP_TLS_AF_INET6, /**< IPv6 address family. */
  73. } esp_tls_addr_family_t;
  74. /*
  75. * @brief ESP-TLS TLS Protocol version
  76. */
  77. typedef enum {
  78. ESP_TLS_VER_ANY = 0, /* No preference */
  79. ESP_TLS_VER_TLS_1_2 = 0x1, /* (D)TLS 1.2 */
  80. ESP_TLS_VER_TLS_1_3 = 0x2, /* (D)TLS 1.3 */
  81. ESP_TLS_VER_TLS_MAX, /* to indicate max */
  82. } esp_tls_proto_ver_t;
  83. /**
  84. * @brief ESP-TLS configuration parameters
  85. *
  86. * @note Note about format of certificates:
  87. * - This structure includes certificates of a Certificate Authority, of client or server as well
  88. * as private keys, which may be of PEM or DER format. In case of PEM format, the buffer must be
  89. * NULL terminated (with NULL character included in certificate size).
  90. * - Certificate Authority's certificate may be a chain of certificates in case of PEM format,
  91. * but could be only one certificate in case of DER format
  92. * - Variables names of certificates and private key buffers and sizes are defined as unions providing
  93. * backward compatibility for legacy *_pem_buf and *_pem_bytes names which suggested only PEM format
  94. * was supported. It is encouraged to use generic names such as cacert_buf and cacert_bytes.
  95. */
  96. typedef struct esp_tls_cfg {
  97. const char **alpn_protos; /*!< Application protocols required for HTTP2.
  98. If HTTP2/ALPN support is required, a list
  99. of protocols that should be negotiated.
  100. The format is length followed by protocol
  101. name.
  102. For the most common cases the following is ok:
  103. const char **alpn_protos = { "h2", NULL };
  104. - where 'h2' is the protocol name */
  105. union {
  106. const unsigned char *cacert_buf; /*!< Certificate Authority's certificate in a buffer.
  107. Format may be PEM or DER, depending on mbedtls-support
  108. This buffer should be NULL terminated in case of PEM */
  109. const unsigned char *cacert_pem_buf; /*!< CA certificate buffer legacy name */
  110. };
  111. union {
  112. unsigned int cacert_bytes; /*!< Size of Certificate Authority certificate
  113. pointed to by cacert_buf
  114. (including NULL-terminator in case of PEM format) */
  115. unsigned int cacert_pem_bytes; /*!< Size of Certificate Authority certificate legacy name */
  116. };
  117. union {
  118. const unsigned char *clientcert_buf; /*!< Client certificate in a buffer
  119. Format may be PEM or DER, depending on mbedtls-support
  120. This buffer should be NULL terminated in case of PEM */
  121. const unsigned char *clientcert_pem_buf; /*!< Client certificate legacy name */
  122. };
  123. union {
  124. unsigned int clientcert_bytes; /*!< Size of client certificate pointed to by
  125. clientcert_pem_buf
  126. (including NULL-terminator in case of PEM format) */
  127. unsigned int clientcert_pem_bytes; /*!< Size of client certificate legacy name */
  128. };
  129. union {
  130. const unsigned char *clientkey_buf; /*!< Client key in a buffer
  131. Format may be PEM or DER, depending on mbedtls-support
  132. This buffer should be NULL terminated in case of PEM */
  133. const unsigned char *clientkey_pem_buf; /*!< Client key legacy name */
  134. };
  135. union {
  136. unsigned int clientkey_bytes; /*!< Size of client key pointed to by
  137. clientkey_pem_buf
  138. (including NULL-terminator in case of PEM format) */
  139. unsigned int clientkey_pem_bytes; /*!< Size of client key legacy name */
  140. };
  141. const unsigned char *clientkey_password;/*!< Client key decryption password string */
  142. unsigned int clientkey_password_len; /*!< String length of the password pointed to by
  143. clientkey_password */
  144. bool use_ecdsa_peripheral; /*!< Use the ECDSA peripheral for the private key operations */
  145. uint8_t ecdsa_key_efuse_blk; /*!< The efuse block where the ECDSA key is stored */
  146. bool non_block; /*!< Configure non-blocking mode. If set to true the
  147. underneath socket will be configured in non
  148. blocking mode after tls session is established */
  149. bool use_secure_element; /*!< Enable this option to use secure element or
  150. atecc608a chip ( Integrated with ESP32-WROOM-32SE ) */
  151. int timeout_ms; /*!< Network timeout in milliseconds.
  152. Note: If this value is not set, by default the timeout is
  153. set to 10 seconds. If you wish that the session should wait
  154. indefinitely then please use a larger value e.g., INT32_MAX */
  155. bool use_global_ca_store; /*!< Use a global ca_store for all the connections in which
  156. this bool is set. */
  157. const char *common_name; /*!< If non-NULL, server certificate CN must match this name.
  158. If NULL, server certificate CN must match hostname. */
  159. bool skip_common_name; /*!< Skip any validation of server certificate CN field */
  160. tls_keep_alive_cfg_t *keep_alive_cfg; /*!< Enable TCP keep-alive timeout for SSL connection */
  161. const psk_hint_key_t* psk_hint_key; /*!< Pointer to PSK hint and key. if not NULL (and certificates are NULL)
  162. then PSK authentication is enabled with configured setup.
  163. Important note: the pointer must be valid for connection */
  164. esp_err_t (*crt_bundle_attach)(void *conf);
  165. /*!< Function pointer to esp_crt_bundle_attach. Enables the use of certification
  166. bundle for server verification, must be enabled in menuconfig */
  167. void *ds_data; /*!< Pointer for digital signature peripheral context */
  168. bool is_plain_tcp; /*!< Use non-TLS connection: When set to true, the esp-tls uses
  169. plain TCP transport rather then TLS/SSL connection.
  170. Note, that it is possible to connect using a plain tcp transport
  171. directly with esp_tls_plain_tcp_connect() API */
  172. struct ifreq *if_name; /*!< The name of interface for data to go through. Use the default interface without setting */
  173. #ifdef CONFIG_ESP_TLS_CLIENT_SESSION_TICKETS
  174. esp_tls_client_session_t *client_session; /*! Pointer for the client session ticket context. */
  175. #endif /* CONFIG_ESP_TLS_CLIENT_SESSION_TICKETS */
  176. esp_tls_addr_family_t addr_family; /*!< The address family to use when connecting to a host. */
  177. const int *ciphersuites_list; /*!< Pointer to a zero-terminated array of IANA identifiers of TLS ciphersuites.
  178. Please check the list validity by esp_tls_get_ciphersuites_list() API */
  179. esp_tls_proto_ver_t tls_version; /*!< TLS protocol version of the connection, e.g., TLS 1.2, TLS 1.3 (default - no preference) */
  180. } esp_tls_cfg_t;
  181. #if defined(CONFIG_ESP_TLS_SERVER_SESSION_TICKETS)
  182. /**
  183. * @brief Data structures necessary to support TLS session tickets according to RFC5077
  184. */
  185. typedef struct esp_tls_server_session_ticket_ctx {
  186. mbedtls_entropy_context entropy; /*!< mbedTLS entropy context structure */
  187. mbedtls_ctr_drbg_context ctr_drbg; /*!< mbedTLS ctr drbg context structure.
  188. CTR_DRBG is deterministic random
  189. bit generation based on AES-256 */
  190. mbedtls_ssl_ticket_context ticket_ctx; /*!< Session ticket generation context */
  191. } esp_tls_server_session_ticket_ctx_t;
  192. #endif
  193. #if defined(CONFIG_ESP_TLS_SERVER_CERT_SELECT_HOOK)
  194. /**
  195. * @brief tls handshake callback
  196. * Can be used to configure per-handshake attributes for the TLS connection.
  197. * E.g. Client certificate / Key, Authmode, Client CA verification, etc.
  198. *
  199. * @param ssl mbedtls_ssl_context that can be used for changing settings
  200. * @return The reutn value of the callback must be 0 if successful,
  201. * or a specific MBEDTLS_ERR_XXX code, which will cause the handhsake to abort
  202. */
  203. typedef mbedtls_ssl_hs_cb_t esp_tls_handshake_callback;
  204. #endif
  205. /**
  206. * @brief ESP-TLS Server configuration parameters
  207. */
  208. typedef struct esp_tls_cfg_server {
  209. const char **alpn_protos; /*!< Application protocols required for HTTP2.
  210. If HTTP2/ALPN support is required, a list
  211. of protocols that should be negotiated.
  212. The format is length followed by protocol
  213. name.
  214. For the most common cases the following is ok:
  215. const char **alpn_protos = { "h2", NULL };
  216. - where 'h2' is the protocol name */
  217. union {
  218. const unsigned char *cacert_buf; /*!< Client CA certificate in a buffer.
  219. This buffer should be NULL terminated */
  220. const unsigned char *cacert_pem_buf; /*!< Client CA certificate legacy name */
  221. };
  222. union {
  223. unsigned int cacert_bytes; /*!< Size of client CA certificate
  224. pointed to by cacert_pem_buf */
  225. unsigned int cacert_pem_bytes; /*!< Size of client CA certificate legacy name */
  226. };
  227. union {
  228. const unsigned char *servercert_buf; /*!< Server certificate in a buffer
  229. This buffer should be NULL terminated */
  230. const unsigned char *servercert_pem_buf; /*!< Server certificate legacy name */
  231. };
  232. union {
  233. unsigned int servercert_bytes; /*!< Size of server certificate pointed to by
  234. servercert_pem_buf */
  235. unsigned int servercert_pem_bytes; /*!< Size of server certificate legacy name */
  236. };
  237. union {
  238. const unsigned char *serverkey_buf; /*!< Server key in a buffer
  239. This buffer should be NULL terminated */
  240. const unsigned char *serverkey_pem_buf; /*!< Server key legacy name */
  241. };
  242. union {
  243. unsigned int serverkey_bytes; /*!< Size of server key pointed to by
  244. serverkey_pem_buf */
  245. unsigned int serverkey_pem_bytes; /*!< Size of server key legacy name */
  246. };
  247. const unsigned char *serverkey_password; /*!< Server key decryption password string */
  248. unsigned int serverkey_password_len; /*!< String length of the password pointed to by
  249. serverkey_password */
  250. bool use_ecdsa_peripheral; /*!< Use ECDSA peripheral to use private key */
  251. uint8_t ecdsa_key_efuse_blk; /*!< The efuse block where ECDSA key is stored */
  252. bool use_secure_element; /*!< Enable this option to use secure element or
  253. atecc608a chip ( Integrated with ESP32-WROOM-32SE ) */
  254. #if defined(CONFIG_ESP_TLS_SERVER_SESSION_TICKETS)
  255. esp_tls_server_session_ticket_ctx_t * ticket_ctx; /*!< Session ticket generation context.
  256. You have to call esp_tls_cfg_server_session_tickets_init
  257. to use it.
  258. Call esp_tls_cfg_server_session_tickets_free
  259. to free the data associated with this context. */
  260. #endif
  261. void *userdata; /*!< User data to be added to the ssl context.
  262. Can be retrieved by callbacks */
  263. #if defined(CONFIG_ESP_TLS_SERVER_CERT_SELECT_HOOK)
  264. esp_tls_handshake_callback cert_select_cb; /*!< Certificate selection callback that gets called after ClientHello is processed.
  265. Can be used as an SNI callback, but also has access to other
  266. TLS extensions, such as ALPN and server_certificate_type . */
  267. #endif
  268. } esp_tls_cfg_server_t;
  269. /**
  270. * @brief Initialize the server side TLS session ticket context
  271. *
  272. * This function initializes the server side tls session ticket context
  273. * which holds all necessary data structures to enable tls session tickets
  274. * according to RFC5077.
  275. * Use esp_tls_cfg_server_session_tickets_free to free the data.
  276. *
  277. * @param[in] cfg server configuration as esp_tls_cfg_server_t
  278. * @return
  279. * ESP_OK if setup succeeded
  280. * ESP_ERR_INVALID_ARG if context is already initialized
  281. * ESP_ERR_NO_MEM if memory allocation failed
  282. * ESP_ERR_NOT_SUPPORTED if session tickets are not available due to build configuration
  283. * ESP_FAIL if setup failed
  284. */
  285. esp_err_t esp_tls_cfg_server_session_tickets_init(esp_tls_cfg_server_t *cfg);
  286. /**
  287. * @brief Free the server side TLS session ticket context
  288. *
  289. * @param cfg server configuration as esp_tls_cfg_server_t
  290. */
  291. void esp_tls_cfg_server_session_tickets_free(esp_tls_cfg_server_t *cfg);
  292. typedef struct esp_tls esp_tls_t;
  293. /**
  294. * @brief Create TLS connection
  295. *
  296. * This function allocates and initializes esp-tls structure handle.
  297. *
  298. * @return tls Pointer to esp-tls as esp-tls handle if successfully initialized,
  299. * NULL if allocation error
  300. */
  301. esp_tls_t *esp_tls_init(void);
  302. /**
  303. * @brief Create a new blocking TLS/SSL connection with a given "HTTP" url
  304. *
  305. * Note: This API is present for backward compatibility reasons. Alternative function
  306. * with the same functionality is `esp_tls_conn_http_new_sync` (and its asynchronous version
  307. * `esp_tls_conn_http_new_async`)
  308. *
  309. * @param[in] url url of host.
  310. * @param[in] cfg TLS configuration as esp_tls_cfg_t. If you wish to open
  311. * non-TLS connection, keep this NULL. For TLS connection,
  312. * a pass pointer to 'esp_tls_cfg_t'. At a minimum, this
  313. * structure should be zero-initialized.
  314. * @return pointer to esp_tls_t, or NULL if connection couldn't be opened.
  315. */
  316. esp_tls_t *esp_tls_conn_http_new(const char *url, const esp_tls_cfg_t *cfg) __attribute__((deprecated("Please use esp_tls_conn_http_new_sync (or its asynchronous version esp_tls_conn_http_new_async) instead")));
  317. /**
  318. * @brief Create a new blocking TLS/SSL connection
  319. *
  320. * This function establishes a TLS/SSL connection with the specified host in blocking manner.
  321. *
  322. * @param[in] hostname Hostname of the host.
  323. * @param[in] hostlen Length of hostname.
  324. * @param[in] port Port number of the host.
  325. * @param[in] cfg TLS configuration as esp_tls_cfg_t. If you wish to open
  326. * non-TLS connection, keep this NULL. For TLS connection,
  327. * a pass pointer to esp_tls_cfg_t. At a minimum, this
  328. * structure should be zero-initialized.
  329. * @param[in] tls Pointer to esp-tls as esp-tls handle.
  330. *
  331. * @return
  332. * - -1 If connection establishment fails.
  333. * - 1 If connection establishment is successful.
  334. * - 0 If connection state is in progress.
  335. */
  336. int esp_tls_conn_new_sync(const char *hostname, int hostlen, int port, const esp_tls_cfg_t *cfg, esp_tls_t *tls);
  337. /**
  338. * @brief Create a new blocking TLS/SSL connection with a given "HTTP" url
  339. *
  340. * The behaviour is same as esp_tls_conn_new_sync() API. However this API accepts host's url.
  341. *
  342. * @param[in] url url of host.
  343. * @param[in] cfg TLS configuration as esp_tls_cfg_t. If you wish to open
  344. * non-TLS connection, keep this NULL. For TLS connection,
  345. * a pass pointer to 'esp_tls_cfg_t'. At a minimum, this
  346. * structure should be zero-initialized.
  347. * @param[in] tls Pointer to esp-tls as esp-tls handle.
  348. *
  349. * @return
  350. * - -1 If connection establishment fails.
  351. * - 1 If connection establishment is successful.
  352. * - 0 If connection state is in progress.
  353. */
  354. int esp_tls_conn_http_new_sync(const char *url, const esp_tls_cfg_t *cfg, esp_tls_t *tls);
  355. /**
  356. * @brief Create a new non-blocking TLS/SSL connection
  357. *
  358. * This function initiates a non-blocking TLS/SSL connection with the specified host, but due to
  359. * its non-blocking nature, it doesn't wait for the connection to get established.
  360. *
  361. * @param[in] hostname Hostname of the host.
  362. * @param[in] hostlen Length of hostname.
  363. * @param[in] port Port number of the host.
  364. * @param[in] cfg TLS configuration as esp_tls_cfg_t. `non_block` member of
  365. * this structure should be set to be true.
  366. * @param[in] tls pointer to esp-tls as esp-tls handle.
  367. *
  368. * @return
  369. * - -1 If connection establishment fails.
  370. * - 0 If connection establishment is in progress.
  371. * - 1 If connection establishment is successful.
  372. */
  373. int esp_tls_conn_new_async(const char *hostname, int hostlen, int port, const esp_tls_cfg_t *cfg, esp_tls_t *tls);
  374. /**
  375. * @brief Create a new non-blocking TLS/SSL connection with a given "HTTP" url
  376. *
  377. * The behaviour is same as esp_tls_conn_new_async() API. However this API accepts host's url.
  378. *
  379. * @param[in] url url of host.
  380. * @param[in] cfg TLS configuration as esp_tls_cfg_t.
  381. * @param[in] tls pointer to esp-tls as esp-tls handle.
  382. *
  383. * @return
  384. * - -1 If connection establishment fails.
  385. * - 0 If connection establishment is in progress.
  386. * - 1 If connection establishment is successful.
  387. */
  388. int esp_tls_conn_http_new_async(const char *url, const esp_tls_cfg_t *cfg, esp_tls_t *tls);
  389. /**
  390. * @brief Write from buffer 'data' into specified tls connection.
  391. *
  392. * @param[in] tls pointer to esp-tls as esp-tls handle.
  393. * @param[in] data Buffer from which data will be written.
  394. * @param[in] datalen Length of data buffer.
  395. *
  396. * @return
  397. * - >=0 if write operation was successful, the return value is the number
  398. * of bytes actually written to the TLS/SSL connection.
  399. * - <0 if write operation was not successful, because either an
  400. * error occured or an action must be taken by the calling process.
  401. * - ESP_TLS_ERR_SSL_WANT_READ/
  402. * ESP_TLS_ERR_SSL_WANT_WRITE.
  403. * if the handshake is incomplete and waiting for data to be available for reading.
  404. * In this case this functions needs to be called again when the underlying transport is ready for operation.
  405. */
  406. ssize_t esp_tls_conn_write(esp_tls_t *tls, const void *data, size_t datalen);
  407. /**
  408. * @brief Read from specified tls connection into the buffer 'data'.
  409. *
  410. * @param[in] tls pointer to esp-tls as esp-tls handle.
  411. * @param[in] data Buffer to hold read data.
  412. * @param[in] datalen Length of data buffer.
  413. *
  414. * @return
  415. * - >0 if read operation was successful, the return value is the number
  416. * of bytes actually read from the TLS/SSL connection.
  417. * - 0 if read operation was not successful. The underlying
  418. * connection was closed.
  419. * - <0 if read operation was not successful, because either an
  420. * error occured or an action must be taken by the calling process.
  421. */
  422. ssize_t esp_tls_conn_read(esp_tls_t *tls, void *data, size_t datalen);
  423. /**
  424. * @brief Close the TLS/SSL connection and free any allocated resources.
  425. *
  426. * This function should be called to close each tls connection opened with
  427. * esp_tls_conn_new_sync() (or esp_tls_conn_http_new_sync()) and
  428. * esp_tls_conn_new_async() (or esp_tls_conn_http_new_async()) APIs.
  429. *
  430. * @param[in] tls pointer to esp-tls as esp-tls handle.
  431. *
  432. * @return - 0 on success
  433. * - -1 if socket error or an invalid argument
  434. */
  435. int esp_tls_conn_destroy(esp_tls_t *tls);
  436. /**
  437. * @brief Return the number of application data bytes remaining to be
  438. * read from the current record
  439. *
  440. * This API is a wrapper over mbedtls's mbedtls_ssl_get_bytes_avail() API.
  441. *
  442. * @param[in] tls pointer to esp-tls as esp-tls handle.
  443. *
  444. * @return
  445. * - -1 in case of invalid arg
  446. * - bytes available in the application data
  447. * record read buffer
  448. */
  449. ssize_t esp_tls_get_bytes_avail(esp_tls_t *tls);
  450. /**
  451. * @brief Returns the connection socket file descriptor from esp_tls session
  452. *
  453. * @param[in] tls handle to esp_tls context
  454. *
  455. * @param[out] sockfd int pointer to sockfd value.
  456. *
  457. * @return - ESP_OK on success and value of sockfd will be updated with socket file descriptor for connection
  458. * - ESP_ERR_INVALID_ARG if (tls == NULL || sockfd == NULL)
  459. */
  460. esp_err_t esp_tls_get_conn_sockfd(esp_tls_t *tls, int *sockfd);
  461. /**
  462. * @brief Sets the connection socket file descriptor for the esp_tls session
  463. *
  464. * @param[in] tls handle to esp_tls context
  465. *
  466. * @param[in] sockfd sockfd value to set.
  467. *
  468. * @return - ESP_OK on success and value of sockfd for the tls connection shall updated withthe provided value
  469. * - ESP_ERR_INVALID_ARG if (tls == NULL || sockfd < 0)
  470. */
  471. esp_err_t esp_tls_set_conn_sockfd(esp_tls_t *tls, int sockfd);
  472. /**
  473. * @brief Gets the connection state for the esp_tls session
  474. *
  475. * @param[in] tls handle to esp_tls context
  476. *
  477. * @param[out] conn_state pointer to the connection state value.
  478. *
  479. * @return - ESP_OK on success and value of sockfd for the tls connection shall updated withthe provided value
  480. * - ESP_ERR_INVALID_ARG (Invalid arguments)
  481. */
  482. esp_err_t esp_tls_get_conn_state(esp_tls_t *tls, esp_tls_conn_state_t *conn_state);
  483. /**
  484. * @brief Sets the connection state for the esp_tls session
  485. *
  486. * @param[in] tls handle to esp_tls context
  487. *
  488. * @param[in] conn_state connection state value to set.
  489. *
  490. * @return - ESP_OK on success and value of sockfd for the tls connection shall updated withthe provided value
  491. * - ESP_ERR_INVALID_ARG (Invalid arguments)
  492. */
  493. esp_err_t esp_tls_set_conn_state(esp_tls_t *tls, esp_tls_conn_state_t conn_state);
  494. /**
  495. * @brief Returns the ssl context
  496. *
  497. * @param[in] tls handle to esp_tls context
  498. *
  499. *
  500. * @return - ssl_ctx pointer to ssl context of underlying TLS layer on success
  501. * - NULL in case of error
  502. */
  503. void *esp_tls_get_ssl_context(esp_tls_t *tls);
  504. /**
  505. * @brief Create a global CA store, initially empty.
  506. *
  507. * This function should be called if the application wants to use the same CA store for multiple connections.
  508. * This function initialises the global CA store which can be then set by calling esp_tls_set_global_ca_store().
  509. * To be effective, this function must be called before any call to esp_tls_set_global_ca_store().
  510. *
  511. * @return
  512. * - ESP_OK if creating global CA store was successful.
  513. * - ESP_ERR_NO_MEM if an error occured when allocating the mbedTLS resources.
  514. */
  515. esp_err_t esp_tls_init_global_ca_store(void);
  516. /**
  517. * @brief Set the global CA store with the buffer provided in pem format.
  518. *
  519. * This function should be called if the application wants to set the global CA store for
  520. * multiple connections i.e. to add the certificates in the provided buffer to the certificate chain.
  521. * This function implicitly calls esp_tls_init_global_ca_store() if it has not already been called.
  522. * The application must call this function before calling esp_tls_conn_new().
  523. *
  524. * @param[in] cacert_pem_buf Buffer which has certificates in pem format. This buffer
  525. * is used for creating a global CA store, which can be used
  526. * by other tls connections.
  527. * @param[in] cacert_pem_bytes Length of the buffer.
  528. *
  529. * @return
  530. * - ESP_OK if adding certificates was successful.
  531. * - Other if an error occured or an action must be taken by the calling process.
  532. */
  533. esp_err_t esp_tls_set_global_ca_store(const unsigned char *cacert_pem_buf, const unsigned int cacert_pem_bytes);
  534. /**
  535. * @brief Free the global CA store currently being used.
  536. *
  537. * The memory being used by the global CA store to store all the parsed certificates is
  538. * freed up. The application can call this API if it no longer needs the global CA store.
  539. */
  540. void esp_tls_free_global_ca_store(void);
  541. /**
  542. * @brief Returns last error in esp_tls with detailed mbedtls related error codes.
  543. * The error information is cleared internally upon return
  544. *
  545. * @param[in] h esp-tls error handle.
  546. * @param[out] esp_tls_code last error code returned from mbedtls api (set to zero if none)
  547. * This pointer could be NULL if caller does not care about esp_tls_code
  548. * @param[out] esp_tls_flags last certification verification flags (set to zero if none)
  549. * This pointer could be NULL if caller does not care about esp_tls_code
  550. *
  551. * @return
  552. * - ESP_ERR_INVALID_STATE if invalid parameters
  553. * - ESP_OK (0) if no error occurred
  554. * - specific error code (based on ESP_ERR_ESP_TLS_BASE) otherwise
  555. */
  556. esp_err_t esp_tls_get_and_clear_last_error(esp_tls_error_handle_t h, int *esp_tls_code, int *esp_tls_flags);
  557. /**
  558. * @brief Returns the last error captured in esp_tls of a specific type
  559. * The error information is cleared internally upon return
  560. *
  561. * @param[in] h esp-tls error handle.
  562. * @param[in] err_type specific error type
  563. * @param[out] error_code last error code returned from mbedtls api (set to zero if none)
  564. * This pointer could be NULL if caller does not care about esp_tls_code
  565. * @return
  566. * - ESP_ERR_INVALID_STATE if invalid parameters
  567. * - ESP_OK if a valid error returned and was cleared
  568. */
  569. esp_err_t esp_tls_get_and_clear_error_type(esp_tls_error_handle_t h, esp_tls_error_type_t err_type, int *error_code);
  570. /**
  571. * @brief Returns the ESP-TLS error_handle
  572. *
  573. * @param[in] tls handle to esp_tls context
  574. *
  575. * @param[out] error_handle pointer to the error handle.
  576. *
  577. * @return
  578. * - ESP_OK on success and error_handle will be updated with the ESP-TLS error handle.
  579. *
  580. * - ESP_ERR_INVALID_ARG if (tls == NULL || error_handle == NULL)
  581. */
  582. esp_err_t esp_tls_get_error_handle(esp_tls_t *tls, esp_tls_error_handle_t *error_handle);
  583. #if CONFIG_ESP_TLS_USING_MBEDTLS
  584. /**
  585. * @brief Get the pointer to the global CA store currently being used.
  586. *
  587. * The application must first call esp_tls_set_global_ca_store(). Then the same
  588. * CA store could be used by the application for APIs other than esp_tls.
  589. *
  590. * @note Modifying the pointer might cause a failure in verifying the certificates.
  591. *
  592. * @return
  593. * - Pointer to the global CA store currently being used if successful.
  594. * - NULL if there is no global CA store set.
  595. */
  596. mbedtls_x509_crt *esp_tls_get_global_ca_store(void);
  597. /**
  598. * @brief Get supported TLS ciphersuites list.
  599. *
  600. * See https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-4 for the list of ciphersuites
  601. *
  602. * @return Pointer to a zero-terminated array of IANA identifiers of TLS ciphersuites.
  603. *
  604. */
  605. const int *esp_tls_get_ciphersuites_list(void);
  606. #endif /* CONFIG_ESP_TLS_USING_MBEDTLS */
  607. /**
  608. * @brief Create TLS/SSL server session
  609. *
  610. * This function creates a TLS/SSL server context for already accepted client connection
  611. * and performs TLS/SSL handshake with the client
  612. *
  613. * @param[in] cfg Pointer to esp_tls_cfg_server_t
  614. * @param[in] sockfd FD of accepted connection
  615. * @param[out] tls Pointer to allocated esp_tls_t
  616. *
  617. * @return
  618. * - 0 if successful
  619. * - <0 in case of error
  620. *
  621. */
  622. int esp_tls_server_session_create(esp_tls_cfg_server_t *cfg, int sockfd, esp_tls_t *tls);
  623. /**
  624. * @brief Close the server side TLS/SSL connection and free any allocated resources.
  625. *
  626. * This function should be called to close each tls connection opened with esp_tls_server_session_create()
  627. *
  628. * @param[in] tls pointer to esp_tls_t
  629. */
  630. void esp_tls_server_session_delete(esp_tls_t *tls);
  631. /**
  632. * @brief Creates a plain TCP connection, returning a valid socket fd on success or an error handle
  633. *
  634. * @param[in] host Hostname of the host.
  635. * @param[in] hostlen Length of hostname.
  636. * @param[in] port Port number of the host.
  637. * @param[in] cfg ESP-TLS configuration as esp_tls_cfg_t.
  638. * @param[out] error_handle ESP-TLS error handle holding potential errors occurred during connection
  639. * @param[out] sockfd Socket descriptor if successfully connected on TCP layer
  640. * @return ESP_OK on success
  641. * ESP_ERR_INVALID_ARG if invalid output parameters
  642. * ESP-TLS based error codes on failure
  643. */
  644. esp_err_t esp_tls_plain_tcp_connect(const char *host, int hostlen, int port, const esp_tls_cfg_t *cfg, esp_tls_error_handle_t error_handle, int *sockfd);
  645. #ifdef CONFIG_ESP_TLS_CLIENT_SESSION_TICKETS
  646. /**
  647. * @brief Obtain the client session ticket
  648. *
  649. * This function should be called when the TLS connection is already established.
  650. * This can be passed again in the esp_tls_cfg_t structure, to appropriate tls session create (e.g. esp_tls_conn_http_new_sync) API for session resumption.
  651. *
  652. * @param[in] esp_tls context as esp_tls_t
  653. * @return
  654. * Pointer to the saved client session.
  655. * NULL on Failure
  656. */
  657. esp_tls_client_session_t *esp_tls_get_client_session(esp_tls_t *tls);
  658. /**
  659. * @brief Free the client session
  660. *
  661. * This function should be called after esp_tls_get_client_session().
  662. *
  663. * @param[in] client_session context as esp_tls_client_session_t
  664. *
  665. */
  666. void esp_tls_free_client_session(esp_tls_client_session_t *client_session);
  667. #endif /* CONFIG_ESP_TLS_CLIENT_SESSION_TICKETS */
  668. #ifdef __cplusplus
  669. }
  670. #endif
  671. #endif /* ! _ESP_TLS_H_ */