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 if (mPlatformMode
== SETUP_MODE
){
282 // PK is cleared in runtime. "SecureBootMode" is not updated before reboot
283 // Delete "SecureBootMode" in SetupMode
285 Status
= UpdateVariable (
286 EFI_SECURE_BOOT_ENABLE_NAME
,
287 &gEfiSecureBootEnableDisableGuid
,
290 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
297 SecureBootEnable
= *(GetVariableDataPtr (Variable
.CurrPtr
));
299 } else if (mPlatformMode
== USER_MODE
) {
301 // "SecureBootEnable" not exist, initialize it in USER_MODE.
303 SecureBootEnable
= SECURE_BOOT_ENABLE
;
304 Status
= UpdateVariable (
305 EFI_SECURE_BOOT_ENABLE_NAME
,
306 &gEfiSecureBootEnableDisableGuid
,
309 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
315 if (EFI_ERROR (Status
)) {
321 // Create "SecureBoot" variable with BS+RT attribute set.
323 if (SecureBootEnable
== SECURE_BOOT_ENABLE
&& mPlatformMode
== USER_MODE
) {
324 SecureBootMode
= SECURE_BOOT_MODE_ENABLE
;
326 SecureBootMode
= SECURE_BOOT_MODE_DISABLE
;
328 FindVariable (EFI_SECURE_BOOT_MODE_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
329 Status
= UpdateVariable (
330 EFI_SECURE_BOOT_MODE_NAME
,
331 &gEfiGlobalVariableGuid
,
334 EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
340 if (EFI_ERROR (Status
)) {
344 DEBUG ((EFI_D_INFO
, "Variable %s is %x\n", EFI_SETUP_MODE_NAME
, mPlatformMode
));
345 DEBUG ((EFI_D_INFO
, "Variable %s is %x\n", EFI_SECURE_BOOT_MODE_NAME
, SecureBootMode
));
346 DEBUG ((EFI_D_INFO
, "Variable %s is %x\n", EFI_SECURE_BOOT_ENABLE_NAME
, SecureBootEnable
));
349 // Initialize "CustomMode" in STANDARD_SECURE_BOOT_MODE state.
351 FindVariable (EFI_CUSTOM_MODE_NAME
, &gEfiCustomModeEnableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
352 CustomMode
= STANDARD_SECURE_BOOT_MODE
;
353 Status
= UpdateVariable (
354 EFI_CUSTOM_MODE_NAME
,
355 &gEfiCustomModeEnableGuid
,
358 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
364 if (EFI_ERROR (Status
)) {
368 DEBUG ((EFI_D_INFO
, "Variable %s is %x\n", EFI_CUSTOM_MODE_NAME
, CustomMode
));
371 // Check "certdb" variable's existence.
372 // If it doesn't exist, then create a new one with
373 // EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set.
375 Status
= FindVariable (
379 &mVariableModuleGlobal
->VariableGlobal
,
383 if (Variable
.CurrPtr
== NULL
) {
384 VarAttr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
385 ListSize
= sizeof (UINT32
);
386 Status
= UpdateVariable (
397 if (EFI_ERROR (Status
)) {
403 // Check "VendorKeysNv" variable's existence and create "VendorKeys" variable accordingly.
405 FindVariable (EFI_VENDOR_KEYS_NV_VARIABLE_NAME
, &gEfiVendorKeysNvGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
406 if (Variable
.CurrPtr
!= NULL
) {
407 mVendorKeyState
= *(GetVariableDataPtr (Variable
.CurrPtr
));
410 // "VendorKeysNv" not exist, initialize it in VENDOR_KEYS_VALID state.
412 mVendorKeyState
= VENDOR_KEYS_VALID
;
413 Status
= UpdateVariable (
414 EFI_VENDOR_KEYS_NV_VARIABLE_NAME
,
415 &gEfiVendorKeysNvGuid
,
418 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
,
424 if (EFI_ERROR (Status
)) {
430 // Create "VendorKeys" variable with BS+RT attribute set.
432 FindVariable (EFI_VENDOR_KEYS_VARIABLE_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
433 Status
= UpdateVariable (
434 EFI_VENDOR_KEYS_VARIABLE_NAME
,
435 &gEfiGlobalVariableGuid
,
438 EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
444 if (EFI_ERROR (Status
)) {
448 DEBUG ((EFI_D_INFO
, "Variable %s is %x\n", EFI_VENDOR_KEYS_VARIABLE_NAME
, mVendorKeyState
));
454 Add public key in store and return its index.
456 @param[in] PubKey Input pointer to Public Key data
457 @param[in] VariableDataEntry The variable data entry
459 @return Index of new added item
465 IN VARIABLE_ENTRY_CONSISTENCY
*VariableDataEntry
471 VARIABLE_POINTER_TRACK Variable
;
475 VARIABLE_ENTRY_CONSISTENCY PublicKeyEntry
;
478 if (PubKey
== NULL
) {
482 Status
= FindVariable (
484 &gEfiAuthenticatedVariableGuid
,
486 &mVariableModuleGlobal
->VariableGlobal
,
489 if (EFI_ERROR (Status
)) {
490 DEBUG ((EFI_D_ERROR
, "Get public key database variable failure, Status = %r\n", Status
));
495 // Check whether the public key entry does exist.
498 for (Ptr
= mPubKeyStore
, Index
= 1; Index
<= mPubKeyNumber
; Index
++) {
499 if (CompareMem (Ptr
, PubKey
, EFI_CERT_TYPE_RSA2048_SIZE
) == 0) {
503 Ptr
+= EFI_CERT_TYPE_RSA2048_SIZE
;
508 // Add public key in database.
510 if (mPubKeyNumber
== mMaxKeyNumber
) {
512 // Public key dadatase is full, try to reclaim invalid key.
516 // NV storage can't reclaim at runtime.
522 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
523 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
530 if (EFI_ERROR (Status
)) {
534 Status
= FindVariable (
536 &gEfiAuthenticatedVariableGuid
,
538 &mVariableModuleGlobal
->VariableGlobal
,
541 if (EFI_ERROR (Status
)) {
542 DEBUG ((EFI_D_ERROR
, "Get public key database variable failure, Status = %r\n", Status
));
546 DataSize
= DataSizeOfVariable (Variable
.CurrPtr
);
547 Data
= GetVariableDataPtr (Variable
.CurrPtr
);
548 ASSERT ((DataSize
!= 0) && (Data
!= NULL
));
550 // "AuthVarKeyDatabase" is an internal used variable. Its DataSize is always ensured not to exceed mPubKeyStore buffer size(See definition before)
551 // Therefore, there is no memory overflow in underlying CopyMem.
553 CopyMem (mPubKeyStore
, (UINT8
*) Data
, DataSize
);
554 mPubKeyNumber
= (UINT32
) (DataSize
/ EFI_CERT_TYPE_RSA2048_SIZE
);
556 if (mPubKeyNumber
== mMaxKeyNumber
) {
562 // Check the variable space for both public key and variable data.
564 PublicKeyEntry
.VariableSize
= (mPubKeyNumber
+ 1) * EFI_CERT_TYPE_RSA2048_SIZE
;
565 PublicKeyEntry
.Guid
= &gEfiAuthenticatedVariableGuid
;
566 PublicKeyEntry
.Name
= AUTHVAR_KEYDB_NAME
;
567 Attributes
= VARIABLE_ATTRIBUTE_NV_BS_RT
| EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
;
569 if (!CheckRemainingSpaceForConsistency (Attributes
, &PublicKeyEntry
, VariableDataEntry
, NULL
)) {
571 // No enough variable space.
576 CopyMem (mPubKeyStore
+ mPubKeyNumber
* EFI_CERT_TYPE_RSA2048_SIZE
, PubKey
, EFI_CERT_TYPE_RSA2048_SIZE
);
577 Index
= ++mPubKeyNumber
;
579 // Update public key database variable.
581 Status
= UpdateVariable (
583 &gEfiAuthenticatedVariableGuid
,
585 mPubKeyNumber
* EFI_CERT_TYPE_RSA2048_SIZE
,
592 if (EFI_ERROR (Status
)) {
593 DEBUG ((EFI_D_ERROR
, "Update public key database variable failure, Status = %r\n", Status
));
602 Verify data payload with AuthInfo in EFI_CERT_TYPE_RSA2048_SHA256_GUID type.
603 Follow the steps in UEFI2.2.
605 Caution: This function may receive untrusted input.
606 This function may be invoked in SMM mode, and datasize and data are external input.
607 This function will do basic validation, before parse the data.
608 This function will parse the authentication carefully to avoid security issues, like
609 buffer overflow, integer overflow.
611 @param[in] Data Pointer to data with AuthInfo.
612 @param[in] DataSize Size of Data.
613 @param[in] PubKey Public key used for verification.
615 @retval EFI_INVALID_PARAMETER Invalid parameter.
616 @retval EFI_SECURITY_VIOLATION If authentication failed.
617 @retval EFI_SUCCESS Authentication successful.
621 VerifyCounterBasedPayload (
628 EFI_VARIABLE_AUTHENTICATION
*CertData
;
629 EFI_CERT_BLOCK_RSA_2048_SHA256
*CertBlock
;
630 UINT8 Digest
[SHA256_DIGEST_SIZE
];
634 PayloadSize
= DataSize
- AUTHINFO_SIZE
;
639 if (Data
== NULL
|| PubKey
== NULL
) {
640 return EFI_INVALID_PARAMETER
;
643 CertData
= (EFI_VARIABLE_AUTHENTICATION
*) Data
;
644 CertBlock
= (EFI_CERT_BLOCK_RSA_2048_SHA256
*) (CertData
->AuthInfo
.CertData
);
647 // wCertificateType should be WIN_CERT_TYPE_EFI_GUID.
648 // Cert type should be EFI_CERT_TYPE_RSA2048_SHA256_GUID.
650 if ((CertData
->AuthInfo
.Hdr
.wCertificateType
!= WIN_CERT_TYPE_EFI_GUID
) ||
651 !CompareGuid (&CertData
->AuthInfo
.CertType
, &gEfiCertTypeRsa2048Sha256Guid
)
654 // Invalid AuthInfo type, return EFI_SECURITY_VIOLATION.
656 return EFI_SECURITY_VIOLATION
;
659 // Hash data payload with SHA256.
661 ZeroMem (Digest
, SHA256_DIGEST_SIZE
);
662 Status
= Sha256Init (mHashCtx
);
666 Status
= Sha256Update (mHashCtx
, Data
+ AUTHINFO_SIZE
, PayloadSize
);
673 Status
= Sha256Update (mHashCtx
, &PayloadSize
, sizeof (UINTN
));
678 // Hash Monotonic Count.
680 Status
= Sha256Update (mHashCtx
, &CertData
->MonotonicCount
, sizeof (UINT64
));
684 Status
= Sha256Final (mHashCtx
, Digest
);
689 // Generate & Initialize RSA Context.
692 ASSERT (Rsa
!= NULL
);
694 // Set RSA Key Components.
695 // NOTE: Only N and E are needed to be set as RSA public key for signature verification.
697 Status
= RsaSetKey (Rsa
, RsaKeyN
, PubKey
, EFI_CERT_TYPE_RSA2048_SIZE
);
701 Status
= RsaSetKey (Rsa
, RsaKeyE
, mRsaE
, sizeof (mRsaE
));
706 // Verify the signature.
708 Status
= RsaPkcs1Verify (
712 CertBlock
->Signature
,
713 EFI_CERT_TYPE_RSA2048_SHA256_SIZE
723 return EFI_SECURITY_VIOLATION
;
728 Update platform mode.
730 @param[in] Mode SETUP_MODE or USER_MODE.
732 @return EFI_INVALID_PARAMETER Invalid parameter.
733 @return EFI_SUCCESS Update platform mode successfully.
742 VARIABLE_POINTER_TRACK Variable
;
743 UINT8 SecureBootMode
;
744 UINT8 SecureBootEnable
;
745 UINTN VariableDataSize
;
747 Status
= FindVariable (
749 &gEfiGlobalVariableGuid
,
751 &mVariableModuleGlobal
->VariableGlobal
,
754 if (EFI_ERROR (Status
)) {
759 // Update the value of SetupMode variable by a simple mem copy, this could avoid possible
760 // variable storage reclaim at runtime.
762 mPlatformMode
= (UINT8
) Mode
;
763 CopyMem (GetVariableDataPtr (Variable
.CurrPtr
), &mPlatformMode
, sizeof(UINT8
));
767 // SecureBoot Variable indicates whether the platform firmware is operating
768 // in Secure boot mode (1) or not (0), so we should not change SecureBoot
769 // Variable in runtime.
775 // Check "SecureBoot" variable's existence.
776 // If it doesn't exist, firmware has no capability to perform driver signing verification,
777 // then set "SecureBoot" to 0.
779 Status
= FindVariable (
780 EFI_SECURE_BOOT_MODE_NAME
,
781 &gEfiGlobalVariableGuid
,
783 &mVariableModuleGlobal
->VariableGlobal
,
787 // If "SecureBoot" variable exists, then check "SetupMode" variable update.
788 // If "SetupMode" variable is USER_MODE, "SecureBoot" variable is set to 1.
789 // If "SetupMode" variable is SETUP_MODE, "SecureBoot" variable is set to 0.
791 if (Variable
.CurrPtr
== NULL
) {
792 SecureBootMode
= SECURE_BOOT_MODE_DISABLE
;
794 if (mPlatformMode
== USER_MODE
) {
795 SecureBootMode
= SECURE_BOOT_MODE_ENABLE
;
796 } else if (mPlatformMode
== SETUP_MODE
) {
797 SecureBootMode
= SECURE_BOOT_MODE_DISABLE
;
799 return EFI_NOT_FOUND
;
803 Status
= UpdateVariable (
804 EFI_SECURE_BOOT_MODE_NAME
,
805 &gEfiGlobalVariableGuid
,
808 EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
814 if (EFI_ERROR (Status
)) {
819 // Check "SecureBootEnable" variable's existence. It can enable/disable secure boot feature.
821 Status
= FindVariable (
822 EFI_SECURE_BOOT_ENABLE_NAME
,
823 &gEfiSecureBootEnableDisableGuid
,
825 &mVariableModuleGlobal
->VariableGlobal
,
829 if (SecureBootMode
== SECURE_BOOT_MODE_ENABLE
) {
831 // Create the "SecureBootEnable" variable as secure boot is enabled.
833 SecureBootEnable
= SECURE_BOOT_ENABLE
;
834 VariableDataSize
= sizeof (SecureBootEnable
);
837 // Delete the "SecureBootEnable" variable if this variable exist as "SecureBoot"
838 // variable is not in secure boot state.
840 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
843 SecureBootEnable
= SECURE_BOOT_DISABLE
;
844 VariableDataSize
= 0;
847 Status
= UpdateVariable (
848 EFI_SECURE_BOOT_ENABLE_NAME
,
849 &gEfiSecureBootEnableDisableGuid
,
852 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
862 Check input data form to make sure it is a valid EFI_SIGNATURE_LIST for PK/KEK/db/dbx/dbt variable.
864 @param[in] VariableName Name of Variable to be check.
865 @param[in] VendorGuid Variable vendor GUID.
866 @param[in] Data Point to the variable data to be checked.
867 @param[in] DataSize Size of Data.
869 @return EFI_INVALID_PARAMETER Invalid signature list format.
870 @return EFI_SUCCESS Passed signature list format check successfully.
874 CheckSignatureListFormat(
875 IN CHAR16
*VariableName
,
876 IN EFI_GUID
*VendorGuid
,
881 EFI_SIGNATURE_LIST
*SigList
;
887 EFI_SIGNATURE_DATA
*CertData
;
894 ASSERT (VariableName
!= NULL
&& VendorGuid
!= NULL
&& Data
!= NULL
);
896 if (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, EFI_PLATFORM_KEY_NAME
) == 0)){
898 } else if ((CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, EFI_KEY_EXCHANGE_KEY_NAME
) == 0)) ||
899 (CompareGuid (VendorGuid
, &gEfiImageSecurityDatabaseGuid
) &&
900 ((StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE
) == 0) || (StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE1
) == 0) ||
901 (StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE2
) == 0)))) {
908 SigList
= (EFI_SIGNATURE_LIST
*) Data
;
909 SigDataSize
= DataSize
;
913 // Walk throuth the input signature list and check the data format.
914 // If any signature is incorrectly formed, the whole check will fail.
916 while ((SigDataSize
> 0) && (SigDataSize
>= SigList
->SignatureListSize
)) {
917 for (Index
= 0; Index
< (sizeof (mSupportSigItem
) / sizeof (EFI_SIGNATURE_ITEM
)); Index
++ ) {
918 if (CompareGuid (&SigList
->SignatureType
, &mSupportSigItem
[Index
].SigType
)) {
920 // The value of SignatureSize should always be 16 (size of SignatureOwner
921 // component) add the data length according to signature type.
923 if (mSupportSigItem
[Index
].SigDataSize
!= ((UINT32
) ~0) &&
924 (SigList
->SignatureSize
- sizeof (EFI_GUID
)) != mSupportSigItem
[Index
].SigDataSize
) {
925 return EFI_INVALID_PARAMETER
;
927 if (mSupportSigItem
[Index
].SigHeaderSize
!= ((UINTN
) ~0) &&
928 SigList
->SignatureHeaderSize
!= mSupportSigItem
[Index
].SigHeaderSize
) {
929 return EFI_INVALID_PARAMETER
;
935 if (Index
== (sizeof (mSupportSigItem
) / sizeof (EFI_SIGNATURE_ITEM
))) {
937 // Undefined signature type.
939 return EFI_INVALID_PARAMETER
;
942 if (CompareGuid (&SigList
->SignatureType
, &gEfiCertX509Guid
)) {
944 // Try to retrieve the RSA public key from the X.509 certificate.
945 // If this operation fails, it's not a valid certificate.
947 RsaContext
= RsaNew ();
948 if (RsaContext
== NULL
) {
949 return EFI_INVALID_PARAMETER
;
951 CertData
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) SigList
+ sizeof (EFI_SIGNATURE_LIST
) + SigList
->SignatureHeaderSize
);
952 CertLen
= SigList
->SignatureSize
- sizeof (EFI_GUID
);
953 if (!RsaGetPublicKeyFromX509 (CertData
->SignatureData
, CertLen
, &RsaContext
)) {
954 RsaFree (RsaContext
);
955 return EFI_INVALID_PARAMETER
;
957 RsaFree (RsaContext
);
960 if ((SigList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - SigList
->SignatureHeaderSize
) % SigList
->SignatureSize
!= 0) {
961 return EFI_INVALID_PARAMETER
;
963 SigCount
+= (SigList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - SigList
->SignatureHeaderSize
) / SigList
->SignatureSize
;
965 SigDataSize
-= SigList
->SignatureListSize
;
966 SigList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) SigList
+ SigList
->SignatureListSize
);
969 if (((UINTN
) SigList
- (UINTN
) Data
) != DataSize
) {
970 return EFI_INVALID_PARAMETER
;
973 if (IsPk
&& SigCount
> 1) {
974 return EFI_INVALID_PARAMETER
;
981 Update "VendorKeys" variable to record the out of band secure boot key modification.
983 @return EFI_SUCCESS Variable is updated successfully.
984 @return Others Failed to update variable.
988 VendorKeyIsModified (
993 VARIABLE_POINTER_TRACK Variable
;
995 if (mVendorKeyState
== VENDOR_KEYS_MODIFIED
) {
998 mVendorKeyState
= VENDOR_KEYS_MODIFIED
;
1000 FindVariable (EFI_VENDOR_KEYS_NV_VARIABLE_NAME
, &gEfiVendorKeysNvGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1001 Status
= UpdateVariable (
1002 EFI_VENDOR_KEYS_NV_VARIABLE_NAME
,
1003 &gEfiVendorKeysNvGuid
,
1006 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
,
1012 if (EFI_ERROR (Status
)) {
1016 FindVariable (EFI_VENDOR_KEYS_VARIABLE_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1017 return UpdateVariable (
1018 EFI_VENDOR_KEYS_VARIABLE_NAME
,
1019 &gEfiGlobalVariableGuid
,
1022 EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
1031 Process variable with platform key for verification.
1033 Caution: This function may receive untrusted input.
1034 This function may be invoked in SMM mode, and datasize and data are external input.
1035 This function will do basic validation, before parse the data.
1036 This function will parse the authentication carefully to avoid security issues, like
1037 buffer overflow, integer overflow.
1038 This function will check attribute carefully to avoid authentication bypass.
1040 @param[in] VariableName Name of Variable to be found.
1041 @param[in] VendorGuid Variable vendor GUID.
1042 @param[in] Data Data pointer.
1043 @param[in] DataSize Size of Data found. If size is less than the
1044 data, this value contains the required size.
1045 @param[in] Variable The variable information which is used to keep track of variable usage.
1046 @param[in] Attributes Attribute value of the variable
1047 @param[in] IsPk Indicate whether it is to process pk.
1049 @return EFI_INVALID_PARAMETER Invalid parameter.
1050 @return EFI_SECURITY_VIOLATION The variable does NOT pass the validation.
1051 check carried out by the firmware.
1052 @return EFI_SUCCESS Variable passed validation successfully.
1057 IN CHAR16
*VariableName
,
1058 IN EFI_GUID
*VendorGuid
,
1061 IN VARIABLE_POINTER_TRACK
*Variable
,
1062 IN UINT32 Attributes OPTIONAL
,
1071 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0 ||
1072 (Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) == 0) {
1074 // PK, KEK and db/dbx/dbt should set EFI_VARIABLE_NON_VOLATILE attribute and should be a time-based
1075 // authenticated variable.
1077 return EFI_INVALID_PARAMETER
;
1081 if ((InCustomMode() && UserPhysicalPresent()) || (mPlatformMode
== SETUP_MODE
&& !IsPk
)) {
1082 Payload
= (UINT8
*) Data
+ AUTHINFO2_SIZE (Data
);
1083 PayloadSize
= DataSize
- AUTHINFO2_SIZE (Data
);
1084 if (PayloadSize
== 0) {
1088 Status
= CheckSignatureListFormat(VariableName
, VendorGuid
, Payload
, PayloadSize
);
1089 if (EFI_ERROR (Status
)) {
1093 Status
= UpdateVariable (
1102 &((EFI_VARIABLE_AUTHENTICATION_2
*) Data
)->TimeStamp
1104 if (EFI_ERROR(Status
)) {
1108 if ((mPlatformMode
!= SETUP_MODE
) || IsPk
) {
1109 Status
= VendorKeyIsModified ();
1111 } else if (mPlatformMode
== USER_MODE
) {
1113 // Verify against X509 Cert in PK database.
1115 Status
= VerifyTimeBasedPayload (
1127 // Verify against the certificate in data payload.
1129 Status
= VerifyTimeBasedPayload (
1141 if (!EFI_ERROR(Status
) && IsPk
) {
1142 if (mPlatformMode
== SETUP_MODE
&& !Del
) {
1144 // If enroll PK in setup mode, need change to user mode.
1146 Status
= UpdatePlatformMode (USER_MODE
);
1147 } else if (mPlatformMode
== USER_MODE
&& Del
){
1149 // If delete PK in user mode, need change to setup mode.
1151 Status
= UpdatePlatformMode (SETUP_MODE
);
1159 Process variable with key exchange key for verification.
1161 Caution: This function may receive untrusted input.
1162 This function may be invoked in SMM mode, and datasize and data are external input.
1163 This function will do basic validation, before parse the data.
1164 This function will parse the authentication carefully to avoid security issues, like
1165 buffer overflow, integer overflow.
1166 This function will check attribute carefully to avoid authentication bypass.
1168 @param[in] VariableName Name of Variable to be found.
1169 @param[in] VendorGuid Variable vendor GUID.
1170 @param[in] Data Data pointer.
1171 @param[in] DataSize Size of Data found. If size is less than the
1172 data, this value contains the required size.
1173 @param[in] Variable The variable information which is used to keep track of variable usage.
1174 @param[in] Attributes Attribute value of the variable.
1176 @return EFI_INVALID_PARAMETER Invalid parameter.
1177 @return EFI_SECURITY_VIOLATION The variable does NOT pass the validation
1178 check carried out by the firmware.
1179 @return EFI_SUCCESS Variable pass validation successfully.
1184 IN CHAR16
*VariableName
,
1185 IN EFI_GUID
*VendorGuid
,
1188 IN VARIABLE_POINTER_TRACK
*Variable
,
1189 IN UINT32 Attributes OPTIONAL
1196 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0 ||
1197 (Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) == 0) {
1199 // DB, DBX and DBT should set EFI_VARIABLE_NON_VOLATILE attribute and should be a time-based
1200 // authenticated variable.
1202 return EFI_INVALID_PARAMETER
;
1205 Status
= EFI_SUCCESS
;
1206 if (mPlatformMode
== USER_MODE
&& !(InCustomMode() && UserPhysicalPresent())) {
1208 // Time-based, verify against X509 Cert KEK.
1210 return VerifyTimeBasedPayload (
1222 // If in setup mode or custom secure boot mode, no authentication needed.
1224 Payload
= (UINT8
*) Data
+ AUTHINFO2_SIZE (Data
);
1225 PayloadSize
= DataSize
- AUTHINFO2_SIZE (Data
);
1227 Status
= CheckSignatureListFormat(VariableName
, VendorGuid
, Payload
, PayloadSize
);
1228 if (EFI_ERROR (Status
)) {
1232 Status
= UpdateVariable (
1241 &((EFI_VARIABLE_AUTHENTICATION_2
*) Data
)->TimeStamp
1243 if (EFI_ERROR (Status
)) {
1247 if (mPlatformMode
!= SETUP_MODE
) {
1248 Status
= VendorKeyIsModified ();
1256 Check if it is to delete auth variable.
1258 @param[in] Data Data pointer.
1259 @param[in] DataSize Size of Data.
1260 @param[in] Variable The variable information which is used to keep track of variable usage.
1261 @param[in] Attributes Attribute value of the variable.
1263 @retval TRUE It is to delete auth variable.
1264 @retval FALSE It is not to delete auth variable.
1268 IsDeleteAuthVariable (
1271 IN VARIABLE_POINTER_TRACK
*Variable
,
1272 IN UINT32 Attributes
1281 // To delete a variable created with the EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
1282 // or the EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS attribute,
1283 // SetVariable must be used with attributes matching the existing variable
1284 // and the DataSize set to the size of the AuthInfo descriptor.
1286 if ((Variable
->CurrPtr
!= NULL
) &&
1287 (Attributes
== Variable
->CurrPtr
->Attributes
) &&
1288 ((Attributes
& (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
)) != 0)) {
1289 if ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) {
1290 PayloadSize
= DataSize
- AUTHINFO2_SIZE (Data
);
1291 if (PayloadSize
== 0) {
1295 PayloadSize
= DataSize
- AUTHINFO_SIZE
;
1296 if (PayloadSize
== 0) {
1306 Process variable with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS/EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set
1308 Caution: This function may receive untrusted input.
1309 This function may be invoked in SMM mode, and datasize and data are external input.
1310 This function will do basic validation, before parse the data.
1311 This function will parse the authentication carefully to avoid security issues, like
1312 buffer overflow, integer overflow.
1313 This function will check attribute carefully to avoid authentication bypass.
1315 @param[in] VariableName Name of Variable to be found.
1316 @param[in] VendorGuid Variable vendor GUID.
1318 @param[in] Data Data pointer.
1319 @param[in] DataSize Size of Data.
1320 @param[in] Variable The variable information which is used to keep track of variable usage.
1321 @param[in] Attributes Attribute value of the variable.
1323 @return EFI_INVALID_PARAMETER Invalid parameter.
1324 @return EFI_WRITE_PROTECTED Variable is write-protected and needs authentication with
1325 EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS set.
1326 @return EFI_OUT_OF_RESOURCES The Database to save the public key is full.
1327 @return EFI_SECURITY_VIOLATION The variable is with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
1328 set, but the AuthInfo does NOT pass the validation
1329 check carried out by the firmware.
1330 @return EFI_SUCCESS Variable is not write-protected or pass validation successfully.
1335 IN CHAR16
*VariableName
,
1336 IN EFI_GUID
*VendorGuid
,
1339 IN VARIABLE_POINTER_TRACK
*Variable
,
1340 IN UINT32 Attributes
1345 BOOLEAN IsFirstTime
;
1347 EFI_VARIABLE_AUTHENTICATION
*CertData
;
1348 EFI_CERT_BLOCK_RSA_2048_SHA256
*CertBlock
;
1350 UINT64 MonotonicCount
;
1351 VARIABLE_ENTRY_CONSISTENCY VariableDataEntry
;
1358 Status
= EFI_SUCCESS
;
1360 if (IsDeleteAuthVariable (Data
, DataSize
, Variable
, Attributes
) && UserPhysicalPresent()) {
1362 // Allow the delete operation of common authenticated variable at user physical presence.
1364 if ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) {
1365 Status
= DeleteCertsFromDb (VariableName
, VendorGuid
);
1367 if (!EFI_ERROR (Status
)) {
1368 Status
= UpdateVariable (
1383 if (NeedPhysicallyPresent (VariableName
, VendorGuid
) && !UserPhysicalPresent()) {
1385 // This variable is protected, only physical present user could modify its value.
1387 return EFI_SECURITY_VIOLATION
;
1391 // A time-based authenticated variable and a count-based authenticated variable
1392 // can't be updated by each other.
1394 if (Variable
->CurrPtr
!= NULL
) {
1395 if (((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) &&
1396 ((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0)) {
1397 return EFI_SECURITY_VIOLATION
;
1400 if (((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) &&
1401 ((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0)) {
1402 return EFI_SECURITY_VIOLATION
;
1407 // Process Time-based Authenticated variable.
1409 if ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) {
1410 return VerifyTimeBasedPayload (
1423 // Determine if first time SetVariable with the EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS.
1425 if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) {
1427 // Determine current operation type.
1429 if (DataSize
== AUTHINFO_SIZE
) {
1433 // Determine whether this is the first time with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS set.
1435 if (Variable
->CurrPtr
== NULL
) {
1437 } else if ((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) == 0) {
1440 KeyIndex
= Variable
->CurrPtr
->PubKeyIndex
;
1441 IsFirstTime
= FALSE
;
1443 } else if ((Variable
->CurrPtr
!= NULL
) &&
1444 ((Variable
->CurrPtr
->Attributes
& (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
)) != 0)
1447 // If the variable is already write-protected, it always needs authentication before update.
1449 return EFI_WRITE_PROTECTED
;
1452 // If without EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS, set and attributes collision.
1453 // That means it is not authenticated variable, just update variable as usual.
1455 Status
= UpdateVariable (VariableName
, VendorGuid
, Data
, DataSize
, Attributes
, 0, 0, Variable
, NULL
);
1460 // Get PubKey and check Monotonic Count value corresponding to the variable.
1462 CertData
= (EFI_VARIABLE_AUTHENTICATION
*) Data
;
1463 CertBlock
= (EFI_CERT_BLOCK_RSA_2048_SHA256
*) (CertData
->AuthInfo
.CertData
);
1464 PubKey
= CertBlock
->PublicKey
;
1467 // Update Monotonic Count value.
1469 MonotonicCount
= CertData
->MonotonicCount
;
1473 // 2 cases need to check here
1474 // 1. Internal PubKey variable. PubKeyIndex is always 0
1475 // 2. Other counter-based AuthVariable. Check input PubKey.
1477 if (KeyIndex
== 0 || CompareMem (PubKey
, mPubKeyStore
+ (KeyIndex
- 1) * EFI_CERT_TYPE_RSA2048_SIZE
, EFI_CERT_TYPE_RSA2048_SIZE
) != 0) {
1478 return EFI_SECURITY_VIOLATION
;
1481 // Compare the current monotonic count and ensure that it is greater than the last SetVariable
1482 // operation with the EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS attribute set.
1484 if (CertData
->MonotonicCount
<= Variable
->CurrPtr
->MonotonicCount
) {
1486 // Monotonic count check fail, suspicious replay attack, return EFI_SECURITY_VIOLATION.
1488 return EFI_SECURITY_VIOLATION
;
1492 // Verify the certificate in Data payload.
1494 Status
= VerifyCounterBasedPayload (Data
, DataSize
, PubKey
);
1495 if (EFI_ERROR (Status
)) {
1500 // Now, the signature has been verified!
1502 if (IsFirstTime
&& !IsDeletion
) {
1503 VariableDataEntry
.VariableSize
= DataSize
- AUTHINFO_SIZE
;
1504 VariableDataEntry
.Guid
= VendorGuid
;
1505 VariableDataEntry
.Name
= VariableName
;
1508 // Update public key database variable if need.
1510 KeyIndex
= AddPubKeyInStore (PubKey
, &VariableDataEntry
);
1511 if (KeyIndex
== 0) {
1512 return EFI_OUT_OF_RESOURCES
;
1517 // Verification pass.
1519 return UpdateVariable (VariableName
, VendorGuid
, (UINT8
*)Data
+ AUTHINFO_SIZE
, DataSize
- AUTHINFO_SIZE
, Attributes
, KeyIndex
, MonotonicCount
, Variable
, NULL
);
1523 Merge two buffers which formatted as EFI_SIGNATURE_LIST. Only the new EFI_SIGNATURE_DATA
1524 will be appended to the original EFI_SIGNATURE_LIST, duplicate EFI_SIGNATURE_DATA
1527 @param[in, out] Data Pointer to original EFI_SIGNATURE_LIST.
1528 @param[in] DataSize Size of Data buffer.
1529 @param[in] FreeBufSize Size of free data buffer
1530 @param[in] NewData Pointer to new EFI_SIGNATURE_LIST to be appended.
1531 @param[in] NewDataSize Size of NewData buffer.
1532 @param[out] MergedBufSize Size of the merged buffer
1534 @return EFI_BUFFER_TOO_SMALL if input Data buffer overflowed
1538 AppendSignatureList (
1541 IN UINTN FreeBufSize
,
1543 IN UINTN NewDataSize
,
1544 OUT UINTN
*MergedBufSize
1547 EFI_SIGNATURE_LIST
*CertList
;
1548 EFI_SIGNATURE_DATA
*Cert
;
1550 EFI_SIGNATURE_LIST
*NewCertList
;
1551 EFI_SIGNATURE_DATA
*NewCert
;
1558 UINTN SignatureListSize
;
1561 Tail
= (UINT8
*) Data
+ DataSize
;
1563 NewCertList
= (EFI_SIGNATURE_LIST
*) NewData
;
1564 while ((NewDataSize
> 0) && (NewDataSize
>= NewCertList
->SignatureListSize
)) {
1565 NewCert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) NewCertList
+ sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
);
1566 NewCertCount
= (NewCertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - NewCertList
->SignatureHeaderSize
) / NewCertList
->SignatureSize
;
1569 for (Index
= 0; Index
< NewCertCount
; Index
++) {
1573 CertList
= (EFI_SIGNATURE_LIST
*) Data
;
1574 while ((Size
> 0) && (Size
>= CertList
->SignatureListSize
)) {
1575 if (CompareGuid (&CertList
->SignatureType
, &NewCertList
->SignatureType
) &&
1576 (CertList
->SignatureSize
== NewCertList
->SignatureSize
)) {
1577 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
1578 CertCount
= (CertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - CertList
->SignatureHeaderSize
) / CertList
->SignatureSize
;
1579 for (Index2
= 0; Index2
< CertCount
; Index2
++) {
1581 // Iterate each Signature Data in this Signature List.
1583 if (CompareMem (NewCert
, Cert
, CertList
->SignatureSize
) == 0) {
1587 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) Cert
+ CertList
->SignatureSize
);
1594 Size
-= CertList
->SignatureListSize
;
1595 CertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) CertList
+ CertList
->SignatureListSize
);
1600 // New EFI_SIGNATURE_DATA, append it.
1602 if (CopiedCount
== 0) {
1603 if (FreeBufSize
< sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
) {
1604 return EFI_BUFFER_TOO_SMALL
;
1608 // Copy EFI_SIGNATURE_LIST header for only once.
1611 CopyMem (Tail
, NewCertList
, sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
);
1612 Tail
= Tail
+ sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
;
1613 FreeBufSize
-= sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
;
1616 if (FreeBufSize
< NewCertList
->SignatureSize
) {
1617 return EFI_BUFFER_TOO_SMALL
;
1619 CopyMem (Tail
, NewCert
, NewCertList
->SignatureSize
);
1620 Tail
+= NewCertList
->SignatureSize
;
1621 FreeBufSize
-= NewCertList
->SignatureSize
;
1625 NewCert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) NewCert
+ NewCertList
->SignatureSize
);
1629 // Update SignatureListSize in newly appended EFI_SIGNATURE_LIST.
1631 if (CopiedCount
!= 0) {
1632 SignatureListSize
= sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
+ (CopiedCount
* NewCertList
->SignatureSize
);
1633 CertList
= (EFI_SIGNATURE_LIST
*) (Tail
- SignatureListSize
);
1634 CertList
->SignatureListSize
= (UINT32
) SignatureListSize
;
1637 NewDataSize
-= NewCertList
->SignatureListSize
;
1638 NewCertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) NewCertList
+ NewCertList
->SignatureListSize
);
1641 *MergedBufSize
= (Tail
- (UINT8
*) Data
);
1646 Compare two EFI_TIME data.
1649 @param FirstTime A pointer to the first EFI_TIME data.
1650 @param SecondTime A pointer to the second EFI_TIME data.
1652 @retval TRUE The FirstTime is not later than the SecondTime.
1653 @retval FALSE The FirstTime is later than the SecondTime.
1658 IN EFI_TIME
*FirstTime
,
1659 IN EFI_TIME
*SecondTime
1662 if (FirstTime
->Year
!= SecondTime
->Year
) {
1663 return (BOOLEAN
) (FirstTime
->Year
< SecondTime
->Year
);
1664 } else if (FirstTime
->Month
!= SecondTime
->Month
) {
1665 return (BOOLEAN
) (FirstTime
->Month
< SecondTime
->Month
);
1666 } else if (FirstTime
->Day
!= SecondTime
->Day
) {
1667 return (BOOLEAN
) (FirstTime
->Day
< SecondTime
->Day
);
1668 } else if (FirstTime
->Hour
!= SecondTime
->Hour
) {
1669 return (BOOLEAN
) (FirstTime
->Hour
< SecondTime
->Hour
);
1670 } else if (FirstTime
->Minute
!= SecondTime
->Minute
) {
1671 return (BOOLEAN
) (FirstTime
->Minute
< SecondTime
->Minute
);
1674 return (BOOLEAN
) (FirstTime
->Second
<= SecondTime
->Second
);
1678 Find matching signer's certificates for common authenticated variable
1679 by corresponding VariableName and VendorGuid from "certdb".
1681 The data format of "certdb":
1683 // UINT32 CertDbListSize;
1684 // /// AUTH_CERT_DB_DATA Certs1[];
1685 // /// AUTH_CERT_DB_DATA Certs2[];
1687 // /// AUTH_CERT_DB_DATA Certsn[];
1690 @param[in] VariableName Name of authenticated Variable.
1691 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1692 @param[in] Data Pointer to variable "certdb".
1693 @param[in] DataSize Size of variable "certdb".
1694 @param[out] CertOffset Offset of matching CertData, from starting of Data.
1695 @param[out] CertDataSize Length of CertData in bytes.
1696 @param[out] CertNodeOffset Offset of matching AUTH_CERT_DB_DATA , from
1698 @param[out] CertNodeSize Length of AUTH_CERT_DB_DATA in bytes.
1700 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1701 @retval EFI_NOT_FOUND Fail to find matching certs.
1702 @retval EFI_SUCCESS Find matching certs and output parameters.
1707 IN CHAR16
*VariableName
,
1708 IN EFI_GUID
*VendorGuid
,
1711 OUT UINT32
*CertOffset
, OPTIONAL
1712 OUT UINT32
*CertDataSize
, OPTIONAL
1713 OUT UINT32
*CertNodeOffset
,OPTIONAL
1714 OUT UINT32
*CertNodeSize OPTIONAL
1718 AUTH_CERT_DB_DATA
*Ptr
;
1722 UINT32 CertDbListSize
;
1724 if ((VariableName
== NULL
) || (VendorGuid
== NULL
) || (Data
== NULL
)) {
1725 return EFI_INVALID_PARAMETER
;
1729 // Check whether DataSize matches recorded CertDbListSize.
1731 if (DataSize
< sizeof (UINT32
)) {
1732 return EFI_INVALID_PARAMETER
;
1735 CertDbListSize
= ReadUnaligned32 ((UINT32
*) Data
);
1737 if (CertDbListSize
!= (UINT32
) DataSize
) {
1738 return EFI_INVALID_PARAMETER
;
1741 Offset
= sizeof (UINT32
);
1744 // Get corresponding certificates by VendorGuid and VariableName.
1746 while (Offset
< (UINT32
) DataSize
) {
1747 Ptr
= (AUTH_CERT_DB_DATA
*) (Data
+ Offset
);
1749 // Check whether VendorGuid matches.
1751 if (CompareGuid (&Ptr
->VendorGuid
, VendorGuid
)) {
1752 NodeSize
= ReadUnaligned32 (&Ptr
->CertNodeSize
);
1753 NameSize
= ReadUnaligned32 (&Ptr
->NameSize
);
1754 CertSize
= ReadUnaligned32 (&Ptr
->CertDataSize
);
1756 if (NodeSize
!= sizeof (EFI_GUID
) + sizeof (UINT32
) * 3 + CertSize
+
1757 sizeof (CHAR16
) * NameSize
) {
1758 return EFI_INVALID_PARAMETER
;
1761 Offset
= Offset
+ sizeof (EFI_GUID
) + sizeof (UINT32
) * 3;
1763 // Check whether VariableName matches.
1765 if ((NameSize
== StrLen (VariableName
)) &&
1766 (CompareMem (Data
+ Offset
, VariableName
, NameSize
* sizeof (CHAR16
)) == 0)) {
1767 Offset
= Offset
+ NameSize
* sizeof (CHAR16
);
1769 if (CertOffset
!= NULL
) {
1770 *CertOffset
= Offset
;
1773 if (CertDataSize
!= NULL
) {
1774 *CertDataSize
= CertSize
;
1777 if (CertNodeOffset
!= NULL
) {
1778 *CertNodeOffset
= (UINT32
) ((UINT8
*) Ptr
- Data
);
1781 if (CertNodeSize
!= NULL
) {
1782 *CertNodeSize
= NodeSize
;
1787 Offset
= Offset
+ NameSize
* sizeof (CHAR16
) + CertSize
;
1790 NodeSize
= ReadUnaligned32 (&Ptr
->CertNodeSize
);
1791 Offset
= Offset
+ NodeSize
;
1795 return EFI_NOT_FOUND
;
1799 Retrieve signer's certificates for common authenticated variable
1800 by corresponding VariableName and VendorGuid from "certdb".
1802 @param[in] VariableName Name of authenticated Variable.
1803 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1804 @param[out] CertData Pointer to signer's certificates.
1805 @param[out] CertDataSize Length of CertData in bytes.
1807 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1808 @retval EFI_NOT_FOUND Fail to find "certdb" or matching certs.
1809 @retval EFI_SUCCESS Get signer's certificates successfully.
1814 IN CHAR16
*VariableName
,
1815 IN EFI_GUID
*VendorGuid
,
1816 OUT UINT8
**CertData
,
1817 OUT UINT32
*CertDataSize
1820 VARIABLE_POINTER_TRACK CertDbVariable
;
1826 if ((VariableName
== NULL
) || (VendorGuid
== NULL
) || (CertData
== NULL
) || (CertDataSize
== NULL
)) {
1827 return EFI_INVALID_PARAMETER
;
1831 // Get variable "certdb".
1833 Status
= FindVariable (
1837 &mVariableModuleGlobal
->VariableGlobal
,
1840 if (EFI_ERROR (Status
)) {
1844 DataSize
= DataSizeOfVariable (CertDbVariable
.CurrPtr
);
1845 Data
= GetVariableDataPtr (CertDbVariable
.CurrPtr
);
1846 if ((DataSize
== 0) || (Data
== NULL
)) {
1848 return EFI_NOT_FOUND
;
1851 Status
= FindCertsFromDb (
1862 if (EFI_ERROR (Status
)) {
1866 *CertData
= Data
+ CertOffset
;
1871 Delete matching signer's certificates when deleting common authenticated
1872 variable by corresponding VariableName and VendorGuid from "certdb".
1874 @param[in] VariableName Name of authenticated Variable.
1875 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1877 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1878 @retval EFI_NOT_FOUND Fail to find "certdb" or matching certs.
1879 @retval EFI_OUT_OF_RESOURCES The operation is failed due to lack of resources.
1880 @retval EFI_SUCCESS The operation is completed successfully.
1885 IN CHAR16
*VariableName
,
1886 IN EFI_GUID
*VendorGuid
1889 VARIABLE_POINTER_TRACK CertDbVariable
;
1894 UINT32 CertNodeOffset
;
1895 UINT32 CertNodeSize
;
1897 UINT32 NewCertDbSize
;
1899 if ((VariableName
== NULL
) || (VendorGuid
== NULL
)) {
1900 return EFI_INVALID_PARAMETER
;
1904 // Get variable "certdb".
1906 Status
= FindVariable (
1910 &mVariableModuleGlobal
->VariableGlobal
,
1913 if (EFI_ERROR (Status
)) {
1917 DataSize
= DataSizeOfVariable (CertDbVariable
.CurrPtr
);
1918 Data
= GetVariableDataPtr (CertDbVariable
.CurrPtr
);
1919 if ((DataSize
== 0) || (Data
== NULL
)) {
1921 return EFI_NOT_FOUND
;
1924 if (DataSize
== sizeof (UINT32
)) {
1926 // There is no certs in certdb.
1932 // Get corresponding cert node from certdb.
1934 Status
= FindCertsFromDb (
1945 if (EFI_ERROR (Status
)) {
1949 if (DataSize
< (CertNodeOffset
+ CertNodeSize
)) {
1950 return EFI_NOT_FOUND
;
1954 // Construct new data content of variable "certdb".
1956 NewCertDbSize
= (UINT32
) DataSize
- CertNodeSize
;
1957 NewCertDb
= (UINT8
*) mCertDbStore
;
1960 // Copy the DB entries before deleting node.
1962 CopyMem (NewCertDb
, Data
, CertNodeOffset
);
1964 // Update CertDbListSize.
1966 CopyMem (NewCertDb
, &NewCertDbSize
, sizeof (UINT32
));
1968 // Copy the DB entries after deleting node.
1970 if (DataSize
> (CertNodeOffset
+ CertNodeSize
)) {
1972 NewCertDb
+ CertNodeOffset
,
1973 Data
+ CertNodeOffset
+ CertNodeSize
,
1974 DataSize
- CertNodeOffset
- CertNodeSize
1981 VarAttr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
1982 Status
= UpdateVariable (
1998 Insert signer's certificates for common authenticated variable with VariableName
1999 and VendorGuid in AUTH_CERT_DB_DATA to "certdb".
2001 @param[in] VariableName Name of authenticated Variable.
2002 @param[in] VendorGuid Vendor GUID of authenticated Variable.
2003 @param[in] CertData Pointer to signer's certificates.
2004 @param[in] CertDataSize Length of CertData in bytes.
2006 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
2007 @retval EFI_ACCESS_DENIED An AUTH_CERT_DB_DATA entry with same VariableName
2008 and VendorGuid already exists.
2009 @retval EFI_OUT_OF_RESOURCES The operation is failed due to lack of resources.
2010 @retval EFI_SUCCESS Insert an AUTH_CERT_DB_DATA entry to "certdb"
2015 IN CHAR16
*VariableName
,
2016 IN EFI_GUID
*VendorGuid
,
2018 IN UINTN CertDataSize
2021 VARIABLE_POINTER_TRACK CertDbVariable
;
2027 UINT32 NewCertDbSize
;
2028 UINT32 CertNodeSize
;
2030 AUTH_CERT_DB_DATA
*Ptr
;
2032 if ((VariableName
== NULL
) || (VendorGuid
== NULL
) || (CertData
== NULL
)) {
2033 return EFI_INVALID_PARAMETER
;
2037 // Get variable "certdb".
2039 Status
= FindVariable (
2043 &mVariableModuleGlobal
->VariableGlobal
,
2046 if (EFI_ERROR (Status
)) {
2050 DataSize
= DataSizeOfVariable (CertDbVariable
.CurrPtr
);
2051 Data
= GetVariableDataPtr (CertDbVariable
.CurrPtr
);
2052 if ((DataSize
== 0) || (Data
== NULL
)) {
2054 return EFI_NOT_FOUND
;
2058 // Find whether matching cert node already exists in "certdb".
2059 // If yes return error.
2061 Status
= FindCertsFromDb (
2072 if (!EFI_ERROR (Status
)) {
2074 return EFI_ACCESS_DENIED
;
2078 // Construct new data content of variable "certdb".
2080 NameSize
= (UINT32
) StrLen (VariableName
);
2081 CertNodeSize
= sizeof (AUTH_CERT_DB_DATA
) + (UINT32
) CertDataSize
+ NameSize
* sizeof (CHAR16
);
2082 NewCertDbSize
= (UINT32
) DataSize
+ CertNodeSize
;
2083 if (NewCertDbSize
> mMaxCertDbSize
) {
2084 return EFI_OUT_OF_RESOURCES
;
2086 NewCertDb
= (UINT8
*) mCertDbStore
;
2089 // Copy the DB entries before deleting node.
2091 CopyMem (NewCertDb
, Data
, DataSize
);
2093 // Update CertDbListSize.
2095 CopyMem (NewCertDb
, &NewCertDbSize
, sizeof (UINT32
));
2097 // Construct new cert node.
2099 Ptr
= (AUTH_CERT_DB_DATA
*) (NewCertDb
+ DataSize
);
2100 CopyGuid (&Ptr
->VendorGuid
, VendorGuid
);
2101 CopyMem (&Ptr
->CertNodeSize
, &CertNodeSize
, sizeof (UINT32
));
2102 CopyMem (&Ptr
->NameSize
, &NameSize
, sizeof (UINT32
));
2103 CopyMem (&Ptr
->CertDataSize
, &CertDataSize
, sizeof (UINT32
));
2106 (UINT8
*) Ptr
+ sizeof (AUTH_CERT_DB_DATA
),
2108 NameSize
* sizeof (CHAR16
)
2112 (UINT8
*) Ptr
+ sizeof (AUTH_CERT_DB_DATA
) + NameSize
* sizeof (CHAR16
),
2120 VarAttr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
2121 Status
= UpdateVariable (
2137 Process variable with EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set
2139 Caution: This function may receive untrusted input.
2140 This function may be invoked in SMM mode, and datasize and data are external input.
2141 This function will do basic validation, before parse the data.
2142 This function will parse the authentication carefully to avoid security issues, like
2143 buffer overflow, integer overflow.
2145 @param[in] VariableName Name of Variable to be found.
2146 @param[in] VendorGuid Variable vendor GUID.
2147 @param[in] Data Data pointer.
2148 @param[in] DataSize Size of Data found. If size is less than the
2149 data, this value contains the required size.
2150 @param[in] Variable The variable information which is used to keep track of variable usage.
2151 @param[in] Attributes Attribute value of the variable.
2152 @param[in] AuthVarType Verify against PK, KEK database, private database or certificate in data payload.
2153 @param[out] VarDel Delete the variable or not.
2155 @retval EFI_INVALID_PARAMETER Invalid parameter.
2156 @retval EFI_SECURITY_VIOLATION The variable does NOT pass the validation
2157 check carried out by the firmware.
2158 @retval EFI_OUT_OF_RESOURCES Failed to process variable due to lack
2160 @retval EFI_SUCCESS Variable pass validation successfully.
2164 VerifyTimeBasedPayload (
2165 IN CHAR16
*VariableName
,
2166 IN EFI_GUID
*VendorGuid
,
2169 IN VARIABLE_POINTER_TRACK
*Variable
,
2170 IN UINT32 Attributes
,
2171 IN AUTHVAR_TYPE AuthVarType
,
2185 BOOLEAN VerifyStatus
;
2187 EFI_SIGNATURE_LIST
*CertList
;
2188 EFI_SIGNATURE_DATA
*Cert
;
2189 VARIABLE_POINTER_TRACK KekVariable
;
2190 EFI_VARIABLE_AUTHENTICATION_2
*CertData
;
2193 VARIABLE_POINTER_TRACK PkVariable
;
2197 UINTN CertStackSize
;
2198 UINT8
*CertsInCertDb
;
2199 UINT32 CertsSizeinDb
;
2201 VerifyStatus
= FALSE
;
2207 CertsInCertDb
= NULL
;
2210 // When the attribute EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS is
2211 // set, then the Data buffer shall begin with an instance of a complete (and serialized)
2212 // EFI_VARIABLE_AUTHENTICATION_2 descriptor. The descriptor shall be followed by the new
2213 // variable value and DataSize shall reflect the combined size of the descriptor and the new
2214 // variable value. The authentication descriptor is not part of the variable data and is not
2215 // returned by subsequent calls to GetVariable().
2217 CertData
= (EFI_VARIABLE_AUTHENTICATION_2
*) Data
;
2220 // Verify that Pad1, Nanosecond, TimeZone, Daylight and Pad2 components of the
2221 // TimeStamp value are set to zero.
2223 if ((CertData
->TimeStamp
.Pad1
!= 0) ||
2224 (CertData
->TimeStamp
.Nanosecond
!= 0) ||
2225 (CertData
->TimeStamp
.TimeZone
!= 0) ||
2226 (CertData
->TimeStamp
.Daylight
!= 0) ||
2227 (CertData
->TimeStamp
.Pad2
!= 0)) {
2228 return EFI_SECURITY_VIOLATION
;
2231 if ((Variable
->CurrPtr
!= NULL
) && ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0)) {
2232 if (CompareTimeStamp (&CertData
->TimeStamp
, &Variable
->CurrPtr
->TimeStamp
)) {
2234 // TimeStamp check fail, suspicious replay attack, return EFI_SECURITY_VIOLATION.
2236 return EFI_SECURITY_VIOLATION
;
2241 // wCertificateType should be WIN_CERT_TYPE_EFI_GUID.
2242 // Cert type should be EFI_CERT_TYPE_PKCS7_GUID.
2244 if ((CertData
->AuthInfo
.Hdr
.wCertificateType
!= WIN_CERT_TYPE_EFI_GUID
) ||
2245 !CompareGuid (&CertData
->AuthInfo
.CertType
, &gEfiCertPkcs7Guid
)) {
2247 // Invalid AuthInfo type, return EFI_SECURITY_VIOLATION.
2249 return EFI_SECURITY_VIOLATION
;
2253 // Find out Pkcs7 SignedData which follows the EFI_VARIABLE_AUTHENTICATION_2 descriptor.
2254 // AuthInfo.Hdr.dwLength is the length of the entire certificate, including the length of the header.
2256 SigData
= CertData
->AuthInfo
.CertData
;
2257 SigDataSize
= CertData
->AuthInfo
.Hdr
.dwLength
- (UINT32
) (OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID
, CertData
));
2260 // Find out the new data payload which follows Pkcs7 SignedData directly.
2262 PayloadPtr
= SigData
+ SigDataSize
;
2263 PayloadSize
= DataSize
- OFFSET_OF_AUTHINFO2_CERT_DATA
- (UINTN
) SigDataSize
;
2266 // Construct a serialization buffer of the values of the VariableName, VendorGuid and Attributes
2267 // parameters of the SetVariable() call and the TimeStamp component of the
2268 // EFI_VARIABLE_AUTHENTICATION_2 descriptor followed by the variable's new value
2269 // i.e. (VariableName, VendorGuid, Attributes, TimeStamp, Data)
2271 NewDataSize
= PayloadSize
+ sizeof (EFI_TIME
) + sizeof (UINT32
) +
2272 sizeof (EFI_GUID
) + StrSize (VariableName
) - sizeof (CHAR16
);
2274 // Here is to reuse scratch data area(at the end of volatile variable store)
2275 // to reduce SMRAM consumption for SMM variable driver.
2276 // The scratch buffer is enough to hold the serialized data and safe to use,
2277 // because it will be used at here to do verification only first
2278 // and then used in UpdateVariable() for a time based auth variable set.
2280 NewData
= (UINT8
*) GetEndPointer ((VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
));
2283 Length
= StrLen (VariableName
) * sizeof (CHAR16
);
2284 CopyMem (Buffer
, VariableName
, Length
);
2287 Length
= sizeof (EFI_GUID
);
2288 CopyMem (Buffer
, VendorGuid
, Length
);
2291 Length
= sizeof (UINT32
);
2292 CopyMem (Buffer
, &Attr
, Length
);
2295 Length
= sizeof (EFI_TIME
);
2296 CopyMem (Buffer
, &CertData
->TimeStamp
, Length
);
2299 CopyMem (Buffer
, PayloadPtr
, PayloadSize
);
2301 if (AuthVarType
== AuthVarTypePk
) {
2303 // Verify that the signature has been made with the current Platform Key (no chaining for PK).
2304 // First, get signer's certificates from SignedData.
2306 VerifyStatus
= Pkcs7GetSigners (
2314 if (!VerifyStatus
) {
2319 // Second, get the current platform key from variable. Check whether it's identical with signer's certificates
2320 // in SignedData. If not, return error immediately.
2322 Status
= FindVariable (
2323 EFI_PLATFORM_KEY_NAME
,
2324 &gEfiGlobalVariableGuid
,
2326 &mVariableModuleGlobal
->VariableGlobal
,
2329 if (EFI_ERROR (Status
)) {
2330 VerifyStatus
= FALSE
;
2333 CertList
= (EFI_SIGNATURE_LIST
*) GetVariableDataPtr (PkVariable
.CurrPtr
);
2334 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
2335 if ((RootCertSize
!= (CertList
->SignatureSize
- (sizeof (EFI_SIGNATURE_DATA
) - 1))) ||
2336 (CompareMem (Cert
->SignatureData
, RootCert
, RootCertSize
) != 0)) {
2337 VerifyStatus
= FALSE
;
2342 // Verify Pkcs7 SignedData via Pkcs7Verify library.
2344 VerifyStatus
= Pkcs7Verify (
2353 } else if (AuthVarType
== AuthVarTypeKek
) {
2356 // Get KEK database from variable.
2358 Status
= FindVariable (
2359 EFI_KEY_EXCHANGE_KEY_NAME
,
2360 &gEfiGlobalVariableGuid
,
2362 &mVariableModuleGlobal
->VariableGlobal
,
2365 if (EFI_ERROR (Status
)) {
2370 // Ready to verify Pkcs7 SignedData. Go through KEK Signature Database to find out X.509 CertList.
2372 KekDataSize
= KekVariable
.CurrPtr
->DataSize
;
2373 CertList
= (EFI_SIGNATURE_LIST
*) GetVariableDataPtr (KekVariable
.CurrPtr
);
2374 while ((KekDataSize
> 0) && (KekDataSize
>= CertList
->SignatureListSize
)) {
2375 if (CompareGuid (&CertList
->SignatureType
, &gEfiCertX509Guid
)) {
2376 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
2377 CertCount
= (CertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - CertList
->SignatureHeaderSize
) / CertList
->SignatureSize
;
2378 for (Index
= 0; Index
< CertCount
; Index
++) {
2380 // Iterate each Signature Data Node within this CertList for a verify
2382 RootCert
= Cert
->SignatureData
;
2383 RootCertSize
= CertList
->SignatureSize
- (sizeof (EFI_SIGNATURE_DATA
) - 1);
2386 // Verify Pkcs7 SignedData via Pkcs7Verify library.
2388 VerifyStatus
= Pkcs7Verify (
2399 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) Cert
+ CertList
->SignatureSize
);
2402 KekDataSize
-= CertList
->SignatureListSize
;
2403 CertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) CertList
+ CertList
->SignatureListSize
);
2405 } else if (AuthVarType
== AuthVarTypePriv
) {
2408 // Process common authenticated variable except PK/KEK/DB/DBX/DBT.
2409 // Get signer's certificates from SignedData.
2411 VerifyStatus
= Pkcs7GetSigners (
2419 if (!VerifyStatus
) {
2424 // Get previously stored signer's certificates from certdb for existing
2425 // variable. Check whether they are identical with signer's certificates
2426 // in SignedData. If not, return error immediately.
2428 if ((Variable
->CurrPtr
!= NULL
)) {
2429 VerifyStatus
= FALSE
;
2431 Status
= GetCertsFromDb (VariableName
, VendorGuid
, &CertsInCertDb
, &CertsSizeinDb
);
2432 if (EFI_ERROR (Status
)) {
2436 if ((CertStackSize
!= CertsSizeinDb
) ||
2437 (CompareMem (SignerCerts
, CertsInCertDb
, CertsSizeinDb
) != 0)) {
2442 VerifyStatus
= Pkcs7Verify (
2450 if (!VerifyStatus
) {
2455 // Delete signer's certificates when delete the common authenticated variable.
2457 if ((PayloadSize
== 0) && (Variable
->CurrPtr
!= NULL
) && ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0)) {
2458 Status
= DeleteCertsFromDb (VariableName
, VendorGuid
);
2459 if (EFI_ERROR (Status
)) {
2460 VerifyStatus
= FALSE
;
2463 } else if (Variable
->CurrPtr
== NULL
&& PayloadSize
!= 0) {
2465 // Insert signer's certificates when adding a new common authenticated variable.
2467 Status
= InsertCertsToDb (VariableName
, VendorGuid
, SignerCerts
, CertStackSize
);
2468 if (EFI_ERROR (Status
)) {
2469 VerifyStatus
= FALSE
;
2473 } else if (AuthVarType
== AuthVarTypePayload
) {
2474 CertList
= (EFI_SIGNATURE_LIST
*) PayloadPtr
;
2475 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
2476 RootCert
= Cert
->SignatureData
;
2477 RootCertSize
= CertList
->SignatureSize
- (sizeof (EFI_SIGNATURE_DATA
) - 1);
2479 // Verify Pkcs7 SignedData via Pkcs7Verify library.
2481 VerifyStatus
= Pkcs7Verify (
2490 return EFI_SECURITY_VIOLATION
;
2495 if (AuthVarType
== AuthVarTypePk
|| AuthVarType
== AuthVarTypePriv
) {
2496 Pkcs7FreeSigners (RootCert
);
2497 Pkcs7FreeSigners (SignerCerts
);
2500 if (!VerifyStatus
) {
2501 return EFI_SECURITY_VIOLATION
;
2504 Status
= CheckSignatureListFormat(VariableName
, VendorGuid
, PayloadPtr
, PayloadSize
);
2505 if (EFI_ERROR (Status
)) {
2509 if ((PayloadSize
== 0) && (VarDel
!= NULL
)) {
2514 // Final step: Update/Append Variable if it pass Pkcs7Verify
2516 return UpdateVariable (
2525 &CertData
->TimeStamp