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 foreach_keyring_do
() {
230 # if a --keyring was given, just work on this one
231 if [ -n "$FORCED_KEYRING" ]; then
232 $ACTION "$FORCED_KEYRING" "$@"
234 # otherwise all known keyrings are up for inspection
235 if [ -s "$TRUSTEDFILE" ]; then
236 $ACTION "$TRUSTEDFILE" "$@"
238 local TRUSTEDPARTS
="/etc/apt/trusted.gpg.d"
239 eval "$(apt-config shell TRUSTEDPARTS Dir::Etc::TrustedParts/d)"
240 if [ -d "$TRUSTEDPARTS" ]; then
241 # strip / suffix as gpg will double-slash in that case (#665411)
242 local STRIPPED_TRUSTEDPARTS
="${TRUSTEDPARTS%/}"
243 if [ "${STRIPPED_TRUSTEDPARTS}/" = "$TRUSTEDPARTS" ]; then
244 TRUSTEDPARTS
="$STRIPPED_TRUSTEDPARTS"
246 for trusted
in $(find "$TRUSTEDPARTS" -mindepth 1 -maxdepth 1 -regex '^.*\.gpg$' | sort); do
247 if [ -s "$trusted" ]; then
248 $ACTION "$trusted" "$@"
255 run_cmd_on_keyring
() {
256 local KEYRINGFILE
="$1"
258 # fingerprint and co will fail if key isn't in this keyring
259 aptkey_execute
"$GPG_SH" --keyring "$KEYRINGFILE" --batch "$@" 2>/dev
/null
|| true
262 import_keyring_into_keyring
() {
263 local FROM
="${1:-${GPGHOMEDIR}/pubring.gpg}"
264 local TO
="${2:-${GPGHOMEDIR}/pubring.gpg}"
266 rm -f "${GPGHOMEDIR}/gpgoutput.log"
267 # the idea is simple: We take keys from one keyring and copy it to another
268 # we do this with so many checks in between to ensure that WE control the
269 # creation, so we know that the (potentially) created $TO keyring is a
270 # simple keyring rather than a keybox as gpg2 would create it which in turn
271 # can't be read by gpgv.
272 # BEWARE: This is designed more in the way to work with the current
273 # callers, than to have a well defined it would be easy to add new callers to.
274 if [ ! -s "$TO" ]; then
275 if [ -s "$FROM" ]; then
277 if ! aptkey_execute
"$GPG_SH" --keyring "$FROM" --export ${1:+"$1"} > "$TO" 2> "${GPGHOMEDIR}/gpgoutput.log"; then
278 cat >&2 "${GPGHOMEDIR}/gpgoutput.log"
284 create_new_keyring
"$TO"
287 create_new_keyring
"$TO"
289 elif [ -s "$FROM" ]; then
290 local EXPORTLIMIT
="$1"
291 if [ -n "$1$2" ]; then shift; fi
292 if ! aptkey_execute
"$GPG_SH" --keyring "$FROM" --export ${EXPORTLIMIT:+"$EXPORTLIMIT"} \
293 | aptkey_execute
"$GPG_SH" --keyring "$TO" --batch --import "$@" > "${GPGHOMEDIR}/gpgoutput.log" 2>&1; then
294 cat >&2 "${GPGHOMEDIR}/gpgoutput.log"
300 merge_all_trusted_keyrings_into_pubring
() {
302 # foreach_keyring_do 'import_keys_from_keyring' "${GPGHOMEDIR}/pubring.gpg"
303 # but without using gpg, just cat and find
304 local PUBRING
="${GPGHOMEDIR}/pubring.gpg"
305 # if a --keyring was given, just use this one
306 if [ -n "$FORCED_KEYRING" ]; then
307 if [ -s "$FORCED_KEYRING" ]; then
308 cp --dereference "$FORCED_KEYRING" "$PUBRING"
311 # otherwise all known keyrings are merged
312 local TRUSTEDPARTS
="/etc/apt/trusted.gpg.d"
313 eval $(apt-config shell TRUSTEDPARTS Dir::Etc::TrustedParts/d)
314 if [ -d "$TRUSTEDPARTS" ]; then
315 # ignore errors mostly for non-existing $TRUSTEDFILE
317 cat "$TRUSTEDFILE" || true
318 for parts
in $(find -L "$TRUSTEDPARTS" -type f -name '*.gpg'); do
321 } > "$PUBRING" 2>/dev
/null
322 elif [ -s "$TRUSTEDFILE" ]; then
323 cp --dereference "$TRUSTEDFILE" "$PUBRING"
327 if [ ! -s "$PUBRING" ]; then
332 import_keys_from_keyring
() {
333 import_keyring_into_keyring
"$1" "$2"
336 merge_keys_into_keyrings
() {
337 import_keyring_into_keyring
"$2" "$1" '' --import-options 'merge-only'
340 merge_back_changes
() {
341 if [ -n "$FORCED_KEYRING" ]; then
342 # if the keyring was forced merge is already done
345 if [ -s "${GPGHOMEDIR}/pubring.gpg" ]; then
346 # merge all updated keys
347 foreach_keyring_do
'merge_keys_into_keyrings' "${GPGHOMEDIR}/pubring.gpg"
349 # look for keys which were added or removed
350 get_fingerprints_of_keyring
"${GPGHOMEDIR}/pubring.orig.gpg" > "${GPGHOMEDIR}/pubring.orig.keylst"
351 get_fingerprints_of_keyring
"${GPGHOMEDIR}/pubring.gpg" > "${GPGHOMEDIR}/pubring.keylst"
352 comm -3 "${GPGHOMEDIR}/pubring.keylst" "${GPGHOMEDIR}/pubring.orig.keylst" > "${GPGHOMEDIR}/pubring.diff"
353 # key isn't part of new keyring, so remove
354 cut
-f 2 "${GPGHOMEDIR}/pubring.diff" | while read key
; do
355 if [ -z "$key" ]; then continue; fi
356 foreach_keyring_do
'remove_key_from_keyring' "$key"
358 # key is only part of new keyring, so we need to import it
359 cut
-f 1 "${GPGHOMEDIR}/pubring.diff" | while read key
; do
360 if [ -z "$key" ]; then continue; fi
361 import_keyring_into_keyring
'' "$TRUSTEDFILE" "$key"
365 setup_merged_keyring
() {
366 if [ -n "$FORCED_KEYID" ]; then
367 merge_all_trusted_keyrings_into_pubring
368 FORCED_KEYRING
="${GPGHOMEDIR}/forcedkeyid.gpg"
369 TRUSTEDFILE
="${FORCED_KEYRING}"
371 exec sh '($(escape_shell "${GPG}")' --keyring '$(escape_shell "${TRUSTEDFILE}")' \"\$@\"" > "${GPGHOMEDIR}/gpg.1.sh"
372 GPG
="${GPGHOMEDIR}/gpg.1.sh"
373 # ignore error as this "just" means we haven't found the forced keyid and the keyring will be empty
374 import_keyring_into_keyring
'' "$TRUSTEDFILE" "$FORCED_KEYID" || true
375 elif [ -z "$FORCED_KEYRING" ]; then
376 merge_all_trusted_keyrings_into_pubring
377 if [ -r "${GPGHOMEDIR}/pubring.gpg" ]; then
378 cp -a "${GPGHOMEDIR}/pubring.gpg" "${GPGHOMEDIR}/pubring.orig.gpg"
380 touch "${GPGHOMEDIR}/pubring.gpg" "${GPGHOMEDIR}/pubring.orig.gpg"
383 exec sh '$(escape_shell "${GPG}")' --keyring '$(escape_shell "${GPGHOMEDIR}/pubring.gpg")' \"\$@\"" > "${GPGHOMEDIR}/gpg.1.sh"
384 GPG
="${GPGHOMEDIR}/gpg.1.sh"
386 create_new_keyring
"$TRUSTEDFILE"
388 exec sh '$(escape_shell "${GPG}")' --keyring '$(escape_shell "${TRUSTEDFILE}")' \"\$@\"" > "${GPGHOMEDIR}/gpg.1.sh"
389 GPG
="${GPGHOMEDIR}/gpg.1.sh"
393 create_new_keyring
() {
394 # gpg defaults to mode 0600 for new keyrings. Create one with 0644 instead.
395 if ! [ -e "$TRUSTEDFILE" ]; then
396 if [ -w "$(dirname "$TRUSTEDFILE")" ]; then
397 touch -- "$TRUSTEDFILE"
398 chmod 0644 -- "$TRUSTEDFILE"
403 aptkey_execute
() { sh
"$@"; }
406 echo "Usage: apt-key [--keyring file] [command] [arguments]"
408 echo "Manage apt's list of trusted keys"
410 echo " apt-key add <file> - add the key contained in <file> ('-' for stdin)"
411 echo " apt-key del <keyid> - remove the key <keyid>"
412 echo " apt-key export <keyid> - output the key <keyid>"
413 echo " apt-key exportall - output all trusted keys"
414 echo " apt-key update - update keys using the keyring package"
415 echo " apt-key net-update - update keys using the network"
416 echo " apt-key list - list keys"
417 echo " apt-key finger - list fingerprints"
418 echo " apt-key adv - pass advanced options to gpg (download key)"
420 echo "If no specific keyring file is given the command applies to all keyring files."
423 while [ -n "$1" ]; do
436 FORCED_SECRET_KEYRING
="$1"
439 merge_back_changes
() { true
; }
440 create_new_keyring
() { if [ ! -r "$FORCED_KEYRING" ]; then TRUSTEDFILE
='/dev/null'; FORCED_KEYRING
="$TRUSTEDFILE"; fi; }
443 requires_root
() { true
; }
446 aptkey_echo
() { true
; }
449 # some cmds like finger redirect stderr to /dev/null …
450 aptkey_execute
() { echo 'EXEC:' "$@"; sh
"$@"; }
453 # … other more complicated ones pipe gpg into gpg.
454 aptkey_execute
() { echo >&2 'EXEC:' "$@"; sh
"$@"; }
457 echo >&2 "Unknown option: $1"
466 if [ -z "$TRUSTEDFILE" ]; then
467 TRUSTEDFILE
="/etc/apt/trusted.gpg"
468 eval $(apt-config shell TRUSTEDFILE Apt::GPGV::TrustedKeyring)
469 eval $(apt-config shell TRUSTEDFILE Dir::Etc::Trusted/f)
473 if [ -z "$command" ]; then
480 # gpg needs (in different versions more or less) files to function correctly,
481 # so we give it its own homedir and generate some valid content for it later on
482 if [ -n "$TMPDIR" ]; then
483 # tmpdir is a directory and current user has rwx access to it
484 # same tests as in apt-pkg/contrib/fileutl.cc GetTempDir()
485 if [ ! -d "$TMPDIR" ] || [ ! -r "$TMPDIR" ] || [ ! -w "$TMPDIR" ] || [ ! -x "$TMPDIR" ]; then
489 GPGHOMEDIR
="$(mktemp -d)"
490 CURRENTTRAP
="${CURRENTTRAP} rm -rf '$(escape_shell "${GPGHOMEDIR}")';"
491 trap "${CURRENTTRAP}" 0 HUP INT QUIT ILL ABRT FPE SEGV PIPE TERM
492 chmod 700 "$GPGHOMEDIR"
496 eval "$(apt-config shell GPG_EXE Apt::Key::gpgcommand)"
498 if [ -n "$GPG_EXE" ] && command_available
"$GPG_EXE"; then
500 elif command_available
'gpg'; then
502 elif command_available
'gpg2'; then
505 echo >&2 "Error: gnupg or gnupg2 do not seem to be installed,"
506 echo >&2 "Error: but apt-key requires gnupg or gnupg2 for this operation."
513 # create the trustdb with an (empty) dummy keyring
514 # older gpgs required it, newer gpgs even warn that it isn't needed,
515 # but require it nonetheless for some commands, so we just play safe
516 # here for the foreseeable future and create a dummy one
517 touch "${GPGHOMEDIR}/empty.gpg"
518 if ! "$GPG_EXE" --ignore-time-conflict --no-options --no-default-keyring \
519 --homedir "$GPGHOMEDIR" --quiet --check-trustdb --keyring "${GPGHOMEDIR}/empty.gpg" >"${GPGHOMEDIR}/gpgoutput.log" 2>&1; then
520 cat >&2 "${GPGHOMEDIR}/gpgoutput.log"
524 # now tell gpg that it shouldn't try to maintain this trustdb file
526 exec '$(escape_shell "${GPG_EXE}")' --ignore-time-conflict --no-options --no-default-keyring \\
527 --homedir '$(escape_shell "${GPGHOMEDIR}")' --no-auto-check-trustdb --trust-model always \"\$@\"" > "${GPGHOMEDIR}/gpg.0.sh"
528 GPG_SH
="${GPGHOMEDIR}/gpg.0.sh"
531 # We don't usually need a secret keyring, of course, but
532 # for advanced operations, we might really need a secret keyring after all
533 if [ -n "$FORCED_SECRET_KEYRING" ] && [ -r "$FORCED_SECRET_KEYRING" ]; then
534 if ! aptkey_execute
"$GPG" -v --batch --import "$FORCED_SECRET_KEYRING" >"${GPGHOMEDIR}/gpgoutput.log" 2>&1; then
535 cat >&2 "${GPGHOMEDIR}/gpgoutput.log"
539 # and then, there are older versions of gpg which panic and implode
540 # if there isn't one available - and writeable for imports
541 # and even if not output is littered with the creation of a secring,
542 # so lets call import once to have it create what it wants in silence
543 echo -n | aptkey_execute
"$GPG" --batch --import >/dev
/null
2>&1 || true
547 if [ "$command" != 'help' ] && [ "$command" != 'verify' ]; then
555 aptkey_execute
"$GPG" --quiet --batch --import "$@"
561 foreach_keyring_do
'remove_key_from_keyring' "$@"
577 foreach_keyring_do
'run_cmd_on_keyring' --list-keys "$@"
580 foreach_keyring_do
'run_cmd_on_keyring' --fingerprint "$@"
583 merge_all_trusted_keyrings_into_pubring
584 aptkey_execute
"$GPG_SH" --keyring "${GPGHOMEDIR}/pubring.gpg" --armor --export "$@"
588 aptkey_echo
"Executing: $GPG $*"
589 aptkey_execute
"$GPG" "$@"
594 eval $(apt-config shell GPGV Apt::Key::gpgvcommand)
595 if [ -n "$GPGV" ] && command_available
"$GPGV"; then true
;
596 elif command_available
'gpgv'; then GPGV
='gpgv';
597 elif command_available
'gpgv2'; then GPGV
='gpgv2';
599 echo >&2 'ERROR: gpgv or gpgv2 required for verification'
602 # for a forced keyid we need gpg --export, so full wrapping required
603 if [ -n "$FORCED_KEYID" ]; then
609 if [ -n "$FORCED_KEYRING" ]; then
610 "$GPGV" --homedir "${GPGHOMEDIR}" --keyring "${FORCED_KEYRING}" --ignore-time-conflict "$@"
612 "$GPGV" --homedir "${GPGHOMEDIR}" --keyring "${GPGHOMEDIR}/pubring.gpg" --ignore-time-conflict "$@"