dev-python/agate: Bump to 1.13.0
[gentoo/gentoo.git] / sec-keys / openpgp-keys-gentoo-developers / openpgp-keys-gentoo-developers-20240422.ebuild
blob8be20976acb227578cb03d47d6c04736e1e341d4
1 # Copyright 1999-2024 Gentoo Authors
2 # Distributed under the terms of the GNU General Public License v2
4 EAPI=8
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
12         PROPERTIES="live"
14         BDEPEND="net-misc/curl"
15 else
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"
20 S="${WORKDIR}"
22 LICENSE="public-domain"
23 SLOT="0"
24 IUSE="test"
25 RESTRICT="!test? ( test )"
27 BDEPEND+="
28         $(python_gen_any_dep 'dev-python/python-gnupg[${PYTHON_USEDEP}]')
29         >=sec-keys/openpgp-keys-gentoo-auth-20240703
30         test? (
31                 app-crypt/gnupg
32                 sys-apps/grep[pcre]
33         )
36 python_check_deps() {
37         python_has_version "dev-python/python-gnupg[${PYTHON_USEDEP}]"
40 src_unpack() {
41         if [[ ${PV} == 9999* ]] ; then
42                 curl https://qa-reports.gentoo.org/output/active-devs.gpg -o ${P}-active-devs.gpg || die
43         else
44                 default
45         fi
48 src_compile() {
49         export GNUPGHOME="${T}"/.gnupg
51         get_gpg_keyring_dir() {
52                 if [[ ${PV} == 9999* ]] ; then
53                         echo "${WORKDIR}"
54                 else
55                         echo "${DISTDIR}"
56                 fi
57         }
59         local mygpgargs=(
60                 --no-autostart
61                 --no-default-keyring
62                 --homedir "${GNUPGHOME}"
63         )
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
68         # to GNUPGHOME."
69         addpredict /run/user
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
86 src_test() {
87         export GNUPGHOME="${T}"/tests/.gnupg
89         local mygpgargs=(
90                 # We don't have --no-autostart here because we need
91                 # to let it spawn an agent for the key generation.
92                 --no-default-keyring
93                 --homedir "${GNUPGHOME}"
94         )
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
99         # to GNUPGHOME."
100         addpredict /run/user
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.
114         #
115         # The test is whether this appears in the sanitised keyring we
116         # produce in src_compile (it should not be in there).
117         #
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...
122                 %no-protection
123                 %transient-key
124                 %pubring ${P}-ebuild-test-key.asc
126                 Key-Type: 1
127                 Key-Length: 2048
128                 Subkey-Type: 1
129                 Subkey-Length: 2048
130                 Name-Real: Larry The Cow
131                 Name-Email: larry@example.com
132                 Expire-Date: 0
133                 Handle: ${P}-ebuild-test-key
135                 %commit
136                 %echo Temporary key generated!
137         EOF
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.
164         #
165         # Let's now double check by seeing if a file signed by the injected key
166         # is rejected.
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!"
169         fi
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() {
176                 local mode=${1}
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}"
204                         fi
205                 done < "${T}"/tests/trustdb.log
206         }
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.
214         #
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>" \
217                 | grep "^fpr" \
218                 | sed -n 's/^fpr:::::::::\([[:alnum:]]\+\):/\1/p')
220         local key
221         for key in ${keys[@]} ; do
222                 nonfatal edo gpg "${mygpgargs[@]}" --batch --yes --delete-secret-keys ${key}
223         done
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
231 src_install() {
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?