web.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. /*
  14. * This file is a modified version of the lwIP web server demo. The original
  15. * author is unknown because the file didn't contain any license information.
  16. *
  17. * The HTTPS version is Copyright (C) 2017 - WolfSSL Inc. and is based on the
  18. * demo HTTP code of ChibiOS.
  19. */
  20. /**
  21. * @file web.c
  22. * @brief HTTPS server wrapper thread code.
  23. * @addtogroup WEB_THREAD
  24. * @{
  25. */
  26. #include <ctype.h>
  27. #include "ch.h"
  28. #include "lwip/opt.h"
  29. #include "lwip/arch.h"
  30. #include "lwip/api.h"
  31. #include "wolfssl_chibios.h"
  32. #include "web.h"
  33. #if LWIP_NETCONN
  34. static char url_buffer[WEB_MAX_PATH_SIZE];
  35. extern unsigned char server_cert[];
  36. extern unsigned int server_cert_len;
  37. extern unsigned char server_key[];
  38. extern unsigned int server_key_len;
  39. #define HEXTOI(x) (isdigit(x) ? (x) - '0' : (x) - 'a' + 10)
  40. /**
  41. * @brief Decodes an URL sting.
  42. * @note The string is terminated by a zero or a separator.
  43. *
  44. * @param[in] url encoded URL string
  45. * @param[out] buf buffer for the processed string
  46. * @param[in] max max number of chars to copy into the buffer
  47. * @return The conversion status.
  48. * @retval false string converted.
  49. * @retval true the string was not valid or the buffer overflowed
  50. *
  51. * @notapi
  52. */
  53. static bool decode_url(const char *url, char *buf, size_t max) {
  54. while (true) {
  55. int h, l;
  56. unsigned c = *url++;
  57. switch (c) {
  58. case 0:
  59. case '\r':
  60. case '\n':
  61. case '\t':
  62. case ' ':
  63. case '?':
  64. *buf = 0;
  65. return false;
  66. case '.':
  67. if (max <= 1)
  68. return true;
  69. h = *(url + 1);
  70. if (h == '.')
  71. return true;
  72. break;
  73. case '%':
  74. if (max <= 1)
  75. return true;
  76. h = tolower((int)*url++);
  77. if (h == 0)
  78. return true;
  79. if (!isxdigit(h))
  80. return true;
  81. l = tolower((int)*url++);
  82. if (l == 0)
  83. return true;
  84. if (!isxdigit(l))
  85. return true;
  86. c = (char)((HEXTOI(h) << 4) | HEXTOI(l));
  87. break;
  88. default:
  89. if (max <= 1)
  90. return true;
  91. if (!isalnum(c) && (c != '_') && (c != '-') && (c != '+') &&
  92. (c != '/'))
  93. return true;
  94. break;
  95. }
  96. *buf++ = c;
  97. max--;
  98. }
  99. }
  100. #define MAX_HTTPREQ_SIZE 256
  101. static const char http_html_hdr[] = "HTTP/1.1 200 OK\r\nContent-type: text/html\r\n\r\n";
  102. static const char http_index_html[] = "<html><head><title>Congrats!</title></head><body><h1>Welcome to chibiOS HTTPS server!</h1><p>Powered by LwIP + WolfSSL</body></html>";
  103. static char inbuf[MAX_HTTPREQ_SIZE];
  104. static void https_server_serve(sslconn *sc)
  105. {
  106. int ret;
  107. /* Read the data from the port, blocking if nothing yet there.
  108. We assume the request (the part we care about) is in one netbuf.*/
  109. ret = wolfSSL_read(sc->ssl, inbuf, MAX_HTTPREQ_SIZE);
  110. if (ret >= 5 &&
  111. inbuf[0] == 'G' &&
  112. inbuf[1] == 'E' &&
  113. inbuf[2] == 'T' &&
  114. inbuf[3] == ' ' &&
  115. inbuf[4] == '/') {
  116. if (decode_url(inbuf + 4, url_buffer, WEB_MAX_PATH_SIZE)) {
  117. /* Invalid URL handling.*/
  118. return;
  119. }
  120. /* Send the HTML header
  121. * subtract 1 from the size, since we dont send the \0 in the string
  122. * NETCONN_NOCOPY: our data is const static, so no need to copy it
  123. */
  124. wolfSSL_write(sc->ssl, http_html_hdr, sizeof(http_html_hdr)-1);
  125. /* Send our HTML page */
  126. wolfSSL_write(sc->ssl, http_index_html, sizeof(http_index_html)-1);
  127. }
  128. }
  129. /**
  130. * @brief Stack area for the http thread.
  131. */
  132. THD_WORKING_AREA(wa_https_server, WEB_THREAD_STACK_SIZE);
  133. /**
  134. * @brssl HTTPS server thread.
  135. */
  136. THD_FUNCTION(https_server, p) {
  137. sslconn *sc, *newsc;
  138. (void)p;
  139. chRegSetThreadName("https");
  140. /* Initialize wolfSSL */
  141. wolfSSL_Init();
  142. /* Create a new SSL connection handle */
  143. sc = sslconn_new(NETCONN_TCP, wolfTLSv1_2_server_method());
  144. if (!sc) {
  145. while(1) {}
  146. }
  147. /* Load certificate file for the HTTPS server */
  148. if (wolfSSL_CTX_use_certificate_buffer(sc->ctx, server_cert,
  149. server_cert_len, SSL_FILETYPE_ASN1 ) != SSL_SUCCESS)
  150. while(1) {}
  151. /* Load the private key */
  152. if (wolfSSL_CTX_use_PrivateKey_buffer(sc->ctx, server_key,
  153. server_key_len, SSL_FILETYPE_ASN1 ) != SSL_SUCCESS)
  154. while(1) {}
  155. /* Bind to port 443 (HTTPS) with default IP address */
  156. netconn_bind(sc->conn, NULL, WEB_THREAD_PORT);
  157. /* Put the connection into LISTEN state */
  158. netconn_listen(sc->conn);
  159. /* Goes to the final priority after initialization.*/
  160. chThdSetPriority(WEB_THREAD_PRIORITY);
  161. /* Listening loop */
  162. while (true) {
  163. newsc = sslconn_accept(sc);
  164. if (!newsc) {
  165. chThdSleepMilliseconds(500);
  166. continue;
  167. }
  168. /* New connection: a new SSL connector is spawned */
  169. https_server_serve(newsc);
  170. sslconn_close(newsc);
  171. }
  172. }
  173. #endif /* LWIP_NETCONN */
  174. /** @} */