blob: 6f4e0934f763a23a48351b974969eb8ee42588e4 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
#!/usr/bin/env bash
#
# Helper script to properly download an OpenPGP key from a remote location.
# Inspired by https://gitlab.torproject.org/tpo/onion-services/onionprobe/-/blob/main/scripts/get-tor-debian-key
#
# Parameters
BASENAME="`basename $0`"
URL="$1"
FINGERPRINT="$2"
DEST="$3"
CANDIDATE="`mktemp`"
# Ensure the candidate file is remove upon exit
trap "rm -rf $CANDIDATE" INT TERM EXIT
# Check syntax
if [ -z "$3" ]; then
echo "usage: $BASENAME <url> <fingerprint> <dest-file>"
exit 1
fi
# Download the OpenPGP directly from a remote location.
#
# Advantage: handles any upstram updates in the key, like renewed expiration.
wget -qO- "$URL" | gpg --dearmor | tee "$CANDIDATE" > /dev/null || exit 1
# Get the actual fingerprint after downloading, since we cannot assume that the
# remote file has the correct fingerprint.
#
# Then we need to check the actual key fingerprint.
KEY_FPR="`cat $CANDIDATE | gpg --with-fingerprint --with-colons 2> /dev/null | grep '^fpr' | cut -d : -f 10 | head -1`"
# Compare the actual fingerprint with the one we're looking for
if [ "$KEY_FPR" == "$FINGERPRINT" ]; then
echo "$BASENAME: downloaded $URL key matches the expected fingerprint $FINGERPRINT"
if [ ! -z "$DEST" ]; then
echo "$BASENAME: saving key $FINGERPRINT on $DEST..."
touch "$DEST" || exit 1
chmod 644 "$DEST" || exit 1
cat "$CANDIDATE" > "$DEST" || exit 1
else
echo "$BASENAME: saving key $FINGERPRINT on $FINGERPRINT.asc..."
cp "$CANDIDATE" "$FINGERPRINT.asc"
fi
else
echo "$BASENAME: error: downloaded $URL key does not matche the expected fingerprint $FINGERPRINT (got $KEY_FPR instead)"
exit 1
fi
|