main.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /* main.c
  2. *
  3. * Copyright (C) 2006-2024 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. #include "sdkconfig.h"
  22. #include "main.h"
  23. /* ESP specific */
  24. #include <nvs_flash.h>
  25. #include <esp_log.h>
  26. #include <esp_event.h>
  27. /* wolfSSL */
  28. #include <wolfssl/wolfcrypt/settings.h> /* includes wolfSSL user-settings.h */
  29. #include <wolfssl/wolfcrypt/port/Espressif/esp32-crypt.h>
  30. #ifndef WOLFSSL_ESPIDF
  31. #warning "Problem with wolfSSL user_settings."
  32. #warning "Check components/wolfssl/include"
  33. #endif
  34. /* this project */
  35. #include "server-tls.h"
  36. #include "time_helper.h"
  37. #ifndef CONFIG_IDF_TARGET_ESP32H2
  38. /* There's no WiFi on ESP32-H2.
  39. * For wired ethernet, see:
  40. * https://github.com/wolfSSL/wolfssl-examples/tree/master/ESP32/TLS13-ENC28J60-client */
  41. #include "wifi_connect.h"
  42. #endif
  43. #ifdef WOLFSSL_TRACK_MEMORY
  44. #include <wolfssl/wolfcrypt/mem_track.h>
  45. #endif
  46. static const char* const TAG = "TLS Client";
  47. #if defined(WOLFSSL_ESPWROOM32SE) && defined(HAVE_PK_CALLBACKS) \
  48. && defined(WOLFSSL_ATECC508A)
  49. #include "wolfssl/wolfcrypt/port/atmel/atmel.h"
  50. /* when you want to use a custom slot allocation */
  51. /* enable the definition CUSTOM_SLOT_ALLOCATION. */
  52. #if defined(CUSTOM_SLOT_ALLOCATION)
  53. static byte mSlotList[ATECC_MAX_SLOT];
  54. int atmel_set_slot_allocator(atmel_slot_alloc_cb alloc, atmel_slot_dealloc_cb dealloc);
  55. /* initialize slot array */
  56. void my_atmel_slotInit()
  57. {
  58. int i;
  59. for(i = 0;i < ATECC_MAX_SLOT;i++) {
  60. mSlotList[i] = ATECC_INVALID_SLOT;
  61. }
  62. }
  63. /* allocate slot depending on slotType */
  64. int my_atmel_alloc(int slotType)
  65. {
  66. int i, slot = -1;
  67. switch(slotType){
  68. case ATMEL_SLOT_ENCKEY:
  69. slot = 4;
  70. break;
  71. case ATMEL_SLOT_DEVICE:
  72. slot = 0;
  73. break;
  74. case ATMEL_SLOT_ECDHE:
  75. slot = 0;
  76. break;
  77. case ATMEL_SLOT_ECDHE_ENC:
  78. slot = 4;
  79. break;
  80. case ATMEL_SLOT_ANY:
  81. for(i = 0;i < ATECC_MAX_SLOT;i++){
  82. if(mSlotList[i] == ATECC_INVALID_SLOT){
  83. slot = i;
  84. break;
  85. }
  86. }
  87. }
  88. return slot;
  89. }
  90. /* free slot array */
  91. void my_atmel_free(int slotId)
  92. {
  93. if(slotId >= 0 && slotId < ATECC_MAX_SLOT){
  94. mSlotList[slotId] = ATECC_INVALID_SLOT;
  95. }
  96. }
  97. #endif /* CUSTOM_SLOT_ALLOCATION */
  98. #endif /* WOLFSSL_ESPWROOM32SE && HAVE_PK_CALLBACK && WOLFSSL_ATECC508A */
  99. /* for FreeRTOS */
  100. void app_main(void)
  101. {
  102. int stack_start = 0;
  103. esp_err_t ret = 0;
  104. ESP_LOGI(TAG, "---------------- wolfSSL TLS Server Example ------------");
  105. ESP_LOGI(TAG, "--------------------------------------------------------");
  106. ESP_LOGI(TAG, "--------------------------------------------------------");
  107. ESP_LOGI(TAG, "---------------------- BEGIN MAIN ----------------------");
  108. ESP_LOGI(TAG, "--------------------------------------------------------");
  109. ESP_LOGI(TAG, "--------------------------------------------------------");
  110. #ifdef ESP_TASK_MAIN_STACK
  111. ESP_LOGI(TAG, "ESP_TASK_MAIN_STACK: %d", ESP_TASK_MAIN_STACK);
  112. #endif
  113. #ifdef TASK_EXTRA_STACK_SIZE
  114. ESP_LOGI(TAG, "TASK_EXTRA_STACK_SIZE: %d", TASK_EXTRA_STACK_SIZE);
  115. #endif
  116. #ifdef INCLUDE_uxTaskGetStackHighWaterMark
  117. ESP_LOGI(TAG, "CONFIG_ESP_MAIN_TASK_STACK_SIZE = %d bytes (%d words)",
  118. CONFIG_ESP_MAIN_TASK_STACK_SIZE,
  119. (int)(CONFIG_ESP_MAIN_TASK_STACK_SIZE / sizeof(void*)));
  120. /* Returns the high water mark of the stack associated with xTask. That is,
  121. * the minimum free stack space there has been (in bytes not words, unlike
  122. * vanilla FreeRTOS) since the task started. The smaller the returned
  123. * number the closer the task has come to overflowing its stack.
  124. * see https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/system/freertos_idf.html
  125. */
  126. stack_start = uxTaskGetStackHighWaterMark(NULL);
  127. ESP_LOGI(TAG, "Stack Start HWM: %d bytes", stack_start);
  128. #endif
  129. #ifdef HAVE_VERSION_EXTENDED_INFO
  130. esp_ShowExtendedSystemInfo();
  131. #endif
  132. /* Set time for cert validation.
  133. * Some lwIP APIs, including SNTP functions, are not thread safe. */
  134. ret = set_time(); /* need to setup NTP before WiFi */
  135. /* Optionally erase flash */
  136. /* ESP_ERROR_CHECK(nvs_flash_erase()); */
  137. #ifdef FOUND_PROTOCOL_EXAMPLES_DIR
  138. ESP_LOGI(TAG, "FOUND_PROTOCOL_EXAMPLES_DIR active, using example code.");
  139. ESP_ERROR_CHECK(nvs_flash_init());
  140. #if defined(CONFIG_IDF_TARGET_ESP32H2)
  141. ESP_LOGE(TAG, "There's no WiFi on ESP32-H2.");
  142. #else
  143. #ifdef CONFIG_EXAMPLE_WIFI_SSID
  144. if (XSTRCMP(CONFIG_EXAMPLE_WIFI_SSID, "myssid") == 0) {
  145. ESP_LOGW(TAG, "WARNING: CONFIG_EXAMPLE_WIFI_SSID is myssid.");
  146. ESP_LOGW(TAG, " Do you have a WiFi AP called myssid, or ");
  147. ESP_LOGW(TAG, " did you forget the ESP-IDF configuration?");
  148. }
  149. #else
  150. #define CONFIG_EXAMPLE_WIFI_SSID "myssid"
  151. ESP_LOGW(TAG, "WARNING: CONFIG_EXAMPLE_WIFI_SSID not defined.");
  152. #endif
  153. ESP_ERROR_CHECK(esp_netif_init());
  154. ESP_ERROR_CHECK(esp_event_loop_create_default());
  155. ESP_ERROR_CHECK(example_connect());
  156. #endif
  157. #else
  158. ESP_ERROR_CHECK(nvs_flash_init());
  159. /* Initialize NVS */
  160. ret = nvs_flash_init();
  161. if (ret == ESP_ERR_NVS_NO_FREE_PAGES ||
  162. ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  163. ESP_ERROR_CHECK(nvs_flash_erase());
  164. ret = nvs_flash_init();
  165. }
  166. ESP_ERROR_CHECK(ret);
  167. #if defined(CONFIG_IDF_TARGET_ESP32H2)
  168. ESP_LOGE(TAG, "There's no WiFi on ESP32-H2. ");
  169. #else
  170. /* Initialize WiFi */
  171. ESP_LOGI(TAG, "ESP_WIFI_MODE_STA");
  172. ret = wifi_init_sta();
  173. while (ret != 0) {
  174. ESP_LOGI(TAG, "Waiting...");
  175. vTaskDelay(60000 / portTICK_PERIOD_MS);
  176. ESP_LOGI(TAG, "Trying WiFi again...");
  177. ret = wifi_init_sta();
  178. }
  179. #endif
  180. #endif
  181. /* Once we are connected to the network, start & wait for NTP time */
  182. ret = set_time_wait_for_ntp();
  183. if (ret < -1) {
  184. /* a value of -1 means there was no NTP server, so no need to wait */
  185. ESP_LOGI(TAG, "Waiting 10 more seconds for NTP to complete." );
  186. vTaskDelay(10000 / portTICK_PERIOD_MS); /* brute-force solution */
  187. esp_show_current_datetime();
  188. }
  189. /* HWM is maximum amount of stack space that has been unused, in bytes
  190. * not words (unlike vanilla freeRTOS). */
  191. ESP_LOGI(TAG, "Initial Stack Used (before wolfSSL Server): %d bytes",
  192. CONFIG_ESP_MAIN_TASK_STACK_SIZE
  193. - (uxTaskGetStackHighWaterMark(NULL))
  194. );
  195. ESP_LOGI(TAG, "Starting TLS Server...\n");
  196. #if defined(SINGLE_THREADED)
  197. /* just call the task */
  198. tls_smp_server_task((void*)NULL);
  199. #else
  200. tls_args args[1] = {0};
  201. /* start a thread with the task */
  202. tls_smp_server_init(args); /* NULL will use the DEFAULT_PORT value */
  203. #endif
  204. /* done */
  205. while (1) {
  206. #if defined(SINGLE_THREADED)
  207. ESP_LOGV(TAG, "\n\nDone!\n\n");
  208. while (1);
  209. #else
  210. /* Delete this main task to free up memory */
  211. ESP_LOGV(TAG, "\n\nvTaskDelete...\n\n");
  212. vTaskDelete(NULL);
  213. #endif
  214. } /* done while */
  215. } /* app_main */