esp_tls.h 29 KB

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