test-releasefile-verification 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. #!/bin/sh
  2. set -e
  3. TESTDIR="$(readlink -f "$(dirname "$0")")"
  4. . "$TESTDIR/framework"
  5. setupenvironment
  6. configarchitecture "i386"
  7. buildaptarchive
  8. setupflataptarchive
  9. changetowebserver
  10. webserverconfig 'aptwebserver::support::range' 'false'
  11. prepare() {
  12. local DATE="${2:-now}"
  13. if [ "$DATE" = 'now' ]; then
  14. if [ "$1" = "${PKGFILE}-new" ]; then
  15. DATE='now - 1 day'
  16. else
  17. DATE='now - 7 day'
  18. fi
  19. fi
  20. for release in $(find rootdir/var/lib/apt/lists 2> /dev/null); do
  21. touch -d 'now - 1 year' "$release"
  22. done
  23. aptget clean
  24. cp "$1" aptarchive/Packages
  25. find aptarchive -name 'Release' -delete
  26. compressfile 'aptarchive/Packages' "$DATE"
  27. generatereleasefiles "$DATE"
  28. }
  29. installaptold() {
  30. rm -rf rootdir/var/cache/apt/archives
  31. testsuccessequal "Reading package lists...
  32. Building dependency tree...
  33. Suggested packages:
  34. aptitude | synaptic | wajig dpkg-dev apt-doc bzip2 lzma python-apt
  35. The following NEW packages will be installed:
  36. apt
  37. 0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
  38. After this operation, 5370 kB of additional disk space will be used.
  39. Get:1 http://localhost:${APTHTTPPORT} apt 0.7.25.3
  40. Download complete and in download only mode" aptget install apt -dy
  41. }
  42. installaptnew() {
  43. testsuccessequal "Reading package lists...
  44. Building dependency tree...
  45. Suggested packages:
  46. aptitude | synaptic | wajig dpkg-dev apt-doc bzip2 lzma python-apt
  47. The following NEW packages will be installed:
  48. apt
  49. 0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
  50. After this operation, 5808 kB of additional disk space will be used.
  51. Get:1 http://localhost:${APTHTTPPORT} apt 0.8.0~pre1
  52. Download complete and in download only mode" aptget install apt -dy
  53. }
  54. failaptold() {
  55. testfailureequal 'Reading package lists...
  56. Building dependency tree...
  57. Suggested packages:
  58. aptitude | synaptic | wajig dpkg-dev apt-doc bzip2 lzma python-apt
  59. The following NEW packages will be installed:
  60. apt
  61. 0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
  62. After this operation, 5370 kB of additional disk space will be used.
  63. WARNING: The following packages cannot be authenticated!
  64. apt
  65. E: There were unauthenticated packages and -y was used without --allow-unauthenticated' aptget install apt -dy
  66. }
  67. failaptnew() {
  68. testfailureequal 'Reading package lists...
  69. Building dependency tree...
  70. Suggested packages:
  71. aptitude | synaptic | wajig dpkg-dev apt-doc bzip2 lzma python-apt
  72. The following NEW packages will be installed:
  73. apt
  74. 0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
  75. After this operation, 5808 kB of additional disk space will be used.
  76. WARNING: The following packages cannot be authenticated!
  77. apt
  78. E: There were unauthenticated packages and -y was used without --allow-unauthenticated' aptget install apt -dy
  79. }
  80. # fake our downloadable file
  81. touch aptarchive/apt.deb
  82. PKGFILE="${TESTDIR}/$(echo "$(basename "$0")" | sed 's#^test-#Packages-#')"
  83. updatewithwarnings() {
  84. testwarning aptget update -o Debug::pkgAcquire::Worker=1 -o Debug::Acquire::gpgv=1
  85. testsuccess grep -E "$1" rootdir/tmp/testwarning.output
  86. }
  87. runtest() {
  88. local DELETEFILE="$1"
  89. msgmsg 'Cold archive signed by' 'Joe Sixpack'
  90. prepare "${PKGFILE}"
  91. rm -rf rootdir/var/lib/apt/lists
  92. signreleasefiles 'Joe Sixpack'
  93. find aptarchive/ -name "$DELETEFILE" -delete
  94. successfulaptgetupdate
  95. testsuccessequal "$(cat "${PKGFILE}")
  96. " aptcache show apt
  97. installaptold
  98. msgmsg 'Good warm archive signed by' 'Joe Sixpack'
  99. prepare "${PKGFILE}-new"
  100. signreleasefiles 'Joe Sixpack'
  101. find aptarchive/ -name "$DELETEFILE" -delete
  102. successfulaptgetupdate
  103. testsuccessequal "$(cat "${PKGFILE}-new")
  104. " aptcache show apt
  105. installaptnew
  106. msgmsg 'Cold archive signed by' 'Rex Expired'
  107. prepare "${PKGFILE}"
  108. rm -rf rootdir/var/lib/apt/lists
  109. cp keys/rexexpired.pub rootdir/etc/apt/trusted.gpg.d/rexexpired.gpg
  110. signreleasefiles 'Rex Expired'
  111. find aptarchive/ -name "$DELETEFILE" -delete
  112. updatewithwarnings '^W: .* EXPKEYSIG'
  113. testsuccessequal "$(cat "${PKGFILE}")
  114. " aptcache show apt
  115. failaptold
  116. rm -f rootdir/etc/apt/trusted.gpg.d/rexexpired.gpg
  117. msgmsg 'Cold archive expired signed by' 'Joe Sixpack'
  118. if dpkg --compare-versions "$(aptkey adv --version | head -n 2 | tail -n 1 | cut -d' ' -f 3)" '>=' '2.1' >/dev/null 2>&1; then
  119. touch rootdir/etc/apt/apt.conf.d/99gnupg2
  120. elif gpg2 --version >/dev/null 2>&1; then
  121. echo 'Apt::Key::gpgcommand "gpg2";' > rootdir/etc/apt/apt.conf.d/99gnupg2
  122. if ! dpkg --compare-versions "$(aptkey adv --version | head -n 2 | tail -n 1 | cut -d' ' -f 3)" '>=' '2.1' >/dev/null 2>&1; then
  123. rm rootdir/etc/apt/apt.conf.d/99gnupg2
  124. fi
  125. fi
  126. if [ -e rootdir/etc/apt/apt.conf.d/99gnupg2 ]; then
  127. prepare "${PKGFILE}"
  128. rm -rf rootdir/var/lib/apt/lists
  129. signreleasefiles 'Joe Sixpack' 'aptarchive' --faked-system-time "20070924T154812" --default-sig-expire 2016-04-01
  130. find aptarchive/ -name "$DELETEFILE" -delete
  131. updatewithwarnings '^W: .* EXPSIG'
  132. testsuccessequal "$(cat "${PKGFILE}")
  133. " aptcache show apt
  134. failaptold
  135. rm -f rootdir/etc/apt/apt.conf.d/99gnupg2
  136. else
  137. msgskip 'Not a new enough gpg available providing --fake-system-time'
  138. fi
  139. msgmsg 'Cold archive signed by' 'Joe Sixpack,Marvin Paranoid'
  140. prepare "${PKGFILE}"
  141. rm -rf rootdir/var/lib/apt/lists
  142. signreleasefiles 'Joe Sixpack,Marvin Paranoid'
  143. find aptarchive/ -name "$DELETEFILE" -delete
  144. successfulaptgetupdate 'NO_PUBKEY'
  145. testsuccessequal "$(cat "${PKGFILE}")
  146. " aptcache show apt
  147. installaptold
  148. msgmsg 'Cold archive signed by' 'Joe Sixpack,Rex Expired'
  149. prepare "${PKGFILE}"
  150. rm -rf rootdir/var/lib/apt/lists
  151. signreleasefiles 'Joe Sixpack,Rex Expired'
  152. find aptarchive/ -name "$DELETEFILE" -delete
  153. cp keys/rexexpired.pub rootdir/etc/apt/trusted.gpg.d/rexexpired.gpg
  154. successfulaptgetupdate 'EXPKEYSIG'
  155. rm -f rootdir/etc/apt/trusted.gpg.d/rexexpired.gpg
  156. testsuccessequal "$(cat "${PKGFILE}")
  157. " aptcache show apt
  158. installaptold
  159. msgmsg 'Cold archive signed by' 'Marvin Paranoid'
  160. prepare "${PKGFILE}"
  161. rm -rf rootdir/var/lib/apt/lists
  162. signreleasefiles 'Marvin Paranoid'
  163. find aptarchive/ -name "$DELETEFILE" -delete
  164. updatewithwarnings '^W: .* NO_PUBKEY'
  165. testsuccessequal "$(cat "${PKGFILE}")
  166. " aptcache show apt
  167. failaptold
  168. msgmsg 'Bad warm archive signed by' 'Joe Sixpack'
  169. prepare "${PKGFILE}-new"
  170. signreleasefiles 'Joe Sixpack'
  171. find aptarchive/ -name "$DELETEFILE" -delete
  172. successfulaptgetupdate
  173. testsuccessequal "$(cat "${PKGFILE}-new")
  174. " aptcache show apt
  175. installaptnew
  176. msgmsg 'Cold archive signed by' 'Joe Sixpack'
  177. prepare "${PKGFILE}"
  178. rm -rf rootdir/var/lib/apt/lists
  179. signreleasefiles 'Joe Sixpack'
  180. find aptarchive/ -name "$DELETEFILE" -delete
  181. successfulaptgetupdate
  182. testsuccessequal "$(cat "${PKGFILE}")
  183. " aptcache show apt
  184. installaptold
  185. msgmsg 'Good warm archive signed by' 'Marvin Paranoid'
  186. prepare "${PKGFILE}-new"
  187. signreleasefiles 'Marvin Paranoid'
  188. find aptarchive/ -name "$DELETEFILE" -delete
  189. updatewithwarnings '^W: .* NO_PUBKEY'
  190. testsuccessequal "$(cat "${PKGFILE}")
  191. " aptcache show apt
  192. installaptold
  193. msgmsg 'Good warm archive signed by' 'Rex Expired'
  194. prepare "${PKGFILE}-new"
  195. cp keys/rexexpired.pub rootdir/etc/apt/trusted.gpg.d/rexexpired.gpg
  196. signreleasefiles 'Rex Expired'
  197. find aptarchive/ -name "$DELETEFILE" -delete
  198. updatewithwarnings '^W: .* EXPKEYSIG'
  199. testsuccessequal "$(cat "${PKGFILE}")
  200. " aptcache show apt
  201. installaptold
  202. rm rootdir/etc/apt/trusted.gpg.d/rexexpired.gpg
  203. msgmsg 'Good warm archive signed by' 'Joe Sixpack'
  204. prepare "${PKGFILE}-new"
  205. signreleasefiles
  206. find aptarchive/ -name "$DELETEFILE" -delete
  207. successfulaptgetupdate
  208. testsuccessequal "$(cat "${PKGFILE}-new")
  209. " aptcache show apt
  210. installaptnew
  211. msgmsg 'Cold archive signed by good keyring' 'Marvin Paranoid'
  212. prepare "${PKGFILE}"
  213. rm -rf rootdir/var/lib/apt/lists
  214. signreleasefiles 'Marvin Paranoid'
  215. find aptarchive/ -name "$DELETEFILE" -delete
  216. local MARVIN="$(readlink -f keys/marvinparanoid.pub)"
  217. sed -i "s#^\(deb\(-src\)\?\) #\1 [signed-by=$MARVIN] #" rootdir/etc/apt/sources.list.d/*
  218. successfulaptgetupdate
  219. testsuccessequal "$(cat "${PKGFILE}")
  220. " aptcache show apt
  221. installaptold
  222. msgmsg 'Cold archive signed by bad keyring' 'Joe Sixpack'
  223. rm -rf rootdir/var/lib/apt/lists
  224. signreleasefiles 'Joe Sixpack'
  225. find aptarchive/ -name "$DELETEFILE" -delete
  226. updatewithwarnings '^W: .* NO_PUBKEY'
  227. sed -i "s#^\(deb\(-src\)\?\) \[signed-by=$MARVIN\] #\1 #" rootdir/etc/apt/sources.list.d/*
  228. local MARVIN="$(aptkey --keyring $MARVIN finger | grep 'Key fingerprint' | cut -d'=' -f 2 | tr -d ' ')"
  229. msgmsg 'Cold archive signed by bad keyid' 'Joe Sixpack'
  230. rm -rf rootdir/var/lib/apt/lists
  231. signreleasefiles 'Joe Sixpack'
  232. find aptarchive/ -name "$DELETEFILE" -delete
  233. sed -i "s#^\(deb\(-src\)\?\) #\1 [signed-by=$MARVIN] #" rootdir/etc/apt/sources.list.d/*
  234. updatewithwarnings '^W: .* be verified because the public key is not available: .*'
  235. msgmsg 'Cold archive signed by good keyid' 'Marvin Paranoid'
  236. rm -rf rootdir/var/lib/apt/lists
  237. signreleasefiles 'Marvin Paranoid'
  238. find aptarchive/ -name "$DELETEFILE" -delete
  239. cp keys/marvinparanoid.pub rootdir/etc/apt/trusted.gpg.d/marvinparanoid.gpg
  240. successfulaptgetupdate
  241. testsuccessequal "$(cat "${PKGFILE}")
  242. " aptcache show apt
  243. installaptold
  244. msgmsg 'Cold archive signed by good keyid' 'Marvin Paranoid,Joe Sixpack'
  245. rm -rf rootdir/var/lib/apt/lists
  246. signreleasefiles 'Marvin Paranoid,Joe Sixpack'
  247. find aptarchive/ -name "$DELETEFILE" -delete
  248. successfulaptgetupdate 'NoPubKey: GOODSIG'
  249. testsuccessequal "$(cat "${PKGFILE}")
  250. " aptcache show apt
  251. installaptold
  252. local SIXPACK="$(aptkey --keyring keys/joesixpack.pub finger | grep 'Key fingerprint' | cut -d'=' -f 2 | tr -d ' ')"
  253. msgmsg 'Cold archive signed by good keyids' 'Joe Sixpack'
  254. rm -rf rootdir/var/lib/apt/lists
  255. signreleasefiles 'Joe Sixpack'
  256. find aptarchive/ -name "$DELETEFILE" -delete
  257. sed -i "s#^\(deb\(-src\)\?\) \[signed-by=$MARVIN\] #\1 [signed-by=${SIXPACK},${MARVIN}] #" rootdir/etc/apt/sources.list.d/*
  258. successfulaptgetupdate
  259. testsuccessequal "$(cat "${PKGFILE}")
  260. " aptcache show apt
  261. installaptold
  262. local SIXPACK="$(aptkey --keyring keys/joesixpack.pub finger | grep 'Key fingerprint' | cut -d'=' -f 2 | tr -d ' ')"
  263. msgmsg 'Cold archive signed by good keyids' 'Joe Sixpack'
  264. rm -rf rootdir/var/lib/apt/lists
  265. sed -i "s#^\(deb\(-src\)\?\) \[signed-by=${SIXPACK},${MARVIN}\] #\1 [signed-by=${MARVIN},${SIXPACK}] #" rootdir/etc/apt/sources.list.d/*
  266. successfulaptgetupdate
  267. testsuccessequal "$(cat "${PKGFILE}")
  268. " aptcache show apt
  269. installaptold
  270. rm -f rootdir/etc/apt/trusted.gpg.d/marvinparanoid.gpg
  271. sed -i "s#^\(deb\(-src\)\?\) \[signed-by=${MARVIN},${SIXPACK}\] #\1 #" rootdir/etc/apt/sources.list.d/*
  272. }
  273. runtest2() {
  274. msgmsg 'Cold archive signed by' 'Joe Sixpack'
  275. prepare "${PKGFILE}"
  276. rm -rf rootdir/var/lib/apt/lists
  277. signreleasefiles 'Joe Sixpack'
  278. successfulaptgetupdate
  279. # New .deb but now an unsigned archive. For example MITM to circumvent
  280. # package verification.
  281. msgmsg 'Warm archive signed by' 'nobody'
  282. prepare "${PKGFILE}-new"
  283. find aptarchive/ -name InRelease -delete
  284. find aptarchive/ -name Release.gpg -delete
  285. updatewithwarnings 'W: .* no longer signed.'
  286. testsuccessequal "$(cat "${PKGFILE}-new")
  287. " aptcache show apt
  288. failaptnew
  289. # Unsigned archive from the beginning must also be detected.
  290. msgmsg 'Cold archive signed by' 'nobody'
  291. rm -rf rootdir/var/lib/apt/lists
  292. updatewithwarnings 'W: .* is not signed.'
  293. testsuccessequal "$(cat "${PKGFILE}-new")
  294. " aptcache show apt
  295. failaptnew
  296. }
  297. runtest3() {
  298. echo "APT::Hashes::$APT_TESTS_DIGEST_ALGO::$1 \"yes\";" > rootdir/etc/apt/apt.conf.d/truststate
  299. msgmsg "Running base test with $1 digest"
  300. runtest2
  301. for DELETEFILE in 'InRelease' 'Release.gpg'; do
  302. msgmsg "Running test with deletion of $DELETEFILE and $1 digest"
  303. runtest "$DELETEFILE"
  304. done
  305. }
  306. # diable some protection by default and ensure we still do the verification
  307. # correctly
  308. cat > rootdir/etc/apt/apt.conf.d/weaken-security <<EOF
  309. Acquire::AllowInsecureRepositories "1";
  310. Acquire::AllowDowngradeToInsecureRepositories "1";
  311. EOF
  312. # the hash marked as configureable in our gpgv method
  313. export APT_TESTS_DIGEST_ALGO='SHA224'
  314. successfulaptgetupdate() {
  315. testsuccess aptget update -o Debug::pkgAcquire::Worker=1 -o Debug::Acquire::gpgv=1
  316. if [ -n "$1" ]; then
  317. cp rootdir/tmp/testsuccess.output aptupdate.output
  318. testsuccess grep "$1" aptupdate.output
  319. fi
  320. }
  321. runtest3 'Trusted'
  322. successfulaptgetupdate() {
  323. testwarning aptget update -o Debug::pkgAcquire::Worker=1 -o Debug::Acquire::gpgv=1
  324. if [ -n "$1" ]; then
  325. testsuccess grep "$1" rootdir/tmp/testwarning.output
  326. fi
  327. testsuccess grep 'uses weak digest algorithm' rootdir/tmp/testwarning.output
  328. }
  329. runtest3 'Weak'
  330. msgmsg "Running test with apt-untrusted digest"
  331. echo "APT::Hashes::$APT_TESTS_DIGEST_ALGO::Untrusted \"yes\";" > rootdir/etc/apt/apt.conf.d/truststate
  332. runfailure() {
  333. for DELETEFILE in 'InRelease' 'Release.gpg'; do
  334. msgmsg 'Cold archive signed by' 'Joe Sixpack'
  335. prepare "${PKGFILE}"
  336. rm -rf rootdir/var/lib/apt/lists
  337. signreleasefiles 'Joe Sixpack'
  338. find aptarchive/ -name "$DELETEFILE" -delete
  339. testfailure aptget update --no-allow-insecure-repositories -o Debug::pkgAcquire::Worker=1 -o Debug::Acquire::gpgv=1
  340. testsuccess grep 'The following signatures were invalid' rootdir/tmp/testfailure.output
  341. testnopackage 'apt'
  342. testwarning aptget update --allow-insecure-repositories -o Debug::pkgAcquire::Worker=1 -o Debug::Acquire::gpgv=1
  343. failaptold
  344. msgmsg 'Cold archive signed by' 'Marvin Paranoid'
  345. prepare "${PKGFILE}"
  346. rm -rf rootdir/var/lib/apt/lists
  347. signreleasefiles 'Marvin Paranoid'
  348. find aptarchive/ -name "$DELETEFILE" -delete
  349. testfailure aptget update --no-allow-insecure-repositories -o Debug::pkgAcquire::Worker=1 -o Debug::Acquire::gpgv=1
  350. testnopackage 'apt'
  351. updatewithwarnings '^W: .* NO_PUBKEY'
  352. testsuccessequal "$(cat "${PKGFILE}")
  353. " aptcache show apt
  354. failaptold
  355. done
  356. }
  357. runfailure
  358. msgmsg "Running test with gpgv-untrusted digest"
  359. export APT_TESTS_DIGEST_ALGO='MD5'
  360. runfailure