6 GPG_CMD
="gpg --ignore-time-conflict --no-options --no-default-keyring"
8 # gpg needs a trustdb to function, but it can't be invalid (not even empty)
9 # so we create a temporary directory to store our fresh readable trustdb in
10 TRUSTDBDIR
="$(mktemp -d)"
11 CURRENTTRAP
="${CURRENTTRAP} rm -rf '${TRUSTDBDIR}';"
12 trap "${CURRENTTRAP}" 0 HUP INT QUIT ILL ABRT FPE SEGV PIPE TERM
13 chmod 700 "$TRUSTDBDIR"
14 # We also 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
="${TRUSTDBDIR}/secring.gpg"
18 GPG_CMD
="$GPG_CMD --secret-keyring $SECRETKEYRING"
19 GPG_CMD
="$GPG_CMD --trustdb-name ${TRUSTDBDIR}/trustdb.gpg"
21 # now create the trustdb with an (empty) dummy keyring
22 $GPG_CMD --quiet --check-trustdb --keyring $SECRETKEYRING
23 # and make sure that gpg isn't trying to update the file
24 GPG_CMD
="$GPG_CMD --no-auto-check-trustdb --trust-model always"
29 #MASTER_KEYRING=/usr/share/keyrings/debian-master-keyring.gpg
30 eval $(apt-config shell MASTER_KEYRING APT::Key::MasterKeyring)
31 ARCHIVE_KEYRING_URI
=""
32 #ARCHIVE_KEYRING_URI=http://ftp.debian.org/debian/debian-archive-keyring.gpg
33 eval $(apt-config shell ARCHIVE_KEYRING_URI APT::Key::ArchiveKeyringURI)
35 ARCHIVE_KEYRING
=/usr
/share
/keyrings
/debian
-archive-keyring.gpg
36 eval $(apt-config shell ARCHIVE_KEYRING APT::Key::ArchiveKeyring)
37 REMOVED_KEYS
=/usr
/share
/keyrings
/debian
-archive-removed-keys.gpg
38 eval $(apt-config shell REMOVED_KEYS APT::Key::RemovedKeys)
41 if [ "$(id -u)" -ne 0 ]; then
42 echo >&1 "ERROR: This command can only be used by root."
47 # gpg defaults to mode 0600 for new keyrings. Create one with 0644 instead.
50 if ! [ -e "$path" ]; then
57 add_keys_with_verify_against_master_keyring
() {
61 if [ ! -f "$ADD_KEYRING" ]; then
62 echo "ERROR: '$ADD_KEYRING' not found"
65 if [ ! -f "$MASTER" ]; then
66 echo "ERROR: '$MASTER' not found"
70 # when adding new keys, make sure that the archive-master-keyring
72 # all keys that are exported must have a valid signature
73 # from a key in the $distro-master-keyring
74 add_keys
=`$GPG_CMD --keyring $ADD_KEYRING --with-colons --list-keys | grep ^pub | cut -d: -f5`
75 master_keys
=`$GPG_CMD --keyring $MASTER --with-colons --list-keys | grep ^pub | cut -d: -f5`
76 for add_key
in $add_keys; do
78 for master_key
in $master_keys; do
79 if $GPG_CMD --keyring $ADD_KEYRING --list-sigs --with-colons $add_key | grep ^sig
| cut
-d: -f5 | grep -q $master_key; then
80 $GPG_CMD --quiet --batch --keyring $ADD_KEYRING --export $add_key | $GPG --import
84 if [ $ADDED = 0 ]; then
85 echo >&2 "Key '$add_key' not added. It is not signed with a master key"
90 # update the current archive signing keyring from a network URI
91 # the archive-keyring keys needs to be signed with the master key
92 # (otherwise it does not make sense from a security POV)
94 if [ -z "$ARCHIVE_KEYRING_URI" ]; then
95 echo >&2 "ERROR: Your distribution is not supported in net-update as no uri for the archive-keyring is set"
99 # in theory we would need to depend on wget for this, but this feature
100 # isn't useable in debian anyway as we have no keyring uri nor a master key
101 if ! which wget
>/dev
/null
2>&1; then
102 echo >&2 "ERROR: an installed wget is required for a network-based update"
105 if [ ! -d /var
/lib
/apt
/keyrings
]; then
106 mkdir -p /var
/lib
/apt
/keyrings
108 keyring
=/var
/lib
/apt
/keyrings
/$(basename $ARCHIVE_KEYRING)
110 if [ -e $keyring ]; then
111 old_mtime
=$(stat -c %Y $keyring)
113 (cd /var
/lib
/apt
/keyrings
; wget
-q -N $ARCHIVE_KEYRING_URI)
114 if [ ! -e $keyring ]; then
117 new_mtime
=$(stat -c %Y $keyring)
118 if [ $new_mtime -ne $old_mtime ]; then
119 echo "Checking for new archive signing keys now"
120 add_keys_with_verify_against_master_keyring
$keyring $MASTER_KEYRING
125 if [ ! -f $ARCHIVE_KEYRING ]; then
126 echo >&2 "ERROR: Can't find the archive-keyring"
127 echo >&2 "Is the debian-archive-keyring package installed?"
132 # add new keys from the package;
134 # we do not use add_keys_with_verify_against_master_keyring here,
135 # because "update" is run on regular package updates. A
136 # attacker might as well replace the master-archive-keyring file
137 # in the package and add his own keys. so this check wouldn't
138 # add any security. we *need* this check on net-update though
139 $GPG_CMD --quiet --batch --keyring $ARCHIVE_KEYRING --export | $GPG --import
141 if [ -r "$REMOVED_KEYS" ]; then
142 # remove no-longer supported/used keys
143 keys
=`$GPG_CMD --keyring $REMOVED_KEYS --with-colons --list-keys | grep ^pub | cut -d: -f5`
145 if $GPG --list-keys --with-colons | grep ^pub
| cut
-d: -f5 | grep -q $key; then
146 $GPG --quiet --batch --delete-key --yes ${key}
150 echo "Warning: removed keys keyring $REMOVED_KEYS missing or not readable" >&2
154 remove_key_from_keyring
() {
155 local GPG
="$GPG_CMD --keyring $1"
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]\+$2:"; then
160 if [ ! -w "$1" ]; then
161 echo >&2 "Key ${2} is in keyring ${1}, 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 alltogether
165 if [ '1' = "$($GPG --with-colons --list-keys | grep "^pub:[^:]*:[^:]*:[^:]*:[0-9A-F]\+:" | wc -l)" ]; then
166 mv -f "$1" "${1}~" # behave like gpg
169 # we can't just modify pointed to files as these might be in /usr or something
172 REALTARGET
="$(readlink -f "$1")"
173 mv -f "$1" "${1}.dpkg-tmp"
174 cp -a "$REALTARGET" "$1"
177 # delete the key from the keyring
178 $GPG --batch --delete-key --yes "$2"
179 if [ -n "$REALTARGET" ]; then
180 # the real backup is the old link, not the copy we made
181 mv -f "${1}.dpkg-tmp" "${1}~"
188 # if a --keyring was given, just remove from there
189 if [ -n "$FORCED_KEYRING" ]; then
190 remove_key_from_keyring
"$FORCED_KEYRING" "$1"
192 # otherwise all known keyrings are up for inspection
193 local TRUSTEDFILE
="/etc/apt/trusted.gpg"
194 eval $(apt-config shell TRUSTEDFILE Apt::GPGV::TrustedKeyring)
195 eval $(apt-config shell TRUSTEDFILE Dir::Etc::Trusted/f)
196 remove_key_from_keyring
"$TRUSTEDFILE" "$1"
197 TRUSTEDPARTS
="/etc/apt/trusted.gpg.d"
198 eval $(apt-config shell TRUSTEDPARTS Dir::Etc::TrustedParts/d)
199 if [ -d "$TRUSTEDPARTS" ]; then
200 for trusted
in $(run-parts --list "$TRUSTEDPARTS" --regex '^.*\.gpg$'); do
201 remove_key_from_keyring
"$trusted" "$1"
210 echo "Usage: apt-key [--keyring file] [command] [arguments]"
212 echo "Manage apt's list of trusted keys"
214 echo " apt-key add <file> - add the key contained in <file> ('-' for stdin)"
215 echo " apt-key del <keyid> - remove the key <keyid>"
216 echo " apt-key export <keyid> - output the key <keyid>"
217 echo " apt-key exportall - output all trusted keys"
218 echo " apt-key update - update keys using the keyring package"
219 echo " apt-key net-update - update keys using the network"
220 echo " apt-key list - list keys"
221 echo " apt-key finger - list fingerprints"
222 echo " apt-key adv - pass advanced options to gpg (download key)"
224 echo "If no specific keyring file is given the command applies to all keyring files."
227 while [ -n "$1" ]; do
233 if [ -r "$TRUSTEDFILE" ] || [ "$2" = 'add' ] || [ "$2" = 'adv' ]; then
234 GPG
="$GPG --keyring $TRUSTEDFILE --primary-keyring $TRUSTEDFILE"
236 echo >&2 "Error: The specified keyring »$TRUSTEDFILE« is missing or not readable"
242 requires_root
() { true
; }
246 echo >&2 "Unknown option: $1"
254 if [ -z "$TRUSTEDFILE" ]; then
255 TRUSTEDFILE
="/etc/apt/trusted.gpg"
256 eval $(apt-config shell TRUSTEDFILE Apt::GPGV::TrustedKeyring)
257 eval $(apt-config shell TRUSTEDFILE Dir::Etc::Trusted/f)
258 if [ -r "$TRUSTEDFILE" ]; then
259 GPG
="$GPG --keyring $TRUSTEDFILE"
261 GPG
="$GPG --primary-keyring $TRUSTEDFILE"
262 TRUSTEDPARTS
="/etc/apt/trusted.gpg.d"
263 eval $(apt-config shell TRUSTEDPARTS Dir::Etc::TrustedParts/d)
264 if [ -d "$TRUSTEDPARTS" ]; then
265 # strip / suffix as gpg will double-slash in that case (#665411)
266 STRIPPED_TRUSTEDPARTS
="${TRUSTEDPARTS%/}"
267 if [ "${STRIPPED_TRUSTEDPARTS}/" = "$TRUSTEDPARTS" ]; then
268 TRUSTEDPARTS
="$STRIPPED_TRUSTEDPARTS"
270 for trusted
in $(run-parts --list "$TRUSTEDPARTS" --regex '^.*\.gpg$'); do
271 GPG
="$GPG --keyring $trusted"
277 if [ -z "$command" ]; then
283 if [ "$command" != "help" ] && ! which gpg
>/dev
/null
2>&1; then
284 echo >&2 "Warning: gnupg does not seem to be installed."
285 echo >&2 "Warning: apt-key requires gnupg for most operations."
292 init_keyring
"$TRUSTEDFILE"
293 $GPG --quiet --batch --import "$1"
297 init_keyring
"$TRUSTEDFILE"
301 init_keyring
"$TRUSTEDFILE"
305 init_keyring
"$TRUSTEDFILE"
309 init_keyring
"$TRUSTEDFILE"
310 $GPG --batch --list-keys
313 init_keyring
"$TRUSTEDFILE"
314 $GPG --batch --fingerprint
317 init_keyring
"$TRUSTEDFILE"
318 $GPG --armor --export "$1"
321 init_keyring
"$TRUSTEDFILE"
322 $GPG --armor --export
325 init_keyring
"$TRUSTEDFILE"
326 echo "Executing: $GPG $*"