blob: 71ef723e59e6d016255ebd3223012b9f84297ddd [file] [log] [blame]
AKASHI Takahiro58c95c22020-04-14 11:51:49 +09001# SPDX-License-Identifier: GPL-2.0+
2# Copyright (c) 2019, Linaro Limited
3# Author: AKASHI Takahiro <takahiro.akashi@linaro.org>
4
5import os
6import os.path
AKASHI Takahiro58c95c22020-04-14 11:51:49 +09007from subprocess import call, check_call, check_output, CalledProcessError
AKASHI Takahirobe5dbf02020-07-08 14:01:59 +09008import pytest
AKASHI Takahiro58c95c22020-04-14 11:51:49 +09009from defs import *
10
11# from test/py/conftest.py
AKASHI Takahiro719e89d2020-07-08 14:01:58 +090012
13
AKASHI Takahiro58c95c22020-04-14 11:51:49 +090014def tool_is_in_path(tool):
15 for path in os.environ["PATH"].split(os.pathsep):
AKASHI Takahirobe5dbf02020-07-08 14:01:59 +090016 full_path = os.path.join(path, tool)
17 if os.path.isfile(full_path) and os.access(full_path, os.X_OK):
AKASHI Takahiro58c95c22020-04-14 11:51:49 +090018 return True
19 return False
20
21#
22# Fixture for UEFI secure boot test
23#
AKASHI Takahiro719e89d2020-07-08 14:01:58 +090024
25
AKASHI Takahiro58c95c22020-04-14 11:51:49 +090026@pytest.fixture(scope='session')
27def 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 Takahiro719e89d2020-07-08 14:01:58 +090032 u_boot_config: U-boot configuration.
AKASHI Takahiro58c95c22020-04-14 11:51:49 +090033
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 Schuchardtb7e1d652020-04-21 18:44:39 +020049 mnt_point = u_boot_config.persistent_data_dir + '/mnt_efisecure'
50 check_call('mkdir -p {}'.format(mnt_point), shell=True)
AKASHI Takahiro58c95c22020-04-14 11:51:49 +090051
52 # create a disk/partition
53 check_call('dd if=/dev/zero of=%s bs=1MiB count=%d'
AKASHI Takahiro719e89d2020-07-08 14:01:58 +090054 % (image_path, image_size), shell=True)
AKASHI Takahiro58c95c22020-04-14 11:51:49 +090055 check_call('sgdisk %s -n 1:0:+%dMiB'
AKASHI Takahiro719e89d2020-07-08 14:01:58 +090056 % (image_path, part_size), shell=True)
AKASHI Takahiro58c95c22020-04-14 11:51:49 +090057 # create a file system
58 check_call('dd if=/dev/zero of=%s.tmp bs=1MiB count=%d'
AKASHI Takahiro719e89d2020-07-08 14:01:58 +090059 % (image_path, part_size), shell=True)
AKASHI Takahiro58c95c22020-04-14 11:51:49 +090060 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 Takahiro719e89d2020-07-08 14:01:58 +090062 % (image_path, image_path, 1), shell=True)
AKASHI Takahiro58c95c22020-04-14 11:51:49 +090063 check_call('rm %s.tmp' % image_path, shell=True)
Heinrich Schuchardtb7e1d652020-04-21 18:44:39 +020064 loop_dev = check_output('sudo losetup -o 1MiB --sizelimit %dMiB --show -f %s | tr -d "\n"'
AKASHI Takahiro58c95c22020-04-14 11:51:49 +090065 % (part_size, image_path), shell=True).decode()
Heinrich Schuchardtb7e1d652020-04-21 18:44:39 +020066 check_output('sudo mount -t %s -o umask=000 %s %s'
AKASHI Takahiro719e89d2020-07-08 14:01:58 +090067 % (fs_type, loop_dev, mnt_point), shell=True)
AKASHI Takahiro58c95c22020-04-14 11:51:49 +090068
AKASHI Takahiro58c95c22020-04-14 11:51:49 +090069 # 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 Takahiro719e89d2020-07-08 14:01:58 +090079 # PK
AKASHI Takahiro58c95c22020-04-14 11:51:49 +090080 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 Takahiro719e89d2020-07-08 14:01:58 +090081 % mnt_point, shell=True)
Heinrich Schuchardt446b1482020-07-02 08:25:46 +020082 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 Takahiro719e89d2020-07-08 14:01:58 +090083 % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH),
84 shell=True)
85 # PK_null for deletion
Heinrich Schuchardt446b1482020-07-02 08:25:46 +020086 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 Takahiro719e89d2020-07-08 14:01:58 +090087 % (mnt_point, EFITOOLS_PATH), shell=True)
88 # KEK
AKASHI Takahiro58c95c22020-04-14 11:51:49 +090089 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 Takahiro719e89d2020-07-08 14:01:58 +090090 % mnt_point, shell=True)
Heinrich Schuchardt446b1482020-07-02 08:25:46 +020091 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 Takahiro719e89d2020-07-08 14:01:58 +090092 % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH),
93 shell=True)
94 # db
AKASHI Takahiro58c95c22020-04-14 11:51:49 +090095 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 Takahiro719e89d2020-07-08 14:01:58 +090096 % mnt_point, shell=True)
Heinrich Schuchardt446b1482020-07-02 08:25:46 +020097 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 Takahiro719e89d2020-07-08 14:01:58 +090098 % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH),
99 shell=True)
100 # db1
AKASHI Takahiro58c95c22020-04-14 11:51:49 +0900101 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 Takahiro719e89d2020-07-08 14:01:58 +0900102 % mnt_point, shell=True)
Heinrich Schuchardt446b1482020-07-02 08:25:46 +0200103 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 Takahiro719e89d2020-07-08 14:01:58 +0900104 % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH),
105 shell=True)
106 # db1-update
Heinrich Schuchardt446b1482020-07-02 08:25:46 +0200107 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 Takahiro719e89d2020-07-08 14:01:58 +0900108 % (mnt_point, EFITOOLS_PATH), shell=True)
AKASHI Takahirod4ece532020-07-08 14:02:01 +0900109 ## dbx (TEST_dbx certificate)
AKASHI Takahiro58c95c22020-04-14 11:51:49 +0900110 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 Takahiro719e89d2020-07-08 14:01:58 +0900111 % mnt_point, shell=True)
Heinrich Schuchardt446b1482020-07-02 08:25:46 +0200112 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 Takahiro719e89d2020-07-08 14:01:58 +0900113 % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH),
114 shell=True)
AKASHI Takahirod4ece532020-07-08 14:02:01 +0900115 ## 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 Takahirodb20a3e2020-07-08 14:02:02 +0900119 ## 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 Takahiro0f6e3eb2020-07-08 14:02:03 +0900123 ## 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 Takahiro58c95c22020-04-14 11:51:49 +0900127
128 # Copy image
129 check_call('cp %s %s' % (HELLO_PATH, mnt_point), shell=True)
130
AKASHI Takahiro719e89d2020-07-08 14:01:58 +0900131 # Sign image
AKASHI Takahiro58c95c22020-04-14 11:51:49 +0900132 check_call('cd %s; sbsign --key db.key --cert db.crt helloworld.efi'
AKASHI Takahiro719e89d2020-07-08 14:01:58 +0900133 % mnt_point, shell=True)
AKASHI Takahirodb20a3e2020-07-08 14:02:02 +0900134 ## 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 Schuchardt446b1482020-07-02 08:25:46 +0200138 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 Takahiro719e89d2020-07-08 14:01:58 +0900139 % (mnt_point, EFITOOLS_PATH, EFITOOLS_PATH),
140 shell=True)
AKASHI Takahiro0f6e3eb2020-07-08 14:02:03 +0900141 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 Takahiro58c95c22020-04-14 11:51:49 +0900147
Heinrich Schuchardtb7e1d652020-04-21 18:44:39 +0200148 check_call('sudo umount %s' % loop_dev, shell=True)
149 check_call('sudo losetup -d %s' % loop_dev, shell=True)
AKASHI Takahiro58c95c22020-04-14 11:51:49 +0900150
AKASHI Takahirobe5dbf02020-07-08 14:01:59 +0900151 except CalledProcessError as exception:
152 pytest.skip('Setup failed: %s' % exception.cmd)
AKASHI Takahiro58c95c22020-04-14 11:51:49 +0900153 return
154 else:
155 yield image_path
156 finally:
157 call('rm -f %s' % image_path, shell=True)