Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Mutual TLS between services

After this lesson: I can make two services prove who they are to each other with mutual TLS.

70 min lesson12 commandsWireGuard lab: build a VPN tunnel between two sites in your browser

A quick check that you are not a bot, then your own lab opens on the interactive lesson.

Already have an account? Sign in

An address can be borrowed

The grades app now takes calls from 10.30.0.5 alone. But any program on that machine has that address, and an attacker who lands there has it too.

The app should ask the caller to prove which service it is.

The one rule

Warning

Everything in this module happens on the lab machine that is your own. Presenting a forged certificate to a service you do not own needs written permission. In the UK, without it, it can break the Computer Misuse Act 1990.

Quick check: the rule

Have a go

Quick check: the rule

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

By the end of this lesson

Note

I can make two services prove who they are to each other with mutual TLS.

Key words

WordWhat it meansExample
Certificate authoritythe issuer whose signature makes a certificate trusted
Private CAa CA that only your own services trust
Mutual TLSTLS where both sides show a certificate
Client certificatethe certificate the caller shows
Subject Alternative Namethe names and addresses a certificate is forIP:10.30.0.10
Trust storethe CA certificates a program accepts

The cells students fill in are left blank in this preview.

Starter

Have a go

Starter

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Both sides show a certificate

In ordinary TLS only the server shows a certificate. In mutual TLS the caller shows one too, and each side checks the other's against its trust store.

The app then reads the caller's name from the certificate and decides, as the proxy decided on a token.

The certificates you will make

FileSigned byForLasts
ca.crtitselfsigning the others30 days
grades.crtthe college CADNS:grades, IP:10.30.0.107 days
proxy.crtthe college CADNS:proxy, IP:10.30.0.57 days
reports.crtthe college CAa service grades does not list7 days
rogue.crtitselfa fake "proxy"7 days

The cells students fill in are left blank in this preview.

Try it: the three checks

Code: python
cert = {"subject": "reports", "issuer": "College internal CA", "days_left": 6}
trusted = ["College internal CA"]
callers = ["proxy"]
if cert["issuer"] not in trusted:
    print("handshake refused: unknown CA")
elif cert["subject"] not in callers:
    print("403:", cert["subject"], "may not call grades")
else:
    print("200: welcome,", cert["subject"])
# Change the subject to "proxy", then the issuer to "Home CA". What does each print?

Make a free account to save your work and track your progress.

Check

Have a go

Check

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Make sure the campus is running

Command
cd ~/zt && zt up; zt start db db python3 db.py && zt start grades grades python3 app.py && zt start proxy proxy python3 proxy.py --posture && python3 idp.py issue amy

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Make the college CA

Command
cd ~/zt && openssl req -x509 -newkey ec -pkeyopt ec_paramgen_curve:P-256 -nodes -keyout ca.key -out ca.crt -days 30 -subj "/CN=College internal CA" -addext "keyUsage=critical,keyCertSign,cRLSign" && chmod 600 ca.key && openssl x509 -in ca.crt -noout -subject -ext basicConstraints,keyUsage

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Issue the certificate for grades

Command
cd ~/zt && openssl req -newkey ec -pkeyopt ec_paramgen_curve:P-256 -nodes -keyout grades.key -out grades.csr -subj "/CN=grades" -addext "subjectAltName=DNS:grades,IP:10.30.0.10" && openssl x509 -req -CA ca.crt -CAkey ca.key -days 7 -copy_extensions copy -in grades.csr -out grades.crt

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Issue the certificates for proxy and reports

Command
cd ~/zt && for n in proxy reports; do openssl req -newkey ec -pkeyopt ec_paramgen_curve:P-256 -nodes -keyout $n.key -out $n.csr -subj "/CN=$n" -addext "subjectAltName=DNS:$n,IP:10.30.0.5" && openssl x509 -req -CA ca.crt -CAkey ca.key -days 7 -copy_extensions copy -in $n.csr -out $n.crt; done

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Make a rogue proxy certificate

Command
cd ~/zt && openssl req -x509 -newkey ec -pkeyopt ec_paramgen_curve:P-256 -nodes -keyout rogue.key -out rogue.crt -days 7 -subj "/CN=proxy" -addext "keyUsage=critical,keyCertSign,digitalSignature" && openssl verify -CAfile ca.crt proxy.crt rogue.crt

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Read the grades certificate

