6 GPG_CMD
="gpg --ignore-time-conflict --no-options --no-default-keyring"
8 # gpg needs (in different versions more or less) files to function correctly,
9 # so we give it its own homedir and generate some valid content for it
10 GPGHOMEDIR
="$(mktemp -d)"
11 CURRENTTRAP
="${CURRENTTRAP} rm -rf '${GPGHOMEDIR}';"
12 trap "${CURRENTTRAP}" 0 HUP INT QUIT ILL ABRT FPE SEGV PIPE TERM
13 chmod 700 "$GPGHOMEDIR"
14 # We don't use a secret keyring, of course, but gpg panics and
15 # implodes if there isn't one available - and writeable for imports
16 SECRETKEYRING
="${GPGHOMEDIR}/secring.gpg"
18 GPG_CMD
="$GPG_CMD --homedir $GPGHOMEDIR"
19 # create the trustdb with an (empty) dummy keyring
20 # older gpgs required it, newer gpgs even warn that it isn't needed,
21 # but require it nonetheless for some commands, so we just play safe
22 # here for the foreseeable future and create a dummy one
23 $GPG_CMD --quiet --check-trustdb --keyring $SECRETKEYRING >/dev
/null
2>&1
24 # tell gpg that it shouldn't try to maintain a trustdb file
25 GPG_CMD
="$GPG_CMD --no-auto-check-trustdb --trust-model always"
29 eval $(apt-config shell APT_DIR Dir)
31 MASTER_KEYRING
='&keyring-master-filename;'
32 eval $(apt-config shell MASTER_KEYRING APT::Key::MasterKeyring)
33 ARCHIVE_KEYRING
='&keyring-filename;'
34 eval $(apt-config shell ARCHIVE_KEYRING APT::Key::ArchiveKeyring)
35 REMOVED_KEYS
='&keyring-removed-filename;'
36 eval $(apt-config shell REMOVED_KEYS APT::Key::RemovedKeys)
37 ARCHIVE_KEYRING_URI
='&keyring-uri;'
38 eval $(apt-config shell ARCHIVE_KEYRING_URI APT::Key::ArchiveKeyringURI)
39 TMP_KEYRING
=${APT_DIR}/var
/lib
/apt
/keyrings
/maybe
-import-keyring.gpg
41 aptkey_echo
() { echo "$@"; }
44 if [ "$(id -u)" -ne 0 ]; then
45 echo >&2 "ERROR: This command can only be used by root."
50 add_keys_with_verify_against_master_keyring
() {
54 if [ ! -f "$ADD_KEYRING" ]; then
55 echo >&2 "ERROR: '$ADD_KEYRING' not found"
58 if [ ! -f "$MASTER" ]; then
59 echo >&2 "ERROR: '$MASTER' not found"
63 # when adding new keys, make sure that the archive-master-keyring
65 # all keys that are exported must have a valid signature
66 # from a key in the $distro-master-keyring
67 add_keys
=`$GPG_CMD --keyring $ADD_KEYRING --with-colons --list-keys | grep ^pub | cut -d: -f5`
68 all_add_keys
=`$GPG_CMD --keyring $ADD_KEYRING --with-colons --list-keys | grep ^[ps]ub | cut -d: -f5`
69 master_keys
=`$GPG_CMD --keyring $MASTER --with-colons --list-keys | grep ^pub | cut -d: -f5`
71 # ensure there are no colisions LP: #857472
72 for all_add_key
in $all_add_keys; do
73 for master_key
in $master_keys; do
74 if [ "$all_add_key" = "$master_key" ]; then
75 echo >&2 "Keyid collision for '$all_add_key' detected, operation aborted"
81 for add_key
in $add_keys; do
82 # export the add keyring one-by-one
84 $GPG_CMD --keyring $ADD_KEYRING --output $TMP_KEYRING --export $add_key
85 # check if signed with the master key and only add in this case
87 for master_key
in $master_keys; do
88 if $GPG_CMD --keyring $MASTER --keyring $TMP_KEYRING --check-sigs --with-colons $add_key | grep '^sig:!:' | cut
-d: -f5 | grep -q $master_key; then
89 $GPG --import $TMP_KEYRING
93 if [ $ADDED = 0 ]; then
94 echo >&2 "Key '$add_key' not added. It is not signed with a master key"
100 # update the current archive signing keyring from a network URI
101 # the archive-keyring keys needs to be signed with the master key
102 # (otherwise it does not make sense from a security POV)
104 # Disabled for now as code is insecure (LP: #1013639 (and 857472, 1013128))
105 APT_KEY_NET_UPDATE_ENABLED
=""
106 eval $(apt-config shell APT_KEY_NET_UPDATE_ENABLED APT::Key::Net-Update-Enabled)
107 if [ -z "$APT_KEY_NET_UPDATE_ENABLED" ]; then
111 if [ -z "$ARCHIVE_KEYRING_URI" ]; then
112 echo >&2 "ERROR: Your distribution is not supported in net-update as no uri for the archive-keyring is set"
115 # in theory we would need to depend on wget for this, but this feature
116 # isn't useable in debian anyway as we have no keyring uri nor a master key
117 if ! which wget
>/dev
/null
2>&1; then
118 echo >&2 "ERROR: an installed wget is required for a network-based update"
121 if [ ! -d ${APT_DIR}/var
/lib
/apt
/keyrings
]; then
122 mkdir -p ${APT_DIR}/var
/lib
/apt
/keyrings
124 keyring
=${APT_DIR}/var
/lib
/apt
/keyrings
/$(basename $ARCHIVE_KEYRING_URI)
126 if [ -e $keyring ]; then
127 old_mtime
=$(stat -c %Y $keyring)
129 (cd ${APT_DIR}/var
/lib
/apt
/keyrings
; wget
--timeout=90 -q -N $ARCHIVE_KEYRING_URI)
130 if [ ! -e $keyring ]; then
133 new_mtime
=$(stat -c %Y $keyring)
134 if [ $new_mtime -ne $old_mtime ]; then
135 aptkey_echo
"Checking for new archive signing keys now"
136 add_keys_with_verify_against_master_keyring
$keyring $MASTER_KEYRING
141 if [ ! -f $ARCHIVE_KEYRING ]; then
142 echo >&2 "ERROR: Can't find the archive-keyring"
143 echo >&2 "Is the &keyring-package; package installed?"
147 # add new keys from the package;
149 # we do not use add_keys_with_verify_against_master_keyring here,
150 # because "update" is run on regular package updates. A
151 # attacker might as well replace the master-archive-keyring file
152 # in the package and add his own keys. so this check wouldn't
153 # add any security. we *need* this check on net-update though
154 $GPG_CMD --quiet --batch --keyring $ARCHIVE_KEYRING --export | $GPG --import
156 if [ -r "$REMOVED_KEYS" ]; then
157 # remove no-longer supported/used keys
158 $GPG_CMD --keyring $REMOVED_KEYS --with-colons --list-keys | grep ^pub
| cut
-d: -f5 | while read key
; do
159 foreach_keyring_do
'remove_key_from_keyring' "$key"
162 echo >&2 "Warning: removed keys keyring $REMOVED_KEYS missing or not readable"
166 remove_key_from_keyring
() {
167 local KEYRINGFILE
="$1"
169 # non-existent keyrings have by definition no keys
170 if [ ! -e "$KEYRINGFILE" ]; then
174 local GPG
="$GPG_CMD --keyring $KEYRINGFILE"
175 while [ -n "$1" ]; do
178 # check if the key is in this keyring: the key id is in the 5 column at the end
179 if ! $GPG --with-colons --list-keys 2>&1 | grep -q "^pub:[^:]*:[^:]*:[^:]*:[0-9A-F]*${KEY}:"; then
182 if [ ! -w "$KEYRINGFILE" ]; then
183 echo >&2 "Key ${KEY} is in keyring ${KEYRINGFILE}, but can't be removed as it is read only."
186 # check if it is the only key in the keyring and if so remove the keyring altogether
187 if [ '1' = "$($GPG --with-colons --list-keys | grep "^pub:[^:]*:[^:]*:[^:]*:[0-9A-F]\+:" | wc -l)" ]; then
188 mv -f "$KEYRINGFILE" "${KEYRINGFILE}~" # behave like gpg
191 # we can't just modify pointed to files as these might be in /usr or something
193 if [ -L "$KEYRINGFILE" ]; then
194 REALTARGET
="$(readlink -f "$KEYRINGFILE")"
195 mv -f "$KEYRINGFILE" "${KEYRINGFILE}.dpkg-tmp"
196 cp -a "$REALTARGET" "$KEYRINGFILE"
198 # delete the key from the keyring
199 $GPG --batch --delete-key --yes "$KEY"
200 if [ -n "$REALTARGET" ]; then
201 # the real backup is the old link, not the copy we made
202 mv -f "${KEYRINGFILE}.dpkg-tmp" "${KEYRINGFILE}~"
207 foreach_keyring_do
() {
210 # if a --keyring was given, just remove from there
211 if [ -n "$FORCED_KEYRING" ]; then
212 $ACTION "$FORCED_KEYRING" "$@"
214 # otherwise all known keyrings are up for inspection
215 if [ -s "$TRUSTEDFILE" ]; then
216 $ACTION "$TRUSTEDFILE" "$@"
218 local TRUSTEDPARTS
="/etc/apt/trusted.gpg.d"
219 eval $(apt-config shell TRUSTEDPARTS Dir::Etc::TrustedParts/d)
220 if [ -d "$TRUSTEDPARTS" ]; then
221 # strip / suffix as gpg will double-slash in that case (#665411)
222 local STRIPPED_TRUSTEDPARTS
="${TRUSTEDPARTS%/}"
223 if [ "${STRIPPED_TRUSTEDPARTS}/" = "$TRUSTEDPARTS" ]; then
224 TRUSTEDPARTS
="$STRIPPED_TRUSTEDPARTS"
226 for trusted
in $(run-parts --list "$TRUSTEDPARTS" --regex '^.*\.gpg$'); do
227 if [ -s "$trusted" ]; then
228 $ACTION "$trusted" "$@"
235 list_keys_from_keyring
() {
236 local KEYRINGFILE
="$1"
238 # don't show the error message if this keyring doesn't include the key
239 $GPG_CMD --keyring "$KEYRINGFILE" --batch --list-keys "$@" 2>/dev
/null
|| true
242 fingerprint_keys_from_keyring
() {
243 local KEYRINGFILE
="$1"
245 # don't show the error message if this keyring doesn't include the fingerprint
246 $GPG_CMD --keyring "$KEYRINGFILE" --batch --fingerprint "$@" 2>/dev
/null
|| true
249 import_keys_from_keyring
() {
251 local KEYRINGFILE
="$2"
252 $GPG_CMD --keyring "$KEYRINGFILE" --batch --import "$IMPORT" >/dev
/null
2>&1
255 setup_merged_keyring
() {
256 local TRUSTEDFILE_BAK
="$TRUSTEDFILE"
257 TRUSTEDFILE
='/dev/null'
258 foreach_keyring_do
'import_keys_from_keyring' "${GPGHOMEDIR}/trusted.gpg"
259 TRUSTEDFILE
="$TRUSTEDFILE_BAK"
260 # mark it as non-writeable so users get errors if gnupg tries to modify it
261 if [ -s "${GPGHOMEDIR}/trusted.gpg" ]; then
262 chmod -w "${GPGHOMEDIR}/trusted.gpg"
263 GPG
="$GPG --keyring ${GPGHOMEDIR}/trusted.gpg"
265 if [ -r "$TRUSTEDFILE" ]; then
266 GPG
="$GPG --keyring $TRUSTEDFILE --primary-keyring $TRUSTEDFILE"
272 echo "Usage: apt-key [--keyring file] [command] [arguments]"
274 echo "Manage apt's list of trusted keys"
276 echo " apt-key add <file> - add the key contained in <file> ('-' for stdin)"
277 echo " apt-key del <keyid> - remove the key <keyid>"
278 echo " apt-key export <keyid> - output the key <keyid>"
279 echo " apt-key exportall - output all trusted keys"
280 echo " apt-key update - update keys using the keyring package"
281 echo " apt-key net-update - update keys using the network"
282 echo " apt-key list - list keys"
283 echo " apt-key finger - list fingerprints"
284 echo " apt-key adv - pass advanced options to gpg (download key)"
286 echo "If no specific keyring file is given the command applies to all keyring files."
289 while [ -n "$1" ]; do
298 requires_root
() { true
; }
302 aptkey_echo
() { true
; }
306 echo >&2 "Unknown option: $1"
314 if [ -z "$TRUSTEDFILE" ]; then
315 TRUSTEDFILE
="/etc/apt/trusted.gpg"
316 eval $(apt-config shell TRUSTEDFILE Apt::GPGV::TrustedKeyring)
317 eval $(apt-config shell TRUSTEDFILE Dir::Etc::Trusted/f)
321 if [ -z "$command" ]; then
327 if [ "$command" != "help" ]; then
328 if ! which gpg
>/dev
/null
2>&1; then
329 echo >&2 "Warning: gnupg does not seem to be installed."
330 echo >&2 "Warning: apt-key requires gnupg for most operations."
334 # gpg defaults to mode 0600 for new keyrings. Create one with 0644 instead.
335 if ! [ -e "$TRUSTEDFILE" ]; then
336 if [ -w "$(dirname "$TRUSTEDFILE")" ]; then
337 touch -- "$TRUSTEDFILE"
338 chmod 0644 -- "$TRUSTEDFILE"
347 $GPG --quiet --batch --import "$@"
352 foreach_keyring_do
'remove_key_from_keyring' "$@"
366 foreach_keyring_do
'list_keys_from_keyring' "$@"
369 foreach_keyring_do
'fingerprint_keys_from_keyring' "$@"
372 foreach_keyring_do
'import_keys_from_keyring' "${GPGHOMEDIR}/trusted.gpg"
373 $GPG_CMD --keyring "${GPGHOMEDIR}/trusted.gpg" --armor --export "$@"
377 aptkey_echo
"Executing: $GPG $*"