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 | |
AKASHI Takahiro | 58c95c2 | 2020-04-14 11:51:49 +0900 | [diff] [blame] | 11 | |
| 12 | # |
| 13 | # Fixture for UEFI secure boot test |
| 14 | # |
AKASHI Takahiro | 719e89d | 2020-07-08 14:01:58 +0900 | [diff] [blame] | 15 | |
| 16 | |
AKASHI Takahiro | 58c95c2 | 2020-04-14 11:51:49 +0900 | [diff] [blame] | 17 | @pytest.fixture(scope='session') |
| 18 | def efi_boot_env(request, u_boot_config): |
| 19 | """Set up a file system to be used in UEFI secure boot test. |
| 20 | |
| 21 | Args: |
| 22 | request: Pytest request object. |
AKASHI Takahiro | 719e89d | 2020-07-08 14:01:58 +0900 | [diff] [blame] | 23 | u_boot_config: U-boot configuration. |
AKASHI Takahiro | 58c95c2 | 2020-04-14 11:51:49 +0900 | [diff] [blame] | 24 | |
| 25 | Return: |
| 26 | A path to disk image to be used for testing |
| 27 | """ |
| 28 | global HELLO_PATH |
| 29 | |
| 30 | image_path = u_boot_config.persistent_data_dir |
| 31 | image_path = image_path + '/' + EFI_SECBOOT_IMAGE_NAME |
AKASHI Takahiro | 58c95c2 | 2020-04-14 11:51:49 +0900 | [diff] [blame] | 32 | |
| 33 | if HELLO_PATH == '': |
| 34 | HELLO_PATH = u_boot_config.build_dir + '/lib/efi_loader/helloworld.efi' |
| 35 | |
| 36 | try: |
Heinrich Schuchardt | 33424ea | 2020-07-11 23:05:18 +0200 | [diff] [blame] | 37 | mnt_point = u_boot_config.build_dir + '/mnt_efisecure' |
| 38 | check_call('rm -rf {}'.format(mnt_point), shell=True) |
Heinrich Schuchardt | b7e1d65 | 2020-04-21 18:44:39 +0200 | [diff] [blame] | 39 | check_call('mkdir -p {}'.format(mnt_point), shell=True) |
AKASHI Takahiro | 58c95c2 | 2020-04-14 11:51:49 +0900 | [diff] [blame] | 40 | |
AKASHI Takahiro | 58c95c2 | 2020-04-14 11:51:49 +0900 | [diff] [blame] | 41 | # suffix |
| 42 | # *.key: RSA private key in PEM |
| 43 | # *.crt: X509 certificate (self-signed) in PEM |
| 44 | # *.esl: signature list |
| 45 | # *.hash: message digest of image as signature list |
| 46 | # *.auth: signed signature list in signature database format |
| 47 | # *.efi: UEFI image |
| 48 | # *.efi.signed: signed UEFI image |
| 49 | |
| 50 | # Create signature database |
AKASHI Takahiro | 719e89d | 2020-07-08 14:01:58 +0900 | [diff] [blame] | 51 | # PK |
AKASHI Takahiro | 58c95c2 | 2020-04-14 11:51:49 +0900 | [diff] [blame] | 52 | 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] | 53 | % mnt_point, shell=True) |
Heinrich Schuchardt | 446b148 | 2020-07-02 08:25:46 +0200 | [diff] [blame] | 54 | 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] | 55 | % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH), |
| 56 | shell=True) |
| 57 | # PK_null for deletion |
Heinrich Schuchardt | 446b148 | 2020-07-02 08:25:46 +0200 | [diff] [blame] | 58 | 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] | 59 | % (mnt_point, EFITOOLS_PATH), shell=True) |
| 60 | # KEK |
AKASHI Takahiro | 58c95c2 | 2020-04-14 11:51:49 +0900 | [diff] [blame] | 61 | 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] | 62 | % mnt_point, shell=True) |
Heinrich Schuchardt | 446b148 | 2020-07-02 08:25:46 +0200 | [diff] [blame] | 63 | 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] | 64 | % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH), |
| 65 | shell=True) |
| 66 | # db |
AKASHI Takahiro | 58c95c2 | 2020-04-14 11:51:49 +0900 | [diff] [blame] | 67 | 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] | 68 | % mnt_point, shell=True) |
Heinrich Schuchardt | 446b148 | 2020-07-02 08:25:46 +0200 | [diff] [blame] | 69 | 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] | 70 | % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH), |
| 71 | shell=True) |
| 72 | # db1 |
AKASHI Takahiro | 58c95c2 | 2020-04-14 11:51:49 +0900 | [diff] [blame] | 73 | 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] | 74 | % mnt_point, shell=True) |
Heinrich Schuchardt | 446b148 | 2020-07-02 08:25:46 +0200 | [diff] [blame] | 75 | 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] | 76 | % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH), |
| 77 | shell=True) |
| 78 | # db1-update |
Heinrich Schuchardt | 446b148 | 2020-07-02 08:25:46 +0200 | [diff] [blame] | 79 | 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] | 80 | % (mnt_point, EFITOOLS_PATH), shell=True) |
AKASHI Takahiro | 798f306 | 2020-07-20 15:33:39 +0900 | [diff] [blame^] | 81 | # dbx (TEST_dbx certificate) |
AKASHI Takahiro | 58c95c2 | 2020-04-14 11:51:49 +0900 | [diff] [blame] | 82 | 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] | 83 | % mnt_point, shell=True) |
Heinrich Schuchardt | 446b148 | 2020-07-02 08:25:46 +0200 | [diff] [blame] | 84 | 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] | 85 | % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH), |
| 86 | shell=True) |
AKASHI Takahiro | 798f306 | 2020-07-20 15:33:39 +0900 | [diff] [blame^] | 87 | # dbx_hash (digest of TEST_db certificate) |
AKASHI Takahiro | d4ece53 | 2020-07-08 14:02:01 +0900 | [diff] [blame] | 88 | 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' |
| 89 | % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH), |
| 90 | shell=True) |
AKASHI Takahiro | 798f306 | 2020-07-20 15:33:39 +0900 | [diff] [blame^] | 91 | # dbx_hash1 (digest of TEST_db1 certificate) |
AKASHI Takahiro | db20a3e | 2020-07-08 14:02:02 +0900 | [diff] [blame] | 92 | 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' |
| 93 | % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH), |
| 94 | shell=True) |
AKASHI Takahiro | 798f306 | 2020-07-20 15:33:39 +0900 | [diff] [blame^] | 95 | # dbx_db (with TEST_db certificate) |
AKASHI Takahiro | 0f6e3eb | 2020-07-08 14:02:03 +0900 | [diff] [blame] | 96 | check_call('cd %s; %ssign-efi-sig-list -t "2020-04-05" -c KEK.crt -k KEK.key dbx db.esl dbx_db.auth' |
| 97 | % (mnt_point, EFITOOLS_PATH), |
| 98 | shell=True) |
AKASHI Takahiro | 58c95c2 | 2020-04-14 11:51:49 +0900 | [diff] [blame] | 99 | |
| 100 | # Copy image |
| 101 | check_call('cp %s %s' % (HELLO_PATH, mnt_point), shell=True) |
| 102 | |
AKASHI Takahiro | 719e89d | 2020-07-08 14:01:58 +0900 | [diff] [blame] | 103 | # Sign image |
AKASHI Takahiro | 58c95c2 | 2020-04-14 11:51:49 +0900 | [diff] [blame] | 104 | 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] | 105 | % mnt_point, shell=True) |
AKASHI Takahiro | 798f306 | 2020-07-20 15:33:39 +0900 | [diff] [blame^] | 106 | # Sign already-signed image with another key |
AKASHI Takahiro | db20a3e | 2020-07-08 14:02:02 +0900 | [diff] [blame] | 107 | check_call('cd %s; sbsign --key db1.key --cert db1.crt --output helloworld.efi.signed_2sigs helloworld.efi.signed' |
| 108 | % mnt_point, shell=True) |
AKASHI Takahiro | 798f306 | 2020-07-20 15:33:39 +0900 | [diff] [blame^] | 109 | # Digest image |
Heinrich Schuchardt | 446b148 | 2020-07-02 08:25:46 +0200 | [diff] [blame] | 110 | 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] | 111 | % (mnt_point, EFITOOLS_PATH, EFITOOLS_PATH), |
| 112 | shell=True) |
AKASHI Takahiro | 0f6e3eb | 2020-07-08 14:02:03 +0900 | [diff] [blame] | 113 | 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' |
| 114 | % (mnt_point, EFITOOLS_PATH, EFITOOLS_PATH), |
| 115 | shell=True) |
| 116 | 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' |
| 117 | % (mnt_point, EFITOOLS_PATH), |
| 118 | shell=True) |
AKASHI Takahiro | 58c95c2 | 2020-04-14 11:51:49 +0900 | [diff] [blame] | 119 | |
AKASHI Takahiro | 798f306 | 2020-07-20 15:33:39 +0900 | [diff] [blame^] | 120 | check_call('virt-make-fs --partition=gpt --size=+1M --type=vfat {} {}'.format( |
| 121 | mnt_point, image_path), shell=True) |
Heinrich Schuchardt | 33424ea | 2020-07-11 23:05:18 +0200 | [diff] [blame] | 122 | check_call('rm -rf {}'.format(mnt_point), shell=True) |
AKASHI Takahiro | 58c95c2 | 2020-04-14 11:51:49 +0900 | [diff] [blame] | 123 | |
AKASHI Takahiro | be5dbf0 | 2020-07-08 14:01:59 +0900 | [diff] [blame] | 124 | except CalledProcessError as exception: |
| 125 | pytest.skip('Setup failed: %s' % exception.cmd) |
AKASHI Takahiro | 58c95c2 | 2020-04-14 11:51:49 +0900 | [diff] [blame] | 126 | return |
| 127 | else: |
| 128 | yield image_path |
| 129 | finally: |
| 130 | call('rm -f %s' % image_path, shell=True) |