6 # We don't use a secret keyring, of course, but gpg panics and
7 # implodes if there isn't one available
8 GPG_CMD
="gpg --ignore-time-conflict --no-options --no-default-keyring --secret-keyring /etc/apt/secring.gpg --trustdb-name /etc/apt/trustdb.gpg"
12 ARCHIVE_KEYRING_URI
=""
13 #MASTER_KEYRING=/usr/share/keyrings/debian-master-keyring.gpg
14 #ARCHIVE_KEYRING_URI=http://ftp.debian.org/debian/debian-archive-keyring.gpg
16 ARCHIVE_KEYRING
=/usr
/share
/keyrings
/debian
-archive-keyring.gpg
17 REMOVED_KEYS
=/usr
/share
/keyrings
/debian
-archive-removed-keys.gpg
19 add_keys_with_verify_against_master_keyring
() {
23 if [ ! -f "$ADD_KEYRING" ]; then
24 echo "ERROR: '$ADD_KEYRING' not found"
27 if [ ! -f "$MASTER" ]; then
28 echo "ERROR: '$MASTER' not found"
32 # when adding new keys, make sure that the archive-master-keyring
34 # all keys that are exported must have a valid signature
35 # from a key in the $distro-master-keyring
36 add_keys
=`$GPG_CMD --keyring $ADD_KEYRING --with-colons --list-keys | grep ^pub | cut -d: -f5`
37 master_keys
=`$GPG_CMD --keyring $MASTER --with-colons --list-keys | grep ^pub | cut -d: -f5`
38 for add_key
in $add_keys; do
40 for master_key
in $master_keys; do
41 if $GPG_CMD --keyring $ADD_KEYRING --list-sigs --with-colons $add_key | grep ^sig
| cut
-d: -f5 | grep -q $master_key; then
42 $GPG_CMD --quiet --batch --keyring $ADD_KEYRING --export $add_key | $GPG --import
46 if [ $ADDED = 0 ]; then
47 echo >&2 "Key '$add_key' not added. It is not signed with a master key"
52 # update the current archive signing keyring from a network URI
53 # the archive-keyring keys needs to be signed with the master key
54 # (otherwise it does not make sense from a security POV)
56 if [ -z "$ARCHIVE_KEYRING_URI" ]; then
57 echo "ERROR: no location for the archive-keyring given"
60 # in theory we would need to depend on wget for this, but this feature
61 # isn't useable in debian anyway as we have no keyring uri nor a master key
62 if ! which wget
>/dev
/null
2>&1; then
63 echo "ERROR: an installed wget is required for a network-based update"
66 if [ ! -d /var
/lib
/apt
/keyrings
]; then
67 mkdir -p /var
/lib
/apt
/keyrings
69 keyring
=/var
/lib
/apt
/keyrings
/$(basename $ARCHIVE_KEYRING)
71 if [ -e $keyring ]; then
72 old_mtime
=$(stat -c %Y $keyring)
74 (cd /var
/lib
/apt
/keyrings
; wget
-q -N $ARCHIVE_KEYRING_URI)
75 if [ ! -e $keyring ]; then
78 new_mtime
=$(stat -c %Y $keyring)
79 if [ $new_mtime -ne $old_mtime ]; then
80 echo "Checking for new archive signing keys now"
81 add_keys_with_verify_against_master_keyring
$keyring $MASTER_KEYRING
86 if [ ! -f $ARCHIVE_KEYRING ]; then
87 echo >&2 "ERROR: Can't find the archive-keyring"
88 echo >&2 "Is the debian-archive-keyring package installed?"
92 # add new keys from the package;
94 # we do not use add_keys_with_verify_against_master_keyring here,
95 # because "update" is run on regular package updates. A
96 # attacker might as well replace the master-archive-keyring file
97 # in the package and add his own keys. so this check wouldn't
98 # add any security. we *need* this check on net-update though
99 $GPG_CMD --quiet --batch --keyring $ARCHIVE_KEYRING --export | $GPG --import
101 if [ -r "$REMOVED_KEYS" ]; then
102 # remove no-longer supported/used keys
103 keys
=`$GPG_CMD --keyring $REMOVED_KEYS --with-colons --list-keys | grep ^pub | cut -d: -f5`
105 if $GPG --list-keys --with-colons | grep ^pub
| cut
-d: -f5 | grep -q $key; then
106 $GPG --quiet --batch --delete-key --yes ${key}
110 echo "Warning: removed keys keyring $REMOVED_KEYS missing or not readable" >&2
116 echo "Usage: apt-key [--keyring file] [command] [arguments]"
118 echo "Manage apt's list of trusted keys"
120 echo " apt-key add <file> - add the key contained in <file> ('-' for stdin)"
121 echo " apt-key del <keyid> - remove the key <keyid>"
122 echo " apt-key export <keyid> - output the key <keyid>"
123 echo " apt-key exportall - output all trusted keys"
124 echo " apt-key update - update keys using the keyring package"
125 echo " apt-key net-update - update keys using the network"
126 echo " apt-key list - list keys"
127 echo " apt-key finger - list fingerprints"
128 echo " apt-key adv - pass advanced options to gpg (download key)"
130 echo "If no specific keyring file is given the command applies to all keyring files."
133 # Determine on which keyring we want to work
134 if [ "$1" = "--keyring" ]; then
135 #echo "keyfile given"
138 if [ -r "$TRUSTEDFILE" ]; then
139 GPG
="$GPG --keyring $TRUSTEDFILE --primary-keyring $TRUSTEDFILE"
141 echo >&2 "Error: The specified keyring »$TRUSTEDFILE« is missing or not readable"
145 # otherwise use the default
147 #echo "generate list"
148 TRUSTEDFILE
="/etc/apt/trusted.gpg"
149 eval $(apt-config shell TRUSTEDFILE Apt::GPGV::TrustedKeyring)
150 if [ -r "$TRUSTEDFILE" ]; then
151 GPG
="$GPG --keyring $TRUSTEDFILE"
153 GPG
="$GPG --primary-keyring $TRUSTEDFILE"
154 TRUSTEDPARTS
="/etc/apt/trusted.gpg.d"
155 if [ -d "$TRUSTEDPARTS" ]; then
157 for trusted
in $(run-parts --list $TRUSTEDPARTS --regex '^.*\.gpg$'); do
158 #echo "part -> $trusted"
159 GPG
="$GPG --keyring $trusted"
163 #echo "COMMAND: $GPG"
166 if [ -z "$command" ]; then
172 if [ "$command" != "help" ] && ! which gpg
>/dev
/null
2>&1; then
173 echo >&2 "Warning: gnupg does not seem to be installed."
174 echo >&2 "Warning: apt-key requires gnupg for most operations."
180 $GPG --quiet --batch --import "$1"
184 $GPG --quiet --batch --delete-key --yes "$1"
194 $GPG --batch --list-keys
197 $GPG --batch --fingerprint
200 $GPG --armor --export "$1"
203 $GPG --armor --export
206 echo "Executing: $GPG $*"