Lab 5: Public-Key Infrastructure (PKI)

Adapted from the SEED Labs Public-Key Infrastructure (PKI) Lab (Ubuntu 20.04 edition) by Wenliang Du. See Attribution and License.

EnvironmentSEED Ubuntu 20.04 VM (setup) or any Ubuntu VM with Docker + docker compose, openssl, and Firefox. Tasks 4–6 use a browser, so you need a VM with a desktop. This lab runs on both x86-64 and ARM64 (Apple Silicon) — pick the setup that matches your VM (see Lab setup).
Fileslab5.tar.gz for x86-64 VMs, or lab5-arm.tar.gz for ARM64 / Apple Silicon VMs — the SEED Labsetup, repackaged for this course.
SubmissionOne PDF, structured as described in Submitting a Lab Report

Overview

Lab 4 showed how to keep data secret once two parties share a key. This lab answers the question it left open: when your browser receives a public key that claims to belong to your bank, how does it know the key really is the bank’s and not that of someone sitting in the middle?

Public-key cryptography by itself cannot answer that. The practical answer is the Public-Key Infrastructure (PKI): certificate authorities (CAs) vouch for the binding between a name and a public key by signing an X.509 certificate, and clients trust a small set of root CAs unconditionally. HTTPS, and the TLS protocol underneath it, are built on that trust.

In this lab you become a root CA, issue a certificate for your own web server, deploy it in Apache, and then see how PKI stops a man-in-the-middle (MITM) attack — and how completely it fails once a trusted CA’s private key is stolen.

The theme mirrors Lab 4: the cryptography (RSA, SHA-256) is never broken. Every attack in this lab works because of a trust decision — which CA a client believes, and whether the name on the certificate is the name it wanted.

Topics covered:

  • Public-key encryption and the Public-Key Infrastructure
  • Certificate Authorities (CAs), root CAs, and certificate signing requests (CSRs)
  • X.509 certificates and the Subject Alternative Name (SAN) extension
  • Apache, HTTP, and HTTPS
  • Man-in-the-middle attacks, and what a compromised CA makes possible

Background reading

  • Chapter 23 of Computer & Internet Security: A Hands-on Approach, 2nd/3rd edition, by Wenliang Du — https://www.handsonsecurity.net
  • man openssl-req, man openssl-ca, man openssl-x509, man config

Your server name

Wherever this document says www.bank32.com for your server, use your own server name, built from your last name and the current year — for example, a student named Smith uses:

www.smith2026.com

You do not need to own this domain. You map it to the container’s IP address with /etc/hosts, as shown below. Graders look for this name in your screenshots and certificates.


Lab setup: x86-64 or ARM64

SEED ships two versions of this lab’s Labsetup: one built on x86-64 Docker images, and one built on ARM64 images (the image tag ends in -arm). The files are otherwise identical, so every command in this lab is the same on both — only the download differs.

First, find out which architecture your VM is:

$ uname -m
uname -m printsYour VM isDownloadDocker image used
x86_64Intel/AMD (SEED VM on VirtualBox/VMware, department VM)lab5.tar.gzhandsonsecurity/seed-server:apache-php
aarch64ARM64 (UTM / VMware Fusion / Parallels on an Apple Silicon Mac)lab5-arm.tar.gzhandsonsecurity/seed-server:apache-php-arm

Don’t mix them. An x86 image on an ARM VM either refuses to start (exec format error) or crawls under emulation; an ARM image on an x86 VM fails the same way. If the container dies immediately, check uname -m against the tarball you downloaded.

Download and unpack the matching setup inside the VM:

mkdir -p ~/cs487/lab5 && cd ~/cs487/lab5

# x86-64 VM:
wget https://xiaoguang.wang/computer-security-labs/labs/files/lab5.tar.gz
tar xzf lab5.tar.gz

# ARM64 / Apple Silicon VM (instead of the two lines above):
wget https://xiaoguang.wang/computer-security-labs/labs/files/lab5-arm.tar.gz
tar xzf lab5-arm.tar.gz

cd Labsetup && ls

Either tarball unpacks to the same layout:

