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
)) {
78 "error: %a (\"%s\", %g): %r\n",
93 /** Initializes PKDefault variable with data from FFS section.
95 @retval EFI_SUCCESS Variable was initialized successfully.
96 @retval EFI_UNSUPPORTED Variable already exists.
99 SecureBootInitPKDefault (
103 EFI_SIGNATURE_LIST
*EfiSig
;
110 // Check if variable exists, if so do not change it
112 Status
= GetVariable2 (EFI_PK_DEFAULT_VARIABLE_NAME
, &gEfiGlobalVariableGuid
, (VOID
**)&Data
, &DataSize
);
113 if (Status
== EFI_SUCCESS
) {
114 DEBUG ((DEBUG_INFO
, "Variable %s exists. Old value is preserved\n", EFI_PK_DEFAULT_VARIABLE_NAME
));
116 return EFI_UNSUPPORTED
;
119 if (EFI_ERROR (Status
) && (Status
!= EFI_NOT_FOUND
)) {
124 // Variable does not exist, can be initialized
126 DEBUG ((DEBUG_INFO
, "Variable %s does not exist.\n", EFI_PK_DEFAULT_VARIABLE_NAME
));
128 Status
= SecureBootFetchData (&gDefaultPKFileGuid
, &SigListsSize
, &EfiSig
);
129 if (EFI_ERROR (Status
)) {
130 DEBUG ((DEBUG_INFO
, "Content for %s not found\n", EFI_PK_DEFAULT_VARIABLE_NAME
));
134 Status
= gRT
->SetVariable (
135 EFI_PK_DEFAULT_VARIABLE_NAME
,
136 &gEfiGlobalVariableGuid
,
137 EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
141 if (EFI_ERROR (Status
)) {
142 DEBUG ((DEBUG_INFO
, "Failed to set %s\n", EFI_PK_DEFAULT_VARIABLE_NAME
));
150 /** Initializes KEKDefault variable with data from FFS section.
152 @retval EFI_SUCCESS Variable was initialized successfully.
153 @retval EFI_UNSUPPORTED Variable already exists.
156 SecureBootInitKEKDefault (
160 EFI_SIGNATURE_LIST
*EfiSig
;
167 // Check if variable exists, if so do not change it
169 Status
= GetVariable2 (EFI_KEK_DEFAULT_VARIABLE_NAME
, &gEfiGlobalVariableGuid
, (VOID
**)&Data
, &DataSize
);
170 if (Status
== EFI_SUCCESS
) {
171 DEBUG ((DEBUG_INFO
, "Variable %s exists. Old value is preserved\n", EFI_KEK_DEFAULT_VARIABLE_NAME
));
173 return EFI_UNSUPPORTED
;
176 if (EFI_ERROR (Status
) && (Status
!= EFI_NOT_FOUND
)) {
181 // Variable does not exist, can be initialized
183 DEBUG ((DEBUG_INFO
, "Variable %s does not exist.\n", EFI_KEK_DEFAULT_VARIABLE_NAME
));
185 Status
= SecureBootFetchData (&gDefaultKEKFileGuid
, &SigListsSize
, &EfiSig
);
186 if (EFI_ERROR (Status
)) {
187 DEBUG ((DEBUG_INFO
, "Content for %s not found\n", EFI_KEK_DEFAULT_VARIABLE_NAME
));
191 Status
= gRT
->SetVariable (
192 EFI_KEK_DEFAULT_VARIABLE_NAME
,
193 &gEfiGlobalVariableGuid
,
194 EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
198 if (EFI_ERROR (Status
)) {
199 DEBUG ((DEBUG_INFO
, "Failed to set %s\n", EFI_KEK_DEFAULT_VARIABLE_NAME
));
207 /** Initializes dbDefault variable with data from FFS section.
209 @retval EFI_SUCCESS Variable was initialized successfully.
210 @retval EFI_UNSUPPORTED Variable already exists.
213 SecureBootInitDbDefault (
217 EFI_SIGNATURE_LIST
*EfiSig
;
223 Status
= GetVariable2 (EFI_DB_DEFAULT_VARIABLE_NAME
, &gEfiGlobalVariableGuid
, (VOID
**)&Data
, &DataSize
);
224 if (Status
== EFI_SUCCESS
) {
225 DEBUG ((DEBUG_INFO
, "Variable %s exists. Old value is preserved\n", EFI_DB_DEFAULT_VARIABLE_NAME
));
227 return EFI_UNSUPPORTED
;
230 if (EFI_ERROR (Status
) && (Status
!= EFI_NOT_FOUND
)) {
234 DEBUG ((DEBUG_INFO
, "Variable %s does not exist.\n", EFI_DB_DEFAULT_VARIABLE_NAME
));
236 Status
= SecureBootFetchData (&gDefaultdbFileGuid
, &SigListsSize
, &EfiSig
);
237 if (EFI_ERROR (Status
)) {
241 Status
= gRT
->SetVariable (
242 EFI_DB_DEFAULT_VARIABLE_NAME
,
243 &gEfiGlobalVariableGuid
,
244 EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
248 if (EFI_ERROR (Status
)) {
249 DEBUG ((DEBUG_INFO
, "Failed to set %s\n", EFI_DB_DEFAULT_VARIABLE_NAME
));
257 /** Initializes dbxDefault variable with data from FFS section.
259 @retval EFI_SUCCESS Variable was initialized successfully.
260 @retval EFI_UNSUPPORTED Variable already exists.
263 SecureBootInitDbxDefault (
267 EFI_SIGNATURE_LIST
*EfiSig
;
274 // Check if variable exists, if so do not change it
276 Status
= GetVariable2 (EFI_DBX_DEFAULT_VARIABLE_NAME
, &gEfiGlobalVariableGuid
, (VOID
**)&Data
, &DataSize
);
277 if (Status
== EFI_SUCCESS
) {
278 DEBUG ((DEBUG_INFO
, "Variable %s exists. Old value is preserved\n", EFI_DBX_DEFAULT_VARIABLE_NAME
));
280 return EFI_UNSUPPORTED
;
283 if (EFI_ERROR (Status
) && (Status
!= EFI_NOT_FOUND
)) {
288 // Variable does not exist, can be initialized
290 DEBUG ((DEBUG_INFO
, "Variable %s does not exist.\n", EFI_DBX_DEFAULT_VARIABLE_NAME
));
292 Status
= SecureBootFetchData (&gDefaultdbxFileGuid
, &SigListsSize
, &EfiSig
);
293 if (EFI_ERROR (Status
)) {
294 DEBUG ((DEBUG_INFO
, "Content for %s not found\n", EFI_DBX_DEFAULT_VARIABLE_NAME
));
298 Status
= gRT
->SetVariable (
299 EFI_DBX_DEFAULT_VARIABLE_NAME
,
300 &gEfiGlobalVariableGuid
,
301 EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
305 if (EFI_ERROR (Status
)) {
306 DEBUG ((DEBUG_INFO
, "Failed to set %s\n", EFI_DBX_DEFAULT_VARIABLE_NAME
));
314 /** Initializes dbtDefault variable with data from FFS section.
316 @retval EFI_SUCCESS Variable was initialized successfully.
317 @retval EFI_UNSUPPORTED Variable already exists.
320 SecureBootInitDbtDefault (
324 EFI_SIGNATURE_LIST
*EfiSig
;
331 // Check if variable exists, if so do not change it
333 Status
= GetVariable2 (EFI_DBT_DEFAULT_VARIABLE_NAME
, &gEfiGlobalVariableGuid
, (VOID
**)&Data
, &DataSize
);
334 if (Status
== EFI_SUCCESS
) {
335 DEBUG ((DEBUG_INFO
, "Variable %s exists. Old value is preserved\n", EFI_DBT_DEFAULT_VARIABLE_NAME
));
337 return EFI_UNSUPPORTED
;
340 if (EFI_ERROR (Status
) && (Status
!= EFI_NOT_FOUND
)) {
345 // Variable does not exist, can be initialized
347 DEBUG ((DEBUG_INFO
, "Variable %s does not exist.\n", EFI_DBT_DEFAULT_VARIABLE_NAME
));
349 Status
= SecureBootFetchData (&gDefaultdbtFileGuid
, &SigListsSize
, &EfiSig
);
350 if (EFI_ERROR (Status
)) {
354 Status
= gRT
->SetVariable (
355 EFI_DBT_DEFAULT_VARIABLE_NAME
,
356 &gEfiGlobalVariableGuid
,
357 EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
361 if (EFI_ERROR (Status
)) {
362 DEBUG ((DEBUG_INFO
, "Failed to set %s\n", EFI_DBT_DEFAULT_VARIABLE_NAME
));
371 Sets the content of the 'db' variable based on 'dbDefault' variable content.
373 @retval EFI_OUT_OF_RESOURCES If memory allocation for EFI_VARIABLE_AUTHENTICATION_2 fails
374 while VendorGuid is NULL.
375 @retval other Errors from GetVariable2 (), GetTime () and SetVariable ()
379 EnrollDbFromDefault (
385 Status
= EnrollFromDefault (
386 EFI_IMAGE_SECURITY_DATABASE
,
387 EFI_DB_DEFAULT_VARIABLE_NAME
,
388 &gEfiImageSecurityDatabaseGuid
395 Sets the content of the 'dbx' variable based on 'dbxDefault' variable content.
397 @retval EFI_OUT_OF_RESOURCES If memory allocation for EFI_VARIABLE_AUTHENTICATION_2 fails
398 while VendorGuid is NULL.
399 @retval other Errors from GetVariable2 (), GetTime () and SetVariable ()
403 EnrollDbxFromDefault (
409 Status
= EnrollFromDefault (
410 EFI_IMAGE_SECURITY_DATABASE1
,
411 EFI_DBX_DEFAULT_VARIABLE_NAME
,
412 &gEfiImageSecurityDatabaseGuid
419 Sets the content of the 'dbt' variable based on 'dbtDefault' variable content.
421 @retval EFI_OUT_OF_RESOURCES If memory allocation for EFI_VARIABLE_AUTHENTICATION_2 fails
422 while VendorGuid is NULL.
423 @retval other Errors from GetVariable2 (), GetTime () and SetVariable ()
427 EnrollDbtFromDefault (
433 Status
= EnrollFromDefault (
434 EFI_IMAGE_SECURITY_DATABASE2
,
435 EFI_DBT_DEFAULT_VARIABLE_NAME
,
436 &gEfiImageSecurityDatabaseGuid
443 Sets the content of the 'KEK' variable based on 'KEKDefault' variable content.
445 @retval EFI_OUT_OF_RESOURCES If memory allocation for EFI_VARIABLE_AUTHENTICATION_2 fails
446 while VendorGuid is NULL.
447 @retval other Errors from GetVariable2 (), GetTime () and SetVariable ()
451 EnrollKEKFromDefault (
457 Status
= EnrollFromDefault (
458 EFI_KEY_EXCHANGE_KEY_NAME
,
459 EFI_KEK_DEFAULT_VARIABLE_NAME
,
460 &gEfiGlobalVariableGuid
467 Sets the content of the 'KEK' variable based on 'KEKDefault' variable content.
469 @retval EFI_OUT_OF_RESOURCES If memory allocation for EFI_VARIABLE_AUTHENTICATION_2 fails
470 while VendorGuid is NULL.
471 @retval other Errors from GetVariable2 (), GetTime () and SetVariable ()
475 EnrollPKFromDefault (
481 Status
= EnrollFromDefault (
482 EFI_PLATFORM_KEY_NAME
,
483 EFI_PK_DEFAULT_VARIABLE_NAME
,
484 &gEfiGlobalVariableGuid