5 export IFS
="$(printf "\n\b")"
8 eval $(apt-config shell APT_DIR Dir)
10 MASTER_KEYRING
='&keyring-master-filename;'
11 eval $(apt-config shell MASTER_KEYRING APT::Key::MasterKeyring)
12 ARCHIVE_KEYRING
='&keyring-filename;'
13 eval $(apt-config shell ARCHIVE_KEYRING APT::Key::ArchiveKeyring)
14 REMOVED_KEYS
='&keyring-removed-filename;'
15 eval $(apt-config shell REMOVED_KEYS APT::Key::RemovedKeys)
16 ARCHIVE_KEYRING_URI
='&keyring-uri;'
17 eval $(apt-config shell ARCHIVE_KEYRING_URI APT::Key::ArchiveKeyringURI)
19 aptkey_echo
() { echo "$@"; }
22 if [ "$(id -u)" -ne 0 ]; then
23 echo >&2 "ERROR: This command can only be used by root."
28 get_fingerprints_of_keyring
() {
29 aptkey_execute
"$GPG_SH" --keyring "$1" --with-colons --fingerprint | while read publine
; do
30 # search for a public key
31 if [ "${publine%%:*}" != 'pub' ]; then continue; fi
32 # search for the associated fingerprint (should be the very next line)
33 while read fprline
; do
34 if [ "${fprline%%:*}" = 'sub' ]; then break; # should never happen
35 elif [ "${fprline%%:*}" != 'fpr' ]; then continue; fi
36 echo "$fprline" | cut
-d':' -f 10
38 # order in the keyring shouldn't be important
42 add_keys_with_verify_against_master_keyring
() {
46 if [ ! -f "$ADD_KEYRING" ]; then
47 echo >&2 "ERROR: '$ADD_KEYRING' not found"
50 if [ ! -f "$MASTER" ]; then
51 echo >&2 "ERROR: '$MASTER' not found"
55 # when adding new keys, make sure that the archive-master-keyring
57 # all keys that are exported must have a valid signature
58 # from a key in the $distro-master-keyring
59 add_keys
="$(get_fingerprints_of_keyring "$ADD_KEYRING")"
60 all_add_keys
=`aptkey_execute "$GPG_SH" --keyring "$ADD_KEYRING" --with-colons --list-keys | grep ^[ps]ub | cut -d: -f5`
61 master_keys
=`aptkey_execute "$GPG_SH" --keyring "$MASTER" --with-colons --list-keys | grep ^pub | cut -d: -f5`
63 # ensure there are no colisions LP: #857472
64 for all_add_key
in $all_add_keys; do
65 for master_key
in $master_keys; do
66 if [ "$all_add_key" = "$master_key" ]; then
67 echo >&2 "Keyid collision for '$all_add_key' detected, operation aborted"
73 for add_key
in $add_keys; do
74 # export the add keyring one-by-one
75 local TMP_KEYRING
="${GPGHOMEDIR}/tmp-keyring.gpg"
76 aptkey_execute
"$GPG_SH" --batch --yes --keyring "$ADD_KEYRING" --output "$TMP_KEYRING" --export "$add_key"
77 if ! aptkey_execute
"$GPG_SH" --batch --yes --keyring "$TMP_KEYRING" --import "$MASTER" > "${GPGHOMEDIR}/gpgoutput.log" 2>&1; then
78 cat >&2 "${GPGHOMEDIR}/gpgoutput.log"
81 # check if signed with the master key and only add in this case
83 for master_key
in $master_keys; do
84 if aptkey_execute
"$GPG_SH" --keyring "$TMP_KEYRING" --check-sigs --with-colons "$add_key" \
85 | grep '^sig:!:' | cut
-d: -f5 | grep -q "$master_key"; then
86 aptkey_execute
"$GPG_SH" --batch --yes --keyring "$ADD_KEYRING" --export "$add_key" \
87 | aptkey_execute
"$GPG" --batch --yes --import
91 if [ $ADDED = 0 ]; then
92 echo >&2 "Key '$add_key' not added. It is not signed with a master key"
94 rm -f "${TMP_KEYRING}"
98 # update the current archive signing keyring from a network URI
99 # the archive-keyring keys needs to be signed with the master key
100 # (otherwise it does not make sense from a security POV)
102 # Disabled for now as code is insecure (LP: #1013639 (and 857472, 1013128))
103 APT_KEY_NET_UPDATE_ENABLED
=""
104 eval $(apt-config shell APT_KEY_NET_UPDATE_ENABLED APT::Key::Net-Update-Enabled)
105 if [ -z "$APT_KEY_NET_UPDATE_ENABLED" ]; then
109 if [ -z "$ARCHIVE_KEYRING_URI" ]; then
110 echo >&2 "ERROR: Your distribution is not supported in net-update as no uri for the archive-keyring is set"
113 # in theory we would need to depend on wget for this, but this feature
114 # isn't useable in debian anyway as we have no keyring uri nor a master key
115 if ! which wget
>/dev
/null
2>&1; then
116 echo >&2 "ERROR: an installed wget is required for a network-based update"
119 if [ ! -d "${APT_DIR}/var/lib/apt/keyrings" ]; then
120 mkdir -p "${APT_DIR}/var/lib/apt/keyrings"
122 keyring
="${APT_DIR}/var/lib/apt/keyrings/$(basename "$ARCHIVE_KEYRING_URI")"
124 if [ -e $keyring ]; then
125 old_mtime
=$(stat -c %Y $keyring)
127 (cd "${APT_DIR}/var/lib/apt/keyrings"; wget
--timeout=90 -q -N "$ARCHIVE_KEYRING_URI")
128 if [ ! -e "$keyring" ]; then
131 new_mtime
=$(stat -c %Y "$keyring")
132 if [ $new_mtime -ne $old_mtime ]; then
133 aptkey_echo
"Checking for new archive signing keys now"
134 add_keys_with_verify_against_master_keyring
"$keyring" "$MASTER_KEYRING"
139 if [ ! -f "$ARCHIVE_KEYRING" ]; then
140 echo >&2 "ERROR: Can't find the archive-keyring"
141 echo >&2 "Is the &keyring-package; package installed?"
145 # add new keys from the package;
147 # we do not use add_keys_with_verify_against_master_keyring here,
148 # because "update" is run on regular package updates. A
149 # attacker might as well replace the master-archive-keyring file
150 # in the package and add his own keys. so this check wouldn't
151 # add any security. we *need* this check on net-update though
152 import_keyring_into_keyring
"$ARCHIVE_KEYRING" '' && cat "${GPGHOMEDIR}/gpgoutput.log"
154 if [ -r "$REMOVED_KEYS" ]; then
155 # remove no-longer supported/used keys
156 get_fingerprints_of_keyring
"$REMOVED_KEYS" | while read key
; do
157 foreach_keyring_do
'remove_key_from_keyring' "$key"
160 echo >&2 "Warning: removed keys keyring $REMOVED_KEYS missing or not readable"
164 remove_key_from_keyring
() {
165 local KEYRINGFILE
="$1"
167 # non-existent keyrings have by definition no keys
168 if [ ! -e "$KEYRINGFILE" ]; then
173 local FINGERPRINTS
="${GPGHOMEDIR}/keyringfile.keylst"
174 get_fingerprints_of_keyring
"$KEYRINGFILE" > "$FINGERPRINTS"
175 # check if the key is in this keyring
176 if ! grep -iq "^[0-9A-F]*${KEY}$" "$FINGERPRINTS"; then
179 if [ ! -w "$KEYRINGFILE" ]; then
180 echo >&2 "Key ${KEY} is in keyring ${KEYRINGFILE}, but can't be removed as it is read only."
183 # check if it is the only key in the keyring and if so remove the keyring altogether
184 if [ '1' = "$(uniq "$FINGERPRINTS" | wc -l)" ]; then
185 mv -f "$KEYRINGFILE" "${KEYRINGFILE}~" # behave like gpg
188 # we can't just modify pointed to files as these might be in /usr or something
190 if [ -L "$KEYRINGFILE" ]; then
191 REALTARGET
="$(readlink -f "$KEYRINGFILE")"
192 mv -f "$KEYRINGFILE" "${KEYRINGFILE}.dpkg-tmp"
193 cp -a "$REALTARGET" "$KEYRINGFILE"
195 # delete the key from the keyring
196 aptkey_execute
"$GPG_SH" --keyring "$KEYRINGFILE" --batch --delete-keys --yes "$KEY"
197 if [ -n "$REALTARGET" ]; then
198 # the real backup is the old link, not the copy we made
199 mv -f "${KEYRINGFILE}.dpkg-tmp" "${KEYRINGFILE}~"
204 foreach_keyring_do
() {
207 # if a --keyring was given, just work on this one
208 if [ -n "$FORCED_KEYRING" ]; then
209 $ACTION "$FORCED_KEYRING" "$@"
211 # otherwise all known keyrings are up for inspection
212 if [ -s "$TRUSTEDFILE" ]; then
213 $ACTION "$TRUSTEDFILE" "$@"
215 local TRUSTEDPARTS
="/etc/apt/trusted.gpg.d"
216 eval $(apt-config shell TRUSTEDPARTS Dir::Etc::TrustedParts/d)
217 if [ -d "$TRUSTEDPARTS" ]; then
218 # strip / suffix as gpg will double-slash in that case (#665411)
219 local STRIPPED_TRUSTEDPARTS
="${TRUSTEDPARTS%/}"
220 if [ "${STRIPPED_TRUSTEDPARTS}/" = "$TRUSTEDPARTS" ]; then
221 TRUSTEDPARTS
="$STRIPPED_TRUSTEDPARTS"
223 for trusted
in $(run-parts --list "$TRUSTEDPARTS" --regex '^.*\.gpg$'); do
224 if [ -s "$trusted" ]; then
225 $ACTION "$trusted" "$@"
232 run_cmd_on_keyring
() {
233 local KEYRINGFILE
="$1"
235 # fingerprint and co will fail if key isn't in this keyring
236 aptkey_execute
"$GPG_SH" --keyring "$KEYRINGFILE" --batch "$@" 2>/dev
/null
|| true
239 import_keyring_into_keyring
() {
240 local FROM
="${1:-${GPGHOMEDIR}/pubring.gpg}"
241 local TO
="${2:-${GPGHOMEDIR}/pubring.gpg}"
243 rm -f "${GPGHOMEDIR}/gpgoutput.log"
244 # the idea is simple: We take keys from one keyring and copy it to another
245 # we do this with so many checks in between to ensure that WE control the
246 # creation, so we know that the (potentially) created $TO keyring is a
247 # simple keyring rather than a keybox as gpg2 would create it which in turn
248 # can't be read by gpgv.
249 # BEWARE: This is designed more in the way to work with the current
250 # callers, than to have a well defined it would be easy to add new callers to.
251 if [ ! -s "$TO" ]; then
252 if [ -s "$FROM" ]; then
254 if ! aptkey_execute
"$GPG_SH" --keyring "$FROM" --export ${1:+"$1"} > "$TO" 2> "${GPGHOMEDIR}/gpgoutput.log"; then
255 cat >&2 "${GPGHOMEDIR}/gpgoutput.log"
261 create_new_keyring
"$TO"
264 create_new_keyring
"$TO"
266 elif [ -s "$FROM" ]; then
267 local EXPORTLIMIT
="$1"
268 if [ -n "$1$2" ]; then shift; fi
269 if ! aptkey_execute
"$GPG_SH" --keyring "$FROM" --export ${EXPORTLIMIT:+"$EXPORTLIMIT"} \
270 | aptkey_execute
"$GPG_SH" --keyring "$TO" --batch --import "$@" > "${GPGHOMEDIR}/gpgoutput.log" 2>&1; then
271 cat >&2 "${GPGHOMEDIR}/gpgoutput.log"
277 merge_all_trusted_keyrings_into_pubring
() {
279 # foreach_keyring_do 'import_keys_from_keyring' "${GPGHOMEDIR}/pubring.gpg"
280 # but without using gpg, just cat and find
281 local PUBRING
="${GPGHOMEDIR}/pubring.gpg"
282 # if a --keyring was given, just use this one
283 if [ -n "$FORCED_KEYRING" ]; then
284 if [ -s "$FORCED_KEYRING" ]; then
285 cp --dereference "$FORCED_KEYRING" "$PUBRING"
288 # otherwise all known keyrings are merged
289 local TRUSTEDPARTS
="/etc/apt/trusted.gpg.d"
290 eval $(apt-config shell TRUSTEDPARTS Dir::Etc::TrustedParts/d)
291 if [ -d "$TRUSTEDPARTS" ]; then
292 # ignore errors mostly for non-existing $TRUSTEDFILE
294 cat "$TRUSTEDFILE" || true
295 for parts
in $(find -L "$TRUSTEDPARTS" -type f -name '*.gpg'); do
298 } > "$PUBRING" 2>/dev
/null
299 elif [ -s "$TRUSTEDFILE" ]; then
300 cp --dereference "$TRUSTEDFILE" "$PUBRING"
304 if [ ! -s "$PUBRING" ]; then
309 import_keys_from_keyring
() {
310 import_keyring_into_keyring
"$1" "$2"
313 merge_keys_into_keyrings
() {
314 import_keyring_into_keyring
"$2" "$1" '' --import-options 'merge-only'
317 merge_back_changes
() {
318 if [ -n "$FORCED_KEYRING" ]; then
319 # if the keyring was forced merge is already done
322 if [ -s "${GPGHOMEDIR}/pubring.gpg" ]; then
323 # merge all updated keys
324 foreach_keyring_do
'merge_keys_into_keyrings' "${GPGHOMEDIR}/pubring.gpg"
326 # look for keys which were added or removed
327 get_fingerprints_of_keyring
"${GPGHOMEDIR}/pubring.orig.gpg" > "${GPGHOMEDIR}/pubring.orig.keylst"
328 get_fingerprints_of_keyring
"${GPGHOMEDIR}/pubring.gpg" > "${GPGHOMEDIR}/pubring.keylst"
329 comm -3 "${GPGHOMEDIR}/pubring.keylst" "${GPGHOMEDIR}/pubring.orig.keylst" > "${GPGHOMEDIR}/pubring.diff"
330 # key isn't part of new keyring, so remove
331 cut
-f 2 "${GPGHOMEDIR}/pubring.diff" | while read key
; do
332 if [ -z "$key" ]; then continue; fi
333 foreach_keyring_do
'remove_key_from_keyring' "$key"
335 # key is only part of new keyring, so we need to import it
336 cut
-f 1 "${GPGHOMEDIR}/pubring.diff" | while read key
; do
337 if [ -z "$key" ]; then continue; fi
338 import_keyring_into_keyring
'' "$TRUSTEDFILE" "$key"
342 setup_merged_keyring
() {
343 if [ -n "$FORCED_KEYID" ]; then
344 merge_all_trusted_keyrings_into_pubring
345 FORCED_KEYRING
="${GPGHOMEDIR}/forcedkeyid.gpg"
346 TRUSTEDFILE
="${FORCED_KEYRING}"
348 exec sh \"${GPG}\" --keyring \"${TRUSTEDFILE}\" \"\$@\"" > "${GPGHOMEDIR}/gpg.1.sh"
349 GPG
="${GPGHOMEDIR}/gpg.1.sh"
350 # ignore error as this "just" means we haven't found the forced keyid and the keyring will be empty
351 import_keyring_into_keyring
'' "$TRUSTEDFILE" "$FORCED_KEYID" || true
352 elif [ -z "$FORCED_KEYRING" ]; then
353 merge_all_trusted_keyrings_into_pubring
354 if [ -r "${GPGHOMEDIR}/pubring.gpg" ]; then
355 cp -a "${GPGHOMEDIR}/pubring.gpg" "${GPGHOMEDIR}/pubring.orig.gpg"
357 touch "${GPGHOMEDIR}/pubring.gpg" "${GPGHOMEDIR}/pubring.orig.gpg"
360 exec sh \"${GPG}\" --keyring \"${GPGHOMEDIR}/pubring.gpg\" \"\$@\"" > "${GPGHOMEDIR}/gpg.1.sh"
361 GPG
="${GPGHOMEDIR}/gpg.1.sh"
363 create_new_keyring
"$TRUSTEDFILE"
365 exec sh \"${GPG}\" --keyring \"${TRUSTEDFILE}\" \"\$@\"" > "${GPGHOMEDIR}/gpg.1.sh"
366 GPG
="${GPGHOMEDIR}/gpg.1.sh"
370 create_new_keyring
() {
371 # gpg defaults to mode 0600 for new keyrings. Create one with 0644 instead.
372 if ! [ -e "$TRUSTEDFILE" ]; then
373 if [ -w "$(dirname "$TRUSTEDFILE")" ]; then
374 touch -- "$TRUSTEDFILE"
375 chmod 0644 -- "$TRUSTEDFILE"
380 aptkey_execute
() { sh
"$@"; }
383 echo "Usage: apt-key [--keyring file] [command] [arguments]"
385 echo "Manage apt's list of trusted keys"
387 echo " apt-key add <file> - add the key contained in <file> ('-' for stdin)"
388 echo " apt-key del <keyid> - remove the key <keyid>"
389 echo " apt-key export <keyid> - output the key <keyid>"
390 echo " apt-key exportall - output all trusted keys"
391 echo " apt-key update - update keys using the keyring package"
392 echo " apt-key net-update - update keys using the network"
393 echo " apt-key list - list keys"
394 echo " apt-key finger - list fingerprints"
395 echo " apt-key adv - pass advanced options to gpg (download key)"
397 echo "If no specific keyring file is given the command applies to all keyring files."
400 while [ -n "$1" ]; do
413 FORCED_SECRET_KEYRING
="$1"
416 merge_back_changes
() { true
; }
417 create_new_keyring
() { if [ ! -r "$FORCED_KEYRING" ]; then TRUSTEDFILE
='/dev/null'; FORCED_KEYRING
="$TRUSTEDFILE"; fi; }
420 requires_root
() { true
; }
423 aptkey_echo
() { true
; }
426 # some cmds like finger redirect stderr to /dev/null …
427 aptkey_execute
() { echo 'EXEC:' "$@"; sh
"$@"; }
430 # … other more complicated ones pipe gpg into gpg.
431 aptkey_execute
() { echo >&2 'EXEC:' "$@"; sh
"$@"; }
434 echo >&2 "Unknown option: $1"
443 if [ -z "$TRUSTEDFILE" ]; then
444 TRUSTEDFILE
="/etc/apt/trusted.gpg"
445 eval $(apt-config shell TRUSTEDFILE Apt::GPGV::TrustedKeyring)
446 eval $(apt-config shell TRUSTEDFILE Dir::Etc::Trusted/f)
450 if [ -z "$command" ]; then
457 # gpg needs (in different versions more or less) files to function correctly,
458 # so we give it its own homedir and generate some valid content for it later on
459 if [ -n "$TMPDIR" ]; then
460 # tmpdir is a directory and current user has rwx access to it
461 # same tests as in apt-pkg/contrib/fileutl.cc GetTempDir()
462 if [ ! -d "$TMPDIR" ] || [ ! -r "$TMPDIR" ] || [ ! -w "$TMPDIR" ] || [ ! -x "$TMPDIR" ]; then
466 GPGHOMEDIR
="$(mktemp -d)"
467 CURRENTTRAP
="${CURRENTTRAP} rm -rf '${GPGHOMEDIR}';"
468 trap "${CURRENTTRAP}" 0 HUP INT QUIT ILL ABRT FPE SEGV PIPE TERM
469 chmod 700 "$GPGHOMEDIR"
473 eval $(apt-config shell GPG_EXE Apt::Key::gpgcommand)
475 if [ -n "$GPG_EXE" ] && which "$GPG_EXE" >/dev
/null
2>&1; then
477 elif which gpg
>/dev
/null
2>&1; then
479 elif which gpg2
>/dev
/null
2>&1; then
482 echo >&2 "Error: gnupg or gnupg2 do not seem to be installed,"
483 echo >&2 "Error: but apt-key requires gnupg or gnupg2 for this operation."
490 # We don't use a secret keyring, of course, but gpg panics and
491 # implodes if there isn't one available - and writeable for imports
492 SECRETKEYRING
="${GPGHOMEDIR}/secring.gpg"
493 touch "$SECRETKEYRING"
495 # create the trustdb with an (empty) dummy keyring
496 # older gpgs required it, newer gpgs even warn that it isn't needed,
497 # but require it nonetheless for some commands, so we just play safe
498 # here for the foreseeable future and create a dummy one
499 if ! "$GPG_EXE" --ignore-time-conflict --no-options --no-default-keyring \
500 --homedir "$GPGHOMEDIR" --quiet --check-trustdb --keyring "$SECRETKEYRING" >"${GPGHOMEDIR}/gpgoutput.log" 2>&1; then
501 cat >&2 "${GPGHOMEDIR}/gpgoutput.log"
504 # tell gpg that it shouldn't try to maintain a trustdb file
506 exec \"${GPG_EXE}\" --ignore-time-conflict --no-options --no-default-keyring \\
507 --homedir \"${GPGHOMEDIR}\" --no-auto-check-trustdb --trust-model always \"\$@\"" > "${GPGHOMEDIR}/gpg.0.sh"
508 GPG_SH
="${GPGHOMEDIR}/gpg.0.sh"
511 # for advanced operations, we might really need a secret keyring after all
512 if [ -n "$FORCED_SECRET_KEYRING" ] && [ -r "$FORCED_SECRET_KEYRING" ]; then
513 rm -f "$SECRETKEYRING"
514 cp -a "$FORCED_SECRET_KEYRING" "$SECRETKEYRING"
517 # older gpg versions need a secring file, but newer versions take it as
518 # a hint to start a migration from earlier versions. The file is empty
519 # anyhow, so nothing actually happens, but its three lines of output
520 # nobody expects to see in apt-key context, so trigger it in silence
521 echo -n | aptkey_execute
"$GPG" --batch --import >/dev
/null
2>&1 || true
524 if [ "$command" != 'help' ] && [ "$command" != 'verify' ]; then
532 aptkey_execute
"$GPG" --quiet --batch --import "$@"
538 foreach_keyring_do
'remove_key_from_keyring' "$@"
554 foreach_keyring_do
'run_cmd_on_keyring' --list-keys "$@"
557 foreach_keyring_do
'run_cmd_on_keyring' --fingerprint "$@"
560 merge_all_trusted_keyrings_into_pubring
561 aptkey_execute
"$GPG_SH" --keyring "${GPGHOMEDIR}/pubring.gpg" --armor --export "$@"
565 aptkey_echo
"Executing: $GPG $*"
566 aptkey_execute
"$GPG" "$@"
571 eval $(apt-config shell GPGV Apt::Key::gpgvcommand)
572 if [ -n "$GPGV" ] && which "$GPGV" >/dev
/null
2>&1; then true
;
573 elif which gpgv
>/dev
/null
2>&1; then GPGV
='gpgv';
574 elif which gpgv2
>/dev
/null
2>&1; then GPGV
='gpgv2';
576 echo >&2 'ERROR: gpgv or gpgv2 required for verification'
579 # for a forced keyid we need gpg --export, so full wrapping required
580 if [ -n "$FORCED_KEYID" ]; then
586 if [ -n "$FORCED_KEYRING" ]; then
587 "$GPGV" --homedir "${GPGHOMEDIR}" --keyring "${FORCED_KEYRING}" --ignore-time-conflict "$@"
589 "$GPGV" --homedir "${GPGHOMEDIR}" --keyring "${GPGHOMEDIR}/pubring.gpg" --ignore-time-conflict "$@"