Labsetup/
├── docker-compose.yml     # one Apache web server container: www-10.9.0.80
├── image_www/             # Dockerfile, Apache site config, index pages
│   └── certs/             # the bank32 example certificate and key, and its CA
└── volumes/               # shared with the container at /volumes

Docker commands

The SEED VM defines these aliases in ~/.bashrc; run them from inside Labsetup:

$ dcbuild        # docker-compose build   -- build the image
$ dcup           # docker-compose up      -- start the container (add -d to detach)
$ dcdown         # docker-compose down    -- stop and remove the container
$ dockps         # docker ps --format "{{.ID}} {{.Names}}"
$ docksh <id>    # docker exec -it <id> /bin/bash  -- shell in a container

For docksh, the first few characters of the container ID are enough.

Not on the SEED VM? (Typical for an ARM VM you installed yourself.) Install Docker by following https://docs.docker.com/engine/install/ubuntu/ (this includes the Compose plugin), add yourself to the docker group (sudo usermod -aG docker $USER, then log out and back in), and add the aliases to ~/.bashrc. With Compose v2 the command is docker compose (a space, not a hyphen):

alias dcbuild='docker compose build'
alias dcup='docker compose up'
alias dcdown='docker compose down'
alias dockps='docker ps --format "{{.ID}} {{.Names}}"'
docksh() { docker exec -it "$1" /bin/bash; }

Starting the container

$ cd ~/cs487/lab5/Labsetup
$ dcbuild
$ dcup -d

The volumes/ folder is mounted at /volumes inside the container. Generate keys and certificates on the VM (Tasks 1–3), then copy what the web server needs into volumes/ (Task 4).

Map the server names to the web server container (10.9.0.80) by adding these lines to /etc/hosts on the VM (the bank32 entry is required for the built-in example):

10.9.0.80       www.bank32.com
10.9.0.80       www.smith2026.com

Firefox ignoring /etc/hosts? Tasks 4–6 redirect hostnames by editing /etc/hosts. Recent Firefox releases may resolve names with DNS over HTTPS, which can bypass that file. If Firefox reaches the real site instead of your container, turn DNS over HTTPS off (Settings → Privacy & Security → DNS over HTTPS → Off) and restart Firefox.


Task 1: Becoming a Certificate Authority (CA)

A Certificate Authority issues certificates: it signs a statement that a particular public key belongs to a particular name. Most certificates are signed by another CA; a root CA’s certificate is self-signed, ships preloaded in operating systems and browsers, and is trusted unconditionally. Rather than pay a commercial CA, you will become a root CA yourself.

Do Tasks 1–3 on the VM in a working directory of your choice, e.g. ~/cs487/lab5/ca/.

The configuration file. The openssl ca, req, and x509 commands read a configuration file, by default /usr/lib/ssl/openssl.cnf. Copy it into your working directory (do not edit the system copy):

$ cp /usr/lib/ssl/openssl.cnf myCA_openssl.cnf

Its [ CA_default ] section says where the CA keeps its state:

[ CA_default ]
dir             = ./demoCA          # Where everything is kept
certs           = $dir/certs        # Where the issued certs are kept
crl_dir         = $dir/crl          # Where the issued crl are kept
database        = $dir/index.txt    # database index file.
#unique_subject = no                # Set to 'no' to allow creation of
                                    # several certs with same subject.
new_certs_dir   = $dir/newcerts     # default place for new certs.
serial          = $dir/serial       # The current serial number

In your copy, uncomment the unique_subject line — you will very likely issue several certificates with the same subject in this lab. Then create the directories and files it expects: demoCA/, demoCA/certs/, demoCA/crl/, demoCA/newcerts/, an empty demoCA/index.txt, and demoCA/serial containing a single number such as 1000.

Create the CA. Generate the CA’s key pair and self-signed certificate. -subj supplies the subject and -passout the password that protects ca.key, so you are not prompted:

$ openssl req -x509 -newkey rsa:4096 -sha256 -days 3650 \
      -keyout ca.key -out ca.crt \
      -subj "/CN=www.modelCA.com/O=Model CA LTD./C=US" \
      -passout pass:dees

