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 @param[in] MaxAuthVariableSize Reflect the overhead associated with the saving
128 of a single EFI authenticated variable with the exception
129 of the overhead associated with the length
130 of the string name of the EFI variable.
132 @retval EFI_SUCCESS Function successfully executed.
133 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resources.
137 AutenticatedVariableServiceInitialize (
138 IN UINTN MaxAuthVariableSize
142 VARIABLE_POINTER_TRACK Variable
;
143 VARIABLE_POINTER_TRACK PkVariable
;
149 UINT8 SecureBootMode
;
150 UINT8 SecureBootEnable
;
155 // Initialize hash context.
157 CtxSize
= Sha256GetContextSize ();
158 mHashCtx
= AllocateRuntimePool (CtxSize
);
159 if (mHashCtx
== NULL
) {
160 return EFI_OUT_OF_RESOURCES
;
164 // Reserve runtime buffer for public key database. The size excludes variable header and name size.
166 mMaxKeyDbSize
= (UINT32
) (MaxAuthVariableSize
- sizeof (AUTHVAR_KEYDB_NAME
));
167 mMaxKeyNumber
= mMaxKeyDbSize
/ EFI_CERT_TYPE_RSA2048_SIZE
;
168 mPubKeyStore
= AllocateRuntimePool (mMaxKeyDbSize
);
169 if (mPubKeyStore
== NULL
) {
170 return EFI_OUT_OF_RESOURCES
;
174 // Reserve runtime buffer for certificate database. The size excludes variable header and name size.
176 mMaxCertDbSize
= (UINT32
) (MaxAuthVariableSize
- sizeof (EFI_CERT_DB_NAME
));
177 mCertDbStore
= AllocateRuntimePool (mMaxCertDbSize
);
178 if (mCertDbStore
== NULL
) {
179 return EFI_OUT_OF_RESOURCES
;
183 // Check "AuthVarKeyDatabase" variable's existence.
184 // If it doesn't exist, create a new one with initial value of 0 and EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS set.
186 Status
= FindVariable (
188 &gEfiAuthenticatedVariableGuid
,
190 &mVariableModuleGlobal
->VariableGlobal
,
194 if (Variable
.CurrPtr
== NULL
) {
195 VarAttr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
;
198 Status
= UpdateVariable (
200 &gEfiAuthenticatedVariableGuid
,
209 if (EFI_ERROR (Status
)) {
214 // Load database in global variable for cache.
216 DataSize
= DataSizeOfVariable (Variable
.CurrPtr
);
217 Data
= GetVariableDataPtr (Variable
.CurrPtr
);
218 ASSERT ((DataSize
!= 0) && (Data
!= NULL
));
220 // "AuthVarKeyDatabase" is an internal variable. Its DataSize is always ensured not to exceed mPubKeyStore buffer size(See definition before)
221 // Therefore, there is no memory overflow in underlying CopyMem.
223 CopyMem (mPubKeyStore
, (UINT8
*) Data
, DataSize
);
224 mPubKeyNumber
= (UINT32
) (DataSize
/ EFI_CERT_TYPE_RSA2048_SIZE
);
227 FindVariable (EFI_PLATFORM_KEY_NAME
, &gEfiGlobalVariableGuid
, &PkVariable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
228 if (PkVariable
.CurrPtr
== NULL
) {
229 DEBUG ((EFI_D_INFO
, "Variable %s does not exist.\n", EFI_PLATFORM_KEY_NAME
));
231 DEBUG ((EFI_D_INFO
, "Variable %s exists.\n", EFI_PLATFORM_KEY_NAME
));
235 // Create "SetupMode" variable with BS+RT attribute set.
237 FindVariable (EFI_SETUP_MODE_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
238 if (PkVariable
.CurrPtr
== NULL
) {
239 mPlatformMode
= SETUP_MODE
;
241 mPlatformMode
= USER_MODE
;
243 Status
= UpdateVariable (
245 &gEfiGlobalVariableGuid
,
248 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
254 if (EFI_ERROR (Status
)) {
259 // Create "SignatureSupport" variable with BS+RT attribute set.
261 FindVariable (EFI_SIGNATURE_SUPPORT_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
262 Status
= UpdateVariable (
263 EFI_SIGNATURE_SUPPORT_NAME
,
264 &gEfiGlobalVariableGuid
,
266 sizeof(mSignatureSupport
),
267 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
273 if (EFI_ERROR (Status
)) {
278 // If "SecureBootEnable" variable exists, then update "SecureBoot" variable.
279 // If "SecureBootEnable" variable is SECURE_BOOT_ENABLE and in USER_MODE, Set "SecureBoot" variable to SECURE_BOOT_MODE_ENABLE.
280 // If "SecureBootEnable" variable is SECURE_BOOT_DISABLE, Set "SecureBoot" variable to SECURE_BOOT_MODE_DISABLE.
282 SecureBootEnable
= SECURE_BOOT_DISABLE
;
283 FindVariable (EFI_SECURE_BOOT_ENABLE_NAME
, &gEfiSecureBootEnableDisableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
284 if (Variable
.CurrPtr
!= NULL
) {
285 if (mPlatformMode
== SETUP_MODE
){
287 // PK is cleared in runtime. "SecureBootMode" is not updated before reboot
288 // Delete "SecureBootMode" in SetupMode
290 Status
= UpdateVariable (
291 EFI_SECURE_BOOT_ENABLE_NAME
,
292 &gEfiSecureBootEnableDisableGuid
,
295 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
302 SecureBootEnable
= *(GetVariableDataPtr (Variable
.CurrPtr
));
304 } else if (mPlatformMode
== USER_MODE
) {
306 // "SecureBootEnable" not exist, initialize it in USER_MODE.
308 SecureBootEnable
= SECURE_BOOT_ENABLE
;
309 Status
= UpdateVariable (
310 EFI_SECURE_BOOT_ENABLE_NAME
,
311 &gEfiSecureBootEnableDisableGuid
,
314 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
320 if (EFI_ERROR (Status
)) {
326 // Create "SecureBoot" variable with BS+RT attribute set.
328 if (SecureBootEnable
== SECURE_BOOT_ENABLE
&& mPlatformMode
== USER_MODE
) {
329 SecureBootMode
= SECURE_BOOT_MODE_ENABLE
;
331 SecureBootMode
= SECURE_BOOT_MODE_DISABLE
;
333 FindVariable (EFI_SECURE_BOOT_MODE_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
334 Status
= UpdateVariable (
335 EFI_SECURE_BOOT_MODE_NAME
,
336 &gEfiGlobalVariableGuid
,
339 EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
345 if (EFI_ERROR (Status
)) {
349 DEBUG ((EFI_D_INFO
, "Variable %s is %x\n", EFI_SETUP_MODE_NAME
, mPlatformMode
));
350 DEBUG ((EFI_D_INFO
, "Variable %s is %x\n", EFI_SECURE_BOOT_MODE_NAME
, SecureBootMode
));
351 DEBUG ((EFI_D_INFO
, "Variable %s is %x\n", EFI_SECURE_BOOT_ENABLE_NAME
, SecureBootEnable
));
354 // Initialize "CustomMode" in STANDARD_SECURE_BOOT_MODE state.
356 FindVariable (EFI_CUSTOM_MODE_NAME
, &gEfiCustomModeEnableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
357 CustomMode
= STANDARD_SECURE_BOOT_MODE
;
358 Status
= UpdateVariable (
359 EFI_CUSTOM_MODE_NAME
,
360 &gEfiCustomModeEnableGuid
,
363 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
369 if (EFI_ERROR (Status
)) {
373 DEBUG ((EFI_D_INFO
, "Variable %s is %x\n", EFI_CUSTOM_MODE_NAME
, CustomMode
));
376 // Check "certdb" variable's existence.
377 // If it doesn't exist, then create a new one with
378 // EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set.
380 Status
= FindVariable (
384 &mVariableModuleGlobal
->VariableGlobal
,
388 if (Variable
.CurrPtr
== NULL
) {
389 VarAttr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
390 ListSize
= sizeof (UINT32
);
391 Status
= UpdateVariable (
402 if (EFI_ERROR (Status
)) {
408 // Check "VendorKeysNv" variable's existence and create "VendorKeys" variable accordingly.
410 FindVariable (EFI_VENDOR_KEYS_NV_VARIABLE_NAME
, &gEfiVendorKeysNvGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
411 if (Variable
.CurrPtr
!= NULL
) {
412 mVendorKeyState
= *(GetVariableDataPtr (Variable
.CurrPtr
));
415 // "VendorKeysNv" not exist, initialize it in VENDOR_KEYS_VALID state.
417 mVendorKeyState
= VENDOR_KEYS_VALID
;
418 Status
= UpdateVariable (
419 EFI_VENDOR_KEYS_NV_VARIABLE_NAME
,
420 &gEfiVendorKeysNvGuid
,
423 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
,
429 if (EFI_ERROR (Status
)) {
435 // Create "VendorKeys" variable with BS+RT attribute set.
437 FindVariable (EFI_VENDOR_KEYS_VARIABLE_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
438 Status
= UpdateVariable (
439 EFI_VENDOR_KEYS_VARIABLE_NAME
,
440 &gEfiGlobalVariableGuid
,
443 EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
449 if (EFI_ERROR (Status
)) {
453 DEBUG ((EFI_D_INFO
, "Variable %s is %x\n", EFI_VENDOR_KEYS_VARIABLE_NAME
, mVendorKeyState
));
459 Add public key in store and return its index.
461 @param[in] PubKey Input pointer to Public Key data
462 @param[in] VariableDataEntry The variable data entry
464 @return Index of new added item
470 IN VARIABLE_ENTRY_CONSISTENCY
*VariableDataEntry
476 VARIABLE_POINTER_TRACK Variable
;
480 VARIABLE_ENTRY_CONSISTENCY PublicKeyEntry
;
483 if (PubKey
== NULL
) {
487 Status
= FindVariable (
489 &gEfiAuthenticatedVariableGuid
,
491 &mVariableModuleGlobal
->VariableGlobal
,
494 if (EFI_ERROR (Status
)) {
495 DEBUG ((EFI_D_ERROR
, "Get public key database variable failure, Status = %r\n", Status
));
500 // Check whether the public key entry does exist.
503 for (Ptr
= mPubKeyStore
, Index
= 1; Index
<= mPubKeyNumber
; Index
++) {
504 if (CompareMem (Ptr
, PubKey
, EFI_CERT_TYPE_RSA2048_SIZE
) == 0) {
508 Ptr
+= EFI_CERT_TYPE_RSA2048_SIZE
;
513 // Add public key in database.
515 if (mPubKeyNumber
== mMaxKeyNumber
) {
517 // Public key dadatase is full, try to reclaim invalid key.
521 // NV storage can't reclaim at runtime.
527 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
528 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
535 if (EFI_ERROR (Status
)) {
539 Status
= FindVariable (
541 &gEfiAuthenticatedVariableGuid
,
543 &mVariableModuleGlobal
->VariableGlobal
,
546 if (EFI_ERROR (Status
)) {
547 DEBUG ((EFI_D_ERROR
, "Get public key database variable failure, Status = %r\n", Status
));
551 DataSize
= DataSizeOfVariable (Variable
.CurrPtr
);
552 Data
= GetVariableDataPtr (Variable
.CurrPtr
);
553 ASSERT ((DataSize
!= 0) && (Data
!= NULL
));
555 // "AuthVarKeyDatabase" is an internal used variable. Its DataSize is always ensured not to exceed mPubKeyStore buffer size(See definition before)
556 // Therefore, there is no memory overflow in underlying CopyMem.
558 CopyMem (mPubKeyStore
, (UINT8
*) Data
, DataSize
);
559 mPubKeyNumber
= (UINT32
) (DataSize
/ EFI_CERT_TYPE_RSA2048_SIZE
);
561 if (mPubKeyNumber
== mMaxKeyNumber
) {
567 // Check the variable space for both public key and variable data.
569 PublicKeyEntry
.VariableSize
= (mPubKeyNumber
+ 1) * EFI_CERT_TYPE_RSA2048_SIZE
;
570 PublicKeyEntry
.Guid
= &gEfiAuthenticatedVariableGuid
;
571 PublicKeyEntry
.Name
= AUTHVAR_KEYDB_NAME
;
572 Attributes
= VARIABLE_ATTRIBUTE_NV_BS_RT
| EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
;
574 if (!CheckRemainingSpaceForConsistency (Attributes
, &PublicKeyEntry
, VariableDataEntry
, NULL
)) {
576 // No enough variable space.
581 CopyMem (mPubKeyStore
+ mPubKeyNumber
* EFI_CERT_TYPE_RSA2048_SIZE
, PubKey
, EFI_CERT_TYPE_RSA2048_SIZE
);
582 Index
= ++mPubKeyNumber
;
584 // Update public key database variable.
586 Status
= UpdateVariable (
588 &gEfiAuthenticatedVariableGuid
,
590 mPubKeyNumber
* EFI_CERT_TYPE_RSA2048_SIZE
,
597 if (EFI_ERROR (Status
)) {
598 DEBUG ((EFI_D_ERROR
, "Update public key database variable failure, Status = %r\n", Status
));
607 Verify data payload with AuthInfo in EFI_CERT_TYPE_RSA2048_SHA256_GUID type.
608 Follow the steps in UEFI2.2.
610 Caution: This function may receive untrusted input.
611 This function may be invoked in SMM mode, and datasize and data are external input.
612 This function will do basic validation, before parse the data.
613 This function will parse the authentication carefully to avoid security issues, like
614 buffer overflow, integer overflow.
616 @param[in] Data Pointer to data with AuthInfo.
617 @param[in] DataSize Size of Data.
618 @param[in] PubKey Public key used for verification.
620 @retval EFI_INVALID_PARAMETER Invalid parameter.
621 @retval EFI_SECURITY_VIOLATION If authentication failed.
622 @retval EFI_SUCCESS Authentication successful.
626 VerifyCounterBasedPayload (
633 EFI_VARIABLE_AUTHENTICATION
*CertData
;
634 EFI_CERT_BLOCK_RSA_2048_SHA256
*CertBlock
;
635 UINT8 Digest
[SHA256_DIGEST_SIZE
];
639 PayloadSize
= DataSize
- AUTHINFO_SIZE
;
644 if (Data
== NULL
|| PubKey
== NULL
) {
645 return EFI_INVALID_PARAMETER
;
648 CertData
= (EFI_VARIABLE_AUTHENTICATION
*) Data
;
649 CertBlock
= (EFI_CERT_BLOCK_RSA_2048_SHA256
*) (CertData
->AuthInfo
.CertData
);
652 // wCertificateType should be WIN_CERT_TYPE_EFI_GUID.
653 // Cert type should be EFI_CERT_TYPE_RSA2048_SHA256_GUID.
655 if ((CertData
->AuthInfo
.Hdr
.wCertificateType
!= WIN_CERT_TYPE_EFI_GUID
) ||
656 !CompareGuid (&CertData
->AuthInfo
.CertType
, &gEfiCertTypeRsa2048Sha256Guid
)
659 // Invalid AuthInfo type, return EFI_SECURITY_VIOLATION.
661 return EFI_SECURITY_VIOLATION
;
664 // Hash data payload with SHA256.
666 ZeroMem (Digest
, SHA256_DIGEST_SIZE
);
667 Status
= Sha256Init (mHashCtx
);
671 Status
= Sha256Update (mHashCtx
, Data
+ AUTHINFO_SIZE
, PayloadSize
);
678 Status
= Sha256Update (mHashCtx
, &PayloadSize
, sizeof (UINTN
));
683 // Hash Monotonic Count.
685 Status
= Sha256Update (mHashCtx
, &CertData
->MonotonicCount
, sizeof (UINT64
));
689 Status
= Sha256Final (mHashCtx
, Digest
);
694 // Generate & Initialize RSA Context.
697 ASSERT (Rsa
!= NULL
);
699 // Set RSA Key Components.
700 // NOTE: Only N and E are needed to be set as RSA public key for signature verification.
702 Status
= RsaSetKey (Rsa
, RsaKeyN
, PubKey
, EFI_CERT_TYPE_RSA2048_SIZE
);
706 Status
= RsaSetKey (Rsa
, RsaKeyE
, mRsaE
, sizeof (mRsaE
));
711 // Verify the signature.
713 Status
= RsaPkcs1Verify (
717 CertBlock
->Signature
,
718 EFI_CERT_TYPE_RSA2048_SHA256_SIZE
728 return EFI_SECURITY_VIOLATION
;
733 Update platform mode.
735 @param[in] Mode SETUP_MODE or USER_MODE.
737 @return EFI_INVALID_PARAMETER Invalid parameter.
738 @return EFI_SUCCESS Update platform mode successfully.
747 VARIABLE_POINTER_TRACK Variable
;
748 UINT8 SecureBootMode
;
749 UINT8 SecureBootEnable
;
750 UINTN VariableDataSize
;
752 Status
= FindVariable (
754 &gEfiGlobalVariableGuid
,
756 &mVariableModuleGlobal
->VariableGlobal
,
759 if (EFI_ERROR (Status
)) {
764 // Update the value of SetupMode variable by a simple mem copy, this could avoid possible
765 // variable storage reclaim at runtime.
767 mPlatformMode
= (UINT8
) Mode
;
768 CopyMem (GetVariableDataPtr (Variable
.CurrPtr
), &mPlatformMode
, sizeof(UINT8
));
772 // SecureBoot Variable indicates whether the platform firmware is operating
773 // in Secure boot mode (1) or not (0), so we should not change SecureBoot
774 // Variable in runtime.
780 // Check "SecureBoot" variable's existence.
781 // If it doesn't exist, firmware has no capability to perform driver signing verification,
782 // then set "SecureBoot" to 0.
784 Status
= FindVariable (
785 EFI_SECURE_BOOT_MODE_NAME
,
786 &gEfiGlobalVariableGuid
,
788 &mVariableModuleGlobal
->VariableGlobal
,
792 // If "SecureBoot" variable exists, then check "SetupMode" variable update.
793 // If "SetupMode" variable is USER_MODE, "SecureBoot" variable is set to 1.
794 // If "SetupMode" variable is SETUP_MODE, "SecureBoot" variable is set to 0.
796 if (Variable
.CurrPtr
== NULL
) {
797 SecureBootMode
= SECURE_BOOT_MODE_DISABLE
;
799 if (mPlatformMode
== USER_MODE
) {
800 SecureBootMode
= SECURE_BOOT_MODE_ENABLE
;
801 } else if (mPlatformMode
== SETUP_MODE
) {
802 SecureBootMode
= SECURE_BOOT_MODE_DISABLE
;
804 return EFI_NOT_FOUND
;
808 Status
= UpdateVariable (
809 EFI_SECURE_BOOT_MODE_NAME
,
810 &gEfiGlobalVariableGuid
,
813 EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
819 if (EFI_ERROR (Status
)) {
824 // Check "SecureBootEnable" variable's existence. It can enable/disable secure boot feature.
826 Status
= FindVariable (
827 EFI_SECURE_BOOT_ENABLE_NAME
,
828 &gEfiSecureBootEnableDisableGuid
,
830 &mVariableModuleGlobal
->VariableGlobal
,
834 if (SecureBootMode
== SECURE_BOOT_MODE_ENABLE
) {
836 // Create the "SecureBootEnable" variable as secure boot is enabled.
838 SecureBootEnable
= SECURE_BOOT_ENABLE
;
839 VariableDataSize
= sizeof (SecureBootEnable
);
842 // Delete the "SecureBootEnable" variable if this variable exist as "SecureBoot"
843 // variable is not in secure boot state.
845 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
848 SecureBootEnable
= SECURE_BOOT_DISABLE
;
849 VariableDataSize
= 0;
852 Status
= UpdateVariable (
853 EFI_SECURE_BOOT_ENABLE_NAME
,
854 &gEfiSecureBootEnableDisableGuid
,
857 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
867 Check input data form to make sure it is a valid EFI_SIGNATURE_LIST for PK/KEK/db/dbx/dbt variable.
869 @param[in] VariableName Name of Variable to be check.
870 @param[in] VendorGuid Variable vendor GUID.
871 @param[in] Data Point to the variable data to be checked.
872 @param[in] DataSize Size of Data.
874 @return EFI_INVALID_PARAMETER Invalid signature list format.
875 @return EFI_SUCCESS Passed signature list format check successfully.
879 CheckSignatureListFormat(
880 IN CHAR16
*VariableName
,
881 IN EFI_GUID
*VendorGuid
,
886 EFI_SIGNATURE_LIST
*SigList
;
892 EFI_SIGNATURE_DATA
*CertData
;
899 ASSERT (VariableName
!= NULL
&& VendorGuid
!= NULL
&& Data
!= NULL
);
901 if (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, EFI_PLATFORM_KEY_NAME
) == 0)){
903 } else if ((CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, EFI_KEY_EXCHANGE_KEY_NAME
) == 0)) ||
904 (CompareGuid (VendorGuid
, &gEfiImageSecurityDatabaseGuid
) &&
905 ((StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE
) == 0) || (StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE1
) == 0) ||
906 (StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE2
) == 0)))) {
913 SigList
= (EFI_SIGNATURE_LIST
*) Data
;
914 SigDataSize
= DataSize
;
918 // Walk throuth the input signature list and check the data format.
919 // If any signature is incorrectly formed, the whole check will fail.
921 while ((SigDataSize
> 0) && (SigDataSize
>= SigList
->SignatureListSize
)) {
922 for (Index
= 0; Index
< (sizeof (mSupportSigItem
) / sizeof (EFI_SIGNATURE_ITEM
)); Index
++ ) {
923 if (CompareGuid (&SigList
->SignatureType
, &mSupportSigItem
[Index
].SigType
)) {
925 // The value of SignatureSize should always be 16 (size of SignatureOwner
926 // component) add the data length according to signature type.
928 if (mSupportSigItem
[Index
].SigDataSize
!= ((UINT32
) ~0) &&
929 (SigList
->SignatureSize
- sizeof (EFI_GUID
)) != mSupportSigItem
[Index
].SigDataSize
) {
930 return EFI_INVALID_PARAMETER
;
932 if (mSupportSigItem
[Index
].SigHeaderSize
!= ((UINTN
) ~0) &&
933 SigList
->SignatureHeaderSize
!= mSupportSigItem
[Index
].SigHeaderSize
) {
934 return EFI_INVALID_PARAMETER
;
940 if (Index
== (sizeof (mSupportSigItem
) / sizeof (EFI_SIGNATURE_ITEM
))) {
942 // Undefined signature type.
944 return EFI_INVALID_PARAMETER
;
947 if (CompareGuid (&SigList
->SignatureType
, &gEfiCertX509Guid
)) {
949 // Try to retrieve the RSA public key from the X.509 certificate.
950 // If this operation fails, it's not a valid certificate.
952 RsaContext
= RsaNew ();
953 if (RsaContext
== NULL
) {
954 return EFI_INVALID_PARAMETER
;
956 CertData
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) SigList
+ sizeof (EFI_SIGNATURE_LIST
) + SigList
->SignatureHeaderSize
);
957 CertLen
= SigList
->SignatureSize
- sizeof (EFI_GUID
);
958 if (!RsaGetPublicKeyFromX509 (CertData
->SignatureData
, CertLen
, &RsaContext
)) {
959 RsaFree (RsaContext
);
960 return EFI_INVALID_PARAMETER
;
962 RsaFree (RsaContext
);
965 if ((SigList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - SigList
->SignatureHeaderSize
) % SigList
->SignatureSize
!= 0) {
966 return EFI_INVALID_PARAMETER
;
968 SigCount
+= (SigList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - SigList
->SignatureHeaderSize
) / SigList
->SignatureSize
;
970 SigDataSize
-= SigList
->SignatureListSize
;
971 SigList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) SigList
+ SigList
->SignatureListSize
);
974 if (((UINTN
) SigList
- (UINTN
) Data
) != DataSize
) {
975 return EFI_INVALID_PARAMETER
;
978 if (IsPk
&& SigCount
> 1) {
979 return EFI_INVALID_PARAMETER
;
986 Update "VendorKeys" variable to record the out of band secure boot key modification.
988 @return EFI_SUCCESS Variable is updated successfully.
989 @return Others Failed to update variable.
993 VendorKeyIsModified (
998 VARIABLE_POINTER_TRACK Variable
;
1000 if (mVendorKeyState
== VENDOR_KEYS_MODIFIED
) {
1003 mVendorKeyState
= VENDOR_KEYS_MODIFIED
;
1005 FindVariable (EFI_VENDOR_KEYS_NV_VARIABLE_NAME
, &gEfiVendorKeysNvGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1006 Status
= UpdateVariable (
1007 EFI_VENDOR_KEYS_NV_VARIABLE_NAME
,
1008 &gEfiVendorKeysNvGuid
,
1011 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
,
1017 if (EFI_ERROR (Status
)) {
1021 FindVariable (EFI_VENDOR_KEYS_VARIABLE_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1022 return UpdateVariable (
1023 EFI_VENDOR_KEYS_VARIABLE_NAME
,
1024 &gEfiGlobalVariableGuid
,
1027 EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
1036 Process variable with platform key for verification.
1038 Caution: This function may receive untrusted input.
1039 This function may be invoked in SMM mode, and datasize and data are external input.
1040 This function will do basic validation, before parse the data.
1041 This function will parse the authentication carefully to avoid security issues, like
1042 buffer overflow, integer overflow.
1043 This function will check attribute carefully to avoid authentication bypass.
1045 @param[in] VariableName Name of Variable to be found.
1046 @param[in] VendorGuid Variable vendor GUID.
1047 @param[in] Data Data pointer.
1048 @param[in] DataSize Size of Data found. If size is less than the
1049 data, this value contains the required size.
1050 @param[in] Variable The variable information which is used to keep track of variable usage.
1051 @param[in] Attributes Attribute value of the variable
1052 @param[in] IsPk Indicate whether it is to process pk.
1054 @return EFI_INVALID_PARAMETER Invalid parameter.
1055 @return EFI_SECURITY_VIOLATION The variable does NOT pass the validation.
1056 check carried out by the firmware.
1057 @return EFI_SUCCESS Variable passed validation successfully.
1062 IN CHAR16
*VariableName
,
1063 IN EFI_GUID
*VendorGuid
,
1066 IN VARIABLE_POINTER_TRACK
*Variable
,
1067 IN UINT32 Attributes OPTIONAL
,
1076 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0 ||
1077 (Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) == 0) {
1079 // PK, KEK and db/dbx/dbt should set EFI_VARIABLE_NON_VOLATILE attribute and should be a time-based
1080 // authenticated variable.
1082 return EFI_INVALID_PARAMETER
;
1086 if ((InCustomMode() && UserPhysicalPresent()) || (mPlatformMode
== SETUP_MODE
&& !IsPk
)) {
1087 Payload
= (UINT8
*) Data
+ AUTHINFO2_SIZE (Data
);
1088 PayloadSize
= DataSize
- AUTHINFO2_SIZE (Data
);
1089 if (PayloadSize
== 0) {
1093 Status
= CheckSignatureListFormat(VariableName
, VendorGuid
, Payload
, PayloadSize
);
1094 if (EFI_ERROR (Status
)) {
1098 Status
= UpdateVariable (
1107 &((EFI_VARIABLE_AUTHENTICATION_2
*) Data
)->TimeStamp
1109 if (EFI_ERROR(Status
)) {
1113 if ((mPlatformMode
!= SETUP_MODE
) || IsPk
) {
1114 Status
= VendorKeyIsModified ();
1116 } else if (mPlatformMode
== USER_MODE
) {
1118 // Verify against X509 Cert in PK database.
1120 Status
= VerifyTimeBasedPayload (
1132 // Verify against the certificate in data payload.
1134 Status
= VerifyTimeBasedPayload (
1146 if (!EFI_ERROR(Status
) && IsPk
) {
1147 if (mPlatformMode
== SETUP_MODE
&& !Del
) {
1149 // If enroll PK in setup mode, need change to user mode.
1151 Status
= UpdatePlatformMode (USER_MODE
);
1152 } else if (mPlatformMode
== USER_MODE
&& Del
){
1154 // If delete PK in user mode, need change to setup mode.
1156 Status
= UpdatePlatformMode (SETUP_MODE
);
1164 Process variable with key exchange key for verification.
1166 Caution: This function may receive untrusted input.
1167 This function may be invoked in SMM mode, and datasize and data are external input.
1168 This function will do basic validation, before parse the data.
1169 This function will parse the authentication carefully to avoid security issues, like
1170 buffer overflow, integer overflow.
1171 This function will check attribute carefully to avoid authentication bypass.
1173 @param[in] VariableName Name of Variable to be found.
1174 @param[in] VendorGuid Variable vendor GUID.
1175 @param[in] Data Data pointer.
1176 @param[in] DataSize Size of Data found. If size is less than the
1177 data, this value contains the required size.
1178 @param[in] Variable The variable information which is used to keep track of variable usage.
1179 @param[in] Attributes Attribute value of the variable.
1181 @return EFI_INVALID_PARAMETER Invalid parameter.
1182 @return EFI_SECURITY_VIOLATION The variable does NOT pass the validation
1183 check carried out by the firmware.
1184 @return EFI_SUCCESS Variable pass validation successfully.
1189 IN CHAR16
*VariableName
,
1190 IN EFI_GUID
*VendorGuid
,
1193 IN VARIABLE_POINTER_TRACK
*Variable
,
1194 IN UINT32 Attributes OPTIONAL
1201 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0 ||
1202 (Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) == 0) {
1204 // DB, DBX and DBT should set EFI_VARIABLE_NON_VOLATILE attribute and should be a time-based
1205 // authenticated variable.
1207 return EFI_INVALID_PARAMETER
;
1210 Status
= EFI_SUCCESS
;
1211 if (mPlatformMode
== USER_MODE
&& !(InCustomMode() && UserPhysicalPresent())) {
1213 // Time-based, verify against X509 Cert KEK.
1215 return VerifyTimeBasedPayload (
1227 // If in setup mode or custom secure boot mode, no authentication needed.
1229 Payload
= (UINT8
*) Data
+ AUTHINFO2_SIZE (Data
);
1230 PayloadSize
= DataSize
- AUTHINFO2_SIZE (Data
);
1232 Status
= CheckSignatureListFormat(VariableName
, VendorGuid
, Payload
, PayloadSize
);
1233 if (EFI_ERROR (Status
)) {
1237 Status
= UpdateVariable (
1246 &((EFI_VARIABLE_AUTHENTICATION_2
*) Data
)->TimeStamp
1248 if (EFI_ERROR (Status
)) {
1252 if (mPlatformMode
!= SETUP_MODE
) {
1253 Status
= VendorKeyIsModified ();
1261 Check if it is to delete auth variable.
1263 @param[in] Data Data pointer.
1264 @param[in] DataSize Size of Data.
1265 @param[in] Variable The variable information which is used to keep track of variable usage.
1266 @param[in] Attributes Attribute value of the variable.
1268 @retval TRUE It is to delete auth variable.
1269 @retval FALSE It is not to delete auth variable.
1273 IsDeleteAuthVariable (
1276 IN VARIABLE_POINTER_TRACK
*Variable
,
1277 IN UINT32 Attributes
1286 // To delete a variable created with the EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
1287 // or the EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS attribute,
1288 // SetVariable must be used with attributes matching the existing variable
1289 // and the DataSize set to the size of the AuthInfo descriptor.
1291 if ((Variable
->CurrPtr
!= NULL
) &&
1292 (Attributes
== Variable
->CurrPtr
->Attributes
) &&
1293 ((Attributes
& (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
)) != 0)) {
1294 if ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) {
1295 PayloadSize
= DataSize
- AUTHINFO2_SIZE (Data
);
1296 if (PayloadSize
== 0) {
1300 PayloadSize
= DataSize
- AUTHINFO_SIZE
;
1301 if (PayloadSize
== 0) {
1311 Process variable with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS/EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set
1313 Caution: This function may receive untrusted input.
1314 This function may be invoked in SMM mode, and datasize and data are external input.
1315 This function will do basic validation, before parse the data.
1316 This function will parse the authentication carefully to avoid security issues, like
1317 buffer overflow, integer overflow.
1318 This function will check attribute carefully to avoid authentication bypass.
1320 @param[in] VariableName Name of Variable to be found.
1321 @param[in] VendorGuid Variable vendor GUID.
1323 @param[in] Data Data pointer.
1324 @param[in] DataSize Size of Data.
1325 @param[in] Variable The variable information which is used to keep track of variable usage.
1326 @param[in] Attributes Attribute value of the variable.
1328 @return EFI_INVALID_PARAMETER Invalid parameter.
1329 @return EFI_WRITE_PROTECTED Variable is write-protected and needs authentication with
1330 EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS set.
1331 @return EFI_OUT_OF_RESOURCES The Database to save the public key is full.
1332 @return EFI_SECURITY_VIOLATION The variable is with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
1333 set, but the AuthInfo does NOT pass the validation
1334 check carried out by the firmware.
1335 @return EFI_SUCCESS Variable is not write-protected or pass validation successfully.
1340 IN CHAR16
*VariableName
,
1341 IN EFI_GUID
*VendorGuid
,
1344 IN VARIABLE_POINTER_TRACK
*Variable
,
1345 IN UINT32 Attributes
1350 BOOLEAN IsFirstTime
;
1352 EFI_VARIABLE_AUTHENTICATION
*CertData
;
1353 EFI_CERT_BLOCK_RSA_2048_SHA256
*CertBlock
;
1355 UINT64 MonotonicCount
;
1356 VARIABLE_ENTRY_CONSISTENCY VariableDataEntry
;
1363 Status
= EFI_SUCCESS
;
1365 if (IsDeleteAuthVariable (Data
, DataSize
, Variable
, Attributes
) && UserPhysicalPresent()) {
1367 // Allow the delete operation of common authenticated variable at user physical presence.
1369 if ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) {
1370 Status
= DeleteCertsFromDb (VariableName
, VendorGuid
);
1372 if (!EFI_ERROR (Status
)) {
1373 Status
= UpdateVariable (
1388 if (NeedPhysicallyPresent (VariableName
, VendorGuid
) && !UserPhysicalPresent()) {
1390 // This variable is protected, only physical present user could modify its value.
1392 return EFI_SECURITY_VIOLATION
;
1396 // A time-based authenticated variable and a count-based authenticated variable
1397 // can't be updated by each other.
1399 if (Variable
->CurrPtr
!= NULL
) {
1400 if (((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) &&
1401 ((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0)) {
1402 return EFI_SECURITY_VIOLATION
;
1405 if (((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) &&
1406 ((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0)) {
1407 return EFI_SECURITY_VIOLATION
;
1412 // Process Time-based Authenticated variable.
1414 if ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) {
1415 return VerifyTimeBasedPayload (
1428 // Determine if first time SetVariable with the EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS.
1430 if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) {
1432 // Determine current operation type.
1434 if (DataSize
== AUTHINFO_SIZE
) {
1438 // Determine whether this is the first time with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS set.
1440 if (Variable
->CurrPtr
== NULL
) {
1442 } else if ((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) == 0) {
1445 KeyIndex
= Variable
->CurrPtr
->PubKeyIndex
;
1446 IsFirstTime
= FALSE
;
1448 } else if ((Variable
->CurrPtr
!= NULL
) &&
1449 ((Variable
->CurrPtr
->Attributes
& (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
)) != 0)
1452 // If the variable is already write-protected, it always needs authentication before update.
1454 return EFI_WRITE_PROTECTED
;
1457 // If without EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS, set and attributes collision.
1458 // That means it is not authenticated variable, just update variable as usual.
1460 Status
= UpdateVariable (VariableName
, VendorGuid
, Data
, DataSize
, Attributes
, 0, 0, Variable
, NULL
);
1465 // Get PubKey and check Monotonic Count value corresponding to the variable.
1467 CertData
= (EFI_VARIABLE_AUTHENTICATION
*) Data
;
1468 CertBlock
= (EFI_CERT_BLOCK_RSA_2048_SHA256
*) (CertData
->AuthInfo
.CertData
);
1469 PubKey
= CertBlock
->PublicKey
;
1472 // Update Monotonic Count value.
1474 MonotonicCount
= CertData
->MonotonicCount
;
1478 // 2 cases need to check here
1479 // 1. Internal PubKey variable. PubKeyIndex is always 0
1480 // 2. Other counter-based AuthVariable. Check input PubKey.
1482 if (KeyIndex
== 0 || CompareMem (PubKey
, mPubKeyStore
+ (KeyIndex
- 1) * EFI_CERT_TYPE_RSA2048_SIZE
, EFI_CERT_TYPE_RSA2048_SIZE
) != 0) {
1483 return EFI_SECURITY_VIOLATION
;
1486 // Compare the current monotonic count and ensure that it is greater than the last SetVariable
1487 // operation with the EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS attribute set.
1489 if (CertData
->MonotonicCount
<= Variable
->CurrPtr
->MonotonicCount
) {
1491 // Monotonic count check fail, suspicious replay attack, return EFI_SECURITY_VIOLATION.
1493 return EFI_SECURITY_VIOLATION
;
1497 // Verify the certificate in Data payload.
1499 Status
= VerifyCounterBasedPayload (Data
, DataSize
, PubKey
);
1500 if (EFI_ERROR (Status
)) {
1505 // Now, the signature has been verified!
1507 if (IsFirstTime
&& !IsDeletion
) {
1508 VariableDataEntry
.VariableSize
= DataSize
- AUTHINFO_SIZE
;
1509 VariableDataEntry
.Guid
= VendorGuid
;
1510 VariableDataEntry
.Name
= VariableName
;
1513 // Update public key database variable if need.
1515 KeyIndex
= AddPubKeyInStore (PubKey
, &VariableDataEntry
);
1516 if (KeyIndex
== 0) {
1517 return EFI_OUT_OF_RESOURCES
;
1522 // Verification pass.
1524 return UpdateVariable (VariableName
, VendorGuid
, (UINT8
*)Data
+ AUTHINFO_SIZE
, DataSize
- AUTHINFO_SIZE
, Attributes
, KeyIndex
, MonotonicCount
, Variable
, NULL
);
1528 Merge two buffers which formatted as EFI_SIGNATURE_LIST. Only the new EFI_SIGNATURE_DATA
1529 will be appended to the original EFI_SIGNATURE_LIST, duplicate EFI_SIGNATURE_DATA
1532 @param[in, out] Data Pointer to original EFI_SIGNATURE_LIST.
1533 @param[in] DataSize Size of Data buffer.
1534 @param[in] FreeBufSize Size of free data buffer
1535 @param[in] NewData Pointer to new EFI_SIGNATURE_LIST to be appended.
1536 @param[in] NewDataSize Size of NewData buffer.
1537 @param[out] MergedBufSize Size of the merged buffer
1539 @return EFI_BUFFER_TOO_SMALL if input Data buffer overflowed
1543 AppendSignatureList (
1546 IN UINTN FreeBufSize
,
1548 IN UINTN NewDataSize
,
1549 OUT UINTN
*MergedBufSize
1552 EFI_SIGNATURE_LIST
*CertList
;
1553 EFI_SIGNATURE_DATA
*Cert
;
1555 EFI_SIGNATURE_LIST
*NewCertList
;
1556 EFI_SIGNATURE_DATA
*NewCert
;
1563 UINTN SignatureListSize
;
1566 Tail
= (UINT8
*) Data
+ DataSize
;
1568 NewCertList
= (EFI_SIGNATURE_LIST
*) NewData
;
1569 while ((NewDataSize
> 0) && (NewDataSize
>= NewCertList
->SignatureListSize
)) {
1570 NewCert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) NewCertList
+ sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
);
1571 NewCertCount
= (NewCertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - NewCertList
->SignatureHeaderSize
) / NewCertList
->SignatureSize
;
1574 for (Index
= 0; Index
< NewCertCount
; Index
++) {
1578 CertList
= (EFI_SIGNATURE_LIST
*) Data
;
1579 while ((Size
> 0) && (Size
>= CertList
->SignatureListSize
)) {
1580 if (CompareGuid (&CertList
->SignatureType
, &NewCertList
->SignatureType
) &&
1581 (CertList
->SignatureSize
== NewCertList
->SignatureSize
)) {
1582 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
1583 CertCount
= (CertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - CertList
->SignatureHeaderSize
) / CertList
->SignatureSize
;
1584 for (Index2
= 0; Index2
< CertCount
; Index2
++) {
1586 // Iterate each Signature Data in this Signature List.
1588 if (CompareMem (NewCert
, Cert
, CertList
->SignatureSize
) == 0) {
1592 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) Cert
+ CertList
->SignatureSize
);
1599 Size
-= CertList
->SignatureListSize
;
1600 CertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) CertList
+ CertList
->SignatureListSize
);
1605 // New EFI_SIGNATURE_DATA, append it.
1607 if (CopiedCount
== 0) {
1608 if (FreeBufSize
< sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
) {
1609 return EFI_BUFFER_TOO_SMALL
;
1613 // Copy EFI_SIGNATURE_LIST header for only once.
1616 CopyMem (Tail
, NewCertList
, sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
);
1617 Tail
= Tail
+ sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
;
1618 FreeBufSize
-= sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
;
1621 if (FreeBufSize
< NewCertList
->SignatureSize
) {
1622 return EFI_BUFFER_TOO_SMALL
;
1624 CopyMem (Tail
, NewCert
, NewCertList
->SignatureSize
);
1625 Tail
+= NewCertList
->SignatureSize
;
1626 FreeBufSize
-= NewCertList
->SignatureSize
;
1630 NewCert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) NewCert
+ NewCertList
->SignatureSize
);
1634 // Update SignatureListSize in newly appended EFI_SIGNATURE_LIST.
1636 if (CopiedCount
!= 0) {
1637 SignatureListSize
= sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
+ (CopiedCount
* NewCertList
->SignatureSize
);
1638 CertList
= (EFI_SIGNATURE_LIST
*) (Tail
- SignatureListSize
);
1639 CertList
->SignatureListSize
= (UINT32
) SignatureListSize
;
1642 NewDataSize
-= NewCertList
->SignatureListSize
;
1643 NewCertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) NewCertList
+ NewCertList
->SignatureListSize
);
1646 *MergedBufSize
= (Tail
- (UINT8
*) Data
);
1651 Compare two EFI_TIME data.
1654 @param FirstTime A pointer to the first EFI_TIME data.
1655 @param SecondTime A pointer to the second EFI_TIME data.
1657 @retval TRUE The FirstTime is not later than the SecondTime.
1658 @retval FALSE The FirstTime is later than the SecondTime.
1663 IN EFI_TIME
*FirstTime
,
1664 IN EFI_TIME
*SecondTime
1667 if (FirstTime
->Year
!= SecondTime
->Year
) {
1668 return (BOOLEAN
) (FirstTime
->Year
< SecondTime
->Year
);
1669 } else if (FirstTime
->Month
!= SecondTime
->Month
) {
1670 return (BOOLEAN
) (FirstTime
->Month
< SecondTime
->Month
);
1671 } else if (FirstTime
->Day
!= SecondTime
->Day
) {
1672 return (BOOLEAN
) (FirstTime
->Day
< SecondTime
->Day
);
1673 } else if (FirstTime
->Hour
!= SecondTime
->Hour
) {
1674 return (BOOLEAN
) (FirstTime
->Hour
< SecondTime
->Hour
);
1675 } else if (FirstTime
->Minute
!= SecondTime
->Minute
) {
1676 return (BOOLEAN
) (FirstTime
->Minute
< SecondTime
->Minute
);
1679 return (BOOLEAN
) (FirstTime
->Second
<= SecondTime
->Second
);
1683 Find matching signer's certificates for common authenticated variable
1684 by corresponding VariableName and VendorGuid from "certdb".
1686 The data format of "certdb":
1688 // UINT32 CertDbListSize;
1689 // /// AUTH_CERT_DB_DATA Certs1[];
1690 // /// AUTH_CERT_DB_DATA Certs2[];
1692 // /// AUTH_CERT_DB_DATA Certsn[];
1695 @param[in] VariableName Name of authenticated Variable.
1696 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1697 @param[in] Data Pointer to variable "certdb".
1698 @param[in] DataSize Size of variable "certdb".
1699 @param[out] CertOffset Offset of matching CertData, from starting of Data.
1700 @param[out] CertDataSize Length of CertData in bytes.
1701 @param[out] CertNodeOffset Offset of matching AUTH_CERT_DB_DATA , from
1703 @param[out] CertNodeSize Length of AUTH_CERT_DB_DATA in bytes.
1705 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1706 @retval EFI_NOT_FOUND Fail to find matching certs.
1707 @retval EFI_SUCCESS Find matching certs and output parameters.
1712 IN CHAR16
*VariableName
,
1713 IN EFI_GUID
*VendorGuid
,
1716 OUT UINT32
*CertOffset
, OPTIONAL
1717 OUT UINT32
*CertDataSize
, OPTIONAL
1718 OUT UINT32
*CertNodeOffset
,OPTIONAL
1719 OUT UINT32
*CertNodeSize OPTIONAL
1723 AUTH_CERT_DB_DATA
*Ptr
;
1727 UINT32 CertDbListSize
;
1729 if ((VariableName
== NULL
) || (VendorGuid
== NULL
) || (Data
== NULL
)) {
1730 return EFI_INVALID_PARAMETER
;
1734 // Check whether DataSize matches recorded CertDbListSize.
1736 if (DataSize
< sizeof (UINT32
)) {
1737 return EFI_INVALID_PARAMETER
;
1740 CertDbListSize
= ReadUnaligned32 ((UINT32
*) Data
);
1742 if (CertDbListSize
!= (UINT32
) DataSize
) {
1743 return EFI_INVALID_PARAMETER
;
1746 Offset
= sizeof (UINT32
);
1749 // Get corresponding certificates by VendorGuid and VariableName.
1751 while (Offset
< (UINT32
) DataSize
) {
1752 Ptr
= (AUTH_CERT_DB_DATA
*) (Data
+ Offset
);
1754 // Check whether VendorGuid matches.
1756 if (CompareGuid (&Ptr
->VendorGuid
, VendorGuid
)) {
1757 NodeSize
= ReadUnaligned32 (&Ptr
->CertNodeSize
);
1758 NameSize
= ReadUnaligned32 (&Ptr
->NameSize
);
1759 CertSize
= ReadUnaligned32 (&Ptr
->CertDataSize
);
1761 if (NodeSize
!= sizeof (EFI_GUID
) + sizeof (UINT32
) * 3 + CertSize
+
1762 sizeof (CHAR16
) * NameSize
) {
1763 return EFI_INVALID_PARAMETER
;
1766 Offset
= Offset
+ sizeof (EFI_GUID
) + sizeof (UINT32
) * 3;
1768 // Check whether VariableName matches.
1770 if ((NameSize
== StrLen (VariableName
)) &&
1771 (CompareMem (Data
+ Offset
, VariableName
, NameSize
* sizeof (CHAR16
)) == 0)) {
1772 Offset
= Offset
+ NameSize
* sizeof (CHAR16
);
1774 if (CertOffset
!= NULL
) {
1775 *CertOffset
= Offset
;
1778 if (CertDataSize
!= NULL
) {
1779 *CertDataSize
= CertSize
;
1782 if (CertNodeOffset
!= NULL
) {
1783 *CertNodeOffset
= (UINT32
) ((UINT8
*) Ptr
- Data
);
1786 if (CertNodeSize
!= NULL
) {
1787 *CertNodeSize
= NodeSize
;
1792 Offset
= Offset
+ NameSize
* sizeof (CHAR16
) + CertSize
;
1795 NodeSize
= ReadUnaligned32 (&Ptr
->CertNodeSize
);
1796 Offset
= Offset
+ NodeSize
;
1800 return EFI_NOT_FOUND
;
1804 Retrieve signer's certificates for common authenticated variable
1805 by corresponding VariableName and VendorGuid from "certdb".
1807 @param[in] VariableName Name of authenticated Variable.
1808 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1809 @param[out] CertData Pointer to signer's certificates.
1810 @param[out] CertDataSize Length of CertData in bytes.
1812 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1813 @retval EFI_NOT_FOUND Fail to find "certdb" or matching certs.
1814 @retval EFI_SUCCESS Get signer's certificates successfully.
1819 IN CHAR16
*VariableName
,
1820 IN EFI_GUID
*VendorGuid
,
1821 OUT UINT8
**CertData
,
1822 OUT UINT32
*CertDataSize
1825 VARIABLE_POINTER_TRACK CertDbVariable
;
1831 if ((VariableName
== NULL
) || (VendorGuid
== NULL
) || (CertData
== NULL
) || (CertDataSize
== NULL
)) {
1832 return EFI_INVALID_PARAMETER
;
1836 // Get variable "certdb".
1838 Status
= FindVariable (
1842 &mVariableModuleGlobal
->VariableGlobal
,
1845 if (EFI_ERROR (Status
)) {
1849 DataSize
= DataSizeOfVariable (CertDbVariable
.CurrPtr
);
1850 Data
= GetVariableDataPtr (CertDbVariable
.CurrPtr
);
1851 if ((DataSize
== 0) || (Data
== NULL
)) {
1853 return EFI_NOT_FOUND
;
1856 Status
= FindCertsFromDb (
1867 if (EFI_ERROR (Status
)) {
1871 *CertData
= Data
+ CertOffset
;
1876 Delete matching signer's certificates when deleting common authenticated
1877 variable by corresponding VariableName and VendorGuid from "certdb".
1879 @param[in] VariableName Name of authenticated Variable.
1880 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1882 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1883 @retval EFI_NOT_FOUND Fail to find "certdb" or matching certs.
1884 @retval EFI_OUT_OF_RESOURCES The operation is failed due to lack of resources.
1885 @retval EFI_SUCCESS The operation is completed successfully.
1890 IN CHAR16
*VariableName
,
1891 IN EFI_GUID
*VendorGuid
1894 VARIABLE_POINTER_TRACK CertDbVariable
;
1899 UINT32 CertNodeOffset
;
1900 UINT32 CertNodeSize
;
1902 UINT32 NewCertDbSize
;
1904 if ((VariableName
== NULL
) || (VendorGuid
== NULL
)) {
1905 return EFI_INVALID_PARAMETER
;
1909 // Get variable "certdb".
1911 Status
= FindVariable (
1915 &mVariableModuleGlobal
->VariableGlobal
,
1918 if (EFI_ERROR (Status
)) {
1922 DataSize
= DataSizeOfVariable (CertDbVariable
.CurrPtr
);
1923 Data
= GetVariableDataPtr (CertDbVariable
.CurrPtr
);
1924 if ((DataSize
== 0) || (Data
== NULL
)) {
1926 return EFI_NOT_FOUND
;
1929 if (DataSize
== sizeof (UINT32
)) {
1931 // There is no certs in certdb.
1937 // Get corresponding cert node from certdb.
1939 Status
= FindCertsFromDb (
1950 if (EFI_ERROR (Status
)) {
1954 if (DataSize
< (CertNodeOffset
+ CertNodeSize
)) {
1955 return EFI_NOT_FOUND
;
1959 // Construct new data content of variable "certdb".
1961 NewCertDbSize
= (UINT32
) DataSize
- CertNodeSize
;
1962 NewCertDb
= (UINT8
*) mCertDbStore
;
1965 // Copy the DB entries before deleting node.
1967 CopyMem (NewCertDb
, Data
, CertNodeOffset
);
1969 // Update CertDbListSize.
1971 CopyMem (NewCertDb
, &NewCertDbSize
, sizeof (UINT32
));
1973 // Copy the DB entries after deleting node.
1975 if (DataSize
> (CertNodeOffset
+ CertNodeSize
)) {
1977 NewCertDb
+ CertNodeOffset
,
1978 Data
+ CertNodeOffset
+ CertNodeSize
,
1979 DataSize
- CertNodeOffset
- CertNodeSize
1986 VarAttr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
1987 Status
= UpdateVariable (
2003 Insert signer's certificates for common authenticated variable with VariableName
2004 and VendorGuid in AUTH_CERT_DB_DATA to "certdb".
2006 @param[in] VariableName Name of authenticated Variable.
2007 @param[in] VendorGuid Vendor GUID of authenticated Variable.
2008 @param[in] CertData Pointer to signer's certificates.
2009 @param[in] CertDataSize Length of CertData in bytes.
2011 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
2012 @retval EFI_ACCESS_DENIED An AUTH_CERT_DB_DATA entry with same VariableName
2013 and VendorGuid already exists.
2014 @retval EFI_OUT_OF_RESOURCES The operation is failed due to lack of resources.
2015 @retval EFI_SUCCESS Insert an AUTH_CERT_DB_DATA entry to "certdb"
2020 IN CHAR16
*VariableName
,
2021 IN EFI_GUID
*VendorGuid
,
2023 IN UINTN CertDataSize
2026 VARIABLE_POINTER_TRACK CertDbVariable
;
2032 UINT32 NewCertDbSize
;
2033 UINT32 CertNodeSize
;
2035 AUTH_CERT_DB_DATA
*Ptr
;
2037 if ((VariableName
== NULL
) || (VendorGuid
== NULL
) || (CertData
== NULL
)) {
2038 return EFI_INVALID_PARAMETER
;
2042 // Get variable "certdb".
2044 Status
= FindVariable (
2048 &mVariableModuleGlobal
->VariableGlobal
,
2051 if (EFI_ERROR (Status
)) {
2055 DataSize
= DataSizeOfVariable (CertDbVariable
.CurrPtr
);
2056 Data
= GetVariableDataPtr (CertDbVariable
.CurrPtr
);
2057 if ((DataSize
== 0) || (Data
== NULL
)) {
2059 return EFI_NOT_FOUND
;
2063 // Find whether matching cert node already exists in "certdb".
2064 // If yes return error.
2066 Status
= FindCertsFromDb (
2077 if (!EFI_ERROR (Status
)) {
2079 return EFI_ACCESS_DENIED
;
2083 // Construct new data content of variable "certdb".
2085 NameSize
= (UINT32
) StrLen (VariableName
);
2086 CertNodeSize
= sizeof (AUTH_CERT_DB_DATA
) + (UINT32
) CertDataSize
+ NameSize
* sizeof (CHAR16
);
2087 NewCertDbSize
= (UINT32
) DataSize
+ CertNodeSize
;
2088 if (NewCertDbSize
> mMaxCertDbSize
) {
2089 return EFI_OUT_OF_RESOURCES
;
2091 NewCertDb
= (UINT8
*) mCertDbStore
;
2094 // Copy the DB entries before deleting node.
2096 CopyMem (NewCertDb
, Data
, DataSize
);
2098 // Update CertDbListSize.
2100 CopyMem (NewCertDb
, &NewCertDbSize
, sizeof (UINT32
));
2102 // Construct new cert node.
2104 Ptr
= (AUTH_CERT_DB_DATA
*) (NewCertDb
+ DataSize
);
2105 CopyGuid (&Ptr
->VendorGuid
, VendorGuid
);
2106 CopyMem (&Ptr
->CertNodeSize
, &CertNodeSize
, sizeof (UINT32
));
2107 CopyMem (&Ptr
->NameSize
, &NameSize
, sizeof (UINT32
));
2108 CopyMem (&Ptr
->CertDataSize
, &CertDataSize
, sizeof (UINT32
));
2111 (UINT8
*) Ptr
+ sizeof (AUTH_CERT_DB_DATA
),
2113 NameSize
* sizeof (CHAR16
)
2117 (UINT8
*) Ptr
+ sizeof (AUTH_CERT_DB_DATA
) + NameSize
* sizeof (CHAR16
),
2125 VarAttr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
2126 Status
= UpdateVariable (
2142 Process variable with EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set
2144 Caution: This function may receive untrusted input.
2145 This function may be invoked in SMM mode, and datasize and data are external input.
2146 This function will do basic validation, before parse the data.
2147 This function will parse the authentication carefully to avoid security issues, like
2148 buffer overflow, integer overflow.
2150 @param[in] VariableName Name of Variable to be found.
2151 @param[in] VendorGuid Variable vendor GUID.
2152 @param[in] Data Data pointer.
2153 @param[in] DataSize Size of Data found. If size is less than the
2154 data, this value contains the required size.
2155 @param[in] Variable The variable information which is used to keep track of variable usage.
2156 @param[in] Attributes Attribute value of the variable.
2157 @param[in] AuthVarType Verify against PK, KEK database, private database or certificate in data payload.
2158 @param[out] VarDel Delete the variable or not.
2160 @retval EFI_INVALID_PARAMETER Invalid parameter.
2161 @retval EFI_SECURITY_VIOLATION The variable does NOT pass the validation
2162 check carried out by the firmware.
2163 @retval EFI_OUT_OF_RESOURCES Failed to process variable due to lack
2165 @retval EFI_SUCCESS Variable pass validation successfully.
2169 VerifyTimeBasedPayload (
2170 IN CHAR16
*VariableName
,
2171 IN EFI_GUID
*VendorGuid
,
2174 IN VARIABLE_POINTER_TRACK
*Variable
,
2175 IN UINT32 Attributes
,
2176 IN AUTHVAR_TYPE AuthVarType
,
2190 BOOLEAN VerifyStatus
;
2192 EFI_SIGNATURE_LIST
*CertList
;
2193 EFI_SIGNATURE_DATA
*Cert
;
2194 VARIABLE_POINTER_TRACK KekVariable
;
2195 EFI_VARIABLE_AUTHENTICATION_2
*CertData
;
2198 VARIABLE_POINTER_TRACK PkVariable
;
2202 UINTN CertStackSize
;
2203 UINT8
*CertsInCertDb
;
2204 UINT32 CertsSizeinDb
;
2206 VerifyStatus
= FALSE
;
2212 CertsInCertDb
= NULL
;
2215 // When the attribute EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS is
2216 // set, then the Data buffer shall begin with an instance of a complete (and serialized)
2217 // EFI_VARIABLE_AUTHENTICATION_2 descriptor. The descriptor shall be followed by the new
2218 // variable value and DataSize shall reflect the combined size of the descriptor and the new
2219 // variable value. The authentication descriptor is not part of the variable data and is not
2220 // returned by subsequent calls to GetVariable().
2222 CertData
= (EFI_VARIABLE_AUTHENTICATION_2
*) Data
;
2225 // Verify that Pad1, Nanosecond, TimeZone, Daylight and Pad2 components of the
2226 // TimeStamp value are set to zero.
2228 if ((CertData
->TimeStamp
.Pad1
!= 0) ||
2229 (CertData
->TimeStamp
.Nanosecond
!= 0) ||
2230 (CertData
->TimeStamp
.TimeZone
!= 0) ||
2231 (CertData
->TimeStamp
.Daylight
!= 0) ||
2232 (CertData
->TimeStamp
.Pad2
!= 0)) {
2233 return EFI_SECURITY_VIOLATION
;
2236 if ((Variable
->CurrPtr
!= NULL
) && ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0)) {
2237 if (CompareTimeStamp (&CertData
->TimeStamp
, &Variable
->CurrPtr
->TimeStamp
)) {
2239 // TimeStamp check fail, suspicious replay attack, return EFI_SECURITY_VIOLATION.
2241 return EFI_SECURITY_VIOLATION
;
2246 // wCertificateType should be WIN_CERT_TYPE_EFI_GUID.
2247 // Cert type should be EFI_CERT_TYPE_PKCS7_GUID.
2249 if ((CertData
->AuthInfo
.Hdr
.wCertificateType
!= WIN_CERT_TYPE_EFI_GUID
) ||
2250 !CompareGuid (&CertData
->AuthInfo
.CertType
, &gEfiCertPkcs7Guid
)) {
2252 // Invalid AuthInfo type, return EFI_SECURITY_VIOLATION.
2254 return EFI_SECURITY_VIOLATION
;
2258 // Find out Pkcs7 SignedData which follows the EFI_VARIABLE_AUTHENTICATION_2 descriptor.
2259 // AuthInfo.Hdr.dwLength is the length of the entire certificate, including the length of the header.
2261 SigData
= CertData
->AuthInfo
.CertData
;
2262 SigDataSize
= CertData
->AuthInfo
.Hdr
.dwLength
- (UINT32
) (OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID
, CertData
));
2265 // Find out the new data payload which follows Pkcs7 SignedData directly.
2267 PayloadPtr
= SigData
+ SigDataSize
;
2268 PayloadSize
= DataSize
- OFFSET_OF_AUTHINFO2_CERT_DATA
- (UINTN
) SigDataSize
;
2271 // Construct a serialization buffer of the values of the VariableName, VendorGuid and Attributes
2272 // parameters of the SetVariable() call and the TimeStamp component of the
2273 // EFI_VARIABLE_AUTHENTICATION_2 descriptor followed by the variable's new value
2274 // i.e. (VariableName, VendorGuid, Attributes, TimeStamp, Data)
2276 NewDataSize
= PayloadSize
+ sizeof (EFI_TIME
) + sizeof (UINT32
) +
2277 sizeof (EFI_GUID
) + StrSize (VariableName
) - sizeof (CHAR16
);
2279 // Here is to reuse scratch data area(at the end of volatile variable store)
2280 // to reduce SMRAM consumption for SMM variable driver.
2281 // The scratch buffer is enough to hold the serialized data and safe to use,
2282 // because it will be used at here to do verification only first
2283 // and then used in UpdateVariable() for a time based auth variable set.
2285 NewData
= (UINT8
*) GetEndPointer ((VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
));
2288 Length
= StrLen (VariableName
) * sizeof (CHAR16
);
2289 CopyMem (Buffer
, VariableName
, Length
);
2292 Length
= sizeof (EFI_GUID
);
2293 CopyMem (Buffer
, VendorGuid
, Length
);
2296 Length
= sizeof (UINT32
);
2297 CopyMem (Buffer
, &Attr
, Length
);
2300 Length
= sizeof (EFI_TIME
);
2301 CopyMem (Buffer
, &CertData
->TimeStamp
, Length
);
2304 CopyMem (Buffer
, PayloadPtr
, PayloadSize
);
2306 if (AuthVarType
== AuthVarTypePk
) {
2308 // Verify that the signature has been made with the current Platform Key (no chaining for PK).
2309 // First, get signer's certificates from SignedData.
2311 VerifyStatus
= Pkcs7GetSigners (
2319 if (!VerifyStatus
) {
2324 // Second, get the current platform key from variable. Check whether it's identical with signer's certificates
2325 // in SignedData. If not, return error immediately.
2327 Status
= FindVariable (
2328 EFI_PLATFORM_KEY_NAME
,
2329 &gEfiGlobalVariableGuid
,
2331 &mVariableModuleGlobal
->VariableGlobal
,
2334 if (EFI_ERROR (Status
)) {
2335 VerifyStatus
= FALSE
;
2338 CertList
= (EFI_SIGNATURE_LIST
*) GetVariableDataPtr (PkVariable
.CurrPtr
);
2339 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
2340 if ((RootCertSize
!= (CertList
->SignatureSize
- (sizeof (EFI_SIGNATURE_DATA
) - 1))) ||
2341 (CompareMem (Cert
->SignatureData
, RootCert
, RootCertSize
) != 0)) {
2342 VerifyStatus
= FALSE
;
2347 // Verify Pkcs7 SignedData via Pkcs7Verify library.
2349 VerifyStatus
= Pkcs7Verify (
2358 } else if (AuthVarType
== AuthVarTypeKek
) {
2361 // Get KEK database from variable.
2363 Status
= FindVariable (
2364 EFI_KEY_EXCHANGE_KEY_NAME
,
2365 &gEfiGlobalVariableGuid
,
2367 &mVariableModuleGlobal
->VariableGlobal
,
2370 if (EFI_ERROR (Status
)) {
2375 // Ready to verify Pkcs7 SignedData. Go through KEK Signature Database to find out X.509 CertList.
2377 KekDataSize
= KekVariable
.CurrPtr
->DataSize
;
2378 CertList
= (EFI_SIGNATURE_LIST
*) GetVariableDataPtr (KekVariable
.CurrPtr
);
2379 while ((KekDataSize
> 0) && (KekDataSize
>= CertList
->SignatureListSize
)) {
2380 if (CompareGuid (&CertList
->SignatureType
, &gEfiCertX509Guid
)) {
2381 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
2382 CertCount
= (CertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - CertList
->SignatureHeaderSize
) / CertList
->SignatureSize
;
2383 for (Index
= 0; Index
< CertCount
; Index
++) {
2385 // Iterate each Signature Data Node within this CertList for a verify
2387 RootCert
= Cert
->SignatureData
;
2388 RootCertSize
= CertList
->SignatureSize
- (sizeof (EFI_SIGNATURE_DATA
) - 1);
2391 // Verify Pkcs7 SignedData via Pkcs7Verify library.
2393 VerifyStatus
= Pkcs7Verify (
2404 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) Cert
+ CertList
->SignatureSize
);
2407 KekDataSize
-= CertList
->SignatureListSize
;
2408 CertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) CertList
+ CertList
->SignatureListSize
);
2410 } else if (AuthVarType
== AuthVarTypePriv
) {
2413 // Process common authenticated variable except PK/KEK/DB/DBX/DBT.
2414 // Get signer's certificates from SignedData.
2416 VerifyStatus
= Pkcs7GetSigners (
2424 if (!VerifyStatus
) {
2429 // Get previously stored signer's certificates from certdb for existing
2430 // variable. Check whether they are identical with signer's certificates
2431 // in SignedData. If not, return error immediately.
2433 if ((Variable
->CurrPtr
!= NULL
)) {
2434 VerifyStatus
= FALSE
;
2436 Status
= GetCertsFromDb (VariableName
, VendorGuid
, &CertsInCertDb
, &CertsSizeinDb
);
2437 if (EFI_ERROR (Status
)) {
2441 if ((CertStackSize
!= CertsSizeinDb
) ||
2442 (CompareMem (SignerCerts
, CertsInCertDb
, CertsSizeinDb
) != 0)) {
2447 VerifyStatus
= Pkcs7Verify (
2455 if (!VerifyStatus
) {
2460 // Delete signer's certificates when delete the common authenticated variable.
2462 if ((PayloadSize
== 0) && (Variable
->CurrPtr
!= NULL
) && ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0)) {
2463 Status
= DeleteCertsFromDb (VariableName
, VendorGuid
);
2464 if (EFI_ERROR (Status
)) {
2465 VerifyStatus
= FALSE
;
2468 } else if (Variable
->CurrPtr
== NULL
&& PayloadSize
!= 0) {
2470 // Insert signer's certificates when adding a new common authenticated variable.
2472 Status
= InsertCertsToDb (VariableName
, VendorGuid
, SignerCerts
, CertStackSize
);
2473 if (EFI_ERROR (Status
)) {
2474 VerifyStatus
= FALSE
;
2478 } else if (AuthVarType
== AuthVarTypePayload
) {
2479 CertList
= (EFI_SIGNATURE_LIST
*) PayloadPtr
;
2480 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
2481 RootCert
= Cert
->SignatureData
;
2482 RootCertSize
= CertList
->SignatureSize
- (sizeof (EFI_SIGNATURE_DATA
) - 1);
2484 // Verify Pkcs7 SignedData via Pkcs7Verify library.
2486 VerifyStatus
= Pkcs7Verify (
2495 return EFI_SECURITY_VIOLATION
;
2500 if (AuthVarType
== AuthVarTypePk
|| AuthVarType
== AuthVarTypePriv
) {
2501 Pkcs7FreeSigners (RootCert
);
2502 Pkcs7FreeSigners (SignerCerts
);
2505 if (!VerifyStatus
) {
2506 return EFI_SECURITY_VIOLATION
;
2509 Status
= CheckSignatureListFormat(VariableName
, VendorGuid
, PayloadPtr
, PayloadSize
);
2510 if (EFI_ERROR (Status
)) {
2514 if ((PayloadSize
== 0) && (VarDel
!= NULL
)) {
2519 // Final step: Update/Append Variable if it pass Pkcs7Verify
2521 return UpdateVariable (
2530 &CertData
->TimeStamp