Created
August 28, 2023 13:50
-
-
Save RockfordWei/5748faad3b9ab9a78ecac76a7e12ba26 to your computer and use it in GitHub Desktop.
testssl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <unistd.h> | |
#include <arpa/inet.h> | |
#include <netinet/in.h> | |
#include <sys/socket.h> | |
#include <openssl/ssl.h> | |
#include <openssl/err.h> | |
int main() { | |
SSL_CTX *ctx; | |
SSL *ssl; | |
int server_fd, client_fd; | |
// Initialize OpenSSL | |
SSL_library_init(); | |
SSL_load_error_strings(); | |
ctx = SSL_CTX_new(TLSv1_2_server_method()); | |
// Load the self-signed certificate and private key | |
SSL_CTX_use_certificate_file(ctx, "/tmp/public.pem", SSL_FILETYPE_PEM); | |
SSL_CTX_use_PrivateKey_file(ctx, "/tmp/private.pem", SSL_FILETYPE_PEM); | |
// Create a socket | |
server_fd = socket(AF_INET, SOCK_STREAM, 0); | |
if (server_fd == -1) { | |
perror("Failed to create socket"); | |
exit(EXIT_FAILURE); | |
} | |
// Allow rebind to avoid "Address already in use" error | |
int reuseaddr = 1; | |
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &reuseaddr, sizeof(reuseaddr)) == -1) { | |
perror("Failed to set socket options"); | |
exit(EXIT_FAILURE); | |
} | |
// Bind to port 443 | |
struct sockaddr_in server_addr; | |
server_addr.sin_family = AF_INET; | |
server_addr.sin_port = htons(443); | |
server_addr.sin_addr.s_addr = INADDR_ANY; | |
if (bind(server_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) { | |
perror("Failed to bind"); | |
exit(EXIT_FAILURE); | |
} | |
// Listen for incoming connections | |
if (listen(server_fd, 10) == -1) { | |
perror("Failed to listen"); | |
exit(EXIT_FAILURE); | |
} | |
printf("Server listening on port 443...\n"); | |
while (1) { | |
struct sockaddr_in client_addr; | |
socklen_t client_len = sizeof(client_addr); | |
// Accept a client connection | |
client_fd = accept(server_fd, (struct sockaddr *)&client_addr, &client_len); | |
if (client_fd == -1) { | |
perror("Failed to accept client connection"); | |
continue; | |
} | |
// Create an SSL object and set the socket | |
ssl = SSL_new(ctx); | |
SSL_set_fd(ssl, client_fd); | |
// Perform SSL handshake | |
if (SSL_accept(ssl) != 1) { | |
ERR_print_errors_fp(stderr); | |
close(client_fd); | |
SSL_free(ssl); | |
continue; | |
} | |
// Send an HTTP response | |
const char *response = "HTTP/1.1 200 OK\r\nContent-Length: 12\r\n\r\nHello, World!"; | |
SSL_write(ssl, response, strlen(response)); | |
// Shutdown SSL and close the connection | |
SSL_shutdown(ssl); | |
SSL_free(ssl); | |
close(client_fd); | |
} | |
// Clean up | |
close(server_fd); | |
SSL_CTX_free(ctx); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment