test-apt-key 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. #!/bin/sh
  2. set -e
  3. # apt-key is a shell script, so relatively prune to be effected by 'crazy' things:
  4. # confuses config parser as there exists no way of escaping " currently.
  5. #TMPDIR="$(mktemp -d)/This is \"fü\$\$ing cràzy\", \$(man man | head -n1 | cut -d' ' -f 1)\$!"
  6. # gpg doesn't like | in path names – documented e.g. in the man gpg2 --agent-program
  7. #TMPDIR="$(mktemp -d)/This is fü\$\$ing cràzy, \$(man man | head -n1 | cut -d' ' -f 1)\$!"
  8. TMPDIR_ADD="This is fü\$\$ing cràzy, \$(apt -v)\$!"
  9. TESTDIR="$(readlink -f "$(dirname "$0")")"
  10. . "$TESTDIR/framework"
  11. setupenvironment
  12. configarchitecture 'amd64'
  13. # start from a clean plate again
  14. cleanplate() {
  15. rm -rf "${ROOTDIR}/etc/apt/trusted.gpg.d/" "${ROOTDIR}/etc/apt/trusted.gpg"
  16. mkdir "${ROOTDIR}/etc/apt/trusted.gpg.d/"
  17. }
  18. testmultigpg() {
  19. testfailure --nomsg aptkey --quiet --readonly "$@"
  20. testsuccess grep "^gpgv: Can't check signature" "${ROOTDIR}/tmp/testfailure.output"
  21. testsuccess grep '^gpgv: Good signature from' "${ROOTDIR}/tmp/testfailure.output"
  22. }
  23. testrun() {
  24. echo "APT::Key::ArchiveKeyring \"${KEYDIR}/joesixpack.pub\";
  25. APT::Key::RemovedKeys \"${KEYDIR}/rexexpired.pub\";" > "${ROOTDIR}/etc/apt/apt.conf.d/aptkey.conf"
  26. cleanplate
  27. ln -sf "$(readlink -f "${KEYDIR}/joesixpack.pub")" "${ROOTDIR}/etc/apt/trusted.gpg.d/joesixpack.gpg"
  28. testaptkeys 'Joe Sixpack'
  29. testsuccess aptkey list
  30. msgtest 'Check that paths in list output are not' 'double-slashed'
  31. testfailure --nomsg grep '//' "${ROOTDIR}/tmp/testsuccess.output"
  32. testsuccess aptkey finger
  33. msgtest 'Check that paths in finger output are not' 'double-slashed'
  34. testfailure --nomsg grep '//' "${ROOTDIR}/tmp/testsuccess.output"
  35. testequalor2 'gpg: key DBAC8DAE: "Joe Sixpack (APT Testcases Dummy) <joe@example.org>" not changed
  36. gpg: Total number processed: 1
  37. gpg: unchanged: 1' 'gpg: key 5A90D141DBAC8DAE: "Joe Sixpack (APT Testcases Dummy) <joe@example.org>" not changed
  38. gpg: Total number processed: 1
  39. gpg: unchanged: 1' aptkey --fakeroot update
  40. testaptkeys 'Joe Sixpack'
  41. testfailure test -e "${ROOTDIR}/etc/apt/trusted.gpg"
  42. testsuccess aptkey --fakeroot add "${KEYDIR}/rexexpired.pub"
  43. testfilestats "${ROOTDIR}/etc/apt/trusted.gpg" '%a' '=' '644'
  44. testaptkeys 'Rex Expired' 'Joe Sixpack'
  45. msgtest 'Check that Sixpack key can be' 'exported'
  46. aptkey export 'Sixpack' > "${TMPWORKINGDIRECTORY}/aptkey.export"
  47. aptkey --keyring "${ROOTDIR}/etc/apt/trusted.gpg.d/joesixpack.gpg" exportall > "${TMPWORKINGDIRECTORY}/aptkey.exportall"
  48. testsuccess --nomsg cmp "${TMPWORKINGDIRECTORY}/aptkey.export" "${TMPWORKINGDIRECTORY}/aptkey.exportall"
  49. testsuccess test -s "${TMPWORKINGDIRECTORY}/aptkey.export"
  50. testsuccess test -s "${TMPWORKINGDIRECTORY}/aptkey.exportall"
  51. msgtest 'Execute update again to trigger removal of' 'Rex Expired key'
  52. testsuccess --nomsg aptkey --fakeroot update
  53. testaptkeys 'Joe Sixpack'
  54. msgtest "Try to remove a key which exists, but isn't in the" 'forced keyring'
  55. testsuccess --nomsg aptkey --fakeroot --keyring "${ROOTDIR}/etc/apt/trusted.gpg" del DBAC8DAE
  56. testaptkeys 'Joe Sixpack'
  57. testsuccess aptkey --fakeroot del DBAC8DAE
  58. testempty aptkey list
  59. msgtest 'Test key removal with' 'lowercase key ID' #keylength somewhere between 8byte and short
  60. cleanplate
  61. cp -a "${KEYDIR}/joesixpack.pub" "${ROOTDIR}/etc/apt/trusted.gpg.d/joesixpack.gpg"
  62. testsuccess --nomsg aptkey --fakeroot del d141dbac8dae
  63. testempty aptkey list
  64. msgtest 'Test key removal with' 'single key in real file'
  65. cleanplate
  66. cp -a "${KEYDIR}/joesixpack.pub" "${ROOTDIR}/etc/apt/trusted.gpg.d/joesixpack.gpg"
  67. testsuccess --nomsg aptkey --fakeroot del DBAC8DAE
  68. testempty aptkey list
  69. testfailure test -e "${ROOTDIR}/etc/apt/trusted.gpg.d/joesixpack.gpg"
  70. testsuccess cmp "${KEYDIR}/joesixpack.pub" "${ROOTDIR}/etc/apt/trusted.gpg.d/joesixpack.gpg~"
  71. msgtest 'Test key removal with' 'different key specs'
  72. cleanplate
  73. cp -a "${KEYDIR}/joesixpack.pub" "${ROOTDIR}/etc/apt/trusted.gpg.d/joesixpack.gpg"
  74. cp -a "${KEYDIR}/marvinparanoid.pub" "${ROOTDIR}/etc/apt/trusted.gpg.d/marvinparanoid.gpg"
  75. testsuccess --nomsg aptkey --fakeroot del 0xDBAC8DAE 528144E2
  76. testempty aptkey list
  77. testfailure test -e "${ROOTDIR}/etc/apt/trusted.gpg.d/joesixpack.gpg"
  78. testsuccess cmp "${KEYDIR}/joesixpack.pub" "${ROOTDIR}/etc/apt/trusted.gpg.d/joesixpack.gpg~"
  79. testfailure test -e "${ROOTDIR}/etc/apt/trusted.gpg.d/marvinparanoid.gpg"
  80. testsuccess cmp "${KEYDIR}/marvinparanoid.pub" "${ROOTDIR}/etc/apt/trusted.gpg.d/marvinparanoid.gpg~"
  81. msgtest 'Test key removal with' 'long key ID'
  82. cleanplate
  83. cp -a "${KEYDIR}/joesixpack.pub" "${ROOTDIR}/etc/apt/trusted.gpg.d/joesixpack.gpg"
  84. testsuccess --nomsg aptkey --fakeroot del 5A90D141DBAC8DAE
  85. testempty aptkey list
  86. testfailure test -e "${ROOTDIR}/etc/apt/trusted.gpg.d/joesixpack.gpg"
  87. testsuccess cmp "${KEYDIR}/joesixpack.pub" "${ROOTDIR}/etc/apt/trusted.gpg.d/joesixpack.gpg~"
  88. msgtest 'Test key removal with' 'fingerprint'
  89. cleanplate
  90. cp -a "${KEYDIR}/joesixpack.pub" "${ROOTDIR}/etc/apt/trusted.gpg.d/joesixpack.gpg"
  91. testsuccess --nomsg aptkey --fakeroot del 34A8E9D18DB320F367E8EAA05A90D141DBAC8DAE
  92. testempty aptkey list
  93. testfailure test -e "${ROOTDIR}/etc/apt/trusted.gpg.d/joesixpack.gpg"
  94. testsuccess cmp "${KEYDIR}/joesixpack.pub" "${ROOTDIR}/etc/apt/trusted.gpg.d/joesixpack.gpg~"
  95. msgtest 'Test key removal with' 'spaced fingerprint'
  96. cleanplate
  97. cp -a "${KEYDIR}/joesixpack.pub" "${ROOTDIR}/etc/apt/trusted.gpg.d/joesixpack.gpg"
  98. testsuccess --nomsg aptkey --fakeroot del '34A8 E9D1 8DB3 20F3 67E8 EAA0 5A90 D141 DBAC 8DAE'
  99. testempty aptkey list
  100. testfailure test -e "${ROOTDIR}/etc/apt/trusted.gpg.d/joesixpack.gpg"
  101. testsuccess cmp "${KEYDIR}/joesixpack.pub" "${ROOTDIR}/etc/apt/trusted.gpg.d/joesixpack.gpg~"
  102. msgtest 'Test key removal with' 'single key in softlink'
  103. cleanplate
  104. ln -s "$(readlink -f "${KEYDIR}/joesixpack.pub")" "${ROOTDIR}/etc/apt/trusted.gpg.d/joesixpack.gpg"
  105. testsuccess --nomsg aptkey --fakeroot del DBAC8DAE
  106. testempty aptkey list
  107. testfailure test -e "${ROOTDIR}/etc/apt/trusted.gpg.d/joesixpack.gpg"
  108. testsuccess test -L "${ROOTDIR}/etc/apt/trusted.gpg.d/joesixpack.gpg~"
  109. cleanplate
  110. testsuccess aptkey --fakeroot add "${KEYDIR}/joesixpack.pub"
  111. ln -sf "$(readlink -f "${KEYDIR}/marvinparanoid.pub")" "${KEYDIR}/marvin paránöid.pub"
  112. testsuccess aptkey --fakeroot add "${KEYDIR}/marvin paránöid.pub"
  113. testaptkeys 'Joe Sixpack' 'Marvin Paranoid'
  114. cp -a "${ROOTDIR}/etc/apt/trusted.gpg" "${KEYDIR}/testcase-multikey.pub" # store for reuse
  115. msgtest 'Test key removal with' 'multi key in real file'
  116. cleanplate
  117. cp -a "${KEYDIR}/testcase-multikey.pub" "${ROOTDIR}/etc/apt/trusted.gpg.d/multikey.gpg"
  118. testsuccess --nomsg aptkey --fakeroot del DBAC8DAE
  119. testaptkeys 'Marvin Paranoid'
  120. testsuccess cmp "${KEYDIR}/testcase-multikey.pub" "${ROOTDIR}/etc/apt/trusted.gpg.d/multikey.gpg~"
  121. msgtest 'Test key removal with' 'multi key in softlink'
  122. cleanplate
  123. ln -s "$(readlink -f "${KEYDIR}/testcase-multikey.pub")" "${ROOTDIR}/etc/apt/trusted.gpg.d/multikey.gpg"
  124. testsuccess --nomsg aptkey --fakeroot del DBAC8DAE
  125. testaptkeys 'Marvin Paranoid'
  126. testsuccess cmp "${KEYDIR}/testcase-multikey.pub" "${ROOTDIR}/etc/apt/trusted.gpg.d/multikey.gpg~"
  127. testfailure test -L "${ROOTDIR}/etc/apt/trusted.gpg.d/multikey.gpg"
  128. testsuccess test -L "${ROOTDIR}/etc/apt/trusted.gpg.d/multikey.gpg~"
  129. msgtest 'Test key removal with' 'multiple files including key'
  130. cleanplate
  131. cp -a "${KEYDIR}/joesixpack.pub" "${ROOTDIR}/etc/apt/trusted.gpg.d/joesixpack.gpg"
  132. cp -a "${KEYDIR}/testcase-multikey.pub" "${ROOTDIR}/etc/apt/trusted.gpg.d/multikey.gpg"
  133. testsuccess --nomsg aptkey --fakeroot del DBAC8DAE
  134. testaptkeys 'Marvin Paranoid'
  135. testfailure test -e "${ROOTDIR}/etc/apt/trusted.gpg.d/joesixpack.gpg"
  136. testsuccess cmp "${KEYDIR}/joesixpack.pub" "${ROOTDIR}/etc/apt/trusted.gpg.d/joesixpack.gpg~"
  137. testsuccess cmp "${KEYDIR}/testcase-multikey.pub" "${ROOTDIR}/etc/apt/trusted.gpg.d/multikey.gpg~"
  138. cleanplate
  139. cp -a "${KEYDIR}/joesixpack.pub" "${ROOTDIR}/etc/apt/trusted.gpg.d/joesixpack.gpg"
  140. cp -a "${KEYDIR}/testcase-multikey.pub" "${ROOTDIR}/etc/apt/trusted.gpg.d/multikey.gpg"
  141. testaptkeys 'Joe Sixpack' 'Joe Sixpack' 'Marvin Paranoid'
  142. msgtest 'Test merge-back of' 'added keys'
  143. testsuccess --nomsg aptkey adv --batch --yes --import "${KEYDIR}/rexexpired.pub"
  144. testaptkeys 'Rex Expired' 'Joe Sixpack' 'Joe Sixpack' 'Marvin Paranoid'
  145. msgtest 'Test merge-back of' 'removed keys'
  146. testsuccess --nomsg aptkey adv --batch --yes --delete-keys 27CE74F9
  147. testaptkeys 'Joe Sixpack' 'Joe Sixpack' 'Marvin Paranoid'
  148. msgtest 'Test merge-back of' 'removed duplicate keys'
  149. testsuccess --nomsg aptkey adv --batch --yes --delete-keys DBAC8DAE
  150. testaptkeys 'Marvin Paranoid'
  151. cleanplate
  152. cp -a "${KEYDIR}/joesixpack.pub" "${ROOTDIR}/etc/apt/trusted.gpg.d/joesixpack.gpg"
  153. cp -a "${KEYDIR}/testcase-multikey.pub" "${ROOTDIR}/etc/apt/trusted.gpg.d/multikey.gpg"
  154. local SIGNATURE="${TMPWORKINGDIRECTORY}/signature"
  155. msgtest 'Test signing a file' 'with a key'
  156. echo 'Verify me. This is my signature.' > "$SIGNATURE"
  157. echo 'lalalalala' > "${SIGNATURE}2"
  158. testsuccess --nomsg aptkey --quiet --keyring "${KEYDIR}/marvinparanoid.pub" --secret-keyring "${KEYDIR}/marvinparanoid.sec" --readonly \
  159. adv --batch --yes --default-key 'Marvin' --armor --detach-sign --sign --output "${SIGNATURE}.gpg" "${SIGNATURE}"
  160. testsuccess test -s "${SIGNATURE}.gpg" -a -s "${SIGNATURE}"
  161. msgtest 'Test verify a file' 'with no sig'
  162. testfailure --nomsg aptkey --quiet --readonly --keyring "${KEYDIR}/testcase-multikey.pub" verify "${SIGNATURE}" "${SIGNATURE}2"
  163. for GPGV in '' 'gpgv' 'gpgv1' 'gpgv2'; do
  164. echo "APT::Key::GPGVCommand \"$GPGV\";" > "${ROOTDIR}/etc/apt/apt.conf.d/00gpgvcmd"
  165. if [ -n "$GPGV" ] && ! command dpkg -l gnupg1 2>&1 | grep -q '^ii'; then continue; fi
  166. msgtest 'Test verify a file' 'with all keys'
  167. testsuccess --nomsg aptkey --quiet --readonly verify "${SIGNATURE}.gpg" "${SIGNATURE}"
  168. msgtest 'Test verify a file' 'with good keyring'
  169. testsuccess --nomsg aptkey --quiet --readonly --keyring "${KEYDIR}/testcase-multikey.pub" verify "${SIGNATURE}.gpg" "${SIGNATURE}"
  170. msgtest 'Test fail verify a file' 'with bad keyring'
  171. testfailure --nomsg aptkey --quiet --readonly --keyring "${KEYDIR}/joesixpack.pub" verify "${SIGNATURE}.gpg" "${SIGNATURE}"
  172. msgtest 'Test fail verify a file' 'with non-existing keyring'
  173. testfailure --nomsg aptkey --quiet --readonly --keyring "${KEYDIR}/does-not-exist.pub" verify "${SIGNATURE}.gpg" "${SIGNATURE}"
  174. testfailure test -e "${KEYDIR}/does-not-exist.pub"
  175. # note: this isn't how apts gpgv method implements keyid for verify
  176. msgtest 'Test verify a file' 'with good keyid'
  177. testsuccess --nomsg aptkey --quiet --readonly --keyid 'Paranoid' verify "${SIGNATURE}.gpg" "${SIGNATURE}"
  178. msgtest 'Test fail verify a file' 'with bad keyid'
  179. testfailure --nomsg aptkey --quiet --readonly --keyid 'Sixpack' verify "${SIGNATURE}.gpg" "${SIGNATURE}"
  180. msgtest 'Test fail verify a file' 'with non-existing keyid'
  181. testfailure --nomsg aptkey --quiet --readonly --keyid 'Kalnischkies' verify "${SIGNATURE}.gpg" "${SIGNATURE}"
  182. msgtest 'Test verify fails on' 'bad file'
  183. testfailure --nomsg aptkey --quiet --readonly verify "${SIGNATURE}.gpg" "${SIGNATURE}2"
  184. done
  185. rm -f "${ROOTDIR}/etc/apt/apt.conf.d/00gpgvcmd"
  186. msgtest 'Test verify a file' 'with good keyring'
  187. testsuccess --nomsg aptkey --quiet --readonly --keyring "${KEYDIR}/testcase-multikey.pub" verify "${SIGNATURE}.gpg" "${SIGNATURE}"
  188. cleanplate
  189. cat "${KEYDIR}/joesixpack.pub" "${KEYDIR}/marvinparanoid.pub" > "${KEYDIR}/double.pub"
  190. cat "${KEYDIR}/joesixpack.sec" "${KEYDIR}/marvinparanoid.sec" > "${KEYDIR}/double.sec"
  191. cp -a "${KEYDIR}/double.pub" "${ROOTDIR}/etc/apt/trusted.gpg.d/double.gpg"
  192. cp -a "${KEYDIR}/testcase-multikey.pub" "${ROOTDIR}/etc/apt/trusted.gpg.d/multikey.gpg"
  193. rm -f "${SIGNATURE}.gpg"
  194. testsuccess aptkey --quiet --keyring "${KEYDIR}/double.pub" --secret-keyring "${KEYDIR}/double.sec" --readonly \
  195. adv --batch --yes -u 'Marvin' -u 'Joe' --armor --detach-sign --sign --output "${SIGNATURE}.gpg" "${SIGNATURE}"
  196. testsuccess test -s "${SIGNATURE}.gpg" -a -s "${SIGNATURE}"
  197. for GPGV in '' 'gpgv' 'gpgv1' 'gpgv2'; do
  198. echo "APT::Key::GPGVCommand \"$GPGV\";" > "${ROOTDIR}/etc/apt/apt.conf.d/00gpgvcmd"
  199. if [ -n "$GPGV" ] && ! command dpkg -l gnupg1 2>&1 | grep -q '^ii'; then continue; fi
  200. msgtest 'Test verify a doublesigned file' 'with all keys'
  201. testsuccess --nomsg aptkey --quiet --readonly verify "${SIGNATURE}.gpg" "${SIGNATURE}"
  202. msgtest 'Test verify a doublesigned file' 'with good keyring joe'
  203. testmultigpg --keyring "${KEYDIR}/joesixpack.pub" verify "${SIGNATURE}.gpg" "${SIGNATURE}"
  204. msgtest 'Test verify a doublesigned file' 'with good keyring marvin'
  205. testmultigpg --keyring "${KEYDIR}/marvinparanoid.pub" verify "${SIGNATURE}.gpg" "${SIGNATURE}"
  206. msgtest 'Test fail verify a doublesigned file' 'with bad keyring'
  207. testfailure --nomsg aptkey --quiet --readonly --keyring "${KEYDIR}/rexexpired.pub" verify "${SIGNATURE}.gpg" "${SIGNATURE}"
  208. msgtest 'Test fail verify a doublesigned file' 'with non-existing keyring'
  209. testfailure --nomsg aptkey --quiet --readonly --keyring "${KEYDIR}/does-not-exist.pub" verify "${SIGNATURE}.gpg" "${SIGNATURE}"
  210. testfailure test -e "${KEYDIR}/does-not-exist.pub"
  211. # note: this isn't how apts gpgv method implements keyid for verify
  212. msgtest 'Test verify a doublesigned file' 'with good keyid'
  213. testmultigpg --keyid 'Paranoid' verify "${SIGNATURE}.gpg" "${SIGNATURE}"
  214. msgtest 'Test fail verify a doublesigned file' 'with bad keyid'
  215. testfailure --nomsg aptkey --quiet --readonly --keyid 'Rex' verify "${SIGNATURE}.gpg" "${SIGNATURE}"
  216. msgtest 'Test fail verify a doublesigned file' 'with non-existing keyid'
  217. testfailure --nomsg aptkey --quiet --readonly --keyid 'Kalnischkies' verify "${SIGNATURE}.gpg" "${SIGNATURE}"
  218. msgtest 'Test verify fails on' 'bad doublesigned file'
  219. testfailure --nomsg aptkey --quiet --readonly verify "${SIGNATURE}.gpg" "${SIGNATURE}2"
  220. done
  221. rm -f "${ROOTDIR}/etc/apt/apt.conf.d/00gpgvcmd"
  222. }
  223. setupgpgcommand() {
  224. local GPGEXE;
  225. if command dpkg -l gnupg1 2>&1 | grep -q '^ii'; then
  226. if [ "$1" = '1' ]; then
  227. GPGEXE='gpg1'
  228. else
  229. GPGEXE='gpg'
  230. fi
  231. else
  232. if [ "$1" = '1' ]; then
  233. GPGEXE='gpg'
  234. else
  235. GPGEXE='gpg2'
  236. fi
  237. fi
  238. msgmsg 'Force tests to be run with' "$GPGEXE"
  239. echo "APT::Key::GPGCommand \"$GPGEXE\";" > "${ROOTDIR}/etc/apt/apt.conf.d/00gpgcmd"
  240. testsuccess aptkey --readonly adv --version
  241. cp "${ROOTDIR}/tmp/testsuccess.output" "${TMPWORKINGDIRECTORY}/aptkey.version"
  242. testsuccess grep "^gpg (GnuPG) $1\." "${TMPWORKINGDIRECTORY}/aptkey.version"
  243. }
  244. # run with default (whatever this is) in current CWD with relative paths
  245. ROOTDIR="./rootdir"
  246. KEYDIR="./keys"
  247. testrun
  248. # run with … and up the game with a strange CWD & absolute paths
  249. ROOTDIR="${TMPWORKINGDIRECTORY}/rootdir"
  250. KEYDIR="${TMPWORKINGDIRECTORY}/keys"
  251. mkdir inaccessible
  252. cd inaccessible
  253. chmod 600 ../inaccessible
  254. testfilestats "${TMPWORKINGDIRECTORY}/inaccessible" '%a' '=' '600'
  255. setupgpgcommand '1'
  256. testrun
  257. setupgpgcommand '2'
  258. testrun