TianoCore EDK2 master
Loading...
Searching...
No Matches
AuthVariableLib.c
Go to the documentation of this file.
1
19#include "AuthServiceInternal.h"
20
25UINT32 mMaxCertDbSize;
26UINT32 mPlatformMode;
27UINT8 mVendorKeyState;
28
30
31//
32// Hash context pointer
33//
34VOID *mHashSha256Ctx = NULL;
35VOID *mHashSha384Ctx = NULL;
36VOID *mHashSha512Ctx = NULL;
37
38VARIABLE_ENTRY_PROPERTY mAuthVarEntry[] = {
39 {
40 &gEfiSecureBootEnableDisableGuid,
42 {
43 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
44 0,
46 sizeof (UINT8),
47 sizeof (UINT8)
48 }
49 },
50 {
51 &gEfiCustomModeEnableGuid,
53 {
54 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
55 0,
57 sizeof (UINT8),
58 sizeof (UINT8)
59 }
60 },
61 {
62 &gEfiVendorKeysNvGuid,
64 {
65 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
66 VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY,
67 VARIABLE_ATTRIBUTE_NV_BS_RT_AT,
68 sizeof (UINT8),
69 sizeof (UINT8)
70 }
71 },
72 {
73 &gEfiCertDbGuid,
75 {
76 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
77 VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY,
78 VARIABLE_ATTRIBUTE_NV_BS_RT_AT,
79 sizeof (UINT32),
80 MAX_UINTN
81 }
82 },
83 {
84 &gEfiCertDbGuid,
85 EFI_CERT_DB_VOLATILE_NAME,
86 {
87 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
88 VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY,
89 VARIABLE_ATTRIBUTE_BS_RT_AT,
90 sizeof (UINT32),
91 MAX_UINTN
92 }
93 },
94};
95
96VOID **mAuthVarAddressPointer[11];
97
98AUTH_VAR_LIB_CONTEXT_IN *mAuthVarLibContextIn = NULL;
99
115EFIAPI
117 IN AUTH_VAR_LIB_CONTEXT_IN *AuthVarLibContextIn,
118 OUT AUTH_VAR_LIB_CONTEXT_OUT *AuthVarLibContextOut
119 )
120{
121 EFI_STATUS Status;
122 UINT32 VarAttr;
123 UINT8 *Data;
124 UINTN DataSize;
125 UINT8 SecureBootMode;
126 UINT8 SecureBootEnable;
127 UINT8 CustomMode;
128 UINT32 ListSize;
129
130 if ((AuthVarLibContextIn == NULL) || (AuthVarLibContextOut == NULL)) {
131 return EFI_INVALID_PARAMETER;
132 }
133
134 mAuthVarLibContextIn = AuthVarLibContextIn;
135
136 //
137 // Initialize hash context.
138 //
139 mHashSha256Ctx = AllocateRuntimePool (Sha256GetContextSize ());
140 if (mHashSha256Ctx == NULL) {
141 return EFI_OUT_OF_RESOURCES;
142 }
143
144 mHashSha384Ctx = AllocateRuntimePool (Sha384GetContextSize ());
145 if (mHashSha384Ctx == NULL) {
146 return EFI_OUT_OF_RESOURCES;
147 }
148
149 mHashSha512Ctx = AllocateRuntimePool (Sha512GetContextSize ());
150 if (mHashSha512Ctx == NULL) {
151 return EFI_OUT_OF_RESOURCES;
152 }
153
154 //
155 // Reserve runtime buffer for certificate database. The size excludes variable header and name size.
156 // Use EFI_CERT_DB_VOLATILE_NAME size since it is longer.
157 //
158 mMaxCertDbSize = (UINT32)(mAuthVarLibContextIn->MaxAuthVariableSize - sizeof (EFI_CERT_DB_VOLATILE_NAME));
159 mCertDbStore = AllocateRuntimePool (mMaxCertDbSize);
160 if (mCertDbStore == NULL) {
161 return EFI_OUT_OF_RESOURCES;
162 }
163
164 Status = AuthServiceInternalFindVariable (EFI_PLATFORM_KEY_NAME, &gEfiGlobalVariableGuid, (VOID **)&Data, &DataSize);
165 if (EFI_ERROR (Status)) {
166 DEBUG ((DEBUG_INFO, "Variable %s does not exist.\n", EFI_PLATFORM_KEY_NAME));
167 } else {
168 DEBUG ((DEBUG_INFO, "Variable %s exists.\n", EFI_PLATFORM_KEY_NAME));
169 }
170
171 //
172 // Create "SetupMode" variable with BS+RT attribute set.
173 //
174 if (EFI_ERROR (Status)) {
175 mPlatformMode = SETUP_MODE;
176 } else {
177 mPlatformMode = USER_MODE;
178 }
179
182 &gEfiGlobalVariableGuid,
183 &mPlatformMode,
184 sizeof (UINT8),
185 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS
186 );
187 if (EFI_ERROR (Status)) {
188 return Status;
189 }
190
191 //
192 // Create "SignatureSupport" variable with BS+RT attribute set.
193 //
196 &gEfiGlobalVariableGuid,
197 mSignatureSupport,
198 sizeof (mSignatureSupport),
199 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS
200 );
201 if (EFI_ERROR (Status)) {
202 return Status;
203 }
204
205 //
206 // If "SecureBootEnable" variable exists, then update "SecureBoot" variable.
207 // If "SecureBootEnable" variable is SECURE_BOOT_ENABLE and in USER_MODE, Set "SecureBoot" variable to SECURE_BOOT_MODE_ENABLE.
208 // If "SecureBootEnable" variable is SECURE_BOOT_DISABLE, Set "SecureBoot" variable to SECURE_BOOT_MODE_DISABLE.
209 //
210 SecureBootEnable = SECURE_BOOT_DISABLE;
211 Status = AuthServiceInternalFindVariable (EFI_SECURE_BOOT_ENABLE_NAME, &gEfiSecureBootEnableDisableGuid, (VOID **)&Data, &DataSize);
212 if (!EFI_ERROR (Status)) {
213 if (mPlatformMode == USER_MODE) {
214 SecureBootEnable = *(UINT8 *)Data;
215 }
216 } else if (mPlatformMode == USER_MODE) {
217 //
218 // "SecureBootEnable" not exist, initialize it in USER_MODE.
219 //
220 SecureBootEnable = SECURE_BOOT_ENABLE;
223 &gEfiSecureBootEnableDisableGuid,
224 &SecureBootEnable,
225 sizeof (UINT8),
226 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS
227 );
228 if (EFI_ERROR (Status)) {
229 return Status;
230 }
231 }
232
233 //
234 // Create "SecureBoot" variable with BS+RT attribute set.
235 //
236 if ((SecureBootEnable == SECURE_BOOT_ENABLE) && (mPlatformMode == USER_MODE)) {
237 SecureBootMode = SECURE_BOOT_MODE_ENABLE;
238 } else {
239 SecureBootMode = SECURE_BOOT_MODE_DISABLE;
240 }
241
244 &gEfiGlobalVariableGuid,
245 &SecureBootMode,
246 sizeof (UINT8),
247 EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS
248 );
249 if (EFI_ERROR (Status)) {
250 return Status;
251 }
252
253 DEBUG ((DEBUG_INFO, "Variable %s is %x\n", EFI_SETUP_MODE_NAME, mPlatformMode));
254 DEBUG ((DEBUG_INFO, "Variable %s is %x\n", EFI_SECURE_BOOT_MODE_NAME, SecureBootMode));
255 DEBUG ((DEBUG_INFO, "Variable %s is %x\n", EFI_SECURE_BOOT_ENABLE_NAME, SecureBootEnable));
256
257 //
258 // Initialize "CustomMode" in STANDARD_SECURE_BOOT_MODE state.
259 //
260 CustomMode = STANDARD_SECURE_BOOT_MODE;
263 &gEfiCustomModeEnableGuid,
264 &CustomMode,
265 sizeof (UINT8),
266 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS
267 );
268 if (EFI_ERROR (Status)) {
269 return Status;
270 }
271
272 DEBUG ((DEBUG_INFO, "Variable %s is %x\n", EFI_CUSTOM_MODE_NAME, CustomMode));
273
274 //
275 // Check "certdb" variable's existence.
276 // If it doesn't exist, then create a new one with
277 // EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set.
278 //
281 &gEfiCertDbGuid,
282 (VOID **)&Data,
283 &DataSize
284 );
285 if (EFI_ERROR (Status)) {
286 VarAttr = EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS;
287 ListSize = sizeof (UINT32);
290 &gEfiCertDbGuid,
291 &ListSize,
292 sizeof (UINT32),
293 VarAttr
294 );
295 if (EFI_ERROR (Status)) {
296 return Status;
297 }
298 } else {
299 //
300 // Clean up Certs to make certDB & Time based auth variable consistent
301 //
302 Status = CleanCertsFromDb ();
303 if (EFI_ERROR (Status)) {
304 DEBUG ((DEBUG_ERROR, "Clean up CertDB fail! Status %x\n", Status));
305 return Status;
306 }
307 }
308
309 //
310 // Create "certdbv" variable with RT+BS+AT set.
311 //
312 VarAttr = EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS;
313 ListSize = sizeof (UINT32);
315 EFI_CERT_DB_VOLATILE_NAME,
316 &gEfiCertDbGuid,
317 &ListSize,
318 sizeof (UINT32),
319 VarAttr
320 );
321 if (EFI_ERROR (Status)) {
322 return Status;
323 }
324
325 //
326 // Check "VendorKeysNv" variable's existence and create "VendorKeys" variable accordingly.
327 //
328 Status = AuthServiceInternalFindVariable (EFI_VENDOR_KEYS_NV_VARIABLE_NAME, &gEfiVendorKeysNvGuid, (VOID **)&Data, &DataSize);
329 if (!EFI_ERROR (Status)) {
330 mVendorKeyState = *(UINT8 *)Data;
331 } else {
332 //
333 // "VendorKeysNv" not exist, initialize it in VENDOR_KEYS_VALID state.
334 //
335 mVendorKeyState = VENDOR_KEYS_VALID;
338 &gEfiVendorKeysNvGuid,
339 &mVendorKeyState,
340 sizeof (UINT8),
342 );
343 if (EFI_ERROR (Status)) {
344 return Status;
345 }
346 }
347
348 //
349 // Create "VendorKeys" variable with BS+RT attribute set.
350 //
353 &gEfiGlobalVariableGuid,
354 &mVendorKeyState,
355 sizeof (UINT8),
356 EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS
357 );
358 if (EFI_ERROR (Status)) {
359 return Status;
360 }
361
362 DEBUG ((DEBUG_INFO, "Variable %s is %x\n", EFI_VENDOR_KEYS_VARIABLE_NAME, mVendorKeyState));
363
364 AuthVarLibContextOut->StructVersion = AUTH_VAR_LIB_CONTEXT_OUT_STRUCT_VERSION;
365 AuthVarLibContextOut->StructSize = sizeof (AUTH_VAR_LIB_CONTEXT_OUT);
366 AuthVarLibContextOut->AuthVarEntry = mAuthVarEntry;
367 AuthVarLibContextOut->AuthVarEntryCount = ARRAY_SIZE (mAuthVarEntry);
368 mAuthVarAddressPointer[0] = (VOID **)&mCertDbStore;
369 mAuthVarAddressPointer[1] = (VOID **)&mHashSha256Ctx;
370 mAuthVarAddressPointer[2] = (VOID **)&mHashSha384Ctx;
371 mAuthVarAddressPointer[3] = (VOID **)&mHashSha512Ctx;
372 mAuthVarAddressPointer[4] = (VOID **)&mAuthVarLibContextIn;
373 mAuthVarAddressPointer[5] = (VOID **)&(mAuthVarLibContextIn->FindVariable),
374 mAuthVarAddressPointer[6] = (VOID **)&(mAuthVarLibContextIn->FindNextVariable),
375 mAuthVarAddressPointer[7] = (VOID **)&(mAuthVarLibContextIn->UpdateVariable),
376 mAuthVarAddressPointer[8] = (VOID **)&(mAuthVarLibContextIn->GetScratchBuffer),
377 mAuthVarAddressPointer[9] = (VOID **)&(mAuthVarLibContextIn->CheckRemainingSpaceForConsistency),
378 mAuthVarAddressPointer[10] = (VOID **)&(mAuthVarLibContextIn->AtRuntime),
379 AuthVarLibContextOut->AddressPointer = mAuthVarAddressPointer;
380 AuthVarLibContextOut->AddressPointerCount = ARRAY_SIZE (mAuthVarAddressPointer);
381
382 return Status;
383}
384
406EFIAPI
408 IN CHAR16 *VariableName,
409 IN EFI_GUID *VendorGuid,
410 IN VOID *Data,
411 IN UINTN DataSize,
412 IN UINT32 Attributes
413 )
414{
415 EFI_STATUS Status;
416
417 if (CompareGuid (VendorGuid, &gEfiGlobalVariableGuid) && (StrCmp (VariableName, EFI_PLATFORM_KEY_NAME) == 0)) {
418 Status = ProcessVarWithPk (VariableName, VendorGuid, Data, DataSize, Attributes, TRUE);
419 } else if (CompareGuid (VendorGuid, &gEfiGlobalVariableGuid) && (StrCmp (VariableName, EFI_KEY_EXCHANGE_KEY_NAME) == 0)) {
420 Status = ProcessVarWithPk (VariableName, VendorGuid, Data, DataSize, Attributes, FALSE);
421 } else if (CompareGuid (VendorGuid, &gEfiImageSecurityDatabaseGuid) &&
422 ((StrCmp (VariableName, EFI_IMAGE_SECURITY_DATABASE) == 0) ||
423 (StrCmp (VariableName, EFI_IMAGE_SECURITY_DATABASE1) == 0) ||
424 (StrCmp (VariableName, EFI_IMAGE_SECURITY_DATABASE2) == 0)
425 ))
426 {
427 Status = ProcessVarWithPk (VariableName, VendorGuid, Data, DataSize, Attributes, FALSE);
428 if (EFI_ERROR (Status)) {
429 Status = ProcessVarWithKek (VariableName, VendorGuid, Data, DataSize, Attributes);
430 }
431 } else {
432 Status = ProcessVariable (VariableName, VendorGuid, Data, DataSize, Attributes);
433 }
434
435 return Status;
436}
UINT64 UINTN
EFI_STATUS AuthServiceInternalUpdateVariable(IN CHAR16 *VariableName, IN EFI_GUID *VendorGuid, IN VOID *Data, IN UINTN DataSize, IN UINT32 Attributes)
Definition: AuthService.c:228
EFI_STATUS ProcessVarWithKek(IN CHAR16 *VariableName, IN EFI_GUID *VendorGuid, IN VOID *Data, IN UINTN DataSize, IN UINT32 Attributes OPTIONAL)
Definition: AuthService.c:820
EFI_STATUS AuthServiceInternalFindVariable(IN CHAR16 *VariableName, IN EFI_GUID *VendorGuid, OUT VOID **Data, OUT UINTN *DataSize)
Definition: AuthService.c:191
EFI_STATUS ProcessVarWithPk(IN CHAR16 *VariableName, IN EFI_GUID *VendorGuid, IN VOID *Data, IN UINTN DataSize, IN UINT32 Attributes OPTIONAL, IN BOOLEAN IsPk)
Definition: AuthService.c:691
EFI_STATUS ProcessVariable(IN CHAR16 *VariableName, IN EFI_GUID *VendorGuid, IN VOID *Data, IN UINTN DataSize, IN UINT32 Attributes)
Definition: AuthService.c:965
EFI_STATUS CleanCertsFromDb(VOID)
Definition: AuthService.c:1825
#define EFI_CERT_DB_NAME
EFI_STATUS EFIAPI AuthVariableLibInitialize(IN AUTH_VAR_LIB_CONTEXT_IN *AuthVarLibContextIn, OUT AUTH_VAR_LIB_CONTEXT_OUT *AuthVarLibContextOut)
EFI_STATUS EFIAPI AuthVariableLibProcessVariable(IN CHAR16 *VariableName, IN EFI_GUID *VendorGuid, IN VOID *Data, IN UINTN DataSize, IN UINT32 Attributes)
UINT8 * mCertDbStore
#define EFI_CUSTOM_MODE_NAME
#define EFI_VENDOR_KEYS_NV_VARIABLE_NAME
#define EFI_SECURE_BOOT_ENABLE_NAME
UINTN EFIAPI Sha256GetContextSize(VOID)
Definition: CryptSha256.c:20
UINTN EFIAPI Sha384GetContextSize(VOID)
Definition: CryptSha512.c:20
UINTN EFIAPI Sha512GetContextSize(VOID)
Definition: CryptSha512.c:246
INTN EFIAPI StrCmp(IN CONST CHAR16 *FirstString, IN CONST CHAR16 *SecondString)
Definition: String.c:109
BOOLEAN EFIAPI CompareGuid(IN CONST GUID *Guid1, IN CONST GUID *Guid2)
Definition: MemLibGuid.c:73
VOID *EFIAPI AllocateRuntimePool(IN UINTN AllocationSize)
#define EFI_SETUP_MODE_NAME
#define EFI_KEY_EXCHANGE_KEY_NAME
#define EFI_SIGNATURE_SUPPORT_NAME
#define EFI_PLATFORM_KEY_NAME
#define EFI_VENDOR_KEYS_VARIABLE_NAME
#define EFI_SECURE_BOOT_MODE_NAME
#define EFI_CERT_SHA512_GUID
#define EFI_CERT_SHA384_GUID
#define EFI_CERT_RSA2048_GUID
#define EFI_IMAGE_SECURITY_DATABASE2
#define EFI_CERT_SHA256_GUID
#define EFI_IMAGE_SECURITY_DATABASE1
#define EFI_IMAGE_SECURITY_DATABASE
#define EFI_CERT_X509_GUID
#define EFI_CERT_SHA1_GUID
#define NULL
Definition: Base.h:319
#define TRUE
Definition: Base.h:301
#define FALSE
Definition: Base.h:307
#define ARRAY_SIZE(Array)
Definition: Base.h:1393
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
#define DEBUG(Expression)
Definition: DebugLib.h:434
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
#define EFI_VARIABLE_NON_VOLATILE
#define EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
#define VARIABLE_ATTRIBUTE_NV_BS
Definition: Base.h:213