2 Implement authentication services for the authenticated variables.
4 Caution: This module requires additional review when modified.
5 This driver will have external input - variable data. It may be input in SMM mode.
6 This external input must be validated carefully to avoid security issue like
7 buffer overflow, integer overflow.
8 Variable attribute should also be checked to avoid authentication bypass.
9 The whole SMM authentication variable design relies on the integrity of flash part and SMM.
10 which is assumed to be protected by platform. All variable code and metadata in flash/SMM Memory
11 may not be modified without authorization. If platform fails to protect these resources,
12 the authentication service provided in this driver will be broken, and the behavior is undefined.
14 ProcessVarWithPk(), ProcessVarWithKek() and ProcessVariable() are the function to do
15 variable authentication.
17 VerifyTimeBasedPayloadAndUpdate() and VerifyCounterBasedPayload() are sub function to do verification.
18 They will do basic validation for authentication data structure, then call crypto library
19 to verify the signature.
21 Copyright (c) 2009 - 2015, Intel Corporation. All rights reserved.<BR>
22 This program and the accompanying materials
23 are licensed and made available under the terms and conditions of the BSD License
24 which accompanies this distribution. The full text of the license may be found at
25 http://opensource.org/licenses/bsd-license.php
27 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
28 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
32 #include "AuthServiceInternal.h"
35 // Public Exponent of RSA Key.
37 CONST UINT8 mRsaE
[] = { 0x01, 0x00, 0x01 };
40 // Requirement for different signature type which have been defined in UEFI spec.
41 // These data are used to perform SignatureList format check while setting PK/KEK variable.
43 EFI_SIGNATURE_ITEM mSupportSigItem
[] = {
44 //{SigType, SigHeaderSize, SigDataSize }
45 {EFI_CERT_SHA256_GUID
, 0, 32 },
46 {EFI_CERT_RSA2048_GUID
, 0, 256 },
47 {EFI_CERT_RSA2048_SHA256_GUID
, 0, 256 },
48 {EFI_CERT_SHA1_GUID
, 0, 20 },
49 {EFI_CERT_RSA2048_SHA1_GUID
, 0, 256 },
50 {EFI_CERT_X509_GUID
, 0, ((UINT32
) ~0)},
51 {EFI_CERT_SHA224_GUID
, 0, 28 },
52 {EFI_CERT_SHA384_GUID
, 0, 48 },
53 {EFI_CERT_SHA512_GUID
, 0, 64 },
54 {EFI_CERT_X509_SHA256_GUID
, 0, 48 },
55 {EFI_CERT_X509_SHA384_GUID
, 0, 64 },
56 {EFI_CERT_X509_SHA512_GUID
, 0, 80 }
60 // Secure Boot Mode state machine
62 SECURE_BOOT_MODE mSecureBootState
[SecureBootModeTypeMax
] = {
65 AUDIT_MODE_DISABLE
, // AuditMode
66 FALSE
, // IsAuditModeRO, AuditMode is RW
67 DEPLOYED_MODE_DISABLE
, // DeployedMode
68 FALSE
, // IsDeployedModeRO, DeployedMode is RW
69 SETUP_MODE_DISABLE
, // SetupMode
70 // SetupMode is always RO
71 SECURE_BOOT_MODE_ENABLE
// SecureBoot
75 AUDIT_MODE_DISABLE
, // AuditMode
76 FALSE
, // IsAuditModeRO, AuditMode is RW
77 DEPLOYED_MODE_DISABLE
, // DeployedMode
78 TRUE
, // IsDeployedModeRO, DeployedMode is RO
79 SETUP_MODE_ENABLE
, // SetupMode
80 // SetupMode is always RO
81 SECURE_BOOT_MODE_DISABLE
// SecureBoot
85 AUDIT_MODE_ENABLE
, // AuditMode
86 TRUE
, // AuditModeValAttr RO, AuditMode is RO
87 DEPLOYED_MODE_DISABLE
, // DeployedMode
88 TRUE
, // DeployedModeValAttr RO, DeployedMode is RO
89 SETUP_MODE_ENABLE
, // SetupMode
90 // SetupMode is always RO
91 SECURE_BOOT_MODE_DISABLE
// SecureBoot
95 AUDIT_MODE_DISABLE
, // AuditMode, AuditMode is RO
96 TRUE
, // AuditModeValAttr RO
97 DEPLOYED_MODE_ENABLE
, // DeployedMode
98 TRUE
, // DeployedModeValAttr RO, DeployedMode is RO
99 SETUP_MODE_DISABLE
, // SetupMode
100 // SetupMode is always RO
101 SECURE_BOOT_MODE_ENABLE
// SecureBoot
105 SECURE_BOOT_MODE_TYPE mSecureBootMode
;
108 Finds variable in storage blocks of volatile and non-volatile storage areas.
110 This code finds variable in storage blocks of volatile and non-volatile storage areas.
111 If VariableName is an empty string, then we just return the first
112 qualified variable without comparing VariableName and VendorGuid.
114 @param[in] VariableName Name of the variable to be found.
115 @param[in] VendorGuid Variable vendor GUID to be found.
116 @param[out] Data Pointer to data address.
117 @param[out] DataSize Pointer to data size.
119 @retval EFI_INVALID_PARAMETER If VariableName is not an empty string,
120 while VendorGuid is NULL.
121 @retval EFI_SUCCESS Variable successfully found.
122 @retval EFI_NOT_FOUND Variable not found
126 AuthServiceInternalFindVariable (
127 IN CHAR16
*VariableName
,
128 IN EFI_GUID
*VendorGuid
,
134 AUTH_VARIABLE_INFO AuthVariableInfo
;
136 ZeroMem (&AuthVariableInfo
, sizeof (AuthVariableInfo
));
137 Status
= mAuthVarLibContextIn
->FindVariable (
142 *Data
= AuthVariableInfo
.Data
;
143 *DataSize
= AuthVariableInfo
.DataSize
;
148 Update the variable region with Variable information.
150 @param[in] VariableName Name of variable.
151 @param[in] VendorGuid Guid of variable.
152 @param[in] Data Data pointer.
153 @param[in] DataSize Size of Data.
154 @param[in] Attributes Attribute value of the variable.
156 @retval EFI_SUCCESS The update operation is success.
157 @retval EFI_INVALID_PARAMETER Invalid parameter.
158 @retval EFI_WRITE_PROTECTED Variable is write-protected.
159 @retval EFI_OUT_OF_RESOURCES There is not enough resource.
163 AuthServiceInternalUpdateVariable (
164 IN CHAR16
*VariableName
,
165 IN EFI_GUID
*VendorGuid
,
171 AUTH_VARIABLE_INFO AuthVariableInfo
;
173 ZeroMem (&AuthVariableInfo
, sizeof (AuthVariableInfo
));
174 AuthVariableInfo
.VariableName
= VariableName
;
175 AuthVariableInfo
.VendorGuid
= VendorGuid
;
176 AuthVariableInfo
.Data
= Data
;
177 AuthVariableInfo
.DataSize
= DataSize
;
178 AuthVariableInfo
.Attributes
= Attributes
;
180 return mAuthVarLibContextIn
->UpdateVariable (
186 Update the variable region with Variable information.
188 @param[in] VariableName Name of variable.
189 @param[in] VendorGuid Guid of variable.
190 @param[in] Data Data pointer.
191 @param[in] DataSize Size of Data.
192 @param[in] Attributes Attribute value of the variable.
193 @param[in] KeyIndex Index of associated public key.
194 @param[in] MonotonicCount Value of associated monotonic count.
196 @retval EFI_SUCCESS The update operation is success.
197 @retval EFI_INVALID_PARAMETER Invalid parameter.
198 @retval EFI_WRITE_PROTECTED Variable is write-protected.
199 @retval EFI_OUT_OF_RESOURCES There is not enough resource.
203 AuthServiceInternalUpdateVariableWithMonotonicCount (
204 IN CHAR16
*VariableName
,
205 IN EFI_GUID
*VendorGuid
,
208 IN UINT32 Attributes
,
210 IN UINT64 MonotonicCount
213 AUTH_VARIABLE_INFO AuthVariableInfo
;
215 ZeroMem (&AuthVariableInfo
, sizeof (AuthVariableInfo
));
216 AuthVariableInfo
.VariableName
= VariableName
;
217 AuthVariableInfo
.VendorGuid
= VendorGuid
;
218 AuthVariableInfo
.Data
= Data
;
219 AuthVariableInfo
.DataSize
= DataSize
;
220 AuthVariableInfo
.Attributes
= Attributes
;
221 AuthVariableInfo
.PubKeyIndex
= KeyIndex
;
222 AuthVariableInfo
.MonotonicCount
= MonotonicCount
;
224 return mAuthVarLibContextIn
->UpdateVariable (
230 Update the variable region with Variable information.
232 @param[in] VariableName Name of variable.
233 @param[in] VendorGuid Guid of variable.
234 @param[in] Data Data pointer.
235 @param[in] DataSize Size of Data.
236 @param[in] Attributes Attribute value of the variable.
237 @param[in] TimeStamp Value of associated TimeStamp.
239 @retval EFI_SUCCESS The update operation is success.
240 @retval EFI_INVALID_PARAMETER Invalid parameter.
241 @retval EFI_WRITE_PROTECTED Variable is write-protected.
242 @retval EFI_OUT_OF_RESOURCES There is not enough resource.
246 AuthServiceInternalUpdateVariableWithTimeStamp (
247 IN CHAR16
*VariableName
,
248 IN EFI_GUID
*VendorGuid
,
251 IN UINT32 Attributes
,
252 IN EFI_TIME
*TimeStamp
255 EFI_STATUS FindStatus
;
258 AUTH_VARIABLE_INFO AuthVariableInfo
;
260 FindStatus
= AuthServiceInternalFindVariable (
268 // EFI_VARIABLE_APPEND_WRITE attribute only effects for existing variable
270 if (!EFI_ERROR (FindStatus
) && ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) != 0)) {
271 if ((CompareGuid (VendorGuid
, &gEfiImageSecurityDatabaseGuid
) &&
272 ((StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE
) == 0) || (StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE1
) == 0) ||
273 (StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE2
) == 0))) ||
274 (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, EFI_KEY_EXCHANGE_KEY_NAME
) == 0))) {
276 // For variables with formatted as EFI_SIGNATURE_LIST, the driver shall not perform an append of
277 // EFI_SIGNATURE_DATA values that are already part of the existing variable value.
279 FilterSignatureList (
288 ZeroMem (&AuthVariableInfo
, sizeof (AuthVariableInfo
));
289 AuthVariableInfo
.VariableName
= VariableName
;
290 AuthVariableInfo
.VendorGuid
= VendorGuid
;
291 AuthVariableInfo
.Data
= Data
;
292 AuthVariableInfo
.DataSize
= DataSize
;
293 AuthVariableInfo
.Attributes
= Attributes
;
294 AuthVariableInfo
.TimeStamp
= TimeStamp
;
295 return mAuthVarLibContextIn
->UpdateVariable (
301 Initialize Secure Boot variables.
303 @retval EFI_SUCCESS The initialization operation is successful.
304 @retval EFI_OUT_OF_RESOURCES There is not enough resource.
308 InitSecureBootVariables (
316 UINT8 SecureBootEnable
;
317 SECURE_BOOT_MODE_TYPE SecureBootMode
;
321 // Find "PK" variable
323 Status
= AuthServiceInternalFindVariable (EFI_PLATFORM_KEY_NAME
, &gEfiGlobalVariableGuid
, (VOID
**) &Data
, &DataSize
);
324 if (EFI_ERROR (Status
)) {
326 DEBUG ((EFI_D_INFO
, "Variable %s does not exist.\n", EFI_PLATFORM_KEY_NAME
));
329 DEBUG ((EFI_D_INFO
, "Variable %s exists.\n", EFI_PLATFORM_KEY_NAME
));
333 // Init "SecureBootMode" variable.
335 // SecureBootMode doesn't exist. Init it with PK state
336 // 3 inconsistency cases need to sync
337 // 1.1 Add PK -> system break -> update SecureBootMode Var
338 // 1.2 Delete PK -> system break -> update SecureBootMode Var
339 // 1.3 Set AuditMode ->Delete PK -> system break -> Update SecureBootMode Var
341 Status
= AuthServiceInternalFindVariable (EDKII_SECURE_BOOT_MODE_NAME
, &gEdkiiSecureBootModeGuid
, (VOID
**)&Data
, &DataSize
);
342 if (EFI_ERROR(Status
)) {
344 // Variable driver Initial Case
347 SecureBootMode
= SecureBootModeTypeUserMode
;
349 SecureBootMode
= SecureBootModeTypeSetupMode
;
353 // 3 inconsistency cases need to sync
355 SecureBootMode
= (SECURE_BOOT_MODE_TYPE
)*Data
;
356 ASSERT(SecureBootMode
< SecureBootModeTypeMax
);
360 // 3.1 Add PK -> system break -> update SecureBootMode Var
362 if (SecureBootMode
== SecureBootModeTypeSetupMode
) {
363 SecureBootMode
= SecureBootModeTypeUserMode
;
364 } else if (SecureBootMode
== SecureBootModeTypeAuditMode
) {
365 SecureBootMode
= SecureBootModeTypeDeployedMode
;
369 // 3.2 Delete PK -> system break -> update SecureBootMode Var
370 // 3.3 Set AuditMode ->Delete PK -> system break -> Update SecureBootMode Var. Reinit to be SetupMode
372 if ((SecureBootMode
== SecureBootModeTypeUserMode
) || (SecureBootMode
== SecureBootModeTypeDeployedMode
)) {
373 SecureBootMode
= SecureBootModeTypeSetupMode
;
378 if (EFI_ERROR(Status
) || (SecureBootMode
!= (SECURE_BOOT_MODE_TYPE
)*Data
)) {
380 // Update SecureBootMode Var
382 Status
= AuthServiceInternalUpdateVariable (
383 EDKII_SECURE_BOOT_MODE_NAME
,
384 &gEdkiiSecureBootModeGuid
,
387 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
389 if (EFI_ERROR(Status
)) {
397 Status
= AuthServiceInternalUpdateVariable (
399 &gEfiGlobalVariableGuid
,
400 &mSecureBootState
[SecureBootMode
].AuditMode
,
402 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
404 if (EFI_ERROR(Status
)) {
409 // Init "DeployedMode"
411 Status
= AuthServiceInternalUpdateVariable (
412 EFI_DEPLOYED_MODE_NAME
,
413 &gEfiGlobalVariableGuid
,
414 &mSecureBootState
[SecureBootMode
].DeployedMode
,
416 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
418 if (EFI_ERROR(Status
)) {
425 Status
= AuthServiceInternalUpdateVariable (
427 &gEfiGlobalVariableGuid
,
428 &mSecureBootState
[SecureBootMode
].SetupMode
,
430 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
432 if (EFI_ERROR(Status
)) {
437 // If "SecureBootEnable" variable exists, then update "SecureBoot" variable.
438 // If "SecureBootEnable" variable is SECURE_BOOT_ENABLE and in User Mode or Deployed Mode, Set "SecureBoot" variable to SECURE_BOOT_MODE_ENABLE.
439 // If "SecureBootEnable" variable is SECURE_BOOT_DISABLE, Set "SecureBoot" variable to SECURE_BOOT_MODE_DISABLE.
441 SecureBootEnable
= SECURE_BOOT_DISABLE
;
442 Status
= AuthServiceInternalFindVariable (EFI_SECURE_BOOT_ENABLE_NAME
, &gEfiSecureBootEnableDisableGuid
, (VOID
**)&Data
, &DataSize
);
443 if (!EFI_ERROR(Status
)) {
446 // PK is cleared in runtime. "SecureBootMode" is not updated before reboot
447 // Delete "SecureBootMode"
449 Status
= AuthServiceInternalUpdateVariable (
450 EFI_SECURE_BOOT_ENABLE_NAME
,
451 &gEfiSecureBootEnableDisableGuid
,
454 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
457 SecureBootEnable
= *Data
;
459 } else if ((SecureBootMode
== SecureBootModeTypeUserMode
) || (SecureBootMode
== SecureBootModeTypeDeployedMode
)) {
461 // "SecureBootEnable" not exist, initialize it in User Mode or Deployed Mode.
463 SecureBootEnable
= SECURE_BOOT_ENABLE
;
464 Status
= AuthServiceInternalUpdateVariable (
465 EFI_SECURE_BOOT_ENABLE_NAME
,
466 &gEfiSecureBootEnableDisableGuid
,
469 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
471 if (EFI_ERROR (Status
)) {
477 // Create "SecureBoot" variable with BS+RT attribute set.
479 if ((SecureBootEnable
== SECURE_BOOT_ENABLE
)
480 && ((SecureBootMode
== SecureBootModeTypeUserMode
) || (SecureBootMode
== SecureBootModeTypeDeployedMode
))) {
481 SecureBoot
= SECURE_BOOT_MODE_ENABLE
;
483 SecureBoot
= SECURE_BOOT_MODE_DISABLE
;
485 Status
= AuthServiceInternalUpdateVariable (
486 EFI_SECURE_BOOT_MODE_NAME
,
487 &gEfiGlobalVariableGuid
,
490 EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
493 DEBUG ((EFI_D_INFO
, "SecureBootMode is %x\n", SecureBootMode
));
494 DEBUG ((EFI_D_INFO
, "Variable %s is %x\n", EFI_SECURE_BOOT_MODE_NAME
, SecureBoot
));
495 DEBUG ((EFI_D_INFO
, "Variable %s is %x\n", EFI_SECURE_BOOT_ENABLE_NAME
, SecureBootEnable
));
498 // Save SecureBootMode in global space
500 mSecureBootMode
= SecureBootMode
;
506 Update SecureBootMode variable.
508 @param[in] NewMode New Secure Boot Mode.
510 @retval EFI_SUCCESS The initialization operation is successful.
511 @retval EFI_OUT_OF_RESOURCES There is not enough resource.
515 UpdateSecureBootMode(
516 IN SECURE_BOOT_MODE_TYPE NewMode
522 // Update "SecureBootMode" variable to new Secure Boot Mode
524 Status
= AuthServiceInternalUpdateVariable (
525 EDKII_SECURE_BOOT_MODE_NAME
,
526 &gEdkiiSecureBootModeGuid
,
529 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
532 if (!EFI_ERROR(Status
)) {
533 DEBUG((EFI_D_INFO
, "SecureBootMode Update to %x\n", NewMode
));
534 mSecureBootMode
= NewMode
;
536 DEBUG((EFI_D_ERROR
, "SecureBootMode Update failure %x\n", Status
));
543 Current secure boot mode is AuditMode. This function performs secure boot mode transition
546 @param[in] NewMode New Secure Boot Mode.
548 @retval EFI_SUCCESS The initialization operation is successful.
549 @retval EFI_OUT_OF_RESOURCES There is not enough resource.
553 TransitionFromAuditMode(
554 IN SECURE_BOOT_MODE_TYPE NewMode
559 VOID
*DeployedVarData
;
561 VOID
*SecureBootVarData
;
562 UINT8 SecureBootEnable
;
566 // AuditMode/DeployedMode/SetupMode/SecureBoot are all NON_NV variable maintained by Variable driver
567 // they can be RW. but can't be deleted. so they can always be found.
569 Status
= AuthServiceInternalFindVariable (
571 &gEfiGlobalVariableGuid
,
575 if (EFI_ERROR (Status
)) {
579 Status
= AuthServiceInternalFindVariable (
580 EFI_DEPLOYED_MODE_NAME
,
581 &gEfiGlobalVariableGuid
,
585 if (EFI_ERROR (Status
)) {
589 Status
= AuthServiceInternalFindVariable (
591 &gEfiGlobalVariableGuid
,
595 if (EFI_ERROR (Status
)) {
599 Status
= AuthServiceInternalFindVariable (
600 EFI_SECURE_BOOT_MODE_NAME
,
601 &gEfiGlobalVariableGuid
,
605 if (EFI_ERROR (Status
)) {
610 // Make Secure Boot Mode transition ATOMIC
611 // Update Private NV SecureBootMode Variable first, because it may fail due to NV range overflow.
612 // other tranisition logic are all memory operations.
614 Status
= UpdateSecureBootMode(NewMode
);
615 if (EFI_ERROR(Status
)) {
616 DEBUG((EFI_D_ERROR
, "Update SecureBootMode Variable fail %x\n", Status
));
619 if (NewMode
== SecureBootModeTypeDeployedMode
) {
621 // Since PK is enrolled, can't rollback, always update SecureBootMode in memory
623 mSecureBootMode
= NewMode
;
624 Status
= EFI_SUCCESS
;
627 // AuditMode ----> DeployedMode
629 // AuditMode =: 0 / DeployedMode := 1 / SetupMode := 0
631 // Update the value of AuditMode variable by a simple mem copy, this could avoid possible
632 // variable storage reclaim at runtime.
634 CopyMem (AuditVarData
, &mSecureBootState
[NewMode
].AuditMode
, sizeof(UINT8
));
636 // Update the value of DeployedMode variable by a simple mem copy, this could avoid possible
637 // variable storage reclaim at runtime.
639 CopyMem (DeployedVarData
, &mSecureBootState
[NewMode
].DeployedMode
, sizeof(UINT8
));
641 // Update the value of SetupMode variable by a simple mem copy, this could avoid possible
642 // variable storage reclaim at runtime.
644 CopyMem (SetupVarData
, &mSecureBootState
[NewMode
].SetupMode
, sizeof(UINT8
));
646 if (mAuthVarLibContextIn
->AtRuntime ()) {
648 // SecureBoot Variable indicates whether the platform firmware is operating
649 // in Secure boot mode (1) or not (0), so we should not change SecureBoot
650 // Variable in runtime.
656 // Update the value of SecureBoot variable by a simple mem copy, this could avoid possible
657 // variable storage reclaim at runtime.
659 CopyMem (SecureBootVarData
, &mSecureBootState
[NewMode
].SecureBoot
, sizeof(UINT8
));
662 // Create "SecureBootEnable" variable as secure boot is enabled.
664 SecureBootEnable
= SECURE_BOOT_ENABLE
;
665 AuthServiceInternalUpdateVariable (
666 EFI_SECURE_BOOT_ENABLE_NAME
,
667 &gEfiSecureBootEnableDisableGuid
,
669 sizeof (SecureBootEnable
),
670 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
673 DEBUG((EFI_D_ERROR
, "Invalid state tranition from %x to %x\n", SecureBootModeTypeAuditMode
, NewMode
));
681 Current secure boot mode is DeployedMode. This function performs secure boot mode transition
684 @param[in] NewMode New Secure Boot Mode.
686 @retval EFI_SUCCESS The initialization operation is successful.
687 @retval EFI_OUT_OF_RESOURCES There is not enough resource.
691 TransitionFromDeployedMode(
692 IN SECURE_BOOT_MODE_TYPE NewMode
696 VOID
*DeployedVarData
;
698 VOID
*SecureBootVarData
;
699 UINT8 SecureBootEnable
;
703 // AuditMode/DeployedMode/SetupMode/SecureBoot are all NON_NV variable maintained by Variable driver
704 // they can be RW. but can't be deleted. so they can always be found.
706 Status
= AuthServiceInternalFindVariable (
707 EFI_DEPLOYED_MODE_NAME
,
708 &gEfiGlobalVariableGuid
,
712 if (EFI_ERROR (Status
)) {
716 Status
= AuthServiceInternalFindVariable (
718 &gEfiGlobalVariableGuid
,
722 if (EFI_ERROR (Status
)) {
726 Status
= AuthServiceInternalFindVariable (
727 EFI_SECURE_BOOT_MODE_NAME
,
728 &gEfiGlobalVariableGuid
,
732 if (EFI_ERROR (Status
)) {
737 // Make Secure Boot Mode transition ATOMIC
738 // Update Private NV SecureBootMode Variable first, because it may fail due to NV range overflow.
739 // other tranisition logic are all memory operations.
741 Status
= UpdateSecureBootMode(NewMode
);
742 if (EFI_ERROR(Status
)) {
743 DEBUG((EFI_D_ERROR
, "Update SecureBootMode Variable fail %x\n", Status
));
747 case SecureBootModeTypeUserMode
:
749 // DeployedMode ----> UserMode
753 // Platform Specific DeployedMode clear. UpdateSecureBootMode fails and no other variables are updated before. rollback this transition
755 if (EFI_ERROR(Status
)) {
758 CopyMem (DeployedVarData
, &mSecureBootState
[NewMode
].DeployedMode
, sizeof(UINT8
));
762 case SecureBootModeTypeSetupMode
:
764 // Since PK is processed before, can't rollback, still update SecureBootMode in memory
766 mSecureBootMode
= NewMode
;
767 Status
= EFI_SUCCESS
;
770 // DeployedMode ----> SetupMode
772 // Platform Specific PKpub clear or Delete Pkpub
774 // DeployedMode := 0 / SetupMode := 1 / SecureBoot := 0
776 // Update the value of DeployedMode variable by a simple mem copy, this could avoid possible
777 // variable storage reclaim at runtime.
779 CopyMem (DeployedVarData
, &mSecureBootState
[NewMode
].DeployedMode
, sizeof(UINT8
));
781 // Update the value of SetupMode variable by a simple mem copy, this could avoid possible
782 // variable storage reclaim at runtime.
784 CopyMem (SetupVarData
, &mSecureBootState
[NewMode
].SetupMode
, sizeof(UINT8
));
786 if (mAuthVarLibContextIn
->AtRuntime ()) {
788 // SecureBoot Variable indicates whether the platform firmware is operating
789 // in Secure boot mode (1) or not (0), so we should not change SecureBoot
790 // Variable in runtime.
796 // Update the value of SecureBoot variable by a simple mem copy, this could avoid possible
797 // variable storage reclaim at runtime.
799 CopyMem (SecureBootVarData
, &mSecureBootState
[NewMode
].SecureBoot
, sizeof(UINT8
));
802 // Delete the "SecureBootEnable" variable as secure boot is Disabled.
804 SecureBootEnable
= SECURE_BOOT_DISABLE
;
805 AuthServiceInternalUpdateVariable (
806 EFI_SECURE_BOOT_ENABLE_NAME
,
807 &gEfiSecureBootEnableDisableGuid
,
810 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
815 DEBUG((EFI_D_ERROR
, "Invalid state tranition from %x to %x\n", SecureBootModeTypeDeployedMode
, NewMode
));
823 Current secure boot mode is UserMode. This function performs secure boot mode transition
826 @param[in] NewMode New Secure Boot Mode.
828 @retval EFI_SUCCESS The initialization operation is successful.
829 @retval EFI_OUT_OF_RESOURCES There is not enough resource.
833 TransitionFromUserMode(
834 IN SECURE_BOOT_MODE_TYPE NewMode
839 VOID
*DeployedVarData
;
842 VOID
*SecureBootVarData
;
843 UINT8 SecureBootEnable
;
845 VARIABLE_ENTRY_CONSISTENCY VariableEntry
;
848 // AuditMode/DeployedMode/SetupMode/SecureBoot are all NON_NV variable maintained by Variable driver
849 // they can be RW. but can't be deleted. so they can always be found.
851 Status
= AuthServiceInternalFindVariable (
853 &gEfiGlobalVariableGuid
,
857 if (EFI_ERROR (Status
)) {
861 Status
= AuthServiceInternalFindVariable (
862 EFI_DEPLOYED_MODE_NAME
,
863 &gEfiGlobalVariableGuid
,
867 if (EFI_ERROR (Status
)) {
871 Status
= AuthServiceInternalFindVariable (
873 &gEfiGlobalVariableGuid
,
877 if (EFI_ERROR (Status
)) {
881 Status
= AuthServiceInternalFindVariable (
882 EFI_SECURE_BOOT_MODE_NAME
,
883 &gEfiGlobalVariableGuid
,
887 if (EFI_ERROR (Status
)) {
892 // Make Secure Boot Mode transition ATOMIC
893 // Update Private NV SecureBootMode Variable first, because it may fail due to NV range overflow.
894 // Other tranisition logic are all memory operations and PK delete is assumed to be always successful.
896 if (NewMode
!= SecureBootModeTypeAuditMode
) {
897 Status
= UpdateSecureBootMode(NewMode
);
898 if (EFI_ERROR(Status
)) {
899 DEBUG((EFI_D_ERROR
, "Update SecureBootMode Variable fail %x\n", Status
));
903 // UserMode -----> AuditMode. Check RemainingSpace for SecureBootMode var first.
904 // Will update SecureBootMode after DeletePK logic
906 VariableEntry
.VariableSize
= sizeof(UINT8
);
907 VariableEntry
.Guid
= &gEdkiiSecureBootModeGuid
;
908 VariableEntry
.Name
= EDKII_SECURE_BOOT_MODE_NAME
;
909 if (!mAuthVarLibContextIn
->CheckRemainingSpaceForConsistency (VARIABLE_ATTRIBUTE_NV_BS_RT
, &VariableEntry
, NULL
)) {
910 return EFI_OUT_OF_RESOURCES
;
915 case SecureBootModeTypeDeployedMode
:
917 // UpdateSecureBootMode fails and no other variables are updated before. rollback this transition
919 if (EFI_ERROR(Status
)) {
924 // UserMode ----> DeployedMode
928 CopyMem (DeployedVarData
, &mSecureBootState
[NewMode
].DeployedMode
, sizeof(UINT8
));
931 case SecureBootModeTypeAuditMode
:
933 // UserMode ----> AuditMode
935 // Delete PKpub / SetupMode := 1 / SecureBoot := 0
937 // Delete PKpub without verification. Should always succeed.
940 Status
= AuthServiceInternalUpdateVariable (
941 EFI_PLATFORM_KEY_NAME
,
942 &gEfiGlobalVariableGuid
,
945 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
947 if (EFI_ERROR(Status
)) {
948 DEBUG((EFI_D_ERROR
, "UserMode -> AuditMode. Delete PK fail %x\n", Status
));
953 // Update Private NV SecureBootMode Variable
955 Status
= UpdateSecureBootMode(NewMode
);
956 if (EFI_ERROR(Status
)) {
958 // Since PK is deleted successfully, Doesn't break, continue to update other variable.
960 DEBUG((EFI_D_ERROR
, "Update SecureBootMode Variable fail %x\n", Status
));
962 CopyMem (AuditVarData
, &mSecureBootState
[NewMode
].AuditMode
, sizeof(UINT8
));
965 // Fall into SetupMode logic
967 case SecureBootModeTypeSetupMode
:
969 // Since PK is deleted before , can't rollback, still update SecureBootMode in memory
971 mSecureBootMode
= NewMode
;
972 Status
= EFI_SUCCESS
;
975 // UserMode ----> SetupMode
977 // DeployedMode :=0 / SetupMode :=1 / SecureBoot :=0
979 // Update the value of SetupMode variable by a simple mem copy, this could avoid possible
980 // variable storage reclaim at runtime.
982 CopyMem (SetupVarData
, &mSecureBootState
[NewMode
].SetupMode
, sizeof(UINT8
));
984 if (mAuthVarLibContextIn
->AtRuntime ()) {
986 // SecureBoot Variable indicates whether the platform firmware is operating
987 // in Secure boot mode (1) or not (0), so we should not change SecureBoot
988 // Variable in runtime.
994 // Update the value of SecureBoot variable by a simple mem copy, this could avoid possible
995 // variable storage reclaim at runtime.
997 CopyMem (SecureBootVarData
, &mSecureBootState
[NewMode
].SecureBoot
, sizeof(UINT8
));
1000 // Delete the "SecureBootEnable" variable as secure boot is Disabled.
1002 SecureBootEnable
= SECURE_BOOT_DISABLE
;
1003 AuthServiceInternalUpdateVariable (
1004 EFI_SECURE_BOOT_ENABLE_NAME
,
1005 &gEfiSecureBootEnableDisableGuid
,
1008 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
1014 DEBUG((EFI_D_ERROR
, "Invalid state tranition from %x to %x\n", SecureBootModeTypeUserMode
, NewMode
));
1022 Current secure boot mode is SetupMode. This function performs secure boot mode transition
1025 @param[in] NewMode New Secure Boot Mode.
1027 @retval EFI_SUCCESS The initialization operation is successful.
1028 @retval EFI_OUT_OF_RESOURCES There is not enough resource.
1032 TransitionFromSetupMode(
1033 IN SECURE_BOOT_MODE_TYPE NewMode
1039 VOID
*SecureBootVarData
;
1040 UINT8 SecureBootEnable
;
1044 // AuditMode/DeployedMode/SetupMode/SecureBoot are all NON_NV variable maintained by Variable driver
1045 // they can be RW. but can't be deleted. so they can always be found.
1047 Status
= AuthServiceInternalFindVariable (
1048 EFI_AUDIT_MODE_NAME
,
1049 &gEfiGlobalVariableGuid
,
1053 if (EFI_ERROR (Status
)) {
1057 Status
= AuthServiceInternalFindVariable (
1058 EFI_SETUP_MODE_NAME
,
1059 &gEfiGlobalVariableGuid
,
1063 if (EFI_ERROR (Status
)) {
1067 Status
= AuthServiceInternalFindVariable (
1068 EFI_SECURE_BOOT_MODE_NAME
,
1069 &gEfiGlobalVariableGuid
,
1073 if (EFI_ERROR (Status
)) {
1078 // Make Secure Boot Mode transition ATOMIC
1079 // Update Private NV SecureBootMode Variable first, because it may fail due to NV range overflow.
1080 // Other tranisition logic are all memory operations and PK delete is assumed to be always successful.
1082 Status
= UpdateSecureBootMode(NewMode
);
1083 if (EFI_ERROR(Status
)) {
1084 DEBUG((EFI_D_ERROR
, "Update SecureBootMode Variable fail %x\n", Status
));
1088 case SecureBootModeTypeAuditMode
:
1090 // UpdateSecureBootMode fails and no other variables are updated before. rollback this transition
1092 if (EFI_ERROR(Status
)) {
1097 // SetupMode ----> AuditMode
1101 // Update the value of AuditMode variable by a simple mem copy, this could avoid possible
1102 // variable storage reclaim at runtime.
1104 CopyMem (AuditVarData
, &mSecureBootState
[NewMode
].AuditMode
, sizeof(UINT8
));
1107 case SecureBootModeTypeUserMode
:
1109 // Since PK is enrolled before, can't rollback, still update SecureBootMode in memory
1111 mSecureBootMode
= NewMode
;
1112 Status
= EFI_SUCCESS
;
1115 // SetupMode ----> UserMode
1117 // SetupMode := 0 / SecureBoot := 1
1119 // Update the value of AuditMode variable by a simple mem copy, this could avoid possible
1120 // variable storage reclaim at runtime.
1122 CopyMem (SetupVarData
, &mSecureBootState
[NewMode
].SetupMode
, sizeof(UINT8
));
1124 if (mAuthVarLibContextIn
->AtRuntime ()) {
1126 // SecureBoot Variable indicates whether the platform firmware is operating
1127 // in Secure boot mode (1) or not (0), so we should not change SecureBoot
1128 // Variable in runtime.
1134 // Update the value of SecureBoot variable by a simple mem copy, this could avoid possible
1135 // variable storage reclaim at runtime.
1137 CopyMem (SecureBootVarData
, &mSecureBootState
[NewMode
].SecureBoot
, sizeof(UINT8
));
1140 // Create the "SecureBootEnable" variable as secure boot is enabled.
1142 SecureBootEnable
= SECURE_BOOT_ENABLE
;
1143 AuthServiceInternalUpdateVariable (
1144 EFI_SECURE_BOOT_ENABLE_NAME
,
1145 &gEfiSecureBootEnableDisableGuid
,
1147 sizeof (SecureBootEnable
),
1148 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
1153 DEBUG((EFI_D_ERROR
, "Invalid state tranition from %x to %x\n", SecureBootModeTypeSetupMode
, NewMode
));
1161 This function performs main secure boot mode transition logic.
1163 @param[in] CurMode Current Secure Boot Mode.
1164 @param[in] NewMode New Secure Boot Mode.
1166 @retval EFI_SUCCESS The initialization operation is successful.
1167 @retval EFI_OUT_OF_RESOURCES There is not enough resource.
1168 @retval EFI_INVALID_PARAMETER The Current Secure Boot Mode is wrong.
1172 SecureBootModeTransition(
1173 IN SECURE_BOOT_MODE_TYPE CurMode
,
1174 IN SECURE_BOOT_MODE_TYPE NewMode
1180 // SecureBootMode transition
1183 case SecureBootModeTypeUserMode
:
1184 Status
= TransitionFromUserMode(NewMode
);
1187 case SecureBootModeTypeSetupMode
:
1188 Status
= TransitionFromSetupMode(NewMode
);
1191 case SecureBootModeTypeAuditMode
:
1192 Status
= TransitionFromAuditMode(NewMode
);
1195 case SecureBootModeTypeDeployedMode
:
1196 Status
= TransitionFromDeployedMode(NewMode
);
1200 Status
= EFI_INVALID_PARAMETER
;
1209 Determine whether this operation needs a physical present user.
1211 @param[in] VariableName Name of the Variable.
1212 @param[in] VendorGuid GUID of the Variable.
1214 @retval TRUE This variable is protected, only a physical present user could set this variable.
1215 @retval FALSE This variable is not protected.
1219 NeedPhysicallyPresent(
1220 IN CHAR16
*VariableName
,
1221 IN EFI_GUID
*VendorGuid
1224 if ((CompareGuid (VendorGuid
, &gEfiSecureBootEnableDisableGuid
) && (StrCmp (VariableName
, EFI_SECURE_BOOT_ENABLE_NAME
) == 0))
1225 || (CompareGuid (VendorGuid
, &gEfiCustomModeEnableGuid
) && (StrCmp (VariableName
, EFI_CUSTOM_MODE_NAME
) == 0))) {
1233 Determine whether the platform is operating in Custom Secure Boot mode.
1235 @retval TRUE The platform is operating in Custom mode.
1236 @retval FALSE The platform is operating in Standard mode.
1248 Status
= AuthServiceInternalFindVariable (EFI_CUSTOM_MODE_NAME
, &gEfiCustomModeEnableGuid
, &Data
, &DataSize
);
1249 if (!EFI_ERROR (Status
) && (*(UINT8
*) Data
== CUSTOM_SECURE_BOOT_MODE
)) {
1257 Get available public key index.
1259 @param[in] PubKey Pointer to Public Key data.
1261 @return Public key index, 0 if no any public key index available.
1265 GetAvailableKeyIndex (
1275 EFI_GUID VendorGuid
;
1277 AUTH_VARIABLE_INFO AuthVariableInfo
;
1280 Status
= AuthServiceInternalFindVariable (
1282 &gEfiAuthenticatedVariableGuid
,
1286 if (EFI_ERROR (Status
)) {
1287 DEBUG ((EFI_D_ERROR
, "Get public key database variable failure, Status = %r\n", Status
));
1291 if (mPubKeyNumber
== mMaxKeyNumber
) {
1293 AuthVariableInfo
.VariableName
= Name
;
1294 ZeroMem (&VendorGuid
, sizeof (VendorGuid
));
1295 AuthVariableInfo
.VendorGuid
= &VendorGuid
;
1298 // Collect valid key data.
1301 Status
= mAuthVarLibContextIn
->FindNextVariable (AuthVariableInfo
.VariableName
, AuthVariableInfo
.VendorGuid
, &AuthVariableInfo
);
1302 if (!EFI_ERROR (Status
)) {
1303 if (AuthVariableInfo
.PubKeyIndex
!= 0) {
1304 for (Ptr
= Data
; Ptr
< (Data
+ DataSize
); Ptr
+= sizeof (AUTHVAR_KEY_DB_DATA
)) {
1305 if (ReadUnaligned32 (&(((AUTHVAR_KEY_DB_DATA
*) Ptr
)->KeyIndex
)) == AuthVariableInfo
.PubKeyIndex
) {
1307 // Check if the key data has been collected.
1309 for (Index
= 0; Index
< mPubKeyNumber
; Index
++) {
1310 if (ReadUnaligned32 (&(((AUTHVAR_KEY_DB_DATA
*) mPubKeyStore
+ Index
)->KeyIndex
)) == AuthVariableInfo
.PubKeyIndex
) {
1314 if (Index
== mPubKeyNumber
) {
1318 CopyMem ((AUTHVAR_KEY_DB_DATA
*) mPubKeyStore
+ mPubKeyNumber
, Ptr
, sizeof (AUTHVAR_KEY_DB_DATA
));
1326 } while (Status
!= EFI_NOT_FOUND
);
1329 // No available space to add new public key.
1331 if (mPubKeyNumber
== mMaxKeyNumber
) {
1337 // Find available public key index.
1339 for (KeyIndex
= 1; KeyIndex
<= mMaxKeyNumber
; KeyIndex
++) {
1341 for (Ptr
= mPubKeyStore
; Ptr
< (mPubKeyStore
+ mPubKeyNumber
* sizeof (AUTHVAR_KEY_DB_DATA
)); Ptr
+= sizeof (AUTHVAR_KEY_DB_DATA
)) {
1342 if (ReadUnaligned32 (&(((AUTHVAR_KEY_DB_DATA
*) Ptr
)->KeyIndex
)) == KeyIndex
) {
1356 Add public key in store and return its index.
1358 @param[in] PubKey Input pointer to Public Key data.
1359 @param[in] VariableDataEntry The variable data entry.
1361 @return Index of new added public key.
1367 IN VARIABLE_ENTRY_CONSISTENCY
*VariableDataEntry
1372 VARIABLE_ENTRY_CONSISTENCY PublicKeyEntry
;
1376 if (PubKey
== NULL
) {
1381 // Check whether the public key entry does exist.
1383 for (Index
= 0; Index
< mPubKeyNumber
; Index
++) {
1384 if (CompareMem (((AUTHVAR_KEY_DB_DATA
*) mPubKeyStore
+ Index
)->KeyData
, PubKey
, EFI_CERT_TYPE_RSA2048_SIZE
) == 0) {
1385 return ReadUnaligned32 (&(((AUTHVAR_KEY_DB_DATA
*) mPubKeyStore
+ Index
)->KeyIndex
));
1389 KeyIndex
= GetAvailableKeyIndex (PubKey
);
1390 if (KeyIndex
== 0) {
1395 // Check the variable space for both public key and variable data.
1397 PublicKeyEntry
.VariableSize
= (mPubKeyNumber
+ 1) * sizeof (AUTHVAR_KEY_DB_DATA
);
1398 PublicKeyEntry
.Guid
= &gEfiAuthenticatedVariableGuid
;
1399 PublicKeyEntry
.Name
= AUTHVAR_KEYDB_NAME
;
1400 Attributes
= VARIABLE_ATTRIBUTE_NV_BS_RT
| EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
;
1402 if (!mAuthVarLibContextIn
->CheckRemainingSpaceForConsistency (Attributes
, &PublicKeyEntry
, VariableDataEntry
, NULL
)) {
1404 // No enough variable space.
1409 WriteUnaligned32 (&(((AUTHVAR_KEY_DB_DATA
*) mPubKeyStore
+ mPubKeyNumber
)->KeyIndex
), KeyIndex
);
1410 CopyMem (((AUTHVAR_KEY_DB_DATA
*) mPubKeyStore
+ mPubKeyNumber
)->KeyData
, PubKey
, EFI_CERT_TYPE_RSA2048_SIZE
);
1414 // Update public key database variable.
1416 Status
= AuthServiceInternalUpdateVariable (
1418 &gEfiAuthenticatedVariableGuid
,
1420 mPubKeyNumber
* sizeof (AUTHVAR_KEY_DB_DATA
),
1423 if (EFI_ERROR (Status
)) {
1424 DEBUG ((EFI_D_ERROR
, "Update public key database variable failure, Status = %r\n", Status
));
1432 Verify data payload with AuthInfo in EFI_CERT_TYPE_RSA2048_SHA256_GUID type.
1433 Follow the steps in UEFI2.2.
1435 Caution: This function may receive untrusted input.
1436 This function may be invoked in SMM mode, and datasize and data are external input.
1437 This function will do basic validation, before parse the data.
1438 This function will parse the authentication carefully to avoid security issues, like
1439 buffer overflow, integer overflow.
1441 @param[in] Data Pointer to data with AuthInfo.
1442 @param[in] DataSize Size of Data.
1443 @param[in] PubKey Public key used for verification.
1445 @retval EFI_INVALID_PARAMETER Invalid parameter.
1446 @retval EFI_SECURITY_VIOLATION If authentication failed.
1447 @retval EFI_SUCCESS Authentication successful.
1451 VerifyCounterBasedPayload (
1458 EFI_VARIABLE_AUTHENTICATION
*CertData
;
1459 EFI_CERT_BLOCK_RSA_2048_SHA256
*CertBlock
;
1460 UINT8 Digest
[SHA256_DIGEST_SIZE
];
1464 PayloadSize
= DataSize
- AUTHINFO_SIZE
;
1469 if (Data
== NULL
|| PubKey
== NULL
) {
1470 return EFI_INVALID_PARAMETER
;
1473 CertData
= (EFI_VARIABLE_AUTHENTICATION
*) Data
;
1474 CertBlock
= (EFI_CERT_BLOCK_RSA_2048_SHA256
*) (CertData
->AuthInfo
.CertData
);
1477 // wCertificateType should be WIN_CERT_TYPE_EFI_GUID.
1478 // Cert type should be EFI_CERT_TYPE_RSA2048_SHA256_GUID.
1480 if ((CertData
->AuthInfo
.Hdr
.wCertificateType
!= WIN_CERT_TYPE_EFI_GUID
) ||
1481 !CompareGuid (&CertData
->AuthInfo
.CertType
, &gEfiCertTypeRsa2048Sha256Guid
)) {
1483 // Invalid AuthInfo type, return EFI_SECURITY_VIOLATION.
1485 return EFI_SECURITY_VIOLATION
;
1488 // Hash data payload with SHA256.
1490 ZeroMem (Digest
, SHA256_DIGEST_SIZE
);
1491 Status
= Sha256Init (mHashCtx
);
1495 Status
= Sha256Update (mHashCtx
, Data
+ AUTHINFO_SIZE
, PayloadSize
);
1502 Status
= Sha256Update (mHashCtx
, &PayloadSize
, sizeof (UINTN
));
1507 // Hash Monotonic Count.
1509 Status
= Sha256Update (mHashCtx
, &CertData
->MonotonicCount
, sizeof (UINT64
));
1513 Status
= Sha256Final (mHashCtx
, Digest
);
1518 // Generate & Initialize RSA Context.
1521 ASSERT (Rsa
!= NULL
);
1523 // Set RSA Key Components.
1524 // NOTE: Only N and E are needed to be set as RSA public key for signature verification.
1526 Status
= RsaSetKey (Rsa
, RsaKeyN
, PubKey
, EFI_CERT_TYPE_RSA2048_SIZE
);
1530 Status
= RsaSetKey (Rsa
, RsaKeyE
, mRsaE
, sizeof (mRsaE
));
1535 // Verify the signature.
1537 Status
= RsaPkcs1Verify (
1541 CertBlock
->Signature
,
1542 EFI_CERT_TYPE_RSA2048_SHA256_SIZE
1552 return EFI_SECURITY_VIOLATION
;
1558 Check input data form to make sure it is a valid EFI_SIGNATURE_LIST for PK/KEK/db/dbx/dbt variable.
1560 @param[in] VariableName Name of Variable to be check.
1561 @param[in] VendorGuid Variable vendor GUID.
1562 @param[in] Data Point to the variable data to be checked.
1563 @param[in] DataSize Size of Data.
1565 @return EFI_INVALID_PARAMETER Invalid signature list format.
1566 @return EFI_SUCCESS Passed signature list format check successfully.
1570 CheckSignatureListFormat(
1571 IN CHAR16
*VariableName
,
1572 IN EFI_GUID
*VendorGuid
,
1577 EFI_SIGNATURE_LIST
*SigList
;
1583 EFI_SIGNATURE_DATA
*CertData
;
1586 if (DataSize
== 0) {
1590 ASSERT (VariableName
!= NULL
&& VendorGuid
!= NULL
&& Data
!= NULL
);
1592 if (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, EFI_PLATFORM_KEY_NAME
) == 0)){
1594 } else if ((CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, EFI_KEY_EXCHANGE_KEY_NAME
) == 0)) ||
1595 (CompareGuid (VendorGuid
, &gEfiImageSecurityDatabaseGuid
) &&
1596 ((StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE
) == 0) || (StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE1
) == 0) ||
1597 (StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE2
) == 0)))) {
1604 SigList
= (EFI_SIGNATURE_LIST
*) Data
;
1605 SigDataSize
= DataSize
;
1609 // Walk throuth the input signature list and check the data format.
1610 // If any signature is incorrectly formed, the whole check will fail.
1612 while ((SigDataSize
> 0) && (SigDataSize
>= SigList
->SignatureListSize
)) {
1613 for (Index
= 0; Index
< (sizeof (mSupportSigItem
) / sizeof (EFI_SIGNATURE_ITEM
)); Index
++ ) {
1614 if (CompareGuid (&SigList
->SignatureType
, &mSupportSigItem
[Index
].SigType
)) {
1616 // The value of SignatureSize should always be 16 (size of SignatureOwner
1617 // component) add the data length according to signature type.
1619 if (mSupportSigItem
[Index
].SigDataSize
!= ((UINT32
) ~0) &&
1620 (SigList
->SignatureSize
- sizeof (EFI_GUID
)) != mSupportSigItem
[Index
].SigDataSize
) {
1621 return EFI_INVALID_PARAMETER
;
1623 if (mSupportSigItem
[Index
].SigHeaderSize
!= ((UINT32
) ~0) &&
1624 SigList
->SignatureHeaderSize
!= mSupportSigItem
[Index
].SigHeaderSize
) {
1625 return EFI_INVALID_PARAMETER
;
1631 if (Index
== (sizeof (mSupportSigItem
) / sizeof (EFI_SIGNATURE_ITEM
))) {
1633 // Undefined signature type.
1635 return EFI_INVALID_PARAMETER
;
1638 if (CompareGuid (&SigList
->SignatureType
, &gEfiCertX509Guid
)) {
1640 // Try to retrieve the RSA public key from the X.509 certificate.
1641 // If this operation fails, it's not a valid certificate.
1643 RsaContext
= RsaNew ();
1644 if (RsaContext
== NULL
) {
1645 return EFI_INVALID_PARAMETER
;
1647 CertData
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) SigList
+ sizeof (EFI_SIGNATURE_LIST
) + SigList
->SignatureHeaderSize
);
1648 CertLen
= SigList
->SignatureSize
- sizeof (EFI_GUID
);
1649 if (!RsaGetPublicKeyFromX509 (CertData
->SignatureData
, CertLen
, &RsaContext
)) {
1650 RsaFree (RsaContext
);
1651 return EFI_INVALID_PARAMETER
;
1653 RsaFree (RsaContext
);
1656 if ((SigList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - SigList
->SignatureHeaderSize
) % SigList
->SignatureSize
!= 0) {
1657 return EFI_INVALID_PARAMETER
;
1659 SigCount
+= (SigList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - SigList
->SignatureHeaderSize
) / SigList
->SignatureSize
;
1661 SigDataSize
-= SigList
->SignatureListSize
;
1662 SigList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) SigList
+ SigList
->SignatureListSize
);
1665 if (((UINTN
) SigList
- (UINTN
) Data
) != DataSize
) {
1666 return EFI_INVALID_PARAMETER
;
1669 if (IsPk
&& SigCount
> 1) {
1670 return EFI_INVALID_PARAMETER
;
1677 Update "VendorKeys" variable to record the out of band secure boot key modification.
1679 @return EFI_SUCCESS Variable is updated successfully.
1680 @return Others Failed to update variable.
1684 VendorKeyIsModified (
1690 if (mVendorKeyState
== VENDOR_KEYS_MODIFIED
) {
1693 mVendorKeyState
= VENDOR_KEYS_MODIFIED
;
1695 Status
= AuthServiceInternalUpdateVariable (
1696 EFI_VENDOR_KEYS_NV_VARIABLE_NAME
,
1697 &gEfiVendorKeysNvGuid
,
1700 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
1702 if (EFI_ERROR (Status
)) {
1706 return AuthServiceInternalUpdateVariable (
1707 EFI_VENDOR_KEYS_VARIABLE_NAME
,
1708 &gEfiGlobalVariableGuid
,
1711 EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
1716 Process Secure Boot Mode variable.
1718 Caution: This function may receive untrusted input.
1719 This function may be invoked in SMM mode, and datasize and data are external input.
1720 This function will do basic validation, before parse the data.
1721 This function will parse the authentication carefully to avoid security issues, like
1722 buffer overflow, integer overflow.
1723 This function will check attribute carefully to avoid authentication bypass.
1725 @param[in] VariableName Name of Variable to be found.
1726 @param[in] VendorGuid Variable vendor GUID.
1727 @param[in] Data Data pointer.
1728 @param[in] DataSize Size of Data found. If size is less than the
1729 data, this value contains the required size.
1730 @param[in] Attributes Attribute value of the variable
1732 @return EFI_INVALID_PARAMETER Invalid parameter
1733 @return EFI_SECURITY_VIOLATION The variable does NOT pass the validation
1734 check carried out by the firmware.
1735 @return EFI_WRITE_PROTECTED Variable is Read-Only.
1736 @return EFI_SUCCESS Variable passed validation successfully.
1740 ProcessSecureBootModeVar (
1741 IN CHAR16
*VariableName
,
1742 IN EFI_GUID
*VendorGuid
,
1745 IN UINT32 Attributes OPTIONAL
1753 // Check "AuditMode", "DeployedMode" Variable ReadWrite Attributes
1754 // if in Runtime, Always RO
1755 // if in Boottime, Depends on current Secure Boot Mode
1757 if (mAuthVarLibContextIn
->AtRuntime()) {
1758 return EFI_WRITE_PROTECTED
;
1764 if ((DataSize
!= sizeof(UINT8
)) || (Attributes
== 0)) {
1765 return EFI_INVALID_PARAMETER
;
1768 if (StrCmp (VariableName
, EFI_AUDIT_MODE_NAME
) == 0) {
1769 if(mSecureBootState
[mSecureBootMode
].IsAuditModeRO
) {
1770 return EFI_WRITE_PROTECTED
;
1774 // Platform specific deployedMode clear. Set DeployedMode = RW
1776 if (!InCustomMode() || !UserPhysicalPresent() || mSecureBootMode
!= SecureBootModeTypeDeployedMode
) {
1777 if(mSecureBootState
[mSecureBootMode
].IsDeployedModeRO
) {
1778 return EFI_WRITE_PROTECTED
;
1783 if (*(UINT8
*)Data
!= 0 && *(UINT8
*)Data
!= 1) {
1784 return EFI_INVALID_PARAMETER
;
1788 // AuditMode/DeployedMode/SetupMode/SecureBoot are all NON_NV variable maintained by Variable driver
1789 // they can be RW. but can't be deleted. so they can always be found.
1791 Status
= AuthServiceInternalFindVariable (
1797 if (EFI_ERROR(Status
)) {
1802 // If AuditMode/DeployedMode is assigned same value. Simply return EFI_SUCCESS
1804 if (*(UINT8
*)VarData
== *(UINT8
*)Data
) {
1809 // Perform SecureBootMode transition
1811 if (StrCmp (VariableName
, EFI_AUDIT_MODE_NAME
) == 0) {
1812 DEBUG((EFI_D_INFO
, "Current SecureBootMode %x Transfer to SecureBootMode %x\n", mSecureBootMode
, SecureBootModeTypeAuditMode
));
1813 return SecureBootModeTransition(mSecureBootMode
, SecureBootModeTypeAuditMode
);
1814 } else if (StrCmp (VariableName
, EFI_DEPLOYED_MODE_NAME
) == 0) {
1815 if (mSecureBootMode
== SecureBootModeTypeDeployedMode
) {
1817 // Platform specific DeployedMode clear. InCustomMode() && UserPhysicalPresent() is checked before
1819 DEBUG((EFI_D_INFO
, "Current SecureBootMode %x. Transfer to SecureBootMode %x\n", mSecureBootMode
, SecureBootModeTypeUserMode
));
1820 return SecureBootModeTransition(mSecureBootMode
, SecureBootModeTypeUserMode
);
1822 DEBUG((EFI_D_INFO
, "Current SecureBootMode %x. Transfer to SecureBootMode %x\n", mSecureBootMode
, SecureBootModeTypeDeployedMode
));
1823 return SecureBootModeTransition(mSecureBootMode
, SecureBootModeTypeDeployedMode
);
1827 return EFI_INVALID_PARAMETER
;
1831 Process variable with platform key for verification.
1833 Caution: This function may receive untrusted input.
1834 This function may be invoked in SMM mode, and datasize and data are external input.
1835 This function will do basic validation, before parse the data.
1836 This function will parse the authentication carefully to avoid security issues, like
1837 buffer overflow, integer overflow.
1838 This function will check attribute carefully to avoid authentication bypass.
1840 @param[in] VariableName Name of Variable to be found.
1841 @param[in] VendorGuid Variable vendor GUID.
1842 @param[in] Data Data pointer.
1843 @param[in] DataSize Size of Data found. If size is less than the
1844 data, this value contains the required size.
1845 @param[in] Attributes Attribute value of the variable
1846 @param[in] IsPk Indicate whether it is to process pk.
1848 @return EFI_INVALID_PARAMETER Invalid parameter.
1849 @return EFI_SECURITY_VIOLATION The variable does NOT pass the validation.
1850 check carried out by the firmware.
1851 @return EFI_SUCCESS Variable passed validation successfully.
1856 IN CHAR16
*VariableName
,
1857 IN EFI_GUID
*VendorGuid
,
1860 IN UINT32 Attributes OPTIONAL
,
1868 VARIABLE_ENTRY_CONSISTENCY VariableEntry
[2];
1870 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0 ||
1871 (Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) == 0) {
1873 // PK, KEK and db/dbx/dbt should set EFI_VARIABLE_NON_VOLATILE attribute and should be a time-based
1874 // authenticated variable.
1876 return EFI_INVALID_PARAMETER
;
1880 // Init state of Del. State may change due to secure check
1883 Payload
= (UINT8
*) Data
+ AUTHINFO2_SIZE (Data
);
1884 PayloadSize
= DataSize
- AUTHINFO2_SIZE (Data
);
1885 if (PayloadSize
== 0) {
1890 // Check the variable space for both PKpub and SecureBootMode variable.
1892 VariableEntry
[0].VariableSize
= PayloadSize
;
1893 VariableEntry
[0].Guid
= &gEfiGlobalVariableGuid
;
1894 VariableEntry
[0].Name
= EFI_PLATFORM_KEY_NAME
;
1896 VariableEntry
[1].VariableSize
= sizeof(UINT8
);
1897 VariableEntry
[1].Guid
= &gEdkiiSecureBootModeGuid
;
1898 VariableEntry
[1].Name
= EDKII_SECURE_BOOT_MODE_NAME
;
1900 if ((InCustomMode() && UserPhysicalPresent()) ||
1901 (((mSecureBootMode
== SecureBootModeTypeSetupMode
) || (mSecureBootMode
== SecureBootModeTypeAuditMode
)) && !IsPk
)) {
1903 Status
= CheckSignatureListFormat(VariableName
, VendorGuid
, Payload
, PayloadSize
);
1904 if (EFI_ERROR (Status
)) {
1909 // If delete PKpub, only check for "SecureBootMode" only
1910 // if update / add PKpub, check both NewPKpub & "SecureBootMode"
1916 if (Del
&& ((mSecureBootMode
== SecureBootModeTypeUserMode
) || (mSecureBootMode
== SecureBootModeTypeDeployedMode
))
1917 && !mAuthVarLibContextIn
->CheckRemainingSpaceForConsistency (VARIABLE_ATTRIBUTE_NV_BS_RT
, &VariableEntry
[1], NULL
)){
1918 return EFI_OUT_OF_RESOURCES
;
1922 } else if (!Del
&& ((mSecureBootMode
== SecureBootModeTypeSetupMode
) || (mSecureBootMode
== SecureBootModeTypeAuditMode
))
1923 && !mAuthVarLibContextIn
->CheckRemainingSpaceForConsistency (VARIABLE_ATTRIBUTE_NV_BS_RT
, &VariableEntry
[0], &VariableEntry
[1], NULL
)) {
1924 return EFI_OUT_OF_RESOURCES
;
1928 Status
= AuthServiceInternalUpdateVariableWithTimeStamp (
1934 &((EFI_VARIABLE_AUTHENTICATION_2
*) Data
)->TimeStamp
1936 if (EFI_ERROR(Status
)) {
1940 if (((mSecureBootMode
!= SecureBootModeTypeSetupMode
) && (mSecureBootMode
!= SecureBootModeTypeAuditMode
)) || IsPk
) {
1941 Status
= VendorKeyIsModified ();
1943 } else if (mSecureBootMode
== SecureBootModeTypeUserMode
|| mSecureBootMode
== SecureBootModeTypeDeployedMode
) {
1945 // If delete PKpub, check "SecureBootMode" only
1947 if (IsPk
&& Del
&& !mAuthVarLibContextIn
->CheckRemainingSpaceForConsistency (VARIABLE_ATTRIBUTE_NV_BS_RT
, &VariableEntry
[1], NULL
)){
1948 return EFI_OUT_OF_RESOURCES
;
1952 // Verify against X509 Cert in PK database.
1954 Status
= VerifyTimeBasedPayloadAndUpdate (
1965 // SetupMode or AuditMode to add PK
1966 // Verify against the certificate in data payload.
1969 // Check PKpub & SecureBootMode variable space consistency
1971 if (!mAuthVarLibContextIn
->CheckRemainingSpaceForConsistency (VARIABLE_ATTRIBUTE_NV_BS_RT
, &VariableEntry
[0], &VariableEntry
[1], NULL
)) {
1973 // No enough variable space to set PK successfully.
1975 return EFI_OUT_OF_RESOURCES
;
1978 Status
= VerifyTimeBasedPayloadAndUpdate (
1989 if (!EFI_ERROR(Status
) && IsPk
) {
1991 // Delete or Enroll PK causes SecureBootMode change
1994 if (mSecureBootMode
== SecureBootModeTypeSetupMode
) {
1996 // If enroll PK in setup mode, change to user mode.
1998 Status
= SecureBootModeTransition (mSecureBootMode
, SecureBootModeTypeUserMode
);
1999 } else if (mSecureBootMode
== SecureBootModeTypeAuditMode
) {
2001 // If enroll PK in Audit mode, change to Deployed mode.
2003 Status
= SecureBootModeTransition (mSecureBootMode
, SecureBootModeTypeDeployedMode
);
2005 DEBUG((EFI_D_INFO
, "PK is updated in %x mode. No SecureBootMode change.\n", mSecureBootMode
));
2008 if ((mSecureBootMode
== SecureBootModeTypeUserMode
) || (mSecureBootMode
== SecureBootModeTypeDeployedMode
)) {
2010 // If delete PK in User Mode or DeployedMode, change to Setup Mode.
2012 Status
= SecureBootModeTransition (mSecureBootMode
, SecureBootModeTypeSetupMode
);
2021 Process variable with key exchange key for verification.
2023 Caution: This function may receive untrusted input.
2024 This function may be invoked in SMM mode, and datasize and data are external input.
2025 This function will do basic validation, before parse the data.
2026 This function will parse the authentication carefully to avoid security issues, like
2027 buffer overflow, integer overflow.
2028 This function will check attribute carefully to avoid authentication bypass.
2030 @param[in] VariableName Name of Variable to be found.
2031 @param[in] VendorGuid Variable vendor GUID.
2032 @param[in] Data Data pointer.
2033 @param[in] DataSize Size of Data found. If size is less than the
2034 data, this value contains the required size.
2035 @param[in] Attributes Attribute value of the variable.
2037 @return EFI_INVALID_PARAMETER Invalid parameter.
2038 @return EFI_SECURITY_VIOLATION The variable does NOT pass the validation
2039 check carried out by the firmware.
2040 @return EFI_SUCCESS Variable pass validation successfully.
2045 IN CHAR16
*VariableName
,
2046 IN EFI_GUID
*VendorGuid
,
2049 IN UINT32 Attributes OPTIONAL
2056 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0 ||
2057 (Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) == 0) {
2059 // DB, DBX and DBT should set EFI_VARIABLE_NON_VOLATILE attribute and should be a time-based
2060 // authenticated variable.
2062 return EFI_INVALID_PARAMETER
;
2065 Status
= EFI_SUCCESS
;
2066 if ((mSecureBootMode
== SecureBootModeTypeUserMode
|| mSecureBootMode
== SecureBootModeTypeDeployedMode
)
2067 && !(InCustomMode() && UserPhysicalPresent())) {
2069 // Time-based, verify against X509 Cert KEK.
2071 return VerifyTimeBasedPayloadAndUpdate (
2082 // If in setup mode or custom secure boot mode, no authentication needed.
2084 Payload
= (UINT8
*) Data
+ AUTHINFO2_SIZE (Data
);
2085 PayloadSize
= DataSize
- AUTHINFO2_SIZE (Data
);
2087 Status
= CheckSignatureListFormat(VariableName
, VendorGuid
, Payload
, PayloadSize
);
2088 if (EFI_ERROR (Status
)) {
2092 Status
= AuthServiceInternalUpdateVariableWithTimeStamp (
2098 &((EFI_VARIABLE_AUTHENTICATION_2
*) Data
)->TimeStamp
2100 if (EFI_ERROR (Status
)) {
2104 if ((mSecureBootMode
!= SecureBootModeTypeSetupMode
) && (mSecureBootMode
!= SecureBootModeTypeAuditMode
)) {
2105 Status
= VendorKeyIsModified ();
2113 Check if it is to delete auth variable.
2115 @param[in] OrgAttributes Original attribute value of the variable.
2116 @param[in] Data Data pointer.
2117 @param[in] DataSize Size of Data.
2118 @param[in] Attributes Attribute value of the variable.
2120 @retval TRUE It is to delete auth variable.
2121 @retval FALSE It is not to delete auth variable.
2125 IsDeleteAuthVariable (
2126 IN UINT32 OrgAttributes
,
2129 IN UINT32 Attributes
2138 // To delete a variable created with the EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
2139 // or the EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS attribute,
2140 // SetVariable must be used with attributes matching the existing variable
2141 // and the DataSize set to the size of the AuthInfo descriptor.
2143 if ((Attributes
== OrgAttributes
) &&
2144 ((Attributes
& (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
)) != 0)) {
2145 if ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) {
2146 PayloadSize
= DataSize
- AUTHINFO2_SIZE (Data
);
2147 if (PayloadSize
== 0) {
2151 PayloadSize
= DataSize
- AUTHINFO_SIZE
;
2152 if (PayloadSize
== 0) {
2162 Process variable with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS/EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set
2164 Caution: This function may receive untrusted input.
2165 This function may be invoked in SMM mode, and datasize and data are external input.
2166 This function will do basic validation, before parse the data.
2167 This function will parse the authentication carefully to avoid security issues, like
2168 buffer overflow, integer overflow.
2169 This function will check attribute carefully to avoid authentication bypass.
2171 @param[in] VariableName Name of the variable.
2172 @param[in] VendorGuid Variable vendor GUID.
2173 @param[in] Data Data pointer.
2174 @param[in] DataSize Size of Data.
2175 @param[in] Attributes Attribute value of the variable.
2177 @return EFI_INVALID_PARAMETER Invalid parameter.
2178 @return EFI_WRITE_PROTECTED Variable is write-protected and needs authentication with
2179 EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS set.
2180 @return EFI_OUT_OF_RESOURCES The Database to save the public key is full.
2181 @return EFI_SECURITY_VIOLATION The variable is with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
2182 set, but the AuthInfo does NOT pass the validation
2183 check carried out by the firmware.
2184 @return EFI_SUCCESS Variable is not write-protected or pass validation successfully.
2189 IN CHAR16
*VariableName
,
2190 IN EFI_GUID
*VendorGuid
,
2193 IN UINT32 Attributes OPTIONAL
2198 BOOLEAN IsFirstTime
;
2200 EFI_VARIABLE_AUTHENTICATION
*CertData
;
2201 EFI_CERT_BLOCK_RSA_2048_SHA256
*CertBlock
;
2203 UINT64 MonotonicCount
;
2204 VARIABLE_ENTRY_CONSISTENCY VariableDataEntry
;
2206 AUTH_VARIABLE_INFO OrgVariableInfo
;
2213 Status
= EFI_SUCCESS
;
2215 ZeroMem (&OrgVariableInfo
, sizeof (OrgVariableInfo
));
2216 Status
= mAuthVarLibContextIn
->FindVariable (
2222 if ((!EFI_ERROR (Status
)) && IsDeleteAuthVariable (OrgVariableInfo
.Attributes
, Data
, DataSize
, Attributes
) && UserPhysicalPresent()) {
2224 // Allow the delete operation of common authenticated variable at user physical presence.
2226 Status
= AuthServiceInternalUpdateVariable (
2233 if (!EFI_ERROR (Status
) && ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0)) {
2234 Status
= DeleteCertsFromDb (VariableName
, VendorGuid
);
2240 if (NeedPhysicallyPresent (VariableName
, VendorGuid
) && !UserPhysicalPresent()) {
2242 // This variable is protected, only physical present user could modify its value.
2244 return EFI_SECURITY_VIOLATION
;
2248 // A time-based authenticated variable and a count-based authenticated variable
2249 // can't be updated by each other.
2251 if (OrgVariableInfo
.Data
!= NULL
) {
2252 if (((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) &&
2253 ((OrgVariableInfo
.Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0)) {
2254 return EFI_SECURITY_VIOLATION
;
2257 if (((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) &&
2258 ((OrgVariableInfo
.Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0)) {
2259 return EFI_SECURITY_VIOLATION
;
2264 // Process Time-based Authenticated variable.
2266 if ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) {
2267 return VerifyTimeBasedPayloadAndUpdate (
2279 // Determine if first time SetVariable with the EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS.
2281 if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) {
2283 // Determine current operation type.
2285 if (DataSize
== AUTHINFO_SIZE
) {
2289 // Determine whether this is the first time with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS set.
2291 if (OrgVariableInfo
.Data
== NULL
) {
2293 } else if ((OrgVariableInfo
.Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) == 0) {
2296 KeyIndex
= OrgVariableInfo
.PubKeyIndex
;
2297 IsFirstTime
= FALSE
;
2299 } else if ((OrgVariableInfo
.Data
!= NULL
) &&
2300 ((OrgVariableInfo
.Attributes
& (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
)) != 0)
2303 // If the variable is already write-protected, it always needs authentication before update.
2305 return EFI_WRITE_PROTECTED
;
2308 // If without EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS, set and attributes collision.
2309 // That means it is not authenticated variable, just update variable as usual.
2311 Status
= AuthServiceInternalUpdateVariable (VariableName
, VendorGuid
, Data
, DataSize
, Attributes
);
2316 // Get PubKey and check Monotonic Count value corresponding to the variable.
2318 CertData
= (EFI_VARIABLE_AUTHENTICATION
*) Data
;
2319 CertBlock
= (EFI_CERT_BLOCK_RSA_2048_SHA256
*) (CertData
->AuthInfo
.CertData
);
2320 PubKey
= CertBlock
->PublicKey
;
2323 // Update Monotonic Count value.
2325 MonotonicCount
= CertData
->MonotonicCount
;
2329 // 2 cases need to check here
2330 // 1. Internal PubKey variable. PubKeyIndex is always 0
2331 // 2. Other counter-based AuthVariable. Check input PubKey.
2333 if (KeyIndex
== 0) {
2334 return EFI_SECURITY_VIOLATION
;
2336 for (Index
= 0; Index
< mPubKeyNumber
; Index
++) {
2337 if (ReadUnaligned32 (&(((AUTHVAR_KEY_DB_DATA
*) mPubKeyStore
+ Index
)->KeyIndex
)) == KeyIndex
) {
2338 if (CompareMem (((AUTHVAR_KEY_DB_DATA
*) mPubKeyStore
+ Index
)->KeyData
, PubKey
, EFI_CERT_TYPE_RSA2048_SIZE
) == 0) {
2341 return EFI_SECURITY_VIOLATION
;
2345 if (Index
== mPubKeyNumber
) {
2346 return EFI_SECURITY_VIOLATION
;
2350 // Compare the current monotonic count and ensure that it is greater than the last SetVariable
2351 // operation with the EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS attribute set.
2353 if (MonotonicCount
<= OrgVariableInfo
.MonotonicCount
) {
2355 // Monotonic count check fail, suspicious replay attack, return EFI_SECURITY_VIOLATION.
2357 return EFI_SECURITY_VIOLATION
;
2361 // Verify the certificate in Data payload.
2363 Status
= VerifyCounterBasedPayload (Data
, DataSize
, PubKey
);
2364 if (EFI_ERROR (Status
)) {
2369 // Now, the signature has been verified!
2371 if (IsFirstTime
&& !IsDeletion
) {
2372 VariableDataEntry
.VariableSize
= DataSize
- AUTHINFO_SIZE
;
2373 VariableDataEntry
.Guid
= VendorGuid
;
2374 VariableDataEntry
.Name
= VariableName
;
2377 // Update public key database variable if need.
2379 KeyIndex
= AddPubKeyInStore (PubKey
, &VariableDataEntry
);
2380 if (KeyIndex
== 0) {
2381 return EFI_OUT_OF_RESOURCES
;
2386 // Verification pass.
2388 return AuthServiceInternalUpdateVariableWithMonotonicCount (VariableName
, VendorGuid
, (UINT8
*)Data
+ AUTHINFO_SIZE
, DataSize
- AUTHINFO_SIZE
, Attributes
, KeyIndex
, MonotonicCount
);
2392 Filter out the duplicated EFI_SIGNATURE_DATA from the new data by comparing to the original data.
2394 @param[in] Data Pointer to original EFI_SIGNATURE_LIST.
2395 @param[in] DataSize Size of Data buffer.
2396 @param[in, out] NewData Pointer to new EFI_SIGNATURE_LIST.
2397 @param[in, out] NewDataSize Size of NewData buffer.
2401 FilterSignatureList (
2404 IN OUT VOID
*NewData
,
2405 IN OUT UINTN
*NewDataSize
2408 EFI_SIGNATURE_LIST
*CertList
;
2409 EFI_SIGNATURE_DATA
*Cert
;
2411 EFI_SIGNATURE_LIST
*NewCertList
;
2412 EFI_SIGNATURE_DATA
*NewCert
;
2419 UINTN SignatureListSize
;
2425 if (*NewDataSize
== 0) {
2429 TempDataSize
= *NewDataSize
;
2430 Status
= mAuthVarLibContextIn
->GetScratchBuffer (&TempDataSize
, (VOID
**) &TempData
);
2431 if (EFI_ERROR (Status
)) {
2432 return EFI_OUT_OF_RESOURCES
;
2437 NewCertList
= (EFI_SIGNATURE_LIST
*) NewData
;
2438 while ((*NewDataSize
> 0) && (*NewDataSize
>= NewCertList
->SignatureListSize
)) {
2439 NewCert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) NewCertList
+ sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
);
2440 NewCertCount
= (NewCertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - NewCertList
->SignatureHeaderSize
) / NewCertList
->SignatureSize
;
2443 for (Index
= 0; Index
< NewCertCount
; Index
++) {
2447 CertList
= (EFI_SIGNATURE_LIST
*) Data
;
2448 while ((Size
> 0) && (Size
>= CertList
->SignatureListSize
)) {
2449 if (CompareGuid (&CertList
->SignatureType
, &NewCertList
->SignatureType
) &&
2450 (CertList
->SignatureSize
== NewCertList
->SignatureSize
)) {
2451 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
2452 CertCount
= (CertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - CertList
->SignatureHeaderSize
) / CertList
->SignatureSize
;
2453 for (Index2
= 0; Index2
< CertCount
; Index2
++) {
2455 // Iterate each Signature Data in this Signature List.
2457 if (CompareMem (NewCert
, Cert
, CertList
->SignatureSize
) == 0) {
2461 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) Cert
+ CertList
->SignatureSize
);
2468 Size
-= CertList
->SignatureListSize
;
2469 CertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) CertList
+ CertList
->SignatureListSize
);
2474 // New EFI_SIGNATURE_DATA, keep it.
2476 if (CopiedCount
== 0) {
2478 // Copy EFI_SIGNATURE_LIST header for only once.
2480 CopyMem (Tail
, NewCertList
, sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
);
2481 Tail
= Tail
+ sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
;
2484 CopyMem (Tail
, NewCert
, NewCertList
->SignatureSize
);
2485 Tail
+= NewCertList
->SignatureSize
;
2489 NewCert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) NewCert
+ NewCertList
->SignatureSize
);
2493 // Update SignatureListSize in the kept EFI_SIGNATURE_LIST.
2495 if (CopiedCount
!= 0) {
2496 SignatureListSize
= sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
+ (CopiedCount
* NewCertList
->SignatureSize
);
2497 CertList
= (EFI_SIGNATURE_LIST
*) (Tail
- SignatureListSize
);
2498 CertList
->SignatureListSize
= (UINT32
) SignatureListSize
;
2501 *NewDataSize
-= NewCertList
->SignatureListSize
;
2502 NewCertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) NewCertList
+ NewCertList
->SignatureListSize
);
2505 TempDataSize
= (Tail
- (UINT8
*) TempData
);
2507 CopyMem (NewData
, TempData
, TempDataSize
);
2508 *NewDataSize
= TempDataSize
;
2514 Compare two EFI_TIME data.
2517 @param FirstTime A pointer to the first EFI_TIME data.
2518 @param SecondTime A pointer to the second EFI_TIME data.
2520 @retval TRUE The FirstTime is not later than the SecondTime.
2521 @retval FALSE The FirstTime is later than the SecondTime.
2525 AuthServiceInternalCompareTimeStamp (
2526 IN EFI_TIME
*FirstTime
,
2527 IN EFI_TIME
*SecondTime
2530 if (FirstTime
->Year
!= SecondTime
->Year
) {
2531 return (BOOLEAN
) (FirstTime
->Year
< SecondTime
->Year
);
2532 } else if (FirstTime
->Month
!= SecondTime
->Month
) {
2533 return (BOOLEAN
) (FirstTime
->Month
< SecondTime
->Month
);
2534 } else if (FirstTime
->Day
!= SecondTime
->Day
) {
2535 return (BOOLEAN
) (FirstTime
->Day
< SecondTime
->Day
);
2536 } else if (FirstTime
->Hour
!= SecondTime
->Hour
) {
2537 return (BOOLEAN
) (FirstTime
->Hour
< SecondTime
->Hour
);
2538 } else if (FirstTime
->Minute
!= SecondTime
->Minute
) {
2539 return (BOOLEAN
) (FirstTime
->Minute
< SecondTime
->Minute
);
2542 return (BOOLEAN
) (FirstTime
->Second
<= SecondTime
->Second
);
2546 Find matching signer's certificates for common authenticated variable
2547 by corresponding VariableName and VendorGuid from "certdb".
2549 The data format of "certdb":
2551 // UINT32 CertDbListSize;
2552 // /// AUTH_CERT_DB_DATA Certs1[];
2553 // /// AUTH_CERT_DB_DATA Certs2[];
2555 // /// AUTH_CERT_DB_DATA Certsn[];
2558 @param[in] VariableName Name of authenticated Variable.
2559 @param[in] VendorGuid Vendor GUID of authenticated Variable.
2560 @param[in] Data Pointer to variable "certdb".
2561 @param[in] DataSize Size of variable "certdb".
2562 @param[out] CertOffset Offset of matching CertData, from starting of Data.
2563 @param[out] CertDataSize Length of CertData in bytes.
2564 @param[out] CertNodeOffset Offset of matching AUTH_CERT_DB_DATA , from
2566 @param[out] CertNodeSize Length of AUTH_CERT_DB_DATA in bytes.
2568 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
2569 @retval EFI_NOT_FOUND Fail to find matching certs.
2570 @retval EFI_SUCCESS Find matching certs and output parameters.
2575 IN CHAR16
*VariableName
,
2576 IN EFI_GUID
*VendorGuid
,
2579 OUT UINT32
*CertOffset
, OPTIONAL
2580 OUT UINT32
*CertDataSize
, OPTIONAL
2581 OUT UINT32
*CertNodeOffset
,OPTIONAL
2582 OUT UINT32
*CertNodeSize OPTIONAL
2586 AUTH_CERT_DB_DATA
*Ptr
;
2590 UINT32 CertDbListSize
;
2592 if ((VariableName
== NULL
) || (VendorGuid
== NULL
) || (Data
== NULL
)) {
2593 return EFI_INVALID_PARAMETER
;
2597 // Check whether DataSize matches recorded CertDbListSize.
2599 if (DataSize
< sizeof (UINT32
)) {
2600 return EFI_INVALID_PARAMETER
;
2603 CertDbListSize
= ReadUnaligned32 ((UINT32
*) Data
);
2605 if (CertDbListSize
!= (UINT32
) DataSize
) {
2606 return EFI_INVALID_PARAMETER
;
2609 Offset
= sizeof (UINT32
);
2612 // Get corresponding certificates by VendorGuid and VariableName.
2614 while (Offset
< (UINT32
) DataSize
) {
2615 Ptr
= (AUTH_CERT_DB_DATA
*) (Data
+ Offset
);
2617 // Check whether VendorGuid matches.
2619 if (CompareGuid (&Ptr
->VendorGuid
, VendorGuid
)) {
2620 NodeSize
= ReadUnaligned32 (&Ptr
->CertNodeSize
);
2621 NameSize
= ReadUnaligned32 (&Ptr
->NameSize
);
2622 CertSize
= ReadUnaligned32 (&Ptr
->CertDataSize
);
2624 if (NodeSize
!= sizeof (EFI_GUID
) + sizeof (UINT32
) * 3 + CertSize
+
2625 sizeof (CHAR16
) * NameSize
) {
2626 return EFI_INVALID_PARAMETER
;
2629 Offset
= Offset
+ sizeof (EFI_GUID
) + sizeof (UINT32
) * 3;
2631 // Check whether VariableName matches.
2633 if ((NameSize
== StrLen (VariableName
)) &&
2634 (CompareMem (Data
+ Offset
, VariableName
, NameSize
* sizeof (CHAR16
)) == 0)) {
2635 Offset
= Offset
+ NameSize
* sizeof (CHAR16
);
2637 if (CertOffset
!= NULL
) {
2638 *CertOffset
= Offset
;
2641 if (CertDataSize
!= NULL
) {
2642 *CertDataSize
= CertSize
;
2645 if (CertNodeOffset
!= NULL
) {
2646 *CertNodeOffset
= (UINT32
) ((UINT8
*) Ptr
- Data
);
2649 if (CertNodeSize
!= NULL
) {
2650 *CertNodeSize
= NodeSize
;
2655 Offset
= Offset
+ NameSize
* sizeof (CHAR16
) + CertSize
;
2658 NodeSize
= ReadUnaligned32 (&Ptr
->CertNodeSize
);
2659 Offset
= Offset
+ NodeSize
;
2663 return EFI_NOT_FOUND
;
2667 Retrieve signer's certificates for common authenticated variable
2668 by corresponding VariableName and VendorGuid from "certdb".
2670 @param[in] VariableName Name of authenticated Variable.
2671 @param[in] VendorGuid Vendor GUID of authenticated Variable.
2672 @param[out] CertData Pointer to signer's certificates.
2673 @param[out] CertDataSize Length of CertData in bytes.
2675 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
2676 @retval EFI_NOT_FOUND Fail to find "certdb" or matching certs.
2677 @retval EFI_SUCCESS Get signer's certificates successfully.
2682 IN CHAR16
*VariableName
,
2683 IN EFI_GUID
*VendorGuid
,
2684 OUT UINT8
**CertData
,
2685 OUT UINT32
*CertDataSize
2693 if ((VariableName
== NULL
) || (VendorGuid
== NULL
) || (CertData
== NULL
) || (CertDataSize
== NULL
)) {
2694 return EFI_INVALID_PARAMETER
;
2698 // Get variable "certdb".
2700 Status
= AuthServiceInternalFindVariable (
2706 if (EFI_ERROR (Status
)) {
2710 if ((DataSize
== 0) || (Data
== NULL
)) {
2712 return EFI_NOT_FOUND
;
2715 Status
= FindCertsFromDb (
2726 if (EFI_ERROR (Status
)) {
2730 *CertData
= Data
+ CertOffset
;
2735 Delete matching signer's certificates when deleting common authenticated
2736 variable by corresponding VariableName and VendorGuid from "certdb".
2738 @param[in] VariableName Name of authenticated Variable.
2739 @param[in] VendorGuid Vendor GUID of authenticated Variable.
2741 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
2742 @retval EFI_NOT_FOUND Fail to find "certdb" or matching certs.
2743 @retval EFI_OUT_OF_RESOURCES The operation is failed due to lack of resources.
2744 @retval EFI_SUCCESS The operation is completed successfully.
2749 IN CHAR16
*VariableName
,
2750 IN EFI_GUID
*VendorGuid
2757 UINT32 CertNodeOffset
;
2758 UINT32 CertNodeSize
;
2760 UINT32 NewCertDbSize
;
2762 if ((VariableName
== NULL
) || (VendorGuid
== NULL
)) {
2763 return EFI_INVALID_PARAMETER
;
2767 // Get variable "certdb".
2769 Status
= AuthServiceInternalFindVariable (
2775 if (EFI_ERROR (Status
)) {
2779 if ((DataSize
== 0) || (Data
== NULL
)) {
2781 return EFI_NOT_FOUND
;
2784 if (DataSize
== sizeof (UINT32
)) {
2786 // There is no certs in certdb.
2792 // Get corresponding cert node from certdb.
2794 Status
= FindCertsFromDb (
2805 if (EFI_ERROR (Status
)) {
2809 if (DataSize
< (CertNodeOffset
+ CertNodeSize
)) {
2810 return EFI_NOT_FOUND
;
2814 // Construct new data content of variable "certdb".
2816 NewCertDbSize
= (UINT32
) DataSize
- CertNodeSize
;
2817 NewCertDb
= (UINT8
*) mCertDbStore
;
2820 // Copy the DB entries before deleting node.
2822 CopyMem (NewCertDb
, Data
, CertNodeOffset
);
2824 // Update CertDbListSize.
2826 CopyMem (NewCertDb
, &NewCertDbSize
, sizeof (UINT32
));
2828 // Copy the DB entries after deleting node.
2830 if (DataSize
> (CertNodeOffset
+ CertNodeSize
)) {
2832 NewCertDb
+ CertNodeOffset
,
2833 Data
+ CertNodeOffset
+ CertNodeSize
,
2834 DataSize
- CertNodeOffset
- CertNodeSize
2841 VarAttr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
2842 Status
= AuthServiceInternalUpdateVariable (
2854 Insert signer's certificates for common authenticated variable with VariableName
2855 and VendorGuid in AUTH_CERT_DB_DATA to "certdb".
2857 @param[in] VariableName Name of authenticated Variable.
2858 @param[in] VendorGuid Vendor GUID of authenticated Variable.
2859 @param[in] CertData Pointer to signer's certificates.
2860 @param[in] CertDataSize Length of CertData in bytes.
2862 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
2863 @retval EFI_ACCESS_DENIED An AUTH_CERT_DB_DATA entry with same VariableName
2864 and VendorGuid already exists.
2865 @retval EFI_OUT_OF_RESOURCES The operation is failed due to lack of resources.
2866 @retval EFI_SUCCESS Insert an AUTH_CERT_DB_DATA entry to "certdb"
2871 IN CHAR16
*VariableName
,
2872 IN EFI_GUID
*VendorGuid
,
2874 IN UINTN CertDataSize
2882 UINT32 NewCertDbSize
;
2883 UINT32 CertNodeSize
;
2885 AUTH_CERT_DB_DATA
*Ptr
;
2887 if ((VariableName
== NULL
) || (VendorGuid
== NULL
) || (CertData
== NULL
)) {
2888 return EFI_INVALID_PARAMETER
;
2892 // Get variable "certdb".
2894 Status
= AuthServiceInternalFindVariable (
2900 if (EFI_ERROR (Status
)) {
2904 if ((DataSize
== 0) || (Data
== NULL
)) {
2906 return EFI_NOT_FOUND
;
2910 // Find whether matching cert node already exists in "certdb".
2911 // If yes return error.
2913 Status
= FindCertsFromDb (
2924 if (!EFI_ERROR (Status
)) {
2926 return EFI_ACCESS_DENIED
;
2930 // Construct new data content of variable "certdb".
2932 NameSize
= (UINT32
) StrLen (VariableName
);
2933 CertNodeSize
= sizeof (AUTH_CERT_DB_DATA
) + (UINT32
) CertDataSize
+ NameSize
* sizeof (CHAR16
);
2934 NewCertDbSize
= (UINT32
) DataSize
+ CertNodeSize
;
2935 if (NewCertDbSize
> mMaxCertDbSize
) {
2936 return EFI_OUT_OF_RESOURCES
;
2938 NewCertDb
= (UINT8
*) mCertDbStore
;
2941 // Copy the DB entries before inserting node.
2943 CopyMem (NewCertDb
, Data
, DataSize
);
2945 // Update CertDbListSize.
2947 CopyMem (NewCertDb
, &NewCertDbSize
, sizeof (UINT32
));
2949 // Construct new cert node.
2951 Ptr
= (AUTH_CERT_DB_DATA
*) (NewCertDb
+ DataSize
);
2952 CopyGuid (&Ptr
->VendorGuid
, VendorGuid
);
2953 CopyMem (&Ptr
->CertNodeSize
, &CertNodeSize
, sizeof (UINT32
));
2954 CopyMem (&Ptr
->NameSize
, &NameSize
, sizeof (UINT32
));
2955 CopyMem (&Ptr
->CertDataSize
, &CertDataSize
, sizeof (UINT32
));
2958 (UINT8
*) Ptr
+ sizeof (AUTH_CERT_DB_DATA
),
2960 NameSize
* sizeof (CHAR16
)
2964 (UINT8
*) Ptr
+ sizeof (AUTH_CERT_DB_DATA
) + NameSize
* sizeof (CHAR16
),
2972 VarAttr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
2973 Status
= AuthServiceInternalUpdateVariable (
2985 Clean up signer's certificates for common authenticated variable
2986 by corresponding VariableName and VendorGuid from "certdb".
2987 Sytem may break down during Timebased Variable update & certdb update,
2988 make them inconsistent, this function is called in AuthVariable Init to ensure
2991 @retval EFI_NOT_FOUND Fail to find matching certs.
2992 @retval EFI_SUCCESS Find matching certs and output parameters.
3001 AUTH_CERT_DB_DATA
*Ptr
;
3004 CHAR16
*VariableName
;
3006 BOOLEAN CertCleaned
;
3010 UINTN AuthVarDataSize
;
3011 EFI_GUID AuthVarGuid
;
3013 Status
= EFI_SUCCESS
;
3016 // Get corresponding certificates by VendorGuid and VariableName.
3019 CertCleaned
= FALSE
;
3022 // Get latest variable "certdb"
3024 Status
= AuthServiceInternalFindVariable (
3030 if (EFI_ERROR (Status
)) {
3034 if ((DataSize
== 0) || (Data
== NULL
)) {
3036 return EFI_NOT_FOUND
;
3039 Offset
= sizeof (UINT32
);
3041 while (Offset
< (UINT32
) DataSize
) {
3042 Ptr
= (AUTH_CERT_DB_DATA
*) (Data
+ Offset
);
3044 // Check whether VendorGuid matches.
3046 NodeSize
= ReadUnaligned32 (&Ptr
->CertNodeSize
);
3047 NameSize
= ReadUnaligned32 (&Ptr
->NameSize
);
3050 // Get VarName tailed with '\0'
3052 VariableName
= AllocateZeroPool((NameSize
+ 1) * sizeof(CHAR16
));
3053 if (VariableName
== NULL
) {
3054 return EFI_OUT_OF_RESOURCES
;
3056 CopyMem (VariableName
, (UINT8
*) Ptr
+ sizeof (AUTH_CERT_DB_DATA
), NameSize
* sizeof(CHAR16
));
3058 // Keep VarGuid aligned
3060 CopyMem (&AuthVarGuid
, &Ptr
->VendorGuid
, sizeof(EFI_GUID
));
3063 // Find corresponding time auth variable
3065 Status
= AuthServiceInternalFindVariable (
3068 (VOID
**) &AuthVarData
,
3072 if (EFI_ERROR(Status
)) {
3073 Status
= DeleteCertsFromDb(VariableName
, &AuthVarGuid
);
3075 DEBUG((EFI_D_INFO
, "Recovery!! Cert for Auth Variable %s Guid %g is removed for consistency\n", VariableName
, &AuthVarGuid
));
3076 FreePool(VariableName
);
3080 FreePool(VariableName
);
3081 Offset
= Offset
+ NodeSize
;
3083 } while (CertCleaned
);
3089 Process variable with EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set
3091 Caution: This function may receive untrusted input.
3092 This function may be invoked in SMM mode, and datasize and data are external input.
3093 This function will do basic validation, before parse the data.
3094 This function will parse the authentication carefully to avoid security issues, like
3095 buffer overflow, integer overflow.
3097 @param[in] VariableName Name of Variable to be found.
3098 @param[in] VendorGuid Variable vendor GUID.
3099 @param[in] Data Data pointer.
3100 @param[in] DataSize Size of Data found. If size is less than the
3101 data, this value contains the required size.
3102 @param[in] Attributes Attribute value of the variable.
3103 @param[in] AuthVarType Verify against PK, KEK database, private database or certificate in data payload.
3104 @param[in] OrgTimeStamp Pointer to original time stamp,
3105 original variable is not found if NULL.
3106 @param[out] VarPayloadPtr Pointer to variable payload address.
3107 @param[out] VarPayloadSize Pointer to variable payload size.
3109 @retval EFI_INVALID_PARAMETER Invalid parameter.
3110 @retval EFI_SECURITY_VIOLATION The variable does NOT pass the validation
3111 check carried out by the firmware.
3112 @retval EFI_OUT_OF_RESOURCES Failed to process variable due to lack
3114 @retval EFI_SUCCESS Variable pass validation successfully.
3118 VerifyTimeBasedPayload (
3119 IN CHAR16
*VariableName
,
3120 IN EFI_GUID
*VendorGuid
,
3123 IN UINT32 Attributes
,
3124 IN AUTHVAR_TYPE AuthVarType
,
3125 IN EFI_TIME
*OrgTimeStamp
,
3126 OUT UINT8
**VarPayloadPtr
,
3127 OUT UINTN
*VarPayloadSize
3130 EFI_VARIABLE_AUTHENTICATION_2
*CertData
;
3136 BOOLEAN VerifyStatus
;
3138 EFI_SIGNATURE_LIST
*CertList
;
3139 EFI_SIGNATURE_DATA
*Cert
;
3150 UINTN CertStackSize
;
3151 UINT8
*CertsInCertDb
;
3152 UINT32 CertsSizeinDb
;
3154 VerifyStatus
= FALSE
;
3160 CertsInCertDb
= NULL
;
3163 // When the attribute EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS is
3164 // set, then the Data buffer shall begin with an instance of a complete (and serialized)
3165 // EFI_VARIABLE_AUTHENTICATION_2 descriptor. The descriptor shall be followed by the new
3166 // variable value and DataSize shall reflect the combined size of the descriptor and the new
3167 // variable value. The authentication descriptor is not part of the variable data and is not
3168 // returned by subsequent calls to GetVariable().
3170 CertData
= (EFI_VARIABLE_AUTHENTICATION_2
*) Data
;
3173 // Verify that Pad1, Nanosecond, TimeZone, Daylight and Pad2 components of the
3174 // TimeStamp value are set to zero.
3176 if ((CertData
->TimeStamp
.Pad1
!= 0) ||
3177 (CertData
->TimeStamp
.Nanosecond
!= 0) ||
3178 (CertData
->TimeStamp
.TimeZone
!= 0) ||
3179 (CertData
->TimeStamp
.Daylight
!= 0) ||
3180 (CertData
->TimeStamp
.Pad2
!= 0)) {
3181 return EFI_SECURITY_VIOLATION
;
3184 if ((OrgTimeStamp
!= NULL
) && ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0)) {
3185 if (AuthServiceInternalCompareTimeStamp (&CertData
->TimeStamp
, OrgTimeStamp
)) {
3187 // TimeStamp check fail, suspicious replay attack, return EFI_SECURITY_VIOLATION.
3189 return EFI_SECURITY_VIOLATION
;
3194 // wCertificateType should be WIN_CERT_TYPE_EFI_GUID.
3195 // Cert type should be EFI_CERT_TYPE_PKCS7_GUID.
3197 if ((CertData
->AuthInfo
.Hdr
.wCertificateType
!= WIN_CERT_TYPE_EFI_GUID
) ||
3198 !CompareGuid (&CertData
->AuthInfo
.CertType
, &gEfiCertPkcs7Guid
)) {
3200 // Invalid AuthInfo type, return EFI_SECURITY_VIOLATION.
3202 return EFI_SECURITY_VIOLATION
;
3206 // Find out Pkcs7 SignedData which follows the EFI_VARIABLE_AUTHENTICATION_2 descriptor.
3207 // AuthInfo.Hdr.dwLength is the length of the entire certificate, including the length of the header.
3209 SigData
= CertData
->AuthInfo
.CertData
;
3210 SigDataSize
= CertData
->AuthInfo
.Hdr
.dwLength
- (UINT32
) (OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID
, CertData
));
3213 // Find out the new data payload which follows Pkcs7 SignedData directly.
3215 PayloadPtr
= SigData
+ SigDataSize
;
3216 PayloadSize
= DataSize
- OFFSET_OF_AUTHINFO2_CERT_DATA
- (UINTN
) SigDataSize
;
3219 // Construct a serialization buffer of the values of the VariableName, VendorGuid and Attributes
3220 // parameters of the SetVariable() call and the TimeStamp component of the
3221 // EFI_VARIABLE_AUTHENTICATION_2 descriptor followed by the variable's new value
3222 // i.e. (VariableName, VendorGuid, Attributes, TimeStamp, Data)
3224 NewDataSize
= PayloadSize
+ sizeof (EFI_TIME
) + sizeof (UINT32
) +
3225 sizeof (EFI_GUID
) + StrSize (VariableName
) - sizeof (CHAR16
);
3228 // Here is to reuse scratch data area(at the end of volatile variable store)
3229 // to reduce SMRAM consumption for SMM variable driver.
3230 // The scratch buffer is enough to hold the serialized data and safe to use,
3231 // because it is only used at here to do verification temporarily first
3232 // and then used in UpdateVariable() for a time based auth variable set.
3234 Status
= mAuthVarLibContextIn
->GetScratchBuffer (&NewDataSize
, (VOID
**) &NewData
);
3235 if (EFI_ERROR (Status
)) {
3236 return EFI_OUT_OF_RESOURCES
;
3240 Length
= StrLen (VariableName
) * sizeof (CHAR16
);
3241 CopyMem (Buffer
, VariableName
, Length
);
3244 Length
= sizeof (EFI_GUID
);
3245 CopyMem (Buffer
, VendorGuid
, Length
);
3248 Length
= sizeof (UINT32
);
3249 CopyMem (Buffer
, &Attr
, Length
);
3252 Length
= sizeof (EFI_TIME
);
3253 CopyMem (Buffer
, &CertData
->TimeStamp
, Length
);
3256 CopyMem (Buffer
, PayloadPtr
, PayloadSize
);
3258 if (AuthVarType
== AuthVarTypePk
) {
3260 // Verify that the signature has been made with the current Platform Key (no chaining for PK).
3261 // First, get signer's certificates from SignedData.
3263 VerifyStatus
= Pkcs7GetSigners (
3271 if (!VerifyStatus
) {
3276 // Second, get the current platform key from variable. Check whether it's identical with signer's certificates
3277 // in SignedData. If not, return error immediately.
3279 Status
= AuthServiceInternalFindVariable (
3280 EFI_PLATFORM_KEY_NAME
,
3281 &gEfiGlobalVariableGuid
,
3285 if (EFI_ERROR (Status
)) {
3286 VerifyStatus
= FALSE
;
3289 CertList
= (EFI_SIGNATURE_LIST
*) Data
;
3290 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
3291 if ((RootCertSize
!= (CertList
->SignatureSize
- (sizeof (EFI_SIGNATURE_DATA
) - 1))) ||
3292 (CompareMem (Cert
->SignatureData
, RootCert
, RootCertSize
) != 0)) {
3293 VerifyStatus
= FALSE
;
3298 // Verify Pkcs7 SignedData via Pkcs7Verify library.
3300 VerifyStatus
= Pkcs7Verify (
3309 } else if (AuthVarType
== AuthVarTypeKek
) {
3312 // Get KEK database from variable.
3314 Status
= AuthServiceInternalFindVariable (
3315 EFI_KEY_EXCHANGE_KEY_NAME
,
3316 &gEfiGlobalVariableGuid
,
3320 if (EFI_ERROR (Status
)) {
3325 // Ready to verify Pkcs7 SignedData. Go through KEK Signature Database to find out X.509 CertList.
3327 KekDataSize
= (UINT32
) DataSize
;
3328 CertList
= (EFI_SIGNATURE_LIST
*) Data
;
3329 while ((KekDataSize
> 0) && (KekDataSize
>= CertList
->SignatureListSize
)) {
3330 if (CompareGuid (&CertList
->SignatureType
, &gEfiCertX509Guid
)) {
3331 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
3332 CertCount
= (CertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - CertList
->SignatureHeaderSize
) / CertList
->SignatureSize
;
3333 for (Index
= 0; Index
< CertCount
; Index
++) {
3335 // Iterate each Signature Data Node within this CertList for a verify
3337 RootCert
= Cert
->SignatureData
;
3338 RootCertSize
= CertList
->SignatureSize
- (sizeof (EFI_SIGNATURE_DATA
) - 1);
3341 // Verify Pkcs7 SignedData via Pkcs7Verify library.
3343 VerifyStatus
= Pkcs7Verify (
3354 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) Cert
+ CertList
->SignatureSize
);
3357 KekDataSize
-= CertList
->SignatureListSize
;
3358 CertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) CertList
+ CertList
->SignatureListSize
);
3360 } else if (AuthVarType
== AuthVarTypePriv
) {
3363 // Process common authenticated variable except PK/KEK/DB/DBX/DBT.
3364 // Get signer's certificates from SignedData.
3366 VerifyStatus
= Pkcs7GetSigners (
3374 if (!VerifyStatus
) {
3379 // Get previously stored signer's certificates from certdb for existing
3380 // variable. Check whether they are identical with signer's certificates
3381 // in SignedData. If not, return error immediately.
3383 if (OrgTimeStamp
!= NULL
) {
3384 VerifyStatus
= FALSE
;
3386 Status
= GetCertsFromDb (VariableName
, VendorGuid
, &CertsInCertDb
, &CertsSizeinDb
);
3387 if (EFI_ERROR (Status
)) {
3391 if ((CertStackSize
!= CertsSizeinDb
) ||
3392 (CompareMem (SignerCerts
, CertsInCertDb
, CertsSizeinDb
) != 0)) {
3397 VerifyStatus
= Pkcs7Verify (
3405 if (!VerifyStatus
) {
3409 if ((OrgTimeStamp
== NULL
) && (PayloadSize
!= 0)) {
3411 // Insert signer's certificates when adding a new common authenticated variable.
3413 Status
= InsertCertsToDb (VariableName
, VendorGuid
, SignerCerts
, CertStackSize
);
3414 if (EFI_ERROR (Status
)) {
3415 VerifyStatus
= FALSE
;
3419 } else if (AuthVarType
== AuthVarTypePayload
) {
3420 CertList
= (EFI_SIGNATURE_LIST
*) PayloadPtr
;
3421 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
3422 RootCert
= Cert
->SignatureData
;
3423 RootCertSize
= CertList
->SignatureSize
- (sizeof (EFI_SIGNATURE_DATA
) - 1);
3425 // Verify Pkcs7 SignedData via Pkcs7Verify library.
3427 VerifyStatus
= Pkcs7Verify (
3436 return EFI_SECURITY_VIOLATION
;
3441 if (AuthVarType
== AuthVarTypePk
|| AuthVarType
== AuthVarTypePriv
) {
3442 Pkcs7FreeSigners (RootCert
);
3443 Pkcs7FreeSigners (SignerCerts
);
3446 if (!VerifyStatus
) {
3447 return EFI_SECURITY_VIOLATION
;
3450 Status
= CheckSignatureListFormat(VariableName
, VendorGuid
, PayloadPtr
, PayloadSize
);
3451 if (EFI_ERROR (Status
)) {
3455 *VarPayloadPtr
= PayloadPtr
;
3456 *VarPayloadSize
= PayloadSize
;
3462 Process variable with EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set
3464 Caution: This function may receive untrusted input.
3465 This function may be invoked in SMM mode, and datasize and data are external input.
3466 This function will do basic validation, before parse the data.
3467 This function will parse the authentication carefully to avoid security issues, like
3468 buffer overflow, integer overflow.
3470 @param[in] VariableName Name of Variable to be found.
3471 @param[in] VendorGuid Variable vendor GUID.
3472 @param[in] Data Data pointer.
3473 @param[in] DataSize Size of Data found. If size is less than the
3474 data, this value contains the required size.
3475 @param[in] Attributes Attribute value of the variable.
3476 @param[in] AuthVarType Verify against PK, KEK database, private database or certificate in data payload.
3477 @param[out] VarDel Delete the variable or not.
3479 @retval EFI_INVALID_PARAMETER Invalid parameter.
3480 @retval EFI_SECURITY_VIOLATION The variable does NOT pass the validation
3481 check carried out by the firmware.
3482 @retval EFI_OUT_OF_RESOURCES Failed to process variable due to lack
3484 @retval EFI_SUCCESS Variable pass validation successfully.
3488 VerifyTimeBasedPayloadAndUpdate (
3489 IN CHAR16
*VariableName
,
3490 IN EFI_GUID
*VendorGuid
,
3493 IN UINT32 Attributes
,
3494 IN AUTHVAR_TYPE AuthVarType
,
3499 EFI_STATUS FindStatus
;
3502 EFI_VARIABLE_AUTHENTICATION_2
*CertData
;
3503 AUTH_VARIABLE_INFO OrgVariableInfo
;
3506 ZeroMem (&OrgVariableInfo
, sizeof (OrgVariableInfo
));
3507 FindStatus
= mAuthVarLibContextIn
->FindVariable (
3513 Status
= VerifyTimeBasedPayload (
3520 (!EFI_ERROR (FindStatus
)) ? OrgVariableInfo
.TimeStamp
: NULL
,
3524 if (EFI_ERROR (Status
)) {
3528 if (!EFI_ERROR(FindStatus
)
3529 && (PayloadSize
== 0)
3530 && ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0)) {
3536 CertData
= (EFI_VARIABLE_AUTHENTICATION_2
*) Data
;
3539 // Final step: Update/Append Variable if it pass Pkcs7Verify
3541 Status
= AuthServiceInternalUpdateVariableWithTimeStamp (
3547 &CertData
->TimeStamp
3551 // Delete signer's certificates when delete the common authenticated variable.
3553 if (IsDel
&& AuthVarType
== AuthVarTypePriv
&& !EFI_ERROR(Status
) ) {
3554 Status
= DeleteCertsFromDb (VariableName
, VendorGuid
);
3557 if (VarDel
!= NULL
) {
3558 if (IsDel
&& !EFI_ERROR(Status
)) {