esp_tls_private.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. /**
  8. * @brief ESP-TLS Connection Handle
  9. */
  10. #include <stdbool.h>
  11. #include <sys/socket.h>
  12. #include <fcntl.h>
  13. #include "esp_err.h"
  14. #include "esp_tls_errors.h"
  15. #ifdef CONFIG_ESP_TLS_USING_MBEDTLS
  16. #include "mbedtls/platform.h"
  17. #include "mbedtls/net_sockets.h"
  18. #include "mbedtls/esp_debug.h"
  19. #include "mbedtls/ssl.h"
  20. #include "mbedtls/entropy.h"
  21. #include "mbedtls/ctr_drbg.h"
  22. #include "mbedtls/error.h"
  23. #ifdef CONFIG_ESP_TLS_SERVER_SESSION_TICKETS
  24. #include "mbedtls/ssl_ticket.h"
  25. #endif
  26. #ifdef CONFIG_MBEDTLS_SSL_PROTO_TLS1_3
  27. #include "psa/crypto.h"
  28. #endif
  29. #elif CONFIG_ESP_TLS_USING_WOLFSSL
  30. #include "wolfssl/wolfcrypt/settings.h"
  31. #include "wolfssl/ssl.h"
  32. #endif
  33. struct esp_tls {
  34. #ifdef CONFIG_ESP_TLS_USING_MBEDTLS
  35. mbedtls_ssl_context ssl; /*!< TLS/SSL context */
  36. mbedtls_entropy_context entropy; /*!< mbedTLS entropy context structure */
  37. mbedtls_ctr_drbg_context ctr_drbg; /*!< mbedTLS ctr drbg context structure.
  38. CTR_DRBG is deterministic random
  39. bit generation based on AES-256 */
  40. mbedtls_ssl_config conf; /*!< TLS/SSL configuration to be shared
  41. between mbedtls_ssl_context
  42. structures */
  43. mbedtls_net_context server_fd; /*!< mbedTLS wrapper type for sockets */
  44. mbedtls_x509_crt cacert; /*!< Container for the X.509 CA certificate */
  45. mbedtls_x509_crt *cacert_ptr; /*!< Pointer to the cacert being used. */
  46. union {
  47. mbedtls_x509_crt clientcert; /*!< Container for the X.509 client certificate */
  48. mbedtls_x509_crt servercert; /*!< Container for the X.509 server certificate */
  49. };
  50. union {
  51. mbedtls_pk_context clientkey; /*!< Container for the private key of the client
  52. certificate */
  53. mbedtls_pk_context serverkey; /*!< Container for the private key of the server
  54. certificate */
  55. };
  56. #ifdef CONFIG_MBEDTLS_HARDWARE_ECDSA_SIGN
  57. bool use_ecdsa_peripheral; /*!< Use the ECDSA peripheral for the private key operations. */
  58. uint8_t ecdsa_efuse_blk; /*!< The efuse block number where the ECDSA key is stored. */
  59. #endif
  60. #elif CONFIG_ESP_TLS_USING_WOLFSSL
  61. void *priv_ctx;
  62. void *priv_ssl;
  63. #endif
  64. int sockfd; /*!< Underlying socket file descriptor. */
  65. ssize_t (*read)(esp_tls_t *tls, char *data, size_t datalen); /*!< Callback function for reading data from TLS/SSL
  66. connection. */
  67. ssize_t (*write)(esp_tls_t *tls, const char *data, size_t datalen); /*!< Callback function for writing data to TLS/SSL
  68. connection. */
  69. esp_tls_conn_state_t conn_state; /*!< ESP-TLS Connection state */
  70. fd_set rset; /*!< read file descriptors */
  71. fd_set wset; /*!< write file descriptors */
  72. bool is_tls; /*!< indicates connection type (TLS or NON-TLS) */
  73. esp_tls_role_t role; /*!< esp-tls role
  74. - ESP_TLS_CLIENT
  75. - ESP_TLS_SERVER */
  76. esp_tls_error_handle_t error_handle; /*!< handle to error descriptor */
  77. };
  78. // Function pointer for the server configuration API
  79. typedef esp_err_t (*set_server_config_func_ptr) (esp_tls_cfg_server_t *cfg, esp_tls_t *tls);
  80. // This struct contains any data that is only specific to the server session and not required by the client.
  81. typedef struct esp_tls_server_params {
  82. set_server_config_func_ptr set_server_cfg;
  83. } esp_tls_server_params_t;