5 export IFS
="$(printf "\n\b")"
7 MASTER_KEYRING
='&keyring-master-filename;'
8 eval "$(apt-config shell MASTER_KEYRING APT::Key::MasterKeyring)"
9 ARCHIVE_KEYRING
='&keyring-filename;'
10 eval "$(apt-config shell ARCHIVE_KEYRING APT::Key::ArchiveKeyring)"
11 REMOVED_KEYS
='&keyring-removed-filename;'
12 eval "$(apt-config shell REMOVED_KEYS APT::Key::RemovedKeys)"
13 ARCHIVE_KEYRING_URI
='&keyring-uri;'
14 eval "$(apt-config shell ARCHIVE_KEYRING_URI APT::Key::ArchiveKeyringURI)"
16 aptkey_echo
() { echo "$@"; }
19 if [ "$(id -u)" -ne 0 ]; then
20 echo >&2 "ERROR: This command can only be used by root."
26 # command -v "$1" >/dev/null 2>&1 # not required by policy, see #747320
27 # which "$1" >/dev/null 2>&1 # is in debianutils (essential) but not on non-debian systems
31 if [ -x "${p}/${1}" ]; then
41 echo "$@" | sed -e "s#'#'\"'\"'#g"
44 get_fingerprints_of_keyring
() {
45 aptkey_execute
"$GPG_SH" --keyring "$1" --with-colons --fingerprint | while read publine
; do
46 # search for a public key
47 if [ "${publine%%:*}" != 'pub' ]; then continue; fi
48 # search for the associated fingerprint (should be the very next line)
49 while read fprline
; do
50 if [ "${fprline%%:*}" = 'sub' ]; then break; # should never happen
51 elif [ "${fprline%%:*}" != 'fpr' ]; then continue; fi
52 echo "$fprline" | cut
-d':' -f 10
54 # order in the keyring shouldn't be important
58 add_keys_with_verify_against_master_keyring
() {
62 if [ ! -f "$ADD_KEYRING" ]; then
63 echo >&2 "ERROR: '$ADD_KEYRING' not found"
66 if [ ! -f "$MASTER" ]; then
67 echo >&2 "ERROR: '$MASTER' not found"
71 # when adding new keys, make sure that the archive-master-keyring
73 # all keys that are exported must have a valid signature
74 # from a key in the $distro-master-keyring
75 add_keys
="$(get_fingerprints_of_keyring "$ADD_KEYRING")"
76 all_add_keys
="$(aptkey_execute "$GPG_SH" --keyring "$ADD_KEYRING" --with-colons --list-keys | grep ^[ps]ub | cut -d: -f5)"
77 master_keys
="$(aptkey_execute "$GPG_SH" --keyring "$MASTER" --with-colons --list-keys | grep ^pub | cut -d: -f5)"
79 # ensure there are no colisions LP: #857472
80 for all_add_key
in $all_add_keys; do
81 for master_key
in $master_keys; do
82 if [ "$all_add_key" = "$master_key" ]; then
83 echo >&2 "Keyid collision for '$all_add_key' detected, operation aborted"
89 for add_key
in $add_keys; do
90 # export the add keyring one-by-one
91 local TMP_KEYRING
="${GPGHOMEDIR}/tmp-keyring.gpg"
92 aptkey_execute
"$GPG_SH" --batch --yes --keyring "$ADD_KEYRING" --output "$TMP_KEYRING" --export "$add_key"
93 if ! aptkey_execute
"$GPG_SH" --batch --yes --keyring "$TMP_KEYRING" --import "$MASTER" > "${GPGHOMEDIR}/gpgoutput.log" 2>&1; then
94 cat >&2 "${GPGHOMEDIR}/gpgoutput.log"
97 # check if signed with the master key and only add in this case
99 for master_key
in $master_keys; do
100 if aptkey_execute
"$GPG_SH" --keyring "$TMP_KEYRING" --check-sigs --with-colons "$add_key" \
101 | grep '^sig:!:' | cut
-d: -f5 | grep -q "$master_key"; then
102 aptkey_execute
"$GPG_SH" --batch --yes --keyring "$ADD_KEYRING" --export "$add_key" \
103 | aptkey_execute
"$GPG" --batch --yes --import
107 if [ $ADDED = 0 ]; then
108 echo >&2 "Key '$add_key' not added. It is not signed with a master key"
110 rm -f "${TMP_KEYRING}"
114 # update the current archive signing keyring from a network URI
115 # the archive-keyring keys needs to be signed with the master key
116 # (otherwise it does not make sense from a security POV)
119 eval $(apt-config shell APT_DIR Dir)
121 # Disabled for now as code is insecure (LP: #1013639 (and 857472, 1013128))
122 APT_KEY_NET_UPDATE_ENABLED
=""
123 eval $(apt-config shell APT_KEY_NET_UPDATE_ENABLED APT::Key::Net-Update-Enabled)
124 if [ -z "$APT_KEY_NET_UPDATE_ENABLED" ]; then
128 if [ -z "$ARCHIVE_KEYRING_URI" ]; then
129 echo >&2 "ERROR: Your distribution is not supported in net-update as no uri for the archive-keyring is set"
132 # in theory we would need to depend on wget for this, but this feature
133 # isn't useable in debian anyway as we have no keyring uri nor a master key
134 if ! command_available
'wget'; then
135 echo >&2 "ERROR: an installed wget is required for a network-based update"
138 if [ ! -d "${APT_DIR}/var/lib/apt/keyrings" ]; then
139 mkdir -p "${APT_DIR}/var/lib/apt/keyrings"
141 keyring
="${APT_DIR}/var/lib/apt/keyrings/$(basename "$ARCHIVE_KEYRING_URI")"
143 if [ -e $keyring ]; then
144 old_mtime
=$(stat -c %Y "$keyring")
146 (cd "${APT_DIR}/var/lib/apt/keyrings"; wget
--timeout=90 -q -N "$ARCHIVE_KEYRING_URI")
147 if [ ! -e "$keyring" ]; then
150 new_mtime
=$(stat -c %Y "$keyring")
151 if [ $new_mtime -ne $old_mtime ]; then
152 aptkey_echo
"Checking for new archive signing keys now"
153 add_keys_with_verify_against_master_keyring
"$keyring" "$MASTER_KEYRING"
158 if [ ! -f "$ARCHIVE_KEYRING" ]; then
159 echo >&2 "ERROR: Can't find the archive-keyring"
160 echo >&2 "Is the &keyring-package; package installed?"
164 # add new keys from the package;
166 # we do not use add_keys_with_verify_against_master_keyring here,
167 # because "update" is run on regular package updates. A
168 # attacker might as well replace the master-archive-keyring file
169 # in the package and add his own keys. so this check wouldn't
170 # add any security. we *need* this check on net-update though
171 import_keyring_into_keyring
"$ARCHIVE_KEYRING" '' && cat "${GPGHOMEDIR}/gpgoutput.log"
173 if [ -r "$REMOVED_KEYS" ]; then
174 # remove no-longer supported/used keys
175 get_fingerprints_of_keyring
"$REMOVED_KEYS" | while read key
; do
176 foreach_keyring_do
'remove_key_from_keyring' "$key"
179 echo >&2 "Warning: removed keys keyring $REMOVED_KEYS missing or not readable"
183 remove_key_from_keyring
() {
184 local KEYRINGFILE
="$1"
186 # non-existent keyrings have by definition no keys
187 if [ ! -e "$KEYRINGFILE" ]; then
192 local FINGERPRINTS
="${GPGHOMEDIR}/keyringfile.keylst"
193 get_fingerprints_of_keyring
"$KEYRINGFILE" > "$FINGERPRINTS"
195 # strip leading 0x, if present:
198 # check if the key is in this keyring
199 if ! grep -iq "^[0-9A-F]*${KEY}$" "$FINGERPRINTS"; then
202 if [ ! -w "$KEYRINGFILE" ]; then
203 echo >&2 "Key ${KEY} is in keyring ${KEYRINGFILE}, but can't be removed as it is read only."
206 # check if it is the only key in the keyring and if so remove the keyring altogether
207 if [ '1' = "$(uniq "$FINGERPRINTS" | wc -l)" ]; then
208 mv -f "$KEYRINGFILE" "${KEYRINGFILE}~" # behave like gpg
211 # we can't just modify pointed to files as these might be in /usr or something
213 if [ -L "$KEYRINGFILE" ]; then
214 REALTARGET
="$(readlink -f "$KEYRINGFILE")"
215 mv -f "$KEYRINGFILE" "${KEYRINGFILE}.dpkg-tmp"
216 cp -a "$REALTARGET" "$KEYRINGFILE"
218 # delete the key from the keyring
219 aptkey_execute
"$GPG_SH" --keyring "$KEYRINGFILE" --batch --delete-keys --yes "$KEY"
220 if [ -n "$REALTARGET" ]; then
221 # the real backup is the old link, not the copy we made
222 mv -f "${KEYRINGFILE}.dpkg-tmp" "${KEYRINGFILE}~"
227 accessible_file_exists
() {
228 if ! test -s "$1"; then
231 if test -r "$1"; then
234 warn
"The key(s) in the keyring $1 are ignored as the file is not readable by user '$USER' executing apt-key."
238 foreach_keyring_do
() {
241 # if a --keyring was given, just work on this one
242 if [ -n "$FORCED_KEYRING" ]; then
243 $ACTION "$FORCED_KEYRING" "$@"
245 # otherwise all known keyrings are up for inspection
246 if accessible_file_exists
"$TRUSTEDFILE"; then
247 $ACTION "$TRUSTEDFILE" "$@"
249 local TRUSTEDPARTS
="/etc/apt/trusted.gpg.d"
250 eval "$(apt-config shell TRUSTEDPARTS Dir::Etc::TrustedParts/d)"
251 if [ -d "$TRUSTEDPARTS" ]; then
252 TRUSTEDPARTS
="$(readlink -f "$TRUSTEDPARTS")"
253 local TRUSTEDPARTSLIST
="$(cd /; find "$TRUSTEDPARTS" -mindepth 1 -maxdepth 1 -name '*.gpg')"
254 for trusted
in $(echo "$TRUSTEDPARTSLIST" | sort); do
255 if accessible_file_exists
"$trusted"; then
256 $ACTION "$trusted" "$@"
263 run_cmd_on_keyring
() {
264 local KEYRINGFILE
="$1"
266 # fingerprint and co will fail if key isn't in this keyring
267 aptkey_execute
"$GPG_SH" --keyring "$KEYRINGFILE" --batch "$@" 2>/dev
/null
|| true
270 import_keyring_into_keyring
() {
271 local FROM
="${1:-${GPGHOMEDIR}/pubring.gpg}"
272 local TO
="${2:-${GPGHOMEDIR}/pubring.gpg}"
274 rm -f "${GPGHOMEDIR}/gpgoutput.log"
275 # the idea is simple: We take keys from one keyring and copy it to another
276 # we do this with so many checks in between to ensure that WE control the
277 # creation, so we know that the (potentially) created $TO keyring is a
278 # simple keyring rather than a keybox as gpg2 would create it which in turn
279 # can't be read by gpgv.
280 # BEWARE: This is designed more in the way to work with the current
281 # callers, than to have a well defined it would be easy to add new callers to.
282 if [ ! -s "$TO" ]; then
283 if [ -s "$FROM" ]; then
285 if ! aptkey_execute
"$GPG_SH" --keyring "$FROM" --export ${1:+"$1"} > "$TO" 2> "${GPGHOMEDIR}/gpgoutput.log"; then
286 cat >&2 "${GPGHOMEDIR}/gpgoutput.log"
292 create_new_keyring
"$TO"
295 create_new_keyring
"$TO"
297 elif [ -s "$FROM" ]; then
298 local EXPORTLIMIT
="$1"
299 if [ -n "$1$2" ]; then shift; fi
300 if ! aptkey_execute
"$GPG_SH" --keyring "$FROM" --export ${EXPORTLIMIT:+"$EXPORTLIMIT"} \
301 | aptkey_execute
"$GPG_SH" --keyring "$TO" --batch --import "$@" > "${GPGHOMEDIR}/gpgoutput.log" 2>&1; then
302 cat >&2 "${GPGHOMEDIR}/gpgoutput.log"
312 merge_all_trusted_keyrings_into_pubring
() {
314 # foreach_keyring_do 'import_keys_from_keyring' "${GPGHOMEDIR}/pubring.gpg"
315 # but without using gpg, just cat and find
316 local PUBRING
="$(readlink -f "${GPGHOMEDIR}/pubring.gpg")"
319 foreach_keyring_do
'catfile' "$PUBRING"
322 import_keys_from_keyring
() {
323 import_keyring_into_keyring
"$1" "$2"
326 merge_keys_into_keyrings
() {
327 import_keyring_into_keyring
"$2" "$1" '' --import-options 'merge-only'
330 merge_back_changes
() {
331 if [ -n "$FORCED_KEYRING" ]; then
332 # if the keyring was forced merge is already done
335 if [ -s "${GPGHOMEDIR}/pubring.gpg" ]; then
336 # merge all updated keys
337 foreach_keyring_do
'merge_keys_into_keyrings' "${GPGHOMEDIR}/pubring.gpg"
339 # look for keys which were added or removed
340 get_fingerprints_of_keyring
"${GPGHOMEDIR}/pubring.orig.gpg" > "${GPGHOMEDIR}/pubring.orig.keylst"
341 get_fingerprints_of_keyring
"${GPGHOMEDIR}/pubring.gpg" > "${GPGHOMEDIR}/pubring.keylst"
342 comm -3 "${GPGHOMEDIR}/pubring.keylst" "${GPGHOMEDIR}/pubring.orig.keylst" > "${GPGHOMEDIR}/pubring.diff"
343 # key isn't part of new keyring, so remove
344 cut
-f 2 "${GPGHOMEDIR}/pubring.diff" | while read key
; do
345 if [ -z "$key" ]; then continue; fi
346 foreach_keyring_do
'remove_key_from_keyring' "$key"
348 # key is only part of new keyring, so we need to import it
349 cut
-f 1 "${GPGHOMEDIR}/pubring.diff" | while read key
; do
350 if [ -z "$key" ]; then continue; fi
351 import_keyring_into_keyring
'' "$TRUSTEDFILE" "$key"
355 setup_merged_keyring
() {
356 if [ -n "$FORCED_KEYID" ]; then
357 merge_all_trusted_keyrings_into_pubring
358 FORCED_KEYRING
="${GPGHOMEDIR}/forcedkeyid.gpg"
359 TRUSTEDFILE
="${FORCED_KEYRING}"
361 exec sh '($(escape_shell "${GPG}")' --keyring '$(escape_shell "${TRUSTEDFILE}")' \"\$@\"" > "${GPGHOMEDIR}/gpg.1.sh"
362 GPG
="${GPGHOMEDIR}/gpg.1.sh"
363 # ignore error as this "just" means we haven't found the forced keyid and the keyring will be empty
364 import_keyring_into_keyring
'' "$TRUSTEDFILE" "$FORCED_KEYID" || true
365 elif [ -z "$FORCED_KEYRING" ]; then
366 merge_all_trusted_keyrings_into_pubring
367 if [ -r "${GPGHOMEDIR}/pubring.gpg" ]; then
368 cp -a "${GPGHOMEDIR}/pubring.gpg" "${GPGHOMEDIR}/pubring.orig.gpg"
370 touch "${GPGHOMEDIR}/pubring.gpg" "${GPGHOMEDIR}/pubring.orig.gpg"
373 exec sh '$(escape_shell "${GPG}")' --keyring '$(escape_shell "${GPGHOMEDIR}/pubring.gpg")' \"\$@\"" > "${GPGHOMEDIR}/gpg.1.sh"
374 GPG
="${GPGHOMEDIR}/gpg.1.sh"
376 create_new_keyring
"$TRUSTEDFILE"
378 exec sh '$(escape_shell "${GPG}")' --keyring '$(escape_shell "${TRUSTEDFILE}")' \"\$@\"" > "${GPGHOMEDIR}/gpg.1.sh"
379 GPG
="${GPGHOMEDIR}/gpg.1.sh"
383 create_new_keyring
() {
384 # gpg defaults to mode 0600 for new keyrings. Create one with 0644 instead.
385 if ! [ -e "$TRUSTEDFILE" ]; then
386 if [ -w "$(dirname "$TRUSTEDFILE")" ]; then
387 touch -- "$TRUSTEDFILE"
388 chmod 0644 -- "$TRUSTEDFILE"
393 aptkey_execute
() { sh
"$@"; }
396 echo "Usage: apt-key [--keyring file] [command] [arguments]"
398 echo "Manage apt's list of trusted keys"
400 echo " apt-key add <file> - add the key contained in <file> ('-' for stdin)"
401 echo " apt-key del <keyid> - remove the key <keyid>"
402 echo " apt-key export <keyid> - output the key <keyid>"
403 echo " apt-key exportall - output all trusted keys"
404 echo " apt-key update - update keys using the keyring package"
405 echo " apt-key net-update - update keys using the network"
406 echo " apt-key list - list keys"
407 echo " apt-key finger - list fingerprints"
408 echo " apt-key adv - pass advanced options to gpg (download key)"
410 echo "If no specific keyring file is given the command applies to all keyring files."
413 while [ -n "$1" ]; do
426 FORCED_SECRET_KEYRING
="$1"
429 merge_back_changes
() { true
; }
430 create_new_keyring
() { if [ ! -r "$FORCED_KEYRING" ]; then TRUSTEDFILE
='/dev/null'; FORCED_KEYRING
="$TRUSTEDFILE"; fi; }
433 requires_root
() { true
; }
436 aptkey_echo
() { true
; }
439 # some cmds like finger redirect stderr to /dev/null …
440 aptkey_execute
() { echo 'EXEC:' "$@"; sh
"$@"; }
443 # … other more complicated ones pipe gpg into gpg.
444 aptkey_execute
() { echo >&2 'EXEC:' "$@"; sh
"$@"; }
447 echo >&2 "Unknown option: $1"
456 if [ -z "$TRUSTEDFILE" ]; then
457 TRUSTEDFILE
="/etc/apt/trusted.gpg"
458 eval $(apt-config shell TRUSTEDFILE Apt::GPGV::TrustedKeyring)
459 eval $(apt-config shell TRUSTEDFILE Dir::Etc::Trusted/f)
463 if [ -z "$command" ]; then
469 find_gpgv_status_fd
() {
470 while [ -n "$1" ]; do
471 if [ "$1" = '--status-fd' ]; then
479 GPGSTATUSFD
="$(find_gpgv_status_fd "$@")"
483 if [ -n "$GPGSTATUSFD" ]; then
484 echo >&${GPGSTATUSFD} '[APTKEY:] WARNING' "$@"
489 # gpg needs (in different versions more or less) files to function correctly,
490 # so we give it its own homedir and generate some valid content for it later on
491 if [ -n "$TMPDIR" ]; then
492 # tmpdir is a directory and current user has rwx access to it
493 # same tests as in apt-pkg/contrib/fileutl.cc GetTempDir()
494 if [ ! -d "$TMPDIR" ] || [ ! -r "$TMPDIR" ] || [ ! -w "$TMPDIR" ] || [ ! -x "$TMPDIR" ]; then
498 GPGHOMEDIR
="$(mktemp -d)"
499 CURRENTTRAP
="${CURRENTTRAP} rm -rf '$(escape_shell "${GPGHOMEDIR}")';"
500 trap "${CURRENTTRAP}" 0 HUP INT QUIT ILL ABRT FPE SEGV PIPE TERM
501 chmod 700 "$GPGHOMEDIR"
505 eval "$(apt-config shell GPG_EXE Apt::Key::gpgcommand)"
507 if [ -n "$GPG_EXE" ] && command_available
"$GPG_EXE"; then
509 elif command_available
'gpg'; then
511 elif command_available
'gpg2'; then
514 echo >&2 "Error: gnupg or gnupg2 do not seem to be installed,"
515 echo >&2 "Error: but apt-key requires gnupg or gnupg2 for this operation."
522 # create the trustdb with an (empty) dummy keyring
523 # older gpgs required it, newer gpgs even warn that it isn't needed,
524 # but require it nonetheless for some commands, so we just play safe
525 # here for the foreseeable future and create a dummy one
526 touch "${GPGHOMEDIR}/empty.gpg"
527 if ! "$GPG_EXE" --ignore-time-conflict --no-options --no-default-keyring \
528 --homedir "$GPGHOMEDIR" --quiet --check-trustdb --keyring "${GPGHOMEDIR}/empty.gpg" >"${GPGHOMEDIR}/gpgoutput.log" 2>&1; then
529 cat >&2 "${GPGHOMEDIR}/gpgoutput.log"
533 # now tell gpg that it shouldn't try to maintain this trustdb file
535 exec '$(escape_shell "${GPG_EXE}")' --ignore-time-conflict --no-options --no-default-keyring \\
536 --homedir '$(escape_shell "${GPGHOMEDIR}")' --no-auto-check-trustdb --trust-model always \"\$@\"" > "${GPGHOMEDIR}/gpg.0.sh"
537 GPG_SH
="${GPGHOMEDIR}/gpg.0.sh"
540 # We don't usually need a secret keyring, of course, but
541 # for advanced operations, we might really need a secret keyring after all
542 if [ -n "$FORCED_SECRET_KEYRING" ] && [ -r "$FORCED_SECRET_KEYRING" ]; then
543 if ! aptkey_execute
"$GPG" -v --batch --import "$FORCED_SECRET_KEYRING" >"${GPGHOMEDIR}/gpgoutput.log" 2>&1; then
544 cat >&2 "${GPGHOMEDIR}/gpgoutput.log"
548 # and then, there are older versions of gpg which panic and implode
549 # if there isn't one available - and writeable for imports
550 # and even if not output is littered with the creation of a secring,
551 # so lets call import once to have it create what it wants in silence
552 echo -n | aptkey_execute
"$GPG" --batch --import >/dev
/null
2>&1 || true
556 if [ "$command" != 'help' ] && [ "$command" != 'verify' ]; then
564 aptkey_execute
"$GPG" --quiet --batch --import "$@"
570 foreach_keyring_do
'remove_key_from_keyring' "$@"
586 foreach_keyring_do
'run_cmd_on_keyring' --list-keys "$@"
589 foreach_keyring_do
'run_cmd_on_keyring' --fingerprint "$@"
592 merge_all_trusted_keyrings_into_pubring
593 aptkey_execute
"$GPG_SH" --keyring "${GPGHOMEDIR}/pubring.gpg" --armor --export "$@"
597 aptkey_echo
"Executing: $GPG $*"
598 aptkey_execute
"$GPG" "$@"
603 eval $(apt-config shell GPGV Apt::Key::gpgvcommand)
604 if [ -n "$GPGV" ] && command_available
"$GPGV"; then true
;
605 elif command_available
'gpgv'; then GPGV
='gpgv';
606 elif command_available
'gpgv2'; then GPGV
='gpgv2';
608 echo >&2 'ERROR: gpgv or gpgv2 required for verification'
611 # for a forced keyid we need gpg --export, so full wrapping required
612 if [ -n "$FORCED_KEYID" ]; then
618 if [ -n "$FORCED_KEYRING" ]; then
619 "$GPGV" --homedir "${GPGHOMEDIR}" --keyring "${FORCED_KEYRING}" --ignore-time-conflict "$@"
621 "$GPGV" --homedir "${GPGHOMEDIR}" --keyring "${GPGHOMEDIR}/pubring.gpg" --ignore-time-conflict "$@"