conftest.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. # SPDX-License-Identifier: GPL-2.0+
  2. # Copyright (c) 2019, Linaro Limited
  3. # Author: AKASHI Takahiro <takahiro.akashi@linaro.org>
  4. import os
  5. import os.path
  6. from subprocess import call, check_call, check_output, CalledProcessError
  7. import pytest
  8. from defs import *
  9. # from test/py/conftest.py
  10. def tool_is_in_path(tool):
  11. for path in os.environ["PATH"].split(os.pathsep):
  12. full_path = os.path.join(path, tool)
  13. if os.path.isfile(full_path) and os.access(full_path, os.X_OK):
  14. return True
  15. return False
  16. #
  17. # Fixture for UEFI secure boot test
  18. #
  19. @pytest.fixture(scope='session')
  20. def efi_boot_env(request, u_boot_config):
  21. """Set up a file system to be used in UEFI secure boot test.
  22. Args:
  23. request: Pytest request object.
  24. u_boot_config: U-boot configuration.
  25. Return:
  26. A path to disk image to be used for testing
  27. """
  28. global HELLO_PATH
  29. image_path = u_boot_config.persistent_data_dir
  30. image_path = image_path + '/' + EFI_SECBOOT_IMAGE_NAME
  31. if HELLO_PATH == '':
  32. HELLO_PATH = u_boot_config.build_dir + '/lib/efi_loader/helloworld.efi'
  33. try:
  34. mnt_point = u_boot_config.build_dir + '/mnt_efisecure'
  35. check_call('rm -rf {}'.format(mnt_point), shell=True)
  36. check_call('mkdir -p {}'.format(mnt_point), shell=True)
  37. # suffix
  38. # *.key: RSA private key in PEM
  39. # *.crt: X509 certificate (self-signed) in PEM
  40. # *.esl: signature list
  41. # *.hash: message digest of image as signature list
  42. # *.auth: signed signature list in signature database format
  43. # *.efi: UEFI image
  44. # *.efi.signed: signed UEFI image
  45. # Create signature database
  46. # PK
  47. check_call('cd %s; openssl req -x509 -sha256 -newkey rsa:2048 -subj /CN=TEST_PK/ -keyout PK.key -out PK.crt -nodes -days 365'
  48. % mnt_point, shell=True)
  49. check_call('cd %s; %scert-to-efi-sig-list -g %s PK.crt PK.esl; %ssign-efi-sig-list -t "2020-04-01" -c PK.crt -k PK.key PK PK.esl PK.auth'
  50. % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH),
  51. shell=True)
  52. # PK_null for deletion
  53. check_call('cd %s; touch PK_null.esl; %ssign-efi-sig-list -t "2020-04-02" -c PK.crt -k PK.key PK PK_null.esl PK_null.auth'
  54. % (mnt_point, EFITOOLS_PATH), shell=True)
  55. # KEK
  56. check_call('cd %s; openssl req -x509 -sha256 -newkey rsa:2048 -subj /CN=TEST_KEK/ -keyout KEK.key -out KEK.crt -nodes -days 365'
  57. % mnt_point, shell=True)
  58. check_call('cd %s; %scert-to-efi-sig-list -g %s KEK.crt KEK.esl; %ssign-efi-sig-list -t "2020-04-03" -c PK.crt -k PK.key KEK KEK.esl KEK.auth'
  59. % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH),
  60. shell=True)
  61. # db
  62. check_call('cd %s; openssl req -x509 -sha256 -newkey rsa:2048 -subj /CN=TEST_db/ -keyout db.key -out db.crt -nodes -days 365'
  63. % mnt_point, shell=True)
  64. check_call('cd %s; %scert-to-efi-sig-list -g %s db.crt db.esl; %ssign-efi-sig-list -t "2020-04-04" -c KEK.crt -k KEK.key db db.esl db.auth'
  65. % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH),
  66. shell=True)
  67. # db1
  68. check_call('cd %s; openssl req -x509 -sha256 -newkey rsa:2048 -subj /CN=TEST_db1/ -keyout db1.key -out db1.crt -nodes -days 365'
  69. % mnt_point, shell=True)
  70. check_call('cd %s; %scert-to-efi-sig-list -g %s db1.crt db1.esl; %ssign-efi-sig-list -t "2020-04-05" -c KEK.crt -k KEK.key db db1.esl db1.auth'
  71. % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH),
  72. shell=True)
  73. # db1-update
  74. check_call('cd %s; %ssign-efi-sig-list -t "2020-04-06" -a -c KEK.crt -k KEK.key db db1.esl db1-update.auth'
  75. % (mnt_point, EFITOOLS_PATH), shell=True)
  76. ## dbx (TEST_dbx certificate)
  77. check_call('cd %s; openssl req -x509 -sha256 -newkey rsa:2048 -subj /CN=TEST_dbx/ -keyout dbx.key -out dbx.crt -nodes -days 365'
  78. % mnt_point, shell=True)
  79. check_call('cd %s; %scert-to-efi-sig-list -g %s dbx.crt dbx.esl; %ssign-efi-sig-list -t "2020-04-05" -c KEK.crt -k KEK.key dbx dbx.esl dbx.auth'
  80. % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH),
  81. shell=True)
  82. ## dbx_hash (digest of TEST_db certificate)
  83. check_call('cd %s; %scert-to-efi-hash-list -g %s -t 0 -s 256 db.crt dbx_hash.crl; %ssign-efi-sig-list -t "2020-04-05" -c KEK.crt -k KEK.key dbx dbx_hash.crl dbx_hash.auth'
  84. % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH),
  85. shell=True)
  86. ## dbx_hash1 (digest of TEST_db1 certificate)
  87. check_call('cd %s; %scert-to-efi-hash-list -g %s -t 0 -s 256 db1.crt dbx_hash1.crl; %ssign-efi-sig-list -t "2020-04-05" -c KEK.crt -k KEK.key dbx dbx_hash1.crl dbx_hash1.auth'
  88. % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH),
  89. shell=True)
  90. ## dbx_db (with TEST_db certificate)
  91. check_call('cd %s; %ssign-efi-sig-list -t "2020-04-05" -c KEK.crt -k KEK.key dbx db.esl dbx_db.auth'
  92. % (mnt_point, EFITOOLS_PATH),
  93. shell=True)
  94. # Copy image
  95. check_call('cp %s %s' % (HELLO_PATH, mnt_point), shell=True)
  96. # Sign image
  97. check_call('cd %s; sbsign --key db.key --cert db.crt helloworld.efi'
  98. % mnt_point, shell=True)
  99. ## Sign already-signed image with another key
  100. check_call('cd %s; sbsign --key db1.key --cert db1.crt --output helloworld.efi.signed_2sigs helloworld.efi.signed'
  101. % mnt_point, shell=True)
  102. ## Digest image
  103. check_call('cd %s; %shash-to-efi-sig-list helloworld.efi db_hello.hash; %ssign-efi-sig-list -t "2020-04-07" -c KEK.crt -k KEK.key db db_hello.hash db_hello.auth'
  104. % (mnt_point, EFITOOLS_PATH, EFITOOLS_PATH),
  105. shell=True)
  106. check_call('cd %s; %shash-to-efi-sig-list helloworld.efi.signed db_hello_signed.hash; %ssign-efi-sig-list -t "2020-04-03" -c KEK.crt -k KEK.key db db_hello_signed.hash db_hello_signed.auth'
  107. % (mnt_point, EFITOOLS_PATH, EFITOOLS_PATH),
  108. shell=True)
  109. check_call('cd %s; %ssign-efi-sig-list -t "2020-04-07" -c KEK.crt -k KEK.key dbx db_hello_signed.hash dbx_hello_signed.auth'
  110. % (mnt_point, EFITOOLS_PATH),
  111. shell=True)
  112. check_call('virt-make-fs --partition=gpt --size=+1M --type=vfat {} {}'.format(mnt_point, image_path), shell=True)
  113. check_call('rm -rf {}'.format(mnt_point), shell=True)
  114. except CalledProcessError as exception:
  115. pytest.skip('Setup failed: %s' % exception.cmd)
  116. return
  117. else:
  118. yield image_path
  119. finally:
  120. call('rm -f %s' % image_path, shell=True)