esp_tls.h 32 KB

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