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
1263 // To delete a variable created with the EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
1264 // or the EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS attribute,
1265 // SetVariable must be used with attributes matching the existing variable
1266 // and the DataSize set to the size of the AuthInfo descriptor.
1268 if ((Variable
->CurrPtr
!= NULL
) &&
1269 (Attributes
== Variable
->CurrPtr
->Attributes
) &&
1270 ((Attributes
& (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
)) != 0)) {
1271 if ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) {
1272 PayloadSize
= DataSize
- AUTHINFO2_SIZE (Data
);
1273 if (PayloadSize
== 0) {
1277 PayloadSize
= DataSize
- AUTHINFO_SIZE
;
1278 if (PayloadSize
== 0) {
1288 Process variable with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS/EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set
1290 Caution: This function may receive untrusted input.
1291 This function may be invoked in SMM mode, and datasize and data are external input.
1292 This function will do basic validation, before parse the data.
1293 This function will parse the authentication carefully to avoid security issues, like
1294 buffer overflow, integer overflow.
1295 This function will check attribute carefully to avoid authentication bypass.
1297 @param[in] VariableName Name of Variable to be found.
1298 @param[in] VendorGuid Variable vendor GUID.
1300 @param[in] Data Data pointer.
1301 @param[in] DataSize Size of Data.
1302 @param[in] Variable The variable information which is used to keep track of variable usage.
1303 @param[in] Attributes Attribute value of the variable.
1305 @return EFI_INVALID_PARAMETER Invalid parameter.
1306 @return EFI_WRITE_PROTECTED Variable is write-protected and needs authentication with
1307 EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS set.
1308 @return EFI_OUT_OF_RESOURCES The Database to save the public key is full.
1309 @return EFI_SECURITY_VIOLATION The variable is with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
1310 set, but the AuthInfo does NOT pass the validation
1311 check carried out by the firmware.
1312 @return EFI_SUCCESS Variable is not write-protected or pass validation successfully.
1317 IN CHAR16
*VariableName
,
1318 IN EFI_GUID
*VendorGuid
,
1321 IN VARIABLE_POINTER_TRACK
*Variable
,
1322 IN UINT32 Attributes
1327 BOOLEAN IsFirstTime
;
1329 EFI_VARIABLE_AUTHENTICATION
*CertData
;
1330 EFI_CERT_BLOCK_RSA_2048_SHA256
*CertBlock
;
1332 UINT64 MonotonicCount
;
1333 VARIABLE_ENTRY_CONSISTENCY VariableDataEntry
;
1340 Status
= EFI_SUCCESS
;
1342 if (IsDeleteAuthVariable (Data
, DataSize
, Variable
, Attributes
) && UserPhysicalPresent()) {
1344 // Allow the delete operation of common authenticated variable at user physical presence.
1346 if ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) {
1347 Status
= DeleteCertsFromDb (VariableName
, VendorGuid
);
1349 if (!EFI_ERROR (Status
)) {
1350 Status
= UpdateVariable (
1365 if (NeedPhysicallyPresent (VariableName
, VendorGuid
) && !UserPhysicalPresent()) {
1367 // This variable is protected, only physical present user could modify its value.
1369 return EFI_SECURITY_VIOLATION
;
1373 // A time-based authenticated variable and a count-based authenticated variable
1374 // can't be updated by each other.
1376 if (Variable
->CurrPtr
!= NULL
) {
1377 if (((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) &&
1378 ((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0)) {
1379 return EFI_SECURITY_VIOLATION
;
1382 if (((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) &&
1383 ((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0)) {
1384 return EFI_SECURITY_VIOLATION
;
1389 // Process Time-based Authenticated variable.
1391 if ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) {
1392 return VerifyTimeBasedPayload (
1405 // Determine if first time SetVariable with the EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS.
1407 if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) {
1409 // Determine current operation type.
1411 if (DataSize
== AUTHINFO_SIZE
) {
1415 // Determine whether this is the first time with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS set.
1417 if (Variable
->CurrPtr
== NULL
) {
1419 } else if ((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) == 0) {
1422 KeyIndex
= Variable
->CurrPtr
->PubKeyIndex
;
1423 IsFirstTime
= FALSE
;
1425 } else if ((Variable
->CurrPtr
!= NULL
) &&
1426 ((Variable
->CurrPtr
->Attributes
& (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
)) != 0)
1429 // If the variable is already write-protected, it always needs authentication before update.
1431 return EFI_WRITE_PROTECTED
;
1434 // If without EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS, set and attributes collision.
1435 // That means it is not authenticated variable, just update variable as usual.
1437 Status
= UpdateVariable (VariableName
, VendorGuid
, Data
, DataSize
, Attributes
, 0, 0, Variable
, NULL
);
1442 // Get PubKey and check Monotonic Count value corresponding to the variable.
1444 CertData
= (EFI_VARIABLE_AUTHENTICATION
*) Data
;
1445 CertBlock
= (EFI_CERT_BLOCK_RSA_2048_SHA256
*) (CertData
->AuthInfo
.CertData
);
1446 PubKey
= CertBlock
->PublicKey
;
1449 // Update Monotonic Count value.
1451 MonotonicCount
= CertData
->MonotonicCount
;
1455 // 2 cases need to check here
1456 // 1. Internal PubKey variable. PubKeyIndex is always 0
1457 // 2. Other counter-based AuthVariable. Check input PubKey.
1459 if (KeyIndex
== 0 || CompareMem (PubKey
, mPubKeyStore
+ (KeyIndex
- 1) * EFI_CERT_TYPE_RSA2048_SIZE
, EFI_CERT_TYPE_RSA2048_SIZE
) != 0) {
1460 return EFI_SECURITY_VIOLATION
;
1463 // Compare the current monotonic count and ensure that it is greater than the last SetVariable
1464 // operation with the EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS attribute set.
1466 if (CertData
->MonotonicCount
<= Variable
->CurrPtr
->MonotonicCount
) {
1468 // Monotonic count check fail, suspicious replay attack, return EFI_SECURITY_VIOLATION.
1470 return EFI_SECURITY_VIOLATION
;
1474 // Verify the certificate in Data payload.
1476 Status
= VerifyCounterBasedPayload (Data
, DataSize
, PubKey
);
1477 if (EFI_ERROR (Status
)) {
1482 // Now, the signature has been verified!
1484 if (IsFirstTime
&& !IsDeletion
) {
1485 VariableDataEntry
.VariableSize
= DataSize
- AUTHINFO_SIZE
;
1486 VariableDataEntry
.Guid
= VendorGuid
;
1487 VariableDataEntry
.Name
= VariableName
;
1490 // Update public key database variable if need.
1492 KeyIndex
= AddPubKeyInStore (PubKey
, &VariableDataEntry
);
1493 if (KeyIndex
== 0) {
1494 return EFI_OUT_OF_RESOURCES
;
1499 // Verification pass.
1501 return UpdateVariable (VariableName
, VendorGuid
, (UINT8
*)Data
+ AUTHINFO_SIZE
, DataSize
- AUTHINFO_SIZE
, Attributes
, KeyIndex
, MonotonicCount
, Variable
, NULL
);
1505 Merge two buffers which formatted as EFI_SIGNATURE_LIST. Only the new EFI_SIGNATURE_DATA
1506 will be appended to the original EFI_SIGNATURE_LIST, duplicate EFI_SIGNATURE_DATA
1509 @param[in, out] Data Pointer to original EFI_SIGNATURE_LIST.
1510 @param[in] DataSize Size of Data buffer.
1511 @param[in] FreeBufSize Size of free data buffer
1512 @param[in] NewData Pointer to new EFI_SIGNATURE_LIST to be appended.
1513 @param[in] NewDataSize Size of NewData buffer.
1514 @param[out] MergedBufSize Size of the merged buffer
1516 @return EFI_BUFFER_TOO_SMALL if input Data buffer overflowed
1520 AppendSignatureList (
1523 IN UINTN FreeBufSize
,
1525 IN UINTN NewDataSize
,
1526 OUT UINTN
*MergedBufSize
1529 EFI_SIGNATURE_LIST
*CertList
;
1530 EFI_SIGNATURE_DATA
*Cert
;
1532 EFI_SIGNATURE_LIST
*NewCertList
;
1533 EFI_SIGNATURE_DATA
*NewCert
;
1540 UINTN SignatureListSize
;
1543 Tail
= (UINT8
*) Data
+ DataSize
;
1545 NewCertList
= (EFI_SIGNATURE_LIST
*) NewData
;
1546 while ((NewDataSize
> 0) && (NewDataSize
>= NewCertList
->SignatureListSize
)) {
1547 NewCert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) NewCertList
+ sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
);
1548 NewCertCount
= (NewCertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - NewCertList
->SignatureHeaderSize
) / NewCertList
->SignatureSize
;
1551 for (Index
= 0; Index
< NewCertCount
; Index
++) {
1555 CertList
= (EFI_SIGNATURE_LIST
*) Data
;
1556 while ((Size
> 0) && (Size
>= CertList
->SignatureListSize
)) {
1557 if (CompareGuid (&CertList
->SignatureType
, &NewCertList
->SignatureType
) &&
1558 (CertList
->SignatureSize
== NewCertList
->SignatureSize
)) {
1559 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
1560 CertCount
= (CertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - CertList
->SignatureHeaderSize
) / CertList
->SignatureSize
;
1561 for (Index2
= 0; Index2
< CertCount
; Index2
++) {
1563 // Iterate each Signature Data in this Signature List.
1565 if (CompareMem (NewCert
, Cert
, CertList
->SignatureSize
) == 0) {
1569 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) Cert
+ CertList
->SignatureSize
);
1576 Size
-= CertList
->SignatureListSize
;
1577 CertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) CertList
+ CertList
->SignatureListSize
);
1582 // New EFI_SIGNATURE_DATA, append it.
1584 if (CopiedCount
== 0) {
1585 if (FreeBufSize
< sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
) {
1586 return EFI_BUFFER_TOO_SMALL
;
1590 // Copy EFI_SIGNATURE_LIST header for only once.
1593 CopyMem (Tail
, NewCertList
, sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
);
1594 Tail
= Tail
+ sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
;
1595 FreeBufSize
-= sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
;
1598 if (FreeBufSize
< NewCertList
->SignatureSize
) {
1599 return EFI_BUFFER_TOO_SMALL
;
1601 CopyMem (Tail
, NewCert
, NewCertList
->SignatureSize
);
1602 Tail
+= NewCertList
->SignatureSize
;
1603 FreeBufSize
-= NewCertList
->SignatureSize
;
1607 NewCert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) NewCert
+ NewCertList
->SignatureSize
);
1611 // Update SignatureListSize in newly appended EFI_SIGNATURE_LIST.
1613 if (CopiedCount
!= 0) {
1614 SignatureListSize
= sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
+ (CopiedCount
* NewCertList
->SignatureSize
);
1615 CertList
= (EFI_SIGNATURE_LIST
*) (Tail
- SignatureListSize
);
1616 CertList
->SignatureListSize
= (UINT32
) SignatureListSize
;
1619 NewDataSize
-= NewCertList
->SignatureListSize
;
1620 NewCertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) NewCertList
+ NewCertList
->SignatureListSize
);
1623 *MergedBufSize
= (Tail
- (UINT8
*) Data
);
1628 Compare two EFI_TIME data.
1631 @param FirstTime A pointer to the first EFI_TIME data.
1632 @param SecondTime A pointer to the second EFI_TIME data.
1634 @retval TRUE The FirstTime is not later than the SecondTime.
1635 @retval FALSE The FirstTime is later than the SecondTime.
1640 IN EFI_TIME
*FirstTime
,
1641 IN EFI_TIME
*SecondTime
1644 if (FirstTime
->Year
!= SecondTime
->Year
) {
1645 return (BOOLEAN
) (FirstTime
->Year
< SecondTime
->Year
);
1646 } else if (FirstTime
->Month
!= SecondTime
->Month
) {
1647 return (BOOLEAN
) (FirstTime
->Month
< SecondTime
->Month
);
1648 } else if (FirstTime
->Day
!= SecondTime
->Day
) {
1649 return (BOOLEAN
) (FirstTime
->Day
< SecondTime
->Day
);
1650 } else if (FirstTime
->Hour
!= SecondTime
->Hour
) {
1651 return (BOOLEAN
) (FirstTime
->Hour
< SecondTime
->Hour
);
1652 } else if (FirstTime
->Minute
!= SecondTime
->Minute
) {
1653 return (BOOLEAN
) (FirstTime
->Minute
< SecondTime
->Minute
);
1656 return (BOOLEAN
) (FirstTime
->Second
<= SecondTime
->Second
);
1660 Find matching signer's certificates for common authenticated variable
1661 by corresponding VariableName and VendorGuid from "certdb".
1663 The data format of "certdb":
1665 // UINT32 CertDbListSize;
1666 // /// AUTH_CERT_DB_DATA Certs1[];
1667 // /// AUTH_CERT_DB_DATA Certs2[];
1669 // /// AUTH_CERT_DB_DATA Certsn[];
1672 @param[in] VariableName Name of authenticated Variable.
1673 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1674 @param[in] Data Pointer to variable "certdb".
1675 @param[in] DataSize Size of variable "certdb".
1676 @param[out] CertOffset Offset of matching CertData, from starting of Data.
1677 @param[out] CertDataSize Length of CertData in bytes.
1678 @param[out] CertNodeOffset Offset of matching AUTH_CERT_DB_DATA , from
1680 @param[out] CertNodeSize Length of AUTH_CERT_DB_DATA in bytes.
1682 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1683 @retval EFI_NOT_FOUND Fail to find matching certs.
1684 @retval EFI_SUCCESS Find matching certs and output parameters.
1689 IN CHAR16
*VariableName
,
1690 IN EFI_GUID
*VendorGuid
,
1693 OUT UINT32
*CertOffset
, OPTIONAL
1694 OUT UINT32
*CertDataSize
, OPTIONAL
1695 OUT UINT32
*CertNodeOffset
,OPTIONAL
1696 OUT UINT32
*CertNodeSize OPTIONAL
1700 AUTH_CERT_DB_DATA
*Ptr
;
1704 UINT32 CertDbListSize
;
1706 if ((VariableName
== NULL
) || (VendorGuid
== NULL
) || (Data
== NULL
)) {
1707 return EFI_INVALID_PARAMETER
;
1711 // Check whether DataSize matches recorded CertDbListSize.
1713 if (DataSize
< sizeof (UINT32
)) {
1714 return EFI_INVALID_PARAMETER
;
1717 CertDbListSize
= ReadUnaligned32 ((UINT32
*) Data
);
1719 if (CertDbListSize
!= (UINT32
) DataSize
) {
1720 return EFI_INVALID_PARAMETER
;
1723 Offset
= sizeof (UINT32
);
1726 // Get corresponding certificates by VendorGuid and VariableName.
1728 while (Offset
< (UINT32
) DataSize
) {
1729 Ptr
= (AUTH_CERT_DB_DATA
*) (Data
+ Offset
);
1731 // Check whether VendorGuid matches.
1733 if (CompareGuid (&Ptr
->VendorGuid
, VendorGuid
)) {
1734 NodeSize
= ReadUnaligned32 (&Ptr
->CertNodeSize
);
1735 NameSize
= ReadUnaligned32 (&Ptr
->NameSize
);
1736 CertSize
= ReadUnaligned32 (&Ptr
->CertDataSize
);
1738 if (NodeSize
!= sizeof (EFI_GUID
) + sizeof (UINT32
) * 3 + CertSize
+
1739 sizeof (CHAR16
) * NameSize
) {
1740 return EFI_INVALID_PARAMETER
;
1743 Offset
= Offset
+ sizeof (EFI_GUID
) + sizeof (UINT32
) * 3;
1745 // Check whether VariableName matches.
1747 if ((NameSize
== StrLen (VariableName
)) &&
1748 (CompareMem (Data
+ Offset
, VariableName
, NameSize
* sizeof (CHAR16
)) == 0)) {
1749 Offset
= Offset
+ NameSize
* sizeof (CHAR16
);
1751 if (CertOffset
!= NULL
) {
1752 *CertOffset
= Offset
;
1755 if (CertDataSize
!= NULL
) {
1756 *CertDataSize
= CertSize
;
1759 if (CertNodeOffset
!= NULL
) {
1760 *CertNodeOffset
= (UINT32
) ((UINT8
*) Ptr
- Data
);
1763 if (CertNodeSize
!= NULL
) {
1764 *CertNodeSize
= NodeSize
;
1769 Offset
= Offset
+ NameSize
* sizeof (CHAR16
) + CertSize
;
1772 NodeSize
= ReadUnaligned32 (&Ptr
->CertNodeSize
);
1773 Offset
= Offset
+ NodeSize
;
1777 return EFI_NOT_FOUND
;
1781 Retrieve signer's certificates for common authenticated variable
1782 by corresponding VariableName and VendorGuid from "certdb".
1784 @param[in] VariableName Name of authenticated Variable.
1785 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1786 @param[out] CertData Pointer to signer's certificates.
1787 @param[out] CertDataSize Length of CertData in bytes.
1789 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1790 @retval EFI_NOT_FOUND Fail to find "certdb" or matching certs.
1791 @retval EFI_SUCCESS Get signer's certificates successfully.
1796 IN CHAR16
*VariableName
,
1797 IN EFI_GUID
*VendorGuid
,
1798 OUT UINT8
**CertData
,
1799 OUT UINT32
*CertDataSize
1802 VARIABLE_POINTER_TRACK CertDbVariable
;
1808 if ((VariableName
== NULL
) || (VendorGuid
== NULL
) || (CertData
== NULL
) || (CertDataSize
== NULL
)) {
1809 return EFI_INVALID_PARAMETER
;
1813 // Get variable "certdb".
1815 Status
= FindVariable (
1819 &mVariableModuleGlobal
->VariableGlobal
,
1822 if (EFI_ERROR (Status
)) {
1826 DataSize
= DataSizeOfVariable (CertDbVariable
.CurrPtr
);
1827 Data
= GetVariableDataPtr (CertDbVariable
.CurrPtr
);
1828 if ((DataSize
== 0) || (Data
== NULL
)) {
1830 return EFI_NOT_FOUND
;
1833 Status
= FindCertsFromDb (
1844 if (EFI_ERROR (Status
)) {
1848 *CertData
= Data
+ CertOffset
;
1853 Delete matching signer's certificates when deleting common authenticated
1854 variable by corresponding VariableName and VendorGuid from "certdb".
1856 @param[in] VariableName Name of authenticated Variable.
1857 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1859 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1860 @retval EFI_NOT_FOUND Fail to find "certdb" or matching certs.
1861 @retval EFI_OUT_OF_RESOURCES The operation is failed due to lack of resources.
1862 @retval EFI_SUCCESS The operation is completed successfully.
1867 IN CHAR16
*VariableName
,
1868 IN EFI_GUID
*VendorGuid
1871 VARIABLE_POINTER_TRACK CertDbVariable
;
1876 UINT32 CertNodeOffset
;
1877 UINT32 CertNodeSize
;
1879 UINT32 NewCertDbSize
;
1881 if ((VariableName
== NULL
) || (VendorGuid
== NULL
)) {
1882 return EFI_INVALID_PARAMETER
;
1886 // Get variable "certdb".
1888 Status
= FindVariable (
1892 &mVariableModuleGlobal
->VariableGlobal
,
1895 if (EFI_ERROR (Status
)) {
1899 DataSize
= DataSizeOfVariable (CertDbVariable
.CurrPtr
);
1900 Data
= GetVariableDataPtr (CertDbVariable
.CurrPtr
);
1901 if ((DataSize
== 0) || (Data
== NULL
)) {
1903 return EFI_NOT_FOUND
;
1906 if (DataSize
== sizeof (UINT32
)) {
1908 // There is no certs in certdb.
1914 // Get corresponding cert node from certdb.
1916 Status
= FindCertsFromDb (
1927 if (EFI_ERROR (Status
)) {
1931 if (DataSize
< (CertNodeOffset
+ CertNodeSize
)) {
1932 return EFI_NOT_FOUND
;
1936 // Construct new data content of variable "certdb".
1938 NewCertDbSize
= (UINT32
) DataSize
- CertNodeSize
;
1939 NewCertDb
= (UINT8
*) mCertDbStore
;
1942 // Copy the DB entries before deleting node.
1944 CopyMem (NewCertDb
, Data
, CertNodeOffset
);
1946 // Update CertDbListSize.
1948 CopyMem (NewCertDb
, &NewCertDbSize
, sizeof (UINT32
));
1950 // Copy the DB entries after deleting node.
1952 if (DataSize
> (CertNodeOffset
+ CertNodeSize
)) {
1954 NewCertDb
+ CertNodeOffset
,
1955 Data
+ CertNodeOffset
+ CertNodeSize
,
1956 DataSize
- CertNodeOffset
- CertNodeSize
1963 VarAttr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
1964 Status
= UpdateVariable (
1980 Insert signer's certificates for common authenticated variable with VariableName
1981 and VendorGuid in AUTH_CERT_DB_DATA to "certdb".
1983 @param[in] VariableName Name of authenticated Variable.
1984 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1985 @param[in] CertData Pointer to signer's certificates.
1986 @param[in] CertDataSize Length of CertData in bytes.
1988 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1989 @retval EFI_ACCESS_DENIED An AUTH_CERT_DB_DATA entry with same VariableName
1990 and VendorGuid already exists.
1991 @retval EFI_OUT_OF_RESOURCES The operation is failed due to lack of resources.
1992 @retval EFI_SUCCESS Insert an AUTH_CERT_DB_DATA entry to "certdb"
1997 IN CHAR16
*VariableName
,
1998 IN EFI_GUID
*VendorGuid
,
2000 IN UINTN CertDataSize
2003 VARIABLE_POINTER_TRACK CertDbVariable
;
2009 UINT32 NewCertDbSize
;
2010 UINT32 CertNodeSize
;
2012 AUTH_CERT_DB_DATA
*Ptr
;
2014 if ((VariableName
== NULL
) || (VendorGuid
== NULL
) || (CertData
== NULL
)) {
2015 return EFI_INVALID_PARAMETER
;
2019 // Get variable "certdb".
2021 Status
= FindVariable (
2025 &mVariableModuleGlobal
->VariableGlobal
,
2028 if (EFI_ERROR (Status
)) {
2032 DataSize
= DataSizeOfVariable (CertDbVariable
.CurrPtr
);
2033 Data
= GetVariableDataPtr (CertDbVariable
.CurrPtr
);
2034 if ((DataSize
== 0) || (Data
== NULL
)) {
2036 return EFI_NOT_FOUND
;
2040 // Find whether matching cert node already exists in "certdb".
2041 // If yes return error.
2043 Status
= FindCertsFromDb (
2054 if (!EFI_ERROR (Status
)) {
2056 return EFI_ACCESS_DENIED
;
2060 // Construct new data content of variable "certdb".
2062 NameSize
= (UINT32
) StrLen (VariableName
);
2063 CertNodeSize
= sizeof (AUTH_CERT_DB_DATA
) + (UINT32
) CertDataSize
+ NameSize
* sizeof (CHAR16
);
2064 NewCertDbSize
= (UINT32
) DataSize
+ CertNodeSize
;
2065 if (NewCertDbSize
> mMaxCertDbSize
) {
2066 return EFI_OUT_OF_RESOURCES
;
2068 NewCertDb
= (UINT8
*) mCertDbStore
;
2071 // Copy the DB entries before deleting node.
2073 CopyMem (NewCertDb
, Data
, DataSize
);
2075 // Update CertDbListSize.
2077 CopyMem (NewCertDb
, &NewCertDbSize
, sizeof (UINT32
));
2079 // Construct new cert node.
2081 Ptr
= (AUTH_CERT_DB_DATA
*) (NewCertDb
+ DataSize
);
2082 CopyGuid (&Ptr
->VendorGuid
, VendorGuid
);
2083 CopyMem (&Ptr
->CertNodeSize
, &CertNodeSize
, sizeof (UINT32
));
2084 CopyMem (&Ptr
->NameSize
, &NameSize
, sizeof (UINT32
));
2085 CopyMem (&Ptr
->CertDataSize
, &CertDataSize
, sizeof (UINT32
));
2088 (UINT8
*) Ptr
+ sizeof (AUTH_CERT_DB_DATA
),
2090 NameSize
* sizeof (CHAR16
)
2094 (UINT8
*) Ptr
+ sizeof (AUTH_CERT_DB_DATA
) + NameSize
* sizeof (CHAR16
),
2102 VarAttr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
2103 Status
= UpdateVariable (
2119 Process variable with EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set
2121 Caution: This function may receive untrusted input.
2122 This function may be invoked in SMM mode, and datasize and data are external input.
2123 This function will do basic validation, before parse the data.
2124 This function will parse the authentication carefully to avoid security issues, like
2125 buffer overflow, integer overflow.
2127 @param[in] VariableName Name of Variable to be found.
2128 @param[in] VendorGuid Variable vendor GUID.
2129 @param[in] Data Data pointer.
2130 @param[in] DataSize Size of Data found. If size is less than the
2131 data, this value contains the required size.
2132 @param[in] Variable The variable information which is used to keep track of variable usage.
2133 @param[in] Attributes Attribute value of the variable.
2134 @param[in] AuthVarType Verify against PK, KEK database, private database or certificate in data payload.
2135 @param[out] VarDel Delete the variable or not.
2137 @retval EFI_INVALID_PARAMETER Invalid parameter.
2138 @retval EFI_SECURITY_VIOLATION The variable does NOT pass the validation
2139 check carried out by the firmware.
2140 @retval EFI_OUT_OF_RESOURCES Failed to process variable due to lack
2142 @retval EFI_SUCCESS Variable pass validation successfully.
2146 VerifyTimeBasedPayload (
2147 IN CHAR16
*VariableName
,
2148 IN EFI_GUID
*VendorGuid
,
2151 IN VARIABLE_POINTER_TRACK
*Variable
,
2152 IN UINT32 Attributes
,
2153 IN AUTHVAR_TYPE AuthVarType
,
2167 BOOLEAN VerifyStatus
;
2169 EFI_SIGNATURE_LIST
*CertList
;
2170 EFI_SIGNATURE_DATA
*Cert
;
2171 VARIABLE_POINTER_TRACK KekVariable
;
2172 EFI_VARIABLE_AUTHENTICATION_2
*CertData
;
2175 VARIABLE_POINTER_TRACK PkVariable
;
2179 UINTN CertStackSize
;
2180 UINT8
*CertsInCertDb
;
2181 UINT32 CertsSizeinDb
;
2183 VerifyStatus
= FALSE
;
2189 CertsInCertDb
= NULL
;
2192 // When the attribute EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS is
2193 // set, then the Data buffer shall begin with an instance of a complete (and serialized)
2194 // EFI_VARIABLE_AUTHENTICATION_2 descriptor. The descriptor shall be followed by the new
2195 // variable value and DataSize shall reflect the combined size of the descriptor and the new
2196 // variable value. The authentication descriptor is not part of the variable data and is not
2197 // returned by subsequent calls to GetVariable().
2199 CertData
= (EFI_VARIABLE_AUTHENTICATION_2
*) Data
;
2202 // Verify that Pad1, Nanosecond, TimeZone, Daylight and Pad2 components of the
2203 // TimeStamp value are set to zero.
2205 if ((CertData
->TimeStamp
.Pad1
!= 0) ||
2206 (CertData
->TimeStamp
.Nanosecond
!= 0) ||
2207 (CertData
->TimeStamp
.TimeZone
!= 0) ||
2208 (CertData
->TimeStamp
.Daylight
!= 0) ||
2209 (CertData
->TimeStamp
.Pad2
!= 0)) {
2210 return EFI_SECURITY_VIOLATION
;
2213 if ((Variable
->CurrPtr
!= NULL
) && ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0)) {
2214 if (CompareTimeStamp (&CertData
->TimeStamp
, &Variable
->CurrPtr
->TimeStamp
)) {
2216 // TimeStamp check fail, suspicious replay attack, return EFI_SECURITY_VIOLATION.
2218 return EFI_SECURITY_VIOLATION
;
2223 // wCertificateType should be WIN_CERT_TYPE_EFI_GUID.
2224 // Cert type should be EFI_CERT_TYPE_PKCS7_GUID.
2226 if ((CertData
->AuthInfo
.Hdr
.wCertificateType
!= WIN_CERT_TYPE_EFI_GUID
) ||
2227 !CompareGuid (&CertData
->AuthInfo
.CertType
, &gEfiCertPkcs7Guid
)) {
2229 // Invalid AuthInfo type, return EFI_SECURITY_VIOLATION.
2231 return EFI_SECURITY_VIOLATION
;
2235 // Find out Pkcs7 SignedData which follows the EFI_VARIABLE_AUTHENTICATION_2 descriptor.
2236 // AuthInfo.Hdr.dwLength is the length of the entire certificate, including the length of the header.
2238 SigData
= CertData
->AuthInfo
.CertData
;
2239 SigDataSize
= CertData
->AuthInfo
.Hdr
.dwLength
- (UINT32
) (OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID
, CertData
));
2242 // Find out the new data payload which follows Pkcs7 SignedData directly.
2244 PayloadPtr
= SigData
+ SigDataSize
;
2245 PayloadSize
= DataSize
- OFFSET_OF_AUTHINFO2_CERT_DATA
- (UINTN
) SigDataSize
;
2248 // Construct a serialization buffer of the values of the VariableName, VendorGuid and Attributes
2249 // parameters of the SetVariable() call and the TimeStamp component of the
2250 // EFI_VARIABLE_AUTHENTICATION_2 descriptor followed by the variable's new value
2251 // i.e. (VariableName, VendorGuid, Attributes, TimeStamp, Data)
2253 NewDataSize
= PayloadSize
+ sizeof (EFI_TIME
) + sizeof (UINT32
) +
2254 sizeof (EFI_GUID
) + StrSize (VariableName
) - sizeof (CHAR16
);
2256 // Here is to reuse scratch data area(at the end of volatile variable store)
2257 // to reduce SMRAM consumption for SMM variable driver.
2258 // The scratch buffer is enough to hold the serialized data and safe to use,
2259 // because it will be used at here to do verification only first
2260 // and then used in UpdateVariable() for a time based auth variable set.
2262 NewData
= (UINT8
*) GetEndPointer ((VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
));
2265 Length
= StrLen (VariableName
) * sizeof (CHAR16
);
2266 CopyMem (Buffer
, VariableName
, Length
);
2269 Length
= sizeof (EFI_GUID
);
2270 CopyMem (Buffer
, VendorGuid
, Length
);
2273 Length
= sizeof (UINT32
);
2274 CopyMem (Buffer
, &Attr
, Length
);
2277 Length
= sizeof (EFI_TIME
);
2278 CopyMem (Buffer
, &CertData
->TimeStamp
, Length
);
2281 CopyMem (Buffer
, PayloadPtr
, PayloadSize
);
2283 if (AuthVarType
== AuthVarTypePk
) {
2285 // Verify that the signature has been made with the current Platform Key (no chaining for PK).
2286 // First, get signer's certificates from SignedData.
2288 VerifyStatus
= Pkcs7GetSigners (
2296 if (!VerifyStatus
) {
2301 // Second, get the current platform key from variable. Check whether it's identical with signer's certificates
2302 // in SignedData. If not, return error immediately.
2304 Status
= FindVariable (
2305 EFI_PLATFORM_KEY_NAME
,
2306 &gEfiGlobalVariableGuid
,
2308 &mVariableModuleGlobal
->VariableGlobal
,
2311 if (EFI_ERROR (Status
)) {
2312 VerifyStatus
= FALSE
;
2315 CertList
= (EFI_SIGNATURE_LIST
*) GetVariableDataPtr (PkVariable
.CurrPtr
);
2316 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
2317 if ((RootCertSize
!= (CertList
->SignatureSize
- (sizeof (EFI_SIGNATURE_DATA
) - 1))) ||
2318 (CompareMem (Cert
->SignatureData
, RootCert
, RootCertSize
) != 0)) {
2319 VerifyStatus
= FALSE
;
2324 // Verify Pkcs7 SignedData via Pkcs7Verify library.
2326 VerifyStatus
= Pkcs7Verify (
2335 } else if (AuthVarType
== AuthVarTypeKek
) {
2338 // Get KEK database from variable.
2340 Status
= FindVariable (
2341 EFI_KEY_EXCHANGE_KEY_NAME
,
2342 &gEfiGlobalVariableGuid
,
2344 &mVariableModuleGlobal
->VariableGlobal
,
2347 if (EFI_ERROR (Status
)) {
2352 // Ready to verify Pkcs7 SignedData. Go through KEK Signature Database to find out X.509 CertList.
2354 KekDataSize
= KekVariable
.CurrPtr
->DataSize
;
2355 CertList
= (EFI_SIGNATURE_LIST
*) GetVariableDataPtr (KekVariable
.CurrPtr
);
2356 while ((KekDataSize
> 0) && (KekDataSize
>= CertList
->SignatureListSize
)) {
2357 if (CompareGuid (&CertList
->SignatureType
, &gEfiCertX509Guid
)) {
2358 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
2359 CertCount
= (CertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - CertList
->SignatureHeaderSize
) / CertList
->SignatureSize
;
2360 for (Index
= 0; Index
< CertCount
; Index
++) {
2362 // Iterate each Signature Data Node within this CertList for a verify
2364 RootCert
= Cert
->SignatureData
;
2365 RootCertSize
= CertList
->SignatureSize
- (sizeof (EFI_SIGNATURE_DATA
) - 1);
2368 // Verify Pkcs7 SignedData via Pkcs7Verify library.
2370 VerifyStatus
= Pkcs7Verify (
2381 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) Cert
+ CertList
->SignatureSize
);
2384 KekDataSize
-= CertList
->SignatureListSize
;
2385 CertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) CertList
+ CertList
->SignatureListSize
);
2387 } else if (AuthVarType
== AuthVarTypePriv
) {
2390 // Process common authenticated variable except PK/KEK/DB/DBX/DBT.
2391 // Get signer's certificates from SignedData.
2393 VerifyStatus
= Pkcs7GetSigners (
2401 if (!VerifyStatus
) {
2406 // Get previously stored signer's certificates from certdb for existing
2407 // variable. Check whether they are identical with signer's certificates
2408 // in SignedData. If not, return error immediately.
2410 if ((Variable
->CurrPtr
!= NULL
)) {
2411 VerifyStatus
= FALSE
;
2413 Status
= GetCertsFromDb (VariableName
, VendorGuid
, &CertsInCertDb
, &CertsSizeinDb
);
2414 if (EFI_ERROR (Status
)) {
2418 if ((CertStackSize
!= CertsSizeinDb
) ||
2419 (CompareMem (SignerCerts
, CertsInCertDb
, CertsSizeinDb
) != 0)) {
2424 VerifyStatus
= Pkcs7Verify (
2432 if (!VerifyStatus
) {
2437 // Delete signer's certificates when delete the common authenticated variable.
2439 if ((PayloadSize
== 0) && (Variable
->CurrPtr
!= NULL
) && ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0)) {
2440 Status
= DeleteCertsFromDb (VariableName
, VendorGuid
);
2441 if (EFI_ERROR (Status
)) {
2442 VerifyStatus
= FALSE
;
2445 } else if (Variable
->CurrPtr
== NULL
&& PayloadSize
!= 0) {
2447 // Insert signer's certificates when adding a new common authenticated variable.
2449 Status
= InsertCertsToDb (VariableName
, VendorGuid
, SignerCerts
, CertStackSize
);
2450 if (EFI_ERROR (Status
)) {
2451 VerifyStatus
= FALSE
;
2455 } else if (AuthVarType
== AuthVarTypePayload
) {
2456 CertList
= (EFI_SIGNATURE_LIST
*) PayloadPtr
;
2457 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
2458 RootCert
= Cert
->SignatureData
;
2459 RootCertSize
= CertList
->SignatureSize
- (sizeof (EFI_SIGNATURE_DATA
) - 1);
2461 // Verify Pkcs7 SignedData via Pkcs7Verify library.
2463 VerifyStatus
= Pkcs7Verify (
2472 return EFI_SECURITY_VIOLATION
;
2477 if (AuthVarType
== AuthVarTypePk
|| AuthVarType
== AuthVarTypePriv
) {
2478 Pkcs7FreeSigners (RootCert
);
2479 Pkcs7FreeSigners (SignerCerts
);
2482 if (!VerifyStatus
) {
2483 return EFI_SECURITY_VIOLATION
;
2486 Status
= CheckSignatureListFormat(VariableName
, VendorGuid
, PayloadPtr
, PayloadSize
);
2487 if (EFI_ERROR (Status
)) {
2491 if ((PayloadSize
== 0) && (VarDel
!= NULL
)) {
2496 // Final step: Update/Append Variable if it pass Pkcs7Verify
2498 return UpdateVariable (
2507 &CertData
->TimeStamp