Skip to content

abhi9ab/simple-https-server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Simple HTTPS Server in C

A lightweight, educational HTTPS server implementation demonstrating systems programming, network engineering, and security concepts using C and OpenSSL.

🎯 Project Overview

This project showcases:

  • Systems Programming: BSD Sockets API for low-level network communication
  • Security & Cryptography: TLS/SSL handshake implementation using OpenSSL
  • Network Engineering: TCP/IP and HTTPS protocol handling
  • Memory Management: Proper allocation, deallocation, and error handling

πŸ—οΈ Project Structure

simple-https-server/
β”œβ”€β”€ main.c              # Main server implementation
β”œβ”€β”€ Makefile           # Build configuration and automation
β”œβ”€β”€ README.md          # Project documentation
β”œβ”€β”€ server.crt         # SSL certificate (generated automatically)
β”œβ”€β”€ server.key         # SSL private key (generated automatically)
└── https_server       # Compiled executable (after build)

πŸ”§ Prerequisites

Required Dependencies

Ubuntu/Debian:

sudo apt-get update
sudo apt-get install build-essential libssl-dev openssl

CentOS/RHEL/Fedora:

sudo yum install gcc openssl-devel openssl

macOS:

# Install Xcode command line tools
xcode-select --install

# Install OpenSSL via Homebrew
brew install openssl

# Set environment variables for compilation
export CPPFLAGS="-I$(brew --prefix openssl)/include"
export LDFLAGS="-L$(brew --prefix openssl)/lib"

Quick Dependency Installation

The Makefile includes convenience targets:

make install-deps-debian    # For Ubuntu/Debian
make install-deps-redhat    # For CentOS/RHEL/Fedora
make install-deps-macos     # For macOS

πŸš€ Quick Start

1. Build and Run

# Clone or download the project files
# Navigate to the project directory

# Build everything (server + certificates)
make all

# Run the server
./https_server

2. Test the Server

Open your web browser and navigate to:

https://localhost:8443

Note: You'll see a security warning because we're using a self-signed certificate. Click "Advanced" and "Proceed to localhost" to continue.

3. Alternative Testing with curl

curl -k https://localhost:8443

πŸ”¨ Build System

Makefile Targets

Target Description
make all Build server and generate certificates
make https_server Build only the server executable
make certificates Generate self-signed SSL certificates
make clean Remove build files
make clean-all Remove build files and certificates
make test Build and test the server automatically
make help Show available targets

Compilation Details

The server is compiled with these flags:

  • -lssl -lcrypto: Link against OpenSSL libraries
  • -Wall -Wextra: Enable comprehensive warnings
  • -std=c99: Use C99 standard
  • -O2: Optimize for performance
  • -g: Include debugging information

πŸ” SSL/TLS Configuration

Certificate Generation

The Makefile automatically generates a self-signed certificate with:

  • Algorithm: RSA 2048-bit
  • Validity: 365 days
  • Subject: CN=localhost, O=Test Server, C=US

Security Features

  • Minimum TLS Version: TLS 1.2
  • Cipher Suites: Modern secure ciphers (handled by OpenSSL)
  • Certificate Validation: Automatic certificate/key matching

🌐 Network Architecture

Server Configuration

  • Port: 8443 (standard HTTPS alternative port)
  • Protocol: TCP/IP with TLS encryption
  • Binding: All network interfaces (0.0.0.0)
  • Backlog: 10 pending connections

Connection Flow

  1. Socket Creation: Create TCP socket using BSD Sockets API
  2. SSL Context: Initialize OpenSSL context with TLS configuration
  3. Certificate Loading: Load server certificate and private key
  4. Client Accept: Accept incoming TCP connection
  5. SSL Handshake: Perform TLS handshake to establish encryption
  6. Data Exchange: Read HTTP request and send HTML response
  7. Connection Cleanup: Properly close SSL and socket connections

πŸ’‘ Key Learning Points

Systems Programming Concepts

  • File Descriptors: Managing socket file descriptors
  • Process Signals: Graceful shutdown handling
  • Memory Management: Dynamic allocation with proper cleanup
  • Error Handling: Comprehensive error checking and reporting

Network Programming Concepts

  • Socket Programming: Creating, binding, listening, and accepting connections
  • TCP Protocol: Understanding connection-oriented communication
  • HTTP Protocol: Parsing requests and formatting responses
  • Client/Server Architecture: Request-response communication pattern

Security Concepts

  • Public Key Infrastructure: Certificate and private key usage
  • TLS Handshake: Establishing secure, encrypted connections
  • Cryptographic Libraries: Integrating OpenSSL for security
  • Certificate Management: Loading and validating certificates

πŸ”„ Extending to Multi-threading

The current implementation handles one connection at a time. To extend for multiple concurrent connections:

Thread Pool Approach

#include <pthread.h>

// Thread function for handling clients
void* client_handler(void* arg) {
    SSL* ssl = (SSL*)arg;
    handle_client(ssl);
    SSL_shutdown(ssl);
    SSL_free(ssl);
    return NULL;
}

// In main loop, create thread for each connection
pthread_t thread;
pthread_create(&thread, NULL, client_handler, ssl);
pthread_detach(thread);

Process Forking Approach

#include <sys/wait.h>

// In main loop, fork for each connection
pid_t pid = fork();
if (pid == 0) {
    // Child process handles client
    close(server_socket);
    handle_client(ssl);
    exit(0);
} else if (pid > 0) {
    // Parent process continues listening
    SSL_free(ssl);
    close(client_socket);
}

πŸ› Troubleshooting

Common Issues

OpenSSL not found:

# Install development headers
sudo apt-get install libssl-dev  # Ubuntu/Debian

Certificate errors:

# Regenerate certificates
make clean-all
make certificates

Permission denied on port 8443:

# Use unprivileged port or run with sudo
./https_server
# or modify SERVER_PORT in main.c to use port > 1024

Connection refused:

  • Check if server is running: ps aux | grep https_server
  • Verify port is not in use: netstat -an | grep 8443
  • Check firewall settings

Debugging

Compile with debug symbols and run with GDB:

make clean
make CFLAGS="-Wall -Wextra -std=c99 -g -DDEBUG"
gdb ./https_server

πŸ“š Additional Resources

🀝 Contributing

This is an educational project. Feel free to:

  • Add more HTTP methods (POST, PUT, DELETE)
  • Implement HTTP/2 support
  • Add configuration file support
  • Enhance error handling and logging
  • Add unit tests

About

Basic https server for learning purpose

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published