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 ARCHIVE_KEYRING_URI
=""
30 #MASTER_KEYRING=/usr/share/keyrings/debian-master-keyring.gpg
31 #ARCHIVE_KEYRING_URI=http://ftp.debian.org/debian/debian-archive-keyring.gpg
33 ARCHIVE_KEYRING
=/usr
/share
/keyrings
/debian
-archive-keyring.gpg
34 REMOVED_KEYS
=/usr
/share
/keyrings
/debian
-archive-removed-keys.gpg
37 if [ "$(id -u)" -ne 0 ]; then
38 echo >&1 "ERROR: This command can only be used by root."
43 # gpg defaults to mode 0600 for new keyrings. Create one with 0644 instead.
46 if ! [ -e "$path" ]; then
53 add_keys_with_verify_against_master_keyring
() {
57 if [ ! -f "$ADD_KEYRING" ]; then
58 echo "ERROR: '$ADD_KEYRING' not found"
61 if [ ! -f "$MASTER" ]; then
62 echo "ERROR: '$MASTER' not found"
66 # when adding new keys, make sure that the archive-master-keyring
68 # all keys that are exported must have a valid signature
69 # from a key in the $distro-master-keyring
70 add_keys
=`$GPG_CMD --keyring $ADD_KEYRING --with-colons --list-keys | grep ^pub | cut -d: -f5`
71 master_keys
=`$GPG_CMD --keyring $MASTER --with-colons --list-keys | grep ^pub | cut -d: -f5`
72 for add_key
in $add_keys; do
74 for master_key
in $master_keys; do
75 if $GPG_CMD --keyring $ADD_KEYRING --list-sigs --with-colons $add_key | grep ^sig
| cut
-d: -f5 | grep -q $master_key; then
76 $GPG_CMD --quiet --batch --keyring $ADD_KEYRING --export $add_key | $GPG --import
80 if [ $ADDED = 0 ]; then
81 echo >&2 "Key '$add_key' not added. It is not signed with a master key"
86 # update the current archive signing keyring from a network URI
87 # the archive-keyring keys needs to be signed with the master key
88 # (otherwise it does not make sense from a security POV)
90 if [ -z "$ARCHIVE_KEYRING_URI" ]; then
91 echo >&2 "ERROR: Your distribution is not supported in net-update as no uri for the archive-keyring is set"
95 # in theory we would need to depend on wget for this, but this feature
96 # isn't useable in debian anyway as we have no keyring uri nor a master key
97 if ! which wget
>/dev
/null
2>&1; then
98 echo >&2 "ERROR: an installed wget is required for a network-based update"
101 if [ ! -d /var
/lib
/apt
/keyrings
]; then
102 mkdir -p /var
/lib
/apt
/keyrings
104 keyring
=/var
/lib
/apt
/keyrings
/$(basename $ARCHIVE_KEYRING)
106 if [ -e $keyring ]; then
107 old_mtime
=$(stat -c %Y $keyring)
109 (cd /var
/lib
/apt
/keyrings
; wget
-q -N $ARCHIVE_KEYRING_URI)
110 if [ ! -e $keyring ]; then
113 new_mtime
=$(stat -c %Y $keyring)
114 if [ $new_mtime -ne $old_mtime ]; then
115 echo "Checking for new archive signing keys now"
116 add_keys_with_verify_against_master_keyring
$keyring $MASTER_KEYRING
121 if [ ! -f $ARCHIVE_KEYRING ]; then
122 echo >&2 "ERROR: Can't find the archive-keyring"
123 echo >&2 "Is the debian-archive-keyring package installed?"
128 # add new keys from the package;
130 # we do not use add_keys_with_verify_against_master_keyring here,
131 # because "update" is run on regular package updates. A
132 # attacker might as well replace the master-archive-keyring file
133 # in the package and add his own keys. so this check wouldn't
134 # add any security. we *need* this check on net-update though
135 $GPG_CMD --quiet --batch --keyring $ARCHIVE_KEYRING --export | $GPG --import
137 if [ -r "$REMOVED_KEYS" ]; then
138 # remove no-longer supported/used keys
139 keys
=`$GPG_CMD --keyring $REMOVED_KEYS --with-colons --list-keys | grep ^pub | cut -d: -f5`
141 if $GPG --list-keys --with-colons | grep ^pub
| cut
-d: -f5 | grep -q $key; then
142 $GPG --quiet --batch --delete-key --yes ${key}
146 echo "Warning: removed keys keyring $REMOVED_KEYS missing or not readable" >&2
152 echo "Usage: apt-key [--keyring file] [command] [arguments]"
154 echo "Manage apt's list of trusted keys"
156 echo " apt-key add <file> - add the key contained in <file> ('-' for stdin)"
157 echo " apt-key del <keyid> - remove the key <keyid>"
158 echo " apt-key export <keyid> - output the key <keyid>"
159 echo " apt-key exportall - output all trusted keys"
160 echo " apt-key update - update keys using the keyring package"
161 echo " apt-key net-update - update keys using the network"
162 echo " apt-key list - list keys"
163 echo " apt-key finger - list fingerprints"
164 echo " apt-key adv - pass advanced options to gpg (download key)"
166 echo "If no specific keyring file is given the command applies to all keyring files."
169 while [ -n "$1" ]; do
174 if [ -r "$TRUSTEDFILE" ] || [ "$2" = 'add' ] || [ "$2" = 'adv' ]; then
175 GPG
="$GPG --keyring $TRUSTEDFILE --primary-keyring $TRUSTEDFILE"
177 echo >&2 "Error: The specified keyring »$TRUSTEDFILE« is missing or not readable"
183 requires_root
() { true
; }
187 echo >&2 "Unknown option: $1"
195 if [ -z "$TRUSTEDFILE" ]; then
196 TRUSTEDFILE
="/etc/apt/trusted.gpg"
197 eval $(apt-config shell TRUSTEDFILE Apt::GPGV::TrustedKeyring)
198 eval $(apt-config shell TRUSTEDFILE Dir::Etc::Trusted/f)
199 if [ -r "$TRUSTEDFILE" ]; then
200 GPG
="$GPG --keyring $TRUSTEDFILE"
202 GPG
="$GPG --primary-keyring $TRUSTEDFILE"
203 TRUSTEDPARTS
="/etc/apt/trusted.gpg.d"
204 eval $(apt-config shell TRUSTEDPARTS Dir::Etc::TrustedParts/d)
205 if [ -d "$TRUSTEDPARTS" ]; then
206 for trusted
in $(run-parts --list $TRUSTEDPARTS --regex '^.*\.gpg$'); do
207 GPG
="$GPG --keyring $trusted"
213 if [ -z "$command" ]; then
219 if [ "$command" != "help" ] && ! which gpg
>/dev
/null
2>&1; then
220 echo >&2 "Warning: gnupg does not seem to be installed."
221 echo >&2 "Warning: apt-key requires gnupg for most operations."
228 init_keyring
"$TRUSTEDFILE"
229 $GPG --quiet --batch --import "$1"
234 init_keyring
"$TRUSTEDFILE"
235 $GPG --quiet --batch --delete-key --yes "$1"
239 init_keyring
"$TRUSTEDFILE"
243 init_keyring
"$TRUSTEDFILE"
247 init_keyring
"$TRUSTEDFILE"
248 $GPG --batch --list-keys
251 init_keyring
"$TRUSTEDFILE"
252 $GPG --batch --fingerprint
255 init_keyring
"$TRUSTEDFILE"
256 $GPG --armor --export "$1"
259 init_keyring
"$TRUSTEDFILE"
260 $GPG --armor --export
263 init_keyring
"$TRUSTEDFILE"
264 echo "Executing: $GPG $*"