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."
29 # command -v "$1" >/dev/null 2>&1 # not required by policy, see #747320
30 # which "$1" >/dev/null 2>&1 # is in debianutils (essential) but not on non-debian systems
34 if [ -x "${p}/${1}" ]; then
43 get_fingerprints_of_keyring
() {
44 aptkey_execute
"$GPG_SH" --keyring "$1" --with-colons --fingerprint | while read publine
; do
45 # search for a public key
46 if [ "${publine%%:*}" != 'pub' ]; then continue; fi
47 # search for the associated fingerprint (should be the very next line)
48 while read fprline
; do
49 if [ "${fprline%%:*}" = 'sub' ]; then break; # should never happen
50 elif [ "${fprline%%:*}" != 'fpr' ]; then continue; fi
51 echo "$fprline" | cut
-d':' -f 10
53 # order in the keyring shouldn't be important
57 add_keys_with_verify_against_master_keyring
() {
61 if [ ! -f "$ADD_KEYRING" ]; then
62 echo >&2 "ERROR: '$ADD_KEYRING' not found"
65 if [ ! -f "$MASTER" ]; then
66 echo >&2 "ERROR: '$MASTER' not found"
70 # when adding new keys, make sure that the archive-master-keyring
72 # all keys that are exported must have a valid signature
73 # from a key in the $distro-master-keyring
74 add_keys
="$(get_fingerprints_of_keyring "$ADD_KEYRING")"
75 all_add_keys
=`aptkey_execute "$GPG_SH" --keyring "$ADD_KEYRING" --with-colons --list-keys | grep ^[ps]ub | cut -d: -f5`
76 master_keys
=`aptkey_execute "$GPG_SH" --keyring "$MASTER" --with-colons --list-keys | grep ^pub | cut -d: -f5`
78 # ensure there are no colisions LP: #857472
79 for all_add_key
in $all_add_keys; do
80 for master_key
in $master_keys; do
81 if [ "$all_add_key" = "$master_key" ]; then
82 echo >&2 "Keyid collision for '$all_add_key' detected, operation aborted"
88 for add_key
in $add_keys; do
89 # export the add keyring one-by-one
90 local TMP_KEYRING
="${GPGHOMEDIR}/tmp-keyring.gpg"
91 aptkey_execute
"$GPG_SH" --batch --yes --keyring "$ADD_KEYRING" --output "$TMP_KEYRING" --export "$add_key"
92 if ! aptkey_execute
"$GPG_SH" --batch --yes --keyring "$TMP_KEYRING" --import "$MASTER" > "${GPGHOMEDIR}/gpgoutput.log" 2>&1; then
93 cat >&2 "${GPGHOMEDIR}/gpgoutput.log"
96 # check if signed with the master key and only add in this case
98 for master_key
in $master_keys; do
99 if aptkey_execute
"$GPG_SH" --keyring "$TMP_KEYRING" --check-sigs --with-colons "$add_key" \
100 | grep '^sig:!:' | cut
-d: -f5 | grep -q "$master_key"; then
101 aptkey_execute
"$GPG_SH" --batch --yes --keyring "$ADD_KEYRING" --export "$add_key" \
102 | aptkey_execute
"$GPG" --batch --yes --import
106 if [ $ADDED = 0 ]; then
107 echo >&2 "Key '$add_key' not added. It is not signed with a master key"
109 rm -f "${TMP_KEYRING}"
113 # update the current archive signing keyring from a network URI
114 # the archive-keyring keys needs to be signed with the master key
115 # (otherwise it does not make sense from a security POV)
117 # Disabled for now as code is insecure (LP: #1013639 (and 857472, 1013128))
118 APT_KEY_NET_UPDATE_ENABLED
=""
119 eval $(apt-config shell APT_KEY_NET_UPDATE_ENABLED APT::Key::Net-Update-Enabled)
120 if [ -z "$APT_KEY_NET_UPDATE_ENABLED" ]; then
124 if [ -z "$ARCHIVE_KEYRING_URI" ]; then
125 echo >&2 "ERROR: Your distribution is not supported in net-update as no uri for the archive-keyring is set"
128 # in theory we would need to depend on wget for this, but this feature
129 # isn't useable in debian anyway as we have no keyring uri nor a master key
130 if ! command_available
'wget'; then
131 echo >&2 "ERROR: an installed wget is required for a network-based update"
134 if [ ! -d "${APT_DIR}/var/lib/apt/keyrings" ]; then
135 mkdir -p "${APT_DIR}/var/lib/apt/keyrings"
137 keyring
="${APT_DIR}/var/lib/apt/keyrings/$(basename "$ARCHIVE_KEYRING_URI")"
139 if [ -e $keyring ]; then
140 old_mtime
=$(stat -c %Y $keyring)
142 (cd "${APT_DIR}/var/lib/apt/keyrings"; wget
--timeout=90 -q -N "$ARCHIVE_KEYRING_URI")
143 if [ ! -e "$keyring" ]; then
146 new_mtime
=$(stat -c %Y "$keyring")
147 if [ $new_mtime -ne $old_mtime ]; then
148 aptkey_echo
"Checking for new archive signing keys now"
149 add_keys_with_verify_against_master_keyring
"$keyring" "$MASTER_KEYRING"
154 if [ ! -f "$ARCHIVE_KEYRING" ]; then
155 echo >&2 "ERROR: Can't find the archive-keyring"
156 echo >&2 "Is the &keyring-package; package installed?"
160 # add new keys from the package;
162 # we do not use add_keys_with_verify_against_master_keyring here,
163 # because "update" is run on regular package updates. A
164 # attacker might as well replace the master-archive-keyring file
165 # in the package and add his own keys. so this check wouldn't
166 # add any security. we *need* this check on net-update though
167 import_keyring_into_keyring
"$ARCHIVE_KEYRING" '' && cat "${GPGHOMEDIR}/gpgoutput.log"
169 if [ -r "$REMOVED_KEYS" ]; then
170 # remove no-longer supported/used keys
171 get_fingerprints_of_keyring
"$REMOVED_KEYS" | while read key
; do
172 foreach_keyring_do
'remove_key_from_keyring' "$key"
175 echo >&2 "Warning: removed keys keyring $REMOVED_KEYS missing or not readable"
179 remove_key_from_keyring
() {
180 local KEYRINGFILE
="$1"
182 # non-existent keyrings have by definition no keys
183 if [ ! -e "$KEYRINGFILE" ]; then
188 local FINGERPRINTS
="${GPGHOMEDIR}/keyringfile.keylst"
189 get_fingerprints_of_keyring
"$KEYRINGFILE" > "$FINGERPRINTS"
190 # check if the key is in this keyring
191 if ! grep -iq "^[0-9A-F]*${KEY}$" "$FINGERPRINTS"; then
194 if [ ! -w "$KEYRINGFILE" ]; then
195 echo >&2 "Key ${KEY} is in keyring ${KEYRINGFILE}, but can't be removed as it is read only."
198 # check if it is the only key in the keyring and if so remove the keyring altogether
199 if [ '1' = "$(uniq "$FINGERPRINTS" | wc -l)" ]; then
200 mv -f "$KEYRINGFILE" "${KEYRINGFILE}~" # behave like gpg
203 # we can't just modify pointed to files as these might be in /usr or something
205 if [ -L "$KEYRINGFILE" ]; then
206 REALTARGET
="$(readlink -f "$KEYRINGFILE")"
207 mv -f "$KEYRINGFILE" "${KEYRINGFILE}.dpkg-tmp"
208 cp -a "$REALTARGET" "$KEYRINGFILE"
210 # delete the key from the keyring
211 aptkey_execute
"$GPG_SH" --keyring "$KEYRINGFILE" --batch --delete-keys --yes "$KEY"
212 if [ -n "$REALTARGET" ]; then
213 # the real backup is the old link, not the copy we made
214 mv -f "${KEYRINGFILE}.dpkg-tmp" "${KEYRINGFILE}~"
219 foreach_keyring_do
() {
222 # if a --keyring was given, just work on this one
223 if [ -n "$FORCED_KEYRING" ]; then
224 $ACTION "$FORCED_KEYRING" "$@"
226 # otherwise all known keyrings are up for inspection
227 if [ -s "$TRUSTEDFILE" ]; then
228 $ACTION "$TRUSTEDFILE" "$@"
230 local TRUSTEDPARTS
="/etc/apt/trusted.gpg.d"
231 eval $(apt-config shell TRUSTEDPARTS Dir::Etc::TrustedParts/d)
232 if [ -d "$TRUSTEDPARTS" ]; then
233 # strip / suffix as gpg will double-slash in that case (#665411)
234 local STRIPPED_TRUSTEDPARTS
="${TRUSTEDPARTS%/}"
235 if [ "${STRIPPED_TRUSTEDPARTS}/" = "$TRUSTEDPARTS" ]; then
236 TRUSTEDPARTS
="$STRIPPED_TRUSTEDPARTS"
238 for trusted
in $(find "$TRUSTEDPARTS" -mindepth 1 -maxdepth 1 -regex '^.*\.gpg$' | sort); do
239 if [ -s "$trusted" ]; then
240 $ACTION "$trusted" "$@"
247 run_cmd_on_keyring
() {
248 local KEYRINGFILE
="$1"
250 # fingerprint and co will fail if key isn't in this keyring
251 aptkey_execute
"$GPG_SH" --keyring "$KEYRINGFILE" --batch "$@" 2>/dev
/null
|| true
254 import_keyring_into_keyring
() {
255 local FROM
="${1:-${GPGHOMEDIR}/pubring.gpg}"
256 local TO
="${2:-${GPGHOMEDIR}/pubring.gpg}"
258 rm -f "${GPGHOMEDIR}/gpgoutput.log"
259 # the idea is simple: We take keys from one keyring and copy it to another
260 # we do this with so many checks in between to ensure that WE control the
261 # creation, so we know that the (potentially) created $TO keyring is a
262 # simple keyring rather than a keybox as gpg2 would create it which in turn
263 # can't be read by gpgv.
264 # BEWARE: This is designed more in the way to work with the current
265 # callers, than to have a well defined it would be easy to add new callers to.
266 if [ ! -s "$TO" ]; then
267 if [ -s "$FROM" ]; then
269 if ! aptkey_execute
"$GPG_SH" --keyring "$FROM" --export ${1:+"$1"} > "$TO" 2> "${GPGHOMEDIR}/gpgoutput.log"; then
270 cat >&2 "${GPGHOMEDIR}/gpgoutput.log"
276 create_new_keyring
"$TO"
279 create_new_keyring
"$TO"
281 elif [ -s "$FROM" ]; then
282 local EXPORTLIMIT
="$1"
283 if [ -n "$1$2" ]; then shift; fi
284 if ! aptkey_execute
"$GPG_SH" --keyring "$FROM" --export ${EXPORTLIMIT:+"$EXPORTLIMIT"} \
285 | aptkey_execute
"$GPG_SH" --keyring "$TO" --batch --import "$@" > "${GPGHOMEDIR}/gpgoutput.log" 2>&1; then
286 cat >&2 "${GPGHOMEDIR}/gpgoutput.log"
292 merge_all_trusted_keyrings_into_pubring
() {
294 # foreach_keyring_do 'import_keys_from_keyring' "${GPGHOMEDIR}/pubring.gpg"
295 # but without using gpg, just cat and find
296 local PUBRING
="${GPGHOMEDIR}/pubring.gpg"
297 # if a --keyring was given, just use this one
298 if [ -n "$FORCED_KEYRING" ]; then
299 if [ -s "$FORCED_KEYRING" ]; then
300 cp --dereference "$FORCED_KEYRING" "$PUBRING"
303 # otherwise all known keyrings are merged
304 local TRUSTEDPARTS
="/etc/apt/trusted.gpg.d"
305 eval $(apt-config shell TRUSTEDPARTS Dir::Etc::TrustedParts/d)
306 if [ -d "$TRUSTEDPARTS" ]; then
307 # ignore errors mostly for non-existing $TRUSTEDFILE
309 cat "$TRUSTEDFILE" || true
310 for parts
in $(find -L "$TRUSTEDPARTS" -type f -name '*.gpg'); do
313 } > "$PUBRING" 2>/dev
/null
314 elif [ -s "$TRUSTEDFILE" ]; then
315 cp --dereference "$TRUSTEDFILE" "$PUBRING"
319 if [ ! -s "$PUBRING" ]; then
324 import_keys_from_keyring
() {
325 import_keyring_into_keyring
"$1" "$2"
328 merge_keys_into_keyrings
() {
329 import_keyring_into_keyring
"$2" "$1" '' --import-options 'merge-only'
332 merge_back_changes
() {
333 if [ -n "$FORCED_KEYRING" ]; then
334 # if the keyring was forced merge is already done
337 if [ -s "${GPGHOMEDIR}/pubring.gpg" ]; then
338 # merge all updated keys
339 foreach_keyring_do
'merge_keys_into_keyrings' "${GPGHOMEDIR}/pubring.gpg"
341 # look for keys which were added or removed
342 get_fingerprints_of_keyring
"${GPGHOMEDIR}/pubring.orig.gpg" > "${GPGHOMEDIR}/pubring.orig.keylst"
343 get_fingerprints_of_keyring
"${GPGHOMEDIR}/pubring.gpg" > "${GPGHOMEDIR}/pubring.keylst"
344 comm -3 "${GPGHOMEDIR}/pubring.keylst" "${GPGHOMEDIR}/pubring.orig.keylst" > "${GPGHOMEDIR}/pubring.diff"
345 # key isn't part of new keyring, so remove
346 cut
-f 2 "${GPGHOMEDIR}/pubring.diff" | while read key
; do
347 if [ -z "$key" ]; then continue; fi
348 foreach_keyring_do
'remove_key_from_keyring' "$key"
350 # key is only part of new keyring, so we need to import it
351 cut
-f 1 "${GPGHOMEDIR}/pubring.diff" | while read key
; do
352 if [ -z "$key" ]; then continue; fi
353 import_keyring_into_keyring
'' "$TRUSTEDFILE" "$key"
357 setup_merged_keyring
() {
358 if [ -n "$FORCED_KEYID" ]; then
359 merge_all_trusted_keyrings_into_pubring
360 FORCED_KEYRING
="${GPGHOMEDIR}/forcedkeyid.gpg"
361 TRUSTEDFILE
="${FORCED_KEYRING}"
363 exec sh \"${GPG}\" --keyring \"${TRUSTEDFILE}\" \"\$@\"" > "${GPGHOMEDIR}/gpg.1.sh"
364 GPG
="${GPGHOMEDIR}/gpg.1.sh"
365 # ignore error as this "just" means we haven't found the forced keyid and the keyring will be empty
366 import_keyring_into_keyring
'' "$TRUSTEDFILE" "$FORCED_KEYID" || true
367 elif [ -z "$FORCED_KEYRING" ]; then
368 merge_all_trusted_keyrings_into_pubring
369 if [ -r "${GPGHOMEDIR}/pubring.gpg" ]; then
370 cp -a "${GPGHOMEDIR}/pubring.gpg" "${GPGHOMEDIR}/pubring.orig.gpg"
372 touch "${GPGHOMEDIR}/pubring.gpg" "${GPGHOMEDIR}/pubring.orig.gpg"
375 exec sh \"${GPG}\" --keyring \"${GPGHOMEDIR}/pubring.gpg\" \"\$@\"" > "${GPGHOMEDIR}/gpg.1.sh"
376 GPG
="${GPGHOMEDIR}/gpg.1.sh"
378 create_new_keyring
"$TRUSTEDFILE"
380 exec sh \"${GPG}\" --keyring \"${TRUSTEDFILE}\" \"\$@\"" > "${GPGHOMEDIR}/gpg.1.sh"
381 GPG
="${GPGHOMEDIR}/gpg.1.sh"
385 create_new_keyring
() {
386 # gpg defaults to mode 0600 for new keyrings. Create one with 0644 instead.
387 if ! [ -e "$TRUSTEDFILE" ]; then
388 if [ -w "$(dirname "$TRUSTEDFILE")" ]; then
389 touch -- "$TRUSTEDFILE"
390 chmod 0644 -- "$TRUSTEDFILE"
395 aptkey_execute
() { sh
"$@"; }
398 echo "Usage: apt-key [--keyring file] [command] [arguments]"
400 echo "Manage apt's list of trusted keys"
402 echo " apt-key add <file> - add the key contained in <file> ('-' for stdin)"
403 echo " apt-key del <keyid> - remove the key <keyid>"
404 echo " apt-key export <keyid> - output the key <keyid>"
405 echo " apt-key exportall - output all trusted keys"
406 echo " apt-key update - update keys using the keyring package"
407 echo " apt-key net-update - update keys using the network"
408 echo " apt-key list - list keys"
409 echo " apt-key finger - list fingerprints"
410 echo " apt-key adv - pass advanced options to gpg (download key)"
412 echo "If no specific keyring file is given the command applies to all keyring files."
415 while [ -n "$1" ]; do
428 FORCED_SECRET_KEYRING
="$1"
431 merge_back_changes
() { true
; }
432 create_new_keyring
() { if [ ! -r "$FORCED_KEYRING" ]; then TRUSTEDFILE
='/dev/null'; FORCED_KEYRING
="$TRUSTEDFILE"; fi; }
435 requires_root
() { true
; }
438 aptkey_echo
() { true
; }
441 # some cmds like finger redirect stderr to /dev/null …
442 aptkey_execute
() { echo 'EXEC:' "$@"; sh
"$@"; }
445 # … other more complicated ones pipe gpg into gpg.
446 aptkey_execute
() { echo >&2 'EXEC:' "$@"; sh
"$@"; }
449 echo >&2 "Unknown option: $1"
458 if [ -z "$TRUSTEDFILE" ]; then
459 TRUSTEDFILE
="/etc/apt/trusted.gpg"
460 eval $(apt-config shell TRUSTEDFILE Apt::GPGV::TrustedKeyring)
461 eval $(apt-config shell TRUSTEDFILE Dir::Etc::Trusted/f)
465 if [ -z "$command" ]; then
472 # gpg needs (in different versions more or less) files to function correctly,
473 # so we give it its own homedir and generate some valid content for it later on
474 if [ -n "$TMPDIR" ]; then
475 # tmpdir is a directory and current user has rwx access to it
476 # same tests as in apt-pkg/contrib/fileutl.cc GetTempDir()
477 if [ ! -d "$TMPDIR" ] || [ ! -r "$TMPDIR" ] || [ ! -w "$TMPDIR" ] || [ ! -x "$TMPDIR" ]; then
481 GPGHOMEDIR
="$(mktemp -d)"
482 CURRENTTRAP
="${CURRENTTRAP} rm -rf '${GPGHOMEDIR}';"
483 trap "${CURRENTTRAP}" 0 HUP INT QUIT ILL ABRT FPE SEGV PIPE TERM
484 chmod 700 "$GPGHOMEDIR"
488 eval $(apt-config shell GPG_EXE Apt::Key::gpgcommand)
490 if [ -n "$GPG_EXE" ] && command_available
"$GPG_EXE"; then
492 elif command_available
'gpg'; then
494 elif command_available
'gpg2'; then
497 echo >&2 "Error: gnupg or gnupg2 do not seem to be installed,"
498 echo >&2 "Error: but apt-key requires gnupg or gnupg2 for this operation."
505 # We don't use a secret keyring, of course, but gpg panics and
506 # implodes if there isn't one available - and writeable for imports
507 SECRETKEYRING
="${GPGHOMEDIR}/secring.gpg"
508 touch "$SECRETKEYRING"
510 # create the trustdb with an (empty) dummy keyring
511 # older gpgs required it, newer gpgs even warn that it isn't needed,
512 # but require it nonetheless for some commands, so we just play safe
513 # here for the foreseeable future and create a dummy one
514 if ! "$GPG_EXE" --ignore-time-conflict --no-options --no-default-keyring \
515 --homedir "$GPGHOMEDIR" --quiet --check-trustdb --keyring "$SECRETKEYRING" >"${GPGHOMEDIR}/gpgoutput.log" 2>&1; then
516 cat >&2 "${GPGHOMEDIR}/gpgoutput.log"
519 # tell gpg that it shouldn't try to maintain a trustdb file
521 exec \"${GPG_EXE}\" --ignore-time-conflict --no-options --no-default-keyring \\
522 --homedir \"${GPGHOMEDIR}\" --no-auto-check-trustdb --trust-model always \"\$@\"" > "${GPGHOMEDIR}/gpg.0.sh"
523 GPG_SH
="${GPGHOMEDIR}/gpg.0.sh"
526 # for advanced operations, we might really need a secret keyring after all
527 if [ -n "$FORCED_SECRET_KEYRING" ] && [ -r "$FORCED_SECRET_KEYRING" ]; then
528 rm -f "$SECRETKEYRING"
529 cp -a "$FORCED_SECRET_KEYRING" "$SECRETKEYRING"
532 # older gpg versions need a secring file, but newer versions take it as
533 # a hint to start a migration from earlier versions. The file is empty
534 # anyhow, so nothing actually happens, but its three lines of output
535 # nobody expects to see in apt-key context, so trigger it in silence
536 echo -n | aptkey_execute
"$GPG" --batch --import >/dev
/null
2>&1 || true
539 if [ "$command" != 'help' ] && [ "$command" != 'verify' ]; then
547 aptkey_execute
"$GPG" --quiet --batch --import "$@"
553 foreach_keyring_do
'remove_key_from_keyring' "$@"
569 foreach_keyring_do
'run_cmd_on_keyring' --list-keys "$@"
572 foreach_keyring_do
'run_cmd_on_keyring' --fingerprint "$@"
575 merge_all_trusted_keyrings_into_pubring
576 aptkey_execute
"$GPG_SH" --keyring "${GPGHOMEDIR}/pubring.gpg" --armor --export "$@"
580 aptkey_echo
"Executing: $GPG $*"
581 aptkey_execute
"$GPG" "$@"
586 eval $(apt-config shell GPGV Apt::Key::gpgvcommand)
587 if [ -n "$GPGV" ] && command_available
"$GPGV"; then true
;
588 elif command_available
'gpgv'; then GPGV
='gpgv';
589 elif command_available
'gpgv2'; then GPGV
='gpgv2';
591 echo >&2 'ERROR: gpgv or gpgv2 required for verification'
594 # for a forced keyid we need gpg --export, so full wrapping required
595 if [ -n "$FORCED_KEYID" ]; then
601 if [ -n "$FORCED_KEYRING" ]; then
602 "$GPGV" --homedir "${GPGHOMEDIR}" --keyring "${FORCED_KEYRING}" --ignore-time-conflict "$@"
604 "$GPGV" --homedir "${GPGHOMEDIR}" --keyring "${GPGHOMEDIR}/pubring.gpg" --ignore-time-conflict "$@"