test_vboot.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. # Copyright (c) 2016, Google Inc.
  2. #
  3. # SPDX-License-Identifier: GPL-2.0+
  4. #
  5. # U-Boot Verified Boot Test
  6. """
  7. This tests verified boot in the following ways:
  8. For image verification:
  9. - Create FIT (unsigned) with mkimage
  10. - Check that verification shows that no keys are verified
  11. - Sign image
  12. - Check that verification shows that a key is now verified
  13. For configuration verification:
  14. - Corrupt signature and check for failure
  15. - Create FIT (with unsigned configuration) with mkimage
  16. - Check that image verification works
  17. - Sign the FIT and mark the key as 'required' for verification
  18. - Check that image verification works
  19. - Corrupt the signature
  20. - Check that image verification no-longer works
  21. Tests run with both SHA1 and SHA256 hashing.
  22. """
  23. import pytest
  24. import sys
  25. import u_boot_utils as util
  26. @pytest.mark.boardspec('sandbox')
  27. @pytest.mark.buildconfigspec('fit_signature')
  28. def test_vboot(u_boot_console):
  29. """Test verified boot signing with mkimage and verification with 'bootm'.
  30. This works using sandbox only as it needs to update the device tree used
  31. by U-Boot to hold public keys from the signing process.
  32. The SHA1 and SHA256 tests are combined into a single test since the
  33. key-generation process is quite slow and we want to avoid doing it twice.
  34. """
  35. def dtc(dts):
  36. """Run the device tree compiler to compile a .dts file
  37. The output file will be the same as the input file but with a .dtb
  38. extension.
  39. Args:
  40. dts: Device tree file to compile.
  41. """
  42. dtb = dts.replace('.dts', '.dtb')
  43. util.run_and_log(cons, 'dtc %s %s%s -O dtb '
  44. '-o %s%s' % (dtc_args, datadir, dts, tmpdir, dtb))
  45. def run_bootm(sha_algo, test_type, expect_string, boots):
  46. """Run a 'bootm' command U-Boot.
  47. This always starts a fresh U-Boot instance since the device tree may
  48. contain a new public key.
  49. Args:
  50. test_type: A string identifying the test type.
  51. expect_string: A string which is expected in the output.
  52. sha_algo: Either 'sha1' or 'sha256', to select the algorithm to
  53. use.
  54. boots: A boolean that is True if Linux should boot and False if
  55. we are expected to not boot
  56. """
  57. cons.restart_uboot()
  58. with cons.log.section('Verified boot %s %s' % (sha_algo, test_type)):
  59. output = cons.run_command_list(
  60. ['sb load hostfs - 100 %stest.fit' % tmpdir,
  61. 'fdt addr 100',
  62. 'bootm 100'])
  63. assert(expect_string in ''.join(output))
  64. if boots:
  65. assert('sandbox: continuing, as we cannot run' in ''.join(output))
  66. def make_fit(its):
  67. """Make a new FIT from the .its source file.
  68. This runs 'mkimage -f' to create a new FIT.
  69. Args:
  70. its: Filename containing .its source.
  71. """
  72. util.run_and_log(cons, [mkimage, '-D', dtc_args, '-f',
  73. '%s%s' % (datadir, its), fit])
  74. def sign_fit(sha_algo):
  75. """Sign the FIT
  76. Signs the FIT and writes the signature into it. It also writes the
  77. public key into the dtb.
  78. Args:
  79. sha_algo: Either 'sha1' or 'sha256', to select the algorithm to
  80. use.
  81. """
  82. cons.log.action('%s: Sign images' % sha_algo)
  83. util.run_and_log(cons, [mkimage, '-F', '-k', tmpdir, '-K', dtb,
  84. '-r', fit])
  85. def test_with_algo(sha_algo):
  86. """Test verified boot with the given hash algorithm.
  87. This is the main part of the test code. The same procedure is followed
  88. for both hashing algorithms.
  89. Args:
  90. sha_algo: Either 'sha1' or 'sha256', to select the algorithm to
  91. use.
  92. """
  93. # Compile our device tree files for kernel and U-Boot. These are
  94. # regenerated here since mkimage will modify them (by adding a
  95. # public key) below.
  96. dtc('sandbox-kernel.dts')
  97. dtc('sandbox-u-boot.dts')
  98. # Build the FIT, but don't sign anything yet
  99. cons.log.action('%s: Test FIT with signed images' % sha_algo)
  100. make_fit('sign-images-%s.its' % sha_algo)
  101. run_bootm(sha_algo, 'unsigned images', 'dev-', True)
  102. # Sign images with our dev keys
  103. sign_fit(sha_algo)
  104. run_bootm(sha_algo, 'signed images', 'dev+', True)
  105. # Create a fresh .dtb without the public keys
  106. dtc('sandbox-u-boot.dts')
  107. cons.log.action('%s: Test FIT with signed configuration' % sha_algo)
  108. make_fit('sign-configs-%s.its' % sha_algo)
  109. run_bootm(sha_algo, 'unsigned config', '%s+ OK' % sha_algo, True)
  110. # Sign images with our dev keys
  111. sign_fit(sha_algo)
  112. run_bootm(sha_algo, 'signed config', 'dev+', True)
  113. cons.log.action('%s: Check signed config on the host' % sha_algo)
  114. util.run_and_log(cons, [fit_check_sign, '-f', fit, '-k', tmpdir,
  115. '-k', dtb])
  116. # Increment the first byte of the signature, which should cause failure
  117. sig = util.run_and_log(cons, 'fdtget -t bx %s %s value' %
  118. (fit, sig_node))
  119. byte_list = sig.split()
  120. byte = int(byte_list[0], 16)
  121. byte_list[0] = '%x' % (byte + 1)
  122. sig = ' '.join(byte_list)
  123. util.run_and_log(cons, 'fdtput -t bx %s %s value %s' %
  124. (fit, sig_node, sig))
  125. run_bootm(sha_algo, 'Signed config with bad hash', 'Bad Data Hash', False)
  126. cons.log.action('%s: Check bad config on the host' % sha_algo)
  127. util.run_and_log_expect_exception(cons, [fit_check_sign, '-f', fit,
  128. '-k', dtb], 1, 'Failed to verify required signature')
  129. cons = u_boot_console
  130. tmpdir = cons.config.result_dir + '/'
  131. tmp = tmpdir + 'vboot.tmp'
  132. datadir = cons.config.source_dir + '/test/py/tests/vboot/'
  133. fit = '%stest.fit' % tmpdir
  134. mkimage = cons.config.build_dir + '/tools/mkimage'
  135. fit_check_sign = cons.config.build_dir + '/tools/fit_check_sign'
  136. dtc_args = '-I dts -O dtb -i %s' % tmpdir
  137. dtb = '%ssandbox-u-boot.dtb' % tmpdir
  138. sig_node = '/configurations/conf@1/signature@1'
  139. # Create an RSA key pair
  140. public_exponent = 65537
  141. util.run_and_log(cons, 'openssl genpkey -algorithm RSA -out %sdev.key '
  142. '-pkeyopt rsa_keygen_bits:2048 '
  143. '-pkeyopt rsa_keygen_pubexp:%d '
  144. '2>/dev/null' % (tmpdir, public_exponent))
  145. # Create a certificate containing the public key
  146. util.run_and_log(cons, 'openssl req -batch -new -x509 -key %sdev.key -out '
  147. '%sdev.crt' % (tmpdir, tmpdir))
  148. # Create a number kernel image with zeroes
  149. with open('%stest-kernel.bin' % tmpdir, 'w') as fd:
  150. fd.write(5000 * chr(0))
  151. try:
  152. # We need to use our own device tree file. Remember to restore it
  153. # afterwards.
  154. old_dtb = cons.config.dtb
  155. cons.config.dtb = dtb
  156. test_with_algo('sha1')
  157. test_with_algo('sha256')
  158. finally:
  159. # Go back to the original U-Boot with the correct dtb.
  160. cons.config.dtb = old_dtb
  161. cons.restart_uboot()