AKASHI Takahiro | 58c95c2 | 2020-04-14 11:51:49 +0900 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0+ |
| 2 | # Copyright (c) 2019, Linaro Limited |
| 3 | # Author: AKASHI Takahiro <takahiro.akashi@linaro.org> |
| 4 | |
| 5 | import os |
| 6 | import os.path |
AKASHI Takahiro | 58c95c2 | 2020-04-14 11:51:49 +0900 | [diff] [blame] | 7 | from subprocess import call, check_call, check_output, CalledProcessError |
AKASHI Takahiro | be5dbf0 | 2020-07-08 14:01:59 +0900 | [diff] [blame] | 8 | import pytest |
AKASHI Takahiro | 58c95c2 | 2020-04-14 11:51:49 +0900 | [diff] [blame] | 9 | from defs import * |
| 10 | |
| 11 | # from test/py/conftest.py |
AKASHI Takahiro | 719e89d | 2020-07-08 14:01:58 +0900 | [diff] [blame] | 12 | |
| 13 | |
AKASHI Takahiro | 58c95c2 | 2020-04-14 11:51:49 +0900 | [diff] [blame] | 14 | def tool_is_in_path(tool): |
| 15 | for path in os.environ["PATH"].split(os.pathsep): |
AKASHI Takahiro | be5dbf0 | 2020-07-08 14:01:59 +0900 | [diff] [blame] | 16 | full_path = os.path.join(path, tool) |
| 17 | if os.path.isfile(full_path) and os.access(full_path, os.X_OK): |
AKASHI Takahiro | 58c95c2 | 2020-04-14 11:51:49 +0900 | [diff] [blame] | 18 | return True |
| 19 | return False |
| 20 | |
| 21 | # |
| 22 | # Fixture for UEFI secure boot test |
| 23 | # |
AKASHI Takahiro | 719e89d | 2020-07-08 14:01:58 +0900 | [diff] [blame] | 24 | |
| 25 | |
AKASHI Takahiro | 58c95c2 | 2020-04-14 11:51:49 +0900 | [diff] [blame] | 26 | @pytest.fixture(scope='session') |
| 27 | def efi_boot_env(request, u_boot_config): |
| 28 | """Set up a file system to be used in UEFI secure boot test. |
| 29 | |
| 30 | Args: |
| 31 | request: Pytest request object. |
AKASHI Takahiro | 719e89d | 2020-07-08 14:01:58 +0900 | [diff] [blame] | 32 | u_boot_config: U-boot configuration. |
AKASHI Takahiro | 58c95c2 | 2020-04-14 11:51:49 +0900 | [diff] [blame] | 33 | |
| 34 | Return: |
| 35 | A path to disk image to be used for testing |
| 36 | """ |
| 37 | global HELLO_PATH |
| 38 | |
| 39 | image_path = u_boot_config.persistent_data_dir |
| 40 | image_path = image_path + '/' + EFI_SECBOOT_IMAGE_NAME |
| 41 | image_size = EFI_SECBOOT_IMAGE_SIZE |
| 42 | part_size = EFI_SECBOOT_PART_SIZE |
| 43 | fs_type = EFI_SECBOOT_FS_TYPE |
| 44 | |
| 45 | if HELLO_PATH == '': |
| 46 | HELLO_PATH = u_boot_config.build_dir + '/lib/efi_loader/helloworld.efi' |
| 47 | |
| 48 | try: |
Heinrich Schuchardt | b7e1d65 | 2020-04-21 18:44:39 +0200 | [diff] [blame] | 49 | mnt_point = u_boot_config.persistent_data_dir + '/mnt_efisecure' |
| 50 | check_call('mkdir -p {}'.format(mnt_point), shell=True) |
AKASHI Takahiro | 58c95c2 | 2020-04-14 11:51:49 +0900 | [diff] [blame] | 51 | |
| 52 | # create a disk/partition |
| 53 | check_call('dd if=/dev/zero of=%s bs=1MiB count=%d' |
AKASHI Takahiro | 719e89d | 2020-07-08 14:01:58 +0900 | [diff] [blame] | 54 | % (image_path, image_size), shell=True) |
AKASHI Takahiro | 58c95c2 | 2020-04-14 11:51:49 +0900 | [diff] [blame] | 55 | check_call('sgdisk %s -n 1:0:+%dMiB' |
AKASHI Takahiro | 719e89d | 2020-07-08 14:01:58 +0900 | [diff] [blame] | 56 | % (image_path, part_size), shell=True) |
AKASHI Takahiro | 58c95c2 | 2020-04-14 11:51:49 +0900 | [diff] [blame] | 57 | # create a file system |
| 58 | check_call('dd if=/dev/zero of=%s.tmp bs=1MiB count=%d' |
AKASHI Takahiro | 719e89d | 2020-07-08 14:01:58 +0900 | [diff] [blame] | 59 | % (image_path, part_size), shell=True) |
AKASHI Takahiro | 58c95c2 | 2020-04-14 11:51:49 +0900 | [diff] [blame] | 60 | check_call('mkfs -t %s %s.tmp' % (fs_type, image_path), shell=True) |
| 61 | check_call('dd if=%s.tmp of=%s bs=1MiB seek=1 count=%d conv=notrunc' |
AKASHI Takahiro | 719e89d | 2020-07-08 14:01:58 +0900 | [diff] [blame] | 62 | % (image_path, image_path, 1), shell=True) |
AKASHI Takahiro | 58c95c2 | 2020-04-14 11:51:49 +0900 | [diff] [blame] | 63 | check_call('rm %s.tmp' % image_path, shell=True) |
Heinrich Schuchardt | b7e1d65 | 2020-04-21 18:44:39 +0200 | [diff] [blame] | 64 | loop_dev = check_output('sudo losetup -o 1MiB --sizelimit %dMiB --show -f %s | tr -d "\n"' |
AKASHI Takahiro | 58c95c2 | 2020-04-14 11:51:49 +0900 | [diff] [blame] | 65 | % (part_size, image_path), shell=True).decode() |
Heinrich Schuchardt | b7e1d65 | 2020-04-21 18:44:39 +0200 | [diff] [blame] | 66 | check_output('sudo mount -t %s -o umask=000 %s %s' |
AKASHI Takahiro | 719e89d | 2020-07-08 14:01:58 +0900 | [diff] [blame] | 67 | % (fs_type, loop_dev, mnt_point), shell=True) |
AKASHI Takahiro | 58c95c2 | 2020-04-14 11:51:49 +0900 | [diff] [blame] | 68 | |
AKASHI Takahiro | 58c95c2 | 2020-04-14 11:51:49 +0900 | [diff] [blame] | 69 | # suffix |
| 70 | # *.key: RSA private key in PEM |
| 71 | # *.crt: X509 certificate (self-signed) in PEM |
| 72 | # *.esl: signature list |
| 73 | # *.hash: message digest of image as signature list |
| 74 | # *.auth: signed signature list in signature database format |
| 75 | # *.efi: UEFI image |
| 76 | # *.efi.signed: signed UEFI image |
| 77 | |
| 78 | # Create signature database |
AKASHI Takahiro | 719e89d | 2020-07-08 14:01:58 +0900 | [diff] [blame] | 79 | # PK |
AKASHI Takahiro | 58c95c2 | 2020-04-14 11:51:49 +0900 | [diff] [blame] | 80 | check_call('cd %s; openssl req -x509 -sha256 -newkey rsa:2048 -subj /CN=TEST_PK/ -keyout PK.key -out PK.crt -nodes -days 365' |
AKASHI Takahiro | 719e89d | 2020-07-08 14:01:58 +0900 | [diff] [blame] | 81 | % mnt_point, shell=True) |
Heinrich Schuchardt | 446b148 | 2020-07-02 08:25:46 +0200 | [diff] [blame] | 82 | 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' |
AKASHI Takahiro | 719e89d | 2020-07-08 14:01:58 +0900 | [diff] [blame] | 83 | % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH), |
| 84 | shell=True) |
| 85 | # PK_null for deletion |
Heinrich Schuchardt | 446b148 | 2020-07-02 08:25:46 +0200 | [diff] [blame] | 86 | 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' |
AKASHI Takahiro | 719e89d | 2020-07-08 14:01:58 +0900 | [diff] [blame] | 87 | % (mnt_point, EFITOOLS_PATH), shell=True) |
| 88 | # KEK |
AKASHI Takahiro | 58c95c2 | 2020-04-14 11:51:49 +0900 | [diff] [blame] | 89 | check_call('cd %s; openssl req -x509 -sha256 -newkey rsa:2048 -subj /CN=TEST_KEK/ -keyout KEK.key -out KEK.crt -nodes -days 365' |
AKASHI Takahiro | 719e89d | 2020-07-08 14:01:58 +0900 | [diff] [blame] | 90 | % mnt_point, shell=True) |
Heinrich Schuchardt | 446b148 | 2020-07-02 08:25:46 +0200 | [diff] [blame] | 91 | 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' |
AKASHI Takahiro | 719e89d | 2020-07-08 14:01:58 +0900 | [diff] [blame] | 92 | % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH), |
| 93 | shell=True) |
| 94 | # db |
AKASHI Takahiro | 58c95c2 | 2020-04-14 11:51:49 +0900 | [diff] [blame] | 95 | check_call('cd %s; openssl req -x509 -sha256 -newkey rsa:2048 -subj /CN=TEST_db/ -keyout db.key -out db.crt -nodes -days 365' |
AKASHI Takahiro | 719e89d | 2020-07-08 14:01:58 +0900 | [diff] [blame] | 96 | % mnt_point, shell=True) |
Heinrich Schuchardt | 446b148 | 2020-07-02 08:25:46 +0200 | [diff] [blame] | 97 | 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' |
AKASHI Takahiro | 719e89d | 2020-07-08 14:01:58 +0900 | [diff] [blame] | 98 | % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH), |
| 99 | shell=True) |
| 100 | # db1 |
AKASHI Takahiro | 58c95c2 | 2020-04-14 11:51:49 +0900 | [diff] [blame] | 101 | check_call('cd %s; openssl req -x509 -sha256 -newkey rsa:2048 -subj /CN=TEST_db1/ -keyout db1.key -out db1.crt -nodes -days 365' |
AKASHI Takahiro | 719e89d | 2020-07-08 14:01:58 +0900 | [diff] [blame] | 102 | % mnt_point, shell=True) |
Heinrich Schuchardt | 446b148 | 2020-07-02 08:25:46 +0200 | [diff] [blame] | 103 | 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' |
AKASHI Takahiro | 719e89d | 2020-07-08 14:01:58 +0900 | [diff] [blame] | 104 | % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH), |
| 105 | shell=True) |
| 106 | # db1-update |
Heinrich Schuchardt | 446b148 | 2020-07-02 08:25:46 +0200 | [diff] [blame] | 107 | 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' |
AKASHI Takahiro | 719e89d | 2020-07-08 14:01:58 +0900 | [diff] [blame] | 108 | % (mnt_point, EFITOOLS_PATH), shell=True) |
AKASHI Takahiro | d4ece53 | 2020-07-08 14:02:01 +0900 | [diff] [blame] | 109 | ## dbx (TEST_dbx certificate) |
AKASHI Takahiro | 58c95c2 | 2020-04-14 11:51:49 +0900 | [diff] [blame] | 110 | check_call('cd %s; openssl req -x509 -sha256 -newkey rsa:2048 -subj /CN=TEST_dbx/ -keyout dbx.key -out dbx.crt -nodes -days 365' |
AKASHI Takahiro | 719e89d | 2020-07-08 14:01:58 +0900 | [diff] [blame] | 111 | % mnt_point, shell=True) |
Heinrich Schuchardt | 446b148 | 2020-07-02 08:25:46 +0200 | [diff] [blame] | 112 | 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' |
AKASHI Takahiro | 719e89d | 2020-07-08 14:01:58 +0900 | [diff] [blame] | 113 | % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH), |
| 114 | shell=True) |
AKASHI Takahiro | d4ece53 | 2020-07-08 14:02:01 +0900 | [diff] [blame] | 115 | ## dbx_hash (digest of TEST_db certificate) |
| 116 | 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' |
| 117 | % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH), |
| 118 | shell=True) |
AKASHI Takahiro | db20a3e | 2020-07-08 14:02:02 +0900 | [diff] [blame] | 119 | ## dbx_hash1 (digest of TEST_db1 certificate) |
| 120 | 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' |
| 121 | % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH), |
| 122 | shell=True) |
AKASHI Takahiro | 0f6e3eb | 2020-07-08 14:02:03 +0900 | [diff] [blame^] | 123 | ## dbx_db (with TEST_db certificate) |
| 124 | check_call('cd %s; %ssign-efi-sig-list -t "2020-04-05" -c KEK.crt -k KEK.key dbx db.esl dbx_db.auth' |
| 125 | % (mnt_point, EFITOOLS_PATH), |
| 126 | shell=True) |
AKASHI Takahiro | 58c95c2 | 2020-04-14 11:51:49 +0900 | [diff] [blame] | 127 | |
| 128 | # Copy image |
| 129 | check_call('cp %s %s' % (HELLO_PATH, mnt_point), shell=True) |
| 130 | |
AKASHI Takahiro | 719e89d | 2020-07-08 14:01:58 +0900 | [diff] [blame] | 131 | # Sign image |
AKASHI Takahiro | 58c95c2 | 2020-04-14 11:51:49 +0900 | [diff] [blame] | 132 | check_call('cd %s; sbsign --key db.key --cert db.crt helloworld.efi' |
AKASHI Takahiro | 719e89d | 2020-07-08 14:01:58 +0900 | [diff] [blame] | 133 | % mnt_point, shell=True) |
AKASHI Takahiro | db20a3e | 2020-07-08 14:02:02 +0900 | [diff] [blame] | 134 | ## Sign already-signed image with another key |
| 135 | check_call('cd %s; sbsign --key db1.key --cert db1.crt --output helloworld.efi.signed_2sigs helloworld.efi.signed' |
| 136 | % mnt_point, shell=True) |
| 137 | ## Digest image |
Heinrich Schuchardt | 446b148 | 2020-07-02 08:25:46 +0200 | [diff] [blame] | 138 | 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' |
AKASHI Takahiro | 719e89d | 2020-07-08 14:01:58 +0900 | [diff] [blame] | 139 | % (mnt_point, EFITOOLS_PATH, EFITOOLS_PATH), |
| 140 | shell=True) |
AKASHI Takahiro | 0f6e3eb | 2020-07-08 14:02:03 +0900 | [diff] [blame^] | 141 | 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' |
| 142 | % (mnt_point, EFITOOLS_PATH, EFITOOLS_PATH), |
| 143 | shell=True) |
| 144 | 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' |
| 145 | % (mnt_point, EFITOOLS_PATH), |
| 146 | shell=True) |
AKASHI Takahiro | 58c95c2 | 2020-04-14 11:51:49 +0900 | [diff] [blame] | 147 | |
Heinrich Schuchardt | b7e1d65 | 2020-04-21 18:44:39 +0200 | [diff] [blame] | 148 | check_call('sudo umount %s' % loop_dev, shell=True) |
| 149 | check_call('sudo losetup -d %s' % loop_dev, shell=True) |
AKASHI Takahiro | 58c95c2 | 2020-04-14 11:51:49 +0900 | [diff] [blame] | 150 | |
AKASHI Takahiro | be5dbf0 | 2020-07-08 14:01:59 +0900 | [diff] [blame] | 151 | except CalledProcessError as exception: |
| 152 | pytest.skip('Setup failed: %s' % exception.cmd) |
AKASHI Takahiro | 58c95c2 | 2020-04-14 11:51:49 +0900 | [diff] [blame] | 153 | return |
| 154 | else: |
| 155 | yield image_path |
| 156 | finally: |
| 157 | call('rm -f %s' % image_path, shell=True) |