esp_tls.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. // Copyright 2017-2018 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #ifndef _ESP_TLS_H_
  14. #define _ESP_TLS_H_
  15. #include <stdbool.h>
  16. #include <sys/socket.h>
  17. #include <fcntl.h>
  18. #include "esp_err.h"
  19. #ifdef CONFIG_ESP_TLS_USING_MBEDTLS
  20. #include "mbedtls/platform.h"
  21. #include "mbedtls/net_sockets.h"
  22. #include "mbedtls/esp_debug.h"
  23. #include "mbedtls/ssl.h"
  24. #include "mbedtls/entropy.h"
  25. #include "mbedtls/ctr_drbg.h"
  26. #include "mbedtls/error.h"
  27. #include "mbedtls/certs.h"
  28. #elif CONFIG_ESP_TLS_USING_WOLFSSL
  29. #include "wolfssl/wolfcrypt/settings.h"
  30. #include "wolfssl/ssl.h"
  31. #endif
  32. #ifdef __cplusplus
  33. extern "C" {
  34. #endif
  35. #define ESP_ERR_ESP_TLS_BASE 0x8000 /*!< Starting number of ESP-TLS error codes */
  36. #define ESP_ERR_ESP_TLS_CANNOT_RESOLVE_HOSTNAME (ESP_ERR_ESP_TLS_BASE + 0x01) /*!< Error if hostname couldn't be resolved upon tls connection */
  37. #define ESP_ERR_ESP_TLS_CANNOT_CREATE_SOCKET (ESP_ERR_ESP_TLS_BASE + 0x02) /*!< Failed to create socket */
  38. #define ESP_ERR_ESP_TLS_UNSUPPORTED_PROTOCOL_FAMILY (ESP_ERR_ESP_TLS_BASE + 0x03) /*!< Unsupported protocol family */
  39. #define ESP_ERR_ESP_TLS_FAILED_CONNECT_TO_HOST (ESP_ERR_ESP_TLS_BASE + 0x04) /*!< Failed to connect to host */
  40. #define ESP_ERR_ESP_TLS_SOCKET_SETOPT_FAILED (ESP_ERR_ESP_TLS_BASE + 0x05) /*!< failed to set socket option */
  41. #define ESP_ERR_MBEDTLS_CERT_PARTLY_OK (ESP_ERR_ESP_TLS_BASE + 0x06) /*!< mbedtls parse certificates was partly successful */
  42. #define ESP_ERR_MBEDTLS_CTR_DRBG_SEED_FAILED (ESP_ERR_ESP_TLS_BASE + 0x07) /*!< mbedtls api returned error */
  43. #define ESP_ERR_MBEDTLS_SSL_SET_HOSTNAME_FAILED (ESP_ERR_ESP_TLS_BASE + 0x08) /*!< mbedtls api returned error */
  44. #define ESP_ERR_MBEDTLS_SSL_CONFIG_DEFAULTS_FAILED (ESP_ERR_ESP_TLS_BASE + 0x09) /*!< mbedtls api returned error */
  45. #define ESP_ERR_MBEDTLS_SSL_CONF_ALPN_PROTOCOLS_FAILED (ESP_ERR_ESP_TLS_BASE + 0x0A) /*!< mbedtls api returned error */
  46. #define ESP_ERR_MBEDTLS_X509_CRT_PARSE_FAILED (ESP_ERR_ESP_TLS_BASE + 0x0B) /*!< mbedtls api returned error */
  47. #define ESP_ERR_MBEDTLS_SSL_CONF_OWN_CERT_FAILED (ESP_ERR_ESP_TLS_BASE + 0x0C) /*!< mbedtls api returned error */
  48. #define ESP_ERR_MBEDTLS_SSL_SETUP_FAILED (ESP_ERR_ESP_TLS_BASE + 0x0D) /*!< mbedtls api returned error */
  49. #define ESP_ERR_MBEDTLS_SSL_WRITE_FAILED (ESP_ERR_ESP_TLS_BASE + 0x0E) /*!< mbedtls api returned error */
  50. #define ESP_ERR_MBEDTLS_PK_PARSE_KEY_FAILED (ESP_ERR_ESP_TLS_BASE + 0x0F) /*!< mbedtls api returned failed */
  51. #define ESP_ERR_MBEDTLS_SSL_HANDSHAKE_FAILED (ESP_ERR_ESP_TLS_BASE + 0x10) /*!< mbedtls api returned failed */
  52. #define ESP_ERR_MBEDTLS_SSL_CONF_PSK_FAILED (ESP_ERR_ESP_TLS_BASE + 0x11) /*!< mbedtls api returned failed */
  53. #define ESP_ERR_ESP_TLS_CONNECTION_TIMEOUT (ESP_ERR_ESP_TLS_BASE + 0x12) /*!< new connection in esp_tls_low_level_conn connection timeouted */
  54. #define ESP_ERR_WOLFSSL_SSL_SET_HOSTNAME_FAILED (ESP_ERR_ESP_TLS_BASE + 0x13) /*!< wolfSSL api returned error */
  55. #define ESP_ERR_WOLFSSL_SSL_CONF_ALPN_PROTOCOLS_FAILED (ESP_ERR_ESP_TLS_BASE + 0x14) /*!< wolfSSL api returned error */
  56. #define ESP_ERR_WOLFSSL_CERT_VERIFY_SETUP_FAILED (ESP_ERR_ESP_TLS_BASE + 0x15) /*!< wolfSSL api returned error */
  57. #define ESP_ERR_WOLFSSL_KEY_VERIFY_SETUP_FAILED (ESP_ERR_ESP_TLS_BASE + 0x16) /*!< wolfSSL api returned error */
  58. #define ESP_ERR_WOLFSSL_SSL_HANDSHAKE_FAILED (ESP_ERR_ESP_TLS_BASE + 0x17) /*!< wolfSSL api returned failed */
  59. #define ESP_ERR_WOLFSSL_CTX_SETUP_FAILED (ESP_ERR_ESP_TLS_BASE + 0x18) /*!< wolfSSL api returned failed */
  60. #define ESP_ERR_WOLFSSL_SSL_SETUP_FAILED (ESP_ERR_ESP_TLS_BASE + 0x19) /*!< wolfSSL api returned failed */
  61. #define ESP_ERR_WOLFSSL_SSL_WRITE_FAILED (ESP_ERR_ESP_TLS_BASE + 0x1A) /*!< wolfSSL api returned failed */
  62. #define ESP_ERR_ESP_TLS_SE_FAILED (ESP_ERR_ESP_TLS_BASE + 0x1B) /*< esp-tls use Secure Element returned failed */
  63. #ifdef CONFIG_ESP_TLS_USING_MBEDTLS
  64. #define ESP_TLS_ERR_SSL_WANT_READ MBEDTLS_ERR_SSL_WANT_READ
  65. #define ESP_TLS_ERR_SSL_WANT_WRITE MBEDTLS_ERR_SSL_WANT_WRITE
  66. #define ESP_TLS_ERR_SSL_TIMEOUT MBEDTLS_ERR_SSL_TIMEOUT
  67. #elif CONFIG_ESP_TLS_USING_WOLFSSL /* CONFIG_ESP_TLS_USING_MBEDTLS */
  68. #define ESP_TLS_ERR_SSL_WANT_READ WOLFSSL_ERROR_WANT_READ
  69. #define ESP_TLS_ERR_SSL_WANT_WRITE WOLFSSL_ERROR_WANT_WRITE
  70. #define ESP_TLS_ERR_SSL_TIMEOUT WOLFSSL_CBIO_ERR_TIMEOUT
  71. #endif /*CONFIG_ESP_TLS_USING_WOLFSSL */
  72. typedef struct esp_tls_last_error* esp_tls_error_handle_t;
  73. /**
  74. * @brief Error structure containing relevant errors in case tls error occurred
  75. */
  76. typedef struct esp_tls_last_error {
  77. esp_err_t last_error; /*!< error code (based on ESP_ERR_ESP_TLS_BASE) of the last occurred error */
  78. int esp_tls_error_code; /*!< esp_tls error code from last esp_tls failed api */
  79. int esp_tls_flags; /*!< last certification verification flags */
  80. } esp_tls_last_error_t;
  81. /**
  82. * @brief ESP-TLS Connection State
  83. */
  84. typedef enum esp_tls_conn_state {
  85. ESP_TLS_INIT = 0,
  86. ESP_TLS_CONNECTING,
  87. ESP_TLS_HANDSHAKE,
  88. ESP_TLS_FAIL,
  89. ESP_TLS_DONE,
  90. } esp_tls_conn_state_t;
  91. typedef enum esp_tls_role {
  92. ESP_TLS_CLIENT = 0,
  93. ESP_TLS_SERVER,
  94. } esp_tls_role_t;
  95. /**
  96. * @brief ESP-TLS preshared key and hint structure
  97. */
  98. typedef struct psk_key_hint {
  99. const uint8_t* key; /*!< key in PSK authentication mode in binary format */
  100. const size_t key_size; /*!< length of the key */
  101. const char* hint; /*!< hint in PSK authentication mode in string format */
  102. } psk_hint_key_t;
  103. /**
  104. * @brief Keep alive parameters structure
  105. */
  106. typedef struct tls_keep_alive_cfg {
  107. bool keep_alive_enable; /*!< Enable keep-alive timeout */
  108. int keep_alive_idle; /*!< Keep-alive idle time (second) */
  109. int keep_alive_interval; /*!< Keep-alive interval time (second) */
  110. int keep_alive_count; /*!< Keep-alive packet retry send count */
  111. } tls_keep_alive_cfg_t;
  112. /**
  113. * @brief ESP-TLS configuration parameters
  114. *
  115. * @note Note about format of certificates:
  116. * - This structure includes certificates of a Certificate Authority, of client or server as well
  117. * as private keys, which may be of PEM or DER format. In case of PEM format, the buffer must be
  118. * NULL terminated (with NULL character included in certificate size).
  119. * - Certificate Authority's certificate may be a chain of certificates in case of PEM format,
  120. * but could be only one certificate in case of DER format
  121. * - Variables names of certificates and private key buffers and sizes are defined as unions providing
  122. * backward compatibility for legacy *_pem_buf and *_pem_bytes names which suggested only PEM format
  123. * was supported. It is encouraged to use generic names such as cacert_buf and cacert_bytes.
  124. */
  125. typedef struct esp_tls_cfg {
  126. const char **alpn_protos; /*!< Application protocols required for HTTP2.
  127. If HTTP2/ALPN support is required, a list
  128. of protocols that should be negotiated.
  129. The format is length followed by protocol
  130. name.
  131. For the most common cases the following is ok:
  132. const char **alpn_protos = { "h2", NULL };
  133. - where 'h2' is the protocol name */
  134. union {
  135. const unsigned char *cacert_buf; /*!< Certificate Authority's certificate in a buffer.
  136. Format may be PEM or DER, depending on mbedtls-support
  137. This buffer should be NULL terminated in case of PEM */
  138. const unsigned char *cacert_pem_buf; /*!< CA certificate buffer legacy name */
  139. };
  140. union {
  141. unsigned int cacert_bytes; /*!< Size of Certificate Authority certificate
  142. pointed to by cacert_buf
  143. (including NULL-terminator in case of PEM format) */
  144. unsigned int cacert_pem_bytes; /*!< Size of Certificate Authority certificate legacy name */
  145. };
  146. union {
  147. const unsigned char *clientcert_buf; /*!< Client certificate in a buffer
  148. Format may be PEM or DER, depending on mbedtls-support
  149. This buffer should be NULL terminated in case of PEM */
  150. const unsigned char *clientcert_pem_buf; /*!< Client certificate legacy name */
  151. };
  152. union {
  153. unsigned int clientcert_bytes; /*!< Size of client certificate pointed to by
  154. clientcert_pem_buf
  155. (including NULL-terminator in case of PEM format) */
  156. unsigned int clientcert_pem_bytes; /*!< Size of client certificate legacy name */
  157. };
  158. union {
  159. const unsigned char *clientkey_buf; /*!< Client key in a buffer
  160. Format may be PEM or DER, depending on mbedtls-support
  161. This buffer should be NULL terminated in case of PEM */
  162. const unsigned char *clientkey_pem_buf; /*!< Client key legacy name */
  163. };
  164. union {
  165. unsigned int clientkey_bytes; /*!< Size of client key pointed to by
  166. clientkey_pem_buf
  167. (including NULL-terminator in case of PEM format) */
  168. unsigned int clientkey_pem_bytes; /*!< Size of client key legacy name */
  169. };
  170. const unsigned char *clientkey_password;/*!< Client key decryption password string */
  171. unsigned int clientkey_password_len; /*!< String length of the password pointed to by
  172. clientkey_password */
  173. bool non_block; /*!< Configure non-blocking mode. If set to true the
  174. underneath socket will be configured in non
  175. blocking mode after tls session is established */
  176. bool use_secure_element; /*!< Enable this option to use secure element or
  177. atecc608a chip ( Integrated with ESP32-WROOM-32SE ) */
  178. int timeout_ms; /*!< Network timeout in milliseconds */
  179. bool use_global_ca_store; /*!< Use a global ca_store for all the connections in which
  180. this bool is set. */
  181. const char *common_name; /*!< If non-NULL, server certificate CN must match this name.
  182. If NULL, server certificate CN must match hostname. */
  183. bool skip_common_name; /*!< Skip any validation of server certificate CN field */
  184. const psk_hint_key_t* psk_hint_key; /*!< Pointer to PSK hint and key. if not NULL (and certificates are NULL)
  185. then PSK authentication is enabled with configured setup.
  186. Important note: the pointer must be valid for connection */
  187. esp_err_t (*crt_bundle_attach)(void *conf);
  188. /*!< Function pointer to esp_crt_bundle_attach. Enables the use of certification
  189. bundle for server verification, must be enabled in menuconfig */
  190. tls_keep_alive_cfg_t *keep_alive_cfg; /*!< Enable TCP keep-alive timeout for SSL connection */
  191. } esp_tls_cfg_t;
  192. #ifdef CONFIG_ESP_TLS_SERVER
  193. typedef struct esp_tls_cfg_server {
  194. const char **alpn_protos; /*!< Application protocols required for HTTP2.
  195. If HTTP2/ALPN support is required, a list
  196. of protocols that should be negotiated.
  197. The format is length followed by protocol
  198. name.
  199. For the most common cases the following is ok:
  200. const char **alpn_protos = { "h2", NULL };
  201. - where 'h2' is the protocol name */
  202. union {
  203. const unsigned char *cacert_buf; /*!< Client CA certificate in a buffer.
  204. This buffer should be NULL terminated */
  205. const unsigned char *cacert_pem_buf; /*!< Client CA certificate legacy name */
  206. };
  207. union {
  208. unsigned int cacert_bytes; /*!< Size of client CA certificate
  209. pointed to by cacert_pem_buf */
  210. unsigned int cacert_pem_bytes; /*!< Size of client CA certificate legacy name */
  211. };
  212. union {
  213. const unsigned char *servercert_buf; /*!< Server certificate in a buffer
  214. This buffer should be NULL terminated */
  215. const unsigned char *servercert_pem_buf; /*!< Server certificate legacy name */
  216. };
  217. union {
  218. unsigned int servercert_bytes; /*!< Size of server certificate pointed to by
  219. servercert_pem_buf */
  220. unsigned int servercert_pem_bytes; /*!< Size of server certificate legacy name */
  221. };
  222. union {
  223. const unsigned char *serverkey_buf; /*!< Server key in a buffer
  224. This buffer should be NULL terminated */
  225. const unsigned char *serverkey_pem_buf; /*!< Server key legacy name */
  226. };
  227. union {
  228. unsigned int serverkey_bytes; /*!< Size of server key pointed to by
  229. serverkey_pem_buf */
  230. unsigned int serverkey_pem_bytes; /*!< Size of server key legacy name */
  231. };
  232. const unsigned char *serverkey_password; /*!< Server key decryption password string */
  233. unsigned int serverkey_password_len; /*!< String length of the password pointed to by
  234. serverkey_password */
  235. } esp_tls_cfg_server_t;
  236. #endif /* ! CONFIG_ESP_TLS_SERVER */
  237. /**
  238. * @brief ESP-TLS Connection Handle
  239. */
  240. typedef struct esp_tls {
  241. #ifdef CONFIG_ESP_TLS_USING_MBEDTLS
  242. mbedtls_ssl_context ssl; /*!< TLS/SSL context */
  243. mbedtls_entropy_context entropy; /*!< mbedTLS entropy context structure */
  244. mbedtls_ctr_drbg_context ctr_drbg; /*!< mbedTLS ctr drbg context structure.
  245. CTR_DRBG is deterministic random
  246. bit generation based on AES-256 */
  247. mbedtls_ssl_config conf; /*!< TLS/SSL configuration to be shared
  248. between mbedtls_ssl_context
  249. structures */
  250. mbedtls_net_context server_fd; /*!< mbedTLS wrapper type for sockets */
  251. mbedtls_x509_crt cacert; /*!< Container for the X.509 CA certificate */
  252. mbedtls_x509_crt *cacert_ptr; /*!< Pointer to the cacert being used. */
  253. mbedtls_x509_crt clientcert; /*!< Container for the X.509 client certificate */
  254. mbedtls_pk_context clientkey; /*!< Container for the private key of the client
  255. certificate */
  256. #ifdef CONFIG_ESP_TLS_SERVER
  257. mbedtls_x509_crt servercert; /*!< Container for the X.509 server certificate */
  258. mbedtls_pk_context serverkey; /*!< Container for the private key of the server
  259. certificate */
  260. #endif
  261. #elif CONFIG_ESP_TLS_USING_WOLFSSL
  262. void *priv_ctx;
  263. void *priv_ssl;
  264. #endif
  265. int sockfd; /*!< Underlying socket file descriptor. */
  266. ssize_t (*read)(struct esp_tls *tls, char *data, size_t datalen); /*!< Callback function for reading data from TLS/SSL
  267. connection. */
  268. ssize_t (*write)(struct esp_tls *tls, const char *data, size_t datalen); /*!< Callback function for writing data to TLS/SSL
  269. connection. */
  270. esp_tls_conn_state_t conn_state; /*!< ESP-TLS Connection state */
  271. fd_set rset; /*!< read file descriptors */
  272. fd_set wset; /*!< write file descriptors */
  273. bool is_tls; /*!< indicates connection type (TLS or NON-TLS) */
  274. esp_tls_role_t role; /*!< esp-tls role
  275. - ESP_TLS_CLIENT
  276. - ESP_TLS_SERVER */
  277. esp_tls_error_handle_t error_handle; /*!< handle to error descriptor */
  278. } esp_tls_t;
  279. /**
  280. * @brief Create TLS connection
  281. *
  282. * This function allocates and initializes esp-tls structure handle.
  283. *
  284. * @return tls Pointer to esp-tls as esp-tls handle if successfully initialized,
  285. * NULL if allocation error
  286. */
  287. esp_tls_t *esp_tls_init(void);
  288. /**
  289. * @brief Create a new blocking TLS/SSL connection
  290. *
  291. * This function establishes a TLS/SSL connection with the specified host in blocking manner.
  292. *
  293. * Note: This API is present for backward compatibility reasons. Alternative function
  294. * with the same functionality is `esp_tls_conn_new_sync` (and its asynchronous version
  295. * `esp_tls_conn_new_async`)
  296. *
  297. * @param[in] hostname Hostname of the host.
  298. * @param[in] hostlen Length of hostname.
  299. * @param[in] port Port number of the host.
  300. * @param[in] cfg TLS configuration as esp_tls_cfg_t. If you wish to open
  301. * non-TLS connection, keep this NULL. For TLS connection,
  302. * a pass pointer to esp_tls_cfg_t. At a minimum, this
  303. * structure should be zero-initialized.
  304. *
  305. * @return pointer to esp_tls_t, or NULL if connection couldn't be opened.
  306. */
  307. esp_tls_t *esp_tls_conn_new(const char *hostname, int hostlen, int port, const esp_tls_cfg_t *cfg) __attribute__ ((deprecated));
  308. /**
  309. * @brief Create a new blocking TLS/SSL connection
  310. *
  311. * This function establishes a TLS/SSL connection with the specified host in blocking manner.
  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. If you wish to open
  317. * non-TLS connection, keep this NULL. For TLS connection,
  318. * a pass pointer to esp_tls_cfg_t. At a minimum, this
  319. * structure should be zero-initialized.
  320. * @param[in] tls Pointer to esp-tls as esp-tls handle.
  321. *
  322. * @return
  323. * - -1 If connection establishment fails.
  324. * - 1 If connection establishment is successful.
  325. * - 0 If connection state is in progress.
  326. */
  327. int esp_tls_conn_new_sync(const char *hostname, int hostlen, int port, const esp_tls_cfg_t *cfg, esp_tls_t *tls);
  328. /**
  329. * @brief Create a new blocking TLS/SSL connection with a given "HTTP" url
  330. *
  331. * The behaviour is same as esp_tls_conn_new() API. However this API accepts host's url.
  332. *
  333. * @param[in] url url of host.
  334. * @param[in] cfg TLS configuration as esp_tls_cfg_t. If you wish to open
  335. * non-TLS connection, keep this NULL. For TLS connection,
  336. * a pass pointer to 'esp_tls_cfg_t'. At a minimum, this
  337. * structure should be zero-initialized.
  338. * @return pointer to esp_tls_t, or NULL if connection couldn't be opened.
  339. */
  340. esp_tls_t *esp_tls_conn_http_new(const char *url, const esp_tls_cfg_t *cfg);
  341. /**
  342. * @brief Create a new non-blocking TLS/SSL connection
  343. *
  344. * This function initiates a non-blocking TLS/SSL connection with the specified host, but due to
  345. * its non-blocking nature, it doesn't wait for the connection to get established.
  346. *
  347. * @param[in] hostname Hostname of the host.
  348. * @param[in] hostlen Length of hostname.
  349. * @param[in] port Port number of the host.
  350. * @param[in] cfg TLS configuration as esp_tls_cfg_t. `non_block` member of
  351. * this structure should be set to be true.
  352. * @param[in] tls pointer to esp-tls as esp-tls handle.
  353. *
  354. * @return
  355. * - -1 If connection establishment fails.
  356. * - 0 If connection establishment is in progress.
  357. * - 1 If connection establishment is successful.
  358. */
  359. int esp_tls_conn_new_async(const char *hostname, int hostlen, int port, const esp_tls_cfg_t *cfg, esp_tls_t *tls);
  360. /**
  361. * @brief Create a new non-blocking TLS/SSL connection with a given "HTTP" url
  362. *
  363. * The behaviour is same as esp_tls_conn_new() API. However this API accepts host's url.
  364. *
  365. * @param[in] url url of host.
  366. * @param[in] cfg TLS configuration as esp_tls_cfg_t.
  367. * @param[in] tls pointer to esp-tls as esp-tls handle.
  368. *
  369. * @return
  370. * - -1 If connection establishment fails.
  371. * - 0 If connection establishment is in progress.
  372. * - 1 If connection establishment is successful.
  373. */
  374. int esp_tls_conn_http_new_async(const char *url, const esp_tls_cfg_t *cfg, esp_tls_t *tls);
  375. /**
  376. * @brief Write from buffer 'data' into specified tls connection.
  377. *
  378. * @param[in] tls pointer to esp-tls as esp-tls handle.
  379. * @param[in] data Buffer from which data will be written.
  380. * @param[in] datalen Length of data buffer.
  381. *
  382. * @return
  383. * - >=0 if write operation was successful, the return value is the number
  384. * of bytes actually written to the TLS/SSL connection.
  385. * - <0 if write operation was not successful, because either an
  386. * error occured or an action must be taken by the calling process.
  387. */
  388. static inline ssize_t esp_tls_conn_write(esp_tls_t *tls, const void *data, size_t datalen)
  389. {
  390. return tls->write(tls, (char *)data, datalen);
  391. }
  392. /**
  393. * @brief Read from specified tls connection into the buffer 'data'.
  394. *
  395. * @param[in] tls pointer to esp-tls as esp-tls handle.
  396. * @param[in] data Buffer to hold read data.
  397. * @param[in] datalen Length of data buffer.
  398. *
  399. * @return
  400. * - >0 if read operation was successful, the return value is the number
  401. * of bytes actually read from the TLS/SSL connection.
  402. * - 0 if read operation was not successful. The underlying
  403. * connection was closed.
  404. * - <0 if read operation was not successful, because either an
  405. * error occured or an action must be taken by the calling process.
  406. */
  407. static inline ssize_t esp_tls_conn_read(esp_tls_t *tls, void *data, size_t datalen)
  408. {
  409. return tls->read(tls, (char *)data, datalen);
  410. }
  411. /**
  412. * @brief Compatible version of esp_tls_conn_destroy() to close the TLS/SSL connection
  413. *
  414. * @note This API will be removed in IDFv5.0
  415. *
  416. * @param[in] tls pointer to esp-tls as esp-tls handle.
  417. */
  418. void esp_tls_conn_delete(esp_tls_t *tls);
  419. /**
  420. * @brief Close the TLS/SSL connection and free any allocated resources.
  421. *
  422. * This function should be called to close each tls connection opened with esp_tls_conn_new() or
  423. * esp_tls_conn_http_new() APIs.
  424. *
  425. * @param[in] tls pointer to esp-tls as esp-tls handle.
  426. *
  427. * @return - 0 on success
  428. * - -1 if socket error or an invalid argument
  429. */
  430. int esp_tls_conn_destroy(esp_tls_t *tls);
  431. /**
  432. * @brief Return the number of application data bytes remaining to be
  433. * read from the current record
  434. *
  435. * This API is a wrapper over mbedtls's mbedtls_ssl_get_bytes_avail() API.
  436. *
  437. * @param[in] tls pointer to esp-tls as esp-tls handle.
  438. *
  439. * @return
  440. * - -1 in case of invalid arg
  441. * - bytes available in the application data
  442. * record read buffer
  443. */
  444. ssize_t esp_tls_get_bytes_avail(esp_tls_t *tls);
  445. /**
  446. * @brief Returns the connection socket file descriptor from esp_tls session
  447. *
  448. * @param[in] tls handle to esp_tls context
  449. *
  450. * @param[out] sockfd int pointer to sockfd value.
  451. *
  452. * @return - ESP_OK on success and value of sockfd will be updated with socket file descriptor for connection
  453. * - ESP_ERR_INVALID_ARG if (tls == NULL || sockfd == NULL)
  454. */
  455. esp_err_t esp_tls_get_conn_sockfd(esp_tls_t *tls, int *sockfd);
  456. /**
  457. * @brief Create a global CA store, initially empty.
  458. *
  459. * This function should be called if the application wants to use the same CA store for multiple connections.
  460. * This function initialises the global CA store which can be then set by calling esp_tls_set_global_ca_store().
  461. * To be effective, this function must be called before any call to esp_tls_set_global_ca_store().
  462. *
  463. * @return
  464. * - ESP_OK if creating global CA store was successful.
  465. * - ESP_ERR_NO_MEM if an error occured when allocating the mbedTLS resources.
  466. */
  467. esp_err_t esp_tls_init_global_ca_store(void);
  468. /**
  469. * @brief Set the global CA store with the buffer provided in pem format.
  470. *
  471. * This function should be called if the application wants to set the global CA store for
  472. * multiple connections i.e. to add the certificates in the provided buffer to the certificate chain.
  473. * This function implicitly calls esp_tls_init_global_ca_store() if it has not already been called.
  474. * The application must call this function before calling esp_tls_conn_new().
  475. *
  476. * @param[in] cacert_pem_buf Buffer which has certificates in pem format. This buffer
  477. * is used for creating a global CA store, which can be used
  478. * by other tls connections.
  479. * @param[in] cacert_pem_bytes Length of the buffer.
  480. *
  481. * @return
  482. * - ESP_OK if adding certificates was successful.
  483. * - Other if an error occured or an action must be taken by the calling process.
  484. */
  485. esp_err_t esp_tls_set_global_ca_store(const unsigned char *cacert_pem_buf, const unsigned int cacert_pem_bytes);
  486. /**
  487. * @brief Free the global CA store currently being used.
  488. *
  489. * The memory being used by the global CA store to store all the parsed certificates is
  490. * freed up. The application can call this API if it no longer needs the global CA store.
  491. */
  492. void esp_tls_free_global_ca_store(void);
  493. /**
  494. * @brief Returns last error in esp_tls with detailed mbedtls related error codes.
  495. * The error information is cleared internally upon return
  496. *
  497. * @param[in] h esp-tls error handle.
  498. * @param[out] esp_tls_code last error code returned from mbedtls api (set to zero if none)
  499. * This pointer could be NULL if caller does not care about esp_tls_code
  500. * @param[out] esp_tls_flags last certification verification flags (set to zero if none)
  501. * This pointer could be NULL if caller does not care about esp_tls_code
  502. *
  503. * @return
  504. * - ESP_ERR_INVALID_STATE if invalid parameters
  505. * - ESP_OK (0) if no error occurred
  506. * - specific error code (based on ESP_ERR_ESP_TLS_BASE) otherwise
  507. */
  508. esp_err_t esp_tls_get_and_clear_last_error(esp_tls_error_handle_t h, int *esp_tls_code, int *esp_tls_flags);
  509. #if CONFIG_ESP_TLS_USING_MBEDTLS
  510. /**
  511. * @brief Get the pointer to the global CA store currently being used.
  512. *
  513. * The application must first call esp_tls_set_global_ca_store(). Then the same
  514. * CA store could be used by the application for APIs other than esp_tls.
  515. *
  516. * @note Modifying the pointer might cause a failure in verifying the certificates.
  517. *
  518. * @return
  519. * - Pointer to the global CA store currently being used if successful.
  520. * - NULL if there is no global CA store set.
  521. */
  522. mbedtls_x509_crt *esp_tls_get_global_ca_store(void);
  523. #endif /* CONFIG_ESP_TLS_USING_MBEDTLS */
  524. #ifdef CONFIG_ESP_TLS_SERVER
  525. /**
  526. * @brief Create TLS/SSL server session
  527. *
  528. * This function creates a TLS/SSL server context for already accepted client connection
  529. * and performs TLS/SSL handshake with the client
  530. *
  531. * @param[in] cfg Pointer to esp_tls_cfg_server_t
  532. * @param[in] sockfd FD of accepted connection
  533. * @param[out] tls Pointer to allocated esp_tls_t
  534. *
  535. * @return
  536. * - 0 if successful
  537. * - <0 in case of error
  538. *
  539. */
  540. int esp_tls_server_session_create(esp_tls_cfg_server_t *cfg, int sockfd, esp_tls_t *tls);
  541. /**
  542. * @brief Close the server side TLS/SSL connection and free any allocated resources.
  543. *
  544. * This function should be called to close each tls connection opened with esp_tls_server_session_create()
  545. *
  546. * @param[in] tls pointer to esp_tls_t
  547. */
  548. void esp_tls_server_session_delete(esp_tls_t *tls);
  549. #endif /* ! CONFIG_ESP_TLS_SERVER */
  550. #ifdef __cplusplus
  551. }
  552. #endif
  553. #endif /* ! _ESP_TLS_H_ */