Skip to content

Commit 53b07b2

Browse files
committed
verify-commits: Move trusted-keys valid sig check into verify-commits itself
Instead of having gpg.sh check against the trusted keys for a valid signature, do it inside of verify-commits itself. This also allows us to use the same trusted-keys throughout the verify-commits.py check rather than it possibly being modified during the clean merge check.
1 parent 75f0e0b commit 53b07b2

File tree

2 files changed

+19
-38
lines changed

2 files changed

+19
-38
lines changed

contrib/verify-commits/gpg.sh

+6-36
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,9 @@
55

66
export LC_ALL=C
77
INPUT=$(cat /dev/stdin)
8-
VALID=false
9-
REVSIG=false
10-
IFS='
11-
'
128
if [ "$BITCOIN_VERIFY_COMMITS_ALLOW_SHA1" = 1 ]; then
13-
GPG_RES="$(printf '%s\n' "$INPUT" | gpg --trust-model always "$@" 2>/dev/null)"
9+
printf '%s\n' "$INPUT" | gpg --trust-model always "$@" 2>/dev/null
10+
exit $?
1411
else
1512
# Note how we've disabled SHA1 with the --weak-digest option, disabling
1613
# signatures - including selfsigs - that use SHA1. While you might think that
@@ -20,46 +17,19 @@ else
2017
# an attacker could construct a pull-req that results in a commit object that
2118
# they've created a collision for. Not the most likely attack, but preventing
2219
# it is pretty easy so we do so as a "belt-and-suspenders" measure.
23-
GPG_RES=""
2420
for LINE in $(gpg --version); do
2521
case "$LINE" in
2622
"gpg (GnuPG) 1.4.1"*|"gpg (GnuPG) 2.0."*)
2723
echo "Please upgrade to at least gpg 2.1.10 to check for weak signatures" > /dev/stderr
28-
GPG_RES="$(printf '%s\n' "$INPUT" | gpg --trust-model always "$@" 2>/dev/null)"
24+
printf '%s\n' "$INPUT" | gpg --trust-model always "$@" 2>/dev/null
25+
exit $?
2926
;;
3027
# We assume if you're running 2.1+, you're probably running 2.1.10+
3128
# gpg will fail otherwise
3229
# We assume if you're running 1.X, it is either 1.4.1X or 1.4.20+
3330
# gpg will fail otherwise
3431
esac
3532
done
36-
[ "$GPG_RES" = "" ] && GPG_RES="$(printf '%s\n' "$INPUT" | gpg --trust-model always --weak-digest sha1 "$@" 2>/dev/null)"
37-
fi
38-
for LINE in $GPG_RES; do
39-
case "$LINE" in
40-
"[GNUPG:] VALIDSIG "*)
41-
while read KEY; do
42-
[ "${LINE#?GNUPG:? VALIDSIG * * * * * * * * * }" = "$KEY" ] && VALID=true
43-
done < ./contrib/verify-commits/trusted-keys
44-
;;
45-
"[GNUPG:] REVKEYSIG "*)
46-
[ "$BITCOIN_VERIFY_COMMITS_ALLOW_REVSIG" != 1 ] && exit 1
47-
REVSIG=true
48-
GOODREVSIG="[GNUPG:] GOODSIG ${LINE#* * *}"
49-
;;
50-
"[GNUPG:] EXPKEYSIG "*)
51-
[ "$BITCOIN_VERIFY_COMMITS_ALLOW_REVSIG" != 1 ] && exit 1
52-
REVSIG=true
53-
GOODREVSIG="[GNUPG:] GOODSIG ${LINE#* * *}"
54-
;;
55-
esac
56-
done
57-
if ! $VALID; then
58-
exit 1
59-
fi
60-
if $VALID && $REVSIG; then
61-
printf '%s\n' "$INPUT" | gpg --trust-model always "$@" 2>/dev/null | grep "^\[GNUPG:\] \(NEWSIG\|SIG_ID\|VALIDSIG\)"
62-
echo "$GOODREVSIG"
63-
else
64-
printf '%s\n' "$INPUT" | gpg --trust-model always "$@" 2>/dev/null
33+
printf '%s\n' "$INPUT" | gpg --trust-model always --weak-digest sha1 "$@" 2>/dev/null
34+
exit $?
6535
fi

contrib/verify-commits/verify-commits.py

+13-2
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ def main():
9292
unclean_merge_allowed = f.read().splitlines()
9393
with open(dirname + "/allow-incorrect-sha512-commits", "r", encoding="utf8") as f:
9494
incorrect_sha512_allowed = f.read().splitlines()
95+
with open(dirname + "/trusted-keys", "r", encoding="utf8") as f:
96+
trusted_keys = f.read().splitlines()
9597

9698
# Set commit and branch and set variables
9799
current_commit = args.commit
@@ -120,10 +122,19 @@ def main():
120122
no_sha1 = False
121123

122124
os.environ['BITCOIN_VERIFY_COMMITS_ALLOW_SHA1'] = "0" if no_sha1 else "1"
123-
os.environ['BITCOIN_VERIFY_COMMITS_ALLOW_REVSIG'] = "1" if current_commit in revsig_allowed else "0"
125+
allow_revsig = current_commit in revsig_allowed
124126

125127
# Check that the commit (and parents) was signed with a trusted key
126-
if subprocess.call([GIT, '-c', 'gpg.program={}/gpg.sh'.format(dirname), 'verify-commit', current_commit], stdout=subprocess.DEVNULL):
128+
valid_sig = False
129+
verify_res = subprocess.run([GIT, '-c', 'gpg.program={}/gpg.sh'.format(dirname), 'verify-commit', "--raw", current_commit], capture_output=True)
130+
for line in verify_res.stderr.decode().splitlines():
131+
if line.startswith("[GNUPG:] VALIDSIG "):
132+
key = line.split(" ")[-1]
133+
valid_sig = key in trusted_keys
134+
elif (line.startswith("[GNUPG:] REVKEYSIG ") or line.startswith("[GNUPG:] EXPKEYSIG ")) and not allow_revsig:
135+
valid_sig = False
136+
break
137+
if not valid_sig:
127138
if prev_commit != "":
128139
print("No parent of {} was signed with a trusted key!".format(prev_commit), file=sys.stderr)
129140
print("Parents are:", file=sys.stderr)

0 commit comments

Comments
 (0)