123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- #include <ctype.h>
- #include "ch.h"
- #include "lwip/opt.h"
- #include "lwip/arch.h"
- #include "lwip/api.h"
- #include "wolfssl_chibios.h"
- #include "web.h"
- #if LWIP_NETCONN
- static char url_buffer[WEB_MAX_PATH_SIZE];
- extern unsigned char server_cert[];
- extern unsigned int server_cert_len;
- extern unsigned char server_key[];
- extern unsigned int server_key_len;
- #define HEXTOI(x) (isdigit(x) ? (x) - '0' : (x) - 'a' + 10)
- static bool decode_url(const char *url, char *buf, size_t max) {
- while (true) {
- int h, l;
- unsigned c = *url++;
- switch (c) {
- case 0:
- case '\r':
- case '\n':
- case '\t':
- case ' ':
- case '?':
- *buf = 0;
- return false;
- case '.':
- if (max <= 1)
- return true;
- h = *(url + 1);
- if (h == '.')
- return true;
- break;
- case '%':
- if (max <= 1)
- return true;
- h = tolower((int)*url++);
- if (h == 0)
- return true;
- if (!isxdigit(h))
- return true;
- l = tolower((int)*url++);
- if (l == 0)
- return true;
- if (!isxdigit(l))
- return true;
- c = (char)((HEXTOI(h) << 4) | HEXTOI(l));
- break;
- default:
- if (max <= 1)
- return true;
- if (!isalnum(c) && (c != '_') && (c != '-') && (c != '+') &&
- (c != '/'))
- return true;
- break;
- }
- *buf++ = c;
- max--;
- }
- }
- #define MAX_HTTPREQ_SIZE 256
- static const char http_html_hdr[] = "HTTP/1.1 200 OK\r\nContent-type: text/html\r\n\r\n";
- 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>";
- static char inbuf[MAX_HTTPREQ_SIZE];
- static void https_server_serve(sslconn *sc)
- {
- int ret;
-
- ret = wolfSSL_read(sc->ssl, inbuf, MAX_HTTPREQ_SIZE);
- if (ret >= 5 &&
- inbuf[0] == 'G' &&
- inbuf[1] == 'E' &&
- inbuf[2] == 'T' &&
- inbuf[3] == ' ' &&
- inbuf[4] == '/') {
- if (decode_url(inbuf + 4, url_buffer, WEB_MAX_PATH_SIZE)) {
-
- return;
- }
-
- wolfSSL_write(sc->ssl, http_html_hdr, sizeof(http_html_hdr)-1);
-
- wolfSSL_write(sc->ssl, http_index_html, sizeof(http_index_html)-1);
- }
- }
- THD_WORKING_AREA(wa_https_server, WEB_THREAD_STACK_SIZE);
- THD_FUNCTION(https_server, p) {
- sslconn *sc, *newsc;
- (void)p;
- chRegSetThreadName("https");
-
- wolfSSL_Init();
-
- sc = sslconn_new(NETCONN_TCP, wolfTLSv1_2_server_method());
- if (!sc) {
- while(1) {}
- }
-
- if (wolfSSL_CTX_use_certificate_buffer(sc->ctx, server_cert,
- server_cert_len, SSL_FILETYPE_ASN1 ) != SSL_SUCCESS)
- while(1) {}
-
- if (wolfSSL_CTX_use_PrivateKey_buffer(sc->ctx, server_key,
- server_key_len, SSL_FILETYPE_ASN1 ) != SSL_SUCCESS)
- while(1) {}
-
-
- netconn_bind(sc->conn, NULL, WEB_THREAD_PORT);
-
- netconn_listen(sc->conn);
-
-
- chThdSetPriority(WEB_THREAD_PRIORITY);
-
- while (true) {
- newsc = sslconn_accept(sc);
- if (!newsc) {
- chThdSleepMilliseconds(500);
- continue;
- }
-
- https_server_serve(newsc);
- sslconn_close(newsc);
- }
- }
- #endif
|