Debian Codegraph @

debian/tests/verify-openpgp

raw
1 #!/bin/bash
2
3 # This script aims to run gpg in a number of standard ways to try to
4 # ensure that by default it only generates OpenPGP-compatible
5 # artifacts.
6
7 # Author: Daniel Kahn Gillmor <[email protected]>
8 # License: GPL-3+
9
10
11 if [ -n "$AUTOPKGTEST_ARTIFACTS" ]; then
12 WORKDIR="$AUTOPKGTEST_ARTIFACTS/workdir"
13 mkdir -p "$WORKDIR" || exit 1
14 else
15 WORKDIR=$(mktemp -d)
16 cleanup() {
17 rm -rf "$WORKDIR"
18 }
19 trap cleanup EXIT
20 fi
21
22 errors=''
23
24 add_error() {
25 printf "FAILURE: %s\n" "$@" >&2
26 errors="$errors$*
27 "
28 }
29
30 GPG() {
31 # printf "GPG: %s\n" "$@"
32 gpg --no-tty --pinentry-mode loopback --quiet --passphrase '' --batch --fixed-list-mode "$@"
33 }
34
35 mkdir "$WORKDIR/certs"
36
37 # generate keys in a dedicated homedir:
38 genkey() {
39 local profile="$1"
40 local expected="$2"
41 local GNUPGHOME="$WORKDIR/$profile"
42 export GNUPGHOME
43 mkdir -p -m 0700 "$GNUPGHOME"
44 if [[ "$profile" =~ default ]]; then
45 algo="$profile"
46 # this is a dummy placeholder:
47 extra_arg="--fixed-list-mode"
48 else
49 algo="default"
50 extra_arg="--default-new-key-algo=$profile"
51 fi
52 if GPG "$extra_arg" --quick-gen-key "$profile" "$algo"; then
53 if [ "$expected" = "fail" ]; then
54 add_error "Should have failed to generate key $1"
55 return
56 fi
57 fprs=$(GPG --list-keys | awk -F: '/^fpr:/{print $10 }')
58 for fpr in $fprs; do
59 fprlen=$(printf "%s" "$fpr" | wc -c)
60 if [ "$fprlen" -ne 40 ]; then
61 add_error "Generated fingerprint ($fpr) should have had 40 chars, got $fprlen"
62 fi
63 done
64 mkdir -p "$(dirname "$WORKDIR/certs/$profile.cert")"
65 GPG --export > "$WORKDIR/certs/$profile.cert"
66 else
67 if [ "$expected" != "fail" ]; then
68 add_error "Should not have failed when generating $1"
69 fi
70 fi
71 }
72
73
74 # this is really gross and unprincipled. We treat the output of
75 # --list-packets as a machine-readable format, which upstream always
76 # says we should not do. Alas, without better inspection tooling
77 # (which probably means an external dependency), this is the best i
78 # can do for now.
79 test_generated_key() {
80 local profile="$1"
81 local GNUPGHOME="$WORKDIR/$profile"
82 export GNUPGHOME
83 local dump="$WORKDIR/dumps/$profile.dump"
84 mkdir -p "$(dirname "$dump")"
85 GPG --list-packets > "$dump" < "$WORKDIR/certs/$profile.cert"
86 # FIXME Verify: that no advertising preferences for the
87 # LibrePGP bits are set on the new certs
88 # no 'version 4'
89 # known hashed subpackets:
90 # 2 (sig created)
91 # 9 (key expires)
92 # 11 (pref-sym-algos)
93 # 21 (pref-hash-algos)
94 # 22 (pref-zip-algos)
95 # 23 (keyserver preferences) -- accept only 80 (no-modify)
96 # 27 (key flags) -- accept only 03 (SC) and 0C (E)
97 # 30 (features) -- accept only 01 (v1 SEIPD)
98 # 33 (issuer fpr) -- look for v4
99
100 # Outright reject:
101 # 34 (pref-aead-algos)
102 local unknown_subpacket_types="$( < "$dump" awk '/hashed subpkt/ { print $3 }' | sort -n | uniq | egrep -v '^(2|9|11|21|22|23|27|30|33)$' )"
103 for p in $unknown_subpacket_types; do
104 add_error "Unknown Hashed Subpacket type $p emitted by profile $profile"
105 done
106 local bad_key_flags="$( < "$dump" awk '/hashed subpkt 27/ { print $8 }' | sort | uniq | egrep -v '^(03\)|0C\))$' )"
107 for k in $bad_key_flags; do
108 add_error "Unexpected key flags settings $k by profile $profile"
109 done
110 local bad_features="$( < "$dump" awk '/hashed subpkt 30/ { print $7 }' | sort | uniq | egrep -v '^01\)$' )"
111 for f in $bad_features; do
112 add_error "Unexpected feature flags $f by profile $profile"
113 done
114 local bad_fpr_versions="$(< "$dump" awk '/hashed subpkt 33/ { print $8 }' | sort | uniq | egrep -v '^v4$' )"
115 for f in $bad_fpr_versions; do
116 add_error "Unexpected issuer fingerprint $f by profile $profile"
117 done
118 local bad_versions="$(< "$dump" awk '/^[[:space:]]*version/ { print $2 }' | sort | uniq | egrep -v '^4,$')"
119 for f in $bad_versions; do
120 add_error "unexpected version $f in profile $profile"
121 done
122 }
123
124 librepgp() {
125 local profile="$1"
126 local GNUPGHOME="$WORKDIR/librepgp-$profile"
127 export GNUPGHOME
128 mkdir -p -m 0700 "$GNUPGHOME"
129 if [[ "$profile" =~ default ]]; then
130 algo="$profile"
131 # this is a dummy placeholder:
132 extra_arg="--fixed-list-mode"
133 else
134 algo="default"
135 extra_arg="--default-new-key-algo=$profile"
136 fi
137 if ! GPG --compliance=gnupg "$extra_arg" --quick-gen-key "librepgp-$profile" "$algo"; then
138 add_error "Failed to generate --compliance=gnupg $profile"
139 return
140 fi
141 mkdir -p "$(dirname "$WORKDIR/certs/librepgp-$profile.cert")"
142 GPG --export "$profile" > "$WORKDIR/certs/librepgp-$profile.cert"
143 }
144
145 # try encrypting to keys with LibrePGP settings set.
146 test_encrypt() {
147 local profile="$1"
148 local GNUPGHOME="$WORKDIR/default"
149 export GNUPGHOME
150 GPG --import < "$WORKDIR/certs/librepgp-$profile.cert"
151
152 # --- OpenPGP encryption output
153 # +++ LibrePGP encryption output
154 # @@ -2,7 +2,6 @@
155 # :pubkey enc packet: version 3, algo 18, keyid XXXX
156 # data: [263 bits]
157 # data: [392 bits]
158 # -# off=96 ctb=d2 tag=18 hlen=2 plen=64 new-ctb
159 # -:encrypted data packet:
160 # - length: 64
161 # - mdc_method: 2
162 # +# off=96 ctb=d4 tag=20 hlen=2 plen=74 new-ctb
163 # +:aead encrypted packet: cipher=9 aead=2 cb=16
164 # + length: 74
165
166
167 # diff -u <(echo test | GPG --trust-model=always -r "librepgp-$profile" --encrypt | GPG --list-packets) \
168 # <(echo test | GPG --trust-model=always --compliance=gnupg -r "librepgp-$profile" --encrypt | GPG --list-packets)
169 if echo test | GPG --trust-model=always -r "librepgp-$profile" --encrypt | \
170 GPG --list-packets 2>/dev/null | grep ':aead encrypted packet:'; then
171 add_error "generated AEAD packets when encrypting to librepgp-$profile"
172 fi
173 }
174
175 test_symmetric_encryption() {
176 mkdir -m 0700 -p "$WORKDIR/symmetric"
177 local GNUPGHOME="$WORKDIR/symmetric"
178 export GNUPGHOME
179 if echo test | GPG --passphrase abc123 "$@" --symmetric | \
180 GPG --list-packets 2>/dev/null | grep ':aead encrypted packet:'; then
181 add_error "generated AEAD packets from ($* --symmetric)"
182 fi
183 }
184
185
186
187 for x in default future-default rsa3072+rsa3072 ed25519+cv25519; do
188 genkey "$x" succeed
189 test_generated_key "$x"
190 done
191
192 for x in ed448 ed25519/v5+cv25519/v5 rsa/v5+rsa/v5; do
193 genkey "$x" fail
194 done
195
196 for x in default future-default ed25519/v5+cv25519/v5 ed448+cv448; do
197 librepgp "$x"
198 # uncomment the lines below to see non-OpenPGP artifacts produces with --compliance=gnupg
199 # test_generated_key "librepgp-$x"
200 test_encrypt "$x"
201 done
202
203 test_symmetric_encryption
204 test_symmetric_encryption --compliance=gnupg
205
206 if [ -n "$errors" ]; then
207 printf "=== Errors ====\n%s" "$errors"
208 exit 1
209 fi
210 printf "Success!\n"