test-apt-key 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. testsuccessequal 'gpg: key DBAC8DAE: "Joe Sixpack (APT Testcases Dummy) <joe@example.org>" not changed
  36. gpg: Total number processed: 1
  37. gpg: unchanged: 1' aptkey --fakeroot update
  38. testaptkeys 'Joe Sixpack'
  39. testfailure test -e "${ROOTDIR}/etc/apt/trusted.gpg"
  40. testsuccess aptkey --fakeroot add "${KEYDIR}/rexexpired.pub"
  41. testfilestats "${ROOTDIR}/etc/apt/trusted.gpg" '%a' '=' '644'
  42. testaptkeys 'Rex Expired' 'Joe Sixpack'
  43. msgtest 'Check that Sixpack key can be' 'exported'
  44. aptkey export 'Sixpack' > "${TMPWORKINGDIRECTORY}/aptkey.export"
  45. aptkey --keyring "${ROOTDIR}/etc/apt/trusted.gpg.d/joesixpack.gpg" exportall > "${TMPWORKINGDIRECTORY}/aptkey.exportall"
  46. testsuccess --nomsg cmp "${TMPWORKINGDIRECTORY}/aptkey.export" "${TMPWORKINGDIRECTORY}/aptkey.exportall"
  47. testsuccess test -s "${TMPWORKINGDIRECTORY}/aptkey.export"
  48. testsuccess test -s "${TMPWORKINGDIRECTORY}/aptkey.exportall"
  49. msgtest 'Execute update again to trigger removal of' 'Rex Expired key'
  50. testsuccess --nomsg aptkey --fakeroot update
  51. testaptkeys 'Joe Sixpack'
  52. msgtest "Try to remove a key which exists, but isn't in the" 'forced keyring'
  53. testsuccess --nomsg aptkey --fakeroot --keyring "${ROOTDIR}/etc/apt/trusted.gpg" del DBAC8DAE
  54. testaptkeys 'Joe Sixpack'
  55. testsuccess aptkey --fakeroot del DBAC8DAE
  56. testempty aptkey list
  57. msgtest 'Test key removal with' 'lowercase key ID' #keylength somewhere between 8byte and short
  58. cleanplate
  59. cp -a "${KEYDIR}/joesixpack.pub" "${ROOTDIR}/etc/apt/trusted.gpg.d/joesixpack.gpg"
  60. testsuccess --nomsg aptkey --fakeroot del d141dbac8dae
  61. testempty aptkey list
  62. msgtest 'Test key removal with' 'single key in real file'
  63. cleanplate
  64. cp -a "${KEYDIR}/joesixpack.pub" "${ROOTDIR}/etc/apt/trusted.gpg.d/joesixpack.gpg"
  65. testsuccess --nomsg aptkey --fakeroot del DBAC8DAE
  66. testempty aptkey list
  67. testfailure test -e "${ROOTDIR}/etc/apt/trusted.gpg.d/joesixpack.gpg"
  68. testsuccess cmp "${KEYDIR}/joesixpack.pub" "${ROOTDIR}/etc/apt/trusted.gpg.d/joesixpack.gpg~"
  69. msgtest 'Test key removal with' 'different key specs'
  70. cleanplate
  71. cp -a "${KEYDIR}/joesixpack.pub" "${ROOTDIR}/etc/apt/trusted.gpg.d/joesixpack.gpg"
  72. cp -a "${KEYDIR}/marvinparanoid.pub" "${ROOTDIR}/etc/apt/trusted.gpg.d/marvinparanoid.gpg"
  73. testsuccess --nomsg aptkey --fakeroot del 0xDBAC8DAE 528144E2
  74. testempty aptkey list
  75. testfailure test -e "${ROOTDIR}/etc/apt/trusted.gpg.d/joesixpack.gpg"
  76. testsuccess cmp "${KEYDIR}/joesixpack.pub" "${ROOTDIR}/etc/apt/trusted.gpg.d/joesixpack.gpg~"
  77. testfailure test -e "${ROOTDIR}/etc/apt/trusted.gpg.d/marvinparanoid.gpg"
  78. testsuccess cmp "${KEYDIR}/marvinparanoid.pub" "${ROOTDIR}/etc/apt/trusted.gpg.d/marvinparanoid.gpg~"
  79. msgtest 'Test key removal with' 'long key ID'
  80. cleanplate
  81. cp -a "${KEYDIR}/joesixpack.pub" "${ROOTDIR}/etc/apt/trusted.gpg.d/joesixpack.gpg"
  82. testsuccess --nomsg aptkey --fakeroot del 5A90D141DBAC8DAE
  83. testempty aptkey list
  84. testfailure test -e "${ROOTDIR}/etc/apt/trusted.gpg.d/joesixpack.gpg"
  85. testsuccess cmp "${KEYDIR}/joesixpack.pub" "${ROOTDIR}/etc/apt/trusted.gpg.d/joesixpack.gpg~"
  86. msgtest 'Test key removal with' 'fingerprint'
  87. cleanplate
  88. cp -a "${KEYDIR}/joesixpack.pub" "${ROOTDIR}/etc/apt/trusted.gpg.d/joesixpack.gpg"
  89. testsuccess --nomsg aptkey --fakeroot del 34A8E9D18DB320F367E8EAA05A90D141DBAC8DAE
  90. testempty aptkey list
  91. testfailure test -e "${ROOTDIR}/etc/apt/trusted.gpg.d/joesixpack.gpg"
  92. testsuccess cmp "${KEYDIR}/joesixpack.pub" "${ROOTDIR}/etc/apt/trusted.gpg.d/joesixpack.gpg~"
  93. msgtest 'Test key removal with' 'single key in softlink'
  94. cleanplate
  95. ln -s "$(readlink -f "${KEYDIR}/joesixpack.pub")" "${ROOTDIR}/etc/apt/trusted.gpg.d/joesixpack.gpg"
  96. testsuccess --nomsg aptkey --fakeroot del DBAC8DAE
  97. testempty aptkey list
  98. testfailure test -e "${ROOTDIR}/etc/apt/trusted.gpg.d/joesixpack.gpg"
  99. testsuccess test -L "${ROOTDIR}/etc/apt/trusted.gpg.d/joesixpack.gpg~"
  100. cleanplate
  101. testsuccess aptkey --fakeroot add "${KEYDIR}/joesixpack.pub"
  102. ln -sf "$(readlink -f "${KEYDIR}/marvinparanoid.pub")" "${KEYDIR}/marvin paránöid.pub"
  103. testsuccess aptkey --fakeroot add "${KEYDIR}/marvin paránöid.pub"
  104. testaptkeys 'Joe Sixpack' 'Marvin Paranoid'
  105. cp -a "${ROOTDIR}/etc/apt/trusted.gpg" "${KEYDIR}/testcase-multikey.pub" # store for reuse
  106. msgtest 'Test key removal with' 'multi key in real file'
  107. cleanplate
  108. cp -a "${KEYDIR}/testcase-multikey.pub" "${ROOTDIR}/etc/apt/trusted.gpg.d/multikey.gpg"
  109. testsuccess --nomsg aptkey --fakeroot del DBAC8DAE
  110. testaptkeys 'Marvin Paranoid'
  111. testsuccess cmp "${KEYDIR}/testcase-multikey.pub" "${ROOTDIR}/etc/apt/trusted.gpg.d/multikey.gpg~"
  112. msgtest 'Test key removal with' 'multi key in softlink'
  113. cleanplate
  114. ln -s "$(readlink -f "${KEYDIR}/testcase-multikey.pub")" "${ROOTDIR}/etc/apt/trusted.gpg.d/multikey.gpg"
  115. testsuccess --nomsg aptkey --fakeroot del DBAC8DAE
  116. testaptkeys 'Marvin Paranoid'
  117. testsuccess cmp "${KEYDIR}/testcase-multikey.pub" "${ROOTDIR}/etc/apt/trusted.gpg.d/multikey.gpg~"
  118. testfailure test -L "${ROOTDIR}/etc/apt/trusted.gpg.d/multikey.gpg"
  119. testsuccess test -L "${ROOTDIR}/etc/apt/trusted.gpg.d/multikey.gpg~"
  120. msgtest 'Test key removal with' 'multiple files including key'
  121. cleanplate
  122. cp -a "${KEYDIR}/joesixpack.pub" "${ROOTDIR}/etc/apt/trusted.gpg.d/joesixpack.gpg"
  123. cp -a "${KEYDIR}/testcase-multikey.pub" "${ROOTDIR}/etc/apt/trusted.gpg.d/multikey.gpg"
  124. testsuccess --nomsg aptkey --fakeroot del DBAC8DAE
  125. testaptkeys 'Marvin Paranoid'
  126. testfailure test -e "${ROOTDIR}/etc/apt/trusted.gpg.d/joesixpack.gpg"
  127. testsuccess cmp "${KEYDIR}/joesixpack.pub" "${ROOTDIR}/etc/apt/trusted.gpg.d/joesixpack.gpg~"
  128. testsuccess cmp "${KEYDIR}/testcase-multikey.pub" "${ROOTDIR}/etc/apt/trusted.gpg.d/multikey.gpg~"
  129. cleanplate
  130. cp -a "${KEYDIR}/joesixpack.pub" "${ROOTDIR}/etc/apt/trusted.gpg.d/joesixpack.gpg"
  131. cp -a "${KEYDIR}/testcase-multikey.pub" "${ROOTDIR}/etc/apt/trusted.gpg.d/multikey.gpg"
  132. testaptkeys 'Joe Sixpack' 'Joe Sixpack' 'Marvin Paranoid'
  133. msgtest 'Test merge-back of' 'added keys'
  134. testsuccess --nomsg aptkey adv --batch --yes --import "${KEYDIR}/rexexpired.pub"
  135. testaptkeys 'Rex Expired' 'Joe Sixpack' 'Joe Sixpack' 'Marvin Paranoid'
  136. msgtest 'Test merge-back of' 'removed keys'
  137. testsuccess --nomsg aptkey adv --batch --yes --delete-keys 27CE74F9
  138. testaptkeys 'Joe Sixpack' 'Joe Sixpack' 'Marvin Paranoid'
  139. msgtest 'Test merge-back of' 'removed duplicate keys'
  140. testsuccess --nomsg aptkey adv --batch --yes --delete-keys DBAC8DAE
  141. testaptkeys 'Marvin Paranoid'
  142. cleanplate
  143. cp -a "${KEYDIR}/joesixpack.pub" "${ROOTDIR}/etc/apt/trusted.gpg.d/joesixpack.gpg"
  144. cp -a "${KEYDIR}/testcase-multikey.pub" "${ROOTDIR}/etc/apt/trusted.gpg.d/multikey.gpg"
  145. local SIGNATURE="${TMPWORKINGDIRECTORY}/signature"
  146. msgtest 'Test signing a file' 'with a key'
  147. echo 'Verify me. This is my signature.' > "$SIGNATURE"
  148. echo 'lalalalala' > "${SIGNATURE}2"
  149. testsuccess --nomsg aptkey --quiet --keyring "${KEYDIR}/marvinparanoid.pub" --secret-keyring "${KEYDIR}/marvinparanoid.sec" --readonly \
  150. adv --batch --yes --default-key 'Marvin' --armor --detach-sign --sign --output "${SIGNATURE}.gpg" "${SIGNATURE}"
  151. testsuccess test -s "${SIGNATURE}.gpg" -a -s "${SIGNATURE}"
  152. msgtest 'Test verify a file' 'with no sig'
  153. testfailure --nomsg aptkey --quiet --readonly --keyring "${KEYDIR}/testcase-multikey.pub" verify "${SIGNATURE}" "${SIGNATURE}2"
  154. for GPGV in '' 'gpgv' 'gpgv2'; do
  155. echo "APT::Key::GPGVCommand \"$GPGV\";" > "${ROOTDIR}/etc/apt/apt.conf.d/00gpgvcmd"
  156. msgtest 'Test verify a file' 'with all keys'
  157. testsuccess --nomsg aptkey --quiet --readonly verify "${SIGNATURE}.gpg" "${SIGNATURE}"
  158. msgtest 'Test verify a file' 'with good keyring'
  159. testsuccess --nomsg aptkey --quiet --readonly --keyring "${KEYDIR}/testcase-multikey.pub" verify "${SIGNATURE}.gpg" "${SIGNATURE}"
  160. msgtest 'Test fail verify a file' 'with bad keyring'
  161. testfailure --nomsg aptkey --quiet --readonly --keyring "${KEYDIR}/joesixpack.pub" verify "${SIGNATURE}.gpg" "${SIGNATURE}"
  162. msgtest 'Test fail verify a file' 'with non-existing keyring'
  163. testfailure --nomsg aptkey --quiet --readonly --keyring "${KEYDIR}/does-not-exist.pub" verify "${SIGNATURE}.gpg" "${SIGNATURE}"
  164. testfailure test -e "${KEYDIR}/does-not-exist.pub"
  165. # note: this isn't how apts gpgv method implements keyid for verify
  166. msgtest 'Test verify a file' 'with good keyid'
  167. testsuccess --nomsg aptkey --quiet --readonly --keyid 'Paranoid' verify "${SIGNATURE}.gpg" "${SIGNATURE}"
  168. msgtest 'Test fail verify a file' 'with bad keyid'
  169. testfailure --nomsg aptkey --quiet --readonly --keyid 'Sixpack' verify "${SIGNATURE}.gpg" "${SIGNATURE}"
  170. msgtest 'Test fail verify a file' 'with non-existing keyid'
  171. testfailure --nomsg aptkey --quiet --readonly --keyid 'Kalnischkies' verify "${SIGNATURE}.gpg" "${SIGNATURE}"
  172. msgtest 'Test verify fails on' 'bad file'
  173. testfailure --nomsg aptkey --quiet --readonly verify "${SIGNATURE}.gpg" "${SIGNATURE}2"
  174. done
  175. rm -f "${ROOTDIR}/etc/apt/apt.conf.d/00gpgvcmd"
  176. msgtest 'Test verify a file' 'with good keyring'
  177. testsuccess --nomsg aptkey --quiet --readonly --keyring "${KEYDIR}/testcase-multikey.pub" verify "${SIGNATURE}.gpg" "${SIGNATURE}"
  178. cleanplate
  179. cat "${KEYDIR}/joesixpack.pub" "${KEYDIR}/marvinparanoid.pub" > "${KEYDIR}/double.pub"
  180. cat "${KEYDIR}/joesixpack.sec" "${KEYDIR}/marvinparanoid.sec" > "${KEYDIR}/double.sec"
  181. cp -a "${KEYDIR}/double.pub" "${ROOTDIR}/etc/apt/trusted.gpg.d/double.gpg"
  182. cp -a "${KEYDIR}/testcase-multikey.pub" "${ROOTDIR}/etc/apt/trusted.gpg.d/multikey.gpg"
  183. rm -f "${SIGNATURE}.gpg"
  184. testsuccess aptkey --quiet --keyring "${KEYDIR}/double.pub" --secret-keyring "${KEYDIR}/double.sec" --readonly \
  185. adv --batch --yes -u 'Marvin' -u 'Joe' --armor --detach-sign --sign --output "${SIGNATURE}.gpg" "${SIGNATURE}"
  186. testsuccess test -s "${SIGNATURE}.gpg" -a -s "${SIGNATURE}"
  187. for GPGV in '' 'gpgv' 'gpgv2'; do
  188. echo "APT::Key::GPGVCommand \"$GPGV\";" > "${ROOTDIR}/etc/apt/apt.conf.d/00gpgvcmd"
  189. msgtest 'Test verify a doublesigned file' 'with all keys'
  190. testsuccess --nomsg aptkey --quiet --readonly verify "${SIGNATURE}.gpg" "${SIGNATURE}"
  191. msgtest 'Test verify a doublesigned file' 'with good keyring joe'
  192. testmultigpg --keyring "${KEYDIR}/joesixpack.pub" verify "${SIGNATURE}.gpg" "${SIGNATURE}"
  193. msgtest 'Test verify a doublesigned file' 'with good keyring marvin'
  194. testmultigpg --keyring "${KEYDIR}/marvinparanoid.pub" verify "${SIGNATURE}.gpg" "${SIGNATURE}"
  195. msgtest 'Test fail verify a doublesigned file' 'with bad keyring'
  196. testfailure --nomsg aptkey --quiet --readonly --keyring "${KEYDIR}/rexexpired.pub" verify "${SIGNATURE}.gpg" "${SIGNATURE}"
  197. msgtest 'Test fail verify a doublesigned file' 'with non-existing keyring'
  198. testfailure --nomsg aptkey --quiet --readonly --keyring "${KEYDIR}/does-not-exist.pub" verify "${SIGNATURE}.gpg" "${SIGNATURE}"
  199. testfailure test -e "${KEYDIR}/does-not-exist.pub"
  200. # note: this isn't how apts gpgv method implements keyid for verify
  201. msgtest 'Test verify a doublesigned file' 'with good keyid'
  202. testmultigpg --keyid 'Paranoid' verify "${SIGNATURE}.gpg" "${SIGNATURE}"
  203. msgtest 'Test fail verify a doublesigned file' 'with bad keyid'
  204. testfailure --nomsg aptkey --quiet --readonly --keyid 'Rex' verify "${SIGNATURE}.gpg" "${SIGNATURE}"
  205. msgtest 'Test fail verify a doublesigned file' 'with non-existing keyid'
  206. testfailure --nomsg aptkey --quiet --readonly --keyid 'Kalnischkies' verify "${SIGNATURE}.gpg" "${SIGNATURE}"
  207. msgtest 'Test verify fails on' 'bad doublesigned file'
  208. testfailure --nomsg aptkey --quiet --readonly verify "${SIGNATURE}.gpg" "${SIGNATURE}2"
  209. done
  210. rm -f "${ROOTDIR}/etc/apt/apt.conf.d/00gpgvcmd"
  211. }
  212. setupgpgcommand() {
  213. echo "APT::Key::GPGCommand \"$1\";" > "${ROOTDIR}/etc/apt/apt.conf.d/00gpgcmd"
  214. msgmsg 'Force tests to be run with' "$1"
  215. testsuccess aptkey --readonly adv --version
  216. cp "${ROOTDIR}/tmp/testsuccess.output" "${TMPWORKINGDIRECTORY}/aptkey.version"
  217. testsuccess grep "^gpg (GnuPG) $2\." "${TMPWORKINGDIRECTORY}/aptkey.version"
  218. }
  219. # run with default (whatever this is) in current CWD with relative paths
  220. ROOTDIR="./rootdir"
  221. KEYDIR="./keys"
  222. testrun
  223. # run with … and up the game with a strange CWD & absolute paths
  224. ROOTDIR="${TMPWORKINGDIRECTORY}/rootdir"
  225. KEYDIR="${TMPWORKINGDIRECTORY}/keys"
  226. mkdir inaccessible
  227. cd inaccessible
  228. chmod 600 ../inaccessible
  229. testfilestats "${TMPWORKINGDIRECTORY}/inaccessible" '%a' '=' '600'
  230. setupgpgcommand 'gpg' '1'
  231. testrun
  232. setupgpgcommand 'gpg2' '2'
  233. testrun