Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

abhi9ab/simple-https-server

Open more actions menu

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

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published
Morty Proxy This is a proxified and sanitized view of the page, visit original site.