test-apt-key 12 KB

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