2 This library provides functions to set/clear Secure Boot
5 Copyright (c) 2011 - 2018, Intel Corporation. All rights reserved.<BR>
6 (C) Copyright 2018 Hewlett Packard Enterprise Development LP<BR>
7 Copyright (c) 2021, ARM Ltd. All rights reserved.<BR>
8 Copyright (c) 2021, Semihalf All rights reserved.<BR>
9 SPDX-License-Identifier: BSD-2-Clause-Patent
11 #include <Guid/GlobalVariable.h>
12 #include <Guid/AuthenticatedVariableFormat.h>
13 #include <Guid/ImageAuthentication.h>
14 #include <Library/BaseLib.h>
15 #include <Library/BaseMemoryLib.h>
16 #include <Library/DebugLib.h>
17 #include <Library/UefiLib.h>
18 #include <Library/MemoryAllocationLib.h>
19 #include <Library/UefiRuntimeServicesTableLib.h>
20 #include <Library/SecureBootVariableLib.h>
21 #include <Library/SecureBootVariableProvisionLib.h>
24 Enroll a key/certificate based on a default variable.
26 @param[in] VariableName The name of the key/database.
27 @param[in] DefaultName The name of the default variable.
28 @param[in] VendorGuid The namespace (ie. vendor GUID) of the variable
30 @retval EFI_OUT_OF_RESOURCES Out of memory while allocating AuthHeader.
31 @retval EFI_SUCCESS Successful enrollment.
32 @return Error codes from GetTime () and SetVariable ().
37 IN CHAR16
*VariableName
,
38 IN CHAR16
*DefaultName
,
39 IN EFI_GUID
*VendorGuid
49 Status
= GetVariable2 (DefaultName
, &gEfiGlobalVariableGuid
, &Data
, &DataSize
);
50 if (EFI_ERROR (Status
)) {
51 DEBUG ((DEBUG_ERROR
, "error: GetVariable (\"%s): %r\n", DefaultName
, Status
));
55 CreateTimeBasedPayload (&DataSize
, (UINT8
**)&Data
);
56 if (EFI_ERROR (Status
)) {
57 DEBUG ((DEBUG_ERROR
, "Fail to create time-based data payload: %r", Status
));
62 // Allocate memory for auth variable
64 Status
= gRT
->SetVariable (
67 (EFI_VARIABLE_NON_VOLATILE
|
68 EFI_VARIABLE_BOOTSERVICE_ACCESS
|
69 EFI_VARIABLE_RUNTIME_ACCESS
|
70 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
),
75 if (EFI_ERROR (Status
)) {
76 DEBUG ((DEBUG_ERROR
, "error: %a (\"%s\", %g): %r\n", __FUNCTION__
, VariableName
,
87 /** Initializes PKDefault variable with data from FFS section.
89 @retval EFI_SUCCESS Variable was initialized successfully.
90 @retval EFI_UNSUPPORTED Variable already exists.
93 SecureBootInitPKDefault (
97 EFI_SIGNATURE_LIST
*EfiSig
;
104 // Check if variable exists, if so do not change it
106 Status
= GetVariable2 (EFI_PK_DEFAULT_VARIABLE_NAME
, &gEfiGlobalVariableGuid
, (VOID
**) &Data
, &DataSize
);
107 if (Status
== EFI_SUCCESS
) {
108 DEBUG ((DEBUG_INFO
, "Variable %s exists. Old value is preserved\n", EFI_PK_DEFAULT_VARIABLE_NAME
));
110 return EFI_UNSUPPORTED
;
113 if (EFI_ERROR (Status
) && (Status
!= EFI_NOT_FOUND
)) {
118 // Variable does not exist, can be initialized
120 DEBUG ((DEBUG_INFO
, "Variable %s does not exist.\n", EFI_PK_DEFAULT_VARIABLE_NAME
));
122 Status
= SecureBootFetchData (&gDefaultPKFileGuid
, &SigListsSize
, &EfiSig
);
123 if (EFI_ERROR (Status
)) {
124 DEBUG ((DEBUG_INFO
, "Content for %s not found\n", EFI_PK_DEFAULT_VARIABLE_NAME
));
128 Status
= gRT
->SetVariable (
129 EFI_PK_DEFAULT_VARIABLE_NAME
,
130 &gEfiGlobalVariableGuid
,
131 EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
135 if (EFI_ERROR (Status
)) {
136 DEBUG ((DEBUG_INFO
, "Failed to set %s\n", EFI_PK_DEFAULT_VARIABLE_NAME
));
144 /** Initializes KEKDefault variable with data from FFS section.
146 @retval EFI_SUCCESS Variable was initialized successfully.
147 @retval EFI_UNSUPPORTED Variable already exists.
150 SecureBootInitKEKDefault (
154 EFI_SIGNATURE_LIST
*EfiSig
;
161 // Check if variable exists, if so do not change it
163 Status
= GetVariable2 (EFI_KEK_DEFAULT_VARIABLE_NAME
, &gEfiGlobalVariableGuid
, (VOID
**) &Data
, &DataSize
);
164 if (Status
== EFI_SUCCESS
) {
165 DEBUG ((DEBUG_INFO
, "Variable %s exists. Old value is preserved\n", EFI_KEK_DEFAULT_VARIABLE_NAME
));
167 return EFI_UNSUPPORTED
;
170 if (EFI_ERROR (Status
) && (Status
!= EFI_NOT_FOUND
)) {
175 // Variable does not exist, can be initialized
177 DEBUG ((DEBUG_INFO
, "Variable %s does not exist.\n", EFI_KEK_DEFAULT_VARIABLE_NAME
));
179 Status
= SecureBootFetchData (&gDefaultKEKFileGuid
, &SigListsSize
, &EfiSig
);
180 if (EFI_ERROR (Status
)) {
181 DEBUG ((DEBUG_INFO
, "Content for %s not found\n", EFI_KEK_DEFAULT_VARIABLE_NAME
));
186 Status
= gRT
->SetVariable (
187 EFI_KEK_DEFAULT_VARIABLE_NAME
,
188 &gEfiGlobalVariableGuid
,
189 EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
193 if (EFI_ERROR (Status
)) {
194 DEBUG ((DEBUG_INFO
, "Failed to set %s\n", EFI_KEK_DEFAULT_VARIABLE_NAME
));
202 /** Initializes dbDefault variable with data from FFS section.
204 @retval EFI_SUCCESS Variable was initialized successfully.
205 @retval EFI_UNSUPPORTED Variable already exists.
208 SecureBootInitDbDefault (
212 EFI_SIGNATURE_LIST
*EfiSig
;
218 Status
= GetVariable2 (EFI_DB_DEFAULT_VARIABLE_NAME
, &gEfiGlobalVariableGuid
, (VOID
**) &Data
, &DataSize
);
219 if (Status
== EFI_SUCCESS
) {
220 DEBUG ((DEBUG_INFO
, "Variable %s exists. Old value is preserved\n", EFI_DB_DEFAULT_VARIABLE_NAME
));
222 return EFI_UNSUPPORTED
;
225 if (EFI_ERROR (Status
) && (Status
!= EFI_NOT_FOUND
)) {
229 DEBUG ((DEBUG_INFO
, "Variable %s does not exist.\n", EFI_DB_DEFAULT_VARIABLE_NAME
));
231 Status
= SecureBootFetchData (&gDefaultdbFileGuid
, &SigListsSize
, &EfiSig
);
232 if (EFI_ERROR (Status
)) {
236 Status
= gRT
->SetVariable (
237 EFI_DB_DEFAULT_VARIABLE_NAME
,
238 &gEfiGlobalVariableGuid
,
239 EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
243 if (EFI_ERROR (Status
)) {
244 DEBUG ((DEBUG_INFO
, "Failed to set %s\n", EFI_DB_DEFAULT_VARIABLE_NAME
));
252 /** Initializes dbxDefault variable with data from FFS section.
254 @retval EFI_SUCCESS Variable was initialized successfully.
255 @retval EFI_UNSUPPORTED Variable already exists.
258 SecureBootInitDbxDefault (
262 EFI_SIGNATURE_LIST
*EfiSig
;
269 // Check if variable exists, if so do not change it
271 Status
= GetVariable2 (EFI_DBX_DEFAULT_VARIABLE_NAME
, &gEfiGlobalVariableGuid
, (VOID
**) &Data
, &DataSize
);
272 if (Status
== EFI_SUCCESS
) {
273 DEBUG ((DEBUG_INFO
, "Variable %s exists. Old value is preserved\n", EFI_DBX_DEFAULT_VARIABLE_NAME
));
275 return EFI_UNSUPPORTED
;
278 if (EFI_ERROR (Status
) && (Status
!= EFI_NOT_FOUND
)) {
283 // Variable does not exist, can be initialized
285 DEBUG ((DEBUG_INFO
, "Variable %s does not exist.\n", EFI_DBX_DEFAULT_VARIABLE_NAME
));
287 Status
= SecureBootFetchData (&gDefaultdbxFileGuid
, &SigListsSize
, &EfiSig
);
288 if (EFI_ERROR (Status
)) {
289 DEBUG ((DEBUG_INFO
, "Content for %s not found\n", EFI_DBX_DEFAULT_VARIABLE_NAME
));
293 Status
= gRT
->SetVariable (
294 EFI_DBX_DEFAULT_VARIABLE_NAME
,
295 &gEfiGlobalVariableGuid
,
296 EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
300 if (EFI_ERROR (Status
)) {
301 DEBUG ((DEBUG_INFO
, "Failed to set %s\n", EFI_DBX_DEFAULT_VARIABLE_NAME
));
309 /** Initializes dbtDefault variable with data from FFS section.
311 @retval EFI_SUCCESS Variable was initialized successfully.
312 @retval EFI_UNSUPPORTED Variable already exists.
315 SecureBootInitDbtDefault (
319 EFI_SIGNATURE_LIST
*EfiSig
;
326 // Check if variable exists, if so do not change it
328 Status
= GetVariable2 (EFI_DBT_DEFAULT_VARIABLE_NAME
, &gEfiGlobalVariableGuid
, (VOID
**) &Data
, &DataSize
);
329 if (Status
== EFI_SUCCESS
) {
330 DEBUG ((DEBUG_INFO
, "Variable %s exists. Old value is preserved\n", EFI_DBT_DEFAULT_VARIABLE_NAME
));
332 return EFI_UNSUPPORTED
;
335 if (EFI_ERROR (Status
) && (Status
!= EFI_NOT_FOUND
)) {
340 // Variable does not exist, can be initialized
342 DEBUG ((DEBUG_INFO
, "Variable %s does not exist.\n", EFI_DBT_DEFAULT_VARIABLE_NAME
));
344 Status
= SecureBootFetchData (&gDefaultdbtFileGuid
, &SigListsSize
, &EfiSig
);
345 if (EFI_ERROR (Status
)) {
349 Status
= gRT
->SetVariable (
350 EFI_DBT_DEFAULT_VARIABLE_NAME
,
351 &gEfiGlobalVariableGuid
,
352 EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
356 if (EFI_ERROR (Status
)) {
357 DEBUG ((DEBUG_INFO
, "Failed to set %s\n", EFI_DBT_DEFAULT_VARIABLE_NAME
));
366 Sets the content of the 'db' variable based on 'dbDefault' variable content.
368 @retval EFI_OUT_OF_RESOURCES If memory allocation for EFI_VARIABLE_AUTHENTICATION_2 fails
369 while VendorGuid is NULL.
370 @retval other Errors from GetVariable2 (), GetTime () and SetVariable ()
374 EnrollDbFromDefault (
380 Status
= EnrollFromDefault (
381 EFI_IMAGE_SECURITY_DATABASE
,
382 EFI_DB_DEFAULT_VARIABLE_NAME
,
383 &gEfiImageSecurityDatabaseGuid
390 Sets the content of the 'dbx' variable based on 'dbxDefault' variable content.
392 @retval EFI_OUT_OF_RESOURCES If memory allocation for EFI_VARIABLE_AUTHENTICATION_2 fails
393 while VendorGuid is NULL.
394 @retval other Errors from GetVariable2 (), GetTime () and SetVariable ()
398 EnrollDbxFromDefault (
404 Status
= EnrollFromDefault (
405 EFI_IMAGE_SECURITY_DATABASE1
,
406 EFI_DBX_DEFAULT_VARIABLE_NAME
,
407 &gEfiImageSecurityDatabaseGuid
414 Sets the content of the 'dbt' variable based on 'dbtDefault' variable content.
416 @retval EFI_OUT_OF_RESOURCES If memory allocation for EFI_VARIABLE_AUTHENTICATION_2 fails
417 while VendorGuid is NULL.
418 @retval other Errors from GetVariable2 (), GetTime () and SetVariable ()
422 EnrollDbtFromDefault (
428 Status
= EnrollFromDefault (
429 EFI_IMAGE_SECURITY_DATABASE2
,
430 EFI_DBT_DEFAULT_VARIABLE_NAME
,
431 &gEfiImageSecurityDatabaseGuid
);
437 Sets the content of the 'KEK' variable based on 'KEKDefault' variable content.
439 @retval EFI_OUT_OF_RESOURCES If memory allocation for EFI_VARIABLE_AUTHENTICATION_2 fails
440 while VendorGuid is NULL.
441 @retval other Errors from GetVariable2 (), GetTime () and SetVariable ()
445 EnrollKEKFromDefault (
451 Status
= EnrollFromDefault (
452 EFI_KEY_EXCHANGE_KEY_NAME
,
453 EFI_KEK_DEFAULT_VARIABLE_NAME
,
454 &gEfiGlobalVariableGuid
461 Sets the content of the 'KEK' variable based on 'KEKDefault' variable content.
463 @retval EFI_OUT_OF_RESOURCES If memory allocation for EFI_VARIABLE_AUTHENTICATION_2 fails
464 while VendorGuid is NULL.
465 @retval other Errors from GetVariable2 (), GetTime () and SetVariable ()
469 EnrollPKFromDefault (
475 Status
= EnrollFromDefault (
476 EFI_PLATFORM_KEY_NAME
,
477 EFI_PK_DEFAULT_VARIABLE_NAME
,
478 &gEfiGlobalVariableGuid