]>
Commit | Line | Data |
---|---|---|
fe6ca4d5 AT |
1 | # SPDX-License-Identifier: GPL-2.0+ |
2 | # Copyright (c) 2019, Linaro Limited | |
3 | # Author: AKASHI Takahiro <takahiro.akashi@linaro.org> | |
4 | ||
bd730aa0 | 5 | """Fixture for UEFI secure boot test.""" |
874490c7 HS |
6 | |
7 | from subprocess import call, check_call, CalledProcessError | |
d863b307 | 8 | import pytest |
fe6ca4d5 AT |
9 | from defs import * |
10 | ||
fe6ca4d5 AT |
11 | @pytest.fixture(scope='session') |
12 | def 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. | |
1be82afa | 17 | u_boot_config: U-Boot configuration. |
fe6ca4d5 AT |
18 | |
19 | Return: | |
20 | A path to disk image to be used for testing | |
21 | """ | |
fe6ca4d5 | 22 | image_path = u_boot_config.persistent_data_dir |
57be8cdc | 23 | image_path = image_path + '/test_efi_secboot.img' |
fe6ca4d5 AT |
24 | |
25 | try: | |
53ce9a6e HS |
26 | mnt_point = u_boot_config.build_dir + '/mnt_efisecure' |
27 | check_call('rm -rf {}'.format(mnt_point), shell=True) | |
b32ac16f | 28 | check_call('mkdir -p {}'.format(mnt_point), shell=True) |
fe6ca4d5 | 29 | |
fe6ca4d5 AT |
30 | # 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 | |
91d2b621 | 40 | # PK |
fe6ca4d5 | 41 | check_call('cd %s; openssl req -x509 -sha256 -newkey rsa:2048 -subj /CN=TEST_PK/ -keyout PK.key -out PK.crt -nodes -days 365' |
91d2b621 | 42 | % mnt_point, shell=True) |
bc246c69 | 43 | 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' |
91d2b621 AT |
44 | % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH), |
45 | shell=True) | |
46 | # PK_null for deletion | |
bc246c69 | 47 | 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' |
91d2b621 AT |
48 | % (mnt_point, EFITOOLS_PATH), shell=True) |
49 | # KEK | |
fe6ca4d5 | 50 | check_call('cd %s; openssl req -x509 -sha256 -newkey rsa:2048 -subj /CN=TEST_KEK/ -keyout KEK.key -out KEK.crt -nodes -days 365' |
91d2b621 | 51 | % mnt_point, shell=True) |
bc246c69 | 52 | 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' |
91d2b621 AT |
53 | % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH), |
54 | shell=True) | |
55 | # db | |
fe6ca4d5 | 56 | check_call('cd %s; openssl req -x509 -sha256 -newkey rsa:2048 -subj /CN=TEST_db/ -keyout db.key -out db.crt -nodes -days 365' |
91d2b621 | 57 | % mnt_point, shell=True) |
bc246c69 | 58 | 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' |
91d2b621 AT |
59 | % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH), |
60 | shell=True) | |
61 | # db1 | |
fe6ca4d5 | 62 | check_call('cd %s; openssl req -x509 -sha256 -newkey rsa:2048 -subj /CN=TEST_db1/ -keyout db1.key -out db1.crt -nodes -days 365' |
91d2b621 | 63 | % mnt_point, shell=True) |
bc246c69 | 64 | 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' |
91d2b621 AT |
65 | % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH), |
66 | shell=True) | |
a58dfd29 | 67 | # dbx (TEST_dbx certificate) |
fe6ca4d5 | 68 | check_call('cd %s; openssl req -x509 -sha256 -newkey rsa:2048 -subj /CN=TEST_dbx/ -keyout dbx.key -out dbx.crt -nodes -days 365' |
91d2b621 | 69 | % mnt_point, shell=True) |
bc246c69 | 70 | 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' |
91d2b621 AT |
71 | % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH), |
72 | shell=True) | |
a58dfd29 | 73 | # dbx_hash (digest of TEST_db certificate) |
0b4cbeba | 74 | 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' |
c6361e73 AT |
75 | % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH), |
76 | shell=True) | |
0b4cbeba | 77 | 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' |
4b494770 IA |
78 | % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH), |
79 | shell=True) | |
0b4cbeba | 80 | 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' |
4b494770 IA |
81 | % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH), |
82 | shell=True) | |
a58dfd29 | 83 | # dbx_hash1 (digest of TEST_db1 certificate) |
0b4cbeba | 84 | 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' |
0c7772d4 AT |
85 | % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH), |
86 | shell=True) | |
a58dfd29 | 87 | # dbx_db (with TEST_db certificate) |
7fdc02b3 AT |
88 | check_call('cd %s; %ssign-efi-sig-list -t "2020-04-05" -c KEK.crt -k KEK.key dbx db.esl dbx_db.auth' |
89 | % (mnt_point, EFITOOLS_PATH), | |
90 | shell=True) | |
fe6ca4d5 AT |
91 | |
92 | # Copy image | |
57be8cdc AT |
93 | check_call('cp %s/lib/efi_loader/helloworld.efi %s' % |
94 | (u_boot_config.build_dir, mnt_point), shell=True) | |
fe6ca4d5 | 95 | |
91d2b621 | 96 | # Sign image |
fe6ca4d5 | 97 | check_call('cd %s; sbsign --key db.key --cert db.crt helloworld.efi' |
91d2b621 | 98 | % mnt_point, shell=True) |
a58dfd29 | 99 | # Sign already-signed image with another key |
0c7772d4 AT |
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) | |
8fb9dbde AT |
102 | # Create a corrupted signed image |
103 | check_call('cd %s; sh %s/test/py/tests/test_efi_secboot/forge_image.sh helloworld.efi.signed helloworld_forged.efi.signed' | |
104 | % (mnt_point, u_boot_config.source_dir), shell=True) | |
a58dfd29 | 105 | # Digest image |
bc246c69 | 106 | 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' |
91d2b621 AT |
107 | % (mnt_point, EFITOOLS_PATH, EFITOOLS_PATH), |
108 | shell=True) | |
7fdc02b3 AT |
109 | 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' |
110 | % (mnt_point, EFITOOLS_PATH, EFITOOLS_PATH), | |
111 | shell=True) | |
112 | 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' | |
113 | % (mnt_point, EFITOOLS_PATH), | |
114 | shell=True) | |
fe6ca4d5 | 115 | |
a58dfd29 AT |
116 | check_call('virt-make-fs --partition=gpt --size=+1M --type=vfat {} {}'.format( |
117 | mnt_point, image_path), shell=True) | |
53ce9a6e | 118 | check_call('rm -rf {}'.format(mnt_point), shell=True) |
fe6ca4d5 | 119 | |
d863b307 AT |
120 | except CalledProcessError as exception: |
121 | pytest.skip('Setup failed: %s' % exception.cmd) | |
fe6ca4d5 AT |
122 | return |
123 | else: | |
124 | yield image_path | |
125 | finally: | |
126 | call('rm -f %s' % image_path, shell=True) | |
e1174c56 AT |
127 | |
128 | # | |
129 | # Fixture for UEFI secure boot test of intermediate certificates | |
130 | # | |
131 | ||
132 | ||
133 | @pytest.fixture(scope='session') | |
134 | def efi_boot_env_intca(request, u_boot_config): | |
bd730aa0 HS |
135 | """Set up file system for secure boot test. |
136 | ||
137 | Set up a file system to be used in UEFI secure boot test | |
e1174c56 AT |
138 | of intermediate certificates. |
139 | ||
140 | Args: | |
141 | request: Pytest request object. | |
1be82afa | 142 | u_boot_config: U-Boot configuration. |
e1174c56 AT |
143 | |
144 | Return: | |
145 | A path to disk image to be used for testing | |
146 | """ | |
147 | image_path = u_boot_config.persistent_data_dir | |
148 | image_path = image_path + '/test_efi_secboot_intca.img' | |
149 | ||
150 | try: | |
151 | mnt_point = u_boot_config.persistent_data_dir + '/mnt_efi_secboot_intca' | |
152 | check_call('rm -rf {}'.format(mnt_point), shell=True) | |
153 | check_call('mkdir -p {}'.format(mnt_point), shell=True) | |
154 | ||
155 | # Create signature database | |
156 | # PK | |
157 | check_call('cd %s; openssl req -x509 -sha256 -newkey rsa:2048 -subj /CN=TEST_PK/ -keyout PK.key -out PK.crt -nodes -days 365' | |
158 | % mnt_point, shell=True) | |
159 | 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' | |
160 | % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH), | |
161 | shell=True) | |
162 | # KEK | |
163 | check_call('cd %s; openssl req -x509 -sha256 -newkey rsa:2048 -subj /CN=TEST_KEK/ -keyout KEK.key -out KEK.crt -nodes -days 365' | |
164 | % mnt_point, shell=True) | |
165 | 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' | |
166 | % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH), | |
167 | shell=True) | |
168 | ||
169 | # We will have three-tier hierarchy of certificates: | |
170 | # TestRoot: Root CA (self-signed) | |
171 | # TestSub: Intermediate CA (signed by Root CA) | |
172 | # TestCert: User certificate (signed by Intermediate CA, and used | |
173 | # for signing an image) | |
174 | # | |
175 | # NOTE: | |
176 | # I consulted the following EDK2 document for certificate options: | |
177 | # BaseTools/Source/Python/Pkcs7Sign/Readme.md | |
178 | # Please not use them as they are in product system. They are | |
179 | # for test purpose only. | |
180 | ||
181 | # TestRoot | |
182 | check_call('cp %s/test/py/tests/test_efi_secboot/openssl.cnf %s' | |
183 | % (u_boot_config.source_dir, mnt_point), shell=True) | |
184 | 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' | |
185 | % mnt_point, shell=True) | |
186 | # TestSub | |
187 | 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' | |
188 | % mnt_point, shell=True) | |
189 | # TestCert | |
190 | 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' | |
191 | % mnt_point, shell=True) | |
192 | # db | |
193 | # for TestCert | |
194 | 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' | |
195 | % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH), | |
196 | shell=True) | |
197 | # for TestSub | |
198 | 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' | |
199 | % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH), | |
200 | shell=True) | |
201 | # for TestRoot | |
202 | 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' | |
203 | % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH), | |
204 | shell=True) | |
205 | ## dbx (hash of certificate with revocation time) | |
206 | # for TestCert | |
207 | 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' | |
208 | % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH), | |
209 | shell=True) | |
210 | # for TestSub | |
211 | 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' | |
212 | % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH), | |
213 | shell=True) | |
214 | # for TestRoot | |
215 | 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' | |
216 | % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH), | |
217 | shell=True) | |
218 | ||
219 | # Sign image | |
220 | # additional intermediate certificates may be included | |
221 | # in SignedData | |
222 | ||
223 | check_call('cp %s/lib/efi_loader/helloworld.efi %s' % | |
224 | (u_boot_config.build_dir, mnt_point), shell=True) | |
225 | # signed by TestCert | |
226 | check_call('cd %s; %ssbsign --key TestCert.key --cert TestCert.crt --out helloworld.efi.signed_a helloworld.efi' | |
227 | % (mnt_point, SBSIGN_PATH), shell=True) | |
228 | # signed by TestCert with TestSub in signature | |
229 | check_call('cd %s; %ssbsign --key TestCert.key --cert TestCert.crt --addcert TestSub.crt --out helloworld.efi.signed_ab helloworld.efi' | |
230 | % (mnt_point, SBSIGN_PATH), shell=True) | |
231 | # signed by TestCert with TestSub and TestRoot in signature | |
232 | 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' | |
233 | % (mnt_point, SBSIGN_PATH), shell=True) | |
234 | ||
235 | check_call('virt-make-fs --partition=gpt --size=+1M --type=vfat {} {}'.format(mnt_point, image_path), shell=True) | |
236 | check_call('rm -rf {}'.format(mnt_point), shell=True) | |
237 | ||
238 | except CalledProcessError as e: | |
239 | pytest.skip('Setup failed: %s' % e.cmd) | |
240 | return | |
241 | else: | |
242 | yield image_path | |
243 | finally: | |
244 | call('rm -f %s' % image_path, shell=True) |