Command
cd ~/zt && openssl x509 -in grades.crt -noout -subject -issuer -ext subjectAltName

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Move grades to mutual TLS

tlsapp.py serves the same app on port 8443. It asks every caller for a certificate signed by ca.crt, and app.py lets only the name proxy in.

Change the wall too: grades now takes 8443 from the proxy, and 8080 closes.

Write the TLS server for grades

Command
mkdir -p ~/zt && printf '%s\n' '"""tlsapp.py: the grades app on 8443 over mutual TLS. A caller needs a certificate from the' 'college CA, and app.py lets in only the names in CALLERS."""' 'import ssl' 'from http.server import ThreadingHTTPServer' 'import app' '' 'app.TLS = True' 'server = ThreadingHTTPServer(("0.0.0.0", 8443), app.App)' 'tls = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)' 'tls.load_cert_chain("grades.crt", "grades.key")' 'tls.load_verify_locations("ca.crt")' 'tls.verify_mode = ssl.CERT_REQUIRED' 'server.socket = tls.wrap_socket(server.socket, server_side=True)' 'server.serve_forever()' > ~/zt/tlsapp.py

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Write the new rules for grades

Command
mkdir -p ~/zt && printf '%s\n' 'table inet zt' 'delete table inet zt' 'table inet zt {' '  chain input {' '    type filter hook input priority 0' '    policy drop' '    iif lo accept' '    ct state established,related accept' '    ip saddr 10.30.0.5 tcp dport 8443 accept' '    counter drop comment "refused"' '  }' '}' > ~/zt/grades.nft

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Load them and start grades with TLS

Command
cd ~/zt && sudo ip netns exec zt-grades nft -f grades.nft && zt stop grades && zt start grades-tls grades python3 tlsapp.py

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Call grades from the proxy's machine four ways

Command
zt proxy python3 get.py https://10.30.0.10:8443/grades ca=ca.crt; zt proxy python3 get.py https://10.30.0.10:8443/grades ca=ca.crt cert=rogue; zt proxy python3 get.py https://10.30.0.10:8443/grades ca=ca.crt cert=reports; zt proxy python3 get.py https://10.30.0.10:8443/grades ca=ca.crt cert=proxy

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Check

Have a go

Check

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

A common mistake

Warning

Many people think any certificate from their own CA should be let in. A shared CA proves the caller is one of yours, not which one. The app must still check the name against the callers it allows.

Send amy through the proxy over mutual TLS

Command
cd ~/zt && zt start proxy proxy python3 proxy.py --posture --mtls && sudo ip netns exec zt-laptop python3 devagent.py report laptop && zt laptop python3 get.py http://10.30.0.5:8000/grades token=amy.tok posture=laptop.posture && tail -n 1 logs/grades-tls.log

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

The fix, and how it is seen

Keep certificates short-lived and renew them by machine, as the certificate automation module does. Keep the CA key off the services, readable by its owner alone.

Watch two things: handshakes refused at grades, and any certificate close to its end date.

Ask which certificates end within ten days

Command
cd ~/zt && for c in ca grades proxy; do openssl x509 -in $c.crt -noout -checkend 864000 >/dev/null && echo "$c: fine" || echo "$c: ends within 10 days"; done

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Assignment: decide like a TLS service

Decide whether a caller may in, as the grades app does.

may_call(cert, trusted, callers, now) returns (True, "ok") or (False, reason), checking in order: the issuer is trusted ("unknown ca"), not_before <= now <= not_after ("not valid now"), the subject is a listed caller ("caller not allowed"). expiring(certs, now, days) returns the sorted subjects whose not_after falls within days days of now.

Code: python
def may_call(cert, trusted, callers, now):
    # cert: {"subject": "proxy", "issuer": "College internal CA", "not_before": 100, "not_after": 900}
    # Return (True, "ok") or (False, reason), checking in order:
    # issuer in trusted ("unknown ca"), not_before <= now <= not_after ("not valid now"),
    # subject in callers ("caller not allowed").
    return True, "ok"


def expiring(certs, now, days):
    # The sorted subjects whose not_after comes within days * 86400 seconds of now.
    return []

Make a free account to save your work and track your progress.

Exit quiz

Have a go

Exit quiz

5 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.