#!/bin/sh # gsign / libglacier_verify's Ed25519 (REPO_DESIGN.txt section 2): # - keypairs, signing and verifying round-trip; a changed message, a # changed signature and another key's signature are each refused, # with their own message; # - interop with OpenBSD signify (Alpine's `signify`), both ways; # - an encrypted secret key is refused as such; # - malformed key and signature files are refused (comment line, # extra or missing lines, non-canonical base64, wrong algorithm, # wrong length, not a regular file), and so is a message over the # size limit; # - a new secret key is 0600 whatever the umask; existing keys are # never overwritten; a signature is never written through a symlink. # Runs in a throwaway alpine container: # # docker run --rm -v ~/Projects/libglacier-ng:/libsrc:ro \ # -v ~/Projects/glacier-ng/tests/sign_alpine.sh:/t.sh:ro alpine:latest sh /t.sh set -u apk add --no-cache build-base libarchive-dev libconfig-dev linux-headers signify luajit >/dev/null 2>&1 \ || { echo "apk failed"; exit 1; } cp -a /libsrc /build && cd /build && make clean >/dev/null && make >/dev/null 2>&1 \ || { echo "build failed"; exit 1; } G=/build/build/bin/gsign FAILS=0 check() { d=$1; shift; if "$@"; then echo "PASS: $d"; else echo "FAIL: $d"; FAILS=$((FAILS+1)); fi; } nocolor() { sed 's/\x1b\[[0-9;]*m//g'; } R() { OUT=$( ("$@"; echo "rc=$?") 2>&1 | nocolor) RC=$(echo "$OUT" | sed -n 's/^rc=//p' | tail -1) echo "$OUT" | sed '/^rc=/d; s/^/ /' } has() { echo "$OUT" | grep -qF -- "$1"; } rc() { [ "$RC" = "$1" ]; } mode() { stat -c %a "$1"; } hr() { echo; echo "== $*"; } W=/w; mkdir -p $W && cd $W hr "keypairs" R $G -G -p a.pub -s a.sec -c alice check "gsign -G succeeds" rc 0 check "the secret key is 0600" [ "$(mode a.sec)" = 600 ] check "the public key is 0644" [ "$(mode a.pub)" = 644 ] check "comments are signify's" sh -c 'head -1 a.pub | grep -qx "untrusted comment: alice public key" && head -1 a.sec | grep -qx "untrusted comment: alice secret key"' (umask 000; $G -G -p u.pub -s u.sec >/dev/null 2>&1) check "the secret key is 0600 under umask 000" [ "$(mode u.sec)" = 600 ] (umask 077; $G -G -p v.pub -s v.sec >/dev/null 2>&1) check "the public key is 0644 under umask 077 (users read system keys)" [ "$(mode v.pub)" = 644 ] cp a.sec a.sec.bak R $G -G -p new.pub -s a.sec check "an existing secret key is never overwritten" rc 1 check "...saying which" has "Secret key a.sec already exists." check "...and it is unchanged" cmp -s a.sec a.sec.bak check "...and no public key is left behind" [ ! -e new.pub ] R $G -G -p a.pub -s new.sec check "an existing public key is refused too" has "Public key a.pub already exists." check "...leaving no secret key behind" [ ! -e new.sec ] # The library refuses on its own, not only gsign's pre-check. # `luajit -e CODE ARGS` would run ARGS' first word as a script: use a file. cat > /gen.lua <<'LUA' local ffi = require("ffi") ffi.cdef[[int gl_sign_generate(const char *, const char *, const char *);]] local v = ffi.load("/build/build/lib/shared/libglacier_verify.so") io.write(tostring(v.gl_sign_generate(arg[1], arg[2], "lib")), "\n") LUA lib() { luajit /gen.lua "$@"; } check "gl_sign_generate refuses an existing secret key (EXISTS)" [ "$(lib new2.pub a.sec)" = 8 ] check "...leaving it unchanged" cmp -s a.sec a.sec.bak check "...and writing no public key" [ ! -e new2.pub ] ln -s /w/elsewhere.sec link.sec check "gl_sign_generate refuses a symlink as the secret key" [ "$(lib new3.pub link.sec)" = 8 ] check "...creating nothing through it" [ ! -e /w/elsewhere.sec ] R $G -G -p b.pub -s b.sec -c "$(printf 'two\nlines')" check "a multi-line comment is refused" rc 1 check "...and creates nothing" [ ! -e b.pub ] && [ ! -e b.sec ] $G -G -p b.pub -s b.sec -c bob >/dev/null hr "sign and verify" echo "hello" > m R $G -S -s a.sec -m m check "gsign -S succeeds" rc 0 check "the signature says which key verifies it" sh -c 'head -1 m.sig | grep -qx "untrusted comment: verify with a.pub"' R $G -V -p a.pub -m m check "gsign -V accepts it" rc 0 check "...saying so" has "Signature verified: m." cp m m2; echo "hello!" > m2; cp m.sig m2.sig R $G -V -p a.pub -m m2 check "a changed message is refused" rc 1 check "...as not verifying" has "Signature does not verify: m2." R $G -V -p b.pub -m m check "another key's signature is refused" rc 1 check "...as a different key" has "m was signed by a different key than b.pub." # Flip one bit of the signature itself (byte 20 of the 74 decoded). sed -n 2p m.sig | base64 -d > raw printf "$(printf '\\%03o' $(( $(od -An -tu1 -j20 -N1 raw) ^ 1 )))" | dd of=raw bs=1 seek=20 conv=notrunc 2>/dev/null { head -1 m.sig; base64 -w0 raw; echo; } > m3.sig; cp m m3 R $G -V -p a.pub -m m3 check "a changed signature is refused" rc 1 check "...as not verifying" has "Signature does not verify: m3." R $G -V -p a.pub -m m -x m.sig check "-x names the signature file" rc 0 R $G -S -s a.sec -m m -x other.sig check "-S -x writes there" [ -s other.sig ] hr "interop with signify" R signify -V -p a.pub -m m -x m.sig check "signify verifies gsign's signature" rc 0 signify -G -n -p s.pub -s s.sec -c sig >/dev/null echo "from signify" > sm signify -S -s s.sec -m sm -x sm.sig R $G -V -p s.pub -m sm -x sm.sig check "gsign verifies signify's signature" rc 0 R $G -S -s s.sec -m m -x ms.sig check "gsign signs with signify's unencrypted key" rc 0 R signify -V -p s.pub -m m -x ms.sig check "...and signify verifies that" rc 0 echo "x" > sm2; cp sm.sig sm2.sig R $G -V -p s.pub -m sm2 check "signify's signature over another message is refused" has "Signature does not verify: sm2." hr "encrypted secret keys" # rounds (decoded bytes 4..7) = 42, as a passphrase-protected key has. sed -n 2p s.sec | base64 -d > raw printf '\000\000\000\052' | dd of=raw bs=1 seek=4 conv=notrunc 2>/dev/null { head -1 s.sec; base64 -w0 raw; echo; } > enc.sec R $G -S -s enc.sec -m m -x enc.sig check "an encrypted secret key is refused" rc 1 check "...as encrypted" has "Secret key enc.sec is encrypted: sign with signify." check "...writing nothing" [ ! -e enc.sig ] # A corrupted secret key fails its checksum. sed -n 2p a.sec | base64 -d > raw printf 'X' | dd of=raw bs=1 seek=60 conv=notrunc 2>/dev/null { head -1 a.sec; base64 -w0 raw; echo; } > bad.sec R $G -S -s bad.sec -m m -x bad.sig check "a secret key failing its checksum is malformed" has "Secret key bad.sec is malformed." hr "malformed public keys and signatures" L1=$(head -1 a.pub); L2=$(sed -n 2p a.pub) S1=$(head -1 m.sig); S2=$(sed -n 2p m.sig) bad_pub() { # NAME DESCRIPTION: a.pub variant in NAME must be refused R $G -V -p "$1" -m m check "$2" has "Public key $1 is malformed." } bad_sig() { R $G -V -p a.pub -m m -x "$1" check "$2" has "Signature $1 is malformed." } printf '%s\n' "$L2" > p1; bad_pub p1 "no comment line" printf 'comment: x\n%s\n' "$L2" > p2; bad_pub p2 "a comment line without 'untrusted comment: '" printf '%s\n%s' "$L1" "$L2" > p3; bad_pub p3 "no final newline" printf '%s\n%sx' "$L1" "$L2" > p3b; bad_pub p3b "a final character other than a newline" printf '%s\n%s\n\n' "$L1" "$L2" > p4; bad_pub p4 "an extra empty line" printf '%s\n%s\nmore\n' "$L1" "$L2" > p5; bad_pub p5 "trailing data" printf '%s\n%s\n' "$L1" "${L2%?}" > p6; bad_pub p6 "base64 one character short" printf '%s\n%sA\n' "$L1" "$L2" > p7; bad_pub p7 "base64 one character long" printf '%s\n%s\n' "$L1" "$(echo "$L2" | sed 's/^./*/')" > p8; bad_pub p8 "a character outside base64" printf '%s\n%s\n' "$L1" "$S2" > p9; bad_pub p9 "a signature given as a public key" sed -n 2p a.pub | base64 -d > raw; printf 'XX' | dd of=raw bs=1 conv=notrunc 2>/dev/null printf '%s\n%s\n' "$L1" "$(base64 -w0 raw)" > p10; bad_pub p10 "an algorithm other than Ed" printf '%s\n%s\n' "$L1" "$(echo "$L2" | sed 's/^\(.\{10\}\)/\1 /' | cut -c1-56)" > p11; bad_pub p11 "a space inside the base64" printf '' > p12; bad_pub p12 "an empty file" printf 'untrusted comment: %s\n%s\n' "$(head -c 1100 /dev/zero | tr '\0' x)" "$L2" > p13; bad_pub p13 "a comment over 1024 characters" R $G -V -p /w -m m check "a directory as the public key cannot be read" has "Public key /w cannot be read." # Non-canonical base64: the signature's last data character (before # its one '=') has two unused bits; set one. A=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/ last=$(echo "$S2" | cut -c99) i=$(awk -v a="$A" -v c="$last" 'BEGIN { print index(a, c) - 1 }') nc=$(echo "$A" | cut -c$(( (i | 1) + 1 ))) printf '%s\n%s%s=\n' "$S1" "$(echo "$S2" | cut -c1-98)" "$nc" > s1.sig bad_sig s1.sig "non-canonical base64 (unused bits set)" printf '%s\n%s\n' "$S1" "$(echo "$S2" | tr -d =)" > s2.sig; bad_sig s2.sig "missing padding" printf '%s\n%s\n' "$S1" "$L2" > s3.sig; bad_sig s3.sig "a public key given as a signature" R $G -V -p a.pub -m m -x missing.sig check "a missing signature cannot be read" has "Signature missing.sig cannot be read." hr "limits and symlinks" truncate -s 67108865 big R $G -S -s a.sec -m big check "a message over 64 MiB is refused" has "File big is too large." ln -s /w/target.txt link.sig R $G -S -s a.sec -m m -x link.sig check "a signature is never written through a symlink" rc 1 check "...the symlink's target is not created" [ ! -e /w/target.txt ] R $G -V -p a.pub -m missing -x m.sig check "a missing message cannot be read" has "File missing cannot be read." echo echo "FAILURES: $FAILS" [ $FAILS -eq 0 ] && echo "ALL PASSED" exit $FAILS