blob: c6709700a876398350b29c28002690dda47c4136 [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
AKASHI Takahiro58c95c22020-04-14 11:51:49 +090041
42 if HELLO_PATH == '':
43 HELLO_PATH = u_boot_config.build_dir + '/lib/efi_loader/helloworld.efi'
44
45 try:
Heinrich Schuchardt33424ea2020-07-11 23:05:18 +020046 mnt_point = u_boot_config.build_dir + '/mnt_efisecure'
47 check_call('rm -rf {}'.format(mnt_point), shell=True)
Heinrich Schuchardtb7e1d652020-04-21 18:44:39 +020048 check_call('mkdir -p {}'.format(mnt_point), shell=True)
AKASHI Takahiro58c95c22020-04-14 11:51:49 +090049
AKASHI Takahiro58c95c22020-04-14 11:51:49 +090050 # suffix
51 # *.key: RSA private key in PEM
52 # *.crt: X509 certificate (self-signed) in PEM
53 # *.esl: signature list
54 # *.hash: message digest of image as signature list
55 # *.auth: signed signature list in signature database format
56 # *.efi: UEFI image
57 # *.efi.signed: signed UEFI image
58
59 # Create signature database
AKASHI Takahiro719e89d2020-07-08 14:01:58 +090060 # PK
AKASHI Takahiro58c95c22020-04-14 11:51:49 +090061 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 +090062 % mnt_point, shell=True)
Heinrich Schuchardt446b1482020-07-02 08:25:46 +020063 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 +090064 % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH),
65 shell=True)
66 # PK_null for deletion
Heinrich Schuchardt446b1482020-07-02 08:25:46 +020067 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 +090068 % (mnt_point, EFITOOLS_PATH), shell=True)
69 # KEK
AKASHI Takahiro58c95c22020-04-14 11:51:49 +090070 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 +090071 % mnt_point, shell=True)
Heinrich Schuchardt446b1482020-07-02 08:25:46 +020072 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 +090073 % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH),
74 shell=True)
75 # db
AKASHI Takahiro58c95c22020-04-14 11:51:49 +090076 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 +090077 % mnt_point, shell=True)
Heinrich Schuchardt446b1482020-07-02 08:25:46 +020078 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 +090079 % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH),
80 shell=True)
81 # db1
AKASHI Takahiro58c95c22020-04-14 11:51:49 +090082 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 +090083 % mnt_point, shell=True)
Heinrich Schuchardt446b1482020-07-02 08:25:46 +020084 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 +090085 % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH),
86 shell=True)
87 # db1-update
Heinrich Schuchardt446b1482020-07-02 08:25:46 +020088 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 +090089 % (mnt_point, EFITOOLS_PATH), shell=True)
AKASHI Takahirod4ece532020-07-08 14:02:01 +090090 ## dbx (TEST_dbx certificate)
AKASHI Takahiro58c95c22020-04-14 11:51:49 +090091 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 +090092 % mnt_point, shell=True)
Heinrich Schuchardt446b1482020-07-02 08:25:46 +020093 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 +090094 % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH),
95 shell=True)
AKASHI Takahirod4ece532020-07-08 14:02:01 +090096 ## dbx_hash (digest of TEST_db certificate)
97 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'
98 % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH),
99 shell=True)
AKASHI Takahirodb20a3e2020-07-08 14:02:02 +0900100 ## dbx_hash1 (digest of TEST_db1 certificate)
101 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'
102 % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH),
103 shell=True)
AKASHI Takahiro0f6e3eb2020-07-08 14:02:03 +0900104 ## dbx_db (with TEST_db certificate)
105 check_call('cd %s; %ssign-efi-sig-list -t "2020-04-05" -c KEK.crt -k KEK.key dbx db.esl dbx_db.auth'
106 % (mnt_point, EFITOOLS_PATH),
107 shell=True)
AKASHI Takahiro58c95c22020-04-14 11:51:49 +0900108
109 # Copy image
110 check_call('cp %s %s' % (HELLO_PATH, mnt_point), shell=True)
111
AKASHI Takahiro719e89d2020-07-08 14:01:58 +0900112 # Sign image
AKASHI Takahiro58c95c22020-04-14 11:51:49 +0900113 check_call('cd %s; sbsign --key db.key --cert db.crt helloworld.efi'
AKASHI Takahiro719e89d2020-07-08 14:01:58 +0900114 % mnt_point, shell=True)
AKASHI Takahirodb20a3e2020-07-08 14:02:02 +0900115 ## Sign already-signed image with another key
116 check_call('cd %s; sbsign --key db1.key --cert db1.crt --output helloworld.efi.signed_2sigs helloworld.efi.signed'
117 % mnt_point, shell=True)
118 ## Digest image
Heinrich Schuchardt446b1482020-07-02 08:25:46 +0200119 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 +0900120 % (mnt_point, EFITOOLS_PATH, EFITOOLS_PATH),
121 shell=True)
AKASHI Takahiro0f6e3eb2020-07-08 14:02:03 +0900122 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'
123 % (mnt_point, EFITOOLS_PATH, EFITOOLS_PATH),
124 shell=True)
125 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'
126 % (mnt_point, EFITOOLS_PATH),
127 shell=True)
AKASHI Takahiro58c95c22020-04-14 11:51:49 +0900128
Heinrich Schuchardt33424ea2020-07-11 23:05:18 +0200129 check_call('virt-make-fs --partition=gpt --size=+1M --type=vfat {} {}'.format(mnt_point, image_path), shell=True)
130 check_call('rm -rf {}'.format(mnt_point), shell=True)
AKASHI Takahiro58c95c22020-04-14 11:51:49 +0900131
AKASHI Takahirobe5dbf02020-07-08 14:01:59 +0900132 except CalledProcessError as exception:
133 pytest.skip('Setup failed: %s' % exception.cmd)
AKASHI Takahiro58c95c22020-04-14 11:51:49 +0900134 return
135 else:
136 yield image_path
137 finally:
138 call('rm -f %s' % image_path, shell=True)