https_server.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 "openssl/ssl.h"
  17. #include "esp_log.h"
  18. #include "sdkconfig.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. SSL_shutdown(ctx);
  29. SSL_free(ctx);
  30. ESP_LOGD(TAG, "Secure socket closed");
  31. }
  32. /**
  33. * SSL socket pending-check function
  34. *
  35. * @param server
  36. * @param sockfd
  37. * @return number of pending bytes, negative on error
  38. */
  39. static int httpd_ssl_pending(httpd_handle_t server, int sockfd)
  40. {
  41. SSL *ssl = httpd_sess_get_transport_ctx(server, sockfd);
  42. assert(ssl != NULL);
  43. return SSL_pending(ssl);
  44. }
  45. /**
  46. * Receive from a SSL socket
  47. *
  48. * @param server
  49. * @param sockfd
  50. * @param buf
  51. * @param buf_len
  52. * @param flags
  53. * @return bytes read, negative on error
  54. */
  55. static int httpd_ssl_recv(httpd_handle_t server, int sockfd, char *buf, size_t buf_len, int flags)
  56. {
  57. SSL *ssl = httpd_sess_get_transport_ctx(server, sockfd);
  58. assert(ssl != NULL);
  59. return SSL_read(ssl, buf, buf_len);
  60. }
  61. /**
  62. * Send to a SSL socket
  63. *
  64. * @param server
  65. * @param sockfd
  66. * @param buf
  67. * @param buf_len
  68. * @param flags
  69. * @return bytes sent, negative on error
  70. */
  71. static int httpd_ssl_send(httpd_handle_t server, int sockfd, const char *buf, size_t buf_len, int flags)
  72. {
  73. SSL *ssl = httpd_sess_get_transport_ctx(server, sockfd);
  74. assert(ssl != NULL);
  75. return SSL_write(ssl, buf, buf_len);
  76. }
  77. /**
  78. * Open a SSL socket for the server.
  79. * The fd is already open and ready to read / write raw data.
  80. *
  81. * @param server
  82. * @param sockfd - raw socket fd
  83. * @return success
  84. */
  85. static esp_err_t httpd_ssl_open(httpd_handle_t server, int sockfd)
  86. {
  87. assert(server != NULL);
  88. // Retrieve the SSL context from the global context field (set in config)
  89. SSL_CTX *global_ctx = httpd_get_global_transport_ctx(server);
  90. assert(global_ctx != NULL);
  91. SSL *ssl = SSL_new(global_ctx);
  92. if (NULL == ssl) {
  93. ESP_LOGE(TAG, "SSL_new ret NULL (out of memory)");
  94. return ESP_ERR_NO_MEM;
  95. }
  96. if (1 != SSL_set_fd(ssl, sockfd)) {
  97. ESP_LOGE(TAG, "fail to set SSL fd");
  98. goto teardown;
  99. }
  100. ESP_LOGD(TAG, "SSL accept");
  101. if (1 != SSL_accept(ssl)) {
  102. ESP_LOGW(TAG, "fail to SSL_accept - handshake error");
  103. goto teardown;
  104. }
  105. // Store the SSL session into the context field of the HTTPD session object
  106. httpd_sess_set_transport_ctx(server, sockfd, ssl, httpd_ssl_close);
  107. // Set rx/tx/pending override functions
  108. httpd_sess_set_send_override(server, sockfd, httpd_ssl_send);
  109. httpd_sess_set_recv_override(server, sockfd, httpd_ssl_recv);
  110. httpd_sess_set_pending_override(server, sockfd, httpd_ssl_pending);
  111. // all access should now go through SSL
  112. ESP_LOGD(TAG, "Secure socket open");
  113. return ESP_OK;
  114. teardown:
  115. SSL_free(ssl);
  116. return ESP_FAIL;
  117. }
  118. /**
  119. * Tear down the HTTPD global transport context
  120. *
  121. * @param ctx
  122. */
  123. static void free_secure_context(void *ctx)
  124. {
  125. assert(ctx != NULL);
  126. ESP_LOGI(TAG, "Server shuts down, releasing SSL context");
  127. SSL_CTX_free(ctx);
  128. }
  129. /**
  130. * Create and perform basic init of a SSL_CTX, or return NULL on failure
  131. *
  132. * @return ctx or null
  133. */
  134. static SSL_CTX *create_secure_context(const struct httpd_ssl_config *config)
  135. {
  136. SSL_CTX *ctx = NULL;
  137. ESP_LOGD(TAG, "SSL server context create");
  138. ctx = SSL_CTX_new(TLS_server_method());
  139. if (NULL != ctx) {
  140. //region SSL ctx alloc'd
  141. ESP_LOGD(TAG, "SSL ctx set own cert");
  142. if (SSL_CTX_use_certificate_ASN1(ctx, config->cacert_len, config->cacert_pem)
  143. && SSL_CTX_use_PrivateKey_ASN1(0, ctx, config->prvtkey_pem, (long) config->prvtkey_len)) {
  144. return ctx;
  145. }
  146. else {
  147. ESP_LOGE(TAG, "Failed to set certificate");
  148. SSL_CTX_free(ctx);
  149. ctx = NULL;
  150. }
  151. } else {
  152. ESP_LOGE(TAG, "Failed to create SSL context");
  153. }
  154. return NULL;
  155. }
  156. /** Start the server */
  157. esp_err_t httpd_ssl_start(httpd_handle_t *pHandle, struct httpd_ssl_config *config)
  158. {
  159. assert(config != NULL);
  160. assert(pHandle != NULL);
  161. ESP_LOGI(TAG, "Starting server");
  162. if (HTTPD_SSL_TRANSPORT_SECURE == config->transport_mode) {
  163. SSL_CTX *ctx = create_secure_context(config);
  164. if (!ctx) {
  165. return ESP_FAIL;
  166. }
  167. ESP_LOGD(TAG, "SSL context ready");
  168. // set SSL specific config
  169. config->httpd.global_transport_ctx = ctx;
  170. config->httpd.global_transport_ctx_free_fn = free_secure_context;
  171. config->httpd.open_fn = httpd_ssl_open; // the open function configures the created SSL sessions
  172. config->httpd.server_port = config->port_secure;
  173. } else {
  174. ESP_LOGD(TAG, "SSL disabled, using plain HTTP");
  175. config->httpd.server_port = config->port_insecure;
  176. }
  177. httpd_handle_t handle = NULL;
  178. esp_err_t ret = httpd_start(&handle, &config->httpd);
  179. if (ret != ESP_OK) return ret;
  180. *pHandle = handle;
  181. ESP_LOGI(TAG, "Server listening on port %d", config->httpd.server_port);
  182. return ESP_OK;
  183. }
  184. /** Stop the server */
  185. void httpd_ssl_stop(httpd_handle_t handle)
  186. {
  187. httpd_stop(handle);
  188. }