ca.key holds the CA’s (encrypted) private key and ca.crt its public-key certificate. You will need the password every time the CA signs something. Decode both:

$ openssl x509 -in ca.crt -text -noout
$ openssl rsa  -in ca.key -text -noout

Deliverable. The commands you ran and the decoded output, annotated to answer:

  1. What part of the certificate shows that it is a CA’s certificate?
  2. What part shows that it is self-signed?
  3. RSA uses a public exponent e, a private exponent d, a modulus n, and two secret primes p and q with n = pq. Identify each of these values in your certificate and key files (truncated hex is fine). Which of them appear in the certificate, and why must the others never appear there?

Task 2: Generating a Certificate Signing Request (CSR)

Your company (www.smith2026.com) now wants a certificate from the CA. It first creates a Certificate Signing Request containing its public key and identity; the CA checks the identity and signs. The command is almost the one from Task 1 — without -x509 it produces a request instead of a self-signed certificate:

$ openssl req -newkey rsa:2048 -sha256 \
      -keyout server.key -out server.csr \
      -subj "/CN=www.smith2026.com/O=Smith2026 Inc./C=US" \
      -passout pass:dees
$ openssl req -in server.csr -text -noout
$ openssl rsa -in server.key -text -noout

Alternative names. Many sites answer to several names (example.com, www.example.com, …), but browsers insist that the name they asked for appears in the certificate. The X.509 Subject Alternative Name (SAN) extension lists every valid name. Add it to the request on the command line:

-addext "subjectAltName = DNS:www.smith2026.com, \
                          DNS:www.smith2026A.com, \
                          DNS:www.smith2026B.com"

The SAN list must repeat the common name; when a SAN is present, clients ignore the CN. Regenerate your CSR with two alternative names in addition to your server name — you will use them in Task 4.

Deliverable. The final openssl req command and the decoded CSR, with the subject and the Subject Alternative Name extension highlighted. In one or two sentences: what in the CSR proves that whoever made it holds the matching private key?


Task 3: Generating a Certificate for Your Server

The CA turns the CSR into a certificate by signing it:

$ openssl ca -config myCA_openssl.cnf -policy policy_anything \
      -md sha256 -days 3650 \
      -in server.csr -out server.crt -batch \
      -cert ca.crt -keyfile ca.key

policy_anything lets the request’s subject differ from the CA’s (the default policy requires some fields to match).

Copying extensions. For safety, openssl ca does not copy extensions from the request into the certificate by default — otherwise a requester could ask for, say, CA powers. To keep your SAN, uncomment this line in myCA_openssl.cnf and sign again:

# Extension copying option: use with caution.
copy_extensions = copy

Then check the result:

$ openssl x509 -in server.crt -text -noout

Deliverable. The signing command and the decoded server.crt, showing the issuer, the subject, and the alternative names. Show what happens to the SAN with and without copy_extensions = copy, and explain why copying extensions is off by default (“use with caution”).


Task 4: Deploying the Certificate in an Apache HTTPS Website

Now put the certificate to work. The container already runs an Apache-based HTTPS site for bank32.com, configured in /etc/apache2/sites-available/bank32_apache_ssl.conf:

<VirtualHost *:443>
    DocumentRoot /var/www/bank32
    ServerName www.bank32.com
    ServerAlias www.bank32A.com
    ServerAlias www.bank32B.com
    DirectoryIndex index.html
    SSLEngine On
    SSLCertificateFile    /certs/bank32.crt     # the server certificate
    SSLCertificateKeyFile /certs/bank32.key     # the server private key
</VirtualHost>

ServerName and ServerAlias are the names this site answers to; DocumentRoot is where its pages live. The SSL module and this site were already enabled when the image was built (a2enmod ssl, a2ensite bank32_apache_ssl).

Apache is not started automatically, because it must ask for the password that unlocks the private key. Get a shell in the container and start it (the bank32 key’s password is dees):

$ dockps
$ docksh <id>                 # the www-10.9.0.80 container
# service apache2 start       # also: stop, restart

