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 // The serialization of the values of the VariableName, VendorGuid and Attributes
53 // parameters of the SetVariable() call and the TimeStamp component of the
54 // EFI_VARIABLE_AUTHENTICATION_2 descriptor followed by the variable's new value
55 // i.e. (VariableName, VendorGuid, Attributes, TimeStamp, Data)
57 UINT8
*mSerializationRuntimeBuffer
= NULL
;
60 // Requirement for different signature type which have been defined in UEFI spec.
61 // These data are used to peform SignatureList format check while setting PK/KEK variable.
63 EFI_SIGNATURE_ITEM mSupportSigItem
[] = {
64 //{SigType, SigHeaderSize, SigDataSize }
65 {EFI_CERT_SHA256_GUID
, 0, 32 },
66 {EFI_CERT_RSA2048_GUID
, 0, 256 },
67 {EFI_CERT_RSA2048_SHA256_GUID
, 0, 256 },
68 {EFI_CERT_SHA1_GUID
, 0, 20 },
69 {EFI_CERT_RSA2048_SHA1_GUID
, 0, 256 },
70 {EFI_CERT_X509_GUID
, 0, ((UINT32
) ~0)},
71 {EFI_CERT_SHA224_GUID
, 0, 28 },
72 {EFI_CERT_SHA384_GUID
, 0, 48 },
73 {EFI_CERT_SHA512_GUID
, 0, 64 }
77 Determine whether this operation needs a physical present user.
79 @param[in] VariableName Name of the Variable.
80 @param[in] VendorGuid GUID of the Variable.
82 @retval TRUE This variable is protected, only a physical present user could set this variable.
83 @retval FALSE This variable is not protected.
87 NeedPhysicallyPresent(
88 IN CHAR16
*VariableName
,
89 IN EFI_GUID
*VendorGuid
92 if ((CompareGuid (VendorGuid
, &gEfiSecureBootEnableDisableGuid
) && (StrCmp (VariableName
, EFI_SECURE_BOOT_ENABLE_NAME
) == 0))
93 || (CompareGuid (VendorGuid
, &gEfiCustomModeEnableGuid
) && (StrCmp (VariableName
, EFI_CUSTOM_MODE_NAME
) == 0))) {
101 Determine whether the platform is operating in Custom Secure Boot mode.
103 @retval TRUE The platform is operating in Custom mode.
104 @retval FALSE The platform is operating in Standard mode.
112 VARIABLE_POINTER_TRACK Variable
;
114 FindVariable (EFI_CUSTOM_MODE_NAME
, &gEfiCustomModeEnableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
115 if (Variable
.CurrPtr
!= NULL
&& *(GetVariableDataPtr (Variable
.CurrPtr
)) == CUSTOM_SECURE_BOOT_MODE
) {
124 Internal function to delete a Variable given its name and GUID, no authentication
127 @param[in] VariableName Name of the Variable.
128 @param[in] VendorGuid GUID of the Variable.
130 @retval EFI_SUCCESS Variable deleted successfully.
131 @retval Others The driver failded to start the device.
136 IN CHAR16
*VariableName
,
137 IN EFI_GUID
*VendorGuid
141 VARIABLE_POINTER_TRACK Variable
;
143 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
144 if (EFI_ERROR (Status
)) {
148 ASSERT (Variable
.CurrPtr
!= NULL
);
149 return UpdateVariable (VariableName
, VendorGuid
, NULL
, 0, 0, 0, 0, &Variable
, NULL
);
153 Initializes for authenticated varibale service.
155 @retval EFI_SUCCESS Function successfully executed.
156 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resources.
160 AutenticatedVariableServiceInitialize (
165 VARIABLE_POINTER_TRACK Variable
;
166 VARIABLE_POINTER_TRACK PkVariable
;
172 UINT8 SecureBootMode
;
173 UINT8 SecureBootEnable
;
178 // Initialize hash context.
180 CtxSize
= Sha256GetContextSize ();
181 mHashCtx
= AllocateRuntimePool (CtxSize
);
182 if (mHashCtx
== NULL
) {
183 return EFI_OUT_OF_RESOURCES
;
187 // Prepare runtime buffer for serialized data of time-based authenticated
188 // Variable, i.e. (VariableName, VendorGuid, Attributes, TimeStamp, Data).
190 mSerializationRuntimeBuffer
= AllocateRuntimePool (PcdGet32 (PcdMaxVariableSize
) + sizeof (EFI_GUID
) + sizeof (UINT32
) + sizeof (EFI_TIME
));
191 if (mSerializationRuntimeBuffer
== NULL
) {
192 return EFI_OUT_OF_RESOURCES
;
196 // Check "AuthVarKeyDatabase" variable's existence.
197 // If it doesn't exist, create a new one with initial value of 0 and EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS set.
199 Status
= FindVariable (
201 &gEfiAuthenticatedVariableGuid
,
203 &mVariableModuleGlobal
->VariableGlobal
,
207 if (Variable
.CurrPtr
== NULL
) {
208 VarAttr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
;
211 Status
= UpdateVariable (
213 &gEfiAuthenticatedVariableGuid
,
222 if (EFI_ERROR (Status
)) {
227 // Load database in global variable for cache.
229 DataSize
= DataSizeOfVariable (Variable
.CurrPtr
);
230 Data
= GetVariableDataPtr (Variable
.CurrPtr
);
231 ASSERT ((DataSize
!= 0) && (Data
!= NULL
));
232 CopyMem (mPubKeyStore
, (UINT8
*) Data
, DataSize
);
233 mPubKeyNumber
= (UINT32
) (DataSize
/ EFI_CERT_TYPE_RSA2048_SIZE
);
236 FindVariable (EFI_PLATFORM_KEY_NAME
, &gEfiGlobalVariableGuid
, &PkVariable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
237 if (PkVariable
.CurrPtr
== NULL
) {
238 DEBUG ((EFI_D_INFO
, "Variable %s does not exist.\n", EFI_PLATFORM_KEY_NAME
));
240 DEBUG ((EFI_D_INFO
, "Variable %s exists.\n", EFI_PLATFORM_KEY_NAME
));
244 // Create "SetupMode" variable with BS+RT attribute set.
246 FindVariable (EFI_SETUP_MODE_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
247 if (PkVariable
.CurrPtr
== NULL
) {
248 mPlatformMode
= SETUP_MODE
;
250 mPlatformMode
= USER_MODE
;
252 Status
= UpdateVariable (
254 &gEfiGlobalVariableGuid
,
257 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
263 if (EFI_ERROR (Status
)) {
268 // Create "SignatureSupport" variable with BS+RT attribute set.
270 FindVariable (EFI_SIGNATURE_SUPPORT_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
271 Status
= UpdateVariable (
272 EFI_SIGNATURE_SUPPORT_NAME
,
273 &gEfiGlobalVariableGuid
,
275 sizeof(mSignatureSupport
),
276 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
282 if (EFI_ERROR (Status
)) {
287 // If "SecureBootEnable" variable exists, then update "SecureBoot" variable.
288 // If "SecureBootEnable" variable is SECURE_BOOT_ENABLE and in USER_MODE, Set "SecureBoot" variable to SECURE_BOOT_MODE_ENABLE.
289 // If "SecureBootEnable" variable is SECURE_BOOT_DISABLE, Set "SecureBoot" variable to SECURE_BOOT_MODE_DISABLE.
291 SecureBootEnable
= SECURE_BOOT_DISABLE
;
292 FindVariable (EFI_SECURE_BOOT_ENABLE_NAME
, &gEfiSecureBootEnableDisableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
293 if (Variable
.CurrPtr
!= NULL
) {
294 SecureBootEnable
= *(GetVariableDataPtr (Variable
.CurrPtr
));
295 } else if (mPlatformMode
== USER_MODE
) {
297 // "SecureBootEnable" not exist, initialize it in USER_MODE.
299 SecureBootEnable
= SECURE_BOOT_ENABLE
;
300 Status
= UpdateVariable (
301 EFI_SECURE_BOOT_ENABLE_NAME
,
302 &gEfiSecureBootEnableDisableGuid
,
305 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
311 if (EFI_ERROR (Status
)) {
317 // Create "SecureBoot" variable with BS+RT attribute set.
319 if (SecureBootEnable
== SECURE_BOOT_ENABLE
&& mPlatformMode
== USER_MODE
) {
320 SecureBootMode
= SECURE_BOOT_MODE_ENABLE
;
322 SecureBootMode
= SECURE_BOOT_MODE_DISABLE
;
324 FindVariable (EFI_SECURE_BOOT_MODE_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
325 Status
= UpdateVariable (
326 EFI_SECURE_BOOT_MODE_NAME
,
327 &gEfiGlobalVariableGuid
,
330 EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
336 if (EFI_ERROR (Status
)) {
340 DEBUG ((EFI_D_INFO
, "Variable %s is %x\n", EFI_SETUP_MODE_NAME
, mPlatformMode
));
341 DEBUG ((EFI_D_INFO
, "Variable %s is %x\n", EFI_SECURE_BOOT_MODE_NAME
, SecureBootMode
));
342 DEBUG ((EFI_D_INFO
, "Variable %s is %x\n", EFI_SECURE_BOOT_ENABLE_NAME
, SecureBootEnable
));
345 // Initialize "CustomMode" in STANDARD_SECURE_BOOT_MODE state.
347 FindVariable (EFI_CUSTOM_MODE_NAME
, &gEfiCustomModeEnableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
348 CustomMode
= STANDARD_SECURE_BOOT_MODE
;
349 Status
= UpdateVariable (
350 EFI_CUSTOM_MODE_NAME
,
351 &gEfiCustomModeEnableGuid
,
354 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
360 if (EFI_ERROR (Status
)) {
364 DEBUG ((EFI_D_INFO
, "Variable %s is %x\n", EFI_CUSTOM_MODE_NAME
, CustomMode
));
367 // Check "certdb" variable's existence.
368 // If it doesn't exist, then create a new one with
369 // EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set.
371 Status
= FindVariable (
375 &mVariableModuleGlobal
->VariableGlobal
,
379 if (Variable
.CurrPtr
== NULL
) {
380 VarAttr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
381 ListSize
= sizeof (UINT32
);
382 Status
= UpdateVariable (
393 if (EFI_ERROR (Status
)) {
399 // Check "VendorKeysNv" variable's existence and create "VendorKeys" variable accordingly.
401 FindVariable (EFI_VENDOR_KEYS_NV_VARIABLE_NAME
, &gEfiVendorKeysNvGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
402 if (Variable
.CurrPtr
!= NULL
) {
403 mVendorKeyState
= *(GetVariableDataPtr (Variable
.CurrPtr
));
406 // "VendorKeysNv" not exist, initialize it in VENDOR_KEYS_VALID state.
408 mVendorKeyState
= VENDOR_KEYS_VALID
;
409 Status
= UpdateVariable (
410 EFI_VENDOR_KEYS_NV_VARIABLE_NAME
,
411 &gEfiVendorKeysNvGuid
,
414 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
,
420 if (EFI_ERROR (Status
)) {
426 // Create "VendorKeys" variable with BS+RT attribute set.
428 FindVariable (EFI_VENDOR_KEYS_VARIABLE_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
429 Status
= UpdateVariable (
430 EFI_VENDOR_KEYS_VARIABLE_NAME
,
431 &gEfiGlobalVariableGuid
,
434 EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
440 if (EFI_ERROR (Status
)) {
444 DEBUG ((EFI_D_INFO
, "Variable %s is %x\n", EFI_VENDOR_KEYS_VARIABLE_NAME
, mVendorKeyState
));
450 Add public key in store and return its index.
452 @param[in] PubKey Input pointer to Public Key data
454 @return Index of new added item
465 VARIABLE_POINTER_TRACK Variable
;
470 if (PubKey
== NULL
) {
474 Status
= FindVariable (
476 &gEfiAuthenticatedVariableGuid
,
478 &mVariableModuleGlobal
->VariableGlobal
,
481 ASSERT_EFI_ERROR (Status
);
482 if (EFI_ERROR (Status
)) {
487 // Check whether the public key entry does exist.
490 for (Ptr
= mPubKeyStore
, Index
= 1; Index
<= mPubKeyNumber
; Index
++) {
491 if (CompareMem (Ptr
, PubKey
, EFI_CERT_TYPE_RSA2048_SIZE
) == 0) {
495 Ptr
+= EFI_CERT_TYPE_RSA2048_SIZE
;
500 // Add public key in database.
502 if (mPubKeyNumber
== MAX_KEY_NUM
) {
504 // Public key dadatase is full, try to reclaim invalid key.
508 // NV storage can't reclaim at runtime.
514 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
515 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
522 if (EFI_ERROR (Status
)) {
526 Status
= FindVariable (
528 &gEfiAuthenticatedVariableGuid
,
530 &mVariableModuleGlobal
->VariableGlobal
,
533 ASSERT_EFI_ERROR (Status
);
534 if (EFI_ERROR (Status
)) {
538 DataSize
= DataSizeOfVariable (Variable
.CurrPtr
);
539 Data
= GetVariableDataPtr (Variable
.CurrPtr
);
540 ASSERT ((DataSize
!= 0) && (Data
!= NULL
));
541 CopyMem (mPubKeyStore
, (UINT8
*) Data
, DataSize
);
542 mPubKeyNumber
= (UINT32
) (DataSize
/ EFI_CERT_TYPE_RSA2048_SIZE
);
544 if (mPubKeyNumber
== MAX_KEY_NUM
) {
549 CopyMem (mPubKeyStore
+ mPubKeyNumber
* EFI_CERT_TYPE_RSA2048_SIZE
, PubKey
, EFI_CERT_TYPE_RSA2048_SIZE
);
550 Index
= ++mPubKeyNumber
;
552 // Update public key database variable.
554 Status
= UpdateVariable (
556 &gEfiAuthenticatedVariableGuid
,
558 mPubKeyNumber
* EFI_CERT_TYPE_RSA2048_SIZE
,
559 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
,
565 ASSERT_EFI_ERROR (Status
);
572 Verify data payload with AuthInfo in EFI_CERT_TYPE_RSA2048_SHA256_GUID type.
573 Follow the steps in UEFI2.2.
575 Caution: This function may receive untrusted input.
576 This function may be invoked in SMM mode, and datasize and data are external input.
577 This function will do basic validation, before parse the data.
578 This function will parse the authentication carefully to avoid security issues, like
579 buffer overflow, integer overflow.
581 @param[in] Data Pointer to data with AuthInfo.
582 @param[in] DataSize Size of Data.
583 @param[in] PubKey Public key used for verification.
585 @retval EFI_INVALID_PARAMETER Invalid parameter.
586 @retval EFI_SECURITY_VIOLATION If authentication failed.
587 @retval EFI_SUCCESS Authentication successful.
591 VerifyCounterBasedPayload (
598 EFI_VARIABLE_AUTHENTICATION
*CertData
;
599 EFI_CERT_BLOCK_RSA_2048_SHA256
*CertBlock
;
600 UINT8 Digest
[SHA256_DIGEST_SIZE
];
604 PayloadSize
= DataSize
- AUTHINFO_SIZE
;
609 if (Data
== NULL
|| PubKey
== NULL
) {
610 return EFI_INVALID_PARAMETER
;
613 CertData
= (EFI_VARIABLE_AUTHENTICATION
*) Data
;
614 CertBlock
= (EFI_CERT_BLOCK_RSA_2048_SHA256
*) (CertData
->AuthInfo
.CertData
);
617 // wCertificateType should be WIN_CERT_TYPE_EFI_GUID.
618 // Cert type should be EFI_CERT_TYPE_RSA2048_SHA256_GUID.
620 if ((CertData
->AuthInfo
.Hdr
.wCertificateType
!= WIN_CERT_TYPE_EFI_GUID
) ||
621 !CompareGuid (&CertData
->AuthInfo
.CertType
, &gEfiCertTypeRsa2048Sha256Guid
)
624 // Invalid AuthInfo type, return EFI_SECURITY_VIOLATION.
626 return EFI_SECURITY_VIOLATION
;
629 // Hash data payload with SHA256.
631 ZeroMem (Digest
, SHA256_DIGEST_SIZE
);
632 Status
= Sha256Init (mHashCtx
);
636 Status
= Sha256Update (mHashCtx
, Data
+ AUTHINFO_SIZE
, PayloadSize
);
643 Status
= Sha256Update (mHashCtx
, &PayloadSize
, sizeof (UINTN
));
648 // Hash Monotonic Count.
650 Status
= Sha256Update (mHashCtx
, &CertData
->MonotonicCount
, sizeof (UINT64
));
654 Status
= Sha256Final (mHashCtx
, Digest
);
659 // Generate & Initialize RSA Context.
662 ASSERT (Rsa
!= NULL
);
664 // Set RSA Key Components.
665 // NOTE: Only N and E are needed to be set as RSA public key for signature verification.
667 Status
= RsaSetKey (Rsa
, RsaKeyN
, PubKey
, EFI_CERT_TYPE_RSA2048_SIZE
);
671 Status
= RsaSetKey (Rsa
, RsaKeyE
, mRsaE
, sizeof (mRsaE
));
676 // Verify the signature.
678 Status
= RsaPkcs1Verify (
682 CertBlock
->Signature
,
683 EFI_CERT_TYPE_RSA2048_SHA256_SIZE
693 return EFI_SECURITY_VIOLATION
;
698 Update platform mode.
700 @param[in] Mode SETUP_MODE or USER_MODE.
702 @return EFI_INVALID_PARAMETER Invalid parameter.
703 @return EFI_SUCCESS Update platform mode successfully.
712 VARIABLE_POINTER_TRACK Variable
;
713 UINT8 SecureBootMode
;
714 UINT8 SecureBootEnable
;
715 UINTN VariableDataSize
;
717 Status
= FindVariable (
719 &gEfiGlobalVariableGuid
,
721 &mVariableModuleGlobal
->VariableGlobal
,
724 if (EFI_ERROR (Status
)) {
729 // Update the value of SetupMode variable by a simple mem copy, this could avoid possible
730 // variable storage reclaim at runtime.
732 mPlatformMode
= (UINT8
) Mode
;
733 CopyMem (GetVariableDataPtr (Variable
.CurrPtr
), &mPlatformMode
, sizeof(UINT8
));
737 // SecureBoot Variable indicates whether the platform firmware is operating
738 // in Secure boot mode (1) or not (0), so we should not change SecureBoot
739 // Variable in runtime.
745 // Check "SecureBoot" variable's existence.
746 // If it doesn't exist, firmware has no capability to perform driver signing verification,
747 // then set "SecureBoot" to 0.
749 Status
= FindVariable (
750 EFI_SECURE_BOOT_MODE_NAME
,
751 &gEfiGlobalVariableGuid
,
753 &mVariableModuleGlobal
->VariableGlobal
,
757 // If "SecureBoot" variable exists, then check "SetupMode" variable update.
758 // If "SetupMode" variable is USER_MODE, "SecureBoot" variable is set to 1.
759 // If "SetupMode" variable is SETUP_MODE, "SecureBoot" variable is set to 0.
761 if (Variable
.CurrPtr
== NULL
) {
762 SecureBootMode
= SECURE_BOOT_MODE_DISABLE
;
764 if (mPlatformMode
== USER_MODE
) {
765 SecureBootMode
= SECURE_BOOT_MODE_ENABLE
;
766 } else if (mPlatformMode
== SETUP_MODE
) {
767 SecureBootMode
= SECURE_BOOT_MODE_DISABLE
;
769 return EFI_NOT_FOUND
;
773 Status
= UpdateVariable (
774 EFI_SECURE_BOOT_MODE_NAME
,
775 &gEfiGlobalVariableGuid
,
778 EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
784 if (EFI_ERROR (Status
)) {
789 // Check "SecureBootEnable" variable's existence. It can enable/disable secure boot feature.
791 Status
= FindVariable (
792 EFI_SECURE_BOOT_ENABLE_NAME
,
793 &gEfiSecureBootEnableDisableGuid
,
795 &mVariableModuleGlobal
->VariableGlobal
,
799 if (SecureBootMode
== SECURE_BOOT_MODE_ENABLE
) {
801 // Create the "SecureBootEnable" variable as secure boot is enabled.
803 SecureBootEnable
= SECURE_BOOT_ENABLE
;
804 VariableDataSize
= sizeof (SecureBootEnable
);
807 // Delete the "SecureBootEnable" variable if this variable exist as "SecureBoot"
808 // variable is not in secure boot state.
810 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
813 SecureBootEnable
= SECURE_BOOT_DISABLE
;
814 VariableDataSize
= 0;
817 Status
= UpdateVariable (
818 EFI_SECURE_BOOT_ENABLE_NAME
,
819 &gEfiSecureBootEnableDisableGuid
,
822 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
832 Check input data form to make sure it is a valid EFI_SIGNATURE_LIST for PK/KEK/db/dbx variable.
834 @param[in] VariableName Name of Variable to be check.
835 @param[in] VendorGuid Variable vendor GUID.
836 @param[in] Data Point to the variable data to be checked.
837 @param[in] DataSize Size of Data.
839 @return EFI_INVALID_PARAMETER Invalid signature list format.
840 @return EFI_SUCCESS Passed signature list format check successfully.
844 CheckSignatureListFormat(
845 IN CHAR16
*VariableName
,
846 IN EFI_GUID
*VendorGuid
,
851 EFI_SIGNATURE_LIST
*SigList
;
857 EFI_SIGNATURE_DATA
*CertData
;
864 ASSERT (VariableName
!= NULL
&& VendorGuid
!= NULL
&& Data
!= NULL
);
866 if (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, EFI_PLATFORM_KEY_NAME
) == 0)){
868 } else if ((CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && StrCmp (VariableName
, EFI_KEY_EXCHANGE_KEY_NAME
) == 0) ||
869 (CompareGuid (VendorGuid
, &gEfiImageSecurityDatabaseGuid
) &&
870 (StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE
) == 0 || StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE1
) == 0))){
877 SigList
= (EFI_SIGNATURE_LIST
*) Data
;
878 SigDataSize
= DataSize
;
882 // Walk throuth the input signature list and check the data format.
883 // If any signature is incorrectly formed, the whole check will fail.
885 while ((SigDataSize
> 0) && (SigDataSize
>= SigList
->SignatureListSize
)) {
886 for (Index
= 0; Index
< (sizeof (mSupportSigItem
) / sizeof (EFI_SIGNATURE_ITEM
)); Index
++ ) {
887 if (CompareGuid (&SigList
->SignatureType
, &mSupportSigItem
[Index
].SigType
)) {
889 // The value of SignatureSize should always be 16 (size of SignatureOwner
890 // component) add the data length according to signature type.
892 if (mSupportSigItem
[Index
].SigDataSize
!= ((UINT32
) ~0) &&
893 (SigList
->SignatureSize
- sizeof (EFI_GUID
)) != mSupportSigItem
[Index
].SigDataSize
) {
894 return EFI_INVALID_PARAMETER
;
896 if (mSupportSigItem
[Index
].SigHeaderSize
!= ((UINTN
) ~0) &&
897 SigList
->SignatureHeaderSize
!= mSupportSigItem
[Index
].SigHeaderSize
) {
898 return EFI_INVALID_PARAMETER
;
904 if (Index
== (sizeof (mSupportSigItem
) / sizeof (EFI_SIGNATURE_ITEM
))) {
906 // Undefined signature type.
908 return EFI_INVALID_PARAMETER
;
911 if (CompareGuid (&SigList
->SignatureType
, &gEfiCertX509Guid
)) {
913 // Try to retrieve the RSA public key from the X.509 certificate.
914 // If this operation fails, it's not a valid certificate.
916 RsaContext
= RsaNew ();
917 if (RsaContext
== NULL
) {
918 return EFI_INVALID_PARAMETER
;
920 CertData
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) SigList
+ sizeof (EFI_SIGNATURE_LIST
) + SigList
->SignatureHeaderSize
);
921 CertLen
= SigList
->SignatureSize
- sizeof (EFI_GUID
);
922 if (!RsaGetPublicKeyFromX509 (CertData
->SignatureData
, CertLen
, &RsaContext
)) {
923 RsaFree (RsaContext
);
924 return EFI_INVALID_PARAMETER
;
926 RsaFree (RsaContext
);
929 if ((SigList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - SigList
->SignatureHeaderSize
) % SigList
->SignatureSize
!= 0) {
930 return EFI_INVALID_PARAMETER
;
932 SigCount
+= (SigList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - SigList
->SignatureHeaderSize
) / SigList
->SignatureSize
;
934 SigDataSize
-= SigList
->SignatureListSize
;
935 SigList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) SigList
+ SigList
->SignatureListSize
);
938 if (((UINTN
) SigList
- (UINTN
) Data
) != DataSize
) {
939 return EFI_INVALID_PARAMETER
;
942 if (IsPk
&& SigCount
> 1) {
943 return EFI_INVALID_PARAMETER
;
950 Update "VendorKeys" variable to record the out of band secure boot key modification.
952 @return EFI_SUCCESS Variable is updated successfully.
953 @return Others Failed to update variable.
957 VendorKeyIsModified (
962 VARIABLE_POINTER_TRACK Variable
;
964 if (mVendorKeyState
== VENDOR_KEYS_MODIFIED
) {
967 mVendorKeyState
= VENDOR_KEYS_MODIFIED
;
969 FindVariable (EFI_VENDOR_KEYS_NV_VARIABLE_NAME
, &gEfiVendorKeysNvGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
970 Status
= UpdateVariable (
971 EFI_VENDOR_KEYS_NV_VARIABLE_NAME
,
972 &gEfiVendorKeysNvGuid
,
975 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
,
981 if (EFI_ERROR (Status
)) {
985 FindVariable (EFI_VENDOR_KEYS_VARIABLE_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
986 return UpdateVariable (
987 EFI_VENDOR_KEYS_VARIABLE_NAME
,
988 &gEfiGlobalVariableGuid
,
991 EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
1000 Process variable with platform key for verification.
1002 Caution: This function may receive untrusted input.
1003 This function may be invoked in SMM mode, and datasize and data are external input.
1004 This function will do basic validation, before parse the data.
1005 This function will parse the authentication carefully to avoid security issues, like
1006 buffer overflow, integer overflow.
1007 This function will check attribute carefully to avoid authentication bypass.
1009 @param[in] VariableName Name of Variable to be found.
1010 @param[in] VendorGuid Variable vendor GUID.
1011 @param[in] Data Data pointer.
1012 @param[in] DataSize Size of Data found. If size is less than the
1013 data, this value contains the required size.
1014 @param[in] Variable The variable information which is used to keep track of variable usage.
1015 @param[in] Attributes Attribute value of the variable
1016 @param[in] IsPk Indicate whether it is to process pk.
1018 @return EFI_INVALID_PARAMETER Invalid parameter.
1019 @return EFI_SECURITY_VIOLATION The variable does NOT pass the validation.
1020 check carried out by the firmware.
1021 @return EFI_SUCCESS Variable passed validation successfully.
1026 IN CHAR16
*VariableName
,
1027 IN EFI_GUID
*VendorGuid
,
1030 IN VARIABLE_POINTER_TRACK
*Variable
,
1031 IN UINT32 Attributes OPTIONAL
,
1040 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0 ||
1041 (Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) == 0) {
1043 // PK, KEK and db/dbx should set EFI_VARIABLE_NON_VOLATILE attribute and should be a time-based
1044 // authenticated variable.
1046 return EFI_INVALID_PARAMETER
;
1050 if ((InCustomMode() && UserPhysicalPresent()) || (mPlatformMode
== SETUP_MODE
&& !IsPk
)) {
1051 Payload
= (UINT8
*) Data
+ AUTHINFO2_SIZE (Data
);
1052 PayloadSize
= DataSize
- AUTHINFO2_SIZE (Data
);
1053 if (PayloadSize
== 0) {
1057 Status
= CheckSignatureListFormat(VariableName
, VendorGuid
, Payload
, PayloadSize
);
1058 if (EFI_ERROR (Status
)) {
1062 Status
= UpdateVariable (
1071 &((EFI_VARIABLE_AUTHENTICATION_2
*) Data
)->TimeStamp
1073 if (EFI_ERROR(Status
)) {
1077 if ((mPlatformMode
!= SETUP_MODE
) || IsPk
) {
1078 Status
= VendorKeyIsModified ();
1080 } else if (mPlatformMode
== USER_MODE
) {
1082 // Verify against X509 Cert in PK database.
1084 Status
= VerifyTimeBasedPayload (
1096 // Verify against the certificate in data payload.
1098 Status
= VerifyTimeBasedPayload (
1110 if (!EFI_ERROR(Status
) && IsPk
) {
1111 if (mPlatformMode
== SETUP_MODE
&& !Del
) {
1113 // If enroll PK in setup mode, need change to user mode.
1115 Status
= UpdatePlatformMode (USER_MODE
);
1116 } else if (mPlatformMode
== USER_MODE
&& Del
){
1118 // If delete PK in user mode, need change to setup mode.
1120 Status
= UpdatePlatformMode (SETUP_MODE
);
1128 Process variable with key exchange key for verification.
1130 Caution: This function may receive untrusted input.
1131 This function may be invoked in SMM mode, and datasize and data are external input.
1132 This function will do basic validation, before parse the data.
1133 This function will parse the authentication carefully to avoid security issues, like
1134 buffer overflow, integer overflow.
1135 This function will check attribute carefully to avoid authentication bypass.
1137 @param[in] VariableName Name of Variable to be found.
1138 @param[in] VendorGuid Variable vendor GUID.
1139 @param[in] Data Data pointer.
1140 @param[in] DataSize Size of Data found. If size is less than the
1141 data, this value contains the required size.
1142 @param[in] Variable The variable information which is used to keep track of variable usage.
1143 @param[in] Attributes Attribute value of the variable.
1145 @return EFI_INVALID_PARAMETER Invalid parameter.
1146 @return EFI_SECURITY_VIOLATION The variable does NOT pass the validation
1147 check carried out by the firmware.
1148 @return EFI_SUCCESS Variable pass validation successfully.
1153 IN CHAR16
*VariableName
,
1154 IN EFI_GUID
*VendorGuid
,
1157 IN VARIABLE_POINTER_TRACK
*Variable
,
1158 IN UINT32 Attributes OPTIONAL
1165 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0 ||
1166 (Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) == 0) {
1168 // DB and DBX should set EFI_VARIABLE_NON_VOLATILE attribute and should be a time-based
1169 // authenticated variable.
1171 return EFI_INVALID_PARAMETER
;
1174 Status
= EFI_SUCCESS
;
1175 if (mPlatformMode
== USER_MODE
&& !(InCustomMode() && UserPhysicalPresent())) {
1177 // Time-based, verify against X509 Cert KEK.
1179 return VerifyTimeBasedPayload (
1191 // If in setup mode or custom secure boot mode, no authentication needed.
1193 Payload
= (UINT8
*) Data
+ AUTHINFO2_SIZE (Data
);
1194 PayloadSize
= DataSize
- AUTHINFO2_SIZE (Data
);
1196 Status
= CheckSignatureListFormat(VariableName
, VendorGuid
, Payload
, PayloadSize
);
1197 if (EFI_ERROR (Status
)) {
1201 Status
= UpdateVariable (
1210 &((EFI_VARIABLE_AUTHENTICATION_2
*) Data
)->TimeStamp
1212 if (EFI_ERROR (Status
)) {
1216 if (mPlatformMode
!= SETUP_MODE
) {
1217 Status
= VendorKeyIsModified ();
1225 Process variable with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS/EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set
1227 Caution: This function may receive untrusted input.
1228 This function may be invoked in SMM mode, and datasize and data are external input.
1229 This function will do basic validation, before parse the data.
1230 This function will parse the authentication carefully to avoid security issues, like
1231 buffer overflow, integer overflow.
1232 This function will check attribute carefully to avoid authentication bypass.
1234 @param[in] VariableName Name of Variable to be found.
1235 @param[in] VendorGuid Variable vendor GUID.
1237 @param[in] Data Data pointer.
1238 @param[in] DataSize Size of Data found. If size is less than the
1239 data, this value contains the required size.
1240 @param[in] Variable The variable information which is used to keep track of variable usage.
1241 @param[in] Attributes Attribute value of the variable.
1243 @return EFI_INVALID_PARAMETER Invalid parameter.
1244 @return EFI_WRITE_PROTECTED Variable is write-protected and needs authentication with
1245 EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS set.
1246 @return EFI_OUT_OF_RESOURCES The Database to save the public key is full.
1247 @return EFI_SECURITY_VIOLATION The variable is with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
1248 set, but the AuthInfo does NOT pass the validation
1249 check carried out by the firmware.
1250 @return EFI_SUCCESS Variable is not write-protected or pass validation successfully.
1255 IN CHAR16
*VariableName
,
1256 IN EFI_GUID
*VendorGuid
,
1259 IN VARIABLE_POINTER_TRACK
*Variable
,
1260 IN UINT32 Attributes
1265 BOOLEAN IsFirstTime
;
1267 EFI_VARIABLE_AUTHENTICATION
*CertData
;
1268 EFI_CERT_BLOCK_RSA_2048_SHA256
*CertBlock
;
1270 UINT64 MonotonicCount
;
1278 if (NeedPhysicallyPresent(VariableName
, VendorGuid
) && !UserPhysicalPresent()) {
1280 // This variable is protected, only physical present user could modify its value.
1282 return EFI_SECURITY_VIOLATION
;
1286 // A time-based authenticated variable and a count-based authenticated variable
1287 // can't be updated by each other.
1289 if (Variable
->CurrPtr
!= NULL
) {
1290 if (((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) &&
1291 ((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0)) {
1292 return EFI_SECURITY_VIOLATION
;
1295 if (((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) &&
1296 ((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0)) {
1297 return EFI_SECURITY_VIOLATION
;
1302 // Process Time-based Authenticated variable.
1304 if ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) {
1305 return VerifyTimeBasedPayload (
1318 // Determine if first time SetVariable with the EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS.
1320 if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) {
1322 // Determine current operation type.
1324 if (DataSize
== AUTHINFO_SIZE
) {
1328 // Determine whether this is the first time with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS set.
1330 if (Variable
->CurrPtr
== NULL
) {
1332 } else if ((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) == 0) {
1335 KeyIndex
= Variable
->CurrPtr
->PubKeyIndex
;
1336 IsFirstTime
= FALSE
;
1338 } else if ((Variable
->CurrPtr
!= NULL
) &&
1339 ((Variable
->CurrPtr
->Attributes
& (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
)) != 0)
1342 // If the variable is already write-protected, it always needs authentication before update.
1344 return EFI_WRITE_PROTECTED
;
1347 // If without EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS, set and attributes collision.
1348 // That means it is not authenticated variable, just update variable as usual.
1350 Status
= UpdateVariable (VariableName
, VendorGuid
, Data
, DataSize
, Attributes
, 0, 0, Variable
, NULL
);
1355 // Get PubKey and check Monotonic Count value corresponding to the variable.
1357 CertData
= (EFI_VARIABLE_AUTHENTICATION
*) Data
;
1358 CertBlock
= (EFI_CERT_BLOCK_RSA_2048_SHA256
*) (CertData
->AuthInfo
.CertData
);
1359 PubKey
= CertBlock
->PublicKey
;
1362 // Update Monotonic Count value.
1364 MonotonicCount
= CertData
->MonotonicCount
;
1368 // Check input PubKey.
1370 if (CompareMem (PubKey
, mPubKeyStore
+ (KeyIndex
- 1) * EFI_CERT_TYPE_RSA2048_SIZE
, EFI_CERT_TYPE_RSA2048_SIZE
) != 0) {
1371 return EFI_SECURITY_VIOLATION
;
1374 // Compare the current monotonic count and ensure that it is greater than the last SetVariable
1375 // operation with the EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS attribute set.
1377 if (CertData
->MonotonicCount
<= Variable
->CurrPtr
->MonotonicCount
) {
1379 // Monotonic count check fail, suspicious replay attack, return EFI_SECURITY_VIOLATION.
1381 return EFI_SECURITY_VIOLATION
;
1385 // Verify the certificate in Data payload.
1387 Status
= VerifyCounterBasedPayload (Data
, DataSize
, PubKey
);
1388 if (EFI_ERROR (Status
)) {
1393 // Now, the signature has been verified!
1395 if (IsFirstTime
&& !IsDeletion
) {
1397 // Update public key database variable if need.
1399 KeyIndex
= AddPubKeyInStore (PubKey
);
1400 if (KeyIndex
== 0) {
1401 return EFI_OUT_OF_RESOURCES
;
1406 // Verification pass.
1408 return UpdateVariable (VariableName
, VendorGuid
, (UINT8
*)Data
+ AUTHINFO_SIZE
, DataSize
- AUTHINFO_SIZE
, Attributes
, KeyIndex
, MonotonicCount
, Variable
, NULL
);
1412 Merge two buffers which formatted as EFI_SIGNATURE_LIST. Only the new EFI_SIGNATURE_DATA
1413 will be appended to the original EFI_SIGNATURE_LIST, duplicate EFI_SIGNATURE_DATA
1416 @param[in, out] Data Pointer to original EFI_SIGNATURE_LIST.
1417 @param[in] DataSize Size of Data buffer.
1418 @param[in] FreeBufSize Size of free data buffer
1419 @param[in] NewData Pointer to new EFI_SIGNATURE_LIST to be appended.
1420 @param[in] NewDataSize Size of NewData buffer.
1421 @param[out] MergedBufSize Size of the merged buffer
1423 @return EFI_BUFFER_TOO_SMALL if input Data buffer overflowed
1427 AppendSignatureList (
1430 IN UINTN FreeBufSize
,
1432 IN UINTN NewDataSize
,
1433 OUT UINTN
*MergedBufSize
1436 EFI_SIGNATURE_LIST
*CertList
;
1437 EFI_SIGNATURE_DATA
*Cert
;
1439 EFI_SIGNATURE_LIST
*NewCertList
;
1440 EFI_SIGNATURE_DATA
*NewCert
;
1447 UINTN SignatureListSize
;
1450 Tail
= (UINT8
*) Data
+ DataSize
;
1452 NewCertList
= (EFI_SIGNATURE_LIST
*) NewData
;
1453 while ((NewDataSize
> 0) && (NewDataSize
>= NewCertList
->SignatureListSize
)) {
1454 NewCert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) NewCertList
+ sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
);
1455 NewCertCount
= (NewCertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - NewCertList
->SignatureHeaderSize
) / NewCertList
->SignatureSize
;
1458 for (Index
= 0; Index
< NewCertCount
; Index
++) {
1462 CertList
= (EFI_SIGNATURE_LIST
*) Data
;
1463 while ((Size
> 0) && (Size
>= CertList
->SignatureListSize
)) {
1464 if (CompareGuid (&CertList
->SignatureType
, &NewCertList
->SignatureType
) &&
1465 (CertList
->SignatureSize
== NewCertList
->SignatureSize
)) {
1466 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
1467 CertCount
= (CertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - CertList
->SignatureHeaderSize
) / CertList
->SignatureSize
;
1468 for (Index2
= 0; Index2
< CertCount
; Index2
++) {
1470 // Iterate each Signature Data in this Signature List.
1472 if (CompareMem (NewCert
, Cert
, CertList
->SignatureSize
) == 0) {
1476 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) Cert
+ CertList
->SignatureSize
);
1483 Size
-= CertList
->SignatureListSize
;
1484 CertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) CertList
+ CertList
->SignatureListSize
);
1489 // New EFI_SIGNATURE_DATA, append it.
1491 if (CopiedCount
== 0) {
1492 if (FreeBufSize
< sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
) {
1493 return EFI_BUFFER_TOO_SMALL
;
1497 // Copy EFI_SIGNATURE_LIST header for only once.
1500 CopyMem (Tail
, NewCertList
, sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
);
1501 Tail
= Tail
+ sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
;
1502 FreeBufSize
-= sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
;
1505 if (FreeBufSize
< NewCertList
->SignatureSize
) {
1506 return EFI_BUFFER_TOO_SMALL
;
1508 CopyMem (Tail
, NewCert
, NewCertList
->SignatureSize
);
1509 Tail
+= NewCertList
->SignatureSize
;
1510 FreeBufSize
-= NewCertList
->SignatureSize
;
1514 NewCert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) NewCert
+ NewCertList
->SignatureSize
);
1518 // Update SignatureListSize in newly appended EFI_SIGNATURE_LIST.
1520 if (CopiedCount
!= 0) {
1521 SignatureListSize
= sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
+ (CopiedCount
* NewCertList
->SignatureSize
);
1522 CertList
= (EFI_SIGNATURE_LIST
*) (Tail
- SignatureListSize
);
1523 CertList
->SignatureListSize
= (UINT32
) SignatureListSize
;
1526 NewDataSize
-= NewCertList
->SignatureListSize
;
1527 NewCertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) NewCertList
+ NewCertList
->SignatureListSize
);
1530 *MergedBufSize
= (Tail
- (UINT8
*) Data
);
1535 Compare two EFI_TIME data.
1538 @param FirstTime A pointer to the first EFI_TIME data.
1539 @param SecondTime A pointer to the second EFI_TIME data.
1541 @retval TRUE The FirstTime is not later than the SecondTime.
1542 @retval FALSE The FirstTime is later than the SecondTime.
1547 IN EFI_TIME
*FirstTime
,
1548 IN EFI_TIME
*SecondTime
1551 if (FirstTime
->Year
!= SecondTime
->Year
) {
1552 return (BOOLEAN
) (FirstTime
->Year
< SecondTime
->Year
);
1553 } else if (FirstTime
->Month
!= SecondTime
->Month
) {
1554 return (BOOLEAN
) (FirstTime
->Month
< SecondTime
->Month
);
1555 } else if (FirstTime
->Day
!= SecondTime
->Day
) {
1556 return (BOOLEAN
) (FirstTime
->Day
< SecondTime
->Day
);
1557 } else if (FirstTime
->Hour
!= SecondTime
->Hour
) {
1558 return (BOOLEAN
) (FirstTime
->Hour
< SecondTime
->Hour
);
1559 } else if (FirstTime
->Minute
!= SecondTime
->Minute
) {
1560 return (BOOLEAN
) (FirstTime
->Minute
< SecondTime
->Minute
);
1563 return (BOOLEAN
) (FirstTime
->Second
<= SecondTime
->Second
);
1567 Find matching signer's certificates for common authenticated variable
1568 by corresponding VariableName and VendorGuid from "certdb".
1570 The data format of "certdb":
1572 // UINT32 CertDbListSize;
1573 // /// AUTH_CERT_DB_DATA Certs1[];
1574 // /// AUTH_CERT_DB_DATA Certs2[];
1576 // /// AUTH_CERT_DB_DATA Certsn[];
1579 @param[in] VariableName Name of authenticated Variable.
1580 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1581 @param[in] Data Pointer to variable "certdb".
1582 @param[in] DataSize Size of variable "certdb".
1583 @param[out] CertOffset Offset of matching CertData, from starting of Data.
1584 @param[out] CertDataSize Length of CertData in bytes.
1585 @param[out] CertNodeOffset Offset of matching AUTH_CERT_DB_DATA , from
1587 @param[out] CertNodeSize Length of AUTH_CERT_DB_DATA in bytes.
1589 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1590 @retval EFI_NOT_FOUND Fail to find matching certs.
1591 @retval EFI_SUCCESS Find matching certs and output parameters.
1596 IN CHAR16
*VariableName
,
1597 IN EFI_GUID
*VendorGuid
,
1600 OUT UINT32
*CertOffset
, OPTIONAL
1601 OUT UINT32
*CertDataSize
, OPTIONAL
1602 OUT UINT32
*CertNodeOffset
,OPTIONAL
1603 OUT UINT32
*CertNodeSize OPTIONAL
1607 AUTH_CERT_DB_DATA
*Ptr
;
1611 UINT32 CertDbListSize
;
1613 if ((VariableName
== NULL
) || (VendorGuid
== NULL
) || (Data
== NULL
)) {
1614 return EFI_INVALID_PARAMETER
;
1618 // Check whether DataSize matches recorded CertDbListSize.
1620 if (DataSize
< sizeof (UINT32
)) {
1621 return EFI_INVALID_PARAMETER
;
1624 CertDbListSize
= ReadUnaligned32 ((UINT32
*) Data
);
1626 if (CertDbListSize
!= (UINT32
) DataSize
) {
1627 return EFI_INVALID_PARAMETER
;
1630 Offset
= sizeof (UINT32
);
1633 // Get corresponding certificates by VendorGuid and VariableName.
1635 while (Offset
< (UINT32
) DataSize
) {
1636 Ptr
= (AUTH_CERT_DB_DATA
*) (Data
+ Offset
);
1638 // Check whether VendorGuid matches.
1640 if (CompareGuid (&Ptr
->VendorGuid
, VendorGuid
)) {
1641 NodeSize
= ReadUnaligned32 (&Ptr
->CertNodeSize
);
1642 NameSize
= ReadUnaligned32 (&Ptr
->NameSize
);
1643 CertSize
= ReadUnaligned32 (&Ptr
->CertDataSize
);
1645 if (NodeSize
!= sizeof (EFI_GUID
) + sizeof (UINT32
) * 3 + CertSize
+
1646 sizeof (CHAR16
) * NameSize
) {
1647 return EFI_INVALID_PARAMETER
;
1650 Offset
= Offset
+ sizeof (EFI_GUID
) + sizeof (UINT32
) * 3;
1652 // Check whether VariableName matches.
1654 if ((NameSize
== StrLen (VariableName
)) &&
1655 (CompareMem (Data
+ Offset
, VariableName
, NameSize
* sizeof (CHAR16
)) == 0)) {
1656 Offset
= Offset
+ NameSize
* sizeof (CHAR16
);
1658 if (CertOffset
!= NULL
) {
1659 *CertOffset
= Offset
;
1662 if (CertDataSize
!= NULL
) {
1663 *CertDataSize
= CertSize
;
1666 if (CertNodeOffset
!= NULL
) {
1667 *CertNodeOffset
= (UINT32
) ((UINT8
*) Ptr
- Data
);
1670 if (CertNodeSize
!= NULL
) {
1671 *CertNodeSize
= NodeSize
;
1676 Offset
= Offset
+ NameSize
* sizeof (CHAR16
) + CertSize
;
1679 NodeSize
= ReadUnaligned32 (&Ptr
->CertNodeSize
);
1680 Offset
= Offset
+ NodeSize
;
1684 return EFI_NOT_FOUND
;
1688 Retrieve signer's certificates for common authenticated variable
1689 by corresponding VariableName and VendorGuid from "certdb".
1691 @param[in] VariableName Name of authenticated Variable.
1692 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1693 @param[out] CertData Pointer to signer's certificates.
1694 @param[out] CertDataSize Length of CertData in bytes.
1696 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1697 @retval EFI_NOT_FOUND Fail to find "certdb" or matching certs.
1698 @retval EFI_SUCCESS Get signer's certificates successfully.
1703 IN CHAR16
*VariableName
,
1704 IN EFI_GUID
*VendorGuid
,
1705 OUT UINT8
**CertData
,
1706 OUT UINT32
*CertDataSize
1709 VARIABLE_POINTER_TRACK CertDbVariable
;
1715 if ((VariableName
== NULL
) || (VendorGuid
== NULL
) || (CertData
== NULL
) || (CertDataSize
== NULL
)) {
1716 return EFI_INVALID_PARAMETER
;
1720 // Get variable "certdb".
1722 Status
= FindVariable (
1726 &mVariableModuleGlobal
->VariableGlobal
,
1729 if (EFI_ERROR (Status
)) {
1733 DataSize
= DataSizeOfVariable (CertDbVariable
.CurrPtr
);
1734 Data
= GetVariableDataPtr (CertDbVariable
.CurrPtr
);
1735 if ((DataSize
== 0) || (Data
== NULL
)) {
1737 return EFI_NOT_FOUND
;
1740 Status
= FindCertsFromDb (
1751 if (EFI_ERROR (Status
)) {
1755 *CertData
= Data
+ CertOffset
;
1760 Delete matching signer's certificates when deleting common authenticated
1761 variable by corresponding VariableName and VendorGuid from "certdb".
1763 @param[in] VariableName Name of authenticated Variable.
1764 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1766 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1767 @retval EFI_NOT_FOUND Fail to find "certdb" or matching certs.
1768 @retval EFI_OUT_OF_RESOURCES The operation is failed due to lack of resources.
1769 @retval EFI_SUCCESS The operation is completed successfully.
1774 IN CHAR16
*VariableName
,
1775 IN EFI_GUID
*VendorGuid
1778 VARIABLE_POINTER_TRACK CertDbVariable
;
1783 UINT32 CertNodeOffset
;
1784 UINT32 CertNodeSize
;
1786 UINT32 NewCertDbSize
;
1788 if ((VariableName
== NULL
) || (VendorGuid
== NULL
)) {
1789 return EFI_INVALID_PARAMETER
;
1793 // Get variable "certdb".
1795 Status
= FindVariable (
1799 &mVariableModuleGlobal
->VariableGlobal
,
1802 if (EFI_ERROR (Status
)) {
1806 DataSize
= DataSizeOfVariable (CertDbVariable
.CurrPtr
);
1807 Data
= GetVariableDataPtr (CertDbVariable
.CurrPtr
);
1808 if ((DataSize
== 0) || (Data
== NULL
)) {
1810 return EFI_NOT_FOUND
;
1813 if (DataSize
== sizeof (UINT32
)) {
1815 // There is no certs in certdb.
1821 // Get corresponding cert node from certdb.
1823 Status
= FindCertsFromDb (
1834 if (EFI_ERROR (Status
)) {
1838 if (DataSize
< (CertNodeOffset
+ CertNodeSize
)) {
1839 return EFI_NOT_FOUND
;
1843 // Construct new data content of variable "certdb".
1845 NewCertDbSize
= (UINT32
) DataSize
- CertNodeSize
;
1846 NewCertDb
= (UINT8
*) mCertDbStore
;
1849 // Copy the DB entries before deleting node.
1851 CopyMem (NewCertDb
, Data
, CertNodeOffset
);
1853 // Update CertDbListSize.
1855 CopyMem (NewCertDb
, &NewCertDbSize
, sizeof (UINT32
));
1857 // Copy the DB entries after deleting node.
1859 if (DataSize
> (CertNodeOffset
+ CertNodeSize
)) {
1861 NewCertDb
+ CertNodeOffset
,
1862 Data
+ CertNodeOffset
+ CertNodeSize
,
1863 DataSize
- CertNodeOffset
- CertNodeSize
1870 VarAttr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
1871 Status
= UpdateVariable (
1887 Insert signer's certificates for common authenticated variable with VariableName
1888 and VendorGuid in AUTH_CERT_DB_DATA to "certdb".
1890 @param[in] VariableName Name of authenticated Variable.
1891 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1892 @param[in] CertData Pointer to signer's certificates.
1893 @param[in] CertDataSize Length of CertData in bytes.
1895 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1896 @retval EFI_ACCESS_DENIED An AUTH_CERT_DB_DATA entry with same VariableName
1897 and VendorGuid already exists.
1898 @retval EFI_OUT_OF_RESOURCES The operation is failed due to lack of resources.
1899 @retval EFI_SUCCESS Insert an AUTH_CERT_DB_DATA entry to "certdb"
1904 IN CHAR16
*VariableName
,
1905 IN EFI_GUID
*VendorGuid
,
1907 IN UINTN CertDataSize
1910 VARIABLE_POINTER_TRACK CertDbVariable
;
1916 UINT32 NewCertDbSize
;
1917 UINT32 CertNodeSize
;
1919 AUTH_CERT_DB_DATA
*Ptr
;
1921 if ((VariableName
== NULL
) || (VendorGuid
== NULL
) || (CertData
== NULL
)) {
1922 return EFI_INVALID_PARAMETER
;
1926 // Get variable "certdb".
1928 Status
= FindVariable (
1932 &mVariableModuleGlobal
->VariableGlobal
,
1935 if (EFI_ERROR (Status
)) {
1939 DataSize
= DataSizeOfVariable (CertDbVariable
.CurrPtr
);
1940 Data
= GetVariableDataPtr (CertDbVariable
.CurrPtr
);
1941 if ((DataSize
== 0) || (Data
== NULL
)) {
1943 return EFI_NOT_FOUND
;
1947 // Find whether matching cert node already exists in "certdb".
1948 // If yes return error.
1950 Status
= FindCertsFromDb (
1961 if (!EFI_ERROR (Status
)) {
1963 return EFI_ACCESS_DENIED
;
1967 // Construct new data content of variable "certdb".
1969 NameSize
= (UINT32
) StrLen (VariableName
);
1970 CertNodeSize
= sizeof (AUTH_CERT_DB_DATA
) + (UINT32
) CertDataSize
+ NameSize
* sizeof (CHAR16
);
1971 NewCertDbSize
= (UINT32
) DataSize
+ CertNodeSize
;
1972 if (NewCertDbSize
> MAX_CERTDB_SIZE
) {
1973 return EFI_OUT_OF_RESOURCES
;
1975 NewCertDb
= (UINT8
*) mCertDbStore
;
1978 // Copy the DB entries before deleting node.
1980 CopyMem (NewCertDb
, Data
, DataSize
);
1982 // Update CertDbListSize.
1984 CopyMem (NewCertDb
, &NewCertDbSize
, sizeof (UINT32
));
1986 // Construct new cert node.
1988 Ptr
= (AUTH_CERT_DB_DATA
*) (NewCertDb
+ DataSize
);
1989 CopyGuid (&Ptr
->VendorGuid
, VendorGuid
);
1990 CopyMem (&Ptr
->CertNodeSize
, &CertNodeSize
, sizeof (UINT32
));
1991 CopyMem (&Ptr
->NameSize
, &NameSize
, sizeof (UINT32
));
1992 CopyMem (&Ptr
->CertDataSize
, &CertDataSize
, sizeof (UINT32
));
1995 (UINT8
*) Ptr
+ sizeof (AUTH_CERT_DB_DATA
),
1997 NameSize
* sizeof (CHAR16
)
2001 (UINT8
*) Ptr
+ sizeof (AUTH_CERT_DB_DATA
) + NameSize
* sizeof (CHAR16
),
2009 VarAttr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
2010 Status
= UpdateVariable (
2026 Process variable with EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set
2028 Caution: This function may receive untrusted input.
2029 This function may be invoked in SMM mode, and datasize and data are external input.
2030 This function will do basic validation, before parse the data.
2031 This function will parse the authentication carefully to avoid security issues, like
2032 buffer overflow, integer overflow.
2034 @param[in] VariableName Name of Variable to be found.
2035 @param[in] VendorGuid Variable vendor GUID.
2036 @param[in] Data Data pointer.
2037 @param[in] DataSize Size of Data found. If size is less than the
2038 data, this value contains the required size.
2039 @param[in] Variable The variable information which is used to keep track of variable usage.
2040 @param[in] Attributes Attribute value of the variable.
2041 @param[in] AuthVarType Verify against PK, KEK database, private database or certificate in data payload.
2042 @param[out] VarDel Delete the variable or not.
2044 @retval EFI_INVALID_PARAMETER Invalid parameter.
2045 @retval EFI_SECURITY_VIOLATION The variable does NOT pass the validation
2046 check carried out by the firmware.
2047 @retval EFI_OUT_OF_RESOURCES Failed to process variable due to lack
2049 @retval EFI_SUCCESS Variable pass validation successfully.
2053 VerifyTimeBasedPayload (
2054 IN CHAR16
*VariableName
,
2055 IN EFI_GUID
*VendorGuid
,
2058 IN VARIABLE_POINTER_TRACK
*Variable
,
2059 IN UINT32 Attributes
,
2060 IN AUTHVAR_TYPE AuthVarType
,
2074 BOOLEAN VerifyStatus
;
2076 EFI_SIGNATURE_LIST
*CertList
;
2077 EFI_SIGNATURE_DATA
*Cert
;
2078 VARIABLE_POINTER_TRACK KekVariable
;
2079 EFI_VARIABLE_AUTHENTICATION_2
*CertData
;
2082 VARIABLE_POINTER_TRACK PkVariable
;
2087 UINTN CertStackSize
;
2088 UINT8
*CertsInCertDb
;
2089 UINT32 CertsSizeinDb
;
2091 VerifyStatus
= FALSE
;
2100 // When the attribute EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS is
2101 // set, then the Data buffer shall begin with an instance of a complete (and serialized)
2102 // EFI_VARIABLE_AUTHENTICATION_2 descriptor. The descriptor shall be followed by the new
2103 // variable value and DataSize shall reflect the combined size of the descriptor and the new
2104 // variable value. The authentication descriptor is not part of the variable data and is not
2105 // returned by subsequent calls to GetVariable().
2107 CertData
= (EFI_VARIABLE_AUTHENTICATION_2
*) Data
;
2110 // Verify that Pad1, Nanosecond, TimeZone, Daylight and Pad2 components of the
2111 // TimeStamp value are set to zero.
2113 if ((CertData
->TimeStamp
.Pad1
!= 0) ||
2114 (CertData
->TimeStamp
.Nanosecond
!= 0) ||
2115 (CertData
->TimeStamp
.TimeZone
!= 0) ||
2116 (CertData
->TimeStamp
.Daylight
!= 0) ||
2117 (CertData
->TimeStamp
.Pad2
!= 0)) {
2118 return EFI_SECURITY_VIOLATION
;
2121 if ((Variable
->CurrPtr
!= NULL
) && ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0)) {
2122 if (CompareTimeStamp (&CertData
->TimeStamp
, &Variable
->CurrPtr
->TimeStamp
)) {
2124 // TimeStamp check fail, suspicious replay attack, return EFI_SECURITY_VIOLATION.
2126 return EFI_SECURITY_VIOLATION
;
2131 // wCertificateType should be WIN_CERT_TYPE_EFI_GUID.
2132 // Cert type should be EFI_CERT_TYPE_PKCS7_GUID.
2134 if ((CertData
->AuthInfo
.Hdr
.wCertificateType
!= WIN_CERT_TYPE_EFI_GUID
) ||
2135 !CompareGuid (&CertData
->AuthInfo
.CertType
, &gEfiCertPkcs7Guid
)) {
2137 // Invalid AuthInfo type, return EFI_SECURITY_VIOLATION.
2139 return EFI_SECURITY_VIOLATION
;
2143 // Find out Pkcs7 SignedData which follows the EFI_VARIABLE_AUTHENTICATION_2 descriptor.
2144 // AuthInfo.Hdr.dwLength is the length of the entire certificate, including the length of the header.
2146 SigData
= CertData
->AuthInfo
.CertData
;
2147 SigDataSize
= CertData
->AuthInfo
.Hdr
.dwLength
- (UINT32
) (OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID
, CertData
));
2150 // Find out the new data payload which follows Pkcs7 SignedData directly.
2152 PayloadPtr
= SigData
+ SigDataSize
;
2153 PayloadSize
= DataSize
- OFFSET_OF_AUTHINFO2_CERT_DATA
- (UINTN
) SigDataSize
;
2156 // Construct a buffer to fill with (VariableName, VendorGuid, Attributes, TimeStamp, Data).
2158 NewDataSize
= PayloadSize
+ sizeof (EFI_TIME
) + sizeof (UINT32
) +
2159 sizeof (EFI_GUID
) + StrSize (VariableName
) - sizeof (CHAR16
);
2160 NewData
= mSerializationRuntimeBuffer
;
2163 Length
= StrLen (VariableName
) * sizeof (CHAR16
);
2164 CopyMem (Buffer
, VariableName
, Length
);
2167 Length
= sizeof (EFI_GUID
);
2168 CopyMem (Buffer
, VendorGuid
, Length
);
2171 Length
= sizeof (UINT32
);
2172 CopyMem (Buffer
, &Attr
, Length
);
2175 Length
= sizeof (EFI_TIME
);
2176 CopyMem (Buffer
, &CertData
->TimeStamp
, Length
);
2179 CopyMem (Buffer
, PayloadPtr
, PayloadSize
);
2181 if (AuthVarType
== AuthVarTypePk
) {
2183 // Verify that the signature has been made with the current Platform Key (no chaining for PK).
2184 // First, get signer's certificates from SignedData.
2186 VerifyStatus
= Pkcs7GetSigners (
2194 if (!VerifyStatus
) {
2199 // Second, get the current platform key from variable. Check whether it's identical with signer's certificates
2200 // in SignedData. If not, return error immediately.
2202 Status
= FindVariable (
2203 EFI_PLATFORM_KEY_NAME
,
2204 &gEfiGlobalVariableGuid
,
2206 &mVariableModuleGlobal
->VariableGlobal
,
2209 if (EFI_ERROR (Status
)) {
2210 VerifyStatus
= FALSE
;
2213 CertList
= (EFI_SIGNATURE_LIST
*) GetVariableDataPtr (PkVariable
.CurrPtr
);
2214 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
2215 if ((RootCertSize
!= (CertList
->SignatureSize
- (sizeof (EFI_SIGNATURE_DATA
) - 1))) ||
2216 (CompareMem (Cert
->SignatureData
, RootCert
, RootCertSize
) != 0)) {
2217 VerifyStatus
= FALSE
;
2222 // Verify Pkcs7 SignedData via Pkcs7Verify library.
2224 VerifyStatus
= Pkcs7Verify (
2233 } else if (AuthVarType
== AuthVarTypeKek
) {
2236 // Get KEK database from variable.
2238 Status
= FindVariable (
2239 EFI_KEY_EXCHANGE_KEY_NAME
,
2240 &gEfiGlobalVariableGuid
,
2242 &mVariableModuleGlobal
->VariableGlobal
,
2245 if (EFI_ERROR (Status
)) {
2250 // Ready to verify Pkcs7 SignedData. Go through KEK Signature Database to find out X.509 CertList.
2252 KekDataSize
= KekVariable
.CurrPtr
->DataSize
;
2253 CertList
= (EFI_SIGNATURE_LIST
*) GetVariableDataPtr (KekVariable
.CurrPtr
);
2254 while ((KekDataSize
> 0) && (KekDataSize
>= CertList
->SignatureListSize
)) {
2255 if (CompareGuid (&CertList
->SignatureType
, &gEfiCertX509Guid
)) {
2256 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
2257 CertCount
= (CertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - CertList
->SignatureHeaderSize
) / CertList
->SignatureSize
;
2258 for (Index
= 0; Index
< CertCount
; Index
++) {
2260 // Iterate each Signature Data Node within this CertList for a verify
2262 RootCert
= Cert
->SignatureData
;
2263 RootCertSize
= CertList
->SignatureSize
- (sizeof (EFI_SIGNATURE_DATA
) - 1);
2266 // Verify Pkcs7 SignedData via Pkcs7Verify library.
2268 VerifyStatus
= Pkcs7Verify (
2279 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) Cert
+ CertList
->SignatureSize
);
2282 KekDataSize
-= CertList
->SignatureListSize
;
2283 CertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) CertList
+ CertList
->SignatureListSize
);
2285 } else if (AuthVarType
== AuthVarTypePriv
) {
2288 // Process common authenticated variable except PK/KEK/DB/DBX.
2289 // Get signer's certificates from SignedData.
2291 VerifyStatus
= Pkcs7GetSigners (
2299 if (!VerifyStatus
) {
2304 // Get previously stored signer's certificates from certdb for existing
2305 // variable. Check whether they are identical with signer's certificates
2306 // in SignedData. If not, return error immediately.
2308 if ((Variable
->CurrPtr
!= NULL
)) {
2309 VerifyStatus
= FALSE
;
2311 Status
= GetCertsFromDb (VariableName
, VendorGuid
, &CertsInCertDb
, &CertsSizeinDb
);
2312 if (EFI_ERROR (Status
)) {
2316 if ((CertStackSize
!= CertsSizeinDb
) ||
2317 (CompareMem (SignerCerts
, CertsInCertDb
, CertsSizeinDb
) != 0)) {
2322 VerifyStatus
= Pkcs7Verify (
2330 if (!VerifyStatus
) {
2335 // Delete signer's certificates when delete the common authenticated variable.
2337 if ((PayloadSize
== 0) && (Variable
->CurrPtr
!= NULL
) && ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0)) {
2338 Status
= DeleteCertsFromDb (VariableName
, VendorGuid
);
2339 if (EFI_ERROR (Status
)) {
2340 VerifyStatus
= FALSE
;
2343 } else if (Variable
->CurrPtr
== NULL
&& PayloadSize
!= 0) {
2345 // Insert signer's certificates when adding a new common authenticated variable.
2347 Status
= InsertCertsToDb (VariableName
, VendorGuid
, SignerCerts
, CertStackSize
);
2348 if (EFI_ERROR (Status
)) {
2349 VerifyStatus
= FALSE
;
2353 } else if (AuthVarType
== AuthVarTypePayload
) {
2354 CertList
= (EFI_SIGNATURE_LIST
*) PayloadPtr
;
2355 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
2356 RootCert
= Cert
->SignatureData
;
2357 RootCertSize
= CertList
->SignatureSize
- (sizeof (EFI_SIGNATURE_DATA
) - 1);
2359 // Verify Pkcs7 SignedData via Pkcs7Verify library.
2361 VerifyStatus
= Pkcs7Verify (
2370 return EFI_SECURITY_VIOLATION
;
2375 if (AuthVarType
== AuthVarTypePk
|| AuthVarType
== AuthVarTypePriv
) {
2376 Pkcs7FreeSigners (RootCert
);
2377 Pkcs7FreeSigners (SignerCerts
);
2380 if (!VerifyStatus
) {
2381 return EFI_SECURITY_VIOLATION
;
2384 Status
= CheckSignatureListFormat(VariableName
, VendorGuid
, PayloadPtr
, PayloadSize
);
2385 if (EFI_ERROR (Status
)) {
2389 if ((PayloadSize
== 0) && (VarDel
!= NULL
)) {
2394 // Final step: Update/Append Variable if it pass Pkcs7Verify
2396 return UpdateVariable (
2405 &CertData
->TimeStamp