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.
10 The whole SMM authentication variable design relies on the integrity of flash part and SMM.
11 which is assumed to be protected by platform. All variable code and metadata in flash/SMM Memory
12 may not be modified without authorization. If platform fails to protect these resources,
13 the authentication service provided in this driver will be broken, and the behavior is undefined.
15 ProcessVarWithPk(), ProcessVarWithKek() and ProcessVariable() are the function to do
16 variable authentication.
18 VerifyTimeBasedPayload() and VerifyCounterBasedPayload() are sub function to do verification.
19 They will do basic validation for authentication data structure, then call crypto library
20 to verify the signature.
22 Copyright (c) 2009 - 2015, Intel Corporation. All rights reserved.<BR>
23 This program and the accompanying materials
24 are licensed and made available under the terms and conditions of the BSD License
25 which accompanies this distribution. The full text of the license may be found at
26 http://opensource.org/licenses/bsd-license.php
28 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
29 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
34 #include "AuthService.h"
37 /// Global database array for scratch
44 UINT32 mMaxCertDbSize
;
46 UINT8 mVendorKeyState
;
48 EFI_GUID mSignatureSupport
[] = {EFI_CERT_SHA1_GUID
, EFI_CERT_SHA256_GUID
, EFI_CERT_RSA2048_GUID
, EFI_CERT_X509_GUID
};
50 // Public Exponent of RSA Key.
52 CONST UINT8 mRsaE
[] = { 0x01, 0x00, 0x01 };
54 // Hash context pointer
56 VOID
*mHashCtx
= NULL
;
59 // Requirement for different signature type which have been defined in UEFI spec.
60 // These data are used to peform SignatureList format check while setting PK/KEK variable.
62 EFI_SIGNATURE_ITEM mSupportSigItem
[] = {
63 //{SigType, SigHeaderSize, SigDataSize }
64 {EFI_CERT_SHA256_GUID
, 0, 32 },
65 {EFI_CERT_RSA2048_GUID
, 0, 256 },
66 {EFI_CERT_RSA2048_SHA256_GUID
, 0, 256 },
67 {EFI_CERT_SHA1_GUID
, 0, 20 },
68 {EFI_CERT_RSA2048_SHA1_GUID
, 0, 256 },
69 {EFI_CERT_X509_GUID
, 0, ((UINT32
) ~0)},
70 {EFI_CERT_SHA224_GUID
, 0, 28 },
71 {EFI_CERT_SHA384_GUID
, 0, 48 },
72 {EFI_CERT_SHA512_GUID
, 0, 64 },
73 {EFI_CERT_X509_SHA256_GUID
, 0, 48 },
74 {EFI_CERT_X509_SHA384_GUID
, 0, 64 },
75 {EFI_CERT_X509_SHA512_GUID
, 0, 80 }
79 Determine whether this operation needs a physical present user.
81 @param[in] VariableName Name of the Variable.
82 @param[in] VendorGuid GUID of the Variable.
84 @retval TRUE This variable is protected, only a physical present user could set this variable.
85 @retval FALSE This variable is not protected.
89 NeedPhysicallyPresent(
90 IN CHAR16
*VariableName
,
91 IN EFI_GUID
*VendorGuid
94 if ((CompareGuid (VendorGuid
, &gEfiSecureBootEnableDisableGuid
) && (StrCmp (VariableName
, EFI_SECURE_BOOT_ENABLE_NAME
) == 0))
95 || (CompareGuid (VendorGuid
, &gEfiCustomModeEnableGuid
) && (StrCmp (VariableName
, EFI_CUSTOM_MODE_NAME
) == 0))) {
103 Determine whether the platform is operating in Custom Secure Boot mode.
105 @retval TRUE The platform is operating in Custom mode.
106 @retval FALSE The platform is operating in Standard mode.
114 VARIABLE_POINTER_TRACK Variable
;
116 FindVariable (EFI_CUSTOM_MODE_NAME
, &gEfiCustomModeEnableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
117 if (Variable
.CurrPtr
!= NULL
&& *(GetVariableDataPtr (Variable
.CurrPtr
)) == CUSTOM_SECURE_BOOT_MODE
) {
125 Initializes for authenticated varibale service.
127 @retval EFI_SUCCESS Function successfully executed.
128 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resources.
132 AutenticatedVariableServiceInitialize (
137 VARIABLE_POINTER_TRACK Variable
;
138 VARIABLE_POINTER_TRACK PkVariable
;
144 UINT8 SecureBootMode
;
145 UINT8 SecureBootEnable
;
150 // Initialize hash context.
152 CtxSize
= Sha256GetContextSize ();
153 mHashCtx
= AllocateRuntimePool (CtxSize
);
154 if (mHashCtx
== NULL
) {
155 return EFI_OUT_OF_RESOURCES
;
159 // Reserve runtime buffer for public key database. The size excludes variable header and name size.
161 mMaxKeyDbSize
= PcdGet32 (PcdMaxVariableSize
) - sizeof (VARIABLE_HEADER
) - sizeof (AUTHVAR_KEYDB_NAME
);
162 mMaxKeyNumber
= mMaxKeyDbSize
/ EFI_CERT_TYPE_RSA2048_SIZE
;
163 mPubKeyStore
= AllocateRuntimePool (mMaxKeyDbSize
);
164 if (mPubKeyStore
== NULL
) {
165 return EFI_OUT_OF_RESOURCES
;
169 // Reserve runtime buffer for certificate database. The size excludes variable header and name size.
171 mMaxCertDbSize
= PcdGet32 (PcdMaxVariableSize
) - sizeof (VARIABLE_HEADER
) - sizeof (EFI_CERT_DB_NAME
);
172 mCertDbStore
= AllocateRuntimePool (mMaxCertDbSize
);
173 if (mCertDbStore
== NULL
) {
174 return EFI_OUT_OF_RESOURCES
;
178 // Check "AuthVarKeyDatabase" variable's existence.
179 // If it doesn't exist, create a new one with initial value of 0 and EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS set.
181 Status
= FindVariable (
183 &gEfiAuthenticatedVariableGuid
,
185 &mVariableModuleGlobal
->VariableGlobal
,
189 if (Variable
.CurrPtr
== NULL
) {
190 VarAttr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
;
193 Status
= UpdateVariable (
195 &gEfiAuthenticatedVariableGuid
,
204 if (EFI_ERROR (Status
)) {
209 // Load database in global variable for cache.
211 DataSize
= DataSizeOfVariable (Variable
.CurrPtr
);
212 Data
= GetVariableDataPtr (Variable
.CurrPtr
);
213 ASSERT ((DataSize
!= 0) && (Data
!= NULL
));
215 // "AuthVarKeyDatabase" is an internal variable. Its DataSize is always ensured not to exceed mPubKeyStore buffer size(See definition before)
216 // Therefore, there is no memory overflow in underlying CopyMem.
218 CopyMem (mPubKeyStore
, (UINT8
*) Data
, DataSize
);
219 mPubKeyNumber
= (UINT32
) (DataSize
/ EFI_CERT_TYPE_RSA2048_SIZE
);
222 FindVariable (EFI_PLATFORM_KEY_NAME
, &gEfiGlobalVariableGuid
, &PkVariable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
223 if (PkVariable
.CurrPtr
== NULL
) {
224 DEBUG ((EFI_D_INFO
, "Variable %s does not exist.\n", EFI_PLATFORM_KEY_NAME
));
226 DEBUG ((EFI_D_INFO
, "Variable %s exists.\n", EFI_PLATFORM_KEY_NAME
));
230 // Create "SetupMode" variable with BS+RT attribute set.
232 FindVariable (EFI_SETUP_MODE_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
233 if (PkVariable
.CurrPtr
== NULL
) {
234 mPlatformMode
= SETUP_MODE
;
236 mPlatformMode
= USER_MODE
;
238 Status
= UpdateVariable (
240 &gEfiGlobalVariableGuid
,
243 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
249 if (EFI_ERROR (Status
)) {
254 // Create "SignatureSupport" variable with BS+RT attribute set.
256 FindVariable (EFI_SIGNATURE_SUPPORT_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
257 Status
= UpdateVariable (
258 EFI_SIGNATURE_SUPPORT_NAME
,
259 &gEfiGlobalVariableGuid
,
261 sizeof(mSignatureSupport
),
262 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
268 if (EFI_ERROR (Status
)) {
273 // If "SecureBootEnable" variable exists, then update "SecureBoot" variable.
274 // If "SecureBootEnable" variable is SECURE_BOOT_ENABLE and in USER_MODE, Set "SecureBoot" variable to SECURE_BOOT_MODE_ENABLE.
275 // If "SecureBootEnable" variable is SECURE_BOOT_DISABLE, Set "SecureBoot" variable to SECURE_BOOT_MODE_DISABLE.
277 SecureBootEnable
= SECURE_BOOT_DISABLE
;
278 FindVariable (EFI_SECURE_BOOT_ENABLE_NAME
, &gEfiSecureBootEnableDisableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
279 if (Variable
.CurrPtr
!= NULL
) {
280 SecureBootEnable
= *(GetVariableDataPtr (Variable
.CurrPtr
));
281 } else if (mPlatformMode
== USER_MODE
) {
283 // "SecureBootEnable" not exist, initialize it in USER_MODE.
285 SecureBootEnable
= SECURE_BOOT_ENABLE
;
286 Status
= UpdateVariable (
287 EFI_SECURE_BOOT_ENABLE_NAME
,
288 &gEfiSecureBootEnableDisableGuid
,
291 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
297 if (EFI_ERROR (Status
)) {
303 // Create "SecureBoot" variable with BS+RT attribute set.
305 if (SecureBootEnable
== SECURE_BOOT_ENABLE
&& mPlatformMode
== USER_MODE
) {
306 SecureBootMode
= SECURE_BOOT_MODE_ENABLE
;
308 SecureBootMode
= SECURE_BOOT_MODE_DISABLE
;
310 FindVariable (EFI_SECURE_BOOT_MODE_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
311 Status
= UpdateVariable (
312 EFI_SECURE_BOOT_MODE_NAME
,
313 &gEfiGlobalVariableGuid
,
316 EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
322 if (EFI_ERROR (Status
)) {
326 DEBUG ((EFI_D_INFO
, "Variable %s is %x\n", EFI_SETUP_MODE_NAME
, mPlatformMode
));
327 DEBUG ((EFI_D_INFO
, "Variable %s is %x\n", EFI_SECURE_BOOT_MODE_NAME
, SecureBootMode
));
328 DEBUG ((EFI_D_INFO
, "Variable %s is %x\n", EFI_SECURE_BOOT_ENABLE_NAME
, SecureBootEnable
));
331 // Initialize "CustomMode" in STANDARD_SECURE_BOOT_MODE state.
333 FindVariable (EFI_CUSTOM_MODE_NAME
, &gEfiCustomModeEnableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
334 CustomMode
= STANDARD_SECURE_BOOT_MODE
;
335 Status
= UpdateVariable (
336 EFI_CUSTOM_MODE_NAME
,
337 &gEfiCustomModeEnableGuid
,
340 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
346 if (EFI_ERROR (Status
)) {
350 DEBUG ((EFI_D_INFO
, "Variable %s is %x\n", EFI_CUSTOM_MODE_NAME
, CustomMode
));
353 // Check "certdb" variable's existence.
354 // If it doesn't exist, then create a new one with
355 // EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set.
357 Status
= FindVariable (
361 &mVariableModuleGlobal
->VariableGlobal
,
365 if (Variable
.CurrPtr
== NULL
) {
366 VarAttr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
367 ListSize
= sizeof (UINT32
);
368 Status
= UpdateVariable (
379 if (EFI_ERROR (Status
)) {
385 // Check "VendorKeysNv" variable's existence and create "VendorKeys" variable accordingly.
387 FindVariable (EFI_VENDOR_KEYS_NV_VARIABLE_NAME
, &gEfiVendorKeysNvGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
388 if (Variable
.CurrPtr
!= NULL
) {
389 mVendorKeyState
= *(GetVariableDataPtr (Variable
.CurrPtr
));
392 // "VendorKeysNv" not exist, initialize it in VENDOR_KEYS_VALID state.
394 mVendorKeyState
= VENDOR_KEYS_VALID
;
395 Status
= UpdateVariable (
396 EFI_VENDOR_KEYS_NV_VARIABLE_NAME
,
397 &gEfiVendorKeysNvGuid
,
400 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
,
406 if (EFI_ERROR (Status
)) {
412 // Create "VendorKeys" variable with BS+RT attribute set.
414 FindVariable (EFI_VENDOR_KEYS_VARIABLE_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
415 Status
= UpdateVariable (
416 EFI_VENDOR_KEYS_VARIABLE_NAME
,
417 &gEfiGlobalVariableGuid
,
420 EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
426 if (EFI_ERROR (Status
)) {
430 DEBUG ((EFI_D_INFO
, "Variable %s is %x\n", EFI_VENDOR_KEYS_VARIABLE_NAME
, mVendorKeyState
));
436 Add public key in store and return its index.
438 @param[in] PubKey Input pointer to Public Key data
439 @param[in] VariableDataEntry The variable data entry
441 @return Index of new added item
447 IN VARIABLE_ENTRY_CONSISTENCY
*VariableDataEntry
453 VARIABLE_POINTER_TRACK Variable
;
457 VARIABLE_ENTRY_CONSISTENCY PublicKeyEntry
;
460 if (PubKey
== NULL
) {
464 Status
= FindVariable (
466 &gEfiAuthenticatedVariableGuid
,
468 &mVariableModuleGlobal
->VariableGlobal
,
471 if (EFI_ERROR (Status
)) {
472 DEBUG ((EFI_D_ERROR
, "Get public key database variable failure, Status = %r\n", Status
));
477 // Check whether the public key entry does exist.
480 for (Ptr
= mPubKeyStore
, Index
= 1; Index
<= mPubKeyNumber
; Index
++) {
481 if (CompareMem (Ptr
, PubKey
, EFI_CERT_TYPE_RSA2048_SIZE
) == 0) {
485 Ptr
+= EFI_CERT_TYPE_RSA2048_SIZE
;
490 // Add public key in database.
492 if (mPubKeyNumber
== mMaxKeyNumber
) {
494 // Public key dadatase is full, try to reclaim invalid key.
498 // NV storage can't reclaim at runtime.
504 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
505 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
512 if (EFI_ERROR (Status
)) {
516 Status
= FindVariable (
518 &gEfiAuthenticatedVariableGuid
,
520 &mVariableModuleGlobal
->VariableGlobal
,
523 if (EFI_ERROR (Status
)) {
524 DEBUG ((EFI_D_ERROR
, "Get public key database variable failure, Status = %r\n", Status
));
528 DataSize
= DataSizeOfVariable (Variable
.CurrPtr
);
529 Data
= GetVariableDataPtr (Variable
.CurrPtr
);
530 ASSERT ((DataSize
!= 0) && (Data
!= NULL
));
532 // "AuthVarKeyDatabase" is an internal used variable. Its DataSize is always ensured not to exceed mPubKeyStore buffer size(See definition before)
533 // Therefore, there is no memory overflow in underlying CopyMem.
535 CopyMem (mPubKeyStore
, (UINT8
*) Data
, DataSize
);
536 mPubKeyNumber
= (UINT32
) (DataSize
/ EFI_CERT_TYPE_RSA2048_SIZE
);
538 if (mPubKeyNumber
== mMaxKeyNumber
) {
544 // Check the variable space for both public key and variable data.
546 PublicKeyEntry
.VariableSize
= (mPubKeyNumber
+ 1) * EFI_CERT_TYPE_RSA2048_SIZE
;
547 PublicKeyEntry
.Guid
= &gEfiAuthenticatedVariableGuid
;
548 PublicKeyEntry
.Name
= AUTHVAR_KEYDB_NAME
;
549 Attributes
= VARIABLE_ATTRIBUTE_NV_BS_RT
| EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
;
551 if (!CheckRemainingSpaceForConsistency (Attributes
, &PublicKeyEntry
, VariableDataEntry
, NULL
)) {
553 // No enough variable space.
558 CopyMem (mPubKeyStore
+ mPubKeyNumber
* EFI_CERT_TYPE_RSA2048_SIZE
, PubKey
, EFI_CERT_TYPE_RSA2048_SIZE
);
559 Index
= ++mPubKeyNumber
;
561 // Update public key database variable.
563 Status
= UpdateVariable (
565 &gEfiAuthenticatedVariableGuid
,
567 mPubKeyNumber
* EFI_CERT_TYPE_RSA2048_SIZE
,
574 if (EFI_ERROR (Status
)) {
575 DEBUG ((EFI_D_ERROR
, "Update public key database variable failure, Status = %r\n", Status
));
584 Verify data payload with AuthInfo in EFI_CERT_TYPE_RSA2048_SHA256_GUID type.
585 Follow the steps in UEFI2.2.
587 Caution: This function may receive untrusted input.
588 This function may be invoked in SMM mode, and datasize and data are external input.
589 This function will do basic validation, before parse the data.
590 This function will parse the authentication carefully to avoid security issues, like
591 buffer overflow, integer overflow.
593 @param[in] Data Pointer to data with AuthInfo.
594 @param[in] DataSize Size of Data.
595 @param[in] PubKey Public key used for verification.
597 @retval EFI_INVALID_PARAMETER Invalid parameter.
598 @retval EFI_SECURITY_VIOLATION If authentication failed.
599 @retval EFI_SUCCESS Authentication successful.
603 VerifyCounterBasedPayload (
610 EFI_VARIABLE_AUTHENTICATION
*CertData
;
611 EFI_CERT_BLOCK_RSA_2048_SHA256
*CertBlock
;
612 UINT8 Digest
[SHA256_DIGEST_SIZE
];
616 PayloadSize
= DataSize
- AUTHINFO_SIZE
;
621 if (Data
== NULL
|| PubKey
== NULL
) {
622 return EFI_INVALID_PARAMETER
;
625 CertData
= (EFI_VARIABLE_AUTHENTICATION
*) Data
;
626 CertBlock
= (EFI_CERT_BLOCK_RSA_2048_SHA256
*) (CertData
->AuthInfo
.CertData
);
629 // wCertificateType should be WIN_CERT_TYPE_EFI_GUID.
630 // Cert type should be EFI_CERT_TYPE_RSA2048_SHA256_GUID.
632 if ((CertData
->AuthInfo
.Hdr
.wCertificateType
!= WIN_CERT_TYPE_EFI_GUID
) ||
633 !CompareGuid (&CertData
->AuthInfo
.CertType
, &gEfiCertTypeRsa2048Sha256Guid
)
636 // Invalid AuthInfo type, return EFI_SECURITY_VIOLATION.
638 return EFI_SECURITY_VIOLATION
;
641 // Hash data payload with SHA256.
643 ZeroMem (Digest
, SHA256_DIGEST_SIZE
);
644 Status
= Sha256Init (mHashCtx
);
648 Status
= Sha256Update (mHashCtx
, Data
+ AUTHINFO_SIZE
, PayloadSize
);
655 Status
= Sha256Update (mHashCtx
, &PayloadSize
, sizeof (UINTN
));
660 // Hash Monotonic Count.
662 Status
= Sha256Update (mHashCtx
, &CertData
->MonotonicCount
, sizeof (UINT64
));
666 Status
= Sha256Final (mHashCtx
, Digest
);
671 // Generate & Initialize RSA Context.
674 ASSERT (Rsa
!= NULL
);
676 // Set RSA Key Components.
677 // NOTE: Only N and E are needed to be set as RSA public key for signature verification.
679 Status
= RsaSetKey (Rsa
, RsaKeyN
, PubKey
, EFI_CERT_TYPE_RSA2048_SIZE
);
683 Status
= RsaSetKey (Rsa
, RsaKeyE
, mRsaE
, sizeof (mRsaE
));
688 // Verify the signature.
690 Status
= RsaPkcs1Verify (
694 CertBlock
->Signature
,
695 EFI_CERT_TYPE_RSA2048_SHA256_SIZE
705 return EFI_SECURITY_VIOLATION
;
710 Update platform mode.
712 @param[in] Mode SETUP_MODE or USER_MODE.
714 @return EFI_INVALID_PARAMETER Invalid parameter.
715 @return EFI_SUCCESS Update platform mode successfully.
724 VARIABLE_POINTER_TRACK Variable
;
725 UINT8 SecureBootMode
;
726 UINT8 SecureBootEnable
;
727 UINTN VariableDataSize
;
729 Status
= FindVariable (
731 &gEfiGlobalVariableGuid
,
733 &mVariableModuleGlobal
->VariableGlobal
,
736 if (EFI_ERROR (Status
)) {
741 // Update the value of SetupMode variable by a simple mem copy, this could avoid possible
742 // variable storage reclaim at runtime.
744 mPlatformMode
= (UINT8
) Mode
;
745 CopyMem (GetVariableDataPtr (Variable
.CurrPtr
), &mPlatformMode
, sizeof(UINT8
));
749 // SecureBoot Variable indicates whether the platform firmware is operating
750 // in Secure boot mode (1) or not (0), so we should not change SecureBoot
751 // Variable in runtime.
757 // Check "SecureBoot" variable's existence.
758 // If it doesn't exist, firmware has no capability to perform driver signing verification,
759 // then set "SecureBoot" to 0.
761 Status
= FindVariable (
762 EFI_SECURE_BOOT_MODE_NAME
,
763 &gEfiGlobalVariableGuid
,
765 &mVariableModuleGlobal
->VariableGlobal
,
769 // If "SecureBoot" variable exists, then check "SetupMode" variable update.
770 // If "SetupMode" variable is USER_MODE, "SecureBoot" variable is set to 1.
771 // If "SetupMode" variable is SETUP_MODE, "SecureBoot" variable is set to 0.
773 if (Variable
.CurrPtr
== NULL
) {
774 SecureBootMode
= SECURE_BOOT_MODE_DISABLE
;
776 if (mPlatformMode
== USER_MODE
) {
777 SecureBootMode
= SECURE_BOOT_MODE_ENABLE
;
778 } else if (mPlatformMode
== SETUP_MODE
) {
779 SecureBootMode
= SECURE_BOOT_MODE_DISABLE
;
781 return EFI_NOT_FOUND
;
785 Status
= UpdateVariable (
786 EFI_SECURE_BOOT_MODE_NAME
,
787 &gEfiGlobalVariableGuid
,
790 EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
796 if (EFI_ERROR (Status
)) {
801 // Check "SecureBootEnable" variable's existence. It can enable/disable secure boot feature.
803 Status
= FindVariable (
804 EFI_SECURE_BOOT_ENABLE_NAME
,
805 &gEfiSecureBootEnableDisableGuid
,
807 &mVariableModuleGlobal
->VariableGlobal
,
811 if (SecureBootMode
== SECURE_BOOT_MODE_ENABLE
) {
813 // Create the "SecureBootEnable" variable as secure boot is enabled.
815 SecureBootEnable
= SECURE_BOOT_ENABLE
;
816 VariableDataSize
= sizeof (SecureBootEnable
);
819 // Delete the "SecureBootEnable" variable if this variable exist as "SecureBoot"
820 // variable is not in secure boot state.
822 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
825 SecureBootEnable
= SECURE_BOOT_DISABLE
;
826 VariableDataSize
= 0;
829 Status
= UpdateVariable (
830 EFI_SECURE_BOOT_ENABLE_NAME
,
831 &gEfiSecureBootEnableDisableGuid
,
834 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
844 Check input data form to make sure it is a valid EFI_SIGNATURE_LIST for PK/KEK/db/dbx/dbt variable.
846 @param[in] VariableName Name of Variable to be check.
847 @param[in] VendorGuid Variable vendor GUID.
848 @param[in] Data Point to the variable data to be checked.
849 @param[in] DataSize Size of Data.
851 @return EFI_INVALID_PARAMETER Invalid signature list format.
852 @return EFI_SUCCESS Passed signature list format check successfully.
856 CheckSignatureListFormat(
857 IN CHAR16
*VariableName
,
858 IN EFI_GUID
*VendorGuid
,
863 EFI_SIGNATURE_LIST
*SigList
;
869 EFI_SIGNATURE_DATA
*CertData
;
876 ASSERT (VariableName
!= NULL
&& VendorGuid
!= NULL
&& Data
!= NULL
);
878 if (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, EFI_PLATFORM_KEY_NAME
) == 0)){
880 } else if ((CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, EFI_KEY_EXCHANGE_KEY_NAME
) == 0)) ||
881 (CompareGuid (VendorGuid
, &gEfiImageSecurityDatabaseGuid
) &&
882 ((StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE
) == 0) || (StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE1
) == 0) ||
883 (StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE2
) == 0)))) {
890 SigList
= (EFI_SIGNATURE_LIST
*) Data
;
891 SigDataSize
= DataSize
;
895 // Walk throuth the input signature list and check the data format.
896 // If any signature is incorrectly formed, the whole check will fail.
898 while ((SigDataSize
> 0) && (SigDataSize
>= SigList
->SignatureListSize
)) {
899 for (Index
= 0; Index
< (sizeof (mSupportSigItem
) / sizeof (EFI_SIGNATURE_ITEM
)); Index
++ ) {
900 if (CompareGuid (&SigList
->SignatureType
, &mSupportSigItem
[Index
].SigType
)) {
902 // The value of SignatureSize should always be 16 (size of SignatureOwner
903 // component) add the data length according to signature type.
905 if (mSupportSigItem
[Index
].SigDataSize
!= ((UINT32
) ~0) &&
906 (SigList
->SignatureSize
- sizeof (EFI_GUID
)) != mSupportSigItem
[Index
].SigDataSize
) {
907 return EFI_INVALID_PARAMETER
;
909 if (mSupportSigItem
[Index
].SigHeaderSize
!= ((UINTN
) ~0) &&
910 SigList
->SignatureHeaderSize
!= mSupportSigItem
[Index
].SigHeaderSize
) {
911 return EFI_INVALID_PARAMETER
;
917 if (Index
== (sizeof (mSupportSigItem
) / sizeof (EFI_SIGNATURE_ITEM
))) {
919 // Undefined signature type.
921 return EFI_INVALID_PARAMETER
;
924 if (CompareGuid (&SigList
->SignatureType
, &gEfiCertX509Guid
)) {
926 // Try to retrieve the RSA public key from the X.509 certificate.
927 // If this operation fails, it's not a valid certificate.
929 RsaContext
= RsaNew ();
930 if (RsaContext
== NULL
) {
931 return EFI_INVALID_PARAMETER
;
933 CertData
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) SigList
+ sizeof (EFI_SIGNATURE_LIST
) + SigList
->SignatureHeaderSize
);
934 CertLen
= SigList
->SignatureSize
- sizeof (EFI_GUID
);
935 if (!RsaGetPublicKeyFromX509 (CertData
->SignatureData
, CertLen
, &RsaContext
)) {
936 RsaFree (RsaContext
);
937 return EFI_INVALID_PARAMETER
;
939 RsaFree (RsaContext
);
942 if ((SigList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - SigList
->SignatureHeaderSize
) % SigList
->SignatureSize
!= 0) {
943 return EFI_INVALID_PARAMETER
;
945 SigCount
+= (SigList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - SigList
->SignatureHeaderSize
) / SigList
->SignatureSize
;
947 SigDataSize
-= SigList
->SignatureListSize
;
948 SigList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) SigList
+ SigList
->SignatureListSize
);
951 if (((UINTN
) SigList
- (UINTN
) Data
) != DataSize
) {
952 return EFI_INVALID_PARAMETER
;
955 if (IsPk
&& SigCount
> 1) {
956 return EFI_INVALID_PARAMETER
;
963 Update "VendorKeys" variable to record the out of band secure boot key modification.
965 @return EFI_SUCCESS Variable is updated successfully.
966 @return Others Failed to update variable.
970 VendorKeyIsModified (
975 VARIABLE_POINTER_TRACK Variable
;
977 if (mVendorKeyState
== VENDOR_KEYS_MODIFIED
) {
980 mVendorKeyState
= VENDOR_KEYS_MODIFIED
;
982 FindVariable (EFI_VENDOR_KEYS_NV_VARIABLE_NAME
, &gEfiVendorKeysNvGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
983 Status
= UpdateVariable (
984 EFI_VENDOR_KEYS_NV_VARIABLE_NAME
,
985 &gEfiVendorKeysNvGuid
,
988 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
,
994 if (EFI_ERROR (Status
)) {
998 FindVariable (EFI_VENDOR_KEYS_VARIABLE_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
999 return UpdateVariable (
1000 EFI_VENDOR_KEYS_VARIABLE_NAME
,
1001 &gEfiGlobalVariableGuid
,
1004 EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
1013 Process variable with platform key for verification.
1015 Caution: This function may receive untrusted input.
1016 This function may be invoked in SMM mode, and datasize and data are external input.
1017 This function will do basic validation, before parse the data.
1018 This function will parse the authentication carefully to avoid security issues, like
1019 buffer overflow, integer overflow.
1020 This function will check attribute carefully to avoid authentication bypass.
1022 @param[in] VariableName Name of Variable to be found.
1023 @param[in] VendorGuid Variable vendor GUID.
1024 @param[in] Data Data pointer.
1025 @param[in] DataSize Size of Data found. If size is less than the
1026 data, this value contains the required size.
1027 @param[in] Variable The variable information which is used to keep track of variable usage.
1028 @param[in] Attributes Attribute value of the variable
1029 @param[in] IsPk Indicate whether it is to process pk.
1031 @return EFI_INVALID_PARAMETER Invalid parameter.
1032 @return EFI_SECURITY_VIOLATION The variable does NOT pass the validation.
1033 check carried out by the firmware.
1034 @return EFI_SUCCESS Variable passed validation successfully.
1039 IN CHAR16
*VariableName
,
1040 IN EFI_GUID
*VendorGuid
,
1043 IN VARIABLE_POINTER_TRACK
*Variable
,
1044 IN UINT32 Attributes OPTIONAL
,
1053 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0 ||
1054 (Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) == 0) {
1056 // PK, KEK and db/dbx/dbt should set EFI_VARIABLE_NON_VOLATILE attribute and should be a time-based
1057 // authenticated variable.
1059 return EFI_INVALID_PARAMETER
;
1063 if ((InCustomMode() && UserPhysicalPresent()) || (mPlatformMode
== SETUP_MODE
&& !IsPk
)) {
1064 Payload
= (UINT8
*) Data
+ AUTHINFO2_SIZE (Data
);
1065 PayloadSize
= DataSize
- AUTHINFO2_SIZE (Data
);
1066 if (PayloadSize
== 0) {
1070 Status
= CheckSignatureListFormat(VariableName
, VendorGuid
, Payload
, PayloadSize
);
1071 if (EFI_ERROR (Status
)) {
1075 Status
= UpdateVariable (
1084 &((EFI_VARIABLE_AUTHENTICATION_2
*) Data
)->TimeStamp
1086 if (EFI_ERROR(Status
)) {
1090 if ((mPlatformMode
!= SETUP_MODE
) || IsPk
) {
1091 Status
= VendorKeyIsModified ();
1093 } else if (mPlatformMode
== USER_MODE
) {
1095 // Verify against X509 Cert in PK database.
1097 Status
= VerifyTimeBasedPayload (
1109 // Verify against the certificate in data payload.
1111 Status
= VerifyTimeBasedPayload (
1123 if (!EFI_ERROR(Status
) && IsPk
) {
1124 if (mPlatformMode
== SETUP_MODE
&& !Del
) {
1126 // If enroll PK in setup mode, need change to user mode.
1128 Status
= UpdatePlatformMode (USER_MODE
);
1129 } else if (mPlatformMode
== USER_MODE
&& Del
){
1131 // If delete PK in user mode, need change to setup mode.
1133 Status
= UpdatePlatformMode (SETUP_MODE
);
1141 Process variable with key exchange key for verification.
1143 Caution: This function may receive untrusted input.
1144 This function may be invoked in SMM mode, and datasize and data are external input.
1145 This function will do basic validation, before parse the data.
1146 This function will parse the authentication carefully to avoid security issues, like
1147 buffer overflow, integer overflow.
1148 This function will check attribute carefully to avoid authentication bypass.
1150 @param[in] VariableName Name of Variable to be found.
1151 @param[in] VendorGuid Variable vendor GUID.
1152 @param[in] Data Data pointer.
1153 @param[in] DataSize Size of Data found. If size is less than the
1154 data, this value contains the required size.
1155 @param[in] Variable The variable information which is used to keep track of variable usage.
1156 @param[in] Attributes Attribute value of the variable.
1158 @return EFI_INVALID_PARAMETER Invalid parameter.
1159 @return EFI_SECURITY_VIOLATION The variable does NOT pass the validation
1160 check carried out by the firmware.
1161 @return EFI_SUCCESS Variable pass validation successfully.
1166 IN CHAR16
*VariableName
,
1167 IN EFI_GUID
*VendorGuid
,
1170 IN VARIABLE_POINTER_TRACK
*Variable
,
1171 IN UINT32 Attributes OPTIONAL
1178 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0 ||
1179 (Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) == 0) {
1181 // DB, DBX and DBT should set EFI_VARIABLE_NON_VOLATILE attribute and should be a time-based
1182 // authenticated variable.
1184 return EFI_INVALID_PARAMETER
;
1187 Status
= EFI_SUCCESS
;
1188 if (mPlatformMode
== USER_MODE
&& !(InCustomMode() && UserPhysicalPresent())) {
1190 // Time-based, verify against X509 Cert KEK.
1192 return VerifyTimeBasedPayload (
1204 // If in setup mode or custom secure boot mode, no authentication needed.
1206 Payload
= (UINT8
*) Data
+ AUTHINFO2_SIZE (Data
);
1207 PayloadSize
= DataSize
- AUTHINFO2_SIZE (Data
);
1209 Status
= CheckSignatureListFormat(VariableName
, VendorGuid
, Payload
, PayloadSize
);
1210 if (EFI_ERROR (Status
)) {
1214 Status
= UpdateVariable (
1223 &((EFI_VARIABLE_AUTHENTICATION_2
*) Data
)->TimeStamp
1225 if (EFI_ERROR (Status
)) {
1229 if (mPlatformMode
!= SETUP_MODE
) {
1230 Status
= VendorKeyIsModified ();
1238 Check if it is to delete auth variable.
1240 @param[in] Data Data pointer.
1241 @param[in] DataSize Size of Data.
1242 @param[in] Variable The variable information which is used to keep track of variable usage.
1243 @param[in] Attributes Attribute value of the variable.
1245 @retval TRUE It is to delete auth variable.
1246 @retval FALSE It is not to delete auth variable.
1250 IsDeleteAuthVariable (
1253 IN VARIABLE_POINTER_TRACK
*Variable
,
1254 IN UINT32 Attributes
1264 // To delete a variable created with the EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
1265 // or the EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS attribute,
1266 // SetVariable must be used with attributes matching the existing variable
1267 // and the DataSize set to the size of the AuthInfo descriptor.
1269 if ((Variable
->CurrPtr
!= NULL
) &&
1270 (Attributes
== Variable
->CurrPtr
->Attributes
) &&
1271 ((Attributes
& (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
)) != 0)) {
1272 if ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) {
1273 Payload
= (UINT8
*) Data
+ AUTHINFO2_SIZE (Data
);
1274 PayloadSize
= DataSize
- AUTHINFO2_SIZE (Data
);
1275 if (PayloadSize
== 0) {
1279 Payload
= (UINT8
*) Data
+ AUTHINFO_SIZE
;
1280 PayloadSize
= DataSize
- AUTHINFO_SIZE
;
1281 if (PayloadSize
== 0) {
1291 Process variable with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS/EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set
1293 Caution: This function may receive untrusted input.
1294 This function may be invoked in SMM mode, and datasize and data are external input.
1295 This function will do basic validation, before parse the data.
1296 This function will parse the authentication carefully to avoid security issues, like
1297 buffer overflow, integer overflow.
1298 This function will check attribute carefully to avoid authentication bypass.
1300 @param[in] VariableName Name of Variable to be found.
1301 @param[in] VendorGuid Variable vendor GUID.
1303 @param[in] Data Data pointer.
1304 @param[in] DataSize Size of Data.
1305 @param[in] Variable The variable information which is used to keep track of variable usage.
1306 @param[in] Attributes Attribute value of the variable.
1308 @return EFI_INVALID_PARAMETER Invalid parameter.
1309 @return EFI_WRITE_PROTECTED Variable is write-protected and needs authentication with
1310 EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS set.
1311 @return EFI_OUT_OF_RESOURCES The Database to save the public key is full.
1312 @return EFI_SECURITY_VIOLATION The variable is with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
1313 set, but the AuthInfo does NOT pass the validation
1314 check carried out by the firmware.
1315 @return EFI_SUCCESS Variable is not write-protected or pass validation successfully.
1320 IN CHAR16
*VariableName
,
1321 IN EFI_GUID
*VendorGuid
,
1324 IN VARIABLE_POINTER_TRACK
*Variable
,
1325 IN UINT32 Attributes
1330 BOOLEAN IsFirstTime
;
1332 EFI_VARIABLE_AUTHENTICATION
*CertData
;
1333 EFI_CERT_BLOCK_RSA_2048_SHA256
*CertBlock
;
1335 UINT64 MonotonicCount
;
1336 VARIABLE_ENTRY_CONSISTENCY VariableDataEntry
;
1343 Status
= EFI_SUCCESS
;
1345 if (IsDeleteAuthVariable (Data
, DataSize
, Variable
, Attributes
) && UserPhysicalPresent()) {
1347 // Allow the delete operation of common authenticated variable at user physical presence.
1349 if ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) {
1350 Status
= DeleteCertsFromDb (VariableName
, VendorGuid
);
1352 if (!EFI_ERROR (Status
)) {
1353 Status
= UpdateVariable (
1368 if (NeedPhysicallyPresent (VariableName
, VendorGuid
) && !UserPhysicalPresent()) {
1370 // This variable is protected, only physical present user could modify its value.
1372 return EFI_SECURITY_VIOLATION
;
1376 // A time-based authenticated variable and a count-based authenticated variable
1377 // can't be updated by each other.
1379 if (Variable
->CurrPtr
!= NULL
) {
1380 if (((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) &&
1381 ((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0)) {
1382 return EFI_SECURITY_VIOLATION
;
1385 if (((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) &&
1386 ((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0)) {
1387 return EFI_SECURITY_VIOLATION
;
1392 // Process Time-based Authenticated variable.
1394 if ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) {
1395 return VerifyTimeBasedPayload (
1408 // Determine if first time SetVariable with the EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS.
1410 if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) {
1412 // Determine current operation type.
1414 if (DataSize
== AUTHINFO_SIZE
) {
1418 // Determine whether this is the first time with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS set.
1420 if (Variable
->CurrPtr
== NULL
) {
1422 } else if ((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) == 0) {
1425 KeyIndex
= Variable
->CurrPtr
->PubKeyIndex
;
1426 IsFirstTime
= FALSE
;
1428 } else if ((Variable
->CurrPtr
!= NULL
) &&
1429 ((Variable
->CurrPtr
->Attributes
& (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
)) != 0)
1432 // If the variable is already write-protected, it always needs authentication before update.
1434 return EFI_WRITE_PROTECTED
;
1437 // If without EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS, set and attributes collision.
1438 // That means it is not authenticated variable, just update variable as usual.
1440 Status
= UpdateVariable (VariableName
, VendorGuid
, Data
, DataSize
, Attributes
, 0, 0, Variable
, NULL
);
1445 // Get PubKey and check Monotonic Count value corresponding to the variable.
1447 CertData
= (EFI_VARIABLE_AUTHENTICATION
*) Data
;
1448 CertBlock
= (EFI_CERT_BLOCK_RSA_2048_SHA256
*) (CertData
->AuthInfo
.CertData
);
1449 PubKey
= CertBlock
->PublicKey
;
1452 // Update Monotonic Count value.
1454 MonotonicCount
= CertData
->MonotonicCount
;
1458 // 2 cases need to check here
1459 // 1. Internal PubKey variable. PubKeyIndex is always 0
1460 // 2. Other counter-based AuthVariable. Check input PubKey.
1462 if (KeyIndex
== 0 || CompareMem (PubKey
, mPubKeyStore
+ (KeyIndex
- 1) * EFI_CERT_TYPE_RSA2048_SIZE
, EFI_CERT_TYPE_RSA2048_SIZE
) != 0) {
1463 return EFI_SECURITY_VIOLATION
;
1466 // Compare the current monotonic count and ensure that it is greater than the last SetVariable
1467 // operation with the EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS attribute set.
1469 if (CertData
->MonotonicCount
<= Variable
->CurrPtr
->MonotonicCount
) {
1471 // Monotonic count check fail, suspicious replay attack, return EFI_SECURITY_VIOLATION.
1473 return EFI_SECURITY_VIOLATION
;
1477 // Verify the certificate in Data payload.
1479 Status
= VerifyCounterBasedPayload (Data
, DataSize
, PubKey
);
1480 if (EFI_ERROR (Status
)) {
1485 // Now, the signature has been verified!
1487 if (IsFirstTime
&& !IsDeletion
) {
1488 VariableDataEntry
.VariableSize
= DataSize
- AUTHINFO_SIZE
;
1489 VariableDataEntry
.Guid
= VendorGuid
;
1490 VariableDataEntry
.Name
= VariableName
;
1493 // Update public key database variable if need.
1495 KeyIndex
= AddPubKeyInStore (PubKey
, &VariableDataEntry
);
1496 if (KeyIndex
== 0) {
1497 return EFI_OUT_OF_RESOURCES
;
1502 // Verification pass.
1504 return UpdateVariable (VariableName
, VendorGuid
, (UINT8
*)Data
+ AUTHINFO_SIZE
, DataSize
- AUTHINFO_SIZE
, Attributes
, KeyIndex
, MonotonicCount
, Variable
, NULL
);
1508 Merge two buffers which formatted as EFI_SIGNATURE_LIST. Only the new EFI_SIGNATURE_DATA
1509 will be appended to the original EFI_SIGNATURE_LIST, duplicate EFI_SIGNATURE_DATA
1512 @param[in, out] Data Pointer to original EFI_SIGNATURE_LIST.
1513 @param[in] DataSize Size of Data buffer.
1514 @param[in] FreeBufSize Size of free data buffer
1515 @param[in] NewData Pointer to new EFI_SIGNATURE_LIST to be appended.
1516 @param[in] NewDataSize Size of NewData buffer.
1517 @param[out] MergedBufSize Size of the merged buffer
1519 @return EFI_BUFFER_TOO_SMALL if input Data buffer overflowed
1523 AppendSignatureList (
1526 IN UINTN FreeBufSize
,
1528 IN UINTN NewDataSize
,
1529 OUT UINTN
*MergedBufSize
1532 EFI_SIGNATURE_LIST
*CertList
;
1533 EFI_SIGNATURE_DATA
*Cert
;
1535 EFI_SIGNATURE_LIST
*NewCertList
;
1536 EFI_SIGNATURE_DATA
*NewCert
;
1543 UINTN SignatureListSize
;
1546 Tail
= (UINT8
*) Data
+ DataSize
;
1548 NewCertList
= (EFI_SIGNATURE_LIST
*) NewData
;
1549 while ((NewDataSize
> 0) && (NewDataSize
>= NewCertList
->SignatureListSize
)) {
1550 NewCert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) NewCertList
+ sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
);
1551 NewCertCount
= (NewCertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - NewCertList
->SignatureHeaderSize
) / NewCertList
->SignatureSize
;
1554 for (Index
= 0; Index
< NewCertCount
; Index
++) {
1558 CertList
= (EFI_SIGNATURE_LIST
*) Data
;
1559 while ((Size
> 0) && (Size
>= CertList
->SignatureListSize
)) {
1560 if (CompareGuid (&CertList
->SignatureType
, &NewCertList
->SignatureType
) &&
1561 (CertList
->SignatureSize
== NewCertList
->SignatureSize
)) {
1562 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
1563 CertCount
= (CertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - CertList
->SignatureHeaderSize
) / CertList
->SignatureSize
;
1564 for (Index2
= 0; Index2
< CertCount
; Index2
++) {
1566 // Iterate each Signature Data in this Signature List.
1568 if (CompareMem (NewCert
, Cert
, CertList
->SignatureSize
) == 0) {
1572 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) Cert
+ CertList
->SignatureSize
);
1579 Size
-= CertList
->SignatureListSize
;
1580 CertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) CertList
+ CertList
->SignatureListSize
);
1585 // New EFI_SIGNATURE_DATA, append it.
1587 if (CopiedCount
== 0) {
1588 if (FreeBufSize
< sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
) {
1589 return EFI_BUFFER_TOO_SMALL
;
1593 // Copy EFI_SIGNATURE_LIST header for only once.
1596 CopyMem (Tail
, NewCertList
, sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
);
1597 Tail
= Tail
+ sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
;
1598 FreeBufSize
-= sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
;
1601 if (FreeBufSize
< NewCertList
->SignatureSize
) {
1602 return EFI_BUFFER_TOO_SMALL
;
1604 CopyMem (Tail
, NewCert
, NewCertList
->SignatureSize
);
1605 Tail
+= NewCertList
->SignatureSize
;
1606 FreeBufSize
-= NewCertList
->SignatureSize
;
1610 NewCert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) NewCert
+ NewCertList
->SignatureSize
);
1614 // Update SignatureListSize in newly appended EFI_SIGNATURE_LIST.
1616 if (CopiedCount
!= 0) {
1617 SignatureListSize
= sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
+ (CopiedCount
* NewCertList
->SignatureSize
);
1618 CertList
= (EFI_SIGNATURE_LIST
*) (Tail
- SignatureListSize
);
1619 CertList
->SignatureListSize
= (UINT32
) SignatureListSize
;
1622 NewDataSize
-= NewCertList
->SignatureListSize
;
1623 NewCertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) NewCertList
+ NewCertList
->SignatureListSize
);
1626 *MergedBufSize
= (Tail
- (UINT8
*) Data
);
1631 Compare two EFI_TIME data.
1634 @param FirstTime A pointer to the first EFI_TIME data.
1635 @param SecondTime A pointer to the second EFI_TIME data.
1637 @retval TRUE The FirstTime is not later than the SecondTime.
1638 @retval FALSE The FirstTime is later than the SecondTime.
1643 IN EFI_TIME
*FirstTime
,
1644 IN EFI_TIME
*SecondTime
1647 if (FirstTime
->Year
!= SecondTime
->Year
) {
1648 return (BOOLEAN
) (FirstTime
->Year
< SecondTime
->Year
);
1649 } else if (FirstTime
->Month
!= SecondTime
->Month
) {
1650 return (BOOLEAN
) (FirstTime
->Month
< SecondTime
->Month
);
1651 } else if (FirstTime
->Day
!= SecondTime
->Day
) {
1652 return (BOOLEAN
) (FirstTime
->Day
< SecondTime
->Day
);
1653 } else if (FirstTime
->Hour
!= SecondTime
->Hour
) {
1654 return (BOOLEAN
) (FirstTime
->Hour
< SecondTime
->Hour
);
1655 } else if (FirstTime
->Minute
!= SecondTime
->Minute
) {
1656 return (BOOLEAN
) (FirstTime
->Minute
< SecondTime
->Minute
);
1659 return (BOOLEAN
) (FirstTime
->Second
<= SecondTime
->Second
);
1663 Find matching signer's certificates for common authenticated variable
1664 by corresponding VariableName and VendorGuid from "certdb".
1666 The data format of "certdb":
1668 // UINT32 CertDbListSize;
1669 // /// AUTH_CERT_DB_DATA Certs1[];
1670 // /// AUTH_CERT_DB_DATA Certs2[];
1672 // /// AUTH_CERT_DB_DATA Certsn[];
1675 @param[in] VariableName Name of authenticated Variable.
1676 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1677 @param[in] Data Pointer to variable "certdb".
1678 @param[in] DataSize Size of variable "certdb".
1679 @param[out] CertOffset Offset of matching CertData, from starting of Data.
1680 @param[out] CertDataSize Length of CertData in bytes.
1681 @param[out] CertNodeOffset Offset of matching AUTH_CERT_DB_DATA , from
1683 @param[out] CertNodeSize Length of AUTH_CERT_DB_DATA in bytes.
1685 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1686 @retval EFI_NOT_FOUND Fail to find matching certs.
1687 @retval EFI_SUCCESS Find matching certs and output parameters.
1692 IN CHAR16
*VariableName
,
1693 IN EFI_GUID
*VendorGuid
,
1696 OUT UINT32
*CertOffset
, OPTIONAL
1697 OUT UINT32
*CertDataSize
, OPTIONAL
1698 OUT UINT32
*CertNodeOffset
,OPTIONAL
1699 OUT UINT32
*CertNodeSize OPTIONAL
1703 AUTH_CERT_DB_DATA
*Ptr
;
1707 UINT32 CertDbListSize
;
1709 if ((VariableName
== NULL
) || (VendorGuid
== NULL
) || (Data
== NULL
)) {
1710 return EFI_INVALID_PARAMETER
;
1714 // Check whether DataSize matches recorded CertDbListSize.
1716 if (DataSize
< sizeof (UINT32
)) {
1717 return EFI_INVALID_PARAMETER
;
1720 CertDbListSize
= ReadUnaligned32 ((UINT32
*) Data
);
1722 if (CertDbListSize
!= (UINT32
) DataSize
) {
1723 return EFI_INVALID_PARAMETER
;
1726 Offset
= sizeof (UINT32
);
1729 // Get corresponding certificates by VendorGuid and VariableName.
1731 while (Offset
< (UINT32
) DataSize
) {
1732 Ptr
= (AUTH_CERT_DB_DATA
*) (Data
+ Offset
);
1734 // Check whether VendorGuid matches.
1736 if (CompareGuid (&Ptr
->VendorGuid
, VendorGuid
)) {
1737 NodeSize
= ReadUnaligned32 (&Ptr
->CertNodeSize
);
1738 NameSize
= ReadUnaligned32 (&Ptr
->NameSize
);
1739 CertSize
= ReadUnaligned32 (&Ptr
->CertDataSize
);
1741 if (NodeSize
!= sizeof (EFI_GUID
) + sizeof (UINT32
) * 3 + CertSize
+
1742 sizeof (CHAR16
) * NameSize
) {
1743 return EFI_INVALID_PARAMETER
;
1746 Offset
= Offset
+ sizeof (EFI_GUID
) + sizeof (UINT32
) * 3;
1748 // Check whether VariableName matches.
1750 if ((NameSize
== StrLen (VariableName
)) &&
1751 (CompareMem (Data
+ Offset
, VariableName
, NameSize
* sizeof (CHAR16
)) == 0)) {
1752 Offset
= Offset
+ NameSize
* sizeof (CHAR16
);
1754 if (CertOffset
!= NULL
) {
1755 *CertOffset
= Offset
;
1758 if (CertDataSize
!= NULL
) {
1759 *CertDataSize
= CertSize
;
1762 if (CertNodeOffset
!= NULL
) {
1763 *CertNodeOffset
= (UINT32
) ((UINT8
*) Ptr
- Data
);
1766 if (CertNodeSize
!= NULL
) {
1767 *CertNodeSize
= NodeSize
;
1772 Offset
= Offset
+ NameSize
* sizeof (CHAR16
) + CertSize
;
1775 NodeSize
= ReadUnaligned32 (&Ptr
->CertNodeSize
);
1776 Offset
= Offset
+ NodeSize
;
1780 return EFI_NOT_FOUND
;
1784 Retrieve signer's certificates for common authenticated variable
1785 by corresponding VariableName and VendorGuid from "certdb".
1787 @param[in] VariableName Name of authenticated Variable.
1788 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1789 @param[out] CertData Pointer to signer's certificates.
1790 @param[out] CertDataSize Length of CertData in bytes.
1792 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1793 @retval EFI_NOT_FOUND Fail to find "certdb" or matching certs.
1794 @retval EFI_SUCCESS Get signer's certificates successfully.
1799 IN CHAR16
*VariableName
,
1800 IN EFI_GUID
*VendorGuid
,
1801 OUT UINT8
**CertData
,
1802 OUT UINT32
*CertDataSize
1805 VARIABLE_POINTER_TRACK CertDbVariable
;
1811 if ((VariableName
== NULL
) || (VendorGuid
== NULL
) || (CertData
== NULL
) || (CertDataSize
== NULL
)) {
1812 return EFI_INVALID_PARAMETER
;
1816 // Get variable "certdb".
1818 Status
= FindVariable (
1822 &mVariableModuleGlobal
->VariableGlobal
,
1825 if (EFI_ERROR (Status
)) {
1829 DataSize
= DataSizeOfVariable (CertDbVariable
.CurrPtr
);
1830 Data
= GetVariableDataPtr (CertDbVariable
.CurrPtr
);
1831 if ((DataSize
== 0) || (Data
== NULL
)) {
1833 return EFI_NOT_FOUND
;
1836 Status
= FindCertsFromDb (
1847 if (EFI_ERROR (Status
)) {
1851 *CertData
= Data
+ CertOffset
;
1856 Delete matching signer's certificates when deleting common authenticated
1857 variable by corresponding VariableName and VendorGuid from "certdb".
1859 @param[in] VariableName Name of authenticated Variable.
1860 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1862 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1863 @retval EFI_NOT_FOUND Fail to find "certdb" or matching certs.
1864 @retval EFI_OUT_OF_RESOURCES The operation is failed due to lack of resources.
1865 @retval EFI_SUCCESS The operation is completed successfully.
1870 IN CHAR16
*VariableName
,
1871 IN EFI_GUID
*VendorGuid
1874 VARIABLE_POINTER_TRACK CertDbVariable
;
1879 UINT32 CertNodeOffset
;
1880 UINT32 CertNodeSize
;
1882 UINT32 NewCertDbSize
;
1884 if ((VariableName
== NULL
) || (VendorGuid
== NULL
)) {
1885 return EFI_INVALID_PARAMETER
;
1889 // Get variable "certdb".
1891 Status
= FindVariable (
1895 &mVariableModuleGlobal
->VariableGlobal
,
1898 if (EFI_ERROR (Status
)) {
1902 DataSize
= DataSizeOfVariable (CertDbVariable
.CurrPtr
);
1903 Data
= GetVariableDataPtr (CertDbVariable
.CurrPtr
);
1904 if ((DataSize
== 0) || (Data
== NULL
)) {
1906 return EFI_NOT_FOUND
;
1909 if (DataSize
== sizeof (UINT32
)) {
1911 // There is no certs in certdb.
1917 // Get corresponding cert node from certdb.
1919 Status
= FindCertsFromDb (
1930 if (EFI_ERROR (Status
)) {
1934 if (DataSize
< (CertNodeOffset
+ CertNodeSize
)) {
1935 return EFI_NOT_FOUND
;
1939 // Construct new data content of variable "certdb".
1941 NewCertDbSize
= (UINT32
) DataSize
- CertNodeSize
;
1942 NewCertDb
= (UINT8
*) mCertDbStore
;
1945 // Copy the DB entries before deleting node.
1947 CopyMem (NewCertDb
, Data
, CertNodeOffset
);
1949 // Update CertDbListSize.
1951 CopyMem (NewCertDb
, &NewCertDbSize
, sizeof (UINT32
));
1953 // Copy the DB entries after deleting node.
1955 if (DataSize
> (CertNodeOffset
+ CertNodeSize
)) {
1957 NewCertDb
+ CertNodeOffset
,
1958 Data
+ CertNodeOffset
+ CertNodeSize
,
1959 DataSize
- CertNodeOffset
- CertNodeSize
1966 VarAttr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
1967 Status
= UpdateVariable (
1983 Insert signer's certificates for common authenticated variable with VariableName
1984 and VendorGuid in AUTH_CERT_DB_DATA to "certdb".
1986 @param[in] VariableName Name of authenticated Variable.
1987 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1988 @param[in] CertData Pointer to signer's certificates.
1989 @param[in] CertDataSize Length of CertData in bytes.
1991 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1992 @retval EFI_ACCESS_DENIED An AUTH_CERT_DB_DATA entry with same VariableName
1993 and VendorGuid already exists.
1994 @retval EFI_OUT_OF_RESOURCES The operation is failed due to lack of resources.
1995 @retval EFI_SUCCESS Insert an AUTH_CERT_DB_DATA entry to "certdb"
2000 IN CHAR16
*VariableName
,
2001 IN EFI_GUID
*VendorGuid
,
2003 IN UINTN CertDataSize
2006 VARIABLE_POINTER_TRACK CertDbVariable
;
2012 UINT32 NewCertDbSize
;
2013 UINT32 CertNodeSize
;
2015 AUTH_CERT_DB_DATA
*Ptr
;
2017 if ((VariableName
== NULL
) || (VendorGuid
== NULL
) || (CertData
== NULL
)) {
2018 return EFI_INVALID_PARAMETER
;
2022 // Get variable "certdb".
2024 Status
= FindVariable (
2028 &mVariableModuleGlobal
->VariableGlobal
,
2031 if (EFI_ERROR (Status
)) {
2035 DataSize
= DataSizeOfVariable (CertDbVariable
.CurrPtr
);
2036 Data
= GetVariableDataPtr (CertDbVariable
.CurrPtr
);
2037 if ((DataSize
== 0) || (Data
== NULL
)) {
2039 return EFI_NOT_FOUND
;
2043 // Find whether matching cert node already exists in "certdb".
2044 // If yes return error.
2046 Status
= FindCertsFromDb (
2057 if (!EFI_ERROR (Status
)) {
2059 return EFI_ACCESS_DENIED
;
2063 // Construct new data content of variable "certdb".
2065 NameSize
= (UINT32
) StrLen (VariableName
);
2066 CertNodeSize
= sizeof (AUTH_CERT_DB_DATA
) + (UINT32
) CertDataSize
+ NameSize
* sizeof (CHAR16
);
2067 NewCertDbSize
= (UINT32
) DataSize
+ CertNodeSize
;
2068 if (NewCertDbSize
> mMaxCertDbSize
) {
2069 return EFI_OUT_OF_RESOURCES
;
2071 NewCertDb
= (UINT8
*) mCertDbStore
;
2074 // Copy the DB entries before deleting node.
2076 CopyMem (NewCertDb
, Data
, DataSize
);
2078 // Update CertDbListSize.
2080 CopyMem (NewCertDb
, &NewCertDbSize
, sizeof (UINT32
));
2082 // Construct new cert node.
2084 Ptr
= (AUTH_CERT_DB_DATA
*) (NewCertDb
+ DataSize
);
2085 CopyGuid (&Ptr
->VendorGuid
, VendorGuid
);
2086 CopyMem (&Ptr
->CertNodeSize
, &CertNodeSize
, sizeof (UINT32
));
2087 CopyMem (&Ptr
->NameSize
, &NameSize
, sizeof (UINT32
));
2088 CopyMem (&Ptr
->CertDataSize
, &CertDataSize
, sizeof (UINT32
));
2091 (UINT8
*) Ptr
+ sizeof (AUTH_CERT_DB_DATA
),
2093 NameSize
* sizeof (CHAR16
)
2097 (UINT8
*) Ptr
+ sizeof (AUTH_CERT_DB_DATA
) + NameSize
* sizeof (CHAR16
),
2105 VarAttr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
2106 Status
= UpdateVariable (
2122 Process variable with EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set
2124 Caution: This function may receive untrusted input.
2125 This function may be invoked in SMM mode, and datasize and data are external input.
2126 This function will do basic validation, before parse the data.
2127 This function will parse the authentication carefully to avoid security issues, like
2128 buffer overflow, integer overflow.
2130 @param[in] VariableName Name of Variable to be found.
2131 @param[in] VendorGuid Variable vendor GUID.
2132 @param[in] Data Data pointer.
2133 @param[in] DataSize Size of Data found. If size is less than the
2134 data, this value contains the required size.
2135 @param[in] Variable The variable information which is used to keep track of variable usage.
2136 @param[in] Attributes Attribute value of the variable.
2137 @param[in] AuthVarType Verify against PK, KEK database, private database or certificate in data payload.
2138 @param[out] VarDel Delete the variable or not.
2140 @retval EFI_INVALID_PARAMETER Invalid parameter.
2141 @retval EFI_SECURITY_VIOLATION The variable does NOT pass the validation
2142 check carried out by the firmware.
2143 @retval EFI_OUT_OF_RESOURCES Failed to process variable due to lack
2145 @retval EFI_SUCCESS Variable pass validation successfully.
2149 VerifyTimeBasedPayload (
2150 IN CHAR16
*VariableName
,
2151 IN EFI_GUID
*VendorGuid
,
2154 IN VARIABLE_POINTER_TRACK
*Variable
,
2155 IN UINT32 Attributes
,
2156 IN AUTHVAR_TYPE AuthVarType
,
2170 BOOLEAN VerifyStatus
;
2172 EFI_SIGNATURE_LIST
*CertList
;
2173 EFI_SIGNATURE_DATA
*Cert
;
2174 VARIABLE_POINTER_TRACK KekVariable
;
2175 EFI_VARIABLE_AUTHENTICATION_2
*CertData
;
2178 VARIABLE_POINTER_TRACK PkVariable
;
2183 UINTN CertStackSize
;
2184 UINT8
*CertsInCertDb
;
2185 UINT32 CertsSizeinDb
;
2187 VerifyStatus
= FALSE
;
2194 CertsInCertDb
= NULL
;
2197 // When the attribute EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS is
2198 // set, then the Data buffer shall begin with an instance of a complete (and serialized)
2199 // EFI_VARIABLE_AUTHENTICATION_2 descriptor. The descriptor shall be followed by the new
2200 // variable value and DataSize shall reflect the combined size of the descriptor and the new
2201 // variable value. The authentication descriptor is not part of the variable data and is not
2202 // returned by subsequent calls to GetVariable().
2204 CertData
= (EFI_VARIABLE_AUTHENTICATION_2
*) Data
;
2207 // Verify that Pad1, Nanosecond, TimeZone, Daylight and Pad2 components of the
2208 // TimeStamp value are set to zero.
2210 if ((CertData
->TimeStamp
.Pad1
!= 0) ||
2211 (CertData
->TimeStamp
.Nanosecond
!= 0) ||
2212 (CertData
->TimeStamp
.TimeZone
!= 0) ||
2213 (CertData
->TimeStamp
.Daylight
!= 0) ||
2214 (CertData
->TimeStamp
.Pad2
!= 0)) {
2215 return EFI_SECURITY_VIOLATION
;
2218 if ((Variable
->CurrPtr
!= NULL
) && ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0)) {
2219 if (CompareTimeStamp (&CertData
->TimeStamp
, &Variable
->CurrPtr
->TimeStamp
)) {
2221 // TimeStamp check fail, suspicious replay attack, return EFI_SECURITY_VIOLATION.
2223 return EFI_SECURITY_VIOLATION
;
2228 // wCertificateType should be WIN_CERT_TYPE_EFI_GUID.
2229 // Cert type should be EFI_CERT_TYPE_PKCS7_GUID.
2231 if ((CertData
->AuthInfo
.Hdr
.wCertificateType
!= WIN_CERT_TYPE_EFI_GUID
) ||
2232 !CompareGuid (&CertData
->AuthInfo
.CertType
, &gEfiCertPkcs7Guid
)) {
2234 // Invalid AuthInfo type, return EFI_SECURITY_VIOLATION.
2236 return EFI_SECURITY_VIOLATION
;
2240 // Find out Pkcs7 SignedData which follows the EFI_VARIABLE_AUTHENTICATION_2 descriptor.
2241 // AuthInfo.Hdr.dwLength is the length of the entire certificate, including the length of the header.
2243 SigData
= CertData
->AuthInfo
.CertData
;
2244 SigDataSize
= CertData
->AuthInfo
.Hdr
.dwLength
- (UINT32
) (OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID
, CertData
));
2247 // Find out the new data payload which follows Pkcs7 SignedData directly.
2249 PayloadPtr
= SigData
+ SigDataSize
;
2250 PayloadSize
= DataSize
- OFFSET_OF_AUTHINFO2_CERT_DATA
- (UINTN
) SigDataSize
;
2253 // Construct a serialization buffer of the values of the VariableName, VendorGuid and Attributes
2254 // parameters of the SetVariable() call and the TimeStamp component of the
2255 // EFI_VARIABLE_AUTHENTICATION_2 descriptor followed by the variable's new value
2256 // i.e. (VariableName, VendorGuid, Attributes, TimeStamp, Data)
2258 NewDataSize
= PayloadSize
+ sizeof (EFI_TIME
) + sizeof (UINT32
) +
2259 sizeof (EFI_GUID
) + StrSize (VariableName
) - sizeof (CHAR16
);
2261 // Here is to reuse scratch data area(at the end of volatile variable store)
2262 // to reduce SMRAM consumption for SMM variable driver.
2263 // The scratch buffer is enough to hold the serialized data and safe to use,
2264 // because it will be used at here to do verification only first
2265 // and then used in UpdateVariable() for a time based auth variable set.
2267 NewData
= (UINT8
*) GetEndPointer ((VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
));
2270 Length
= StrLen (VariableName
) * sizeof (CHAR16
);
2271 CopyMem (Buffer
, VariableName
, Length
);
2274 Length
= sizeof (EFI_GUID
);
2275 CopyMem (Buffer
, VendorGuid
, Length
);
2278 Length
= sizeof (UINT32
);
2279 CopyMem (Buffer
, &Attr
, Length
);
2282 Length
= sizeof (EFI_TIME
);
2283 CopyMem (Buffer
, &CertData
->TimeStamp
, Length
);
2286 CopyMem (Buffer
, PayloadPtr
, PayloadSize
);
2288 if (AuthVarType
== AuthVarTypePk
) {
2290 // Verify that the signature has been made with the current Platform Key (no chaining for PK).
2291 // First, get signer's certificates from SignedData.
2293 VerifyStatus
= Pkcs7GetSigners (
2301 if (!VerifyStatus
) {
2306 // Second, get the current platform key from variable. Check whether it's identical with signer's certificates
2307 // in SignedData. If not, return error immediately.
2309 Status
= FindVariable (
2310 EFI_PLATFORM_KEY_NAME
,
2311 &gEfiGlobalVariableGuid
,
2313 &mVariableModuleGlobal
->VariableGlobal
,
2316 if (EFI_ERROR (Status
)) {
2317 VerifyStatus
= FALSE
;
2320 CertList
= (EFI_SIGNATURE_LIST
*) GetVariableDataPtr (PkVariable
.CurrPtr
);
2321 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
2322 if ((RootCertSize
!= (CertList
->SignatureSize
- (sizeof (EFI_SIGNATURE_DATA
) - 1))) ||
2323 (CompareMem (Cert
->SignatureData
, RootCert
, RootCertSize
) != 0)) {
2324 VerifyStatus
= FALSE
;
2329 // Verify Pkcs7 SignedData via Pkcs7Verify library.
2331 VerifyStatus
= Pkcs7Verify (
2340 } else if (AuthVarType
== AuthVarTypeKek
) {
2343 // Get KEK database from variable.
2345 Status
= FindVariable (
2346 EFI_KEY_EXCHANGE_KEY_NAME
,
2347 &gEfiGlobalVariableGuid
,
2349 &mVariableModuleGlobal
->VariableGlobal
,
2352 if (EFI_ERROR (Status
)) {
2357 // Ready to verify Pkcs7 SignedData. Go through KEK Signature Database to find out X.509 CertList.
2359 KekDataSize
= KekVariable
.CurrPtr
->DataSize
;
2360 CertList
= (EFI_SIGNATURE_LIST
*) GetVariableDataPtr (KekVariable
.CurrPtr
);
2361 while ((KekDataSize
> 0) && (KekDataSize
>= CertList
->SignatureListSize
)) {
2362 if (CompareGuid (&CertList
->SignatureType
, &gEfiCertX509Guid
)) {
2363 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
2364 CertCount
= (CertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - CertList
->SignatureHeaderSize
) / CertList
->SignatureSize
;
2365 for (Index
= 0; Index
< CertCount
; Index
++) {
2367 // Iterate each Signature Data Node within this CertList for a verify
2369 RootCert
= Cert
->SignatureData
;
2370 RootCertSize
= CertList
->SignatureSize
- (sizeof (EFI_SIGNATURE_DATA
) - 1);
2373 // Verify Pkcs7 SignedData via Pkcs7Verify library.
2375 VerifyStatus
= Pkcs7Verify (
2386 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) Cert
+ CertList
->SignatureSize
);
2389 KekDataSize
-= CertList
->SignatureListSize
;
2390 CertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) CertList
+ CertList
->SignatureListSize
);
2392 } else if (AuthVarType
== AuthVarTypePriv
) {
2395 // Process common authenticated variable except PK/KEK/DB/DBX/DBT.
2396 // Get signer's certificates from SignedData.
2398 VerifyStatus
= Pkcs7GetSigners (
2406 if (!VerifyStatus
) {
2411 // Get previously stored signer's certificates from certdb for existing
2412 // variable. Check whether they are identical with signer's certificates
2413 // in SignedData. If not, return error immediately.
2415 if ((Variable
->CurrPtr
!= NULL
)) {
2416 VerifyStatus
= FALSE
;
2418 Status
= GetCertsFromDb (VariableName
, VendorGuid
, &CertsInCertDb
, &CertsSizeinDb
);
2419 if (EFI_ERROR (Status
)) {
2423 if ((CertStackSize
!= CertsSizeinDb
) ||
2424 (CompareMem (SignerCerts
, CertsInCertDb
, CertsSizeinDb
) != 0)) {
2429 VerifyStatus
= Pkcs7Verify (
2437 if (!VerifyStatus
) {
2442 // Delete signer's certificates when delete the common authenticated variable.
2444 if ((PayloadSize
== 0) && (Variable
->CurrPtr
!= NULL
) && ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0)) {
2445 Status
= DeleteCertsFromDb (VariableName
, VendorGuid
);
2446 if (EFI_ERROR (Status
)) {
2447 VerifyStatus
= FALSE
;
2450 } else if (Variable
->CurrPtr
== NULL
&& PayloadSize
!= 0) {
2452 // Insert signer's certificates when adding a new common authenticated variable.
2454 Status
= InsertCertsToDb (VariableName
, VendorGuid
, SignerCerts
, CertStackSize
);
2455 if (EFI_ERROR (Status
)) {
2456 VerifyStatus
= FALSE
;
2460 } else if (AuthVarType
== AuthVarTypePayload
) {
2461 CertList
= (EFI_SIGNATURE_LIST
*) PayloadPtr
;
2462 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
2463 RootCert
= Cert
->SignatureData
;
2464 RootCertSize
= CertList
->SignatureSize
- (sizeof (EFI_SIGNATURE_DATA
) - 1);
2466 // Verify Pkcs7 SignedData via Pkcs7Verify library.
2468 VerifyStatus
= Pkcs7Verify (
2477 return EFI_SECURITY_VIOLATION
;
2482 if (AuthVarType
== AuthVarTypePk
|| AuthVarType
== AuthVarTypePriv
) {
2483 Pkcs7FreeSigners (RootCert
);
2484 Pkcs7FreeSigners (SignerCerts
);
2487 if (!VerifyStatus
) {
2488 return EFI_SECURITY_VIOLATION
;
2491 Status
= CheckSignatureListFormat(VariableName
, VendorGuid
, PayloadPtr
, PayloadSize
);
2492 if (EFI_ERROR (Status
)) {
2496 if ((PayloadSize
== 0) && (VarDel
!= NULL
)) {
2501 // Final step: Update/Append Variable if it pass Pkcs7Verify
2503 return UpdateVariable (
2512 &CertData
->TimeStamp