Following this example, set up an HTTPS site for your server name:

  1. Copy your server.crt and server.key into Labsetup/volumes/ on the VM; in the container they appear under /volumes.
  2. In the container, create a document root with a page that shows your name, and a new VirtualHost file in /etc/apache2/sites-available/ with your ServerName, your two ServerAlias entries, and your certificate and key.
  3. Enable it with a2ensite <your-file-name> and restart Apache.
  4. Add the alias names to the VM’s /etc/hosts too.

Container changes are temporary. Files you create inside the container disappear when you dcdown. Keep your VirtualHost file and page in /volumes and copy them in, so you can recreate the site quickly.

Browse it. Point Firefox on the VM at https://www.smith2026.com (note https). It will most likely refuse. Work out why, then fix it. The fix involves importing a certificate into Firefox; we deliberately do not say which one. To import, open about:preferences#privacy, scroll to Certificates → View Certificates…, choose the Authorities tab, click Import…, and tick “Trust this CA to identify websites.”

Deliverable. The steps you took and the VirtualHost entry you added. A screenshot of the failed first visit with your explanation of the error, then screenshots of your site loading successfully under your server name and under each of your two alternative names. Which certificate did you import, and why that one and not server.crt?


Task 5: Launching a Man-in-the-Middle Attack

Now see PKI defeat an attacker. In a MITM attack, Mallory sits between Alice and example.com and swaps the server’s public key for her own; if Alice accepts it, Mallory can read and relay everything.

              Mallory's public key            Server's public key
  Alice  <---------------------------> Mallory <-------------------> example.com
               [secret]                              [secret]

Pick a popular target site — a bank or a social network — rather than example.com; the example below uses www.example.com.

  1. Set up the malicious site. Following Task 4, add a VirtualHost to the container’s Apache whose ServerName is the target (www.example.com). You cannot get a real certificate for that name, so reuse your own server’s certificate and key from Task 4. Restart Apache.

  2. Become the man in the middle. Real attackers hijack routing or poison DNS. Simulate the result of a DNS attack by adding this line to the victim’s (the VM’s) /etc/hosts:

    10.9.0.80       www.example.com
    
  3. Browse the target in Firefox and see what happens.

Deliverable. Your VirtualHost entry and a screenshot of what Firefox shows. Explain exactly which check failed and why — the browser trusts your CA from Task 4, so why is that not enough? Does Firefox offer to let the user continue? (If your target site uses HSTS, note the difference.)


Task 6: A MITM Attack with a Compromised CA

Assume now that an attacker has stolen the private key of the root CA you created in Task 1. The attacker can sign a certificate for any name, and every browser that trusts that CA will accept it.

Design and run an experiment showing that, with the stolen CA key, the MITM attack from Task 5 succeeds: the victim browses to the target site, lands on the attacker’s server, and Firefox shows no warning at all. You may reuse the Task 5 setup.

Deliverable. Your experiment design, the commands you used to create the forged certificate, the updated VirtualHost, and a screenshot of Firefox displaying the fake site for the target name with no warning (open the padlock to show who issued the certificate). Then explain, in a short paragraph, why the security of every HTTPS site depends on every root CA your browser trusts.


Cleaning up

This lab changes your VM in ways you must undo:

  • Remove your CA from Firefox: about:preferences#privacy → View Certificates → Authorities → select your CA (e.g. “Model CA LTD.”) → Delete or Distrust. Leaving a CA whose private key is sitting in your home directory trusted in your browser is exactly the vulnerability you exploited in Task 6.
  • Remove every line you added to /etc/hosts on the VM (www.bank32.com, your server names, and the target site).
  • Stop the container: dcdown in Labsetup.
  • If you turned off DNS over HTTPS in Firefox only for this lab, you may turn it back on.

Submission

Submit a single PDF containing, for each task: the commands you ran, screenshots or transcripts of what you observed, and an explanation of why it happened. Your server name (last name + year) must appear in your certificates and screenshots. Listing commands or configuration with no explanation will not receive credit.

Scoring. Tasks 1–6 are all required.

See Submitting a Lab Report for the expected structure.