test_authvar.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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: Variable Authentication Test
  6. """
  7. This test verifies variable authentication
  8. """
  9. import pytest
  10. @pytest.mark.boardspec('sandbox')
  11. @pytest.mark.buildconfigspec('efi_secure_boot')
  12. @pytest.mark.buildconfigspec('cmd_fat')
  13. @pytest.mark.buildconfigspec('cmd_nvedit_efi')
  14. @pytest.mark.slow
  15. class TestEfiAuthVar(object):
  16. def test_efi_var_auth1(self, u_boot_console, efi_boot_env):
  17. """
  18. Test Case 1 - Install signature database
  19. """
  20. u_boot_console.restart_uboot()
  21. disk_img = efi_boot_env
  22. with u_boot_console.log.section('Test Case 1a'):
  23. # Test Case 1a, Initial secure state
  24. output = u_boot_console.run_command_list([
  25. 'host bind 0 %s' % disk_img,
  26. 'printenv -e SecureBoot'])
  27. assert '00000000: 00' in ''.join(output)
  28. output = u_boot_console.run_command(
  29. 'printenv -e SetupMode')
  30. assert '00000000: 01' in output
  31. with u_boot_console.log.section('Test Case 1b'):
  32. # Test Case 1b, PK without AUTHENTICATED_WRITE_ACCESS
  33. output = u_boot_console.run_command_list([
  34. 'fatload host 0:1 4000000 PK.auth',
  35. 'setenv -e -nv -bs -rt -i 4000000:$filesize PK'])
  36. assert 'Failed to set EFI variable' in ''.join(output)
  37. with u_boot_console.log.section('Test Case 1c'):
  38. # Test Case 1c, install PK
  39. output = u_boot_console.run_command_list([
  40. 'fatload host 0:1 4000000 PK.auth',
  41. 'setenv -e -nv -bs -rt -at -i 4000000:$filesize PK',
  42. 'printenv -e -n PK'])
  43. assert 'PK:' in ''.join(output)
  44. output = u_boot_console.run_command(
  45. 'printenv -e SecureBoot')
  46. assert '00000000: 01' in output
  47. output = u_boot_console.run_command(
  48. 'printenv -e SetupMode')
  49. assert '00000000: 00' in output
  50. with u_boot_console.log.section('Test Case 1d'):
  51. # Test Case 1d, db/dbx without KEK
  52. output = u_boot_console.run_command_list([
  53. 'fatload host 0:1 4000000 db.auth',
  54. 'setenv -e -nv -bs -rt -at -i 4000000:$filesize db'])
  55. assert 'Failed to set EFI variable' in ''.join(output)
  56. output = u_boot_console.run_command_list([
  57. 'fatload host 0:1 4000000 db.auth',
  58. 'setenv -e -nv -bs -rt -at -i 4000000:$filesize dbx'])
  59. assert 'Failed to set EFI variable' in ''.join(output)
  60. with u_boot_console.log.section('Test Case 1e'):
  61. # Test Case 1e, install KEK
  62. output = u_boot_console.run_command_list([
  63. 'fatload host 0:1 4000000 KEK.auth',
  64. 'setenv -e -nv -bs -rt -i 4000000:$filesize KEK'])
  65. assert 'Failed to set EFI variable' in ''.join(output)
  66. output = u_boot_console.run_command_list([
  67. 'fatload host 0:1 4000000 KEK.auth',
  68. 'setenv -e -nv -bs -rt -at -i 4000000:$filesize KEK',
  69. 'printenv -e -n KEK'])
  70. assert 'KEK:' in ''.join(output)
  71. output = u_boot_console.run_command(
  72. 'printenv -e SecureBoot')
  73. assert '00000000: 01' in output
  74. with u_boot_console.log.section('Test Case 1f'):
  75. # Test Case 1f, install db
  76. output = u_boot_console.run_command_list([
  77. 'fatload host 0:1 4000000 db.auth',
  78. 'setenv -e -nv -bs -rt -i 4000000:$filesize db'])
  79. assert 'Failed to set EFI variable' in ''.join(output)
  80. output = u_boot_console.run_command_list([
  81. 'fatload host 0:1 4000000 db.auth',
  82. 'setenv -e -nv -bs -rt -at -i 4000000:$filesize db',
  83. 'printenv -e -n -guid d719b2cb-3d3a-4596-a3bc-dad00e67656f db'])
  84. assert 'Failed to set EFI variable' not in ''.join(output)
  85. assert 'db:' in ''.join(output)
  86. output = u_boot_console.run_command(
  87. 'printenv -e SecureBoot')
  88. assert '00000000: 01' in output
  89. with u_boot_console.log.section('Test Case 1g'):
  90. # Test Case 1g, install dbx
  91. output = u_boot_console.run_command_list([
  92. 'fatload host 0:1 4000000 dbx.auth',
  93. 'setenv -e -nv -bs -rt -i 4000000:$filesize dbx'])
  94. assert 'Failed to set EFI variable' in ''.join(output)
  95. output = u_boot_console.run_command_list([
  96. 'fatload host 0:1 4000000 dbx.auth',
  97. 'setenv -e -nv -bs -rt -at -i 4000000:$filesize dbx',
  98. 'printenv -e -n -guid d719b2cb-3d3a-4596-a3bc-dad00e67656f dbx'])
  99. assert 'Failed to set EFI variable' not in ''.join(output)
  100. assert 'dbx:' in ''.join(output)
  101. output = u_boot_console.run_command(
  102. 'printenv -e SecureBoot')
  103. assert '00000000: 01' in output
  104. def test_efi_var_auth2(self, u_boot_console, efi_boot_env):
  105. """
  106. Test Case 2 - Update database by overwriting
  107. """
  108. u_boot_console.restart_uboot()
  109. disk_img = efi_boot_env
  110. with u_boot_console.log.section('Test Case 2a'):
  111. # Test Case 2a, update without AUTHENTICATED_WRITE_ACCESS
  112. output = u_boot_console.run_command_list([
  113. 'host bind 0 %s' % disk_img,
  114. 'fatload host 0:1 4000000 PK.auth',
  115. 'setenv -e -nv -bs -rt -at -i 4000000:$filesize PK',
  116. 'fatload host 0:1 4000000 KEK.auth',
  117. 'setenv -e -nv -bs -rt -at -i 4000000:$filesize KEK',
  118. 'fatload host 0:1 4000000 db.auth',
  119. 'setenv -e -nv -bs -rt -at -i 4000000:$filesize db',
  120. 'printenv -e -n -guid d719b2cb-3d3a-4596-a3bc-dad00e67656f db'])
  121. assert 'Failed to set EFI variable' not in ''.join(output)
  122. assert 'db:' in ''.join(output)
  123. output = u_boot_console.run_command_list([
  124. 'fatload host 0:1 4000000 db1.auth',
  125. 'setenv -e -nv -bs -rt -i 4000000:$filesize db'])
  126. assert 'Failed to set EFI variable' in ''.join(output)
  127. with u_boot_console.log.section('Test Case 2b'):
  128. # Test Case 2b, update without correct signature
  129. output = u_boot_console.run_command_list([
  130. 'fatload host 0:1 4000000 db.esl',
  131. 'setenv -e -nv -bs -rt -at -i 4000000:$filesize db'])
  132. assert 'Failed to set EFI variable' in ''.join(output)
  133. with u_boot_console.log.section('Test Case 2c'):
  134. # Test Case 2c, update with correct signature
  135. output = u_boot_console.run_command_list([
  136. 'fatload host 0:1 4000000 db1.auth',
  137. 'setenv -e -nv -bs -rt -at -i 4000000:$filesize db',
  138. 'printenv -e -n -guid d719b2cb-3d3a-4596-a3bc-dad00e67656f db'])
  139. assert 'Failed to set EFI variable' not in ''.join(output)
  140. assert 'db:' in ''.join(output)
  141. def test_efi_var_auth3(self, u_boot_console, efi_boot_env):
  142. """
  143. Test Case 3 - Append database
  144. """
  145. u_boot_console.restart_uboot()
  146. disk_img = efi_boot_env
  147. with u_boot_console.log.section('Test Case 3a'):
  148. # Test Case 3a, update without AUTHENTICATED_WRITE_ACCESS
  149. output = u_boot_console.run_command_list([
  150. 'host bind 0 %s' % disk_img,
  151. 'fatload host 0:1 4000000 PK.auth',
  152. 'setenv -e -nv -bs -rt -at -i 4000000:$filesize PK',
  153. 'fatload host 0:1 4000000 KEK.auth',
  154. 'setenv -e -nv -bs -rt -at -i 4000000:$filesize KEK',
  155. 'fatload host 0:1 4000000 db.auth',
  156. 'setenv -e -nv -bs -rt -at -i 4000000:$filesize db',
  157. 'printenv -e -n -guid d719b2cb-3d3a-4596-a3bc-dad00e67656f db'])
  158. assert 'Failed to set EFI variable' not in ''.join(output)
  159. assert 'db:' in ''.join(output)
  160. output = u_boot_console.run_command_list([
  161. 'fatload host 0:1 4000000 db1.auth',
  162. 'setenv -e -nv -bs -rt -a -i 4000000:$filesize db'])
  163. assert 'Failed to set EFI variable' in ''.join(output)
  164. with u_boot_console.log.section('Test Case 3b'):
  165. # Test Case 3b, update without correct signature
  166. output = u_boot_console.run_command_list([
  167. 'fatload host 0:1 4000000 db.esl',
  168. 'setenv -e -nv -bs -rt -at -a -i 4000000:$filesize db'])
  169. assert 'Failed to set EFI variable' in ''.join(output)
  170. with u_boot_console.log.section('Test Case 3c'):
  171. # Test Case 3c, update with correct signature
  172. output = u_boot_console.run_command_list([
  173. 'fatload host 0:1 4000000 db1.auth',
  174. 'setenv -e -nv -bs -rt -at -a -i 4000000:$filesize db',
  175. 'printenv -e -n -guid d719b2cb-3d3a-4596-a3bc-dad00e67656f db'])
  176. assert 'Failed to set EFI variable' not in ''.join(output)
  177. assert 'db:' in ''.join(output)
  178. def test_efi_var_auth4(self, u_boot_console, efi_boot_env):
  179. """
  180. Test Case 4 - Delete database without authentication
  181. """
  182. u_boot_console.restart_uboot()
  183. disk_img = efi_boot_env
  184. with u_boot_console.log.section('Test Case 4a'):
  185. # Test Case 4a, update without AUTHENTICATED_WRITE_ACCESS
  186. output = u_boot_console.run_command_list([
  187. 'host bind 0 %s' % disk_img,
  188. 'fatload host 0:1 4000000 PK.auth',
  189. 'setenv -e -nv -bs -rt -at -i 4000000:$filesize PK',
  190. 'fatload host 0:1 4000000 KEK.auth',
  191. 'setenv -e -nv -bs -rt -at -i 4000000:$filesize KEK',
  192. 'fatload host 0:1 4000000 db.auth',
  193. 'setenv -e -nv -bs -rt -at -i 4000000:$filesize db',
  194. 'printenv -e -n -guid d719b2cb-3d3a-4596-a3bc-dad00e67656f db'])
  195. assert 'Failed to set EFI variable' not in ''.join(output)
  196. assert 'db:' in ''.join(output)
  197. output = u_boot_console.run_command_list([
  198. 'setenv -e -nv -bs -rt db',
  199. 'printenv -e -n -guid d719b2cb-3d3a-4596-a3bc-dad00e67656f db'])
  200. assert 'Failed to set EFI variable' in ''.join(output)
  201. assert 'db:' in ''.join(output)
  202. with u_boot_console.log.section('Test Case 4b'):
  203. # Test Case 4b, update without correct signature/data
  204. output = u_boot_console.run_command_list([
  205. 'setenv -e -nv -bs -rt -at db',
  206. 'printenv -e -n -guid d719b2cb-3d3a-4596-a3bc-dad00e67656f db'])
  207. assert 'Failed to set EFI variable' in ''.join(output)
  208. assert 'db:' in ''.join(output)
  209. def test_efi_var_auth5(self, u_boot_console, efi_boot_env):
  210. """
  211. Test Case 5 - Uninstall(delete) PK
  212. """
  213. u_boot_console.restart_uboot()
  214. disk_img = efi_boot_env
  215. with u_boot_console.log.section('Test Case 5a'):
  216. # Test Case 5a, Uninstall PK without correct signature
  217. output = u_boot_console.run_command_list([
  218. 'host bind 0 %s' % disk_img,
  219. 'fatload host 0:1 4000000 PK.auth',
  220. 'setenv -e -nv -bs -rt -at -i 4000000:$filesize PK',
  221. 'fatload host 0:1 4000000 KEK.auth',
  222. 'setenv -e -nv -bs -rt -at -i 4000000:$filesize KEK',
  223. 'fatload host 0:1 4000000 db.auth',
  224. 'setenv -e -nv -bs -rt -at -i 4000000:$filesize db',
  225. 'printenv -e -n PK'])
  226. assert 'Failed to set EFI variable' not in ''.join(output)
  227. assert 'PK:' in ''.join(output)
  228. output = u_boot_console.run_command_list([
  229. 'fatload host 0:1 4000000 PK_null.esl',
  230. 'setenv -e -nv -bs -rt -at -i 4000000:$filesize PK',
  231. 'printenv -e -n PK'])
  232. assert 'Failed to set EFI variable' in ''.join(output)
  233. assert 'PK:' in ''.join(output)
  234. with u_boot_console.log.section('Test Case 5b'):
  235. # Test Case 5b, Uninstall PK with correct signature
  236. output = u_boot_console.run_command_list([
  237. 'fatload host 0:1 4000000 PK_null.auth',
  238. 'setenv -e -nv -bs -rt -at -i 4000000:$filesize PK',
  239. 'printenv -e -n PK'])
  240. assert 'Failed to set EFI variable' not in ''.join(output)
  241. assert '\"PK\" not defined' in ''.join(output)
  242. output = u_boot_console.run_command(
  243. 'printenv -e SecureBoot')
  244. assert '00000000: 00' in output
  245. output = u_boot_console.run_command(
  246. 'printenv -e SetupMode')
  247. assert '00000000: 01' in output