Is this the file they made?
You download a program and next to it is a long string called a checksum. It matches the file. Does that prove the file is the one its makers built?
Not if somebody changed the file and the checksum on the same page. A signature is harder to fake.
By the end of this lesson
Note
I can sign a release and verify the signature before I use it.
Key words
| Word | What it means | Example |
|---|---|---|
| Artifact | a file a build produces, such as a release | |
| Digest | a short fingerprint of a file's bytes | sha256sum |
| Private key | the secret half that makes a signature | |
| Public key | the shared half that checks one | |
| Signature | proof that a key holder approved these exact bytes | |
| Allowed signers | the list of keys you trust, by name |
The cells students fill in are left blank in this preview.
Starter
Have a go
Starter
4 questionsThe rule for this lesson
Warning
You will swap a file in a package registry. Do it only in your own lab copy, or on a system whose owner has said yes in writing. Changing files on someone else's system without permission is a crime under the Computer Misuse Act 1990.
A digest proves the bytes match
A digest such as SHA-256 is a fingerprint of a file. Change one byte and the digest changes completely.
A digest only helps if you got it from somewhere the attacker cannot reach. On the same download page, it proves nothing.
Try it: one space changes everything
import hashlib
release = b"the tuck shop, version 1.0"
print(hashlib.sha256(release).hexdigest())
changed = b"the tuck shop, version 1.0 "
print(hashlib.sha256(changed).hexdigest())
# One space was added. Change another character and press Run.
Make a free account to save your work and track your progress.
Clone the shop in the terminal
test -d tuckshop || git clone -q http://student:student@gitea:3000/student/tuckshop.gitShown here, not run. Press Start lab to type it into your own lab’s terminal.
Build the release file
git -C tuckshop fetch -q && git -C tuckshop archive --format=tar.gz --prefix=tuckshop/ -o ../tuckshop-1.0.tar.gz origin/mainShown here, not run. Press Start lab to type it into your own lab’s terminal.
Take its digest
sha256sum tuckshop-1.0.tar.gzShown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsA signature ties the bytes to a key
The release manager keeps a private key secret and shares the public key. Signing makes a signature from the file and the private key.
Anyone can check it with the public key. The lab uses ssh-keygen, the same signing Git uses for tags. The namespace, here file, stops a signature made for one purpose being reused for another.
Make the release key
test -f release_key || ssh-keygen -q -t ed25519 -N "" -C [email protected] -f release_keyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Sign the release
rm -f tuckshop-1.0.tar.gz.sig && ssh-keygen -Y sign -f release_key -n file tuckshop-1.0.tar.gzShown here, not run. Press Start lab to type it into your own lab’s terminal.
Write the list of trusted signers
echo "[email protected] $(cat release_key.pub)" > allowed_signers && cat allowed_signersShown here, not run. Press Start lab to type it into your own lab’s terminal.
Publish the file and its signature
for f in tuckshop-1.0.tar.gz tuckshop-1.0.tar.gz.sig; do curl -s -o /dev/null -w "$f %{http_code}\n" -u student:student --upload-file $f http://gitea:3000/api/packages/student/generic/tuckshop/1.0/$f; doneShown here, not run. Press Start lab to type it into your own lab’s terminal.
Download it as a user would and verify
mkdir -p download && curl -s -o download/tuckshop-1.0.tar.gz http://gitea:3000/api/packages/student/generic/tuckshop/1.0/tuckshop-1.0.tar.gz && curl -s -o download/tuckshop-1.0.tar.gz.sig http://gitea:3000/api/packages/student/generic/tuckshop/1.0/tuckshop-1.0.tar.gz.sig && ssh-keygen -Y verify -f allowed_signers -I [email protected] -n file -s download/tuckshop-1.0.tar.gz.sig < download/tuckshop-1.0.tar.gzShown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsSwap the release in your registry
Now play an attacker who can write to the registry, a stolen password perhaps. Add one file to the release and put it back under the same name.
The registry refuses to overwrite a version, so the attacker deletes it first. Then download and verify exactly as before.
Make a changed copy of the release
mkdir -p evil && tar -xzf tuckshop-1.0.tar.gz -C evil && echo 'print("not from the shop")' > evil/tuckshop/extra.py && tar -czf evil.tar.gz -C evil tuckshopShown here, not run. Press Start lab to type it into your own lab’s terminal.
Replace the file in the registry
curl -s -o /dev/null -w "%{http_code}\n" -u student:student -X DELETE http://gitea:3000/api/packages/student/generic/tuckshop/1.0/tuckshop-1.0.tar.gz && curl -s -o /dev/null -w "%{http_code}\n" -u student:student --upload-file evil.tar.gz http://gitea:3000/api/packages/student/generic/tuckshop/1.0/tuckshop-1.0.tar.gzShown here, not run. Press Start lab to type it into your own lab’s terminal.
Download and verify again
curl -s -o download/tuckshop-1.0.tar.gz http://gitea:3000/api/packages/student/generic/tuckshop/1.0/tuckshop-1.0.tar.gz && ssh-keygen -Y verify -f allowed_signers -I [email protected] -n file -s download/tuckshop-1.0.tar.gz.sig < download/tuckshop-1.0.tar.gzShown here, not run. Press Start lab to type it into your own lab’s terminal.
A common mistake
Warning
Many people think a valid signature means the software is safe. It only proves which key signed these bytes. A stolen key signs malware just as well. Keep signing keys off laptops, in a hardware key or a key service, and check the signer is one you expect.
Verify every time, and guard the key
The fix is to verify before every install or deploy and stop when it fails. Watch the registry's log for deleted versions.
Sigstore's cosign goes further: it signs with a short-lived certificate tied to a login and records every signature in a public log, Rekor. The lab has no internet, so it uses a key pair. Git tags are signed the same way.
Sign a Git tag with the same key
git -C tuckshop -c gpg.format=ssh -c user.signingkey=$HOME/release_key.pub -c user.name="Ana Lab" -c [email protected] tag -s v1.0 -m "Release 1.0" origin/main && git -C tuckshop -c gpg.ssh.allowedSignersFile=$HOME/allowed_signers verify-tag v1.0Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Assignment: check a release's digests
A release ships a checksum file in the format sha256sum prints: a digest, two spaces and a file name on each line. Write three functions.
parse_sums(text) returns a dictionary from each file name to its digest in lower case, skipping blank lines. check(expected, actual) takes two such dictionaries, what the signed checksum file says and what you measured, and returns a dictionary from every name in either to "ok", "changed", "missing" (expected but not there) or "unexpected" (there but not expected). safe_to_use(results) returns True only when there is at least one result and every result is "ok".
def parse_sums(text):
return {}
def check(expected, actual):
return {}
def safe_to_use(results):
return True
Make a free account to save your work and track your progress.
Exit quiz
Have a go