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 questionsBy the end of this lesson
Note
I can make two services prove who they are to each other with mutual TLS.
Key words
| Word | What it means | Example |
|---|---|---|
| Certificate authority | the issuer whose signature makes a certificate trusted | |
| Private CA | a CA that only your own services trust | |
| Mutual TLS | TLS where both sides show a certificate | |
| Client certificate | the certificate the caller shows | |
| Subject Alternative Name | the names and addresses a certificate is for | IP:10.30.0.10 |
| Trust store | the CA certificates a program accepts |
The cells students fill in are left blank in this preview.
Starter
Have a go
Starter
3 questionsBoth 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
| File | Signed by | For | Lasts |
|---|---|---|---|
| ca.crt | itself | signing the others | 30 days |
| grades.crt | the college CA | DNS:grades, IP:10.30.0.10 | 7 days |
| proxy.crt | the college CA | DNS:proxy, IP:10.30.0.5 | 7 days |
| reports.crt | the college CA | a service grades does not list | 7 days |
| rogue.crt | itself | a fake "proxy" | 7 days |
The cells students fill in are left blank in this preview.
Try it: the three checks
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 questionsMake sure the campus is running
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 amyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Make the college CA
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,keyUsageShown here, not run. Press Start lab to type it into your own lab’s terminal.
Issue the certificate for grades
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.crtShown here, not run. Press Start lab to type it into your own lab’s terminal.
Issue the certificates for proxy and reports
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; doneShown here, not run. Press Start lab to type it into your own lab’s terminal.
Make a rogue proxy certificate
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.crtShown here, not run. Press Start lab to type it into your own lab’s terminal.
Read the grades certificate
cd ~/zt && openssl x509 -in grades.crt -noout -subject -issuer -ext subjectAltNameShown 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
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.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Write the new rules for grades
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.nftShown here, not run. Press Start lab to type it into your own lab’s terminal.
Load them and start grades with TLS
cd ~/zt && sudo ip netns exec zt-grades nft -f grades.nft && zt stop grades && zt start grades-tls grades python3 tlsapp.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Call grades from the proxy's machine four ways
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=proxyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsA 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
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.logShown 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
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"; doneShown 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.
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