1 # Copyright 1999-2024 Gentoo Authors
2 # Distributed under the terms of the GNU General Public License v2
6 PYTHON_COMPAT=( python3_{10..12} )
7 inherit edo python-any-r1
9 DESCRIPTION="Gentoo Authority Keys (GLEP 79)"
10 HOMEPAGE="https://www.gentoo.org/downloads/signatures/"
11 if [[ ${PV} == 9999* ]] ; then
14 BDEPEND="net-misc/curl"
16 SRC_URI="https://qa-reports.gentoo.org/output/keys/active-devs-${PV}.gpg -> ${P}-active-devs.gpg"
17 KEYWORDS="~alpha amd64 arm arm64 hppa ~loong ~m68k ~mips ppc ppc64 ~riscv sparc x86"
22 LICENSE="public-domain"
25 RESTRICT="!test? ( test )"
28 $(python_gen_any_dep 'dev-python/python-gnupg[${PYTHON_USEDEP}]')
29 >=sec-keys/openpgp-keys-gentoo-auth-20240703
37 python_has_version "dev-python/python-gnupg[${PYTHON_USEDEP}]"
41 if [[ ${PV} == 9999* ]] ; then
42 curl https://qa-reports.gentoo.org/output/active-devs.gpg -o ${P}-active-devs.gpg || die
49 export GNUPGHOME="${T}"/.gnupg
51 get_gpg_keyring_dir() {
52 if [[ ${PV} == 9999* ]] ; then
62 --homedir "${GNUPGHOME}"
65 # From verify-sig.eclass:
66 # "GPG upstream knows better than to follow the spec, so we can't
67 # override this directory. However, there is a clean fallback
71 mkdir "${GNUPGHOME}" || die
72 chmod 700 "${GNUPGHOME}" || die
74 # Convert the binary keyring into an armored one so we can process it
75 edo gpg "${mygpgargs[@]}" --import "$(get_gpg_keyring_dir)"/${P}-active-devs.gpg
76 edo gpg "${mygpgargs[@]}" --export --armor > "${WORKDIR}"/gentoo-developers.asc
78 # Now strip out the keys which are expired and/or missing a signature
79 # from our L2 developer authority key
80 edo "${EPYTHON}" "${FILESDIR}"/keyring-mangler.py \
81 "${BROOT}"/usr/share/openpgp-keys/gentoo-auth.asc \
82 "${WORKDIR}"/gentoo-developers.asc \
83 "${WORKDIR}"/gentoo-developers-sanitised.asc
87 export GNUPGHOME="${T}"/tests/.gnupg
90 # We don't have --no-autostart here because we need
91 # to let it spawn an agent for the key generation.
93 --homedir "${GNUPGHOME}"
96 # From verify-sig.eclass:
97 # "GPG upstream knows better than to follow the spec, so we can't
98 # override this directory. However, there is a clean fallback
102 # Check each of the keys to verify they're trusted by
103 # the L2 developer key.
104 mkdir -p "${GNUPGHOME}" || die
105 chmod 700 "${GNUPGHOME}" || die
106 cd "${T}"/tests || die
108 # First, grab the L1 key, and mark it as ultimately trusted.
109 edo gpg "${mygpgargs[@]}" --import "${BROOT}"/usr/share/openpgp-keys/gentoo-auth.asc
110 edo gpg "${mygpgargs[@]}" --import-ownertrust "${BROOT}"/usr/share/openpgp-keys/gentoo-auth-ownertrust.txt
112 # Generate a temporary key which isn't signed by anything to check
113 # whether we're detecting unexpected keys.
115 # The test is whether this appears in the sanitised keyring we
116 # produce in src_compile (it should not be in there).
118 # https://www.gnupg.org/documentation/manuals/gnupg/Unattended-GPG-key-generation.html
119 edo gpg "${mygpgargs[@]}" --batch --gen-key <<-EOF
120 %echo Generating temporary key for testing...
124 %pubring ${P}-ebuild-test-key.asc
130 Name-Real: Larry The Cow
131 Name-Email: larry@example.com
133 Handle: ${P}-ebuild-test-key
136 %echo Temporary key generated!
139 # Import the new injected key that shouldn't be signed by anything into a temporary testing keyring
140 edo gpg "${mygpgargs[@]}" --import "${T}"/tests/${P}-ebuild-test-key.asc
142 # Sign a tiny file with the to-be-injected key for testing rejection below
143 echo "Hello world!" > "${T}"/tests/signme || die
144 edo gpg "${mygpgargs[@]}" -u "Larry The Cow <larry@example.com>" --sign "${T}"/tests/signme || die
146 # keyring-mangler will fail with no valid keys so import the sanitised list from src_compile.
147 edo gpg "${mygpgargs[@]}" --import "${WORKDIR}"/gentoo-developers-sanitised.asc
149 edo gpg "${mygpgargs[@]}" --export --armor > "${T}"/tests/tainted-keyring.asc
151 # keyring-mangler.py should now produce a keyring *without* it
152 edo "${EPYTHON}" "${FILESDIR}"/keyring-mangler.py \
153 "${BROOT}"/usr/share/openpgp-keys/gentoo-auth.asc \
154 "${T}"/tests/tainted-keyring.asc \
155 "${T}"/tests/gentoo-developers-sanitised.asc | tee "${T}"/tests/keyring-mangler.log
156 assert "Key mangling in tests failed?"
158 # Check the log to verify the injected key got detected
159 grep -q "Dropping key.*Larry The Cow" "${T}"/tests/keyring-mangler.log || die "Did not remove injected key from test keyring!"
161 # gnupg doesn't have an easy way for us to actually just.. ask
162 # if a key is known via WoT. So, sign a file using the key
163 # we just made, and then try to gpg --verify it, and check exit code.
165 # Let's now double check by seeing if a file signed by the injected key
167 if gpg "${mygpgargs[@]}" --keyring "${T}"/tests/gentoo-developers-sanitised.asc --verify "${T}"/tests/signme.gpg ; then
168 die "'gpg --verify' using injected test key succeeded! This shouldn't happen!"
171 # Bonus lame sanity check
172 edo gpg "${mygpgargs[@]}" --check-trustdb 2>&1 | tee "${T}"/tests/trustdb.log
173 assert "trustdb call failed!"
175 check_trust_levels() {
178 while IFS= read -r line; do
179 # gpg: depth: 0 valid: 1 signed: 2 trust: 0-, 0q, 0n, 0m, 0f, 1u
180 # gpg: depth: 1 valid: 2 signed: 0 trust: 0-, 0q, 0n, 0m, 2f, 0u
181 if [[ ${line} == *depth* ]] ; then
182 depth=$(echo ${line} | grep -Po "depth: [0-9]")
183 trust=$(echo ${line} | grep -Po "trust:.*")
185 trust_uncalculated=$(echo ${trust} | grep -Po "[0-9]-")
186 [[ ${trust_uncalculated} == 0 ]] || ${mode}
188 trust_insufficient=$(echo ${trust} | grep -Po "[0-9]q")
189 [[ ${trust_insufficient} == 0 ]] || ${mode}
191 trust_never=$(echo ${trust} | grep -Po "[0-9]n")
192 [[ ${trust_never} == 0 ]] || ${mode}
194 trust_marginal=$(echo ${trust} | grep -Po "[0-9]m")
195 [[ ${trust_marginal} == 0 ]] || ${mode}
197 trust_full=$(echo ${trust} | grep -Po "[0-9]f")
198 [[ ${trust_full} != 0 ]] || ${mode}
200 trust_ultimate=$(echo ${trust} | grep -Po "[0-9]u")
201 [[ ${trust_ultimate} == 1 ]] || ${mode}
203 echo "${trust_uncalculated}, ${trust_insufficient}"
205 done < "${T}"/tests/trustdb.log
208 # First, check with the bad key still in the test keyring.
209 # This is supposed to fail, so we want it to return 1
210 check_trust_levels "return 1" && die "Trustdb passed when it should have failed!"
212 # Now check without the bad key in the test keyring.
213 # This one should pass.
215 # Drop the bad key first (https://superuser.com/questions/174583/how-to-delete-gpg-secret-keys-by-force-without-fingerprint)
216 keys=$(gpg "${mygpgargs[@]}" --fingerprint --with-colons --batch "Larry The Cow <larry@example.com>" \
218 | sed -n 's/^fpr:::::::::\([[:alnum:]]\+\):/\1/p')
221 for key in ${keys[@]} ; do
222 nonfatal edo gpg "${mygpgargs[@]}" --batch --yes --delete-secret-keys ${key}
225 edo gpg "${mygpgargs[@]}" --batch --yes --delete-keys "Larry The Cow <larry@example.com>"
226 check_trust_levels "return 0" || die "Trustdb failed when it should have passed!"
228 gpgconf --kill gpg-agent || die
232 insinto /usr/share/openpgp-keys
233 newins gentoo-developers-sanitised.asc gentoo-developers.asc
235 # TODO: install an ownertrust file like sec-keys/openpgp-keys-gentoo-auth?