test_signed.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. # SPDX-License-Identifier: GPL-2.0+
  2. # Copyright (c) 2019, Linaro Limited
  3. # Author: AKASHI Takahiro <takahiro.akashi@linaro.org>
  4. #
  5. # U-Boot UEFI: Signed Image Authentication Test
  6. """
  7. This test verifies image authentication for signed images.
  8. """
  9. import pytest
  10. @pytest.mark.boardspec('sandbox')
  11. @pytest.mark.buildconfigspec('efi_secure_boot')
  12. @pytest.mark.buildconfigspec('cmd_efidebug')
  13. @pytest.mark.buildconfigspec('cmd_fat')
  14. @pytest.mark.buildconfigspec('cmd_nvedit_efi')
  15. @pytest.mark.slow
  16. class TestEfiSignedImage(object):
  17. def test_efi_signed_image_auth1(self, u_boot_console, efi_boot_env):
  18. """
  19. Test Case 1 - Secure boot is not in force
  20. """
  21. u_boot_console.restart_uboot()
  22. disk_img = efi_boot_env
  23. with u_boot_console.log.section('Test Case 1a'):
  24. # Test Case 1a, run signed image if no PK
  25. output = u_boot_console.run_command_list([
  26. 'host bind 0 %s' % disk_img,
  27. 'efidebug boot add 1 HELLO1 host 0:1 /helloworld.efi.signed ""',
  28. 'efidebug boot next 1',
  29. 'bootefi bootmgr'])
  30. assert 'Hello, world!' in ''.join(output)
  31. with u_boot_console.log.section('Test Case 1b'):
  32. # Test Case 1b, run unsigned image if no PK
  33. output = u_boot_console.run_command_list([
  34. 'efidebug boot add 2 HELLO2 host 0:1 /helloworld.efi ""',
  35. 'efidebug boot next 2',
  36. 'bootefi bootmgr'])
  37. assert 'Hello, world!' in ''.join(output)
  38. def test_efi_signed_image_auth2(self, u_boot_console, efi_boot_env):
  39. """
  40. Test Case 2 - Secure boot is in force,
  41. authenticated by db (TEST_db certificate in db)
  42. """
  43. u_boot_console.restart_uboot()
  44. disk_img = efi_boot_env
  45. with u_boot_console.log.section('Test Case 2a'):
  46. # Test Case 2a, db is not yet installed
  47. output = u_boot_console.run_command_list([
  48. 'host bind 0 %s' % disk_img,
  49. 'fatload host 0:1 4000000 KEK.auth',
  50. 'setenv -e -nv -bs -rt -at -i 4000000,$filesize KEK',
  51. 'fatload host 0:1 4000000 PK.auth',
  52. 'setenv -e -nv -bs -rt -at -i 4000000,$filesize PK'])
  53. assert 'Failed to set EFI variable' not in ''.join(output)
  54. output = u_boot_console.run_command_list([
  55. 'efidebug boot add 1 HELLO1 host 0:1 /helloworld.efi.signed ""',
  56. 'efidebug boot next 1',
  57. 'efidebug test bootmgr'])
  58. assert('\'HELLO1\' failed' in ''.join(output))
  59. assert('efi_start_image() returned: 26' in ''.join(output))
  60. output = u_boot_console.run_command_list([
  61. 'efidebug boot add 2 HELLO2 host 0:1 /helloworld.efi ""',
  62. 'efidebug boot next 2',
  63. 'efidebug test bootmgr'])
  64. assert '\'HELLO2\' failed' in ''.join(output)
  65. assert 'efi_start_image() returned: 26' in ''.join(output)
  66. with u_boot_console.log.section('Test Case 2b'):
  67. # Test Case 2b, authenticated by db
  68. output = u_boot_console.run_command_list([
  69. 'fatload host 0:1 4000000 db.auth',
  70. 'setenv -e -nv -bs -rt -at -i 4000000,$filesize db'])
  71. assert 'Failed to set EFI variable' not in ''.join(output)
  72. output = u_boot_console.run_command_list([
  73. 'efidebug boot next 2',
  74. 'efidebug test bootmgr'])
  75. assert '\'HELLO2\' failed' in ''.join(output)
  76. assert 'efi_start_image() returned: 26' in ''.join(output)
  77. output = u_boot_console.run_command_list([
  78. 'efidebug boot next 1',
  79. 'bootefi bootmgr'])
  80. assert 'Hello, world!' in ''.join(output)
  81. def test_efi_signed_image_auth3(self, u_boot_console, efi_boot_env):
  82. """
  83. Test Case 3 - rejected by dbx (TEST_db certificate in dbx)
  84. """
  85. u_boot_console.restart_uboot()
  86. disk_img = efi_boot_env
  87. with u_boot_console.log.section('Test Case 3a'):
  88. # Test Case 3a, rejected by dbx
  89. output = u_boot_console.run_command_list([
  90. 'host bind 0 %s' % disk_img,
  91. 'fatload host 0:1 4000000 db.auth',
  92. 'setenv -e -nv -bs -rt -at -i 4000000,$filesize dbx',
  93. 'fatload host 0:1 4000000 KEK.auth',
  94. 'setenv -e -nv -bs -rt -at -i 4000000,$filesize KEK',
  95. 'fatload host 0:1 4000000 PK.auth',
  96. 'setenv -e -nv -bs -rt -at -i 4000000,$filesize PK'])
  97. assert 'Failed to set EFI variable' not in ''.join(output)
  98. output = u_boot_console.run_command_list([
  99. 'efidebug boot add 1 HELLO host 0:1 /helloworld.efi.signed ""',
  100. 'efidebug boot next 1',
  101. 'efidebug test bootmgr'])
  102. assert '\'HELLO\' failed' in ''.join(output)
  103. assert 'efi_start_image() returned: 26' in ''.join(output)
  104. with u_boot_console.log.section('Test Case 3b'):
  105. # Test Case 3b, rejected by dbx even if db allows
  106. output = u_boot_console.run_command_list([
  107. 'fatload host 0:1 4000000 db.auth',
  108. 'setenv -e -nv -bs -rt -at -i 4000000,$filesize db'])
  109. assert 'Failed to set EFI variable' not in ''.join(output)
  110. output = u_boot_console.run_command_list([
  111. 'efidebug boot next 1',
  112. 'efidebug test bootmgr'])
  113. assert '\'HELLO\' failed' in ''.join(output)
  114. assert 'efi_start_image() returned: 26' in ''.join(output)
  115. def test_efi_signed_image_auth4(self, u_boot_console, efi_boot_env):
  116. """
  117. Test Case 4 - revoked by dbx (digest of TEST_db certificate in dbx)
  118. """
  119. u_boot_console.restart_uboot()
  120. disk_img = efi_boot_env
  121. with u_boot_console.log.section('Test Case 4'):
  122. # Test Case 4, rejected by dbx
  123. output = u_boot_console.run_command_list([
  124. 'host bind 0 %s' % disk_img,
  125. 'fatload host 0:1 4000000 dbx_hash.auth',
  126. 'setenv -e -nv -bs -rt -at -i 4000000,$filesize dbx',
  127. 'fatload host 0:1 4000000 db.auth',
  128. 'setenv -e -nv -bs -rt -at -i 4000000,$filesize db',
  129. 'fatload host 0:1 4000000 KEK.auth',
  130. 'setenv -e -nv -bs -rt -at -i 4000000,$filesize KEK',
  131. 'fatload host 0:1 4000000 PK.auth',
  132. 'setenv -e -nv -bs -rt -at -i 4000000,$filesize PK'])
  133. assert 'Failed to set EFI variable' not in ''.join(output)
  134. output = u_boot_console.run_command_list([
  135. 'efidebug boot add 1 HELLO host 0:1 /helloworld.efi.signed ""',
  136. 'efidebug boot next 1',
  137. 'efidebug test bootmgr'])
  138. assert '\'HELLO\' failed' in ''.join(output)
  139. assert 'efi_start_image() returned: 26' in ''.join(output)
  140. def test_efi_signed_image_auth5(self, u_boot_console, efi_boot_env):
  141. """
  142. Test Case 5 - multiple signatures
  143. one signed with TEST_db, and
  144. one signed with TEST_db1
  145. """
  146. u_boot_console.restart_uboot()
  147. disk_img = efi_boot_env
  148. with u_boot_console.log.section('Test Case 5a'):
  149. # Test Case 5a, rejected if any of signatures is not verified
  150. output = u_boot_console.run_command_list([
  151. 'host bind 0 %s' % disk_img,
  152. 'fatload host 0:1 4000000 db.auth',
  153. 'setenv -e -nv -bs -rt -at -i 4000000,$filesize db',
  154. 'fatload host 0:1 4000000 KEK.auth',
  155. 'setenv -e -nv -bs -rt -at -i 4000000,$filesize KEK',
  156. 'fatload host 0:1 4000000 PK.auth',
  157. 'setenv -e -nv -bs -rt -at -i 4000000,$filesize PK'])
  158. assert 'Failed to set EFI variable' not in ''.join(output)
  159. output = u_boot_console.run_command_list([
  160. 'efidebug boot add 1 HELLO host 0:1 /helloworld.efi.signed_2sigs ""',
  161. 'efidebug boot next 1',
  162. 'efidebug test bootmgr'])
  163. assert '\'HELLO\' failed' in ''.join(output)
  164. assert 'efi_start_image() returned: 26' in ''.join(output)
  165. with u_boot_console.log.section('Test Case 5b'):
  166. # Test Case 5b, authenticated if both signatures are verified
  167. output = u_boot_console.run_command_list([
  168. 'fatload host 0:1 4000000 db1.auth',
  169. 'setenv -e -nv -bs -rt -at -a -i 4000000,$filesize db'])
  170. assert 'Failed to set EFI variable' not in ''.join(output)
  171. output = u_boot_console.run_command_list([
  172. 'efidebug boot add 1 HELLO host 0:1 /helloworld.efi.signed_2sigs ""',
  173. 'efidebug boot next 1',
  174. 'bootefi bootmgr'])
  175. assert 'Hello, world!' in ''.join(output)
  176. with u_boot_console.log.section('Test Case 5c'):
  177. # Test Case 5c, rejected if any of signatures is revoked
  178. output = u_boot_console.run_command_list([
  179. 'fatload host 0:1 4000000 dbx_hash1.auth',
  180. 'setenv -e -nv -bs -rt -at -i 4000000,$filesize dbx'])
  181. assert 'Failed to set EFI variable' not in ''.join(output)
  182. output = u_boot_console.run_command_list([
  183. 'efidebug boot add 1 HELLO host 0:1 /helloworld.efi.signed_2sigs ""',
  184. 'efidebug boot next 1',
  185. 'efidebug test bootmgr'])
  186. assert '\'HELLO\' failed' in ''.join(output)
  187. assert 'efi_start_image() returned: 26' in ''.join(output)
  188. def test_efi_signed_image_auth6(self, u_boot_console, efi_boot_env):
  189. """
  190. Test Case 6 - using digest of signed image in database
  191. """
  192. u_boot_console.restart_uboot()
  193. disk_img = efi_boot_env
  194. with u_boot_console.log.section('Test Case 6a'):
  195. # Test Case 6a, verified by image's digest in db
  196. output = u_boot_console.run_command_list([
  197. 'host bind 0 %s' % disk_img,
  198. 'fatload host 0:1 4000000 db_hello_signed.auth',
  199. 'setenv -e -nv -bs -rt -at -i 4000000,$filesize db',
  200. 'fatload host 0:1 4000000 KEK.auth',
  201. 'setenv -e -nv -bs -rt -at -i 4000000,$filesize KEK',
  202. 'fatload host 0:1 4000000 PK.auth',
  203. 'setenv -e -nv -bs -rt -at -i 4000000,$filesize PK'])
  204. assert 'Failed to set EFI variable' not in ''.join(output)
  205. output = u_boot_console.run_command_list([
  206. 'efidebug boot add 1 HELLO host 0:1 /helloworld.efi.signed ""',
  207. 'efidebug boot next 1',
  208. 'bootefi bootmgr'])
  209. assert 'Hello, world!' in ''.join(output)
  210. with u_boot_console.log.section('Test Case 6b'):
  211. # Test Case 6b, rejected by TEST_db certificate in dbx
  212. output = u_boot_console.run_command_list([
  213. 'fatload host 0:1 4000000 dbx_db.auth',
  214. 'setenv -e -nv -bs -rt -at -i 4000000,$filesize dbx'])
  215. assert 'Failed to set EFI variable' not in ''.join(output)
  216. output = u_boot_console.run_command_list([
  217. 'efidebug boot next 1',
  218. 'efidebug test bootmgr'])
  219. assert '\'HELLO\' failed' in ''.join(output)
  220. assert 'efi_start_image() returned: 26' in ''.join(output)
  221. with u_boot_console.log.section('Test Case 6c'):
  222. # Test Case 6c, rejected by image's digest in dbx
  223. output = u_boot_console.run_command_list([
  224. 'fatload host 0:1 4000000 db.auth',
  225. 'setenv -e -nv -bs -rt -at -i 4000000,$filesize db',
  226. 'fatload host 0:1 4000000 dbx_hello_signed.auth',
  227. 'setenv -e -nv -bs -rt -at -i 4000000,$filesize dbx'])
  228. assert 'Failed to set EFI variable' not in ''.join(output)
  229. output = u_boot_console.run_command_list([
  230. 'efidebug boot next 1',
  231. 'efidebug test bootmgr'])
  232. assert '\'HELLO\' failed' in ''.join(output)
  233. assert 'efi_start_image() returned: 26' in ''.join(output)