Assignment #2 - Secure Chat System
Information Security (CS-3002)
FAST-NUCES, Fall 2025
Student Information:
- Name: Umer Farooq
- Roll No: 22I-0891
- Section: CS-7D
- Instructor: Urooj Ghani
File: README.md
Purpose: Main project documentation and user guide
Description: This file provides comprehensive documentation for the SecureChat system, including setup instructions, execution steps, configuration details, sample input/output formats, and testing procedures.
Links to Other Files:
SETUP.md: Detailed setup guide with prerequisitesPROJECT_STRUCTURE.md: Project structure documentationschema.sql: Database schema definitiontests/manual/README.md: Manual testing documentation and procedures.env.example: Configuration templaterequirements.txt: Python dependencies
Result:
- Provides complete user guide for the system
- Documents all setup and execution procedures
- Includes sample I/O formats and testing guidelines
- Serves as primary reference for users and TAs
A console-based, PKI-enabled Secure Chat System implemented in Python, demonstrating how cryptographic primitives combine to achieve:
Confidentiality, Integrity, Authenticity, and Non-Repudiation (CIANR).
GitHub Repository: https://github.com/Umer-Farooq-CS/SecureChat
Repository Topics:
secure-chatcryptographypkiaes-128rsadiffie-hellmansha-256x509-certificatesinformation-securitypythonclient-serverencryptionauthenticationnon-repudiationciandr
This project implements a secure client-server chat system that uses:
- AES-128 (block cipher) for encryption
- RSA with X.509 certificates for authentication and signatures
- Diffie-Hellman (DH) for key agreement
- SHA-256 for hashing
- Self-built Root CA for certificate issuance
- MySQL for secure user credential storage
The system achieves CIANR through:
- Confidentiality: All messages encrypted with AES-128
- Integrity: SHA-256 hashing and RSA signatures on every message
- Authenticity: X.509 certificate validation and mutual authentication
- Non-Repudiation: Signed session transcripts and SessionReceipts
securechat-cryptography/
├─ app/
│ ├─ client.py # Client workflow (plain TCP, no TLS)
│ ├─ server.py # Server workflow (plain TCP, no TLS)
│ ├─ crypto/
│ │ ├─ aes.py # AES-128(ECB)+PKCS#7 (use cryptography lib)
│ │ ├─ dh.py # Classic DH helpers + key derivation
│ │ ├─ pki.py # X.509 validation (CA signature, validity, CN)
│ │ └─ sign.py # RSA SHA-256 sign/verify (PKCS#1 v1.5)
│ ├─ common/
│ │ ├─ protocol.py # Pydantic message models (hello/login/msg/receipt)
│ │ └─ utils.py # Helpers (base64, now_ms, sha256_hex)
│ └─ storage/
│ ├─ db.py # MySQL user store (salted SHA-256 passwords)
│ └─ transcript.py # Append-only transcript + transcript hash
├─ scripts/
│ ├─ gen_ca.py # Create Root CA (RSA + self-signed X.509)
│ └─ gen_cert.py # Issue client/server certs signed by Root CA
├─ tests/
│ └─ manual/
│ └─ README.md # Manual testing + Wireshark evidence checklist
├─ certs/ # Local certs/keys (gitignored)
├─ transcripts/ # Session logs (gitignored)
├─ .env.example # Sample configuration (no secrets)
├─ .gitignore # Ignore secrets, binaries, logs, and certs
├─ requirements.txt # Python dependencies
└─ README.md # This file
- Python 3.8+
- MySQL 8.0+ (or Docker for MySQL)
- Git (for version control)
git clone [Your GitHub Repository URL]
cd securechat-cryptography# Create virtual environment
python -m venv .venv
# Activate virtual environment
# On Windows:
.venv\Scripts\activate
# On Linux/Mac:
source .venv/bin/activate
# Install dependencies
pip install -r requirements.txt# Copy the example environment file
cp .env.example .env
# Edit .env with your actual configuration
# Update database credentials, server settings, etc.Required Configuration in .env:
DB_HOST: MySQL host (default: localhost)DB_PORT: MySQL port (default: 3306)DB_NAME: Database name (default: securechat)DB_USER: MySQL usernameDB_PASSWORD: MySQL passwordSERVER_HOST: Server hostname (default: localhost)SERVER_PORT: Server port (default: 8888)
Option A: Using Docker (Recommended)
docker run -d --name securechat-db \
-e MYSQL_ROOT_PASSWORD=rootpass \
-e MYSQL_DATABASE=securechat \
-e MYSQL_USER=scuser \
-e MYSQL_PASSWORD=scpass \
-p 3306:3306 \
mysql:8Option B: Using Local MySQL
- Create a database named
securechat - Create a user with appropriate permissions
- Update
.envwith your credentials
Initialize Database Schema:
python -m app.storage.db --initThis creates the users table with the following schema:
CREATE TABLE users (
email VARCHAR(255) PRIMARY KEY,
username VARCHAR(255) UNIQUE NOT NULL,
salt VARBINARY(16) NOT NULL,
pwd_hash CHAR(64) NOT NULL
);Step 1: Generate Root CA
python scripts/gen_ca.py --name "FAST-NU Root CA"This creates:
certs/ca-key.pem(CA private key)certs/ca-cert.pem(CA certificate)
Step 2: Generate Server Certificate
python scripts/gen_cert.py --cn server.local --out certs/serverThis creates:
certs/server-key.pem(server private key)certs/server-cert.pem(server certificate signed by CA)
Step 3: Generate Client Certificate
python scripts/gen_cert.py --cn client.local --out certs/clientThis creates:
certs/client-key.pem(client private key)certs/client-cert.pem(client certificate signed by CA)
# Activate virtual environment first
.venv\Scripts\activate # Windows
# or
source .venv/bin/activate # Linux/Mac
# Start the server
python -m app.serverThe server will:
- Load its certificate and private key
- Connect to MySQL database
- Listen on the configured port (default: 8888)
- Wait for client connections
Expected Server Output:
[INFO] Server starting on localhost:8888
[INFO] Loading server certificate from certs/server-cert.pem
[INFO] Database connection established
[INFO] Server listening for connections...
In a separate terminal:
# Activate virtual environment
.venv\Scripts\activate # Windows
# or
source .venv/bin/activate # Linux/Mac
# Start the client
python -m app.clientThe client will:
- Connect to the server
- Exchange certificates (mutual authentication)
- Prompt for registration or login
- Establish secure session
- Allow encrypted chat messaging
Client Input:
Connected to server
Certificate verified: ✓
Choose action: [1] Register [2] Login
> 1
Enter email: user@example.com
Enter username: alice
Enter password: ********
Registration successful!
Server Output:
[INFO] Client connected from 127.0.0.1:54321
[INFO] Client certificate verified
[INFO] Registration request received
[INFO] User 'alice' registered successfully
Client Input:
Connected to server
Certificate verified: ✓
Choose action: [1] Register [2] Login
> 2
Enter email: user@example.com
Enter password: ********
Login successful!
Session key established
Server Output:
[INFO] Client connected from 127.0.0.1:54322
[INFO] Client certificate verified
[INFO] Login request received
[INFO] User 'alice' authenticated successfully
[INFO] Session key established
Client Input:
> Hello, this is a secure message!
[Sent] Message #1 sent and signed
[Received] Server: Message received and verified
Server Output:
[Received] Client: Hello, this is a secure message!
[INFO] Message #1 verified: ✓
> Response message
[Sent] Message #2 sent and signed
Control Plane Messages:
{
"type": "hello",
"client_cert": "-----BEGIN CERTIFICATE-----\n...\n-----END CERTIFICATE-----",
"nonce": "base64_encoded_nonce"
}{
"type": "server_hello",
"server_cert": "-----BEGIN CERTIFICATE-----\n...\n-----END CERTIFICATE-----",
"nonce": "base64_encoded_nonce"
}{
"type": "register",
"email": "user@example.com",
"username": "alice",
"pwd": "base64_encrypted_password",
"salt": "base64_salt"
}{
"type": "login",
"email": "user@example.com",
"pwd": "base64_encrypted_password",
"nonce": "base64_nonce"
}Key Agreement Messages:
{
"type": "dh_client",
"g": 2,
"p": 12345678901234567890,
"A": 98765432109876543210
}{
"type": "dh_server",
"B": 11223344556677889900
}Data Plane Messages:
{
"type": "msg",
"seqno": 1,
"ts": 1699123456789,
"ct": "base64_encrypted_ciphertext",
"sig": "base64_rsa_signature"
}Non-Repudiation Receipt:
{
"type": "receipt",
"peer": "client",
"first_seq": 1,
"last_seq": 10,
"transcript_sha256": "hex_hash_of_transcript",
"sig": "base64_rsa_signature"
}Certificate Validation Errors:
[ERROR] BAD_CERT: Certificate validation failed
[ERROR] Certificate expired
[ERROR] Certificate not signed by trusted CA
[ERROR] Invalid Common Name (CN)
Authentication Errors:
[ERROR] Invalid credentials
[ERROR] User not found
[ERROR] Password mismatch
Message Integrity Errors:
[ERROR] SIG_FAIL: Signature verification failed
[ERROR] Message tampered
[ERROR] REPLAY: Sequence number already used
[ERROR] Stale message (timestamp check failed)
Setup:
- Start Wireshark
- Capture on loopback interface (lo0 on Linux/Mac, or your network adapter)
- Apply filter:
tcp.port == 8888
Expected Results:
- All payloads should be encrypted (base64 encoded ciphertext)
- No plaintext credentials visible
- Certificate exchange visible but encrypted data in messages
Display Filters:
tcp.port == 8888
tcp contains "type"
Test Cases:
- Self-signed certificate (not from CA)
- Expired certificate
- Certificate with wrong Common Name (CN)
- Forged certificate
Expected Output:
[ERROR] BAD_CERT: Certificate not signed by trusted CA
Connection terminated
Test Procedure:
- Capture a message in Wireshark
- Modify a single bit in the ciphertext (
ctfield) - Resend the modified message
Expected Output:
[ERROR] SIG_FAIL: Signature verification failed
[ERROR] Message integrity check failed
Message rejected
Test Procedure:
- Capture a valid message with
seqno: 5 - Resend the same message (same seqno)
Expected Output:
[ERROR] REPLAY: Sequence number 5 already used
Message rejected
Test Procedure:
- Complete a chat session
- Export transcript and SessionReceipt
- Verify transcript hash
- Verify SessionReceipt signature
Expected Output:
[INFO] Session ended
[INFO] Transcript saved to transcripts/session_20231201_120000.txt
[INFO] SessionReceipt generated
[INFO] Transcript hash: abc123def456...
[INFO] Receipt signature verified: ✓
Offline Verification:
python -m app.storage.transcript --verify transcripts/session_20231201_120000.txt-
Do NOT use TLS/SSL or any secure-channel abstraction
(e.g.,ssl, HTTPS, WSS, OpenSSL socket wrappers).
All crypto operations must occur explicitly at the application layer. -
Do NOT commit secrets (certs, private keys, salts,
.envvalues) to Git. -
Do NOT implement cryptographic algorithms from scratch
Use standard libraries (cryptography,PyMySQL, etc.) -
Maintain commit history
At least 10 meaningful commits showing progressive development.
- Certificate exchange and mutual verification
- Registration/Login with encrypted credentials
- Temporary DH key exchange for credential encryption
- Diffie-Hellman key exchange
- Session key derivation:
K = Trunc16(SHA256(big-endian(Ks))) - AES-128 key establishment
- AES-128 encryption with PKCS#7 padding
- SHA-256 hashing:
h = SHA256(seqno || timestamp || ciphertext) - RSA signature:
sig = RSA_SIGN(h) - Replay protection via sequence numbers
- Append-only transcript maintenance
- Transcript hash computation
- Signed SessionReceipt generation
When submitting on Google Classroom (GCR):
- GitHub Repository ZIP - Complete repository with all commits
- MySQL Schema Dump -
schema.sqlwith table structure and sample records - README.md - This file (with your GitHub repo link)
- Report -
22I-0891-Umer-Farooq-Report-A02.pdf(indocs/folder) - Test Report -
22I-0891-Umer-Farooq-TestReport-A02.pdf(indocs/folder)
Note: Reports are available in both PDF format (in docs/ folder) and LaTeX source format (docs/report.tex and docs/test_report.tex). PDFs can be converted to DOCX format if required for submission.
Umer Farooq
Roll No: 22I-0891
Section: CS-7D
Institution: FAST-NUCES
Semester: Fall 2025
Course: Information Security (CS-3002)
Instructor: Urooj Ghani
Contact:
- GitHub: @Umer-Farooq-CS
This project is licensed under the MIT License - see the LICENSE file for details.
Note: This project is for educational purposes only. Do not use in production without proper security audits.