-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathasmap-verify
More file actions
executable file
·228 lines (187 loc) · 5.99 KB
/
Copy pathasmap-verify
File metadata and controls
executable file
·228 lines (187 loc) · 5.99 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#!/usr/bin/env bash
export LC_ALL=C
set -e -o pipefail
################
# Helper: derive year from unix epoch
################
year_from_epoch() {
local epoch="$1"
if date -d "@${epoch}" +%Y >/dev/null 2>&1; then
# GNU date
date -d "@${epoch}" +%Y
elif date -r "${epoch}" +%Y >/dev/null 2>&1; then
# BSD / macOS date
date -r "${epoch}" +%Y
else
echo "ERR: Cannot determine year from epoch='${epoch}'. Is 'date' available?" >&2
exit 1
fi
}
################
# Required non-builtin commands should be invocable
################
check_tools() {
for cmd in "$@"; do
if ! command -v "$cmd" > /dev/null 2>&1; then
echo "ERR: This script requires that '$cmd' is installed and available in your \$PATH"
exit 1
fi
done
}
check_tools cat diff gpg basename date
################
# Usage
################
cmd_usage() {
cat <<EOF
Synopsis:
env [ ASMAP_DATA_REPO=<path/to/asmap-data> ] \
[ EPOCH=<unix_timestamp> ] \
[ SIGNER=<signer> ] \
./asmap-verify
Example verifying all epochs:
env ./asmap-verify
Example verifying a specific epoch:
env EPOCH=1772726379 \
./asmap-verify
Example using a specific sigs repo and signer's attestation as the compare base:
env ASMAP_DATA_REPO=/home/user/asmap-data \
SIGNER=satoshi \
./asmap-verify
Environment variables:
ASMAP_DATA_REPO (optional) Path to the asmap-data repository clone
EPOCH (optional) Only verify this epoch; default: verify all epochs
SIGNER (optional) Use this signer's SHA256SUMS as the compare base;
default: use the first one found
EOF
}
################
# Assume current repo if empty
################
if [ -z "$ASMAP_DATA_REPO" ]; then
ASMAP_DATA_REPO="$PWD"
fi
################
# ASMAP_DATA_REPO should exist as a directory
################
if [ ! -d "$ASMAP_DATA_REPO" ]; then
cat << EOF
ERR: The specified ASMAP_DATA_REPO is not an existent directory:
'$ASMAP_DATA_REPO'
Hint: Please clone the asmap.sigs repository and point to it with the
ASMAP_DATA_REPO environment variable.
EOF
cmd_usage
exit 1
fi
builder_keys_dir="$ASMAP_DATA_REPO/builder-keys"
##############
## Verify ##
##############
# Usage: verify <compare_base> <current_manifest>
verify() {
local compare_base="$1"
local current_manifest="$2"
local signer_name
signer_name="$(basename "$(dirname "$current_manifest")")"
if ! gpg --quiet --batch \
--verify "${current_manifest}.asc" "$current_manifest" 1>&2; then
echo "ERR: Failed to verify GPG signature for signer '${signer_name}'"
echo " Manifest: '${current_manifest}'"
echo " Signature: '${current_manifest}.asc'"
echo ""
echo "Hint: Either the signature is invalid or the public key is not in"
echo " your GPG keyring. You can import it with:"
echo " gpg --import '${builder_keys_dir}/${signer_name}.gpg'"
echo ""
failure=1
elif ! diff --report-identical "$compare_base" "$current_manifest" 1>&2; then
echo "ERR: The SHA256SUMS attestation differs from the compare base:"
echo " base: '${compare_base}'"
echo " current: '${current_manifest}'"
echo ""
failure=1
else
echo "Verified: '${current_manifest}'"
echo ""
fi
}
# Determine which epoch(s) to verify
if [ -n "$EPOCH" ]; then
YEAR="$(year_from_epoch "$EPOCH")"
epoch_dirs=( "$ASMAP_DATA_REPO/attestations/${YEAR}/${EPOCH}" )
if [ ! -d "${epoch_dirs[0]}" ]; then
echo "ERR: No epoch directory found for EPOCH='${EPOCH}':"
echo " '${epoch_dirs[0]}'"
echo ""
exit 1
fi
else
shopt -s nullglob
epoch_dirs=( "$ASMAP_DATA_REPO"/attestations/*/*/ )
shopt -u nullglob
fi
if (( ${#epoch_dirs[@]} == 0 )); then
echo "ERR: No epoch directories found in '${ASMAP_DATA_REPO}'"
echo ""
echo "Hint: Expected attestation directories of the form:"
echo " '${ASMAP_DATA_REPO}/attestations/<year>/<epoch>/'"
echo ""
exit 1
fi
total_verified=0
failure=""
for epoch_dir in "${epoch_dirs[@]}"; do
epoch_name="$(basename "$epoch_dir")"
echo "===================="
echo "Epoch: ${epoch_name}"
echo "===================="
echo ""
shopt -s nullglob
all_manifests=( "${epoch_dir%/}"/*/SHA256SUMS )
shopt -u nullglob
if (( ${#all_manifests[@]} == 0 )); then
echo "WARN: No SHA256SUMS files found in epoch '${epoch_name}', skipping."
echo ""
continue
fi
# Pick compare base
compare_base="${all_manifests[0]}"
if [ -n "$SIGNER" ]; then
signer_manifest="${epoch_dir%/}/${SIGNER}/SHA256SUMS"
if [ -f "$signer_manifest" ]; then
echo "Using ${SIGNER}'s attestation as the compare base"
compare_base="$signer_manifest"
else
echo "Unable to find ${SIGNER}'s attestation for epoch '${epoch_name}', using the first one found"
fi
fi
for current_manifest in "${all_manifests[@]}"; do
if [ ! -e "${current_manifest}.asc" ]; then
signer_name="$(basename "$(dirname "$current_manifest")")"
echo "WARN: No signature file found for signer '${signer_name}' in epoch '${epoch_name}'"
echo " Expected: '${current_manifest}.asc'"
echo ""
failure=1
continue
fi
verify "$compare_base" "$current_manifest"
total_verified=$(( total_verified + 1 ))
done
echo "DONE: Checked ${#all_manifests[@]} attestation(s) for epoch '${epoch_name}'"
echo ""
done
echo "===================="
echo ""
if (( total_verified == 0 )); then
echo "ERR: No attestations could be verified."
echo ""
exit 1
fi
if [ -n "$failure" ]; then
echo "FAIL: One or more verifications failed. See above for details."
echo ""
exit 1
fi
echo "OK: All ${total_verified} attestation(s) verified successfully."
echo ""