blob: 0fa0747fc764a1a59b3005e66007f8893a7bb237 [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
Heinrich Schuchardt14dee8b2023-05-03 07:08:05 +02005"""Fixture for UEFI secure boot test."""
Heinrich Schuchardt4c18acf2022-10-01 20:55:14 +02006
7from subprocess import call, check_call, CalledProcessError
AKASHI Takahirobe5dbf02020-07-08 14:01:59 +09008import pytest
AKASHI Takahiro58c95c22020-04-14 11:51:49 +09009from defs import *
10
AKASHI Takahiro58c95c22020-04-14 11:51:49 +090011@pytest.fixture(scope='session')
12def efi_boot_env(request, u_boot_config):
13 """Set up a file system to be used in UEFI secure boot test.
14
15 Args:
16 request: Pytest request object.
Michal Simek50fa1182023-05-17 09:17:16 +020017 u_boot_config: U-Boot configuration.
AKASHI Takahiro58c95c22020-04-14 11:51:49 +090018
19 Return:
20 A path to disk image to be used for testing
21 """
AKASHI Takahiro58c95c22020-04-14 11:51:49 +090022 image_path = u_boot_config.persistent_data_dir
AKASHI Takahiro6834fda2020-07-21 19:35:23 +090023 image_path = image_path + '/test_efi_secboot.img'
AKASHI Takahiro58c95c22020-04-14 11:51:49 +090024
25 try:
Heinrich Schuchardt33424ea2020-07-11 23:05:18 +020026 mnt_point = u_boot_config.build_dir + '/mnt_efisecure'
27 check_call('rm -rf {}'.format(mnt_point), shell=True)
Heinrich Schuchardtb7e1d652020-04-21 18:44:39 +020028 check_call('mkdir -p {}'.format(mnt_point), shell=True)
AKASHI Takahiro58c95c22020-04-14 11:51:49 +090029
AKASHI Takahiro58c95c22020-04-14 11:51:49 +090030 # suffix
31 # *.key: RSA private key in PEM
32 # *.crt: X509 certificate (self-signed) in PEM
33 # *.esl: signature list
34 # *.hash: message digest of image as signature list
35 # *.auth: signed signature list in signature database format
36 # *.efi: UEFI image
37 # *.efi.signed: signed UEFI image
38
39 # Create signature database
AKASHI Takahiro719e89d2020-07-08 14:01:58 +090040 # PK
AKASHI Takahiro58c95c22020-04-14 11:51:49 +090041 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 +090042 % mnt_point, shell=True)
Heinrich Schuchardt446b1482020-07-02 08:25:46 +020043 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 +090044 % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH),
45 shell=True)
46 # PK_null for deletion
Heinrich Schuchardt446b1482020-07-02 08:25:46 +020047 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 +090048 % (mnt_point, EFITOOLS_PATH), shell=True)
49 # KEK
AKASHI Takahiro58c95c22020-04-14 11:51:49 +090050 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 +090051 % mnt_point, shell=True)
Heinrich Schuchardt446b1482020-07-02 08:25:46 +020052 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 +090053 % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH),
54 shell=True)
55 # db
AKASHI Takahiro58c95c22020-04-14 11:51:49 +090056 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 +090057 % mnt_point, shell=True)
Heinrich Schuchardt446b1482020-07-02 08:25:46 +020058 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 +090059 % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH),
60 shell=True)
61 # db1
AKASHI Takahiro58c95c22020-04-14 11:51:49 +090062 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 +090063 % mnt_point, shell=True)
Heinrich Schuchardt446b1482020-07-02 08:25:46 +020064 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 +090065 % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH),
66 shell=True)
Weizhao Ouyang73d752b2024-05-08 19:13:12 +080067 # db2 (APPEND_WRITE)
68 check_call('cd %s; openssl req -x509 -sha256 -newkey rsa:2048 -subj /CN=TEST_db2/ -keyout db2.key -out db2.crt -nodes -days 365'
69 % mnt_point, shell=True)
70 check_call('cd %s; %scert-to-efi-sig-list -g %s db2.crt db2.esl; %ssign-efi-sig-list -a -c KEK.crt -k KEK.key db db2.esl db2.auth'
71 % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH),
72 shell=True)
AKASHI Takahiro798f3062020-07-20 15:33:39 +090073 # dbx (TEST_dbx certificate)
AKASHI Takahiro58c95c22020-04-14 11:51:49 +090074 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 +090075 % mnt_point, shell=True)
Heinrich Schuchardt446b1482020-07-02 08:25:46 +020076 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 +090077 % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH),
78 shell=True)
AKASHI Takahiro798f3062020-07-20 15:33:39 +090079 # dbx_hash (digest of TEST_db certificate)
Masahisa Kojimaf29ced62022-10-03 16:12:15 +090080 check_call('cd %s; %scert-to-efi-hash-list -g %s -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'
AKASHI Takahirod4ece532020-07-08 14:02:01 +090081 % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH),
82 shell=True)
Masahisa Kojimaf29ced62022-10-03 16:12:15 +090083 check_call('cd %s; %scert-to-efi-hash-list -g %s -s 384 db.crt dbx_hash384.crl; %ssign-efi-sig-list -t "2020-04-05" -c KEK.crt -k KEK.key dbx dbx_hash384.crl dbx_hash384.auth'
Ilias Apalodimas578c98b2022-05-06 15:36:01 +030084 % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH),
85 shell=True)
Masahisa Kojimaf29ced62022-10-03 16:12:15 +090086 check_call('cd %s; %scert-to-efi-hash-list -g %s -s 512 db.crt dbx_hash512.crl; %ssign-efi-sig-list -t "2020-04-05" -c KEK.crt -k KEK.key dbx dbx_hash512.crl dbx_hash512.auth'
Ilias Apalodimas578c98b2022-05-06 15:36:01 +030087 % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH),
88 shell=True)
AKASHI Takahiro798f3062020-07-20 15:33:39 +090089 # dbx_hash1 (digest of TEST_db1 certificate)
Masahisa Kojimaf29ced62022-10-03 16:12:15 +090090 check_call('cd %s; %scert-to-efi-hash-list -g %s -s 256 db1.crt dbx_hash1.crl; %ssign-efi-sig-list -t "2020-04-06" -c KEK.crt -k KEK.key dbx dbx_hash1.crl dbx_hash1.auth'
AKASHI Takahirodb20a3e2020-07-08 14:02:02 +090091 % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH),
92 shell=True)
Weizhao Ouyang73d752b2024-05-08 19:13:12 +080093 # dbx_hash2 (digest of TEST_db2 certificate, with APPEND_WRITE)
94 check_call('cd %s; %scert-to-efi-hash-list -g %s -s 256 db2.crt dbx_hash2.crl; %ssign-efi-sig-list -a -c KEK.crt -k KEK.key dbx dbx_hash2.crl dbx_hash2.auth'
95 % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH),
96 shell=True)
AKASHI Takahiro798f3062020-07-20 15:33:39 +090097 # dbx_db (with TEST_db certificate)
AKASHI Takahiro0f6e3eb2020-07-08 14:02:03 +090098 check_call('cd %s; %ssign-efi-sig-list -t "2020-04-05" -c KEK.crt -k KEK.key dbx db.esl dbx_db.auth'
99 % (mnt_point, EFITOOLS_PATH),
100 shell=True)
AKASHI Takahiro58c95c22020-04-14 11:51:49 +0900101
102 # Copy image
AKASHI Takahiro6834fda2020-07-21 19:35:23 +0900103 check_call('cp %s/lib/efi_loader/helloworld.efi %s' %
104 (u_boot_config.build_dir, mnt_point), shell=True)
AKASHI Takahiro58c95c22020-04-14 11:51:49 +0900105
AKASHI Takahiro719e89d2020-07-08 14:01:58 +0900106 # Sign image
AKASHI Takahiro58c95c22020-04-14 11:51:49 +0900107 check_call('cd %s; sbsign --key db.key --cert db.crt helloworld.efi'
AKASHI Takahiro719e89d2020-07-08 14:01:58 +0900108 % mnt_point, shell=True)
AKASHI Takahiro798f3062020-07-20 15:33:39 +0900109 # Sign already-signed image with another key
AKASHI Takahirodb20a3e2020-07-08 14:02:02 +0900110 check_call('cd %s; sbsign --key db1.key --cert db1.crt --output helloworld.efi.signed_2sigs helloworld.efi.signed'
111 % mnt_point, shell=True)
AKASHI Takahirod968b792022-07-05 14:48:15 +0900112 # Create a corrupted signed image
113 check_call('cd %s; sh %s/test/py/tests/test_efi_secboot/forge_image.sh helloworld.efi.signed helloworld_forged.efi.signed'
114 % (mnt_point, u_boot_config.source_dir), shell=True)
AKASHI Takahiro798f3062020-07-20 15:33:39 +0900115 # Digest image
Heinrich Schuchardt446b1482020-07-02 08:25:46 +0200116 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 +0900117 % (mnt_point, EFITOOLS_PATH, EFITOOLS_PATH),
118 shell=True)
AKASHI Takahiro0f6e3eb2020-07-08 14:02:03 +0900119 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'
120 % (mnt_point, EFITOOLS_PATH, EFITOOLS_PATH),
121 shell=True)
122 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'
123 % (mnt_point, EFITOOLS_PATH),
124 shell=True)
AKASHI Takahiro58c95c22020-04-14 11:51:49 +0900125
AKASHI Takahiro798f3062020-07-20 15:33:39 +0900126 check_call('virt-make-fs --partition=gpt --size=+1M --type=vfat {} {}'.format(
127 mnt_point, image_path), shell=True)
Heinrich Schuchardt33424ea2020-07-11 23:05:18 +0200128 check_call('rm -rf {}'.format(mnt_point), shell=True)
AKASHI Takahiro58c95c22020-04-14 11:51:49 +0900129
AKASHI Takahirobe5dbf02020-07-08 14:01:59 +0900130 except CalledProcessError as exception:
131 pytest.skip('Setup failed: %s' % exception.cmd)
AKASHI Takahiro58c95c22020-04-14 11:51:49 +0900132 return
133 else:
134 yield image_path
135 finally:
136 call('rm -f %s' % image_path, shell=True)
AKASHI Takahiro79f428e2020-07-21 19:35:24 +0900137
138#
139# Fixture for UEFI secure boot test of intermediate certificates
140#
141
142
143@pytest.fixture(scope='session')
144def efi_boot_env_intca(request, u_boot_config):
Heinrich Schuchardt14dee8b2023-05-03 07:08:05 +0200145 """Set up file system for secure boot test.
146
147 Set up a file system to be used in UEFI secure boot test
AKASHI Takahiro79f428e2020-07-21 19:35:24 +0900148 of intermediate certificates.
149
150 Args:
151 request: Pytest request object.
Michal Simek50fa1182023-05-17 09:17:16 +0200152 u_boot_config: U-Boot configuration.
AKASHI Takahiro79f428e2020-07-21 19:35:24 +0900153
154 Return:
155 A path to disk image to be used for testing
156 """
157 image_path = u_boot_config.persistent_data_dir
158 image_path = image_path + '/test_efi_secboot_intca.img'
159
160 try:
161 mnt_point = u_boot_config.persistent_data_dir + '/mnt_efi_secboot_intca'
162 check_call('rm -rf {}'.format(mnt_point), shell=True)
163 check_call('mkdir -p {}'.format(mnt_point), shell=True)
164
165 # Create signature database
166 # PK
167 check_call('cd %s; openssl req -x509 -sha256 -newkey rsa:2048 -subj /CN=TEST_PK/ -keyout PK.key -out PK.crt -nodes -days 365'
168 % mnt_point, shell=True)
169 check_call('cd %s; %scert-to-efi-sig-list -g %s PK.crt PK.esl; %ssign-efi-sig-list -c PK.crt -k PK.key PK PK.esl PK.auth'
170 % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH),
171 shell=True)
172 # KEK
173 check_call('cd %s; openssl req -x509 -sha256 -newkey rsa:2048 -subj /CN=TEST_KEK/ -keyout KEK.key -out KEK.crt -nodes -days 365'
174 % mnt_point, shell=True)
175 check_call('cd %s; %scert-to-efi-sig-list -g %s KEK.crt KEK.esl; %ssign-efi-sig-list -c PK.crt -k PK.key KEK KEK.esl KEK.auth'
176 % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH),
177 shell=True)
178
179 # We will have three-tier hierarchy of certificates:
180 # TestRoot: Root CA (self-signed)
181 # TestSub: Intermediate CA (signed by Root CA)
182 # TestCert: User certificate (signed by Intermediate CA, and used
183 # for signing an image)
184 #
185 # NOTE:
186 # I consulted the following EDK2 document for certificate options:
187 # BaseTools/Source/Python/Pkcs7Sign/Readme.md
188 # Please not use them as they are in product system. They are
189 # for test purpose only.
190
191 # TestRoot
192 check_call('cp %s/test/py/tests/test_efi_secboot/openssl.cnf %s'
193 % (u_boot_config.source_dir, mnt_point), shell=True)
194 check_call('cd %s; export OPENSSL_CONF=./openssl.cnf; openssl genrsa -out TestRoot.key 2048; openssl req -extensions v3_ca -new -x509 -days 365 -key TestRoot.key -out TestRoot.crt -subj "/CN=TEST_root/"; touch index.txt; touch index.txt.attr'
195 % mnt_point, shell=True)
196 # TestSub
197 check_call('cd %s; touch serial.new; export OPENSSL_CONF=./openssl.cnf; openssl genrsa -out TestSub.key 2048; openssl req -new -key TestSub.key -out TestSub.csr -subj "/CN=TEST_sub/"; openssl ca -in TestSub.csr -out TestSub.crt -extensions v3_int_ca -days 365 -batch -rand_serial -cert TestRoot.crt -keyfile TestRoot.key'
198 % mnt_point, shell=True)
199 # TestCert
200 check_call('cd %s; touch serial.new; export OPENSSL_CONF=./openssl.cnf; openssl genrsa -out TestCert.key 2048; openssl req -new -key TestCert.key -out TestCert.csr -subj "/CN=TEST_cert/"; openssl ca -in TestCert.csr -out TestCert.crt -extensions usr_cert -days 365 -batch -rand_serial -cert TestSub.crt -keyfile TestSub.key'
201 % mnt_point, shell=True)
202 # db
203 # for TestCert
204 check_call('cd %s; %scert-to-efi-sig-list -g %s TestCert.crt TestCert.esl; %ssign-efi-sig-list -c KEK.crt -k KEK.key db TestCert.esl db_a.auth'
205 % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH),
206 shell=True)
207 # for TestSub
208 check_call('cd %s; %scert-to-efi-sig-list -g %s TestSub.crt TestSub.esl; %ssign-efi-sig-list -t "2020-07-16" -c KEK.crt -k KEK.key db TestSub.esl db_b.auth'
209 % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH),
210 shell=True)
211 # for TestRoot
212 check_call('cd %s; %scert-to-efi-sig-list -g %s TestRoot.crt TestRoot.esl; %ssign-efi-sig-list -t "2020-07-17" -c KEK.crt -k KEK.key db TestRoot.esl db_c.auth'
213 % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH),
214 shell=True)
215 ## dbx (hash of certificate with revocation time)
216 # for TestCert
217 check_call('cd %s; %scert-to-efi-hash-list -g %s -t "2020-07-20" -s 256 TestCert.crt TestCert.crl; %ssign-efi-sig-list -c KEK.crt -k KEK.key dbx TestCert.crl dbx_a.auth'
218 % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH),
219 shell=True)
220 # for TestSub
221 check_call('cd %s; %scert-to-efi-hash-list -g %s -t "2020-07-21" -s 256 TestSub.crt TestSub.crl; %ssign-efi-sig-list -t "2020-07-18" -c KEK.crt -k KEK.key dbx TestSub.crl dbx_b.auth'
222 % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH),
223 shell=True)
224 # for TestRoot
225 check_call('cd %s; %scert-to-efi-hash-list -g %s -t "2020-07-22" -s 256 TestRoot.crt TestRoot.crl; %ssign-efi-sig-list -t "2020-07-19" -c KEK.crt -k KEK.key dbx TestRoot.crl dbx_c.auth'
226 % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH),
227 shell=True)
228
229 # Sign image
230 # additional intermediate certificates may be included
231 # in SignedData
232
233 check_call('cp %s/lib/efi_loader/helloworld.efi %s' %
234 (u_boot_config.build_dir, mnt_point), shell=True)
235 # signed by TestCert
236 check_call('cd %s; %ssbsign --key TestCert.key --cert TestCert.crt --out helloworld.efi.signed_a helloworld.efi'
237 % (mnt_point, SBSIGN_PATH), shell=True)
238 # signed by TestCert with TestSub in signature
239 check_call('cd %s; %ssbsign --key TestCert.key --cert TestCert.crt --addcert TestSub.crt --out helloworld.efi.signed_ab helloworld.efi'
240 % (mnt_point, SBSIGN_PATH), shell=True)
241 # signed by TestCert with TestSub and TestRoot in signature
242 check_call('cd %s; cat TestSub.crt TestRoot.crt > TestSubRoot.crt; %ssbsign --key TestCert.key --cert TestCert.crt --addcert TestSubRoot.crt --out helloworld.efi.signed_abc helloworld.efi'
243 % (mnt_point, SBSIGN_PATH), shell=True)
244
245 check_call('virt-make-fs --partition=gpt --size=+1M --type=vfat {} {}'.format(mnt_point, image_path), shell=True)
246 check_call('rm -rf {}'.format(mnt_point), shell=True)
247
248 except CalledProcessError as e:
249 pytest.skip('Setup failed: %s' % e.cmd)
250 return
251 else:
252 yield image_path
253 finally:
254 call('rm -f %s' % image_path, shell=True)