|
|
@@ -7,18 +7,11 @@
|
|
|
#define _ESP_TLS_H_
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
-#include <sys/socket.h>
|
|
|
-#include <fcntl.h>
|
|
|
#include "esp_err.h"
|
|
|
#include "esp_tls_errors.h"
|
|
|
+#include "sdkconfig.h"
|
|
|
#ifdef CONFIG_ESP_TLS_USING_MBEDTLS
|
|
|
-#include "mbedtls/platform.h"
|
|
|
-#include "mbedtls/net_sockets.h"
|
|
|
-#include "mbedtls/esp_debug.h"
|
|
|
#include "mbedtls/ssl.h"
|
|
|
-#include "mbedtls/entropy.h"
|
|
|
-#include "mbedtls/ctr_drbg.h"
|
|
|
-#include "mbedtls/error.h"
|
|
|
#ifdef CONFIG_ESP_TLS_SERVER_SESSION_TICKETS
|
|
|
#include "mbedtls/ssl_ticket.h"
|
|
|
#endif
|
|
|
@@ -27,6 +20,7 @@
|
|
|
#include "wolfssl/ssl.h"
|
|
|
#endif
|
|
|
|
|
|
+
|
|
|
#ifdef __cplusplus
|
|
|
extern "C" {
|
|
|
#endif
|
|
|
@@ -290,66 +284,7 @@ esp_err_t esp_tls_cfg_server_session_tickets_init(esp_tls_cfg_server_t *cfg);
|
|
|
void esp_tls_cfg_server_session_tickets_free(esp_tls_cfg_server_t *cfg);
|
|
|
#endif /* ! CONFIG_ESP_TLS_SERVER */
|
|
|
|
|
|
-/**
|
|
|
- * @brief ESP-TLS Connection Handle
|
|
|
- */
|
|
|
-typedef struct esp_tls {
|
|
|
-#ifdef CONFIG_ESP_TLS_USING_MBEDTLS
|
|
|
- mbedtls_ssl_context ssl; /*!< TLS/SSL context */
|
|
|
-
|
|
|
- mbedtls_entropy_context entropy; /*!< mbedTLS entropy context structure */
|
|
|
-
|
|
|
- mbedtls_ctr_drbg_context ctr_drbg; /*!< mbedTLS ctr drbg context structure.
|
|
|
- CTR_DRBG is deterministic random
|
|
|
- bit generation based on AES-256 */
|
|
|
-
|
|
|
- mbedtls_ssl_config conf; /*!< TLS/SSL configuration to be shared
|
|
|
- between mbedtls_ssl_context
|
|
|
- structures */
|
|
|
-
|
|
|
- mbedtls_net_context server_fd; /*!< mbedTLS wrapper type for sockets */
|
|
|
-
|
|
|
- mbedtls_x509_crt cacert; /*!< Container for the X.509 CA certificate */
|
|
|
-
|
|
|
- mbedtls_x509_crt *cacert_ptr; /*!< Pointer to the cacert being used. */
|
|
|
-
|
|
|
- mbedtls_x509_crt clientcert; /*!< Container for the X.509 client certificate */
|
|
|
-
|
|
|
- mbedtls_pk_context clientkey; /*!< Container for the private key of the client
|
|
|
- certificate */
|
|
|
-#ifdef CONFIG_ESP_TLS_SERVER
|
|
|
- mbedtls_x509_crt servercert; /*!< Container for the X.509 server certificate */
|
|
|
-
|
|
|
- mbedtls_pk_context serverkey; /*!< Container for the private key of the server
|
|
|
- certificate */
|
|
|
-#endif
|
|
|
-#elif CONFIG_ESP_TLS_USING_WOLFSSL
|
|
|
- void *priv_ctx;
|
|
|
- void *priv_ssl;
|
|
|
-#endif
|
|
|
- int sockfd; /*!< Underlying socket file descriptor. */
|
|
|
-
|
|
|
- ssize_t (*read)(struct esp_tls *tls, char *data, size_t datalen); /*!< Callback function for reading data from TLS/SSL
|
|
|
- connection. */
|
|
|
-
|
|
|
- ssize_t (*write)(struct esp_tls *tls, const char *data, size_t datalen); /*!< Callback function for writing data to TLS/SSL
|
|
|
- connection. */
|
|
|
-
|
|
|
- esp_tls_conn_state_t conn_state; /*!< ESP-TLS Connection state */
|
|
|
-
|
|
|
- fd_set rset; /*!< read file descriptors */
|
|
|
-
|
|
|
- fd_set wset; /*!< write file descriptors */
|
|
|
-
|
|
|
- bool is_tls; /*!< indicates connection type (TLS or NON-TLS) */
|
|
|
-
|
|
|
- esp_tls_role_t role; /*!< esp-tls role
|
|
|
- - ESP_TLS_CLIENT
|
|
|
- - ESP_TLS_SERVER */
|
|
|
-
|
|
|
- esp_tls_error_handle_t error_handle; /*!< handle to error descriptor */
|
|
|
-
|
|
|
-} esp_tls_t;
|
|
|
+typedef struct esp_tls esp_tls_t;
|
|
|
|
|
|
/**
|
|
|
* @brief Create TLS connection
|
|
|
@@ -470,10 +405,7 @@ int esp_tls_conn_http_new_async(const char *url, const esp_tls_cfg_t *cfg, esp_t
|
|
|
* if the handshake is incomplete and waiting for data to be available for reading.
|
|
|
* In this case this functions needs to be called again when the underlying transport is ready for operation.
|
|
|
*/
|
|
|
-static inline ssize_t esp_tls_conn_write(esp_tls_t *tls, const void *data, size_t datalen)
|
|
|
-{
|
|
|
- return tls->write(tls, (char *)data, datalen);
|
|
|
-}
|
|
|
+ssize_t esp_tls_conn_write(esp_tls_t *tls, const void *data, size_t datalen);
|
|
|
|
|
|
/**
|
|
|
* @brief Read from specified tls connection into the buffer 'data'.
|
|
|
@@ -490,10 +422,7 @@ static inline ssize_t esp_tls_conn_write(esp_tls_t *tls, const void *data, size_
|
|
|
* - <0 if read operation was not successful, because either an
|
|
|
* error occured or an action must be taken by the calling process.
|
|
|
*/
|
|
|
-static inline ssize_t esp_tls_conn_read(esp_tls_t *tls, void *data, size_t datalen)
|
|
|
-{
|
|
|
- return tls->read(tls, (char *)data, datalen);
|
|
|
-}
|
|
|
+ssize_t esp_tls_conn_read(esp_tls_t *tls, void *data, size_t datalen);
|
|
|
|
|
|
/**
|
|
|
* @brief Close the TLS/SSL connection and free any allocated resources.
|
|
|
@@ -536,6 +465,17 @@ ssize_t esp_tls_get_bytes_avail(esp_tls_t *tls);
|
|
|
*/
|
|
|
esp_err_t esp_tls_get_conn_sockfd(esp_tls_t *tls, int *sockfd);
|
|
|
|
|
|
+/**
|
|
|
+ * @brief Returns the ssl context
|
|
|
+ *
|
|
|
+ * @param[in] tls handle to esp_tls context
|
|
|
+ *
|
|
|
+ *
|
|
|
+ * @return - ssl_ctx pointer to ssl context of underlying TLS layer on success
|
|
|
+ * - NULL in case of error
|
|
|
+ */
|
|
|
+void *esp_tls_get_ssl_context(esp_tls_t *tls);
|
|
|
+
|
|
|
/**
|
|
|
* @brief Create a global CA store, initially empty.
|
|
|
*
|
|
|
@@ -607,6 +547,20 @@ esp_err_t esp_tls_get_and_clear_last_error(esp_tls_error_handle_t h, int *esp_tl
|
|
|
*/
|
|
|
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);
|
|
|
|
|
|
+/**
|
|
|
+ * @brief Returns the ESP-TLS error_handle
|
|
|
+ *
|
|
|
+ * @param[in] tls handle to esp_tls context
|
|
|
+ *
|
|
|
+ * @param[out] error_handle pointer to the error handle.
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ * - ESP_OK on success and error_handle will be updated with the ESP-TLS error handle.
|
|
|
+ *
|
|
|
+ * - ESP_ERR_INVALID_ARG if (tls == NULL || error_handle == NULL)
|
|
|
+ */
|
|
|
+esp_err_t esp_tls_get_error_handle(esp_tls_t *tls, esp_tls_error_handle_t *error_handle);
|
|
|
+
|
|
|
#if CONFIG_ESP_TLS_USING_MBEDTLS
|
|
|
/**
|
|
|
* @brief Get the pointer to the global CA store currently being used.
|