|
| 1 | +/* https_server.c |
| 2 | + * |
| 3 | + * Copyright (C) 2006-2024 wolfSSL Inc. |
| 4 | + * |
| 5 | + * This file is part of wolfSSL. |
| 6 | + * |
| 7 | + * wolfSSL is free software; you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU General Public License as published by |
| 9 | + * the Free Software Foundation; either version 2 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * |
| 12 | + * wolfSSL is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License |
| 18 | + * along with this program; if not, write to the Free Software |
| 19 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
| 20 | + */ |
| 21 | + |
| 22 | +#include "https_server.h" |
| 23 | +#include "httpd.h" |
| 24 | +#include <wolfssl/ssl.h> |
| 25 | +#include <wolfssl/wolfcrypt/error-crypt.h> |
| 26 | + |
| 27 | +static WOLFSSL_CTX *g_ssl_ctx = NULL; |
| 28 | +static struct httpd g_httpd; |
| 29 | + |
| 30 | +/* Root page handler */ |
| 31 | +static int handle_root(struct httpd *httpd, struct http_client *hc, struct http_request *req) { |
| 32 | + const char *response = "<html><body><h1>wolfSSL HTTPS Demo</h1>" |
| 33 | + "<p>TLS 1.3 + FreeRTOS + wolfIP</p></body></html>"; |
| 34 | + http_send_response_headers(hc, HTTP_STATUS_OK, "OK", "text/html", strlen(response)); |
| 35 | + http_send_response_body(hc, response, strlen(response)); |
| 36 | + return 0; |
| 37 | +} |
| 38 | + |
| 39 | +int https_server_init(struct wolfIP *ipstack) { |
| 40 | + int ret; |
| 41 | + |
| 42 | + /* Initialize wolfSSL */ |
| 43 | + if ((ret = wolfSSL_Init()) != WOLFSSL_SUCCESS) { |
| 44 | + printf("Failed to initialize wolfSSL\n"); |
| 45 | + return -1; |
| 46 | + } |
| 47 | + |
| 48 | + /* Create and initialize WOLFSSL_CTX */ |
| 49 | + if ((g_ssl_ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method())) == NULL) { |
| 50 | + printf("Failed to create WOLFSSL_CTX\n"); |
| 51 | + return -1; |
| 52 | + } |
| 53 | + |
| 54 | + /* Load server certificates */ |
| 55 | + if ((ret = wolfSSL_CTX_use_certificate_file(g_ssl_ctx, CERT_FILE, |
| 56 | + WOLFSSL_FILETYPE_PEM)) != WOLFSSL_SUCCESS) { |
| 57 | + printf("Failed to load %s\n", CERT_FILE); |
| 58 | + return -1; |
| 59 | + } |
| 60 | + |
| 61 | + /* Load server key */ |
| 62 | + if ((ret = wolfSSL_CTX_use_PrivateKey_file(g_ssl_ctx, KEY_FILE, |
| 63 | + WOLFSSL_FILETYPE_PEM)) != WOLFSSL_SUCCESS) { |
| 64 | + printf("Failed to load %s\n", KEY_FILE); |
| 65 | + return -1; |
| 66 | + } |
| 67 | + |
| 68 | + /* Initialize HTTP server with SSL context */ |
| 69 | + if (httpd_init(&g_httpd, ipstack, HTTPS_PORT, g_ssl_ctx) != 0) { |
| 70 | + printf("Failed to initialize HTTPS server\n"); |
| 71 | + return -1; |
| 72 | + } |
| 73 | + |
| 74 | + /* Register handlers */ |
| 75 | + if (httpd_register_handler(&g_httpd, "/", handle_root) != 0) { |
| 76 | + printf("Failed to register root handler\n"); |
| 77 | + return -1; |
| 78 | + } |
| 79 | + |
| 80 | + printf("HTTPS server initialized on port %d\n", HTTPS_PORT); |
| 81 | + return 0; |
| 82 | +} |
| 83 | + |
| 84 | +static void https_server_task(void* pvParameters) { |
| 85 | + const TickType_t xDelay = pdMS_TO_TICKS(100); |
| 86 | + |
| 87 | + printf("HTTPS server task started\n"); |
| 88 | + |
| 89 | + /* Task main loop - wolfIP handles connections in callbacks */ |
| 90 | + for(;;) { |
| 91 | + vTaskDelay(xDelay); |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +int https_server_start(void) { |
| 96 | + BaseType_t ret; |
| 97 | + |
| 98 | + ret = xTaskCreate(https_server_task, |
| 99 | + "HTTPS_Server", |
| 100 | + HTTPS_TASK_STACK_SIZE, |
| 101 | + NULL, |
| 102 | + HTTPS_TASK_PRIORITY, |
| 103 | + NULL); |
| 104 | + |
| 105 | + return (ret == pdPASS) ? 0 : -1; |
| 106 | +} |
0 commit comments