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

Free lesson

Signing a release and checking it

After this lesson: I can sign a release and verify the signature before I use it.

55 min lesson12 commandsDevSecOps lab: build a security pipeline and a gate

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

Already have an account? Sign in

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

WordWhat it meansExample
Artifacta file a build produces, such as a release
Digesta short fingerprint of a file's bytessha256sum
Private keythe secret half that makes a signature
Public keythe shared half that checks one
Signatureproof that a key holder approved these exact bytes
Allowed signersthe list of keys you trust, by name

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

Starter

Have a go

Starter

4 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

The 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

Code: python
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

Command
test -d tuckshop || git clone -q http://student:student@gitea:3000/student/tuckshop.git

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

Build the release file

Command
git -C tuckshop fetch -q && git -C tuckshop archive --format=tar.gz --prefix=tuckshop/ -o ../tuckshop-1.0.tar.gz origin/main

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

Take its digest

Command
sha256sum tuckshop-1.0.tar.gz

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 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

Command
test -f release_key || ssh-keygen -q -t ed25519 -N "" -C [email protected] -f release_key

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

Sign the release

Command
rm -f tuckshop-1.0.tar.gz.sig && ssh-keygen -Y sign -f release_key -n file tuckshop-1.0.tar.gz

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

Write the list of trusted signers

Command
echo "[email protected] $(cat release_key.pub)" > allowed_signers && cat allowed_signers

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

Publish the file and its signature

Command
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; done

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

Download it as a user would and verify

Command
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.gz

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

Swap 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

Command
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 tuckshop

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

Replace the file in the registry

Command
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.gz

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

Download and verify again

Command
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.gz

Shown 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

Command
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.0

Shown 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".

Code: python
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

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.