blob: 8a53dabe54144f7603f678fcae5ff770227efc4a [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
AKASHI Takahiro58c95c22020-04-14 11:51:49 +090011
12#
13# Fixture for UEFI secure boot test
14#
AKASHI Takahiro719e89d2020-07-08 14:01:58 +090015
16
AKASHI Takahiro58c95c22020-04-14 11:51:49 +090017@pytest.fixture(scope='session')
18def 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 Takahiro719e89d2020-07-08 14:01:58 +090023 u_boot_config: U-boot configuration.
AKASHI Takahiro58c95c22020-04-14 11:51:49 +090024
25 Return:
26 A path to disk image to be used for testing
27 """
AKASHI Takahiro58c95c22020-04-14 11:51:49 +090028 image_path = u_boot_config.persistent_data_dir
AKASHI Takahiro6834fda2020-07-21 19:35:23 +090029 image_path = image_path + '/test_efi_secboot.img'
AKASHI Takahiro58c95c22020-04-14 11:51:49 +090030
31 try:
Heinrich Schuchardt33424ea2020-07-11 23:05:18 +020032 mnt_point = u_boot_config.build_dir + '/mnt_efisecure'
33 check_call('rm -rf {}'.format(mnt_point), shell=True)
Heinrich Schuchardtb7e1d652020-04-21 18:44:39 +020034 check_call('mkdir -p {}'.format(mnt_point), shell=True)
AKASHI Takahiro58c95c22020-04-14 11:51:49 +090035
AKASHI Takahiro58c95c22020-04-14 11:51:49 +090036 # suffix
37 # *.key: RSA private key in PEM
38 # *.crt: X509 certificate (self-signed) in PEM
39 # *.esl: signature list
40 # *.hash: message digest of image as signature list
41 # *.auth: signed signature list in signature database format
42 # *.efi: UEFI image
43 # *.efi.signed: signed UEFI image
44
45 # Create signature database
AKASHI Takahiro719e89d2020-07-08 14:01:58 +090046 # PK
AKASHI Takahiro58c95c22020-04-14 11:51:49 +090047 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 +090048 % mnt_point, shell=True)
Heinrich Schuchardt446b1482020-07-02 08:25:46 +020049 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 +090050 % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH),
51 shell=True)
52 # PK_null for deletion
Heinrich Schuchardt446b1482020-07-02 08:25:46 +020053 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 +090054 % (mnt_point, EFITOOLS_PATH), shell=True)
55 # KEK
AKASHI Takahiro58c95c22020-04-14 11:51:49 +090056 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 +090057 % mnt_point, shell=True)
Heinrich Schuchardt446b1482020-07-02 08:25:46 +020058 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 +090059 % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH),
60 shell=True)
61 # db
AKASHI Takahiro58c95c22020-04-14 11:51:49 +090062 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 +090063 % mnt_point, shell=True)
Heinrich Schuchardt446b1482020-07-02 08:25:46 +020064 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 +090065 % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH),
66 shell=True)
67 # db1
AKASHI Takahiro58c95c22020-04-14 11:51:49 +090068 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 +090069 % mnt_point, shell=True)
Heinrich Schuchardt446b1482020-07-02 08:25:46 +020070 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 +090071 % (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)
AKASHI Takahirod4ece532020-07-08 14:02:01 +090080 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'
81 % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH),
82 shell=True)
Ilias Apalodimas578c98b2022-05-06 15:36:01 +030083 check_call('cd %s; %scert-to-efi-hash-list -g %s -t 0 -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'
84 % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH),
85 shell=True)
86 check_call('cd %s; %scert-to-efi-hash-list -g %s -t 0 -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'
87 % (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)
AKASHI Takahiroa59ffd32020-08-14 14:39:24 +090090 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-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)
AKASHI Takahiro798f3062020-07-20 15:33:39 +090093 # dbx_db (with TEST_db certificate)
AKASHI Takahiro0f6e3eb2020-07-08 14:02:03 +090094 check_call('cd %s; %ssign-efi-sig-list -t "2020-04-05" -c KEK.crt -k KEK.key dbx db.esl dbx_db.auth'
95 % (mnt_point, EFITOOLS_PATH),
96 shell=True)
AKASHI Takahiro58c95c22020-04-14 11:51:49 +090097
98 # Copy image
AKASHI Takahiro6834fda2020-07-21 19:35:23 +090099 check_call('cp %s/lib/efi_loader/helloworld.efi %s' %
100 (u_boot_config.build_dir, mnt_point), shell=True)
AKASHI Takahiro58c95c22020-04-14 11:51:49 +0900101
AKASHI Takahiro719e89d2020-07-08 14:01:58 +0900102 # Sign image
AKASHI Takahiro58c95c22020-04-14 11:51:49 +0900103 check_call('cd %s; sbsign --key db.key --cert db.crt helloworld.efi'
AKASHI Takahiro719e89d2020-07-08 14:01:58 +0900104 % mnt_point, shell=True)
AKASHI Takahiro798f3062020-07-20 15:33:39 +0900105 # Sign already-signed image with another key
AKASHI Takahirodb20a3e2020-07-08 14:02:02 +0900106 check_call('cd %s; sbsign --key db1.key --cert db1.crt --output helloworld.efi.signed_2sigs helloworld.efi.signed'
107 % mnt_point, shell=True)
AKASHI Takahiro798f3062020-07-20 15:33:39 +0900108 # Digest image
Heinrich Schuchardt446b1482020-07-02 08:25:46 +0200109 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 +0900110 % (mnt_point, EFITOOLS_PATH, EFITOOLS_PATH),
111 shell=True)
AKASHI Takahiro0f6e3eb2020-07-08 14:02:03 +0900112 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'
113 % (mnt_point, EFITOOLS_PATH, EFITOOLS_PATH),
114 shell=True)
115 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'
116 % (mnt_point, EFITOOLS_PATH),
117 shell=True)
AKASHI Takahiro58c95c22020-04-14 11:51:49 +0900118
AKASHI Takahiro798f3062020-07-20 15:33:39 +0900119 check_call('virt-make-fs --partition=gpt --size=+1M --type=vfat {} {}'.format(
120 mnt_point, image_path), shell=True)
Heinrich Schuchardt33424ea2020-07-11 23:05:18 +0200121 check_call('rm -rf {}'.format(mnt_point), shell=True)
AKASHI Takahiro58c95c22020-04-14 11:51:49 +0900122
AKASHI Takahirobe5dbf02020-07-08 14:01:59 +0900123 except CalledProcessError as exception:
124 pytest.skip('Setup failed: %s' % exception.cmd)
AKASHI Takahiro58c95c22020-04-14 11:51:49 +0900125 return
126 else:
127 yield image_path
128 finally:
129 call('rm -f %s' % image_path, shell=True)
AKASHI Takahiro79f428e2020-07-21 19:35:24 +0900130
131#
132# Fixture for UEFI secure boot test of intermediate certificates
133#
134
135
136@pytest.fixture(scope='session')
137def efi_boot_env_intca(request, u_boot_config):
138 """Set up a file system to be used in UEFI secure boot test
139 of intermediate certificates.
140
141 Args:
142 request: Pytest request object.
143 u_boot_config: U-boot configuration.
144
145 Return:
146 A path to disk image to be used for testing
147 """
148 image_path = u_boot_config.persistent_data_dir
149 image_path = image_path + '/test_efi_secboot_intca.img'
150
151 try:
152 mnt_point = u_boot_config.persistent_data_dir + '/mnt_efi_secboot_intca'
153 check_call('rm -rf {}'.format(mnt_point), shell=True)
154 check_call('mkdir -p {}'.format(mnt_point), shell=True)
155
156 # Create signature database
157 # PK
158 check_call('cd %s; openssl req -x509 -sha256 -newkey rsa:2048 -subj /CN=TEST_PK/ -keyout PK.key -out PK.crt -nodes -days 365'
159 % mnt_point, shell=True)
160 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'
161 % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH),
162 shell=True)
163 # KEK
164 check_call('cd %s; openssl req -x509 -sha256 -newkey rsa:2048 -subj /CN=TEST_KEK/ -keyout KEK.key -out KEK.crt -nodes -days 365'
165 % mnt_point, shell=True)
166 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'
167 % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH),
168 shell=True)
169
170 # We will have three-tier hierarchy of certificates:
171 # TestRoot: Root CA (self-signed)
172 # TestSub: Intermediate CA (signed by Root CA)
173 # TestCert: User certificate (signed by Intermediate CA, and used
174 # for signing an image)
175 #
176 # NOTE:
177 # I consulted the following EDK2 document for certificate options:
178 # BaseTools/Source/Python/Pkcs7Sign/Readme.md
179 # Please not use them as they are in product system. They are
180 # for test purpose only.
181
182 # TestRoot
183 check_call('cp %s/test/py/tests/test_efi_secboot/openssl.cnf %s'
184 % (u_boot_config.source_dir, mnt_point), shell=True)
185 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'
186 % mnt_point, shell=True)
187 # TestSub
188 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'
189 % mnt_point, shell=True)
190 # TestCert
191 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'
192 % mnt_point, shell=True)
193 # db
194 # for TestCert
195 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'
196 % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH),
197 shell=True)
198 # for TestSub
199 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'
200 % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH),
201 shell=True)
202 # for TestRoot
203 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'
204 % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH),
205 shell=True)
206 ## dbx (hash of certificate with revocation time)
207 # for TestCert
208 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'
209 % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH),
210 shell=True)
211 # for TestSub
212 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'
213 % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH),
214 shell=True)
215 # for TestRoot
216 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'
217 % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH),
218 shell=True)
219
220 # Sign image
221 # additional intermediate certificates may be included
222 # in SignedData
223
224 check_call('cp %s/lib/efi_loader/helloworld.efi %s' %
225 (u_boot_config.build_dir, mnt_point), shell=True)
226 # signed by TestCert
227 check_call('cd %s; %ssbsign --key TestCert.key --cert TestCert.crt --out helloworld.efi.signed_a helloworld.efi'
228 % (mnt_point, SBSIGN_PATH), shell=True)
229 # signed by TestCert with TestSub in signature
230 check_call('cd %s; %ssbsign --key TestCert.key --cert TestCert.crt --addcert TestSub.crt --out helloworld.efi.signed_ab helloworld.efi'
231 % (mnt_point, SBSIGN_PATH), shell=True)
232 # signed by TestCert with TestSub and TestRoot in signature
233 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'
234 % (mnt_point, SBSIGN_PATH), shell=True)
235
236 check_call('virt-make-fs --partition=gpt --size=+1M --type=vfat {} {}'.format(mnt_point, image_path), shell=True)
237 check_call('rm -rf {}'.format(mnt_point), shell=True)
238
239 except CalledProcessError as e:
240 pytest.skip('Setup failed: %s' % e.cmd)
241 return
242 else:
243 yield image_path
244 finally:
245 call('rm -f %s' % image_path, shell=True)