7 eval $(apt-config shell APT_DIR Dir)
9 MASTER_KEYRING
='&keyring-master-filename;'
10 eval $(apt-config shell MASTER_KEYRING APT::Key::MasterKeyring)
11 ARCHIVE_KEYRING
='&keyring-filename;'
12 eval $(apt-config shell ARCHIVE_KEYRING APT::Key::ArchiveKeyring)
13 REMOVED_KEYS
='&keyring-removed-filename;'
14 eval $(apt-config shell REMOVED_KEYS APT::Key::RemovedKeys)
15 ARCHIVE_KEYRING_URI
='&keyring-uri;'
16 eval $(apt-config shell ARCHIVE_KEYRING_URI APT::Key::ArchiveKeyringURI)
17 TMP_KEYRING
=${APT_DIR}/var
/lib
/apt
/keyrings
/maybe
-import-keyring.gpg
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 $GPG_CMD --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
41 add_keys_with_verify_against_master_keyring
() {
45 if [ ! -f "$ADD_KEYRING" ]; then
46 echo >&2 "ERROR: '$ADD_KEYRING' not found"
49 if [ ! -f "$MASTER" ]; then
50 echo >&2 "ERROR: '$MASTER' not found"
54 # when adding new keys, make sure that the archive-master-keyring
56 # all keys that are exported must have a valid signature
57 # from a key in the $distro-master-keyring
58 add_keys
="$(get_fingerprints_of_keyring "$ADD_KEYRING")"
59 all_add_keys
=`$GPG_CMD --keyring $ADD_KEYRING --with-colons --list-keys | grep ^[ps]ub | cut -d: -f5`
60 master_keys
=`$GPG_CMD --keyring $MASTER --with-colons --list-keys | grep ^pub | cut -d: -f5`
62 # ensure there are no colisions LP: #857472
63 for all_add_key
in $all_add_keys; do
64 for master_key
in $master_keys; do
65 if [ "$all_add_key" = "$master_key" ]; then
66 echo >&2 "Keyid collision for '$all_add_key' detected, operation aborted"
72 for add_key
in $add_keys; do
73 # export the add keyring one-by-one
75 $GPG_CMD --keyring $ADD_KEYRING --output $TMP_KEYRING --export $add_key
76 # check if signed with the master key and only add in this case
78 for master_key
in $master_keys; do
79 if $GPG_CMD --keyring $MASTER --keyring $TMP_KEYRING --check-sigs --with-colons $add_key | grep '^sig:!:' | cut
-d: -f5 | grep -q $master_key; then
80 $GPG --import $TMP_KEYRING
84 if [ $ADDED = 0 ]; then
85 echo >&2 "Key '$add_key' not added. It is not signed with a master key"
91 # update the current archive signing keyring from a network URI
92 # the archive-keyring keys needs to be signed with the master key
93 # (otherwise it does not make sense from a security POV)
95 # Disabled for now as code is insecure (LP: #1013639 (and 857472, 1013128))
96 APT_KEY_NET_UPDATE_ENABLED
=""
97 eval $(apt-config shell APT_KEY_NET_UPDATE_ENABLED APT::Key::Net-Update-Enabled)
98 if [ -z "$APT_KEY_NET_UPDATE_ENABLED" ]; then
102 if [ -z "$ARCHIVE_KEYRING_URI" ]; then
103 echo >&2 "ERROR: Your distribution is not supported in net-update as no uri for the archive-keyring is set"
106 # in theory we would need to depend on wget for this, but this feature
107 # isn't useable in debian anyway as we have no keyring uri nor a master key
108 if ! which wget
>/dev
/null
2>&1; then
109 echo >&2 "ERROR: an installed wget is required for a network-based update"
112 if [ ! -d ${APT_DIR}/var
/lib
/apt
/keyrings
]; then
113 mkdir -p ${APT_DIR}/var
/lib
/apt
/keyrings
115 keyring
=${APT_DIR}/var
/lib
/apt
/keyrings
/$(basename $ARCHIVE_KEYRING_URI)
117 if [ -e $keyring ]; then
118 old_mtime
=$(stat -c %Y $keyring)
120 (cd ${APT_DIR}/var
/lib
/apt
/keyrings
; wget
--timeout=90 -q -N $ARCHIVE_KEYRING_URI)
121 if [ ! -e $keyring ]; then
124 new_mtime
=$(stat -c %Y $keyring)
125 if [ $new_mtime -ne $old_mtime ]; then
126 aptkey_echo
"Checking for new archive signing keys now"
127 add_keys_with_verify_against_master_keyring
$keyring $MASTER_KEYRING
132 if [ ! -f $ARCHIVE_KEYRING ]; then
133 echo >&2 "ERROR: Can't find the archive-keyring"
134 echo >&2 "Is the &keyring-package; package installed?"
138 # add new keys from the package;
140 # we do not use add_keys_with_verify_against_master_keyring here,
141 # because "update" is run on regular package updates. A
142 # attacker might as well replace the master-archive-keyring file
143 # in the package and add his own keys. so this check wouldn't
144 # add any security. we *need* this check on net-update though
145 $GPG_CMD --quiet --batch --keyring $ARCHIVE_KEYRING --export | $GPG --import
147 if [ -r "$REMOVED_KEYS" ]; then
148 # remove no-longer supported/used keys
149 get_fingerprints_of_keyring
"$REMOVED_KEYS" | while read key
; do
150 foreach_keyring_do
'remove_key_from_keyring' "$key"
153 echo >&2 "Warning: removed keys keyring $REMOVED_KEYS missing or not readable"
157 remove_key_from_keyring
() {
158 local KEYRINGFILE
="$1"
160 # non-existent keyrings have by definition no keys
161 if [ ! -e "$KEYRINGFILE" ]; then
165 local GPG
="$GPG_CMD --keyring $KEYRINGFILE"
166 while [ -n "$1" ]; do
169 # check if the key is in this keyring: the key id is in the 5 column at the end
170 if ! get_fingerprints_of_keyring
"$KEYRINGFILE" | grep -q "^[0-9A-F]*${KEY}$"; then
173 if [ ! -w "$KEYRINGFILE" ]; then
174 echo >&2 "Key ${KEY} is in keyring ${KEYRINGFILE}, but can't be removed as it is read only."
177 # check if it is the only key in the keyring and if so remove the keyring altogether
178 if [ '1' = "$(get_fingerprints_of_keyring "$KEYRINGFILE" | wc -l)" ]; then
179 mv -f "$KEYRINGFILE" "${KEYRINGFILE}~" # behave like gpg
182 # we can't just modify pointed to files as these might be in /usr or something
184 if [ -L "$KEYRINGFILE" ]; then
185 REALTARGET
="$(readlink -f "$KEYRINGFILE")"
186 mv -f "$KEYRINGFILE" "${KEYRINGFILE}.dpkg-tmp"
187 cp -a "$REALTARGET" "$KEYRINGFILE"
189 # delete the key from the keyring
190 $GPG --batch --delete-key --yes "$KEY"
191 if [ -n "$REALTARGET" ]; then
192 # the real backup is the old link, not the copy we made
193 mv -f "${KEYRINGFILE}.dpkg-tmp" "${KEYRINGFILE}~"
198 foreach_keyring_do
() {
201 # if a --keyring was given, just remove from there
202 if [ -n "$FORCED_KEYRING" ]; then
203 $ACTION "$FORCED_KEYRING" "$@"
205 # otherwise all known keyrings are up for inspection
206 if [ -s "$TRUSTEDFILE" ]; then
207 $ACTION "$TRUSTEDFILE" "$@"
209 local TRUSTEDPARTS
="/etc/apt/trusted.gpg.d"
210 eval $(apt-config shell TRUSTEDPARTS Dir::Etc::TrustedParts/d)
211 if [ -d "$TRUSTEDPARTS" ]; then
212 # strip / suffix as gpg will double-slash in that case (#665411)
213 local STRIPPED_TRUSTEDPARTS
="${TRUSTEDPARTS%/}"
214 if [ "${STRIPPED_TRUSTEDPARTS}/" = "$TRUSTEDPARTS" ]; then
215 TRUSTEDPARTS
="$STRIPPED_TRUSTEDPARTS"
217 for trusted
in $(run-parts --list "$TRUSTEDPARTS" --regex '^.*\.gpg$'); do
218 if [ -s "$trusted" ]; then
219 $ACTION "$trusted" "$@"
226 list_keys_from_keyring
() {
227 local KEYRINGFILE
="$1"
229 # don't show the error message if this keyring doesn't include the key
230 $GPG_CMD --keyring "$KEYRINGFILE" --batch --list-keys "$@" 2>/dev
/null
|| true
233 fingerprint_keys_from_keyring
() {
234 local KEYRINGFILE
="$1"
236 # don't show the error message if this keyring doesn't include the fingerprint
237 $GPG_CMD --keyring "$KEYRINGFILE" --batch --fingerprint "$@" 2>/dev
/null
|| true
240 import_keys_from_keyring
() {
242 local KEYRINGFILE
="$2"
243 $GPG_CMD --keyring "$KEYRINGFILE" --batch --import "$IMPORT" >/dev
/null
2>&1
246 setup_merged_keyring
() {
247 if [ -z "$FORCED_KEYRING" ]; then
248 local TRUSTEDFILE_BAK
="$TRUSTEDFILE"
249 TRUSTEDFILE
='/dev/null'
250 foreach_keyring_do
'import_keys_from_keyring' "${GPGHOMEDIR}/pubring.gpg"
251 TRUSTEDFILE
="$TRUSTEDFILE_BAK"
252 # mark it as non-writeable so users get errors if gnupg tries to modify it
253 if [ -s "${GPGHOMEDIR}/pubring.gpg" ]; then
254 chmod -w "${GPGHOMEDIR}/pubring.gpg"
255 GPG
="$GPG --keyring ${GPGHOMEDIR}/pubring.gpg"
258 if [ -r "$TRUSTEDFILE" ]; then
259 GPG
="$GPG --keyring $TRUSTEDFILE --primary-keyring $TRUSTEDFILE"
265 echo "Usage: apt-key [--keyring file] [command] [arguments]"
267 echo "Manage apt's list of trusted keys"
269 echo " apt-key add <file> - add the key contained in <file> ('-' for stdin)"
270 echo " apt-key del <keyid> - remove the key <keyid>"
271 echo " apt-key export <keyid> - output the key <keyid>"
272 echo " apt-key exportall - output all trusted keys"
273 echo " apt-key update - update keys using the keyring package"
274 echo " apt-key net-update - update keys using the network"
275 echo " apt-key list - list keys"
276 echo " apt-key finger - list fingerprints"
277 echo " apt-key adv - pass advanced options to gpg (download key)"
279 echo "If no specific keyring file is given the command applies to all keyring files."
282 while [ -n "$1" ]; do
291 requires_root
() { true
; }
295 aptkey_echo
() { true
; }
299 echo >&2 "Unknown option: $1"
307 if [ -z "$TRUSTEDFILE" ]; then
308 TRUSTEDFILE
="/etc/apt/trusted.gpg"
309 eval $(apt-config shell TRUSTEDFILE Apt::GPGV::TrustedKeyring)
310 eval $(apt-config shell TRUSTEDFILE Dir::Etc::Trusted/f)
314 if [ -z "$command" ]; then
320 if [ "$command" != "help" ]; then
321 eval $(apt-config shell GPG_EXE Apt::Key::gpgcommand)
323 if [ -n "$GPG_EXE" ] && which "$GPG_EXE" >/dev
/null
2>&1; then
325 elif which gpg
>/dev
/null
2>&1; then
327 elif which gpg2
>/dev
/null
2>&1; then
330 echo >&2 "Error: gnupg or gnupg2 do not seem to be installed,"
331 echo >&2 "Error: but apt-key requires gnupg or gnupg2 for operation."
336 GPG_CMD
="$GPG_EXE --ignore-time-conflict --no-options --no-default-keyring"
338 # gpg needs (in different versions more or less) files to function correctly,
339 # so we give it its own homedir and generate some valid content for it
340 GPGHOMEDIR
="$(mktemp -d)"
341 CURRENTTRAP
="${CURRENTTRAP} rm -rf '${GPGHOMEDIR}';"
342 trap "${CURRENTTRAP}" 0 HUP INT QUIT ILL ABRT FPE SEGV PIPE TERM
343 chmod 700 "$GPGHOMEDIR"
344 # We don't use a secret keyring, of course, but gpg panics and
345 # implodes if there isn't one available - and writeable for imports
346 SECRETKEYRING
="${GPGHOMEDIR}/secring.gpg"
348 GPG_CMD
="$GPG_CMD --homedir $GPGHOMEDIR"
349 # create the trustdb with an (empty) dummy keyring
350 # older gpgs required it, newer gpgs even warn that it isn't needed,
351 # but require it nonetheless for some commands, so we just play safe
352 # here for the foreseeable future and create a dummy one
353 $GPG_CMD --quiet --check-trustdb --keyring $SECRETKEYRING >/dev
/null
2>&1
354 # tell gpg that it shouldn't try to maintain a trustdb file
355 GPG_CMD
="$GPG_CMD --no-auto-check-trustdb --trust-model always"
358 # gpg defaults to mode 0600 for new keyrings. Create one with 0644 instead.
359 if ! [ -e "$TRUSTEDFILE" ]; then
360 if [ -w "$(dirname "$TRUSTEDFILE")" ]; then
361 touch -- "$TRUSTEDFILE"
362 chmod 0644 -- "$TRUSTEDFILE"
371 $GPG --quiet --batch --import "$@"
376 foreach_keyring_do
'remove_key_from_keyring' "$@"
390 foreach_keyring_do
'list_keys_from_keyring' "$@"
393 foreach_keyring_do
'fingerprint_keys_from_keyring' "$@"
396 foreach_keyring_do
'import_keys_from_keyring' "${GPGHOMEDIR}/pubring.gpg"
397 $GPG_CMD --keyring "${GPGHOMEDIR}/pubring.gpg" --armor --export "$@"
401 aptkey_echo
"Executing: $GPG $*"