2 Implement authentication services for the authenticated variable
5 Caution: This module requires additional review when modified.
6 This driver will have external input - variable data. It may be input in SMM mode.
7 This external input must be validated carefully to avoid security issue like
8 buffer overflow, integer overflow.
9 Variable attribute should also be checked to avoid authentication bypass.
11 ProcessVarWithPk(), ProcessVarWithKek() and ProcessVariable() are the function to do
12 variable authentication.
14 VerifyTimeBasedPayload() and VerifyCounterBasedPayload() are sub function to do verification.
15 They will do basic validation for authentication data structure, then call crypto library
16 to verify the signature.
18 Copyright (c) 2009 - 2013, Intel Corporation. All rights reserved.<BR>
19 This program and the accompanying materials
20 are licensed and made available under the terms and conditions of the BSD License
21 which accompanies this distribution. The full text of the license may be found at
22 http://opensource.org/licenses/bsd-license.php
24 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
25 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
30 #include "AuthService.h"
33 /// Global database array for scratch
35 UINT8 mPubKeyStore
[MAX_KEYDB_SIZE
];
37 UINT8 mCertDbStore
[MAX_CERTDB_SIZE
];
39 UINT8 mVendorKeyState
;
41 EFI_GUID mSignatureSupport
[] = {EFI_CERT_SHA1_GUID
, EFI_CERT_SHA256_GUID
, EFI_CERT_RSA2048_GUID
, EFI_CERT_X509_GUID
};
43 // Public Exponent of RSA Key.
45 CONST UINT8 mRsaE
[] = { 0x01, 0x00, 0x01 };
47 // Hash context pointer
49 VOID
*mHashCtx
= NULL
;
52 // Pointer to runtime buffer.
53 // For "Append" operation to an existing variable, a read/modify/write operation
54 // is supported by firmware internally. Reserve runtime buffer to cache previous
55 // variable data in runtime phase because memory allocation is forbidden in virtual mode.
57 VOID
*mStorageArea
= NULL
;
60 // The serialization of the values of the VariableName, VendorGuid and Attributes
61 // parameters of the SetVariable() call and the TimeStamp component of the
62 // EFI_VARIABLE_AUTHENTICATION_2 descriptor followed by the variable's new value
63 // i.e. (VariableName, VendorGuid, Attributes, TimeStamp, Data)
65 UINT8
*mSerializationRuntimeBuffer
= NULL
;
68 // Requirement for different signature type which have been defined in UEFI spec.
69 // These data are used to peform SignatureList format check while setting PK/KEK variable.
71 EFI_SIGNATURE_ITEM mSupportSigItem
[] = {
72 //{SigType, SigHeaderSize, SigDataSize }
73 {EFI_CERT_SHA256_GUID
, 0, 32 },
74 {EFI_CERT_RSA2048_GUID
, 0, 256 },
75 {EFI_CERT_RSA2048_SHA256_GUID
, 0, 256 },
76 {EFI_CERT_SHA1_GUID
, 0, 20 },
77 {EFI_CERT_RSA2048_SHA1_GUID
, 0, 256 },
78 {EFI_CERT_X509_GUID
, 0, ((UINT32
) ~0)},
79 {EFI_CERT_SHA224_GUID
, 0, 28 },
80 {EFI_CERT_SHA384_GUID
, 0, 48 },
81 {EFI_CERT_SHA512_GUID
, 0, 64 }
85 Determine whether this operation needs a physical present user.
87 @param[in] VariableName Name of the Variable.
88 @param[in] VendorGuid GUID of the Variable.
90 @retval TRUE This variable is protected, only a physical present user could set this variable.
91 @retval FALSE This variable is not protected.
95 NeedPhysicallyPresent(
96 IN CHAR16
*VariableName
,
97 IN EFI_GUID
*VendorGuid
100 if ((CompareGuid (VendorGuid
, &gEfiSecureBootEnableDisableGuid
) && (StrCmp (VariableName
, EFI_SECURE_BOOT_ENABLE_NAME
) == 0))
101 || (CompareGuid (VendorGuid
, &gEfiCustomModeEnableGuid
) && (StrCmp (VariableName
, EFI_CUSTOM_MODE_NAME
) == 0))) {
109 Determine whether the platform is operating in Custom Secure Boot mode.
111 @retval TRUE The platform is operating in Custom mode.
112 @retval FALSE The platform is operating in Standard mode.
120 VARIABLE_POINTER_TRACK Variable
;
122 FindVariable (EFI_CUSTOM_MODE_NAME
, &gEfiCustomModeEnableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
123 if (Variable
.CurrPtr
!= NULL
&& *(GetVariableDataPtr (Variable
.CurrPtr
)) == CUSTOM_SECURE_BOOT_MODE
) {
132 Internal function to delete a Variable given its name and GUID, no authentication
135 @param[in] VariableName Name of the Variable.
136 @param[in] VendorGuid GUID of the Variable.
138 @retval EFI_SUCCESS Variable deleted successfully.
139 @retval Others The driver failded to start the device.
144 IN CHAR16
*VariableName
,
145 IN EFI_GUID
*VendorGuid
149 VARIABLE_POINTER_TRACK Variable
;
151 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
152 if (EFI_ERROR (Status
)) {
156 ASSERT (Variable
.CurrPtr
!= NULL
);
157 return UpdateVariable (VariableName
, VendorGuid
, NULL
, 0, 0, 0, 0, &Variable
, NULL
);
161 Initializes for authenticated varibale service.
163 @retval EFI_SUCCESS Function successfully executed.
164 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resources.
168 AutenticatedVariableServiceInitialize (
173 VARIABLE_POINTER_TRACK Variable
;
174 VARIABLE_POINTER_TRACK PkVariable
;
180 UINT8 SecureBootMode
;
181 UINT8 SecureBootEnable
;
186 // Initialize hash context.
188 CtxSize
= Sha256GetContextSize ();
189 mHashCtx
= AllocateRuntimePool (CtxSize
);
190 if (mHashCtx
== NULL
) {
191 return EFI_OUT_OF_RESOURCES
;
195 // Reserved runtime buffer for "Append" operation in virtual mode.
197 mStorageArea
= AllocateRuntimePool (MAX (PcdGet32 (PcdMaxVariableSize
), PcdGet32 (PcdMaxHardwareErrorVariableSize
)));
198 if (mStorageArea
== NULL
) {
199 return EFI_OUT_OF_RESOURCES
;
203 // Prepare runtime buffer for serialized data of time-based authenticated
204 // Variable, i.e. (VariableName, VendorGuid, Attributes, TimeStamp, Data).
206 mSerializationRuntimeBuffer
= AllocateRuntimePool (PcdGet32 (PcdMaxVariableSize
) + sizeof (EFI_GUID
) + sizeof (UINT32
) + sizeof (EFI_TIME
));
207 if (mSerializationRuntimeBuffer
== NULL
) {
208 return EFI_OUT_OF_RESOURCES
;
212 // Check "AuthVarKeyDatabase" variable's existence.
213 // If it doesn't exist, create a new one with initial value of 0 and EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS set.
215 Status
= FindVariable (
217 &gEfiAuthenticatedVariableGuid
,
219 &mVariableModuleGlobal
->VariableGlobal
,
223 if (Variable
.CurrPtr
== NULL
) {
224 VarAttr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
;
227 Status
= UpdateVariable (
229 &gEfiAuthenticatedVariableGuid
,
238 if (EFI_ERROR (Status
)) {
243 // Load database in global variable for cache.
245 DataSize
= DataSizeOfVariable (Variable
.CurrPtr
);
246 Data
= GetVariableDataPtr (Variable
.CurrPtr
);
247 ASSERT ((DataSize
!= 0) && (Data
!= NULL
));
248 CopyMem (mPubKeyStore
, (UINT8
*) Data
, DataSize
);
249 mPubKeyNumber
= (UINT32
) (DataSize
/ EFI_CERT_TYPE_RSA2048_SIZE
);
252 FindVariable (EFI_PLATFORM_KEY_NAME
, &gEfiGlobalVariableGuid
, &PkVariable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
253 if (PkVariable
.CurrPtr
== NULL
) {
254 DEBUG ((EFI_D_INFO
, "Variable %s does not exist.\n", EFI_PLATFORM_KEY_NAME
));
256 DEBUG ((EFI_D_INFO
, "Variable %s exists.\n", EFI_PLATFORM_KEY_NAME
));
260 // Create "SetupMode" variable with BS+RT attribute set.
262 FindVariable (EFI_SETUP_MODE_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
263 if (PkVariable
.CurrPtr
== NULL
) {
264 mPlatformMode
= SETUP_MODE
;
266 mPlatformMode
= USER_MODE
;
268 Status
= UpdateVariable (
270 &gEfiGlobalVariableGuid
,
273 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
279 if (EFI_ERROR (Status
)) {
284 // Create "SignatureSupport" variable with BS+RT attribute set.
286 FindVariable (EFI_SIGNATURE_SUPPORT_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
287 Status
= UpdateVariable (
288 EFI_SIGNATURE_SUPPORT_NAME
,
289 &gEfiGlobalVariableGuid
,
291 sizeof(mSignatureSupport
),
292 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
298 if (EFI_ERROR (Status
)) {
303 // If "SecureBootEnable" variable exists, then update "SecureBoot" variable.
304 // If "SecureBootEnable" variable is SECURE_BOOT_ENABLE and in USER_MODE, Set "SecureBoot" variable to SECURE_BOOT_MODE_ENABLE.
305 // If "SecureBootEnable" variable is SECURE_BOOT_DISABLE, Set "SecureBoot" variable to SECURE_BOOT_MODE_DISABLE.
307 SecureBootEnable
= SECURE_BOOT_DISABLE
;
308 FindVariable (EFI_SECURE_BOOT_ENABLE_NAME
, &gEfiSecureBootEnableDisableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
309 if (Variable
.CurrPtr
!= NULL
) {
310 SecureBootEnable
= *(GetVariableDataPtr (Variable
.CurrPtr
));
311 } else if (mPlatformMode
== USER_MODE
) {
313 // "SecureBootEnable" not exist, initialize it in USER_MODE.
315 SecureBootEnable
= SECURE_BOOT_ENABLE
;
316 Status
= UpdateVariable (
317 EFI_SECURE_BOOT_ENABLE_NAME
,
318 &gEfiSecureBootEnableDisableGuid
,
321 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
327 if (EFI_ERROR (Status
)) {
333 // Create "SecureBoot" variable with BS+RT attribute set.
335 if (SecureBootEnable
== SECURE_BOOT_ENABLE
&& mPlatformMode
== USER_MODE
) {
336 SecureBootMode
= SECURE_BOOT_MODE_ENABLE
;
338 SecureBootMode
= SECURE_BOOT_MODE_DISABLE
;
340 FindVariable (EFI_SECURE_BOOT_MODE_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
341 Status
= UpdateVariable (
342 EFI_SECURE_BOOT_MODE_NAME
,
343 &gEfiGlobalVariableGuid
,
346 EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
352 if (EFI_ERROR (Status
)) {
356 DEBUG ((EFI_D_INFO
, "Variable %s is %x\n", EFI_SETUP_MODE_NAME
, mPlatformMode
));
357 DEBUG ((EFI_D_INFO
, "Variable %s is %x\n", EFI_SECURE_BOOT_MODE_NAME
, SecureBootMode
));
358 DEBUG ((EFI_D_INFO
, "Variable %s is %x\n", EFI_SECURE_BOOT_ENABLE_NAME
, SecureBootEnable
));
361 // Initialize "CustomMode" in STANDARD_SECURE_BOOT_MODE state.
363 FindVariable (EFI_CUSTOM_MODE_NAME
, &gEfiCustomModeEnableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
364 CustomMode
= STANDARD_SECURE_BOOT_MODE
;
365 Status
= UpdateVariable (
366 EFI_CUSTOM_MODE_NAME
,
367 &gEfiCustomModeEnableGuid
,
370 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
376 if (EFI_ERROR (Status
)) {
380 DEBUG ((EFI_D_INFO
, "Variable %s is %x\n", EFI_CUSTOM_MODE_NAME
, CustomMode
));
383 // Check "certdb" variable's existence.
384 // If it doesn't exist, then create a new one with
385 // EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set.
387 Status
= FindVariable (
391 &mVariableModuleGlobal
->VariableGlobal
,
395 if (Variable
.CurrPtr
== NULL
) {
396 VarAttr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
397 ListSize
= sizeof (UINT32
);
398 Status
= UpdateVariable (
409 if (EFI_ERROR (Status
)) {
415 // Check "VendorKeysNv" variable's existence and create "VendorKeys" variable accordingly.
417 FindVariable (EFI_VENDOR_KEYS_NV_VARIABLE_NAME
, &gEfiVendorKeysNvGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
418 if (Variable
.CurrPtr
!= NULL
) {
419 mVendorKeyState
= *(GetVariableDataPtr (Variable
.CurrPtr
));
422 // "VendorKeysNv" not exist, initialize it in VENDOR_KEYS_VALID state.
424 mVendorKeyState
= VENDOR_KEYS_VALID
;
425 Status
= UpdateVariable (
426 EFI_VENDOR_KEYS_NV_VARIABLE_NAME
,
427 &gEfiVendorKeysNvGuid
,
430 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
,
436 if (EFI_ERROR (Status
)) {
442 // Create "VendorKeys" variable with BS+RT attribute set.
444 FindVariable (EFI_VENDOR_KEYS_VARIABLE_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
445 Status
= UpdateVariable (
446 EFI_VENDOR_KEYS_VARIABLE_NAME
,
447 &gEfiGlobalVariableGuid
,
450 EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
456 if (EFI_ERROR (Status
)) {
460 DEBUG ((EFI_D_INFO
, "Variable %s is %x\n", EFI_VENDOR_KEYS_VARIABLE_NAME
, mVendorKeyState
));
466 Add public key in store and return its index.
468 @param[in] PubKey Input pointer to Public Key data
470 @return Index of new added item
481 VARIABLE_POINTER_TRACK Variable
;
486 if (PubKey
== NULL
) {
490 Status
= FindVariable (
492 &gEfiAuthenticatedVariableGuid
,
494 &mVariableModuleGlobal
->VariableGlobal
,
497 ASSERT_EFI_ERROR (Status
);
498 if (EFI_ERROR (Status
)) {
503 // Check whether the public key entry does exist.
506 for (Ptr
= mPubKeyStore
, Index
= 1; Index
<= mPubKeyNumber
; Index
++) {
507 if (CompareMem (Ptr
, PubKey
, EFI_CERT_TYPE_RSA2048_SIZE
) == 0) {
511 Ptr
+= EFI_CERT_TYPE_RSA2048_SIZE
;
516 // Add public key in database.
518 if (mPubKeyNumber
== MAX_KEY_NUM
) {
520 // Public key dadatase is full, try to reclaim invalid key.
524 // NV storage can't reclaim at runtime.
530 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
531 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
538 if (EFI_ERROR (Status
)) {
542 Status
= FindVariable (
544 &gEfiAuthenticatedVariableGuid
,
546 &mVariableModuleGlobal
->VariableGlobal
,
549 ASSERT_EFI_ERROR (Status
);
550 if (EFI_ERROR (Status
)) {
554 DataSize
= DataSizeOfVariable (Variable
.CurrPtr
);
555 Data
= GetVariableDataPtr (Variable
.CurrPtr
);
556 ASSERT ((DataSize
!= 0) && (Data
!= NULL
));
557 CopyMem (mPubKeyStore
, (UINT8
*) Data
, DataSize
);
558 mPubKeyNumber
= (UINT32
) (DataSize
/ EFI_CERT_TYPE_RSA2048_SIZE
);
560 if (mPubKeyNumber
== MAX_KEY_NUM
) {
565 CopyMem (mPubKeyStore
+ mPubKeyNumber
* EFI_CERT_TYPE_RSA2048_SIZE
, PubKey
, EFI_CERT_TYPE_RSA2048_SIZE
);
566 Index
= ++mPubKeyNumber
;
568 // Update public key database variable.
570 Status
= UpdateVariable (
572 &gEfiAuthenticatedVariableGuid
,
574 mPubKeyNumber
* EFI_CERT_TYPE_RSA2048_SIZE
,
575 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
,
581 ASSERT_EFI_ERROR (Status
);
588 Verify data payload with AuthInfo in EFI_CERT_TYPE_RSA2048_SHA256_GUID type.
589 Follow the steps in UEFI2.2.
591 Caution: This function may receive untrusted input.
592 This function may be invoked in SMM mode, and datasize and data are external input.
593 This function will do basic validation, before parse the data.
594 This function will parse the authentication carefully to avoid security issues, like
595 buffer overflow, integer overflow.
597 @param[in] Data Pointer to data with AuthInfo.
598 @param[in] DataSize Size of Data.
599 @param[in] PubKey Public key used for verification.
601 @retval EFI_INVALID_PARAMETER Invalid parameter.
602 @retval EFI_SECURITY_VIOLATION If authentication failed.
603 @retval EFI_SUCCESS Authentication successful.
607 VerifyCounterBasedPayload (
614 EFI_VARIABLE_AUTHENTICATION
*CertData
;
615 EFI_CERT_BLOCK_RSA_2048_SHA256
*CertBlock
;
616 UINT8 Digest
[SHA256_DIGEST_SIZE
];
620 PayloadSize
= DataSize
- AUTHINFO_SIZE
;
625 if (Data
== NULL
|| PubKey
== NULL
) {
626 return EFI_INVALID_PARAMETER
;
629 CertData
= (EFI_VARIABLE_AUTHENTICATION
*) Data
;
630 CertBlock
= (EFI_CERT_BLOCK_RSA_2048_SHA256
*) (CertData
->AuthInfo
.CertData
);
633 // wCertificateType should be WIN_CERT_TYPE_EFI_GUID.
634 // Cert type should be EFI_CERT_TYPE_RSA2048_SHA256_GUID.
636 if ((CertData
->AuthInfo
.Hdr
.wCertificateType
!= WIN_CERT_TYPE_EFI_GUID
) ||
637 !CompareGuid (&CertData
->AuthInfo
.CertType
, &gEfiCertTypeRsa2048Sha256Guid
)
640 // Invalid AuthInfo type, return EFI_SECURITY_VIOLATION.
642 return EFI_SECURITY_VIOLATION
;
645 // Hash data payload with SHA256.
647 ZeroMem (Digest
, SHA256_DIGEST_SIZE
);
648 Status
= Sha256Init (mHashCtx
);
652 Status
= Sha256Update (mHashCtx
, Data
+ AUTHINFO_SIZE
, PayloadSize
);
659 Status
= Sha256Update (mHashCtx
, &PayloadSize
, sizeof (UINTN
));
664 // Hash Monotonic Count.
666 Status
= Sha256Update (mHashCtx
, &CertData
->MonotonicCount
, sizeof (UINT64
));
670 Status
= Sha256Final (mHashCtx
, Digest
);
675 // Generate & Initialize RSA Context.
678 ASSERT (Rsa
!= NULL
);
680 // Set RSA Key Components.
681 // NOTE: Only N and E are needed to be set as RSA public key for signature verification.
683 Status
= RsaSetKey (Rsa
, RsaKeyN
, PubKey
, EFI_CERT_TYPE_RSA2048_SIZE
);
687 Status
= RsaSetKey (Rsa
, RsaKeyE
, mRsaE
, sizeof (mRsaE
));
692 // Verify the signature.
694 Status
= RsaPkcs1Verify (
698 CertBlock
->Signature
,
699 EFI_CERT_TYPE_RSA2048_SHA256_SIZE
709 return EFI_SECURITY_VIOLATION
;
714 Update platform mode.
716 @param[in] Mode SETUP_MODE or USER_MODE.
718 @return EFI_INVALID_PARAMETER Invalid parameter.
719 @return EFI_SUCCESS Update platform mode successfully.
728 VARIABLE_POINTER_TRACK Variable
;
729 UINT8 SecureBootMode
;
730 UINT8 SecureBootEnable
;
731 UINTN VariableDataSize
;
733 Status
= FindVariable (
735 &gEfiGlobalVariableGuid
,
737 &mVariableModuleGlobal
->VariableGlobal
,
740 if (EFI_ERROR (Status
)) {
745 // Update the value of SetupMode variable by a simple mem copy, this could avoid possible
746 // variable storage reclaim at runtime.
748 mPlatformMode
= (UINT8
) Mode
;
749 CopyMem (GetVariableDataPtr (Variable
.CurrPtr
), &mPlatformMode
, sizeof(UINT8
));
753 // SecureBoot Variable indicates whether the platform firmware is operating
754 // in Secure boot mode (1) or not (0), so we should not change SecureBoot
755 // Variable in runtime.
761 // Check "SecureBoot" variable's existence.
762 // If it doesn't exist, firmware has no capability to perform driver signing verification,
763 // then set "SecureBoot" to 0.
765 Status
= FindVariable (
766 EFI_SECURE_BOOT_MODE_NAME
,
767 &gEfiGlobalVariableGuid
,
769 &mVariableModuleGlobal
->VariableGlobal
,
773 // If "SecureBoot" variable exists, then check "SetupMode" variable update.
774 // If "SetupMode" variable is USER_MODE, "SecureBoot" variable is set to 1.
775 // If "SetupMode" variable is SETUP_MODE, "SecureBoot" variable is set to 0.
777 if (Variable
.CurrPtr
== NULL
) {
778 SecureBootMode
= SECURE_BOOT_MODE_DISABLE
;
780 if (mPlatformMode
== USER_MODE
) {
781 SecureBootMode
= SECURE_BOOT_MODE_ENABLE
;
782 } else if (mPlatformMode
== SETUP_MODE
) {
783 SecureBootMode
= SECURE_BOOT_MODE_DISABLE
;
785 return EFI_NOT_FOUND
;
789 Status
= UpdateVariable (
790 EFI_SECURE_BOOT_MODE_NAME
,
791 &gEfiGlobalVariableGuid
,
794 EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
800 if (EFI_ERROR (Status
)) {
805 // Check "SecureBootEnable" variable's existence. It can enable/disable secure boot feature.
807 Status
= FindVariable (
808 EFI_SECURE_BOOT_ENABLE_NAME
,
809 &gEfiSecureBootEnableDisableGuid
,
811 &mVariableModuleGlobal
->VariableGlobal
,
815 if (SecureBootMode
== SECURE_BOOT_MODE_ENABLE
) {
817 // Create the "SecureBootEnable" variable as secure boot is enabled.
819 SecureBootEnable
= SECURE_BOOT_ENABLE
;
820 VariableDataSize
= sizeof (SecureBootEnable
);
823 // Delete the "SecureBootEnable" variable if this variable exist as "SecureBoot"
824 // variable is not in secure boot state.
826 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
829 SecureBootEnable
= SECURE_BOOT_DISABLE
;
830 VariableDataSize
= 0;
833 Status
= UpdateVariable (
834 EFI_SECURE_BOOT_ENABLE_NAME
,
835 &gEfiSecureBootEnableDisableGuid
,
838 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
848 Check input data form to make sure it is a valid EFI_SIGNATURE_LIST for PK/KEK/db/dbx variable.
850 @param[in] VariableName Name of Variable to be check.
851 @param[in] VendorGuid Variable vendor GUID.
852 @param[in] Data Point to the variable data to be checked.
853 @param[in] DataSize Size of Data.
855 @return EFI_INVALID_PARAMETER Invalid signature list format.
856 @return EFI_SUCCESS Passed signature list format check successfully.
860 CheckSignatureListFormat(
861 IN CHAR16
*VariableName
,
862 IN EFI_GUID
*VendorGuid
,
867 EFI_SIGNATURE_LIST
*SigList
;
873 EFI_SIGNATURE_DATA
*CertData
;
880 ASSERT (VariableName
!= NULL
&& VendorGuid
!= NULL
&& Data
!= NULL
);
882 if (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, EFI_PLATFORM_KEY_NAME
) == 0)){
884 } else if ((CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && StrCmp (VariableName
, EFI_KEY_EXCHANGE_KEY_NAME
) == 0) ||
885 (CompareGuid (VendorGuid
, &gEfiImageSecurityDatabaseGuid
) &&
886 (StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE
) == 0 || StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE1
) == 0))){
893 SigList
= (EFI_SIGNATURE_LIST
*) Data
;
894 SigDataSize
= DataSize
;
898 // Walk throuth the input signature list and check the data format.
899 // If any signature is incorrectly formed, the whole check will fail.
901 while ((SigDataSize
> 0) && (SigDataSize
>= SigList
->SignatureListSize
)) {
902 for (Index
= 0; Index
< (sizeof (mSupportSigItem
) / sizeof (EFI_SIGNATURE_ITEM
)); Index
++ ) {
903 if (CompareGuid (&SigList
->SignatureType
, &mSupportSigItem
[Index
].SigType
)) {
905 // The value of SignatureSize should always be 16 (size of SignatureOwner
906 // component) add the data length according to signature type.
908 if (mSupportSigItem
[Index
].SigDataSize
!= ((UINT32
) ~0) &&
909 (SigList
->SignatureSize
- sizeof (EFI_GUID
)) != mSupportSigItem
[Index
].SigDataSize
) {
910 return EFI_INVALID_PARAMETER
;
912 if (mSupportSigItem
[Index
].SigHeaderSize
!= ((UINTN
) ~0) &&
913 SigList
->SignatureHeaderSize
!= mSupportSigItem
[Index
].SigHeaderSize
) {
914 return EFI_INVALID_PARAMETER
;
920 if (Index
== (sizeof (mSupportSigItem
) / sizeof (EFI_SIGNATURE_ITEM
))) {
922 // Undefined signature type.
924 return EFI_INVALID_PARAMETER
;
927 if (CompareGuid (&SigList
->SignatureType
, &gEfiCertX509Guid
)) {
929 // Try to retrieve the RSA public key from the X.509 certificate.
930 // If this operation fails, it's not a valid certificate.
932 RsaContext
= RsaNew ();
933 if (RsaContext
== NULL
) {
934 return EFI_INVALID_PARAMETER
;
936 CertData
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) SigList
+ sizeof (EFI_SIGNATURE_LIST
) + SigList
->SignatureHeaderSize
);
937 CertLen
= SigList
->SignatureSize
- sizeof (EFI_GUID
);
938 if (!RsaGetPublicKeyFromX509 (CertData
->SignatureData
, CertLen
, &RsaContext
)) {
939 RsaFree (RsaContext
);
940 return EFI_INVALID_PARAMETER
;
942 RsaFree (RsaContext
);
945 if ((SigList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - SigList
->SignatureHeaderSize
) % SigList
->SignatureSize
!= 0) {
946 return EFI_INVALID_PARAMETER
;
948 SigCount
+= (SigList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - SigList
->SignatureHeaderSize
) / SigList
->SignatureSize
;
950 SigDataSize
-= SigList
->SignatureListSize
;
951 SigList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) SigList
+ SigList
->SignatureListSize
);
954 if (((UINTN
) SigList
- (UINTN
) Data
) != DataSize
) {
955 return EFI_INVALID_PARAMETER
;
958 if (IsPk
&& SigCount
> 1) {
959 return EFI_INVALID_PARAMETER
;
966 Update "VendorKeys" variable to record the out of band secure boot key modification.
968 @return EFI_SUCCESS Variable is updated successfully.
969 @return Others Failed to update variable.
973 VendorKeyIsModified (
978 VARIABLE_POINTER_TRACK Variable
;
980 if (mVendorKeyState
== VENDOR_KEYS_MODIFIED
) {
983 mVendorKeyState
= VENDOR_KEYS_MODIFIED
;
985 FindVariable (EFI_VENDOR_KEYS_NV_VARIABLE_NAME
, &gEfiVendorKeysNvGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
986 Status
= UpdateVariable (
987 EFI_VENDOR_KEYS_NV_VARIABLE_NAME
,
988 &gEfiVendorKeysNvGuid
,
991 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
,
997 if (EFI_ERROR (Status
)) {
1001 FindVariable (EFI_VENDOR_KEYS_VARIABLE_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1002 return UpdateVariable (
1003 EFI_VENDOR_KEYS_VARIABLE_NAME
,
1004 &gEfiGlobalVariableGuid
,
1007 EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
1016 Process variable with platform key for verification.
1018 Caution: This function may receive untrusted input.
1019 This function may be invoked in SMM mode, and datasize and data are external input.
1020 This function will do basic validation, before parse the data.
1021 This function will parse the authentication carefully to avoid security issues, like
1022 buffer overflow, integer overflow.
1023 This function will check attribute carefully to avoid authentication bypass.
1025 @param[in] VariableName Name of Variable to be found.
1026 @param[in] VendorGuid Variable vendor GUID.
1027 @param[in] Data Data pointer.
1028 @param[in] DataSize Size of Data found. If size is less than the
1029 data, this value contains the required size.
1030 @param[in] Variable The variable information which is used to keep track of variable usage.
1031 @param[in] Attributes Attribute value of the variable
1032 @param[in] IsPk Indicate whether it is to process pk.
1034 @return EFI_INVALID_PARAMETER Invalid parameter.
1035 @return EFI_SECURITY_VIOLATION The variable does NOT pass the validation.
1036 check carried out by the firmware.
1037 @return EFI_SUCCESS Variable passed validation successfully.
1042 IN CHAR16
*VariableName
,
1043 IN EFI_GUID
*VendorGuid
,
1046 IN VARIABLE_POINTER_TRACK
*Variable
,
1047 IN UINT32 Attributes OPTIONAL
,
1056 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0 ||
1057 (Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) == 0) {
1059 // PK, KEK and db/dbx should set EFI_VARIABLE_NON_VOLATILE attribute and should be a time-based
1060 // authenticated variable.
1062 return EFI_INVALID_PARAMETER
;
1066 if ((InCustomMode() && UserPhysicalPresent()) || (mPlatformMode
== SETUP_MODE
&& !IsPk
)) {
1067 Payload
= (UINT8
*) Data
+ AUTHINFO2_SIZE (Data
);
1068 PayloadSize
= DataSize
- AUTHINFO2_SIZE (Data
);
1069 if (PayloadSize
== 0) {
1073 Status
= CheckSignatureListFormat(VariableName
, VendorGuid
, Payload
, PayloadSize
);
1074 if (EFI_ERROR (Status
)) {
1078 Status
= UpdateVariable (
1087 &((EFI_VARIABLE_AUTHENTICATION_2
*) Data
)->TimeStamp
1089 if (EFI_ERROR(Status
)) {
1093 if ((mPlatformMode
!= SETUP_MODE
) || IsPk
) {
1094 Status
= VendorKeyIsModified ();
1096 } else if (mPlatformMode
== USER_MODE
) {
1098 // Verify against X509 Cert in PK database.
1100 Status
= VerifyTimeBasedPayload (
1112 // Verify against the certificate in data payload.
1114 Status
= VerifyTimeBasedPayload (
1126 if (!EFI_ERROR(Status
) && IsPk
) {
1127 if (mPlatformMode
== SETUP_MODE
&& !Del
) {
1129 // If enroll PK in setup mode, need change to user mode.
1131 Status
= UpdatePlatformMode (USER_MODE
);
1132 } else if (mPlatformMode
== USER_MODE
&& Del
){
1134 // If delete PK in user mode, need change to setup mode.
1136 Status
= UpdatePlatformMode (SETUP_MODE
);
1144 Process variable with key exchange key for verification.
1146 Caution: This function may receive untrusted input.
1147 This function may be invoked in SMM mode, and datasize and data are external input.
1148 This function will do basic validation, before parse the data.
1149 This function will parse the authentication carefully to avoid security issues, like
1150 buffer overflow, integer overflow.
1151 This function will check attribute carefully to avoid authentication bypass.
1153 @param[in] VariableName Name of Variable to be found.
1154 @param[in] VendorGuid Variable vendor GUID.
1155 @param[in] Data Data pointer.
1156 @param[in] DataSize Size of Data found. If size is less than the
1157 data, this value contains the required size.
1158 @param[in] Variable The variable information which is used to keep track of variable usage.
1159 @param[in] Attributes Attribute value of the variable.
1161 @return EFI_INVALID_PARAMETER Invalid parameter.
1162 @return EFI_SECURITY_VIOLATION The variable does NOT pass the validation
1163 check carried out by the firmware.
1164 @return EFI_SUCCESS Variable pass validation successfully.
1169 IN CHAR16
*VariableName
,
1170 IN EFI_GUID
*VendorGuid
,
1173 IN VARIABLE_POINTER_TRACK
*Variable
,
1174 IN UINT32 Attributes OPTIONAL
1181 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0 ||
1182 (Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) == 0) {
1184 // DB and DBX should set EFI_VARIABLE_NON_VOLATILE attribute and should be a time-based
1185 // authenticated variable.
1187 return EFI_INVALID_PARAMETER
;
1190 Status
= EFI_SUCCESS
;
1191 if (mPlatformMode
== USER_MODE
&& !(InCustomMode() && UserPhysicalPresent())) {
1193 // Time-based, verify against X509 Cert KEK.
1195 return VerifyTimeBasedPayload (
1207 // If in setup mode or custom secure boot mode, no authentication needed.
1209 Payload
= (UINT8
*) Data
+ AUTHINFO2_SIZE (Data
);
1210 PayloadSize
= DataSize
- AUTHINFO2_SIZE (Data
);
1212 Status
= CheckSignatureListFormat(VariableName
, VendorGuid
, Payload
, PayloadSize
);
1213 if (EFI_ERROR (Status
)) {
1217 Status
= UpdateVariable (
1226 &((EFI_VARIABLE_AUTHENTICATION_2
*) Data
)->TimeStamp
1228 if (EFI_ERROR (Status
)) {
1232 if (mPlatformMode
!= SETUP_MODE
) {
1233 Status
= VendorKeyIsModified ();
1241 Process variable with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS/EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set
1243 Caution: This function may receive untrusted input.
1244 This function may be invoked in SMM mode, and datasize and data are external input.
1245 This function will do basic validation, before parse the data.
1246 This function will parse the authentication carefully to avoid security issues, like
1247 buffer overflow, integer overflow.
1248 This function will check attribute carefully to avoid authentication bypass.
1250 @param[in] VariableName Name of Variable to be found.
1251 @param[in] VendorGuid Variable vendor GUID.
1253 @param[in] Data Data pointer.
1254 @param[in] DataSize Size of Data found. If size is less than the
1255 data, this value contains the required size.
1256 @param[in] Variable The variable information which is used to keep track of variable usage.
1257 @param[in] Attributes Attribute value of the variable.
1259 @return EFI_INVALID_PARAMETER Invalid parameter.
1260 @return EFI_WRITE_PROTECTED Variable is write-protected and needs authentication with
1261 EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS set.
1262 @return EFI_OUT_OF_RESOURCES The Database to save the public key is full.
1263 @return EFI_SECURITY_VIOLATION The variable is with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
1264 set, but the AuthInfo does NOT pass the validation
1265 check carried out by the firmware.
1266 @return EFI_SUCCESS Variable is not write-protected or pass validation successfully.
1271 IN CHAR16
*VariableName
,
1272 IN EFI_GUID
*VendorGuid
,
1275 IN VARIABLE_POINTER_TRACK
*Variable
,
1276 IN UINT32 Attributes
1281 BOOLEAN IsFirstTime
;
1283 EFI_VARIABLE_AUTHENTICATION
*CertData
;
1284 EFI_CERT_BLOCK_RSA_2048_SHA256
*CertBlock
;
1286 UINT64 MonotonicCount
;
1294 if (NeedPhysicallyPresent(VariableName
, VendorGuid
) && !UserPhysicalPresent()) {
1296 // This variable is protected, only physical present user could modify its value.
1298 return EFI_SECURITY_VIOLATION
;
1302 // A time-based authenticated variable and a count-based authenticated variable
1303 // can't be updated by each other.
1305 if (Variable
->CurrPtr
!= NULL
) {
1306 if (((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) &&
1307 ((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0)) {
1308 return EFI_SECURITY_VIOLATION
;
1311 if (((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) &&
1312 ((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0)) {
1313 return EFI_SECURITY_VIOLATION
;
1318 // Process Time-based Authenticated variable.
1320 if ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) {
1321 return VerifyTimeBasedPayload (
1334 // Determine if first time SetVariable with the EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS.
1336 if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) {
1338 // Determine current operation type.
1340 if (DataSize
== AUTHINFO_SIZE
) {
1344 // Determine whether this is the first time with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS set.
1346 if (Variable
->CurrPtr
== NULL
) {
1348 } else if ((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) == 0) {
1351 KeyIndex
= Variable
->CurrPtr
->PubKeyIndex
;
1352 IsFirstTime
= FALSE
;
1354 } else if ((Variable
->CurrPtr
!= NULL
) &&
1355 ((Variable
->CurrPtr
->Attributes
& (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
)) != 0)
1358 // If the variable is already write-protected, it always needs authentication before update.
1360 return EFI_WRITE_PROTECTED
;
1363 // If without EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS, set and attributes collision.
1364 // That means it is not authenticated variable, just update variable as usual.
1366 Status
= UpdateVariable (VariableName
, VendorGuid
, Data
, DataSize
, Attributes
, 0, 0, Variable
, NULL
);
1371 // Get PubKey and check Monotonic Count value corresponding to the variable.
1373 CertData
= (EFI_VARIABLE_AUTHENTICATION
*) Data
;
1374 CertBlock
= (EFI_CERT_BLOCK_RSA_2048_SHA256
*) (CertData
->AuthInfo
.CertData
);
1375 PubKey
= CertBlock
->PublicKey
;
1378 // Update Monotonic Count value.
1380 MonotonicCount
= CertData
->MonotonicCount
;
1384 // Check input PubKey.
1386 if (CompareMem (PubKey
, mPubKeyStore
+ (KeyIndex
- 1) * EFI_CERT_TYPE_RSA2048_SIZE
, EFI_CERT_TYPE_RSA2048_SIZE
) != 0) {
1387 return EFI_SECURITY_VIOLATION
;
1390 // Compare the current monotonic count and ensure that it is greater than the last SetVariable
1391 // operation with the EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS attribute set.
1393 if (CertData
->MonotonicCount
<= Variable
->CurrPtr
->MonotonicCount
) {
1395 // Monotonic count check fail, suspicious replay attack, return EFI_SECURITY_VIOLATION.
1397 return EFI_SECURITY_VIOLATION
;
1401 // Verify the certificate in Data payload.
1403 Status
= VerifyCounterBasedPayload (Data
, DataSize
, PubKey
);
1404 if (EFI_ERROR (Status
)) {
1409 // Now, the signature has been verified!
1411 if (IsFirstTime
&& !IsDeletion
) {
1413 // Update public key database variable if need.
1415 KeyIndex
= AddPubKeyInStore (PubKey
);
1416 if (KeyIndex
== 0) {
1417 return EFI_OUT_OF_RESOURCES
;
1422 // Verification pass.
1424 return UpdateVariable (VariableName
, VendorGuid
, (UINT8
*)Data
+ AUTHINFO_SIZE
, DataSize
- AUTHINFO_SIZE
, Attributes
, KeyIndex
, MonotonicCount
, Variable
, NULL
);
1428 Merge two buffers which formatted as EFI_SIGNATURE_LIST. Only the new EFI_SIGNATURE_DATA
1429 will be appended to the original EFI_SIGNATURE_LIST, duplicate EFI_SIGNATURE_DATA
1432 @param[in, out] Data Pointer to original EFI_SIGNATURE_LIST.
1433 @param[in] DataSize Size of Data buffer.
1434 @param[in] FreeBufSize Size of free data buffer
1435 @param[in] NewData Pointer to new EFI_SIGNATURE_LIST to be appended.
1436 @param[in] NewDataSize Size of NewData buffer.
1437 @param[out] MergedBufSize Size of the merged buffer
1439 @return EFI_BUFFER_TOO_SMALL if input Data buffer overflowed
1443 AppendSignatureList (
1446 IN UINTN FreeBufSize
,
1448 IN UINTN NewDataSize
,
1449 OUT UINTN
*MergedBufSize
1452 EFI_SIGNATURE_LIST
*CertList
;
1453 EFI_SIGNATURE_DATA
*Cert
;
1455 EFI_SIGNATURE_LIST
*NewCertList
;
1456 EFI_SIGNATURE_DATA
*NewCert
;
1463 UINTN SignatureListSize
;
1466 Tail
= (UINT8
*) Data
+ DataSize
;
1468 NewCertList
= (EFI_SIGNATURE_LIST
*) NewData
;
1469 while ((NewDataSize
> 0) && (NewDataSize
>= NewCertList
->SignatureListSize
)) {
1470 NewCert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) NewCertList
+ sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
);
1471 NewCertCount
= (NewCertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - NewCertList
->SignatureHeaderSize
) / NewCertList
->SignatureSize
;
1474 for (Index
= 0; Index
< NewCertCount
; Index
++) {
1478 CertList
= (EFI_SIGNATURE_LIST
*) Data
;
1479 while ((Size
> 0) && (Size
>= CertList
->SignatureListSize
)) {
1480 if (CompareGuid (&CertList
->SignatureType
, &NewCertList
->SignatureType
) &&
1481 (CertList
->SignatureSize
== NewCertList
->SignatureSize
)) {
1482 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
1483 CertCount
= (CertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - CertList
->SignatureHeaderSize
) / CertList
->SignatureSize
;
1484 for (Index2
= 0; Index2
< CertCount
; Index2
++) {
1486 // Iterate each Signature Data in this Signature List.
1488 if (CompareMem (NewCert
, Cert
, CertList
->SignatureSize
) == 0) {
1492 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) Cert
+ CertList
->SignatureSize
);
1499 Size
-= CertList
->SignatureListSize
;
1500 CertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) CertList
+ CertList
->SignatureListSize
);
1505 // New EFI_SIGNATURE_DATA, append it.
1507 if (CopiedCount
== 0) {
1508 if (FreeBufSize
< sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
) {
1509 return EFI_BUFFER_TOO_SMALL
;
1513 // Copy EFI_SIGNATURE_LIST header for only once.
1516 CopyMem (Tail
, NewCertList
, sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
);
1517 Tail
= Tail
+ sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
;
1518 FreeBufSize
-= sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
;
1521 if (FreeBufSize
< NewCertList
->SignatureSize
) {
1522 return EFI_BUFFER_TOO_SMALL
;
1524 CopyMem (Tail
, NewCert
, NewCertList
->SignatureSize
);
1525 Tail
+= NewCertList
->SignatureSize
;
1526 FreeBufSize
-= NewCertList
->SignatureSize
;
1530 NewCert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) NewCert
+ NewCertList
->SignatureSize
);
1534 // Update SignatureListSize in newly appended EFI_SIGNATURE_LIST.
1536 if (CopiedCount
!= 0) {
1537 SignatureListSize
= sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
+ (CopiedCount
* NewCertList
->SignatureSize
);
1538 CertList
= (EFI_SIGNATURE_LIST
*) (Tail
- SignatureListSize
);
1539 CertList
->SignatureListSize
= (UINT32
) SignatureListSize
;
1542 NewDataSize
-= NewCertList
->SignatureListSize
;
1543 NewCertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) NewCertList
+ NewCertList
->SignatureListSize
);
1546 *MergedBufSize
= (Tail
- (UINT8
*) Data
);
1551 Compare two EFI_TIME data.
1554 @param FirstTime A pointer to the first EFI_TIME data.
1555 @param SecondTime A pointer to the second EFI_TIME data.
1557 @retval TRUE The FirstTime is not later than the SecondTime.
1558 @retval FALSE The FirstTime is later than the SecondTime.
1563 IN EFI_TIME
*FirstTime
,
1564 IN EFI_TIME
*SecondTime
1567 if (FirstTime
->Year
!= SecondTime
->Year
) {
1568 return (BOOLEAN
) (FirstTime
->Year
< SecondTime
->Year
);
1569 } else if (FirstTime
->Month
!= SecondTime
->Month
) {
1570 return (BOOLEAN
) (FirstTime
->Month
< SecondTime
->Month
);
1571 } else if (FirstTime
->Day
!= SecondTime
->Day
) {
1572 return (BOOLEAN
) (FirstTime
->Day
< SecondTime
->Day
);
1573 } else if (FirstTime
->Hour
!= SecondTime
->Hour
) {
1574 return (BOOLEAN
) (FirstTime
->Hour
< SecondTime
->Hour
);
1575 } else if (FirstTime
->Minute
!= SecondTime
->Minute
) {
1576 return (BOOLEAN
) (FirstTime
->Minute
< SecondTime
->Minute
);
1579 return (BOOLEAN
) (FirstTime
->Second
<= SecondTime
->Second
);
1583 Find matching signer's certificates for common authenticated variable
1584 by corresponding VariableName and VendorGuid from "certdb".
1586 The data format of "certdb":
1588 // UINT32 CertDbListSize;
1589 // /// AUTH_CERT_DB_DATA Certs1[];
1590 // /// AUTH_CERT_DB_DATA Certs2[];
1592 // /// AUTH_CERT_DB_DATA Certsn[];
1595 @param[in] VariableName Name of authenticated Variable.
1596 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1597 @param[in] Data Pointer to variable "certdb".
1598 @param[in] DataSize Size of variable "certdb".
1599 @param[out] CertOffset Offset of matching CertData, from starting of Data.
1600 @param[out] CertDataSize Length of CertData in bytes.
1601 @param[out] CertNodeOffset Offset of matching AUTH_CERT_DB_DATA , from
1603 @param[out] CertNodeSize Length of AUTH_CERT_DB_DATA in bytes.
1605 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1606 @retval EFI_NOT_FOUND Fail to find matching certs.
1607 @retval EFI_SUCCESS Find matching certs and output parameters.
1612 IN CHAR16
*VariableName
,
1613 IN EFI_GUID
*VendorGuid
,
1616 OUT UINT32
*CertOffset
, OPTIONAL
1617 OUT UINT32
*CertDataSize
, OPTIONAL
1618 OUT UINT32
*CertNodeOffset
,OPTIONAL
1619 OUT UINT32
*CertNodeSize OPTIONAL
1623 AUTH_CERT_DB_DATA
*Ptr
;
1627 UINT32 CertDbListSize
;
1629 if ((VariableName
== NULL
) || (VendorGuid
== NULL
) || (Data
== NULL
)) {
1630 return EFI_INVALID_PARAMETER
;
1634 // Check whether DataSize matches recorded CertDbListSize.
1636 if (DataSize
< sizeof (UINT32
)) {
1637 return EFI_INVALID_PARAMETER
;
1640 CertDbListSize
= ReadUnaligned32 ((UINT32
*) Data
);
1642 if (CertDbListSize
!= (UINT32
) DataSize
) {
1643 return EFI_INVALID_PARAMETER
;
1646 Offset
= sizeof (UINT32
);
1649 // Get corresponding certificates by VendorGuid and VariableName.
1651 while (Offset
< (UINT32
) DataSize
) {
1652 Ptr
= (AUTH_CERT_DB_DATA
*) (Data
+ Offset
);
1654 // Check whether VendorGuid matches.
1656 if (CompareGuid (&Ptr
->VendorGuid
, VendorGuid
)) {
1657 NodeSize
= ReadUnaligned32 (&Ptr
->CertNodeSize
);
1658 NameSize
= ReadUnaligned32 (&Ptr
->NameSize
);
1659 CertSize
= ReadUnaligned32 (&Ptr
->CertDataSize
);
1661 if (NodeSize
!= sizeof (EFI_GUID
) + sizeof (UINT32
) * 3 + CertSize
+
1662 sizeof (CHAR16
) * NameSize
) {
1663 return EFI_INVALID_PARAMETER
;
1666 Offset
= Offset
+ sizeof (EFI_GUID
) + sizeof (UINT32
) * 3;
1668 // Check whether VariableName matches.
1670 if ((NameSize
== StrLen (VariableName
)) &&
1671 (CompareMem (Data
+ Offset
, VariableName
, NameSize
* sizeof (CHAR16
)) == 0)) {
1672 Offset
= Offset
+ NameSize
* sizeof (CHAR16
);
1674 if (CertOffset
!= NULL
) {
1675 *CertOffset
= Offset
;
1678 if (CertDataSize
!= NULL
) {
1679 *CertDataSize
= CertSize
;
1682 if (CertNodeOffset
!= NULL
) {
1683 *CertNodeOffset
= (UINT32
) ((UINT8
*) Ptr
- Data
);
1686 if (CertNodeSize
!= NULL
) {
1687 *CertNodeSize
= NodeSize
;
1692 Offset
= Offset
+ NameSize
* sizeof (CHAR16
) + CertSize
;
1695 NodeSize
= ReadUnaligned32 (&Ptr
->CertNodeSize
);
1696 Offset
= Offset
+ NodeSize
;
1700 return EFI_NOT_FOUND
;
1704 Retrieve signer's certificates for common authenticated variable
1705 by corresponding VariableName and VendorGuid from "certdb".
1707 @param[in] VariableName Name of authenticated Variable.
1708 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1709 @param[out] CertData Pointer to signer's certificates.
1710 @param[out] CertDataSize Length of CertData in bytes.
1712 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1713 @retval EFI_NOT_FOUND Fail to find "certdb" or matching certs.
1714 @retval EFI_SUCCESS Get signer's certificates successfully.
1719 IN CHAR16
*VariableName
,
1720 IN EFI_GUID
*VendorGuid
,
1721 OUT UINT8
**CertData
,
1722 OUT UINT32
*CertDataSize
1725 VARIABLE_POINTER_TRACK CertDbVariable
;
1731 if ((VariableName
== NULL
) || (VendorGuid
== NULL
) || (CertData
== NULL
) || (CertDataSize
== NULL
)) {
1732 return EFI_INVALID_PARAMETER
;
1736 // Get variable "certdb".
1738 Status
= FindVariable (
1742 &mVariableModuleGlobal
->VariableGlobal
,
1745 if (EFI_ERROR (Status
)) {
1749 DataSize
= DataSizeOfVariable (CertDbVariable
.CurrPtr
);
1750 Data
= GetVariableDataPtr (CertDbVariable
.CurrPtr
);
1751 if ((DataSize
== 0) || (Data
== NULL
)) {
1753 return EFI_NOT_FOUND
;
1756 Status
= FindCertsFromDb (
1767 if (EFI_ERROR (Status
)) {
1771 *CertData
= Data
+ CertOffset
;
1776 Delete matching signer's certificates when deleting common authenticated
1777 variable by corresponding VariableName and VendorGuid from "certdb".
1779 @param[in] VariableName Name of authenticated Variable.
1780 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1782 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1783 @retval EFI_NOT_FOUND Fail to find "certdb" or matching certs.
1784 @retval EFI_OUT_OF_RESOURCES The operation is failed due to lack of resources.
1785 @retval EFI_SUCCESS The operation is completed successfully.
1790 IN CHAR16
*VariableName
,
1791 IN EFI_GUID
*VendorGuid
1794 VARIABLE_POINTER_TRACK CertDbVariable
;
1799 UINT32 CertNodeOffset
;
1800 UINT32 CertNodeSize
;
1802 UINT32 NewCertDbSize
;
1804 if ((VariableName
== NULL
) || (VendorGuid
== NULL
)) {
1805 return EFI_INVALID_PARAMETER
;
1809 // Get variable "certdb".
1811 Status
= FindVariable (
1815 &mVariableModuleGlobal
->VariableGlobal
,
1818 if (EFI_ERROR (Status
)) {
1822 DataSize
= DataSizeOfVariable (CertDbVariable
.CurrPtr
);
1823 Data
= GetVariableDataPtr (CertDbVariable
.CurrPtr
);
1824 if ((DataSize
== 0) || (Data
== NULL
)) {
1826 return EFI_NOT_FOUND
;
1829 if (DataSize
== sizeof (UINT32
)) {
1831 // There is no certs in certdb.
1837 // Get corresponding cert node from certdb.
1839 Status
= FindCertsFromDb (
1850 if (EFI_ERROR (Status
)) {
1854 if (DataSize
< (CertNodeOffset
+ CertNodeSize
)) {
1855 return EFI_NOT_FOUND
;
1859 // Construct new data content of variable "certdb".
1861 NewCertDbSize
= (UINT32
) DataSize
- CertNodeSize
;
1862 NewCertDb
= (UINT8
*) mCertDbStore
;
1865 // Copy the DB entries before deleting node.
1867 CopyMem (NewCertDb
, Data
, CertNodeOffset
);
1869 // Update CertDbListSize.
1871 CopyMem (NewCertDb
, &NewCertDbSize
, sizeof (UINT32
));
1873 // Copy the DB entries after deleting node.
1875 if (DataSize
> (CertNodeOffset
+ CertNodeSize
)) {
1877 NewCertDb
+ CertNodeOffset
,
1878 Data
+ CertNodeOffset
+ CertNodeSize
,
1879 DataSize
- CertNodeOffset
- CertNodeSize
1886 VarAttr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
1887 Status
= UpdateVariable (
1903 Insert signer's certificates for common authenticated variable with VariableName
1904 and VendorGuid in AUTH_CERT_DB_DATA to "certdb".
1906 @param[in] VariableName Name of authenticated Variable.
1907 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1908 @param[in] CertData Pointer to signer's certificates.
1909 @param[in] CertDataSize Length of CertData in bytes.
1911 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1912 @retval EFI_ACCESS_DENIED An AUTH_CERT_DB_DATA entry with same VariableName
1913 and VendorGuid already exists.
1914 @retval EFI_OUT_OF_RESOURCES The operation is failed due to lack of resources.
1915 @retval EFI_SUCCESS Insert an AUTH_CERT_DB_DATA entry to "certdb"
1920 IN CHAR16
*VariableName
,
1921 IN EFI_GUID
*VendorGuid
,
1923 IN UINTN CertDataSize
1926 VARIABLE_POINTER_TRACK CertDbVariable
;
1932 UINT32 NewCertDbSize
;
1933 UINT32 CertNodeSize
;
1935 AUTH_CERT_DB_DATA
*Ptr
;
1937 if ((VariableName
== NULL
) || (VendorGuid
== NULL
) || (CertData
== NULL
)) {
1938 return EFI_INVALID_PARAMETER
;
1942 // Get variable "certdb".
1944 Status
= FindVariable (
1948 &mVariableModuleGlobal
->VariableGlobal
,
1951 if (EFI_ERROR (Status
)) {
1955 DataSize
= DataSizeOfVariable (CertDbVariable
.CurrPtr
);
1956 Data
= GetVariableDataPtr (CertDbVariable
.CurrPtr
);
1957 if ((DataSize
== 0) || (Data
== NULL
)) {
1959 return EFI_NOT_FOUND
;
1963 // Find whether matching cert node already exists in "certdb".
1964 // If yes return error.
1966 Status
= FindCertsFromDb (
1977 if (!EFI_ERROR (Status
)) {
1979 return EFI_ACCESS_DENIED
;
1983 // Construct new data content of variable "certdb".
1985 NameSize
= (UINT32
) StrLen (VariableName
);
1986 CertNodeSize
= sizeof (AUTH_CERT_DB_DATA
) + (UINT32
) CertDataSize
+ NameSize
* sizeof (CHAR16
);
1987 NewCertDbSize
= (UINT32
) DataSize
+ CertNodeSize
;
1988 if (NewCertDbSize
> MAX_CERTDB_SIZE
) {
1989 return EFI_OUT_OF_RESOURCES
;
1991 NewCertDb
= (UINT8
*) mCertDbStore
;
1994 // Copy the DB entries before deleting node.
1996 CopyMem (NewCertDb
, Data
, DataSize
);
1998 // Update CertDbListSize.
2000 CopyMem (NewCertDb
, &NewCertDbSize
, sizeof (UINT32
));
2002 // Construct new cert node.
2004 Ptr
= (AUTH_CERT_DB_DATA
*) (NewCertDb
+ DataSize
);
2005 CopyGuid (&Ptr
->VendorGuid
, VendorGuid
);
2006 CopyMem (&Ptr
->CertNodeSize
, &CertNodeSize
, sizeof (UINT32
));
2007 CopyMem (&Ptr
->NameSize
, &NameSize
, sizeof (UINT32
));
2008 CopyMem (&Ptr
->CertDataSize
, &CertDataSize
, sizeof (UINT32
));
2011 (UINT8
*) Ptr
+ sizeof (AUTH_CERT_DB_DATA
),
2013 NameSize
* sizeof (CHAR16
)
2017 (UINT8
*) Ptr
+ sizeof (AUTH_CERT_DB_DATA
) + NameSize
* sizeof (CHAR16
),
2025 VarAttr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
2026 Status
= UpdateVariable (
2042 Process variable with EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set
2044 Caution: This function may receive untrusted input.
2045 This function may be invoked in SMM mode, and datasize and data are external input.
2046 This function will do basic validation, before parse the data.
2047 This function will parse the authentication carefully to avoid security issues, like
2048 buffer overflow, integer overflow.
2050 @param[in] VariableName Name of Variable to be found.
2051 @param[in] VendorGuid Variable vendor GUID.
2052 @param[in] Data Data pointer.
2053 @param[in] DataSize Size of Data found. If size is less than the
2054 data, this value contains the required size.
2055 @param[in] Variable The variable information which is used to keep track of variable usage.
2056 @param[in] Attributes Attribute value of the variable.
2057 @param[in] AuthVarType Verify against PK, KEK database, private database or certificate in data payload.
2058 @param[out] VarDel Delete the variable or not.
2060 @retval EFI_INVALID_PARAMETER Invalid parameter.
2061 @retval EFI_SECURITY_VIOLATION The variable does NOT pass the validation
2062 check carried out by the firmware.
2063 @retval EFI_OUT_OF_RESOURCES Failed to process variable due to lack
2065 @retval EFI_SUCCESS Variable pass validation successfully.
2069 VerifyTimeBasedPayload (
2070 IN CHAR16
*VariableName
,
2071 IN EFI_GUID
*VendorGuid
,
2074 IN VARIABLE_POINTER_TRACK
*Variable
,
2075 IN UINT32 Attributes
,
2076 IN AUTHVAR_TYPE AuthVarType
,
2090 BOOLEAN VerifyStatus
;
2092 EFI_SIGNATURE_LIST
*CertList
;
2093 EFI_SIGNATURE_DATA
*Cert
;
2094 VARIABLE_POINTER_TRACK KekVariable
;
2095 EFI_VARIABLE_AUTHENTICATION_2
*CertData
;
2098 VARIABLE_POINTER_TRACK PkVariable
;
2103 UINTN CertStackSize
;
2104 UINT8
*CertsInCertDb
;
2105 UINT32 CertsSizeinDb
;
2107 VerifyStatus
= FALSE
;
2116 // When the attribute EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS is
2117 // set, then the Data buffer shall begin with an instance of a complete (and serialized)
2118 // EFI_VARIABLE_AUTHENTICATION_2 descriptor. The descriptor shall be followed by the new
2119 // variable value and DataSize shall reflect the combined size of the descriptor and the new
2120 // variable value. The authentication descriptor is not part of the variable data and is not
2121 // returned by subsequent calls to GetVariable().
2123 CertData
= (EFI_VARIABLE_AUTHENTICATION_2
*) Data
;
2126 // Verify that Pad1, Nanosecond, TimeZone, Daylight and Pad2 components of the
2127 // TimeStamp value are set to zero.
2129 if ((CertData
->TimeStamp
.Pad1
!= 0) ||
2130 (CertData
->TimeStamp
.Nanosecond
!= 0) ||
2131 (CertData
->TimeStamp
.TimeZone
!= 0) ||
2132 (CertData
->TimeStamp
.Daylight
!= 0) ||
2133 (CertData
->TimeStamp
.Pad2
!= 0)) {
2134 return EFI_SECURITY_VIOLATION
;
2137 if ((Variable
->CurrPtr
!= NULL
) && ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0)) {
2138 if (CompareTimeStamp (&CertData
->TimeStamp
, &Variable
->CurrPtr
->TimeStamp
)) {
2140 // TimeStamp check fail, suspicious replay attack, return EFI_SECURITY_VIOLATION.
2142 return EFI_SECURITY_VIOLATION
;
2147 // wCertificateType should be WIN_CERT_TYPE_EFI_GUID.
2148 // Cert type should be EFI_CERT_TYPE_PKCS7_GUID.
2150 if ((CertData
->AuthInfo
.Hdr
.wCertificateType
!= WIN_CERT_TYPE_EFI_GUID
) ||
2151 !CompareGuid (&CertData
->AuthInfo
.CertType
, &gEfiCertPkcs7Guid
)) {
2153 // Invalid AuthInfo type, return EFI_SECURITY_VIOLATION.
2155 return EFI_SECURITY_VIOLATION
;
2159 // Find out Pkcs7 SignedData which follows the EFI_VARIABLE_AUTHENTICATION_2 descriptor.
2160 // AuthInfo.Hdr.dwLength is the length of the entire certificate, including the length of the header.
2162 SigData
= CertData
->AuthInfo
.CertData
;
2163 SigDataSize
= CertData
->AuthInfo
.Hdr
.dwLength
- (UINT32
) (OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID
, CertData
));
2166 // Find out the new data payload which follows Pkcs7 SignedData directly.
2168 PayloadPtr
= SigData
+ SigDataSize
;
2169 PayloadSize
= DataSize
- OFFSET_OF_AUTHINFO2_CERT_DATA
- (UINTN
) SigDataSize
;
2172 // Construct a buffer to fill with (VariableName, VendorGuid, Attributes, TimeStamp, Data).
2174 NewDataSize
= PayloadSize
+ sizeof (EFI_TIME
) + sizeof (UINT32
) +
2175 sizeof (EFI_GUID
) + StrSize (VariableName
) - sizeof (CHAR16
);
2176 NewData
= mSerializationRuntimeBuffer
;
2179 Length
= StrLen (VariableName
) * sizeof (CHAR16
);
2180 CopyMem (Buffer
, VariableName
, Length
);
2183 Length
= sizeof (EFI_GUID
);
2184 CopyMem (Buffer
, VendorGuid
, Length
);
2187 Length
= sizeof (UINT32
);
2188 CopyMem (Buffer
, &Attr
, Length
);
2191 Length
= sizeof (EFI_TIME
);
2192 CopyMem (Buffer
, &CertData
->TimeStamp
, Length
);
2195 CopyMem (Buffer
, PayloadPtr
, PayloadSize
);
2197 if (AuthVarType
== AuthVarTypePk
) {
2199 // Verify that the signature has been made with the current Platform Key (no chaining for PK).
2200 // First, get signer's certificates from SignedData.
2202 VerifyStatus
= Pkcs7GetSigners (
2210 if (!VerifyStatus
) {
2215 // Second, get the current platform key from variable. Check whether it's identical with signer's certificates
2216 // in SignedData. If not, return error immediately.
2218 Status
= FindVariable (
2219 EFI_PLATFORM_KEY_NAME
,
2220 &gEfiGlobalVariableGuid
,
2222 &mVariableModuleGlobal
->VariableGlobal
,
2225 if (EFI_ERROR (Status
)) {
2226 VerifyStatus
= FALSE
;
2229 CertList
= (EFI_SIGNATURE_LIST
*) GetVariableDataPtr (PkVariable
.CurrPtr
);
2230 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
2231 if ((RootCertSize
!= (CertList
->SignatureSize
- (sizeof (EFI_SIGNATURE_DATA
) - 1))) ||
2232 (CompareMem (Cert
->SignatureData
, RootCert
, RootCertSize
) != 0)) {
2233 VerifyStatus
= FALSE
;
2238 // Verify Pkcs7 SignedData via Pkcs7Verify library.
2240 VerifyStatus
= Pkcs7Verify (
2249 } else if (AuthVarType
== AuthVarTypeKek
) {
2252 // Get KEK database from variable.
2254 Status
= FindVariable (
2255 EFI_KEY_EXCHANGE_KEY_NAME
,
2256 &gEfiGlobalVariableGuid
,
2258 &mVariableModuleGlobal
->VariableGlobal
,
2261 if (EFI_ERROR (Status
)) {
2266 // Ready to verify Pkcs7 SignedData. Go through KEK Signature Database to find out X.509 CertList.
2268 KekDataSize
= KekVariable
.CurrPtr
->DataSize
;
2269 CertList
= (EFI_SIGNATURE_LIST
*) GetVariableDataPtr (KekVariable
.CurrPtr
);
2270 while ((KekDataSize
> 0) && (KekDataSize
>= CertList
->SignatureListSize
)) {
2271 if (CompareGuid (&CertList
->SignatureType
, &gEfiCertX509Guid
)) {
2272 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
2273 CertCount
= (CertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - CertList
->SignatureHeaderSize
) / CertList
->SignatureSize
;
2274 for (Index
= 0; Index
< CertCount
; Index
++) {
2276 // Iterate each Signature Data Node within this CertList for a verify
2278 RootCert
= Cert
->SignatureData
;
2279 RootCertSize
= CertList
->SignatureSize
- (sizeof (EFI_SIGNATURE_DATA
) - 1);
2282 // Verify Pkcs7 SignedData via Pkcs7Verify library.
2284 VerifyStatus
= Pkcs7Verify (
2295 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) Cert
+ CertList
->SignatureSize
);
2298 KekDataSize
-= CertList
->SignatureListSize
;
2299 CertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) CertList
+ CertList
->SignatureListSize
);
2301 } else if (AuthVarType
== AuthVarTypePriv
) {
2304 // Process common authenticated variable except PK/KEK/DB/DBX.
2305 // Get signer's certificates from SignedData.
2307 VerifyStatus
= Pkcs7GetSigners (
2315 if (!VerifyStatus
) {
2320 // Get previously stored signer's certificates from certdb for existing
2321 // variable. Check whether they are identical with signer's certificates
2322 // in SignedData. If not, return error immediately.
2324 if ((Variable
->CurrPtr
!= NULL
)) {
2325 VerifyStatus
= FALSE
;
2327 Status
= GetCertsFromDb (VariableName
, VendorGuid
, &CertsInCertDb
, &CertsSizeinDb
);
2328 if (EFI_ERROR (Status
)) {
2332 if ((CertStackSize
!= CertsSizeinDb
) ||
2333 (CompareMem (SignerCerts
, CertsInCertDb
, CertsSizeinDb
) != 0)) {
2338 VerifyStatus
= Pkcs7Verify (
2346 if (!VerifyStatus
) {
2351 // Delete signer's certificates when delete the common authenticated variable.
2353 if ((PayloadSize
== 0) && (Variable
->CurrPtr
!= NULL
) && ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0)) {
2354 Status
= DeleteCertsFromDb (VariableName
, VendorGuid
);
2355 if (EFI_ERROR (Status
)) {
2356 VerifyStatus
= FALSE
;
2359 } else if (Variable
->CurrPtr
== NULL
&& PayloadSize
!= 0) {
2361 // Insert signer's certificates when adding a new common authenticated variable.
2363 Status
= InsertCertsToDb (VariableName
, VendorGuid
, SignerCerts
, CertStackSize
);
2364 if (EFI_ERROR (Status
)) {
2365 VerifyStatus
= FALSE
;
2369 } else if (AuthVarType
== AuthVarTypePayload
) {
2370 CertList
= (EFI_SIGNATURE_LIST
*) PayloadPtr
;
2371 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
2372 RootCert
= Cert
->SignatureData
;
2373 RootCertSize
= CertList
->SignatureSize
- (sizeof (EFI_SIGNATURE_DATA
) - 1);
2375 // Verify Pkcs7 SignedData via Pkcs7Verify library.
2377 VerifyStatus
= Pkcs7Verify (
2386 return EFI_SECURITY_VIOLATION
;
2391 if (AuthVarType
== AuthVarTypePk
|| AuthVarType
== AuthVarTypePriv
) {
2392 Pkcs7FreeSigners (RootCert
);
2393 Pkcs7FreeSigners (SignerCerts
);
2396 if (!VerifyStatus
) {
2397 return EFI_SECURITY_VIOLATION
;
2400 Status
= CheckSignatureListFormat(VariableName
, VendorGuid
, PayloadPtr
, PayloadSize
);
2401 if (EFI_ERROR (Status
)) {
2405 if ((PayloadSize
== 0) && (VarDel
!= NULL
)) {
2410 // Final step: Update/Append Variable if it pass Pkcs7Verify
2412 return UpdateVariable (
2421 &CertData
->TimeStamp