test-releasefile-verification 15 KB

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