commit 8f837ca67b636b8eec18d64c776e5d74dbbd33f7 Author: Kiril Date: Tue Apr 30 14:31:49 2024 +0100 Inital commit diff --git a/compile.sh b/compile.sh new file mode 100755 index 0000000..f26d48e --- /dev/null +++ b/compile.sh @@ -0,0 +1 @@ +gcc server.c -l ssl diff --git a/index.html b/index.html new file mode 100644 index 0000000..40b1318 --- /dev/null +++ b/index.html @@ -0,0 +1,10 @@ + + + + Test Demo Page + + +

Test Page

+

If you are seeing this page, you have correctly configured the web server to run under HTTPS!

+ + diff --git a/server.c b/server.c new file mode 100644 index 0000000..b20d57e --- /dev/null +++ b/server.c @@ -0,0 +1,50 @@ +#include +#include +#include +#include +#include +#include +#include + +static const short PORT = 8081; +static const short BACKLOG_SIZE = 10; +static const char * CERTIFICATE_PATH = "cert.pem"; +static const char * PRIVATE_KEY_PATH = "key"; +static const char * ERROR_404 = "No page found"; + +int main(int argc, char** argv) { + int socket_fd = socket(AF_INET, SOCK_STREAM, 0); + const struct sockaddr addr = { + AF_INET, + htons(PORT), + 0 + }; + bind(socket_fd, &addr, sizeof(addr)); + listen(socket_fd, BACKLOG_SIZE); + int client_fd = accept(socket_fd, NULL, NULL); + SSL_CTX* ctx = SSL_CTX_new(TLS_server_method()); + SSL* ssl = SSL_new(ctx); + SSL_set_fd(ssl, client_fd); + SSL_use_certificate_chain_file(ssl, CERTIFICATE_PATH); + SSL_use_PrivateKey_file(ssl, PRIVATE_KEY_PATH, SSL_FILETYPE_PEM); + SSL_accept(ssl); + char buffer[1024] = {0}; + SSL_read(ssl, buffer, 1023); + // Assume GET request + char* file_request = buffer + 5; + char response[1024] = {0}; + char* headers = "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n"; + + memcpy(response, headers, strlen(headers)); + + if (strncmp(file_request, "index.html", strlen("index.html"))) { + FILE* f = fopen("index.html", "r"); + fread(response + strlen(headers), 1024 - strlen(headers) - 1, 1, f); + fclose(f); + } else { + memcpy(response + strlen(headers), ERROR_404, strlen(ERROR_404)); + } + SSL_write(ssl, response, 1024); + SSL_shutdown(ssl); + +}