-
Notifications
You must be signed in to change notification settings - Fork 11
/
LockDown.c
103 lines (88 loc) · 3.11 KB
/
LockDown.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
* Copyright 2012 <[email protected]>
*
* see COPYING file
*/
#include <efi.h>
#include <efilib.h>
#include <variables.h>
#include <guid.h>
#include "PK.h"
#include "KEK.h"
#include "DB.h"
EFI_STATUS
efi_main (EFI_HANDLE image, EFI_SYSTEM_TABLE *systab)
{
EFI_STATUS efi_status;
UINT8 SecureBoot, SetupMode;
UINTN DataSize = sizeof(SetupMode);
InitializeLib(image, systab);
efi_status = uefi_call_wrapper(RT->GetVariable, 5, L"SetupMode", &GV_GUID, NULL, &DataSize, &SetupMode);
if (efi_status != EFI_SUCCESS) {
Print(L"No SetupMode variable ... is platform secure boot enabled?\n");
return EFI_SUCCESS;
}
if (!SetupMode) {
Print(L"Platform is not in Setup Mode, cannot install Keys\n");
return EFI_SUCCESS;
}
Print(L"Platform is in Setup Mode\n");
efi_status = uefi_call_wrapper(RT->SetVariable, 5, L"KEK", &GV_GUID,
EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS,
KEK_auth_len, KEK_auth);
if (efi_status != EFI_SUCCESS) {
Print(L"Failed to enroll KEK: %d\n", efi_status);
return efi_status;
}
Print(L"Created KEK Cert\n");
efi_status = uefi_call_wrapper(RT->SetVariable, 5, L"db", &SIG_DB,
EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS,
DB_auth_len, DB_auth);
if (efi_status != EFI_SUCCESS) {
Print(L"Failed to enroll db: %d\n", efi_status);
return efi_status;
}
Print(L"Created db Cert\n");
#if 0
/* testing revocation ... this will revoke the certificate
* we just enrolled in db */
efi_status = SetSecureVariable(L"dbx", DB_cer, DB_cer_len, SIG_DB, 0);
if (efi_status != EFI_SUCCESS) {
Print(L"Failed to enroll dbx: %d\n", efi_status);
return efi_status;
}
#endif
/* PK must be updated with a signed copy of itself */
efi_status = uefi_call_wrapper(RT->SetVariable, 5, L"PK", &GV_GUID,
EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS,
PK_auth_len, PK_auth);
if (efi_status != EFI_SUCCESS) {
Print(L"Failed to enroll PK: %d\n", efi_status);
return efi_status;
}
Print(L"Created PK Cert\n");
/* enrolling the PK should put us in SetupMode; check this */
efi_status = uefi_call_wrapper(RT->GetVariable, 5, L"SetupMode", &GV_GUID, NULL, &DataSize, &SetupMode);
if (efi_status != EFI_SUCCESS) {
Print(L"Failed to get SetupMode variable: %d\n", efi_status);
return efi_status;
}
Print(L"Platform is in %s Mode\n", SetupMode ? L"Setup" : L"User");
/* finally, check that SecureBoot is enabled */
efi_status = uefi_call_wrapper(RT->GetVariable, 5, L"SecureBoot", &GV_GUID, NULL, &DataSize, &SecureBoot);
if (efi_status != EFI_SUCCESS) {
Print(L"Failed to get SecureBoot variable: %d\n", efi_status);
return efi_status;
}
Print(L"Platform %s set to boot securely\n", SecureBoot ? L"is" : L"is not");
return EFI_SUCCESS;
}