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 - 2019, Intel Corporation. All rights reserved.<BR>
22 Copyright (c) Microsoft Corporation.
23 SPDX-License-Identifier: BSD-2-Clause-Patent
27 #include "AuthServiceInternal.h"
29 #include <Protocol/VariablePolicy.h>
30 #include <Library/VariablePolicyLib.h>
33 // Public Exponent of RSA Key.
35 CONST UINT8 mRsaE
[] = { 0x01, 0x00, 0x01 };
37 CONST UINT8 mSha256OidValue
[] = { 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 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 Finds variable in storage blocks of volatile and non-volatile storage areas.
62 This code finds variable in storage blocks of volatile and non-volatile storage areas.
63 If VariableName is an empty string, then we just return the first
64 qualified variable without comparing VariableName and VendorGuid.
66 @param[in] VariableName Name of the variable to be found.
67 @param[in] VendorGuid Variable vendor GUID to be found.
68 @param[out] Data Pointer to data address.
69 @param[out] DataSize Pointer to data size.
71 @retval EFI_INVALID_PARAMETER If VariableName is not an empty string,
72 while VendorGuid is NULL.
73 @retval EFI_SUCCESS Variable successfully found.
74 @retval EFI_NOT_FOUND Variable not found
78 AuthServiceInternalFindVariable (
79 IN CHAR16
*VariableName
,
80 IN EFI_GUID
*VendorGuid
,
86 AUTH_VARIABLE_INFO AuthVariableInfo
;
88 ZeroMem (&AuthVariableInfo
, sizeof (AuthVariableInfo
));
89 Status
= mAuthVarLibContextIn
->FindVariable (
94 *Data
= AuthVariableInfo
.Data
;
95 *DataSize
= AuthVariableInfo
.DataSize
;
100 Update the variable region with Variable information.
102 @param[in] VariableName Name of variable.
103 @param[in] VendorGuid Guid of variable.
104 @param[in] Data Data pointer.
105 @param[in] DataSize Size of Data.
106 @param[in] Attributes Attribute value of the variable.
108 @retval EFI_SUCCESS The update operation is success.
109 @retval EFI_INVALID_PARAMETER Invalid parameter.
110 @retval EFI_WRITE_PROTECTED Variable is write-protected.
111 @retval EFI_OUT_OF_RESOURCES There is not enough resource.
115 AuthServiceInternalUpdateVariable (
116 IN CHAR16
*VariableName
,
117 IN EFI_GUID
*VendorGuid
,
123 AUTH_VARIABLE_INFO AuthVariableInfo
;
125 ZeroMem (&AuthVariableInfo
, sizeof (AuthVariableInfo
));
126 AuthVariableInfo
.VariableName
= VariableName
;
127 AuthVariableInfo
.VendorGuid
= VendorGuid
;
128 AuthVariableInfo
.Data
= Data
;
129 AuthVariableInfo
.DataSize
= DataSize
;
130 AuthVariableInfo
.Attributes
= Attributes
;
132 return mAuthVarLibContextIn
->UpdateVariable (
138 Update the variable region with Variable information.
140 @param[in] VariableName Name of variable.
141 @param[in] VendorGuid Guid of variable.
142 @param[in] Data Data pointer.
143 @param[in] DataSize Size of Data.
144 @param[in] Attributes Attribute value of the variable.
145 @param[in] TimeStamp Value of associated TimeStamp.
147 @retval EFI_SUCCESS The update operation is success.
148 @retval EFI_INVALID_PARAMETER Invalid parameter.
149 @retval EFI_WRITE_PROTECTED Variable is write-protected.
150 @retval EFI_OUT_OF_RESOURCES There is not enough resource.
154 AuthServiceInternalUpdateVariableWithTimeStamp (
155 IN CHAR16
*VariableName
,
156 IN EFI_GUID
*VendorGuid
,
159 IN UINT32 Attributes
,
160 IN EFI_TIME
*TimeStamp
163 EFI_STATUS FindStatus
;
166 AUTH_VARIABLE_INFO AuthVariableInfo
;
168 FindStatus
= AuthServiceInternalFindVariable (
176 // EFI_VARIABLE_APPEND_WRITE attribute only effects for existing variable
178 if (!EFI_ERROR (FindStatus
) && ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) != 0)) {
179 if ((CompareGuid (VendorGuid
, &gEfiImageSecurityDatabaseGuid
) &&
180 ((StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE
) == 0) || (StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE1
) == 0) ||
181 (StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE2
) == 0))) ||
182 (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, EFI_KEY_EXCHANGE_KEY_NAME
) == 0)))
185 // For variables with formatted as EFI_SIGNATURE_LIST, the driver shall not perform an append of
186 // EFI_SIGNATURE_DATA values that are already part of the existing variable value.
188 FilterSignatureList (
197 ZeroMem (&AuthVariableInfo
, sizeof (AuthVariableInfo
));
198 AuthVariableInfo
.VariableName
= VariableName
;
199 AuthVariableInfo
.VendorGuid
= VendorGuid
;
200 AuthVariableInfo
.Data
= Data
;
201 AuthVariableInfo
.DataSize
= DataSize
;
202 AuthVariableInfo
.Attributes
= Attributes
;
203 AuthVariableInfo
.TimeStamp
= TimeStamp
;
204 return mAuthVarLibContextIn
->UpdateVariable (
210 Determine whether this operation needs a physical present user.
212 @param[in] VariableName Name of the Variable.
213 @param[in] VendorGuid GUID of the Variable.
215 @retval TRUE This variable is protected, only a physical present user could set this variable.
216 @retval FALSE This variable is not protected.
220 NeedPhysicallyPresent (
221 IN CHAR16
*VariableName
,
222 IN EFI_GUID
*VendorGuid
225 // If the VariablePolicy engine is disabled, allow deletion of any authenticated variables.
226 if (IsVariablePolicyEnabled ()) {
227 if ( (CompareGuid (VendorGuid
, &gEfiSecureBootEnableDisableGuid
) && (StrCmp (VariableName
, EFI_SECURE_BOOT_ENABLE_NAME
) == 0))
228 || (CompareGuid (VendorGuid
, &gEfiCustomModeEnableGuid
) && (StrCmp (VariableName
, EFI_CUSTOM_MODE_NAME
) == 0)))
238 Determine whether the platform is operating in Custom Secure Boot mode.
240 @retval TRUE The platform is operating in Custom mode.
241 @retval FALSE The platform is operating in Standard mode.
253 Status
= AuthServiceInternalFindVariable (EFI_CUSTOM_MODE_NAME
, &gEfiCustomModeEnableGuid
, &Data
, &DataSize
);
254 if (!EFI_ERROR (Status
) && (*(UINT8
*)Data
== CUSTOM_SECURE_BOOT_MODE
)) {
262 Update platform mode.
264 @param[in] Mode SETUP_MODE or USER_MODE.
266 @return EFI_INVALID_PARAMETER Invalid parameter.
267 @return EFI_SUCCESS Update platform mode successfully.
278 UINT8 SecureBootMode
;
279 UINT8 SecureBootEnable
;
280 UINTN VariableDataSize
;
282 Status
= AuthServiceInternalFindVariable (
284 &gEfiGlobalVariableGuid
,
288 if (EFI_ERROR (Status
)) {
293 // Update the value of SetupMode variable by a simple mem copy, this could avoid possible
294 // variable storage reclaim at runtime.
296 mPlatformMode
= (UINT8
)Mode
;
297 CopyMem (Data
, &mPlatformMode
, sizeof (UINT8
));
299 if (mAuthVarLibContextIn
->AtRuntime ()) {
301 // SecureBoot Variable indicates whether the platform firmware is operating
302 // in Secure boot mode (1) or not (0), so we should not change SecureBoot
303 // Variable in runtime.
309 // Check "SecureBoot" variable's existence.
310 // If it doesn't exist, firmware has no capability to perform driver signing verification,
311 // then set "SecureBoot" to 0.
313 Status
= AuthServiceInternalFindVariable (
314 EFI_SECURE_BOOT_MODE_NAME
,
315 &gEfiGlobalVariableGuid
,
320 // If "SecureBoot" variable exists, then check "SetupMode" variable update.
321 // If "SetupMode" variable is USER_MODE, "SecureBoot" variable is set to 1.
322 // If "SetupMode" variable is SETUP_MODE, "SecureBoot" variable is set to 0.
324 if (EFI_ERROR (Status
)) {
325 SecureBootMode
= SECURE_BOOT_MODE_DISABLE
;
327 if (mPlatformMode
== USER_MODE
) {
328 SecureBootMode
= SECURE_BOOT_MODE_ENABLE
;
329 } else if (mPlatformMode
== SETUP_MODE
) {
330 SecureBootMode
= SECURE_BOOT_MODE_DISABLE
;
332 return EFI_NOT_FOUND
;
336 Status
= AuthServiceInternalUpdateVariable (
337 EFI_SECURE_BOOT_MODE_NAME
,
338 &gEfiGlobalVariableGuid
,
341 EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
343 if (EFI_ERROR (Status
)) {
348 // Check "SecureBootEnable" variable's existence. It can enable/disable secure boot feature.
350 Status
= AuthServiceInternalFindVariable (
351 EFI_SECURE_BOOT_ENABLE_NAME
,
352 &gEfiSecureBootEnableDisableGuid
,
357 if (SecureBootMode
== SECURE_BOOT_MODE_ENABLE
) {
359 // Create the "SecureBootEnable" variable as secure boot is enabled.
361 SecureBootEnable
= SECURE_BOOT_ENABLE
;
362 VariableDataSize
= sizeof (SecureBootEnable
);
365 // Delete the "SecureBootEnable" variable if this variable exist as "SecureBoot"
366 // variable is not in secure boot state.
368 if (EFI_ERROR (Status
)) {
372 SecureBootEnable
= SECURE_BOOT_DISABLE
;
373 VariableDataSize
= 0;
376 Status
= AuthServiceInternalUpdateVariable (
377 EFI_SECURE_BOOT_ENABLE_NAME
,
378 &gEfiSecureBootEnableDisableGuid
,
381 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
387 Check input data form to make sure it is a valid EFI_SIGNATURE_LIST for PK/KEK/db/dbx/dbt variable.
389 @param[in] VariableName Name of Variable to be check.
390 @param[in] VendorGuid Variable vendor GUID.
391 @param[in] Data Point to the variable data to be checked.
392 @param[in] DataSize Size of Data.
394 @return EFI_INVALID_PARAMETER Invalid signature list format.
395 @return EFI_SUCCESS Passed signature list format check successfully.
399 CheckSignatureListFormat (
400 IN CHAR16
*VariableName
,
401 IN EFI_GUID
*VendorGuid
,
406 EFI_SIGNATURE_LIST
*SigList
;
412 EFI_SIGNATURE_DATA
*CertData
;
419 ASSERT (VariableName
!= NULL
&& VendorGuid
!= NULL
&& Data
!= NULL
);
421 if (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, EFI_PLATFORM_KEY_NAME
) == 0)) {
423 } else if ((CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, EFI_KEY_EXCHANGE_KEY_NAME
) == 0)) ||
424 (CompareGuid (VendorGuid
, &gEfiImageSecurityDatabaseGuid
) &&
425 ((StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE
) == 0) || (StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE1
) == 0) ||
426 (StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE2
) == 0))))
434 SigList
= (EFI_SIGNATURE_LIST
*)Data
;
435 SigDataSize
= DataSize
;
439 // Walk through the input signature list and check the data format.
440 // If any signature is incorrectly formed, the whole check will fail.
442 while ((SigDataSize
> 0) && (SigDataSize
>= SigList
->SignatureListSize
)) {
443 for (Index
= 0; Index
< (sizeof (mSupportSigItem
) / sizeof (EFI_SIGNATURE_ITEM
)); Index
++ ) {
444 if (CompareGuid (&SigList
->SignatureType
, &mSupportSigItem
[Index
].SigType
)) {
446 // The value of SignatureSize should always be 16 (size of SignatureOwner
447 // component) add the data length according to signature type.
449 if ((mSupportSigItem
[Index
].SigDataSize
!= ((UINT32
) ~0)) &&
450 ((SigList
->SignatureSize
- sizeof (EFI_GUID
)) != mSupportSigItem
[Index
].SigDataSize
))
452 return EFI_INVALID_PARAMETER
;
455 if ((mSupportSigItem
[Index
].SigHeaderSize
!= ((UINT32
) ~0)) &&
456 (SigList
->SignatureHeaderSize
!= mSupportSigItem
[Index
].SigHeaderSize
))
458 return EFI_INVALID_PARAMETER
;
465 if (Index
== (sizeof (mSupportSigItem
) / sizeof (EFI_SIGNATURE_ITEM
))) {
467 // Undefined signature type.
469 return EFI_INVALID_PARAMETER
;
472 if (CompareGuid (&SigList
->SignatureType
, &gEfiCertX509Guid
)) {
474 // Try to retrieve the RSA public key from the X.509 certificate.
475 // If this operation fails, it's not a valid certificate.
477 RsaContext
= RsaNew ();
478 if (RsaContext
== NULL
) {
479 return EFI_INVALID_PARAMETER
;
482 CertData
= (EFI_SIGNATURE_DATA
*)((UINT8
*)SigList
+ sizeof (EFI_SIGNATURE_LIST
) + SigList
->SignatureHeaderSize
);
483 CertLen
= SigList
->SignatureSize
- sizeof (EFI_GUID
);
484 if (!RsaGetPublicKeyFromX509 (CertData
->SignatureData
, CertLen
, &RsaContext
)) {
485 RsaFree (RsaContext
);
486 return EFI_INVALID_PARAMETER
;
489 RsaFree (RsaContext
);
492 if ((SigList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - SigList
->SignatureHeaderSize
) % SigList
->SignatureSize
!= 0) {
493 return EFI_INVALID_PARAMETER
;
496 SigCount
+= (SigList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - SigList
->SignatureHeaderSize
) / SigList
->SignatureSize
;
498 SigDataSize
-= SigList
->SignatureListSize
;
499 SigList
= (EFI_SIGNATURE_LIST
*)((UINT8
*)SigList
+ SigList
->SignatureListSize
);
502 if (((UINTN
)SigList
- (UINTN
)Data
) != DataSize
) {
503 return EFI_INVALID_PARAMETER
;
506 if (IsPk
&& (SigCount
> 1)) {
507 return EFI_INVALID_PARAMETER
;
514 Update "VendorKeys" variable to record the out of band secure boot key modification.
516 @return EFI_SUCCESS Variable is updated successfully.
517 @return Others Failed to update variable.
521 VendorKeyIsModified (
527 if (mVendorKeyState
== VENDOR_KEYS_MODIFIED
) {
531 mVendorKeyState
= VENDOR_KEYS_MODIFIED
;
533 Status
= AuthServiceInternalUpdateVariable (
534 EFI_VENDOR_KEYS_NV_VARIABLE_NAME
,
535 &gEfiVendorKeysNvGuid
,
538 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
540 if (EFI_ERROR (Status
)) {
544 return AuthServiceInternalUpdateVariable (
545 EFI_VENDOR_KEYS_VARIABLE_NAME
,
546 &gEfiGlobalVariableGuid
,
549 EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
554 Process variable with platform key for verification.
556 Caution: This function may receive untrusted input.
557 This function may be invoked in SMM mode, and datasize and data are external input.
558 This function will do basic validation, before parse the data.
559 This function will parse the authentication carefully to avoid security issues, like
560 buffer overflow, integer overflow.
561 This function will check attribute carefully to avoid authentication bypass.
563 @param[in] VariableName Name of Variable to be found.
564 @param[in] VendorGuid Variable vendor GUID.
565 @param[in] Data Data pointer.
566 @param[in] DataSize Size of Data found. If size is less than the
567 data, this value contains the required size.
568 @param[in] Attributes Attribute value of the variable
569 @param[in] IsPk Indicate whether it is to process pk.
571 @return EFI_INVALID_PARAMETER Invalid parameter.
572 @return EFI_SECURITY_VIOLATION The variable does NOT pass the validation.
573 check carried out by the firmware.
574 @return EFI_SUCCESS Variable passed validation successfully.
579 IN CHAR16
*VariableName
,
580 IN EFI_GUID
*VendorGuid
,
583 IN UINT32 Attributes OPTIONAL
,
592 if (((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0) ||
593 ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) == 0))
596 // PK, KEK and db/dbx/dbt should set EFI_VARIABLE_NON_VOLATILE attribute and should be a time-based
597 // authenticated variable.
599 return EFI_INVALID_PARAMETER
;
603 // Init state of Del. State may change due to secure check
606 if ( (InCustomMode () && UserPhysicalPresent ())
607 || ( (mPlatformMode
== SETUP_MODE
)
608 && !(FeaturePcdGet (PcdRequireSelfSignedPk
) && IsPk
)))
610 Payload
= (UINT8
*)Data
+ AUTHINFO2_SIZE (Data
);
611 PayloadSize
= DataSize
- AUTHINFO2_SIZE (Data
);
612 if (PayloadSize
== 0) {
616 Status
= CheckSignatureListFormat (VariableName
, VendorGuid
, Payload
, PayloadSize
);
617 if (EFI_ERROR (Status
)) {
621 Status
= AuthServiceInternalUpdateVariableWithTimeStamp (
627 &((EFI_VARIABLE_AUTHENTICATION_2
*)Data
)->TimeStamp
629 if (EFI_ERROR (Status
)) {
633 if ( (mPlatformMode
!= SETUP_MODE
)
634 || (FeaturePcdGet (PcdRequireSelfSignedPk
) && IsPk
))
636 Status
= VendorKeyIsModified ();
638 } else if (mPlatformMode
== USER_MODE
) {
640 // Verify against X509 Cert in PK database.
642 Status
= VerifyTimeBasedPayloadAndUpdate (
653 // Verify against the certificate in data payload.
655 Status
= VerifyTimeBasedPayloadAndUpdate (
666 if (!EFI_ERROR (Status
) && IsPk
) {
667 if ((mPlatformMode
== SETUP_MODE
) && !Del
) {
669 // If enroll PK in setup mode, need change to user mode.
671 Status
= UpdatePlatformMode (USER_MODE
);
672 } else if ((mPlatformMode
== USER_MODE
) && Del
) {
674 // If delete PK in user mode, need change to setup mode.
676 Status
= UpdatePlatformMode (SETUP_MODE
);
684 Process variable with key exchange key for verification.
686 Caution: This function may receive untrusted input.
687 This function may be invoked in SMM mode, and datasize and data are external input.
688 This function will do basic validation, before parse the data.
689 This function will parse the authentication carefully to avoid security issues, like
690 buffer overflow, integer overflow.
691 This function will check attribute carefully to avoid authentication bypass.
693 @param[in] VariableName Name of Variable to be found.
694 @param[in] VendorGuid Variable vendor GUID.
695 @param[in] Data Data pointer.
696 @param[in] DataSize Size of Data found. If size is less than the
697 data, this value contains the required size.
698 @param[in] Attributes Attribute value of the variable.
700 @return EFI_INVALID_PARAMETER Invalid parameter.
701 @return EFI_SECURITY_VIOLATION The variable does NOT pass the validation
702 check carried out by the firmware.
703 @return EFI_SUCCESS Variable pass validation successfully.
708 IN CHAR16
*VariableName
,
709 IN EFI_GUID
*VendorGuid
,
712 IN UINT32 Attributes OPTIONAL
719 if (((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0) ||
720 ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) == 0))
723 // DB, DBX and DBT should set EFI_VARIABLE_NON_VOLATILE attribute and should be a time-based
724 // authenticated variable.
726 return EFI_INVALID_PARAMETER
;
729 Status
= EFI_SUCCESS
;
730 if ((mPlatformMode
== USER_MODE
) && !(InCustomMode () && UserPhysicalPresent ())) {
732 // Time-based, verify against X509 Cert KEK.
734 return VerifyTimeBasedPayloadAndUpdate (
745 // If in setup mode or custom secure boot mode, no authentication needed.
747 Payload
= (UINT8
*)Data
+ AUTHINFO2_SIZE (Data
);
748 PayloadSize
= DataSize
- AUTHINFO2_SIZE (Data
);
750 Status
= CheckSignatureListFormat (VariableName
, VendorGuid
, Payload
, PayloadSize
);
751 if (EFI_ERROR (Status
)) {
755 Status
= AuthServiceInternalUpdateVariableWithTimeStamp (
761 &((EFI_VARIABLE_AUTHENTICATION_2
*)Data
)->TimeStamp
763 if (EFI_ERROR (Status
)) {
767 if (mPlatformMode
!= SETUP_MODE
) {
768 Status
= VendorKeyIsModified ();
776 Check if it is to delete auth variable.
778 @param[in] OrgAttributes Original attribute value of the variable.
779 @param[in] Data Data pointer.
780 @param[in] DataSize Size of Data.
781 @param[in] Attributes Attribute value of the variable.
783 @retval TRUE It is to delete auth variable.
784 @retval FALSE It is not to delete auth variable.
788 IsDeleteAuthVariable (
789 IN UINT32 OrgAttributes
,
801 // To delete a variable created with the EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
802 // or the EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS attribute,
803 // SetVariable must be used with attributes matching the existing variable
804 // and the DataSize set to the size of the AuthInfo descriptor.
806 if ((Attributes
== OrgAttributes
) &&
807 ((Attributes
& (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
)) != 0))
809 if ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) {
810 PayloadSize
= DataSize
- AUTHINFO2_SIZE (Data
);
811 if (PayloadSize
== 0) {
815 PayloadSize
= DataSize
- AUTHINFO_SIZE
;
816 if (PayloadSize
== 0) {
826 Process variable with EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set
828 Caution: This function may receive untrusted input.
829 This function may be invoked in SMM mode, and datasize and data are external input.
830 This function will do basic validation, before parse the data.
831 This function will parse the authentication carefully to avoid security issues, like
832 buffer overflow, integer overflow.
833 This function will check attribute carefully to avoid authentication bypass.
835 @param[in] VariableName Name of the variable.
836 @param[in] VendorGuid Variable vendor GUID.
837 @param[in] Data Data pointer.
838 @param[in] DataSize Size of Data.
839 @param[in] Attributes Attribute value of the variable.
841 @return EFI_INVALID_PARAMETER Invalid parameter.
842 @return EFI_WRITE_PROTECTED Variable is write-protected and needs authentication with
843 EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS or EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set.
844 @return EFI_OUT_OF_RESOURCES The Database to save the public key is full.
845 @return EFI_SECURITY_VIOLATION The variable is with EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
846 set, but the AuthInfo does NOT pass the validation
847 check carried out by the firmware.
848 @return EFI_SUCCESS Variable is not write-protected or pass validation successfully.
853 IN CHAR16
*VariableName
,
854 IN EFI_GUID
*VendorGuid
,
861 AUTH_VARIABLE_INFO OrgVariableInfo
;
863 Status
= EFI_SUCCESS
;
865 ZeroMem (&OrgVariableInfo
, sizeof (OrgVariableInfo
));
866 Status
= mAuthVarLibContextIn
->FindVariable (
872 // If the VariablePolicy engine is disabled, allow deletion of any authenticated variables.
873 if ((!EFI_ERROR (Status
)) && IsDeleteAuthVariable (OrgVariableInfo
.Attributes
, Data
, DataSize
, Attributes
) && (UserPhysicalPresent () || !IsVariablePolicyEnabled ())) {
875 // Allow the delete operation of common authenticated variable(AT or AW) at user physical presence.
877 Status
= AuthServiceInternalUpdateVariable (
884 if (!EFI_ERROR (Status
) && ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0)) {
885 Status
= DeleteCertsFromDb (VariableName
, VendorGuid
, Attributes
);
891 if (NeedPhysicallyPresent (VariableName
, VendorGuid
) && !UserPhysicalPresent ()) {
893 // This variable is protected, only physical present user could modify its value.
895 return EFI_SECURITY_VIOLATION
;
899 if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) {
901 // Reject Counter Based Auth Variable processing request.
903 return EFI_UNSUPPORTED
;
904 } else if ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) {
906 // Process Time-based Authenticated variable.
908 return VerifyTimeBasedPayloadAndUpdate (
919 if ((OrgVariableInfo
.Data
!= NULL
) &&
920 ((OrgVariableInfo
.Attributes
& (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
)) != 0))
923 // If the variable is already write-protected, it always needs authentication before update.
925 return EFI_WRITE_PROTECTED
;
929 // Not authenticated variable, just update variable as usual.
931 Status
= AuthServiceInternalUpdateVariable (VariableName
, VendorGuid
, Data
, DataSize
, Attributes
);
936 Filter out the duplicated EFI_SIGNATURE_DATA from the new data by comparing to the original data.
938 @param[in] Data Pointer to original EFI_SIGNATURE_LIST.
939 @param[in] DataSize Size of Data buffer.
940 @param[in, out] NewData Pointer to new EFI_SIGNATURE_LIST.
941 @param[in, out] NewDataSize Size of NewData buffer.
945 FilterSignatureList (
948 IN OUT VOID
*NewData
,
949 IN OUT UINTN
*NewDataSize
952 EFI_SIGNATURE_LIST
*CertList
;
953 EFI_SIGNATURE_DATA
*Cert
;
955 EFI_SIGNATURE_LIST
*NewCertList
;
956 EFI_SIGNATURE_DATA
*NewCert
;
963 UINTN SignatureListSize
;
969 if (*NewDataSize
== 0) {
973 TempDataSize
= *NewDataSize
;
974 Status
= mAuthVarLibContextIn
->GetScratchBuffer (&TempDataSize
, (VOID
**)&TempData
);
975 if (EFI_ERROR (Status
)) {
976 return EFI_OUT_OF_RESOURCES
;
981 NewCertList
= (EFI_SIGNATURE_LIST
*)NewData
;
982 while ((*NewDataSize
> 0) && (*NewDataSize
>= NewCertList
->SignatureListSize
)) {
983 NewCert
= (EFI_SIGNATURE_DATA
*)((UINT8
*)NewCertList
+ sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
);
984 NewCertCount
= (NewCertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - NewCertList
->SignatureHeaderSize
) / NewCertList
->SignatureSize
;
987 for (Index
= 0; Index
< NewCertCount
; Index
++) {
991 CertList
= (EFI_SIGNATURE_LIST
*)Data
;
992 while ((Size
> 0) && (Size
>= CertList
->SignatureListSize
)) {
993 if (CompareGuid (&CertList
->SignatureType
, &NewCertList
->SignatureType
) &&
994 (CertList
->SignatureSize
== NewCertList
->SignatureSize
))
996 Cert
= (EFI_SIGNATURE_DATA
*)((UINT8
*)CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
997 CertCount
= (CertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - CertList
->SignatureHeaderSize
) / CertList
->SignatureSize
;
998 for (Index2
= 0; Index2
< CertCount
; Index2
++) {
1000 // Iterate each Signature Data in this Signature List.
1002 if (CompareMem (NewCert
, Cert
, CertList
->SignatureSize
) == 0) {
1007 Cert
= (EFI_SIGNATURE_DATA
*)((UINT8
*)Cert
+ CertList
->SignatureSize
);
1015 Size
-= CertList
->SignatureListSize
;
1016 CertList
= (EFI_SIGNATURE_LIST
*)((UINT8
*)CertList
+ CertList
->SignatureListSize
);
1021 // New EFI_SIGNATURE_DATA, keep it.
1023 if (CopiedCount
== 0) {
1025 // Copy EFI_SIGNATURE_LIST header for only once.
1027 CopyMem (Tail
, NewCertList
, sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
);
1028 Tail
= Tail
+ sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
;
1031 CopyMem (Tail
, NewCert
, NewCertList
->SignatureSize
);
1032 Tail
+= NewCertList
->SignatureSize
;
1036 NewCert
= (EFI_SIGNATURE_DATA
*)((UINT8
*)NewCert
+ NewCertList
->SignatureSize
);
1040 // Update SignatureListSize in the kept EFI_SIGNATURE_LIST.
1042 if (CopiedCount
!= 0) {
1043 SignatureListSize
= sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
+ (CopiedCount
* NewCertList
->SignatureSize
);
1044 CertList
= (EFI_SIGNATURE_LIST
*)(Tail
- SignatureListSize
);
1045 CertList
->SignatureListSize
= (UINT32
)SignatureListSize
;
1048 *NewDataSize
-= NewCertList
->SignatureListSize
;
1049 NewCertList
= (EFI_SIGNATURE_LIST
*)((UINT8
*)NewCertList
+ NewCertList
->SignatureListSize
);
1052 TempDataSize
= (Tail
- (UINT8
*)TempData
);
1054 CopyMem (NewData
, TempData
, TempDataSize
);
1055 *NewDataSize
= TempDataSize
;
1061 Compare two EFI_TIME data.
1064 @param FirstTime A pointer to the first EFI_TIME data.
1065 @param SecondTime A pointer to the second EFI_TIME data.
1067 @retval TRUE The FirstTime is not later than the SecondTime.
1068 @retval FALSE The FirstTime is later than the SecondTime.
1072 AuthServiceInternalCompareTimeStamp (
1073 IN EFI_TIME
*FirstTime
,
1074 IN EFI_TIME
*SecondTime
1077 if (FirstTime
->Year
!= SecondTime
->Year
) {
1078 return (BOOLEAN
)(FirstTime
->Year
< SecondTime
->Year
);
1079 } else if (FirstTime
->Month
!= SecondTime
->Month
) {
1080 return (BOOLEAN
)(FirstTime
->Month
< SecondTime
->Month
);
1081 } else if (FirstTime
->Day
!= SecondTime
->Day
) {
1082 return (BOOLEAN
)(FirstTime
->Day
< SecondTime
->Day
);
1083 } else if (FirstTime
->Hour
!= SecondTime
->Hour
) {
1084 return (BOOLEAN
)(FirstTime
->Hour
< SecondTime
->Hour
);
1085 } else if (FirstTime
->Minute
!= SecondTime
->Minute
) {
1086 return (BOOLEAN
)(FirstTime
->Minute
< SecondTime
->Minute
);
1089 return (BOOLEAN
)(FirstTime
->Second
<= SecondTime
->Second
);
1093 Calculate SHA256 digest of SignerCert CommonName + ToplevelCert tbsCertificate
1094 SignerCert and ToplevelCert are inside the signer certificate chain.
1096 @param[in] SignerCert A pointer to SignerCert data.
1097 @param[in] SignerCertSize Length of SignerCert data.
1098 @param[in] TopLevelCert A pointer to TopLevelCert data.
1099 @param[in] TopLevelCertSize Length of TopLevelCert data.
1100 @param[out] Sha256Digest Sha256 digest calculated.
1102 @return EFI_ABORTED Digest process failed.
1103 @return EFI_SUCCESS SHA256 Digest is successfully calculated.
1107 CalculatePrivAuthVarSignChainSHA256Digest (
1108 IN UINT8
*SignerCert
,
1109 IN UINTN SignerCertSize
,
1110 IN UINT8
*TopLevelCert
,
1111 IN UINTN TopLevelCertSize
,
1112 OUT UINT8
*Sha256Digest
1117 CHAR8 CertCommonName
[128];
1118 UINTN CertCommonNameSize
;
1119 BOOLEAN CryptoStatus
;
1122 CertCommonNameSize
= sizeof (CertCommonName
);
1125 // Get SignerCert CommonName
1127 Status
= X509GetCommonName (SignerCert
, SignerCertSize
, CertCommonName
, &CertCommonNameSize
);
1128 if (EFI_ERROR (Status
)) {
1129 DEBUG ((DEBUG_INFO
, "%a Get SignerCert CommonName failed with status %x\n", __FUNCTION__
, Status
));
1134 // Get TopLevelCert tbsCertificate
1136 if (!X509GetTBSCert (TopLevelCert
, TopLevelCertSize
, &TbsCert
, &TbsCertSize
)) {
1137 DEBUG ((DEBUG_INFO
, "%a Get Top-level Cert tbsCertificate failed!\n", __FUNCTION__
));
1142 // Digest SignerCert CN + TopLevelCert tbsCertificate
1144 ZeroMem (Sha256Digest
, SHA256_DIGEST_SIZE
);
1145 CryptoStatus
= Sha256Init (mHashCtx
);
1146 if (!CryptoStatus
) {
1151 // '\0' is forced in CertCommonName. No overflow issue
1153 CryptoStatus
= Sha256Update (
1156 AsciiStrLen (CertCommonName
)
1158 if (!CryptoStatus
) {
1162 CryptoStatus
= Sha256Update (mHashCtx
, TbsCert
, TbsCertSize
);
1163 if (!CryptoStatus
) {
1167 CryptoStatus
= Sha256Final (mHashCtx
, Sha256Digest
);
1168 if (!CryptoStatus
) {
1176 Find matching signer's certificates for common authenticated variable
1177 by corresponding VariableName and VendorGuid from "certdb" or "certdbv".
1179 The data format of "certdb" or "certdbv":
1181 // UINT32 CertDbListSize;
1182 // /// AUTH_CERT_DB_DATA Certs1[];
1183 // /// AUTH_CERT_DB_DATA Certs2[];
1185 // /// AUTH_CERT_DB_DATA Certsn[];
1188 @param[in] VariableName Name of authenticated Variable.
1189 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1190 @param[in] Data Pointer to variable "certdb" or "certdbv".
1191 @param[in] DataSize Size of variable "certdb" or "certdbv".
1192 @param[out] CertOffset Offset of matching CertData, from starting of Data.
1193 @param[out] CertDataSize Length of CertData in bytes.
1194 @param[out] CertNodeOffset Offset of matching AUTH_CERT_DB_DATA , from
1196 @param[out] CertNodeSize Length of AUTH_CERT_DB_DATA in bytes.
1198 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1199 @retval EFI_NOT_FOUND Fail to find matching certs.
1200 @retval EFI_SUCCESS Find matching certs and output parameters.
1205 IN CHAR16
*VariableName
,
1206 IN EFI_GUID
*VendorGuid
,
1209 OUT UINT32
*CertOffset OPTIONAL
,
1210 OUT UINT32
*CertDataSize OPTIONAL
,
1211 OUT UINT32
*CertNodeOffset OPTIONAL
,
1212 OUT UINT32
*CertNodeSize OPTIONAL
1216 AUTH_CERT_DB_DATA
*Ptr
;
1220 UINT32 CertDbListSize
;
1222 if ((VariableName
== NULL
) || (VendorGuid
== NULL
) || (Data
== NULL
)) {
1223 return EFI_INVALID_PARAMETER
;
1227 // Check whether DataSize matches recorded CertDbListSize.
1229 if (DataSize
< sizeof (UINT32
)) {
1230 return EFI_INVALID_PARAMETER
;
1233 CertDbListSize
= ReadUnaligned32 ((UINT32
*)Data
);
1235 if (CertDbListSize
!= (UINT32
)DataSize
) {
1236 return EFI_INVALID_PARAMETER
;
1239 Offset
= sizeof (UINT32
);
1242 // Get corresponding certificates by VendorGuid and VariableName.
1244 while (Offset
< (UINT32
)DataSize
) {
1245 Ptr
= (AUTH_CERT_DB_DATA
*)(Data
+ Offset
);
1247 // Check whether VendorGuid matches.
1249 if (CompareGuid (&Ptr
->VendorGuid
, VendorGuid
)) {
1250 NodeSize
= ReadUnaligned32 (&Ptr
->CertNodeSize
);
1251 NameSize
= ReadUnaligned32 (&Ptr
->NameSize
);
1252 CertSize
= ReadUnaligned32 (&Ptr
->CertDataSize
);
1254 if (NodeSize
!= sizeof (EFI_GUID
) + sizeof (UINT32
) * 3 + CertSize
+
1255 sizeof (CHAR16
) * NameSize
)
1257 return EFI_INVALID_PARAMETER
;
1260 Offset
= Offset
+ sizeof (EFI_GUID
) + sizeof (UINT32
) * 3;
1262 // Check whether VariableName matches.
1264 if ((NameSize
== StrLen (VariableName
)) &&
1265 (CompareMem (Data
+ Offset
, VariableName
, NameSize
* sizeof (CHAR16
)) == 0))
1267 Offset
= Offset
+ NameSize
* sizeof (CHAR16
);
1269 if (CertOffset
!= NULL
) {
1270 *CertOffset
= Offset
;
1273 if (CertDataSize
!= NULL
) {
1274 *CertDataSize
= CertSize
;
1277 if (CertNodeOffset
!= NULL
) {
1278 *CertNodeOffset
= (UINT32
)((UINT8
*)Ptr
- Data
);
1281 if (CertNodeSize
!= NULL
) {
1282 *CertNodeSize
= NodeSize
;
1287 Offset
= Offset
+ NameSize
* sizeof (CHAR16
) + CertSize
;
1290 NodeSize
= ReadUnaligned32 (&Ptr
->CertNodeSize
);
1291 Offset
= Offset
+ NodeSize
;
1295 return EFI_NOT_FOUND
;
1299 Retrieve signer's certificates for common authenticated variable
1300 by corresponding VariableName and VendorGuid from "certdb"
1301 or "certdbv" according to authenticated variable attributes.
1303 @param[in] VariableName Name of authenticated Variable.
1304 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1305 @param[in] Attributes Attributes of authenticated variable.
1306 @param[out] CertData Pointer to signer's certificates.
1307 @param[out] CertDataSize Length of CertData in bytes.
1309 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1310 @retval EFI_NOT_FOUND Fail to find "certdb"/"certdbv" or matching certs.
1311 @retval EFI_SUCCESS Get signer's certificates successfully.
1316 IN CHAR16
*VariableName
,
1317 IN EFI_GUID
*VendorGuid
,
1318 IN UINT32 Attributes
,
1319 OUT UINT8
**CertData
,
1320 OUT UINT32
*CertDataSize
1329 if ((VariableName
== NULL
) || (VendorGuid
== NULL
) || (CertData
== NULL
) || (CertDataSize
== NULL
)) {
1330 return EFI_INVALID_PARAMETER
;
1333 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
1335 // Get variable "certdb".
1337 DbName
= EFI_CERT_DB_NAME
;
1340 // Get variable "certdbv".
1342 DbName
= EFI_CERT_DB_VOLATILE_NAME
;
1346 // Get variable "certdb" or "certdbv".
1348 Status
= AuthServiceInternalFindVariable (
1354 if (EFI_ERROR (Status
)) {
1358 if ((DataSize
== 0) || (Data
== NULL
)) {
1360 return EFI_NOT_FOUND
;
1363 Status
= FindCertsFromDb (
1374 if (EFI_ERROR (Status
)) {
1378 *CertData
= Data
+ CertOffset
;
1383 Delete matching signer's certificates when deleting common authenticated
1384 variable by corresponding VariableName and VendorGuid from "certdb" or
1385 "certdbv" according to authenticated variable attributes.
1387 @param[in] VariableName Name of authenticated Variable.
1388 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1389 @param[in] Attributes Attributes of authenticated variable.
1391 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1392 @retval EFI_NOT_FOUND Fail to find "certdb"/"certdbv" or matching certs.
1393 @retval EFI_OUT_OF_RESOURCES The operation is failed due to lack of resources.
1394 @retval EFI_SUCCESS The operation is completed successfully.
1399 IN CHAR16
*VariableName
,
1400 IN EFI_GUID
*VendorGuid
,
1401 IN UINT32 Attributes
1408 UINT32 CertNodeOffset
;
1409 UINT32 CertNodeSize
;
1411 UINT32 NewCertDbSize
;
1414 if ((VariableName
== NULL
) || (VendorGuid
== NULL
)) {
1415 return EFI_INVALID_PARAMETER
;
1418 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
1420 // Get variable "certdb".
1422 DbName
= EFI_CERT_DB_NAME
;
1423 VarAttr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
1426 // Get variable "certdbv".
1428 DbName
= EFI_CERT_DB_VOLATILE_NAME
;
1429 VarAttr
= EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
1432 Status
= AuthServiceInternalFindVariable (
1439 if (EFI_ERROR (Status
)) {
1443 if ((DataSize
== 0) || (Data
== NULL
)) {
1445 return EFI_NOT_FOUND
;
1448 if (DataSize
== sizeof (UINT32
)) {
1450 // There is no certs in "certdb" or "certdbv".
1456 // Get corresponding cert node from "certdb" or "certdbv".
1458 Status
= FindCertsFromDb (
1469 if (EFI_ERROR (Status
)) {
1473 if (DataSize
< (CertNodeOffset
+ CertNodeSize
)) {
1474 return EFI_NOT_FOUND
;
1478 // Construct new data content of variable "certdb" or "certdbv".
1480 NewCertDbSize
= (UINT32
)DataSize
- CertNodeSize
;
1481 NewCertDb
= (UINT8
*)mCertDbStore
;
1484 // Copy the DB entries before deleting node.
1486 CopyMem (NewCertDb
, Data
, CertNodeOffset
);
1488 // Update CertDbListSize.
1490 CopyMem (NewCertDb
, &NewCertDbSize
, sizeof (UINT32
));
1492 // Copy the DB entries after deleting node.
1494 if (DataSize
> (CertNodeOffset
+ CertNodeSize
)) {
1496 NewCertDb
+ CertNodeOffset
,
1497 Data
+ CertNodeOffset
+ CertNodeSize
,
1498 DataSize
- CertNodeOffset
- CertNodeSize
1503 // Set "certdb" or "certdbv".
1505 Status
= AuthServiceInternalUpdateVariable (
1517 Insert signer's certificates for common authenticated variable with VariableName
1518 and VendorGuid in AUTH_CERT_DB_DATA to "certdb" or "certdbv" according to
1519 time based authenticated variable attributes. CertData is the SHA256 digest of
1520 SignerCert CommonName + TopLevelCert tbsCertificate.
1522 @param[in] VariableName Name of authenticated Variable.
1523 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1524 @param[in] Attributes Attributes of authenticated variable.
1525 @param[in] SignerCert Signer certificate data.
1526 @param[in] SignerCertSize Length of signer certificate.
1527 @param[in] TopLevelCert Top-level certificate data.
1528 @param[in] TopLevelCertSize Length of top-level certificate.
1530 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1531 @retval EFI_ACCESS_DENIED An AUTH_CERT_DB_DATA entry with same VariableName
1532 and VendorGuid already exists.
1533 @retval EFI_OUT_OF_RESOURCES The operation is failed due to lack of resources.
1534 @retval EFI_SUCCESS Insert an AUTH_CERT_DB_DATA entry to "certdb" or "certdbv"
1539 IN CHAR16
*VariableName
,
1540 IN EFI_GUID
*VendorGuid
,
1541 IN UINT32 Attributes
,
1542 IN UINT8
*SignerCert
,
1543 IN UINTN SignerCertSize
,
1544 IN UINT8
*TopLevelCert
,
1545 IN UINTN TopLevelCertSize
1553 UINT32 NewCertDbSize
;
1554 UINT32 CertNodeSize
;
1556 UINT32 CertDataSize
;
1557 AUTH_CERT_DB_DATA
*Ptr
;
1559 UINT8 Sha256Digest
[SHA256_DIGEST_SIZE
];
1561 if ((VariableName
== NULL
) || (VendorGuid
== NULL
) || (SignerCert
== NULL
) || (TopLevelCert
== NULL
)) {
1562 return EFI_INVALID_PARAMETER
;
1565 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
1567 // Get variable "certdb".
1569 DbName
= EFI_CERT_DB_NAME
;
1570 VarAttr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
1573 // Get variable "certdbv".
1575 DbName
= EFI_CERT_DB_VOLATILE_NAME
;
1576 VarAttr
= EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
1580 // Get variable "certdb" or "certdbv".
1582 Status
= AuthServiceInternalFindVariable (
1588 if (EFI_ERROR (Status
)) {
1592 if ((DataSize
== 0) || (Data
== NULL
)) {
1594 return EFI_NOT_FOUND
;
1598 // Find whether matching cert node already exists in "certdb" or "certdbv".
1599 // If yes return error.
1601 Status
= FindCertsFromDb (
1612 if (!EFI_ERROR (Status
)) {
1614 return EFI_ACCESS_DENIED
;
1618 // Construct new data content of variable "certdb" or "certdbv".
1620 NameSize
= (UINT32
)StrLen (VariableName
);
1621 CertDataSize
= sizeof (Sha256Digest
);
1622 CertNodeSize
= sizeof (AUTH_CERT_DB_DATA
) + (UINT32
)CertDataSize
+ NameSize
* sizeof (CHAR16
);
1623 NewCertDbSize
= (UINT32
)DataSize
+ CertNodeSize
;
1624 if (NewCertDbSize
> mMaxCertDbSize
) {
1625 return EFI_OUT_OF_RESOURCES
;
1628 Status
= CalculatePrivAuthVarSignChainSHA256Digest (
1635 if (EFI_ERROR (Status
)) {
1639 NewCertDb
= (UINT8
*)mCertDbStore
;
1642 // Copy the DB entries before inserting node.
1644 CopyMem (NewCertDb
, Data
, DataSize
);
1646 // Update CertDbListSize.
1648 CopyMem (NewCertDb
, &NewCertDbSize
, sizeof (UINT32
));
1650 // Construct new cert node.
1652 Ptr
= (AUTH_CERT_DB_DATA
*)(NewCertDb
+ DataSize
);
1653 CopyGuid (&Ptr
->VendorGuid
, VendorGuid
);
1654 CopyMem (&Ptr
->CertNodeSize
, &CertNodeSize
, sizeof (UINT32
));
1655 CopyMem (&Ptr
->NameSize
, &NameSize
, sizeof (UINT32
));
1656 CopyMem (&Ptr
->CertDataSize
, &CertDataSize
, sizeof (UINT32
));
1659 (UINT8
*)Ptr
+ sizeof (AUTH_CERT_DB_DATA
),
1661 NameSize
* sizeof (CHAR16
)
1665 (UINT8
*)Ptr
+ sizeof (AUTH_CERT_DB_DATA
) + NameSize
* sizeof (CHAR16
),
1671 // Set "certdb" or "certdbv".
1673 Status
= AuthServiceInternalUpdateVariable (
1685 Clean up signer's certificates for common authenticated variable
1686 by corresponding VariableName and VendorGuid from "certdb".
1687 System may break down during Timebased Variable update & certdb update,
1688 make them inconsistent, this function is called in AuthVariable Init
1689 to ensure consistency.
1691 @retval EFI_NOT_FOUND Fail to find variable "certdb".
1692 @retval EFI_OUT_OF_RESOURCES The operation is failed due to lack of resources.
1693 @retval EFI_SUCCESS The operation is completed successfully.
1702 AUTH_CERT_DB_DATA
*Ptr
;
1705 CHAR16
*VariableName
;
1707 BOOLEAN CertCleaned
;
1710 EFI_GUID AuthVarGuid
;
1711 AUTH_VARIABLE_INFO AuthVariableInfo
;
1713 Status
= EFI_SUCCESS
;
1716 // Get corresponding certificates by VendorGuid and VariableName.
1719 CertCleaned
= FALSE
;
1722 // Get latest variable "certdb"
1724 Status
= AuthServiceInternalFindVariable (
1730 if (EFI_ERROR (Status
)) {
1734 if ((DataSize
== 0) || (Data
== NULL
)) {
1736 return EFI_NOT_FOUND
;
1739 Offset
= sizeof (UINT32
);
1741 while (Offset
< (UINT32
)DataSize
) {
1742 Ptr
= (AUTH_CERT_DB_DATA
*)(Data
+ Offset
);
1743 NodeSize
= ReadUnaligned32 (&Ptr
->CertNodeSize
);
1744 NameSize
= ReadUnaligned32 (&Ptr
->NameSize
);
1747 // Get VarName tailed with '\0'
1749 VariableName
= AllocateZeroPool ((NameSize
+ 1) * sizeof (CHAR16
));
1750 if (VariableName
== NULL
) {
1751 return EFI_OUT_OF_RESOURCES
;
1754 CopyMem (VariableName
, (UINT8
*)Ptr
+ sizeof (AUTH_CERT_DB_DATA
), NameSize
* sizeof (CHAR16
));
1756 // Keep VarGuid aligned
1758 CopyMem (&AuthVarGuid
, &Ptr
->VendorGuid
, sizeof (EFI_GUID
));
1761 // Find corresponding time auth variable
1763 ZeroMem (&AuthVariableInfo
, sizeof (AuthVariableInfo
));
1764 Status
= mAuthVarLibContextIn
->FindVariable (
1770 if (EFI_ERROR (Status
) || ((AuthVariableInfo
.Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) == 0)) {
1772 // While cleaning certdb, always delete the variable in certdb regardless of it attributes.
1774 Status
= DeleteCertsFromDb (
1777 AuthVariableInfo
.Attributes
| EFI_VARIABLE_NON_VOLATILE
1780 DEBUG ((DEBUG_INFO
, "Recovery!! Cert for Auth Variable %s Guid %g is removed for consistency\n", VariableName
, &AuthVarGuid
));
1781 FreePool (VariableName
);
1785 FreePool (VariableName
);
1786 Offset
= Offset
+ NodeSize
;
1788 } while (CertCleaned
);
1794 Process variable with EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set
1796 Caution: This function may receive untrusted input.
1797 This function may be invoked in SMM mode, and datasize and data are external input.
1798 This function will do basic validation, before parse the data.
1799 This function will parse the authentication carefully to avoid security issues, like
1800 buffer overflow, integer overflow.
1802 @param[in] VariableName Name of Variable to be found.
1803 @param[in] VendorGuid Variable vendor GUID.
1804 @param[in] Data Data pointer.
1805 @param[in] DataSize Size of Data found. If size is less than the
1806 data, this value contains the required size.
1807 @param[in] Attributes Attribute value of the variable.
1808 @param[in] AuthVarType Verify against PK, KEK database, private database or certificate in data payload.
1809 @param[in] OrgTimeStamp Pointer to original time stamp,
1810 original variable is not found if NULL.
1811 @param[out] VarPayloadPtr Pointer to variable payload address.
1812 @param[out] VarPayloadSize Pointer to variable payload size.
1814 @retval EFI_INVALID_PARAMETER Invalid parameter.
1815 @retval EFI_SECURITY_VIOLATION The variable does NOT pass the validation
1816 check carried out by the firmware.
1817 @retval EFI_OUT_OF_RESOURCES Failed to process variable due to lack
1819 @retval EFI_SUCCESS Variable pass validation successfully.
1823 VerifyTimeBasedPayload (
1824 IN CHAR16
*VariableName
,
1825 IN EFI_GUID
*VendorGuid
,
1828 IN UINT32 Attributes
,
1829 IN AUTHVAR_TYPE AuthVarType
,
1830 IN EFI_TIME
*OrgTimeStamp
,
1831 OUT UINT8
**VarPayloadPtr
,
1832 OUT UINTN
*VarPayloadSize
1835 EFI_VARIABLE_AUTHENTICATION_2
*CertData
;
1841 BOOLEAN VerifyStatus
;
1843 EFI_SIGNATURE_LIST
*CertList
;
1844 EFI_SIGNATURE_DATA
*Cert
;
1852 UINT8
*TopLevelCert
;
1853 UINTN TopLevelCertSize
;
1855 UINTN TrustedCertSize
;
1857 UINTN CertStackSize
;
1858 UINT8
*CertsInCertDb
;
1859 UINT32 CertsSizeinDb
;
1860 UINT8 Sha256Digest
[SHA256_DIGEST_SIZE
];
1861 EFI_CERT_DATA
*CertDataPtr
;
1864 // 1. TopLevelCert is the top-level issuer certificate in signature Signer Cert Chain
1865 // 2. TrustedCert is the certificate which firmware trusts. It could be saved in protected
1866 // storage or PK payload on PK init
1868 VerifyStatus
= FALSE
;
1873 TopLevelCert
= NULL
;
1874 CertsInCertDb
= NULL
;
1878 // When the attribute EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS is
1879 // set, then the Data buffer shall begin with an instance of a complete (and serialized)
1880 // EFI_VARIABLE_AUTHENTICATION_2 descriptor. The descriptor shall be followed by the new
1881 // variable value and DataSize shall reflect the combined size of the descriptor and the new
1882 // variable value. The authentication descriptor is not part of the variable data and is not
1883 // returned by subsequent calls to GetVariable().
1885 CertData
= (EFI_VARIABLE_AUTHENTICATION_2
*)Data
;
1888 // Verify that Pad1, Nanosecond, TimeZone, Daylight and Pad2 components of the
1889 // TimeStamp value are set to zero.
1891 if ((CertData
->TimeStamp
.Pad1
!= 0) ||
1892 (CertData
->TimeStamp
.Nanosecond
!= 0) ||
1893 (CertData
->TimeStamp
.TimeZone
!= 0) ||
1894 (CertData
->TimeStamp
.Daylight
!= 0) ||
1895 (CertData
->TimeStamp
.Pad2
!= 0))
1897 return EFI_SECURITY_VIOLATION
;
1900 if ((OrgTimeStamp
!= NULL
) && ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0)) {
1901 if (AuthServiceInternalCompareTimeStamp (&CertData
->TimeStamp
, OrgTimeStamp
)) {
1903 // TimeStamp check fail, suspicious replay attack, return EFI_SECURITY_VIOLATION.
1905 return EFI_SECURITY_VIOLATION
;
1910 // wCertificateType should be WIN_CERT_TYPE_EFI_GUID.
1911 // Cert type should be EFI_CERT_TYPE_PKCS7_GUID.
1913 if ((CertData
->AuthInfo
.Hdr
.wCertificateType
!= WIN_CERT_TYPE_EFI_GUID
) ||
1914 !CompareGuid (&CertData
->AuthInfo
.CertType
, &gEfiCertPkcs7Guid
))
1917 // Invalid AuthInfo type, return EFI_SECURITY_VIOLATION.
1919 return EFI_SECURITY_VIOLATION
;
1923 // Find out Pkcs7 SignedData which follows the EFI_VARIABLE_AUTHENTICATION_2 descriptor.
1924 // AuthInfo.Hdr.dwLength is the length of the entire certificate, including the length of the header.
1926 SigData
= CertData
->AuthInfo
.CertData
;
1927 SigDataSize
= CertData
->AuthInfo
.Hdr
.dwLength
- (UINT32
)(OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID
, CertData
));
1930 // SignedData.digestAlgorithms shall contain the digest algorithm used when preparing the
1931 // signature. Only a digest algorithm of SHA-256 is accepted.
1933 // According to PKCS#7 Definition (https://www.rfc-editor.org/rfc/rfc2315):
1934 // SignedData ::= SEQUENCE {
1936 // digestAlgorithms DigestAlgorithmIdentifiers,
1937 // contentInfo ContentInfo,
1939 // The DigestAlgorithmIdentifiers can be used to determine the hash algorithm
1940 // in VARIABLE_AUTHENTICATION_2 descriptor.
1941 // This field has the fixed offset (+13) or (+32) based on whether the DER-encoded
1942 // ContentInfo structure is present or not, and can be calculated based on two
1943 // bytes of length encoding.
1945 // Both condition can be handled in WrapPkcs7Data() in CryptPkcs7VerifyCommon.c.
1947 // See below examples:
1949 // 1. Without ContentInfo
1950 // 30 82 0c da // SEQUENCE (5 element) (3294 BYTES) -- SignedData
1951 // 02 01 01 // INTEGER 1 -- Version
1952 // 31 0f // SET (1 element) (15 BYTES) -- DigestAlgorithmIdentifiers
1953 // 30 0d // SEQUENCE (2 element) (13 BYTES) -- AlgorithmIdentifier
1954 // 06 09 // OBJECT-IDENTIFIER (9 BYTES) -- algorithm
1955 // 60 86 48 01 65 03 04 02 01 // sha256 [2.16.840.1.101.3.4.2.1]
1956 // 05 00 // NULL (0 BYTES) -- parameters
1958 // Example from: https://uefi.org/revocationlistfile
1960 // 2. With ContentInfo
1961 // 30 82 05 90 // SEQUENCE (1424 BYTES) -- ContentInfo
1962 // 06 09 // OBJECT-IDENTIFIER (9 BYTES) -- ContentType
1963 // 2a 86 48 86 f7 0d 01 07 02 // signedData [1.2.840.113549.1.7.2]
1964 // a0 82 05 81 // CONTEXT-SPECIFIC CONSTRUCTED TAG 0 (1409 BYTES) -- content
1965 // 30 82 05 7d // SEQUENCE (1405 BYTES) -- SignedData
1966 // 02 01 01 // INTEGER 1 -- Version
1967 // 31 0f // SET (1 element) (15 BYTES) -- DigestAlgorithmIdentifiers
1968 // 30 0d // SEQUENCE (13 BYTES) -- AlgorithmIdentifier
1969 // 06 09 // OBJECT-IDENTIFIER (9 BYTES) -- algorithm
1970 // 60 86 48 01 65 03 04 02 01 // sha256 [2.16.840.1.101.3.4.2.1]
1971 // 05 00 // NULL (0 BYTES) -- parameters
1973 // Example generated with: https://wiki.archlinux.org/title/Unified_Extensible_Firmware_Interface/Secure_Boot#Manual_process
1975 if ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) {
1976 if ( ( (SigDataSize
>= (13 + sizeof (mSha256OidValue
)))
1977 && ( ((*(SigData
+ 1) & TWO_BYTE_ENCODE
) != TWO_BYTE_ENCODE
)
1978 || (CompareMem (SigData
+ 13, &mSha256OidValue
, sizeof (mSha256OidValue
)) != 0)))
1979 && ( (SigDataSize
>= (32 + sizeof (mSha256OidValue
)))
1980 && ( ((*(SigData
+ 20) & TWO_BYTE_ENCODE
) != TWO_BYTE_ENCODE
)
1981 || (CompareMem (SigData
+ 32, &mSha256OidValue
, sizeof (mSha256OidValue
)) != 0))))
1983 return EFI_SECURITY_VIOLATION
;
1988 // Find out the new data payload which follows Pkcs7 SignedData directly.
1990 PayloadPtr
= SigData
+ SigDataSize
;
1991 PayloadSize
= DataSize
- OFFSET_OF_AUTHINFO2_CERT_DATA
- (UINTN
)SigDataSize
;
1993 // If the VariablePolicy engine is disabled, allow deletion of any authenticated variables.
1994 if ((PayloadSize
== 0) && ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0) && !IsVariablePolicyEnabled ()) {
1995 VerifyStatus
= TRUE
;
2000 // Construct a serialization buffer of the values of the VariableName, VendorGuid and Attributes
2001 // parameters of the SetVariable() call and the TimeStamp component of the
2002 // EFI_VARIABLE_AUTHENTICATION_2 descriptor followed by the variable's new value
2003 // i.e. (VariableName, VendorGuid, Attributes, TimeStamp, Data)
2005 NewDataSize
= PayloadSize
+ sizeof (EFI_TIME
) + sizeof (UINT32
) +
2006 sizeof (EFI_GUID
) + StrSize (VariableName
) - sizeof (CHAR16
);
2009 // Here is to reuse scratch data area(at the end of volatile variable store)
2010 // to reduce SMRAM consumption for SMM variable driver.
2011 // The scratch buffer is enough to hold the serialized data and safe to use,
2012 // because it is only used at here to do verification temporarily first
2013 // and then used in UpdateVariable() for a time based auth variable set.
2015 Status
= mAuthVarLibContextIn
->GetScratchBuffer (&NewDataSize
, (VOID
**)&NewData
);
2016 if (EFI_ERROR (Status
)) {
2017 return EFI_OUT_OF_RESOURCES
;
2021 Length
= StrLen (VariableName
) * sizeof (CHAR16
);
2022 CopyMem (Buffer
, VariableName
, Length
);
2025 Length
= sizeof (EFI_GUID
);
2026 CopyMem (Buffer
, VendorGuid
, Length
);
2029 Length
= sizeof (UINT32
);
2030 CopyMem (Buffer
, &Attr
, Length
);
2033 Length
= sizeof (EFI_TIME
);
2034 CopyMem (Buffer
, &CertData
->TimeStamp
, Length
);
2037 CopyMem (Buffer
, PayloadPtr
, PayloadSize
);
2039 if (AuthVarType
== AuthVarTypePk
) {
2041 // Verify that the signature has been made with the current Platform Key (no chaining for PK).
2042 // First, get signer's certificates from SignedData.
2044 VerifyStatus
= Pkcs7GetSigners (
2052 if (!VerifyStatus
) {
2057 // Second, get the current platform key from variable. Check whether it's identical with signer's certificates
2058 // in SignedData. If not, return error immediately.
2060 Status
= AuthServiceInternalFindVariable (
2061 EFI_PLATFORM_KEY_NAME
,
2062 &gEfiGlobalVariableGuid
,
2066 if (EFI_ERROR (Status
)) {
2067 VerifyStatus
= FALSE
;
2071 CertList
= (EFI_SIGNATURE_LIST
*)Data
;
2072 Cert
= (EFI_SIGNATURE_DATA
*)((UINT8
*)CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
2073 if ((TopLevelCertSize
!= (CertList
->SignatureSize
- (sizeof (EFI_SIGNATURE_DATA
) - 1))) ||
2074 (CompareMem (Cert
->SignatureData
, TopLevelCert
, TopLevelCertSize
) != 0))
2076 VerifyStatus
= FALSE
;
2081 // Verify Pkcs7 SignedData via Pkcs7Verify library.
2083 VerifyStatus
= Pkcs7Verify (
2091 } else if (AuthVarType
== AuthVarTypeKek
) {
2093 // Get KEK database from variable.
2095 Status
= AuthServiceInternalFindVariable (
2096 EFI_KEY_EXCHANGE_KEY_NAME
,
2097 &gEfiGlobalVariableGuid
,
2101 if (EFI_ERROR (Status
)) {
2106 // Ready to verify Pkcs7 SignedData. Go through KEK Signature Database to find out X.509 CertList.
2108 KekDataSize
= (UINT32
)DataSize
;
2109 CertList
= (EFI_SIGNATURE_LIST
*)Data
;
2110 while ((KekDataSize
> 0) && (KekDataSize
>= CertList
->SignatureListSize
)) {
2111 if (CompareGuid (&CertList
->SignatureType
, &gEfiCertX509Guid
)) {
2112 Cert
= (EFI_SIGNATURE_DATA
*)((UINT8
*)CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
2113 CertCount
= (CertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - CertList
->SignatureHeaderSize
) / CertList
->SignatureSize
;
2114 for (Index
= 0; Index
< CertCount
; Index
++) {
2116 // Iterate each Signature Data Node within this CertList for a verify
2118 TrustedCert
= Cert
->SignatureData
;
2119 TrustedCertSize
= CertList
->SignatureSize
- (sizeof (EFI_SIGNATURE_DATA
) - 1);
2122 // Verify Pkcs7 SignedData via Pkcs7Verify library.
2124 VerifyStatus
= Pkcs7Verify (
2136 Cert
= (EFI_SIGNATURE_DATA
*)((UINT8
*)Cert
+ CertList
->SignatureSize
);
2140 KekDataSize
-= CertList
->SignatureListSize
;
2141 CertList
= (EFI_SIGNATURE_LIST
*)((UINT8
*)CertList
+ CertList
->SignatureListSize
);
2143 } else if (AuthVarType
== AuthVarTypePriv
) {
2145 // Process common authenticated variable except PK/KEK/DB/DBX/DBT.
2146 // Get signer's certificates from SignedData.
2148 VerifyStatus
= Pkcs7GetSigners (
2156 if (!VerifyStatus
) {
2161 // Get previously stored signer's certificates from certdb or certdbv for existing
2162 // variable. Check whether they are identical with signer's certificates
2163 // in SignedData. If not, return error immediately.
2165 if (OrgTimeStamp
!= NULL
) {
2166 VerifyStatus
= FALSE
;
2168 Status
= GetCertsFromDb (VariableName
, VendorGuid
, Attributes
, &CertsInCertDb
, &CertsSizeinDb
);
2169 if (EFI_ERROR (Status
)) {
2173 if (CertsSizeinDb
== SHA256_DIGEST_SIZE
) {
2175 // Check hash of signer cert CommonName + Top-level issuer tbsCertificate against data in CertDb
2177 CertDataPtr
= (EFI_CERT_DATA
*)(SignerCerts
+ 1);
2178 Status
= CalculatePrivAuthVarSignChainSHA256Digest (
2179 CertDataPtr
->CertDataBuffer
,
2180 ReadUnaligned32 ((UINT32
*)&(CertDataPtr
->CertDataLength
)),
2185 if (EFI_ERROR (Status
) || (CompareMem (Sha256Digest
, CertsInCertDb
, CertsSizeinDb
) != 0)) {
2190 // Keep backward compatible with previous solution which saves whole signer certs stack in CertDb
2192 if ((CertStackSize
!= CertsSizeinDb
) ||
2193 (CompareMem (SignerCerts
, CertsInCertDb
, CertsSizeinDb
) != 0))
2200 VerifyStatus
= Pkcs7Verify (
2208 if (!VerifyStatus
) {
2212 if ((OrgTimeStamp
== NULL
) && (PayloadSize
!= 0)) {
2214 // When adding a new common authenticated variable, always save Hash of cn of signer cert + tbsCertificate of Top-level issuer
2216 CertDataPtr
= (EFI_CERT_DATA
*)(SignerCerts
+ 1);
2217 Status
= InsertCertsToDb (
2221 CertDataPtr
->CertDataBuffer
,
2222 ReadUnaligned32 ((UINT32
*)&(CertDataPtr
->CertDataLength
)),
2226 if (EFI_ERROR (Status
)) {
2227 VerifyStatus
= FALSE
;
2231 } else if (AuthVarType
== AuthVarTypePayload
) {
2232 CertList
= (EFI_SIGNATURE_LIST
*)PayloadPtr
;
2233 Cert
= (EFI_SIGNATURE_DATA
*)((UINT8
*)CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
2234 TrustedCert
= Cert
->SignatureData
;
2235 TrustedCertSize
= CertList
->SignatureSize
- (sizeof (EFI_SIGNATURE_DATA
) - 1);
2237 // Verify Pkcs7 SignedData via Pkcs7Verify library.
2239 VerifyStatus
= Pkcs7Verify (
2248 return EFI_SECURITY_VIOLATION
;
2253 if ((AuthVarType
== AuthVarTypePk
) || (AuthVarType
== AuthVarTypePriv
)) {
2254 if (TopLevelCert
!= NULL
) {
2255 Pkcs7FreeSigners (TopLevelCert
);
2258 if (SignerCerts
!= NULL
) {
2259 Pkcs7FreeSigners (SignerCerts
);
2263 if (!VerifyStatus
) {
2264 return EFI_SECURITY_VIOLATION
;
2267 Status
= CheckSignatureListFormat (VariableName
, VendorGuid
, PayloadPtr
, PayloadSize
);
2268 if (EFI_ERROR (Status
)) {
2272 *VarPayloadPtr
= PayloadPtr
;
2273 *VarPayloadSize
= PayloadSize
;
2279 Process variable with EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set
2281 Caution: This function may receive untrusted input.
2282 This function may be invoked in SMM mode, and datasize and data are external input.
2283 This function will do basic validation, before parse the data.
2284 This function will parse the authentication carefully to avoid security issues, like
2285 buffer overflow, integer overflow.
2287 @param[in] VariableName Name of Variable to be found.
2288 @param[in] VendorGuid Variable vendor GUID.
2289 @param[in] Data Data pointer.
2290 @param[in] DataSize Size of Data found. If size is less than the
2291 data, this value contains the required size.
2292 @param[in] Attributes Attribute value of the variable.
2293 @param[in] AuthVarType Verify against PK, KEK database, private database or certificate in data payload.
2294 @param[out] VarDel Delete the variable or not.
2296 @retval EFI_INVALID_PARAMETER Invalid parameter.
2297 @retval EFI_SECURITY_VIOLATION The variable does NOT pass the validation
2298 check carried out by the firmware.
2299 @retval EFI_OUT_OF_RESOURCES Failed to process variable due to lack
2301 @retval EFI_SUCCESS Variable pass validation successfully.
2305 VerifyTimeBasedPayloadAndUpdate (
2306 IN CHAR16
*VariableName
,
2307 IN EFI_GUID
*VendorGuid
,
2310 IN UINT32 Attributes
,
2311 IN AUTHVAR_TYPE AuthVarType
,
2316 EFI_STATUS FindStatus
;
2319 EFI_VARIABLE_AUTHENTICATION_2
*CertData
;
2320 AUTH_VARIABLE_INFO OrgVariableInfo
;
2323 ZeroMem (&OrgVariableInfo
, sizeof (OrgVariableInfo
));
2324 FindStatus
= mAuthVarLibContextIn
->FindVariable (
2330 Status
= VerifyTimeBasedPayload (
2337 (!EFI_ERROR (FindStatus
)) ? OrgVariableInfo
.TimeStamp
: NULL
,
2341 if (EFI_ERROR (Status
)) {
2345 if ( !EFI_ERROR (FindStatus
)
2346 && (PayloadSize
== 0)
2347 && ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0))
2354 CertData
= (EFI_VARIABLE_AUTHENTICATION_2
*)Data
;
2357 // Final step: Update/Append Variable if it pass Pkcs7Verify
2359 Status
= AuthServiceInternalUpdateVariableWithTimeStamp (
2365 &CertData
->TimeStamp
2369 // Delete signer's certificates when delete the common authenticated variable.
2371 if (IsDel
&& (AuthVarType
== AuthVarTypePriv
) && !EFI_ERROR (Status
)) {
2372 Status
= DeleteCertsFromDb (VariableName
, VendorGuid
, Attributes
);
2375 if (VarDel
!= NULL
) {
2376 if (IsDel
&& !EFI_ERROR (Status
)) {