main.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /* Simple HTTP + SSL Server Example
  2. This example code is in the Public Domain (or CC0 licensed, at your option.)
  3. Unless required by applicable law or agreed to in writing, this
  4. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  5. CONDITIONS OF ANY KIND, either express or implied.
  6. */
  7. #include <esp_wifi.h>
  8. #include <esp_event.h>
  9. #include <esp_log.h>
  10. #include <esp_system.h>
  11. #include <nvs_flash.h>
  12. #include <sys/param.h>
  13. #include "esp_netif.h"
  14. #include "esp_eth.h"
  15. #include "protocol_examples_common.h"
  16. #include <esp_https_server.h>
  17. #include "esp_tls.h"
  18. #include "sdkconfig.h"
  19. /* A simple example that demonstrates how to create GET and POST
  20. * handlers and start an HTTPS server.
  21. */
  22. static const char *TAG = "example";
  23. /* An HTTP GET handler */
  24. static esp_err_t root_get_handler(httpd_req_t *req)
  25. {
  26. httpd_resp_set_type(req, "text/html");
  27. httpd_resp_send(req, "<h1>Hello Secure World!</h1>", HTTPD_RESP_USE_STRLEN);
  28. return ESP_OK;
  29. }
  30. #if CONFIG_EXAMPLE_ENABLE_HTTPS_USER_CALLBACK
  31. #ifdef CONFIG_ESP_TLS_USING_MBEDTLS
  32. static void print_peer_cert_info(const mbedtls_ssl_context *ssl)
  33. {
  34. const mbedtls_x509_crt *cert;
  35. const size_t buf_size = 1024;
  36. char *buf = calloc(buf_size, sizeof(char));
  37. if (buf == NULL) {
  38. ESP_LOGE(TAG, "Out of memory - Callback execution failed!");
  39. return;
  40. }
  41. // Logging the peer certificate info
  42. cert = mbedtls_ssl_get_peer_cert(ssl);
  43. if (cert != NULL) {
  44. mbedtls_x509_crt_info((char *) buf, buf_size - 1, " ", cert);
  45. ESP_LOGI(TAG, "Peer certificate info:\n%s", buf);
  46. } else {
  47. ESP_LOGW(TAG, "Could not obtain the peer certificate!");
  48. }
  49. free(buf);
  50. }
  51. #endif
  52. /**
  53. * Example callback function to get the certificate of connected clients,
  54. * whenever a new SSL connection is created and closed
  55. *
  56. * Can also be used to other information like Socket FD, Connection state, etc.
  57. *
  58. * NOTE: This callback will not be able to obtain the client certificate if the
  59. * following config `Set minimum Certificate Verification mode to Optional` is
  60. * not enabled (enabled by default in this example).
  61. *
  62. * The config option is found here - Component config → ESP-TLS
  63. *
  64. */
  65. static void https_server_user_callback(esp_https_server_user_cb_arg_t *user_cb)
  66. {
  67. ESP_LOGI(TAG, "User callback invoked!");
  68. #ifdef CONFIG_ESP_TLS_USING_MBEDTLS
  69. mbedtls_ssl_context *ssl_ctx = NULL;
  70. #endif
  71. switch(user_cb->user_cb_state) {
  72. case HTTPD_SSL_USER_CB_SESS_CREATE:
  73. ESP_LOGD(TAG, "At session creation");
  74. // Logging the socket FD
  75. int sockfd = -1;
  76. esp_err_t esp_ret;
  77. esp_ret = esp_tls_get_conn_sockfd(user_cb->tls, &sockfd);
  78. if (esp_ret != ESP_OK) {
  79. ESP_LOGE(TAG, "Error in obtaining the sockfd from tls context");
  80. break;
  81. }
  82. ESP_LOGI(TAG, "Socket FD: %d", sockfd);
  83. #ifdef CONFIG_ESP_TLS_USING_MBEDTLS
  84. ssl_ctx = (mbedtls_ssl_context *) esp_tls_get_ssl_context(user_cb->tls);
  85. if (ssl_ctx == NULL) {
  86. ESP_LOGE(TAG, "Error in obtaining ssl context");
  87. break;
  88. }
  89. // Logging the current ciphersuite
  90. ESP_LOGI(TAG, "Current Ciphersuite: %s", mbedtls_ssl_get_ciphersuite(ssl_ctx));
  91. #endif
  92. break;
  93. case HTTPD_SSL_USER_CB_SESS_CLOSE:
  94. ESP_LOGD(TAG, "At session close");
  95. #ifdef CONFIG_ESP_TLS_USING_MBEDTLS
  96. // Logging the peer certificate
  97. ssl_ctx = (mbedtls_ssl_context *) esp_tls_get_ssl_context(user_cb->tls);
  98. if (ssl_ctx == NULL) {
  99. ESP_LOGE(TAG, "Error in obtaining ssl context");
  100. break;
  101. }
  102. print_peer_cert_info(ssl_ctx);
  103. #endif
  104. break;
  105. default:
  106. ESP_LOGE(TAG, "Illegal state!");
  107. return;
  108. }
  109. }
  110. #endif
  111. static const httpd_uri_t root = {
  112. .uri = "/",
  113. .method = HTTP_GET,
  114. .handler = root_get_handler
  115. };
  116. static httpd_handle_t start_webserver(void)
  117. {
  118. httpd_handle_t server = NULL;
  119. // Start the httpd server
  120. ESP_LOGI(TAG, "Starting server");
  121. httpd_ssl_config_t conf = HTTPD_SSL_CONFIG_DEFAULT();
  122. extern const unsigned char servercert_start[] asm("_binary_servercert_pem_start");
  123. extern const unsigned char servercert_end[] asm("_binary_servercert_pem_end");
  124. conf.servercert = servercert_start;
  125. conf.servercert_len = servercert_end - servercert_start;
  126. extern const unsigned char prvtkey_pem_start[] asm("_binary_prvtkey_pem_start");
  127. extern const unsigned char prvtkey_pem_end[] asm("_binary_prvtkey_pem_end");
  128. conf.prvtkey_pem = prvtkey_pem_start;
  129. conf.prvtkey_len = prvtkey_pem_end - prvtkey_pem_start;
  130. #if CONFIG_EXAMPLE_ENABLE_HTTPS_USER_CALLBACK
  131. conf.user_cb = https_server_user_callback;
  132. #endif
  133. esp_err_t ret = httpd_ssl_start(&server, &conf);
  134. if (ESP_OK != ret) {
  135. ESP_LOGI(TAG, "Error starting server!");
  136. return NULL;
  137. }
  138. // Set URI handlers
  139. ESP_LOGI(TAG, "Registering URI handlers");
  140. httpd_register_uri_handler(server, &root);
  141. return server;
  142. }
  143. static esp_err_t stop_webserver(httpd_handle_t server)
  144. {
  145. // Stop the httpd server
  146. return httpd_ssl_stop(server);
  147. }
  148. static void disconnect_handler(void* arg, esp_event_base_t event_base,
  149. int32_t event_id, void* event_data)
  150. {
  151. httpd_handle_t* server = (httpd_handle_t*) arg;
  152. if (*server) {
  153. if (stop_webserver(*server) == ESP_OK) {
  154. *server = NULL;
  155. } else {
  156. ESP_LOGE(TAG, "Failed to stop https server");
  157. }
  158. }
  159. }
  160. static void connect_handler(void* arg, esp_event_base_t event_base,
  161. int32_t event_id, void* event_data)
  162. {
  163. httpd_handle_t* server = (httpd_handle_t*) arg;
  164. if (*server == NULL) {
  165. *server = start_webserver();
  166. }
  167. }
  168. void app_main(void)
  169. {
  170. static httpd_handle_t server = NULL;
  171. ESP_ERROR_CHECK(nvs_flash_init());
  172. ESP_ERROR_CHECK(esp_netif_init());
  173. ESP_ERROR_CHECK(esp_event_loop_create_default());
  174. /* Register event handlers to start server when Wi-Fi or Ethernet is connected,
  175. * and stop server when disconnection happens.
  176. */
  177. #ifdef CONFIG_EXAMPLE_CONNECT_WIFI
  178. ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &connect_handler, &server));
  179. ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &disconnect_handler, &server));
  180. #endif // CONFIG_EXAMPLE_CONNECT_WIFI
  181. #ifdef CONFIG_EXAMPLE_CONNECT_ETHERNET
  182. ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &connect_handler, &server));
  183. ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ETHERNET_EVENT_DISCONNECTED, &disconnect_handler, &server));
  184. #endif // CONFIG_EXAMPLE_CONNECT_ETHERNET
  185. /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
  186. * Read "Establishing Wi-Fi or Ethernet Connection" section in
  187. * examples/protocols/README.md for more information about this function.
  188. */
  189. ESP_ERROR_CHECK(example_connect());
  190. }