main.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. /* A simple example that demonstrates how to create GET and POST
  19. * handlers and start an HTTPS server.
  20. */
  21. static const char *TAG = "example";
  22. /* An HTTP GET handler */
  23. static esp_err_t root_get_handler(httpd_req_t *req)
  24. {
  25. httpd_resp_set_type(req, "text/html");
  26. httpd_resp_send(req, "<h1>Hello Secure World!</h1>", HTTPD_RESP_USE_STRLEN);
  27. return ESP_OK;
  28. }
  29. #if CONFIG_EXAMPLE_ENABLE_HTTPS_USER_CALLBACK
  30. static void print_peer_cert_info(const mbedtls_ssl_context *ssl)
  31. {
  32. const mbedtls_x509_crt *cert;
  33. const size_t buf_size = 1024;
  34. char *buf = calloc(buf_size, sizeof(char));
  35. if (buf == NULL) {
  36. ESP_LOGE(TAG, "Out of memory - Callback execution failed!");
  37. return;
  38. }
  39. // Logging the peer certificate info
  40. cert = mbedtls_ssl_get_peer_cert(ssl);
  41. if (cert != NULL) {
  42. mbedtls_x509_crt_info((char *) buf, buf_size - 1, " ", cert);
  43. ESP_LOGI(TAG, "Peer certificate info:\n%s", buf);
  44. } else {
  45. ESP_LOGW(TAG, "Could not obtain the peer certificate!");
  46. }
  47. free(buf);
  48. }
  49. /**
  50. * Example callback function to get the certificate of connected clients,
  51. * whenever a new SSL connection is created and closed
  52. *
  53. * Can also be used to other information like Socket FD, Connection state, etc.
  54. *
  55. * NOTE: This callback will not be able to obtain the client certificate if the
  56. * following config `Set minimum Certificate Verification mode to Optional` is
  57. * not enabled (enabled by default in this example).
  58. *
  59. * The config option is found here - Component config → ESP-TLS
  60. *
  61. */
  62. static void https_server_user_callback(esp_https_server_user_cb_arg_t *user_cb)
  63. {
  64. ESP_LOGI(TAG, "User callback invoked!");
  65. switch(user_cb->user_cb_state) {
  66. case HTTPD_SSL_USER_CB_SESS_CREATE:
  67. ESP_LOGD(TAG, "At session creation");
  68. // Logging the socket FD
  69. ESP_LOGI(TAG, "Socket FD: %d", user_cb->tls->sockfd);
  70. // Logging the current ciphersuite
  71. ESP_LOGI(TAG, "Current Ciphersuite: %s", mbedtls_ssl_get_ciphersuite(&user_cb->tls->ssl));
  72. break;
  73. case HTTPD_SSL_USER_CB_SESS_CLOSE:
  74. ESP_LOGD(TAG, "At session close");
  75. // Logging the peer certificate
  76. print_peer_cert_info(&user_cb->tls->ssl);
  77. break;
  78. default:
  79. ESP_LOGE(TAG, "Illegal state!");
  80. return;
  81. }
  82. }
  83. #endif
  84. static const httpd_uri_t root = {
  85. .uri = "/",
  86. .method = HTTP_GET,
  87. .handler = root_get_handler
  88. };
  89. static httpd_handle_t start_webserver(void)
  90. {
  91. httpd_handle_t server = NULL;
  92. // Start the httpd server
  93. ESP_LOGI(TAG, "Starting server");
  94. httpd_ssl_config_t conf = HTTPD_SSL_CONFIG_DEFAULT();
  95. extern const unsigned char servercert_start[] asm("_binary_servercert_pem_start");
  96. extern const unsigned char servercert_end[] asm("_binary_servercert_pem_end");
  97. conf.servercert = servercert_start;
  98. conf.servercert_len = servercert_end - servercert_start;
  99. extern const unsigned char prvtkey_pem_start[] asm("_binary_prvtkey_pem_start");
  100. extern const unsigned char prvtkey_pem_end[] asm("_binary_prvtkey_pem_end");
  101. conf.prvtkey_pem = prvtkey_pem_start;
  102. conf.prvtkey_len = prvtkey_pem_end - prvtkey_pem_start;
  103. #if CONFIG_EXAMPLE_ENABLE_HTTPS_USER_CALLBACK
  104. conf.user_cb = https_server_user_callback;
  105. #endif
  106. esp_err_t ret = httpd_ssl_start(&server, &conf);
  107. if (ESP_OK != ret) {
  108. ESP_LOGI(TAG, "Error starting server!");
  109. return NULL;
  110. }
  111. // Set URI handlers
  112. ESP_LOGI(TAG, "Registering URI handlers");
  113. httpd_register_uri_handler(server, &root);
  114. return server;
  115. }
  116. static esp_err_t stop_webserver(httpd_handle_t server)
  117. {
  118. // Stop the httpd server
  119. return httpd_ssl_stop(server);
  120. }
  121. static void disconnect_handler(void* arg, esp_event_base_t event_base,
  122. int32_t event_id, void* event_data)
  123. {
  124. httpd_handle_t* server = (httpd_handle_t*) arg;
  125. if (*server) {
  126. if (stop_webserver(*server) == ESP_OK) {
  127. *server = NULL;
  128. } else {
  129. ESP_LOGE(TAG, "Failed to stop https server");
  130. }
  131. }
  132. }
  133. static void connect_handler(void* arg, esp_event_base_t event_base,
  134. int32_t event_id, void* event_data)
  135. {
  136. httpd_handle_t* server = (httpd_handle_t*) arg;
  137. if (*server == NULL) {
  138. *server = start_webserver();
  139. }
  140. }
  141. void app_main(void)
  142. {
  143. static httpd_handle_t server = NULL;
  144. ESP_ERROR_CHECK(nvs_flash_init());
  145. ESP_ERROR_CHECK(esp_netif_init());
  146. ESP_ERROR_CHECK(esp_event_loop_create_default());
  147. /* Register event handlers to start server when Wi-Fi or Ethernet is connected,
  148. * and stop server when disconnection happens.
  149. */
  150. #ifdef CONFIG_EXAMPLE_CONNECT_WIFI
  151. ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &connect_handler, &server));
  152. ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &disconnect_handler, &server));
  153. #endif // CONFIG_EXAMPLE_CONNECT_WIFI
  154. #ifdef CONFIG_EXAMPLE_CONNECT_ETHERNET
  155. ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &connect_handler, &server));
  156. ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ETHERNET_EVENT_DISCONNECTED, &disconnect_handler, &server));
  157. #endif // CONFIG_EXAMPLE_CONNECT_ETHERNET
  158. /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
  159. * Read "Establishing Wi-Fi or Ethernet Connection" section in
  160. * examples/protocols/README.md for more information about this function.
  161. */
  162. ESP_ERROR_CHECK(example_connect());
  163. }