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 add_keys_with_verify_against_master_keyring
() {
32 if [ ! -f "$ADD_KEYRING" ]; then
33 echo >&2 "ERROR: '$ADD_KEYRING' not found"
36 if [ ! -f "$MASTER" ]; then
37 echo >&2 "ERROR: '$MASTER' not found"
41 # when adding new keys, make sure that the archive-master-keyring
43 # all keys that are exported must have a valid signature
44 # from a key in the $distro-master-keyring
45 add_keys
=`$GPG_CMD --keyring $ADD_KEYRING --with-colons --list-keys | grep ^pub | cut -d: -f5`
46 all_add_keys
=`$GPG_CMD --keyring $ADD_KEYRING --with-colons --list-keys | grep ^[ps]ub | cut -d: -f5`
47 master_keys
=`$GPG_CMD --keyring $MASTER --with-colons --list-keys | grep ^pub | cut -d: -f5`
49 # ensure there are no colisions LP: #857472
50 for all_add_key
in $all_add_keys; do
51 for master_key
in $master_keys; do
52 if [ "$all_add_key" = "$master_key" ]; then
53 echo >&2 "Keyid collision for '$all_add_key' detected, operation aborted"
59 for add_key
in $add_keys; do
60 # export the add keyring one-by-one
62 $GPG_CMD --keyring $ADD_KEYRING --output $TMP_KEYRING --export $add_key
63 # check if signed with the master key and only add in this case
65 for master_key
in $master_keys; do
66 if $GPG_CMD --keyring $MASTER --keyring $TMP_KEYRING --check-sigs --with-colons $add_key | grep '^sig:!:' | cut
-d: -f5 | grep -q $master_key; then
67 $GPG --import $TMP_KEYRING
71 if [ $ADDED = 0 ]; then
72 echo >&2 "Key '$add_key' not added. It is not signed with a master key"
78 # update the current archive signing keyring from a network URI
79 # the archive-keyring keys needs to be signed with the master key
80 # (otherwise it does not make sense from a security POV)
82 # Disabled for now as code is insecure (LP: #1013639 (and 857472, 1013128))
83 APT_KEY_NET_UPDATE_ENABLED
=""
84 eval $(apt-config shell APT_KEY_NET_UPDATE_ENABLED APT::Key::Net-Update-Enabled)
85 if [ -z "$APT_KEY_NET_UPDATE_ENABLED" ]; then
89 if [ -z "$ARCHIVE_KEYRING_URI" ]; then
90 echo >&2 "ERROR: Your distribution is not supported in net-update as no uri for the archive-keyring is set"
93 # in theory we would need to depend on wget for this, but this feature
94 # isn't useable in debian anyway as we have no keyring uri nor a master key
95 if ! which wget
>/dev
/null
2>&1; then
96 echo >&2 "ERROR: an installed wget is required for a network-based update"
99 if [ ! -d ${APT_DIR}/var
/lib
/apt
/keyrings
]; then
100 mkdir -p ${APT_DIR}/var
/lib
/apt
/keyrings
102 keyring
=${APT_DIR}/var
/lib
/apt
/keyrings
/$(basename $ARCHIVE_KEYRING_URI)
104 if [ -e $keyring ]; then
105 old_mtime
=$(stat -c %Y $keyring)
107 (cd ${APT_DIR}/var
/lib
/apt
/keyrings
; wget
--timeout=90 -q -N $ARCHIVE_KEYRING_URI)
108 if [ ! -e $keyring ]; then
111 new_mtime
=$(stat -c %Y $keyring)
112 if [ $new_mtime -ne $old_mtime ]; then
113 aptkey_echo
"Checking for new archive signing keys now"
114 add_keys_with_verify_against_master_keyring
$keyring $MASTER_KEYRING
119 if [ ! -f $ARCHIVE_KEYRING ]; then
120 echo >&2 "ERROR: Can't find the archive-keyring"
121 echo >&2 "Is the &keyring-package; package installed?"
125 # add new keys from the package;
127 # we do not use add_keys_with_verify_against_master_keyring here,
128 # because "update" is run on regular package updates. A
129 # attacker might as well replace the master-archive-keyring file
130 # in the package and add his own keys. so this check wouldn't
131 # add any security. we *need* this check on net-update though
132 $GPG_CMD --quiet --batch --keyring $ARCHIVE_KEYRING --export | $GPG --import
134 if [ -r "$REMOVED_KEYS" ]; then
135 # remove no-longer supported/used keys
136 $GPG_CMD --keyring $REMOVED_KEYS --with-colons --list-keys | grep ^pub
| cut
-d: -f5 | while read key
; do
137 foreach_keyring_do
'remove_key_from_keyring' "$key"
140 echo >&2 "Warning: removed keys keyring $REMOVED_KEYS missing or not readable"
144 remove_key_from_keyring
() {
145 local KEYRINGFILE
="$1"
147 # non-existent keyrings have by definition no keys
148 if [ ! -e "$KEYRINGFILE" ]; then
152 local GPG
="$GPG_CMD --keyring $KEYRINGFILE"
153 while [ -n "$1" ]; do
156 # check if the key is in this keyring: the key id is in the 5 column at the end
157 if ! $GPG --with-colons --list-keys 2>&1 | grep -q "^pub:[^:]*:[^:]*:[^:]*:[0-9A-F]*${KEY}:"; then
160 if [ ! -w "$KEYRINGFILE" ]; then
161 echo >&2 "Key ${KEY} is in keyring ${KEYRINGFILE}, but can't be removed as it is read only."
164 # check if it is the only key in the keyring and if so remove the keyring altogether
165 if [ '1' = "$($GPG --with-colons --list-keys | grep "^pub:[^:]*:[^:]*:[^:]*:[0-9A-F]\+:" | wc -l)" ]; then
166 mv -f "$KEYRINGFILE" "${KEYRINGFILE}~" # behave like gpg
169 # we can't just modify pointed to files as these might be in /usr or something
171 if [ -L "$KEYRINGFILE" ]; then
172 REALTARGET
="$(readlink -f "$KEYRINGFILE")"
173 mv -f "$KEYRINGFILE" "${KEYRINGFILE}.dpkg-tmp"
174 cp -a "$REALTARGET" "$KEYRINGFILE"
176 # delete the key from the keyring
177 $GPG --batch --delete-key --yes "$KEY"
178 if [ -n "$REALTARGET" ]; then
179 # the real backup is the old link, not the copy we made
180 mv -f "${KEYRINGFILE}.dpkg-tmp" "${KEYRINGFILE}~"
185 foreach_keyring_do
() {
188 # if a --keyring was given, just remove from there
189 if [ -n "$FORCED_KEYRING" ]; then
190 $ACTION "$FORCED_KEYRING" "$@"
192 # otherwise all known keyrings are up for inspection
193 if [ -s "$TRUSTEDFILE" ]; then
194 $ACTION "$TRUSTEDFILE" "$@"
196 local TRUSTEDPARTS
="/etc/apt/trusted.gpg.d"
197 eval $(apt-config shell TRUSTEDPARTS Dir::Etc::TrustedParts/d)
198 if [ -d "$TRUSTEDPARTS" ]; then
199 # strip / suffix as gpg will double-slash in that case (#665411)
200 local STRIPPED_TRUSTEDPARTS
="${TRUSTEDPARTS%/}"
201 if [ "${STRIPPED_TRUSTEDPARTS}/" = "$TRUSTEDPARTS" ]; then
202 TRUSTEDPARTS
="$STRIPPED_TRUSTEDPARTS"
204 for trusted
in $(run-parts --list "$TRUSTEDPARTS" --regex '^.*\.gpg$'); do
205 if [ -s "$trusted" ]; then
206 $ACTION "$trusted" "$@"
213 list_keys_from_keyring
() {
214 local KEYRINGFILE
="$1"
216 # don't show the error message if this keyring doesn't include the key
217 $GPG_CMD --keyring "$KEYRINGFILE" --batch --list-keys "$@" 2>/dev
/null
|| true
220 fingerprint_keys_from_keyring
() {
221 local KEYRINGFILE
="$1"
223 # don't show the error message if this keyring doesn't include the fingerprint
224 $GPG_CMD --keyring "$KEYRINGFILE" --batch --fingerprint "$@" 2>/dev
/null
|| true
227 import_keys_from_keyring
() {
229 local KEYRINGFILE
="$2"
230 $GPG_CMD --keyring "$KEYRINGFILE" --batch --import "$IMPORT" >/dev
/null
2>&1
233 setup_merged_keyring
() {
234 local TRUSTEDFILE_BAK
="$TRUSTEDFILE"
235 TRUSTEDFILE
='/dev/null'
236 foreach_keyring_do
'import_keys_from_keyring' "${GPGHOMEDIR}/trusted.gpg"
237 TRUSTEDFILE
="$TRUSTEDFILE_BAK"
238 # mark it as non-writeable so users get errors if gnupg tries to modify it
239 if [ -s "${GPGHOMEDIR}/trusted.gpg" ]; then
240 chmod -w "${GPGHOMEDIR}/trusted.gpg"
241 GPG
="$GPG --keyring ${GPGHOMEDIR}/trusted.gpg"
243 if [ -r "$TRUSTEDFILE" ]; then
244 GPG
="$GPG --keyring $TRUSTEDFILE --primary-keyring $TRUSTEDFILE"
250 echo "Usage: apt-key [--keyring file] [command] [arguments]"
252 echo "Manage apt's list of trusted keys"
254 echo " apt-key add <file> - add the key contained in <file> ('-' for stdin)"
255 echo " apt-key del <keyid> - remove the key <keyid>"
256 echo " apt-key export <keyid> - output the key <keyid>"
257 echo " apt-key exportall - output all trusted keys"
258 echo " apt-key update - update keys using the keyring package"
259 echo " apt-key net-update - update keys using the network"
260 echo " apt-key list - list keys"
261 echo " apt-key finger - list fingerprints"
262 echo " apt-key adv - pass advanced options to gpg (download key)"
264 echo "If no specific keyring file is given the command applies to all keyring files."
267 while [ -n "$1" ]; do
276 requires_root
() { true
; }
280 aptkey_echo
() { true
; }
284 echo >&2 "Unknown option: $1"
292 if [ -z "$TRUSTEDFILE" ]; then
293 TRUSTEDFILE
="/etc/apt/trusted.gpg"
294 eval $(apt-config shell TRUSTEDFILE Apt::GPGV::TrustedKeyring)
295 eval $(apt-config shell TRUSTEDFILE Dir::Etc::Trusted/f)
299 if [ -z "$command" ]; then
305 if [ "$command" != "help" ]; then
306 eval $(apt-config shell GPG_EXE Apt::Key::gpgcommand)
308 if [ -n "$GPG_EXE" ] && which "$GPG_EXE" >/dev
/null
2>&1; then
310 elif which gpg
>/dev
/null
2>&1; then
312 elif which gpg2
>/dev
/null
2>&1; then
315 echo >&2 "Error: gnupg or gnupg2 do not seem to be installed,"
316 echo >&2 "Error: but apt-key requires gnupg or gnupg2 for operation."
321 GPG_CMD
="$GPG_EXE --ignore-time-conflict --no-options --no-default-keyring"
323 # gpg needs (in different versions more or less) files to function correctly,
324 # so we give it its own homedir and generate some valid content for it
325 GPGHOMEDIR
="$(mktemp -d)"
326 CURRENTTRAP
="${CURRENTTRAP} rm -rf '${GPGHOMEDIR}';"
327 trap "${CURRENTTRAP}" 0 HUP INT QUIT ILL ABRT FPE SEGV PIPE TERM
328 chmod 700 "$GPGHOMEDIR"
329 # We don't use a secret keyring, of course, but gpg panics and
330 # implodes if there isn't one available - and writeable for imports
331 SECRETKEYRING
="${GPGHOMEDIR}/secring.gpg"
333 GPG_CMD
="$GPG_CMD --homedir $GPGHOMEDIR"
334 # create the trustdb with an (empty) dummy keyring
335 # older gpgs required it, newer gpgs even warn that it isn't needed,
336 # but require it nonetheless for some commands, so we just play safe
337 # here for the foreseeable future and create a dummy one
338 $GPG_CMD --quiet --check-trustdb --keyring $SECRETKEYRING >/dev
/null
2>&1
339 # tell gpg that it shouldn't try to maintain a trustdb file
340 GPG_CMD
="$GPG_CMD --no-auto-check-trustdb --trust-model always"
343 # gpg defaults to mode 0600 for new keyrings. Create one with 0644 instead.
344 if ! [ -e "$TRUSTEDFILE" ]; then
345 if [ -w "$(dirname "$TRUSTEDFILE")" ]; then
346 touch -- "$TRUSTEDFILE"
347 chmod 0644 -- "$TRUSTEDFILE"
356 $GPG --quiet --batch --import "$@"
361 foreach_keyring_do
'remove_key_from_keyring' "$@"
375 foreach_keyring_do
'list_keys_from_keyring' "$@"
378 foreach_keyring_do
'fingerprint_keys_from_keyring' "$@"
381 foreach_keyring_do
'import_keys_from_keyring' "${GPGHOMEDIR}/trusted.gpg"
382 $GPG_CMD --keyring "${GPGHOMEDIR}/trusted.gpg" --armor --export "$@"
386 aptkey_echo
"Executing: $GPG $*"