https_server.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // Copyright 2018 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <string.h>
  15. #include "esp_https_server.h"
  16. #include "esp_log.h"
  17. #include "sdkconfig.h"
  18. #include "esp_tls.h"
  19. const static char *TAG = "esp_https_server";
  20. /**
  21. * SSL socket close handler
  22. *
  23. * @param[in] ctx - session transport context (SSL context we stored there)
  24. */
  25. static void httpd_ssl_close(void *ctx)
  26. {
  27. assert(ctx != NULL);
  28. esp_tls_server_session_delete(ctx);
  29. ESP_LOGD(TAG, "Secure socket closed");
  30. }
  31. /**
  32. * SSL socket pending-check function
  33. *
  34. * @param server
  35. * @param sockfd
  36. * @return number of pending bytes, negative on error
  37. */
  38. static int httpd_ssl_pending(httpd_handle_t server, int sockfd)
  39. {
  40. esp_tls_t *tls = httpd_sess_get_transport_ctx(server, sockfd);
  41. assert(tls != NULL);
  42. return esp_tls_get_bytes_avail(tls);
  43. }
  44. /**
  45. * Receive from a SSL socket
  46. *
  47. * @param server
  48. * @param sockfd
  49. * @param buf
  50. * @param buf_len
  51. * @param flags
  52. * @return bytes read, negative on error
  53. */
  54. static int httpd_ssl_recv(httpd_handle_t server, int sockfd, char *buf, size_t buf_len, int flags)
  55. {
  56. esp_tls_t *tls = httpd_sess_get_transport_ctx(server, sockfd);
  57. assert(tls != NULL);
  58. return esp_tls_conn_read(tls, buf, buf_len);
  59. }
  60. /**
  61. * Send to a SSL socket
  62. *
  63. * @param server
  64. * @param sockfd
  65. * @param buf
  66. * @param buf_len
  67. * @param flags
  68. * @return bytes sent, negative on error
  69. */
  70. static int httpd_ssl_send(httpd_handle_t server, int sockfd, const char *buf, size_t buf_len, int flags)
  71. {
  72. esp_tls_t *tls = httpd_sess_get_transport_ctx(server, sockfd);
  73. assert(tls != NULL);
  74. return esp_tls_conn_write(tls, buf, buf_len);
  75. }
  76. /**
  77. * Open a SSL socket for the server.
  78. * The fd is already open and ready to read / write raw data.
  79. *
  80. * @param server
  81. * @param sockfd - raw socket fd
  82. * @return success
  83. */
  84. static esp_err_t httpd_ssl_open(httpd_handle_t server, int sockfd)
  85. {
  86. assert(server != NULL);
  87. // Retrieve the SSL context from the global context field (set in config)
  88. esp_tls_cfg_server_t *global_ctx = httpd_get_global_transport_ctx(server);
  89. assert(global_ctx != NULL);
  90. esp_tls_t *tls = (esp_tls_t *)calloc(1, sizeof(esp_tls_t));
  91. if (!tls) {
  92. return ESP_ERR_NO_MEM;
  93. }
  94. ESP_LOGI(TAG, "performing session handshake");
  95. int ret = esp_tls_server_session_create(global_ctx, sockfd, tls);
  96. if (ret != 0) {
  97. ESP_LOGE(TAG, "esp_tls_create_server_session failed");
  98. goto fail;
  99. }
  100. // Store the SSL session into the context field of the HTTPD session object
  101. httpd_sess_set_transport_ctx(server, sockfd, tls, httpd_ssl_close);
  102. // Set rx/tx/pending override functions
  103. httpd_sess_set_send_override(server, sockfd, httpd_ssl_send);
  104. httpd_sess_set_recv_override(server, sockfd, httpd_ssl_recv);
  105. httpd_sess_set_pending_override(server, sockfd, httpd_ssl_pending);
  106. // all access should now go through SSL
  107. ESP_LOGD(TAG, "Secure socket open");
  108. return ESP_OK;
  109. fail:
  110. esp_tls_server_session_delete(tls);
  111. return ESP_FAIL;
  112. }
  113. /**
  114. * Tear down the HTTPD global transport context
  115. *
  116. * @param ctx
  117. */
  118. static void free_secure_context(void *ctx)
  119. {
  120. assert(ctx != NULL);
  121. esp_tls_cfg_server_t *cfg = (esp_tls_cfg_server_t *)ctx;
  122. ESP_LOGI(TAG, "Server shuts down, releasing SSL context");
  123. if (cfg->cacert_buf) {
  124. free((void *)cfg->cacert_buf);
  125. }
  126. if (cfg->servercert_buf) {
  127. free((void *)cfg->servercert_buf);
  128. }
  129. if (cfg->serverkey_buf) {
  130. free((void *)cfg->serverkey_buf);
  131. }
  132. free(cfg);
  133. }
  134. static esp_tls_cfg_server_t *create_secure_context(const struct httpd_ssl_config *config)
  135. {
  136. esp_tls_cfg_server_t *cfg = (esp_tls_cfg_server_t *)calloc(1, sizeof(esp_tls_cfg_server_t));
  137. if (!cfg) {
  138. return NULL;
  139. }
  140. /* cacert = CA which signs client cert, or client cert itself , which is mapped to client_verify_cert_pem */
  141. if(config->client_verify_cert_pem != NULL) {
  142. cfg->cacert_buf = (unsigned char *)malloc(config->client_verify_cert_len);
  143. if (!cfg->cacert_buf) {
  144. ESP_LOGE(TAG, "Could not allocate memory");
  145. free(cfg);
  146. return NULL;
  147. }
  148. memcpy((char *)cfg->cacert_buf, config->client_verify_cert_pem, config->client_verify_cert_len);
  149. cfg->cacert_bytes = config->client_verify_cert_len;
  150. }
  151. /* servercert = cert of server itself ( in our case it is mapped to cacert in https_server example) */
  152. cfg->servercert_buf = (unsigned char *)malloc(config->cacert_len);
  153. if (!cfg->servercert_buf) {
  154. ESP_LOGE(TAG, "Could not allocate memory");
  155. free((void *)cfg->cacert_buf);
  156. free(cfg);
  157. return NULL;
  158. }
  159. memcpy((char *)cfg->servercert_buf, config->cacert_pem, config->cacert_len);
  160. cfg->servercert_bytes = config->cacert_len;
  161. cfg->serverkey_buf = (unsigned char *)malloc(config->prvtkey_len);
  162. if (!cfg->serverkey_buf) {
  163. ESP_LOGE(TAG, "Could not allocate memory");
  164. free((void *)cfg->servercert_buf);
  165. free((void *)cfg->cacert_buf);
  166. free(cfg);
  167. return NULL;
  168. }
  169. memcpy((char *)cfg->serverkey_buf, config->prvtkey_pem, config->prvtkey_len);
  170. cfg->serverkey_bytes = config->prvtkey_len;
  171. return cfg;
  172. }
  173. /** Start the server */
  174. esp_err_t httpd_ssl_start(httpd_handle_t *pHandle, struct httpd_ssl_config *config)
  175. {
  176. assert(config != NULL);
  177. assert(pHandle != NULL);
  178. ESP_LOGI(TAG, "Starting server");
  179. if (HTTPD_SSL_TRANSPORT_SECURE == config->transport_mode) {
  180. esp_tls_cfg_server_t *esp_tls_cfg = create_secure_context(config);
  181. if (!esp_tls_cfg) {
  182. return -1;
  183. }
  184. ESP_LOGD(TAG, "SSL context ready");
  185. // set SSL specific config
  186. config->httpd.global_transport_ctx = esp_tls_cfg;
  187. config->httpd.global_transport_ctx_free_fn = free_secure_context;
  188. config->httpd.open_fn = httpd_ssl_open; // the open function configures the created SSL sessions
  189. config->httpd.server_port = config->port_secure;
  190. } else {
  191. ESP_LOGD(TAG, "SSL disabled, using plain HTTP");
  192. config->httpd.server_port = config->port_insecure;
  193. }
  194. httpd_handle_t handle = NULL;
  195. esp_err_t ret = httpd_start(&handle, &config->httpd);
  196. if (ret != ESP_OK) return ret;
  197. *pHandle = handle;
  198. ESP_LOGI(TAG, "Server listening on port %d", config->httpd.server_port);
  199. return ESP_OK;
  200. }
  201. /** Stop the server */
  202. void httpd_ssl_stop(httpd_handle_t handle)
  203. {
  204. httpd_stop(handle);
  205. }