test: stabilize test_efi_secboot
[oweals/u-boot.git] / test / py / tests / test_efi_secboot / test_signed.py
1 # SPDX-License-Identifier:      GPL-2.0+
2 # Copyright (c) 2019, Linaro Limited
3 # Author: AKASHI Takahiro <takahiro.akashi@linaro.org>
4 #
5 # U-Boot UEFI: Signed Image Authentication Test
6
7 """
8 This test verifies image authentication for signed images.
9 """
10
11 import pytest
12 import re
13 from defs import *
14
15 @pytest.mark.boardspec('sandbox')
16 @pytest.mark.buildconfigspec('efi_secure_boot')
17 @pytest.mark.buildconfigspec('cmd_efidebug')
18 @pytest.mark.buildconfigspec('cmd_fat')
19 @pytest.mark.buildconfigspec('cmd_nvedit_efi')
20 @pytest.mark.slow
21 class TestEfiSignedImage(object):
22     def test_efi_signed_image_auth1(self, u_boot_console, efi_boot_env):
23         """
24         Test Case 1 - authenticated by db
25         """
26         u_boot_console.restart_uboot()
27         disk_img = efi_boot_env
28         with u_boot_console.log.section('Test Case 1a'):
29             # Test Case 1a, run signed image if no db/dbx
30             output = u_boot_console.run_command_list([
31                 'host bind 0 %s' % disk_img,
32                 'efidebug boot add 1 HELLO1 host 0:1 /helloworld.efi.signed ""; echo',
33                 'efidebug boot next 1',
34                 'bootefi bootmgr'])
35             assert(re.search('Hello, world!', ''.join(output)))
36
37         with u_boot_console.log.section('Test Case 1b'):
38             # Test Case 1b, run unsigned image if no db/dbx
39             output = u_boot_console.run_command_list([
40                 'efidebug boot add 2 HELLO2 host 0:1 /helloworld.efi ""',
41                 'efidebug boot next 2',
42                 'bootefi bootmgr'])
43             assert(re.search('Hello, world!', ''.join(output)))
44
45         with u_boot_console.log.section('Test Case 1c'):
46             # Test Case 1c, not authenticated by db
47             output = u_boot_console.run_command_list([
48                 'fatload host 0:1 4000000 db.auth',
49                 'setenv -e -nv -bs -rt -at -i 4000000,$filesize db',
50                 'fatload host 0:1 4000000 KEK.auth',
51                 'setenv -e -nv -bs -rt -at -i 4000000,$filesize KEK',
52                 'fatload host 0:1 4000000 PK.auth',
53                 'setenv -e -nv -bs -rt -at -i 4000000,$filesize PK'])
54             assert(not re.search('Failed to set EFI variable', ''.join(output)))
55             output = u_boot_console.run_command_list([
56                 'efidebug boot next 2',
57                 'bootefi bootmgr'])
58             assert(re.search('\'HELLO2\' failed', ''.join(output)))
59             output = u_boot_console.run_command_list([
60                 'efidebug boot next 2',
61                 'efidebug test bootmgr'])
62             assert(re.search('efi_start_image[(][)] returned: 26',
63                 ''.join(output)))
64             assert(not re.search('Hello, world!', ''.join(output)))
65
66         with u_boot_console.log.section('Test Case 1d'):
67             # Test Case 1d, authenticated by db
68             output = u_boot_console.run_command_list([
69                 'efidebug boot next 1',
70                 'bootefi bootmgr'])
71             assert(re.search('Hello, world!', ''.join(output)))
72
73     def test_efi_signed_image_auth2(self, u_boot_console, efi_boot_env):
74         """
75         Test Case 2 - rejected by dbx
76         """
77         u_boot_console.restart_uboot()
78         disk_img = efi_boot_env
79         with u_boot_console.log.section('Test Case 2a'):
80             # Test Case 2a, rejected by dbx
81             output = u_boot_console.run_command_list([
82                 'host bind 0 %s' % disk_img,
83                 'fatload host 0:1 4000000 db.auth',
84                 'setenv -e -nv -bs -rt -at -i 4000000,$filesize dbx; echo',
85                 'fatload host 0:1 4000000 KEK.auth',
86                 'setenv -e -nv -bs -rt -at -i 4000000,$filesize KEK',
87                 'fatload host 0:1 4000000 PK.auth',
88                 'setenv -e -nv -bs -rt -at -i 4000000,$filesize PK'])
89             assert(not re.search('Failed to set EFI variable', ''.join(output)))
90             output = u_boot_console.run_command_list([
91                 'efidebug boot add 1 HELLO host 0:1 /helloworld.efi.signed ""',
92                 'efidebug boot next 1',
93                 'bootefi bootmgr'])
94             assert(re.search('\'HELLO\' failed', ''.join(output)))
95             output = u_boot_console.run_command_list([
96                 'efidebug boot next 1',
97                 'efidebug test bootmgr'])
98             assert(re.search('efi_start_image[(][)] returned: 26',
99                 ''.join(output)))
100             assert(not re.search('Hello, world!', ''.join(output)))
101
102         with u_boot_console.log.section('Test Case 2b'):
103             # Test Case 2b, rejected by dbx even if db allows
104             output = u_boot_console.run_command_list([
105                 'fatload host 0:1 4000000 db.auth',
106                 'setenv -e -nv -bs -rt -at -i 4000000,$filesize db'])
107             assert(not re.search('Failed to set EFI variable', ''.join(output)))
108             output = u_boot_console.run_command_list([
109                 'efidebug boot next 1',
110                 'bootefi bootmgr'])
111             assert(re.search('\'HELLO\' failed', ''.join(output)))
112             output = u_boot_console.run_command_list([
113                 'efidebug boot next 1',
114                 'efidebug test bootmgr'])
115             assert(re.search('efi_start_image[(][)] returned: 26',
116                 ''.join(output)))
117             assert(not re.search('Hello, world!', ''.join(output)))