blob: c0943b62501dd2e9453d286fabc699677dd00553 [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 """
28 global HELLO_PATH
29
30 image_path = u_boot_config.persistent_data_dir
31 image_path = image_path + '/' + EFI_SECBOOT_IMAGE_NAME
AKASHI Takahiro58c95c22020-04-14 11:51:49 +090032
33 if HELLO_PATH == '':
34 HELLO_PATH = u_boot_config.build_dir + '/lib/efi_loader/helloworld.efi'
35
36 try:
Heinrich Schuchardt33424ea2020-07-11 23:05:18 +020037 mnt_point = u_boot_config.build_dir + '/mnt_efisecure'
38 check_call('rm -rf {}'.format(mnt_point), shell=True)
Heinrich Schuchardtb7e1d652020-04-21 18:44:39 +020039 check_call('mkdir -p {}'.format(mnt_point), shell=True)
AKASHI Takahiro58c95c22020-04-14 11:51:49 +090040
AKASHI Takahiro58c95c22020-04-14 11:51:49 +090041 # suffix
42 # *.key: RSA private key in PEM
43 # *.crt: X509 certificate (self-signed) in PEM
44 # *.esl: signature list
45 # *.hash: message digest of image as signature list
46 # *.auth: signed signature list in signature database format
47 # *.efi: UEFI image
48 # *.efi.signed: signed UEFI image
49
50 # Create signature database
AKASHI Takahiro719e89d2020-07-08 14:01:58 +090051 # PK
AKASHI Takahiro58c95c22020-04-14 11:51:49 +090052 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 +090053 % mnt_point, shell=True)
Heinrich Schuchardt446b1482020-07-02 08:25:46 +020054 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 +090055 % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH),
56 shell=True)
57 # PK_null for deletion
Heinrich Schuchardt446b1482020-07-02 08:25:46 +020058 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 +090059 % (mnt_point, EFITOOLS_PATH), shell=True)
60 # KEK
AKASHI Takahiro58c95c22020-04-14 11:51:49 +090061 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 +090062 % mnt_point, shell=True)
Heinrich Schuchardt446b1482020-07-02 08:25:46 +020063 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 +090064 % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH),
65 shell=True)
66 # db
AKASHI Takahiro58c95c22020-04-14 11:51:49 +090067 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 +090068 % mnt_point, shell=True)
Heinrich Schuchardt446b1482020-07-02 08:25:46 +020069 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 +090070 % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH),
71 shell=True)
72 # db1
AKASHI Takahiro58c95c22020-04-14 11:51:49 +090073 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 +090074 % mnt_point, shell=True)
Heinrich Schuchardt446b1482020-07-02 08:25:46 +020075 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 +090076 % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH),
77 shell=True)
78 # db1-update
Heinrich Schuchardt446b1482020-07-02 08:25:46 +020079 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 +090080 % (mnt_point, EFITOOLS_PATH), shell=True)
AKASHI Takahiro798f3062020-07-20 15:33:39 +090081 # dbx (TEST_dbx certificate)
AKASHI Takahiro58c95c22020-04-14 11:51:49 +090082 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 +090083 % mnt_point, shell=True)
Heinrich Schuchardt446b1482020-07-02 08:25:46 +020084 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 +090085 % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH),
86 shell=True)
AKASHI Takahiro798f3062020-07-20 15:33:39 +090087 # dbx_hash (digest of TEST_db certificate)
AKASHI Takahirod4ece532020-07-08 14:02:01 +090088 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'
89 % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH),
90 shell=True)
AKASHI Takahiro798f3062020-07-20 15:33:39 +090091 # dbx_hash1 (digest of TEST_db1 certificate)
AKASHI Takahirodb20a3e2020-07-08 14:02:02 +090092 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'
93 % (mnt_point, EFITOOLS_PATH, GUID, EFITOOLS_PATH),
94 shell=True)
AKASHI Takahiro798f3062020-07-20 15:33:39 +090095 # dbx_db (with TEST_db certificate)
AKASHI Takahiro0f6e3eb2020-07-08 14:02:03 +090096 check_call('cd %s; %ssign-efi-sig-list -t "2020-04-05" -c KEK.crt -k KEK.key dbx db.esl dbx_db.auth'
97 % (mnt_point, EFITOOLS_PATH),
98 shell=True)
AKASHI Takahiro58c95c22020-04-14 11:51:49 +090099
100 # Copy image
101 check_call('cp %s %s' % (HELLO_PATH, mnt_point), shell=True)
102
AKASHI Takahiro719e89d2020-07-08 14:01:58 +0900103 # Sign image
AKASHI Takahiro58c95c22020-04-14 11:51:49 +0900104 check_call('cd %s; sbsign --key db.key --cert db.crt helloworld.efi'
AKASHI Takahiro719e89d2020-07-08 14:01:58 +0900105 % mnt_point, shell=True)
AKASHI Takahiro798f3062020-07-20 15:33:39 +0900106 # Sign already-signed image with another key
AKASHI Takahirodb20a3e2020-07-08 14:02:02 +0900107 check_call('cd %s; sbsign --key db1.key --cert db1.crt --output helloworld.efi.signed_2sigs helloworld.efi.signed'
108 % mnt_point, shell=True)
AKASHI Takahiro798f3062020-07-20 15:33:39 +0900109 # Digest image
Heinrich Schuchardt446b1482020-07-02 08:25:46 +0200110 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 +0900111 % (mnt_point, EFITOOLS_PATH, EFITOOLS_PATH),
112 shell=True)
AKASHI Takahiro0f6e3eb2020-07-08 14:02:03 +0900113 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'
114 % (mnt_point, EFITOOLS_PATH, EFITOOLS_PATH),
115 shell=True)
116 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'
117 % (mnt_point, EFITOOLS_PATH),
118 shell=True)
AKASHI Takahiro58c95c22020-04-14 11:51:49 +0900119
AKASHI Takahiro798f3062020-07-20 15:33:39 +0900120 check_call('virt-make-fs --partition=gpt --size=+1M --type=vfat {} {}'.format(
121 mnt_point, image_path), shell=True)
Heinrich Schuchardt33424ea2020-07-11 23:05:18 +0200122 check_call('rm -rf {}'.format(mnt_point), shell=True)
AKASHI Takahiro58c95c22020-04-14 11:51:49 +0900123
AKASHI Takahirobe5dbf02020-07-08 14:01:59 +0900124 except CalledProcessError as exception:
125 pytest.skip('Setup failed: %s' % exception.cmd)
AKASHI Takahiro58c95c22020-04-14 11:51:49 +0900126 return
127 else:
128 yield image_path
129 finally:
130 call('rm -f %s' % image_path, shell=True)