-
Notifications
You must be signed in to change notification settings - Fork 11
/
ReadVars.c
188 lines (166 loc) · 5.09 KB
/
ReadVars.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
* Copyright 2012 <[email protected]>
*
* see COPYING file
*
* Read and dump all the secure variables
*/
#include <efi.h>
#include <efilib.h>
#include <simple_file.h>
#include <guid.h>
#include <variables.h>
#include <shell.h>
#include <x509.h>
#include <sha256.h>
#include "efiauthenticated.h"
void
parse_db(UINT8 *data, UINTN len, EFI_HANDLE image, CHAR16 *name, int save_file)
{
EFI_SIGNATURE_LIST *CertList = (EFI_SIGNATURE_LIST *)data;
EFI_SIGNATURE_DATA *Cert;
UINTN count = 0, DataSize = len;
EFI_FILE *file;
CHAR16 *buf = AllocatePool(StrSize(name) + 4 + 2 + 4 + 8 +100);
CHAR16 *ext;
EFI_STATUS status;
int size;
certlist_for_each_certentry(CertList, data, size, DataSize) {
int Index = 0;
count++;
if (CompareGuid(&CertList->SignatureType, &X509_GUID) == 0) {
ext = L"X509";
} else if (CompareGuid(&CertList->SignatureType, &RSA2048_GUID) == 0) {
ext = L"RSA2048";
} else if (CompareGuid(&CertList->SignatureType, &PKCS7_GUID) == 0) {
ext = L"PKCS7";
} else if (CompareGuid(&CertList->SignatureType, &EFI_CERT_SHA256_GUID) == 0) {
ext = L"SHA256";
} else {
ext = L"Unknown";
}
Print(L"%s: List %d, type %s\n", name, count, ext);
certentry_for_each_cert(Cert, CertList) {
Print(L" Signature %d, size %d, owner %g\n",
Index++, CertList->SignatureSize,
&Cert->SignatureOwner);
if (StrCmp(ext, L"X509") == 0) {
CHAR16 buf1[4096];
x509_to_str(Cert->SignatureData,
CertList->SignatureSize,
X509_OBJ_SUBJECT, buf1,
sizeof(buf1));
Print(L" Subject: %s\n", buf1);
x509_to_str(Cert->SignatureData,
CertList->SignatureSize,
X509_OBJ_ISSUER, buf1,
sizeof(buf1));
Print(L" Issuer: %s\n", buf1);
} else if (StrCmp(ext, L"SHA256") == 0) {
CHAR16 buf1[256];
StrCpy(buf1, L"Hash: ");
sha256_StrCat_hash(buf1, Cert->SignatureData);
Print(L" %s\n", buf1);
}
if (save_file) {
SPrint(buf, 0, L"%s-%d-%d-%s-%g", name, count, Index, ext, &Cert->SignatureOwner);
Print(L"Writing to file %s\n", buf);
status = simple_file_open(image, buf, &file, EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE);
if (status != EFI_SUCCESS) {
Print(L"Failed to open file %s: %d\n", buf, status);
continue;
}
status = simple_file_write_all(file, CertList->SignatureSize-sizeof(EFI_GUID), Cert->SignatureData);
simple_file_close(file);
if (status != EFI_SUCCESS) {
Print(L"Failed to write signature to file %s: %d\n", buf, status);
continue;
}
}
}
}
FreePool(buf);
}
EFI_STATUS
efi_main (EFI_HANDLE image, EFI_SYSTEM_TABLE *systab)
{
EFI_STATUS status;
CHAR16 **variables;
EFI_GUID *owners;
CHAR16 **ARGV, *progname;
UINT8 *data;
UINTN len;
int i, argc, save_keys = 0, no_print = 0;
InitializeLib(image, systab);
if (GetOSIndications() & EFI_OS_INDICATIONS_TIMESTAMP_REVOCATION) {
variables = (CHAR16 *[]){ L"PK", L"KEK", L"db", L"dbx", L"dbt", L"MokList" , NULL};
owners = (EFI_GUID []){ GV_GUID, GV_GUID, SIG_DB, SIG_DB, SIG_DB, MOK_OWNER };
} else {
variables = (CHAR16 *[]){ L"PK", L"KEK", L"db", L"dbx", L"MokList" , NULL};
owners = (EFI_GUID []){ GV_GUID, GV_GUID, SIG_DB, SIG_DB, MOK_OWNER };
}
status = argsplit(image, &argc, &ARGV);
if (status != EFI_SUCCESS) {
Print(L"Failed to parse arguments: %d\n", status);
return status;
}
progname = ARGV[0];
while (argc > 1 && ARGV[1][0] == L'-') {
if (StrCmp(ARGV[1], L"-s") == 0) {
save_keys = 1;
ARGV += 1;
argc -= 1;
} else if (StrCmp(ARGV[1], L"-n") == 0) {
no_print = 1;
ARGV += 1;
argc -= 1;
} else {
/* unrecognised option */
break;
}
}
if ((argc != 2 && argc != 1) || (argc != 1 && no_print)) {
Print(L"Usage: %s: [-s|-n] [var]\n", progname);
return EFI_INVALID_PARAMETER;
}
if (argc == 1) {
for (i = 0; variables[i] != NULL; i++) {
status = get_variable(variables[i], &data, &len, owners[i]);
if (status == EFI_NOT_FOUND) {
Print(L"Variable %s has no entries\n", variables[i]);
} else if (status != EFI_SUCCESS) {
Print(L"Failed to get %s: %d\n", variables[i], status);
} else {
Print(L"Variable %s length %d\n", variables[i], len);
parse_db(data, len, image, variables[i], save_keys);
FreePool(data);
}
}
} else {
CHAR16 *var = ARGV[1];
for(i = 0; variables[i] != NULL; i++) {
if (StrCmp(var, variables[i]) == 0) {
break;
}
}
if (variables[i]== NULL) {
Print(L"Invalid Variable %s\nVariable must be one of: ", var);
for (i = 0; variables[i] != NULL; i++)
Print(L"%s ", variables[i]);
Print(L"\n");
return EFI_INVALID_PARAMETER;
}
status = get_variable(variables[i], &data, &len, owners[i]);
if (status == EFI_NOT_FOUND) {
Print(L"Variable %s has no entries\n", variables[i]);
} else if (status != EFI_SUCCESS) {
Print(L"Failed to get %s: %d\n", variables[i], status);
} else {
Print(L"Variable %s length %d\n", variables[i], len);
parse_db(data, len, image, variables[i], save_keys);
FreePool(data);
parse_db(data, len, image, variables[i], save_keys);
}
}
return EFI_SUCCESS;
}