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 // The serialization of the values of the VariableName, VendorGuid and Attributes
60 // parameters of the SetVariable() call and the TimeStamp component of the
61 // EFI_VARIABLE_AUTHENTICATION_2 descriptor followed by the variable's new value
62 // i.e. (VariableName, VendorGuid, Attributes, TimeStamp, Data)
64 UINT8
*mSerializationRuntimeBuffer
= NULL
;
67 // Requirement for different signature type which have been defined in UEFI spec.
68 // These data are used to peform SignatureList format check while setting PK/KEK variable.
70 EFI_SIGNATURE_ITEM mSupportSigItem
[] = {
71 //{SigType, SigHeaderSize, SigDataSize }
72 {EFI_CERT_SHA256_GUID
, 0, 32 },
73 {EFI_CERT_RSA2048_GUID
, 0, 256 },
74 {EFI_CERT_RSA2048_SHA256_GUID
, 0, 256 },
75 {EFI_CERT_SHA1_GUID
, 0, 20 },
76 {EFI_CERT_RSA2048_SHA1_GUID
, 0, 256 },
77 {EFI_CERT_X509_GUID
, 0, ((UINT32
) ~0)},
78 {EFI_CERT_SHA224_GUID
, 0, 28 },
79 {EFI_CERT_SHA384_GUID
, 0, 48 },
80 {EFI_CERT_SHA512_GUID
, 0, 64 },
81 {EFI_CERT_X509_SHA256_GUID
, 0, 48 },
82 {EFI_CERT_X509_SHA384_GUID
, 0, 64 },
83 {EFI_CERT_X509_SHA512_GUID
, 0, 80 }
87 Determine whether this operation needs a physical present user.
89 @param[in] VariableName Name of the Variable.
90 @param[in] VendorGuid GUID of the Variable.
92 @retval TRUE This variable is protected, only a physical present user could set this variable.
93 @retval FALSE This variable is not protected.
97 NeedPhysicallyPresent(
98 IN CHAR16
*VariableName
,
99 IN EFI_GUID
*VendorGuid
102 if ((CompareGuid (VendorGuid
, &gEfiSecureBootEnableDisableGuid
) && (StrCmp (VariableName
, EFI_SECURE_BOOT_ENABLE_NAME
) == 0))
103 || (CompareGuid (VendorGuid
, &gEfiCustomModeEnableGuid
) && (StrCmp (VariableName
, EFI_CUSTOM_MODE_NAME
) == 0))) {
111 Determine whether the platform is operating in Custom Secure Boot mode.
113 @retval TRUE The platform is operating in Custom mode.
114 @retval FALSE The platform is operating in Standard mode.
122 VARIABLE_POINTER_TRACK Variable
;
124 FindVariable (EFI_CUSTOM_MODE_NAME
, &gEfiCustomModeEnableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
125 if (Variable
.CurrPtr
!= NULL
&& *(GetVariableDataPtr (Variable
.CurrPtr
)) == CUSTOM_SECURE_BOOT_MODE
) {
133 Initializes for authenticated varibale service.
135 @retval EFI_SUCCESS Function successfully executed.
136 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resources.
140 AutenticatedVariableServiceInitialize (
145 VARIABLE_POINTER_TRACK Variable
;
146 VARIABLE_POINTER_TRACK PkVariable
;
152 UINT8 SecureBootMode
;
153 UINT8 SecureBootEnable
;
158 // Initialize hash context.
160 CtxSize
= Sha256GetContextSize ();
161 mHashCtx
= AllocateRuntimePool (CtxSize
);
162 if (mHashCtx
== NULL
) {
163 return EFI_OUT_OF_RESOURCES
;
167 // Reserve runtime buffer for public key database. The size excludes variable header and name size.
169 mMaxKeyDbSize
= PcdGet32 (PcdMaxVariableSize
) - sizeof (VARIABLE_HEADER
) - sizeof (AUTHVAR_KEYDB_NAME
);
170 mMaxKeyNumber
= mMaxKeyDbSize
/ EFI_CERT_TYPE_RSA2048_SIZE
;
171 mPubKeyStore
= AllocateRuntimePool (mMaxKeyDbSize
);
172 if (mPubKeyStore
== NULL
) {
173 return EFI_OUT_OF_RESOURCES
;
177 // Reserve runtime buffer for certificate database. The size excludes variable header and name size.
179 mMaxCertDbSize
= PcdGet32 (PcdMaxVariableSize
) - sizeof (VARIABLE_HEADER
) - sizeof (EFI_CERT_DB_NAME
);
180 mCertDbStore
= AllocateRuntimePool (mMaxCertDbSize
);
181 if (mCertDbStore
== NULL
) {
182 return EFI_OUT_OF_RESOURCES
;
186 // Prepare runtime buffer for serialized data of time-based authenticated
187 // Variable, i.e. (VariableName, VendorGuid, Attributes, TimeStamp, Data).
189 mSerializationRuntimeBuffer
= AllocateRuntimePool (PcdGet32 (PcdMaxVariableSize
) + sizeof (EFI_GUID
) + sizeof (UINT32
) + sizeof (EFI_TIME
));
190 if (mSerializationRuntimeBuffer
== NULL
) {
191 return EFI_OUT_OF_RESOURCES
;
195 // Check "AuthVarKeyDatabase" variable's existence.
196 // If it doesn't exist, create a new one with initial value of 0 and EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS set.
198 Status
= FindVariable (
200 &gEfiAuthenticatedVariableGuid
,
202 &mVariableModuleGlobal
->VariableGlobal
,
206 if (Variable
.CurrPtr
== NULL
) {
207 VarAttr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
;
210 Status
= UpdateVariable (
212 &gEfiAuthenticatedVariableGuid
,
221 if (EFI_ERROR (Status
)) {
226 // Load database in global variable for cache.
228 DataSize
= DataSizeOfVariable (Variable
.CurrPtr
);
229 Data
= GetVariableDataPtr (Variable
.CurrPtr
);
230 ASSERT ((DataSize
!= 0) && (Data
!= NULL
));
232 // "AuthVarKeyDatabase" is an internal variable. Its DataSize is always ensured not to exceed mPubKeyStore buffer size(See definition before)
233 // Therefore, there is no memory overflow in underlying CopyMem.
235 CopyMem (mPubKeyStore
, (UINT8
*) Data
, DataSize
);
236 mPubKeyNumber
= (UINT32
) (DataSize
/ EFI_CERT_TYPE_RSA2048_SIZE
);
239 FindVariable (EFI_PLATFORM_KEY_NAME
, &gEfiGlobalVariableGuid
, &PkVariable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
240 if (PkVariable
.CurrPtr
== NULL
) {
241 DEBUG ((EFI_D_INFO
, "Variable %s does not exist.\n", EFI_PLATFORM_KEY_NAME
));
243 DEBUG ((EFI_D_INFO
, "Variable %s exists.\n", EFI_PLATFORM_KEY_NAME
));
247 // Create "SetupMode" variable with BS+RT attribute set.
249 FindVariable (EFI_SETUP_MODE_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
250 if (PkVariable
.CurrPtr
== NULL
) {
251 mPlatformMode
= SETUP_MODE
;
253 mPlatformMode
= USER_MODE
;
255 Status
= UpdateVariable (
257 &gEfiGlobalVariableGuid
,
260 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
266 if (EFI_ERROR (Status
)) {
271 // Create "SignatureSupport" variable with BS+RT attribute set.
273 FindVariable (EFI_SIGNATURE_SUPPORT_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
274 Status
= UpdateVariable (
275 EFI_SIGNATURE_SUPPORT_NAME
,
276 &gEfiGlobalVariableGuid
,
278 sizeof(mSignatureSupport
),
279 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
285 if (EFI_ERROR (Status
)) {
290 // If "SecureBootEnable" variable exists, then update "SecureBoot" variable.
291 // If "SecureBootEnable" variable is SECURE_BOOT_ENABLE and in USER_MODE, Set "SecureBoot" variable to SECURE_BOOT_MODE_ENABLE.
292 // If "SecureBootEnable" variable is SECURE_BOOT_DISABLE, Set "SecureBoot" variable to SECURE_BOOT_MODE_DISABLE.
294 SecureBootEnable
= SECURE_BOOT_DISABLE
;
295 FindVariable (EFI_SECURE_BOOT_ENABLE_NAME
, &gEfiSecureBootEnableDisableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
296 if (Variable
.CurrPtr
!= NULL
) {
297 SecureBootEnable
= *(GetVariableDataPtr (Variable
.CurrPtr
));
298 } else if (mPlatformMode
== USER_MODE
) {
300 // "SecureBootEnable" not exist, initialize it in USER_MODE.
302 SecureBootEnable
= SECURE_BOOT_ENABLE
;
303 Status
= UpdateVariable (
304 EFI_SECURE_BOOT_ENABLE_NAME
,
305 &gEfiSecureBootEnableDisableGuid
,
308 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
314 if (EFI_ERROR (Status
)) {
320 // Create "SecureBoot" variable with BS+RT attribute set.
322 if (SecureBootEnable
== SECURE_BOOT_ENABLE
&& mPlatformMode
== USER_MODE
) {
323 SecureBootMode
= SECURE_BOOT_MODE_ENABLE
;
325 SecureBootMode
= SECURE_BOOT_MODE_DISABLE
;
327 FindVariable (EFI_SECURE_BOOT_MODE_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
328 Status
= UpdateVariable (
329 EFI_SECURE_BOOT_MODE_NAME
,
330 &gEfiGlobalVariableGuid
,
333 EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
339 if (EFI_ERROR (Status
)) {
343 DEBUG ((EFI_D_INFO
, "Variable %s is %x\n", EFI_SETUP_MODE_NAME
, mPlatformMode
));
344 DEBUG ((EFI_D_INFO
, "Variable %s is %x\n", EFI_SECURE_BOOT_MODE_NAME
, SecureBootMode
));
345 DEBUG ((EFI_D_INFO
, "Variable %s is %x\n", EFI_SECURE_BOOT_ENABLE_NAME
, SecureBootEnable
));
348 // Initialize "CustomMode" in STANDARD_SECURE_BOOT_MODE state.
350 FindVariable (EFI_CUSTOM_MODE_NAME
, &gEfiCustomModeEnableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
351 CustomMode
= STANDARD_SECURE_BOOT_MODE
;
352 Status
= UpdateVariable (
353 EFI_CUSTOM_MODE_NAME
,
354 &gEfiCustomModeEnableGuid
,
357 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
363 if (EFI_ERROR (Status
)) {
367 DEBUG ((EFI_D_INFO
, "Variable %s is %x\n", EFI_CUSTOM_MODE_NAME
, CustomMode
));
370 // Check "certdb" variable's existence.
371 // If it doesn't exist, then create a new one with
372 // EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set.
374 Status
= FindVariable (
378 &mVariableModuleGlobal
->VariableGlobal
,
382 if (Variable
.CurrPtr
== NULL
) {
383 VarAttr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
384 ListSize
= sizeof (UINT32
);
385 Status
= UpdateVariable (
396 if (EFI_ERROR (Status
)) {
402 // Check "VendorKeysNv" variable's existence and create "VendorKeys" variable accordingly.
404 FindVariable (EFI_VENDOR_KEYS_NV_VARIABLE_NAME
, &gEfiVendorKeysNvGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
405 if (Variable
.CurrPtr
!= NULL
) {
406 mVendorKeyState
= *(GetVariableDataPtr (Variable
.CurrPtr
));
409 // "VendorKeysNv" not exist, initialize it in VENDOR_KEYS_VALID state.
411 mVendorKeyState
= VENDOR_KEYS_VALID
;
412 Status
= UpdateVariable (
413 EFI_VENDOR_KEYS_NV_VARIABLE_NAME
,
414 &gEfiVendorKeysNvGuid
,
417 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
,
423 if (EFI_ERROR (Status
)) {
429 // Create "VendorKeys" variable with BS+RT attribute set.
431 FindVariable (EFI_VENDOR_KEYS_VARIABLE_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
432 Status
= UpdateVariable (
433 EFI_VENDOR_KEYS_VARIABLE_NAME
,
434 &gEfiGlobalVariableGuid
,
437 EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
443 if (EFI_ERROR (Status
)) {
447 DEBUG ((EFI_D_INFO
, "Variable %s is %x\n", EFI_VENDOR_KEYS_VARIABLE_NAME
, mVendorKeyState
));
453 Add public key in store and return its index.
455 @param[in] PubKey Input pointer to Public Key data
456 @param[in] VariableDataEntry The variable data entry
458 @return Index of new added item
464 IN VARIABLE_ENTRY_CONSISTENCY
*VariableDataEntry
470 VARIABLE_POINTER_TRACK Variable
;
474 VARIABLE_ENTRY_CONSISTENCY PublicKeyEntry
;
477 if (PubKey
== NULL
) {
481 Status
= FindVariable (
483 &gEfiAuthenticatedVariableGuid
,
485 &mVariableModuleGlobal
->VariableGlobal
,
488 if (EFI_ERROR (Status
)) {
489 DEBUG ((EFI_D_ERROR
, "Get public key database variable failure, Status = %r\n", Status
));
494 // Check whether the public key entry does exist.
497 for (Ptr
= mPubKeyStore
, Index
= 1; Index
<= mPubKeyNumber
; Index
++) {
498 if (CompareMem (Ptr
, PubKey
, EFI_CERT_TYPE_RSA2048_SIZE
) == 0) {
502 Ptr
+= EFI_CERT_TYPE_RSA2048_SIZE
;
507 // Add public key in database.
509 if (mPubKeyNumber
== mMaxKeyNumber
) {
511 // Public key dadatase is full, try to reclaim invalid key.
515 // NV storage can't reclaim at runtime.
521 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
522 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
529 if (EFI_ERROR (Status
)) {
533 Status
= FindVariable (
535 &gEfiAuthenticatedVariableGuid
,
537 &mVariableModuleGlobal
->VariableGlobal
,
540 if (EFI_ERROR (Status
)) {
541 DEBUG ((EFI_D_ERROR
, "Get public key database variable failure, Status = %r\n", Status
));
545 DataSize
= DataSizeOfVariable (Variable
.CurrPtr
);
546 Data
= GetVariableDataPtr (Variable
.CurrPtr
);
547 ASSERT ((DataSize
!= 0) && (Data
!= NULL
));
549 // "AuthVarKeyDatabase" is an internal used variable. Its DataSize is always ensured not to exceed mPubKeyStore buffer size(See definition before)
550 // Therefore, there is no memory overflow in underlying CopyMem.
552 CopyMem (mPubKeyStore
, (UINT8
*) Data
, DataSize
);
553 mPubKeyNumber
= (UINT32
) (DataSize
/ EFI_CERT_TYPE_RSA2048_SIZE
);
555 if (mPubKeyNumber
== mMaxKeyNumber
) {
561 // Check the variable space for both public key and variable data.
563 PublicKeyEntry
.VariableSize
= (mPubKeyNumber
+ 1) * EFI_CERT_TYPE_RSA2048_SIZE
;
564 PublicKeyEntry
.Guid
= &gEfiAuthenticatedVariableGuid
;
565 PublicKeyEntry
.Name
= AUTHVAR_KEYDB_NAME
;
566 Attributes
= VARIABLE_ATTRIBUTE_NV_BS_RT
| EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
;
568 if (!CheckRemainingSpaceForConsistency (Attributes
, &PublicKeyEntry
, VariableDataEntry
, NULL
)) {
570 // No enough variable space.
575 CopyMem (mPubKeyStore
+ mPubKeyNumber
* EFI_CERT_TYPE_RSA2048_SIZE
, PubKey
, EFI_CERT_TYPE_RSA2048_SIZE
);
576 Index
= ++mPubKeyNumber
;
578 // Update public key database variable.
580 Status
= UpdateVariable (
582 &gEfiAuthenticatedVariableGuid
,
584 mPubKeyNumber
* EFI_CERT_TYPE_RSA2048_SIZE
,
591 if (EFI_ERROR (Status
)) {
592 DEBUG ((EFI_D_ERROR
, "Update public key database variable failure, Status = %r\n", Status
));
601 Verify data payload with AuthInfo in EFI_CERT_TYPE_RSA2048_SHA256_GUID type.
602 Follow the steps in UEFI2.2.
604 Caution: This function may receive untrusted input.
605 This function may be invoked in SMM mode, and datasize and data are external input.
606 This function will do basic validation, before parse the data.
607 This function will parse the authentication carefully to avoid security issues, like
608 buffer overflow, integer overflow.
610 @param[in] Data Pointer to data with AuthInfo.
611 @param[in] DataSize Size of Data.
612 @param[in] PubKey Public key used for verification.
614 @retval EFI_INVALID_PARAMETER Invalid parameter.
615 @retval EFI_SECURITY_VIOLATION If authentication failed.
616 @retval EFI_SUCCESS Authentication successful.
620 VerifyCounterBasedPayload (
627 EFI_VARIABLE_AUTHENTICATION
*CertData
;
628 EFI_CERT_BLOCK_RSA_2048_SHA256
*CertBlock
;
629 UINT8 Digest
[SHA256_DIGEST_SIZE
];
633 PayloadSize
= DataSize
- AUTHINFO_SIZE
;
638 if (Data
== NULL
|| PubKey
== NULL
) {
639 return EFI_INVALID_PARAMETER
;
642 CertData
= (EFI_VARIABLE_AUTHENTICATION
*) Data
;
643 CertBlock
= (EFI_CERT_BLOCK_RSA_2048_SHA256
*) (CertData
->AuthInfo
.CertData
);
646 // wCertificateType should be WIN_CERT_TYPE_EFI_GUID.
647 // Cert type should be EFI_CERT_TYPE_RSA2048_SHA256_GUID.
649 if ((CertData
->AuthInfo
.Hdr
.wCertificateType
!= WIN_CERT_TYPE_EFI_GUID
) ||
650 !CompareGuid (&CertData
->AuthInfo
.CertType
, &gEfiCertTypeRsa2048Sha256Guid
)
653 // Invalid AuthInfo type, return EFI_SECURITY_VIOLATION.
655 return EFI_SECURITY_VIOLATION
;
658 // Hash data payload with SHA256.
660 ZeroMem (Digest
, SHA256_DIGEST_SIZE
);
661 Status
= Sha256Init (mHashCtx
);
665 Status
= Sha256Update (mHashCtx
, Data
+ AUTHINFO_SIZE
, PayloadSize
);
672 Status
= Sha256Update (mHashCtx
, &PayloadSize
, sizeof (UINTN
));
677 // Hash Monotonic Count.
679 Status
= Sha256Update (mHashCtx
, &CertData
->MonotonicCount
, sizeof (UINT64
));
683 Status
= Sha256Final (mHashCtx
, Digest
);
688 // Generate & Initialize RSA Context.
691 ASSERT (Rsa
!= NULL
);
693 // Set RSA Key Components.
694 // NOTE: Only N and E are needed to be set as RSA public key for signature verification.
696 Status
= RsaSetKey (Rsa
, RsaKeyN
, PubKey
, EFI_CERT_TYPE_RSA2048_SIZE
);
700 Status
= RsaSetKey (Rsa
, RsaKeyE
, mRsaE
, sizeof (mRsaE
));
705 // Verify the signature.
707 Status
= RsaPkcs1Verify (
711 CertBlock
->Signature
,
712 EFI_CERT_TYPE_RSA2048_SHA256_SIZE
722 return EFI_SECURITY_VIOLATION
;
727 Update platform mode.
729 @param[in] Mode SETUP_MODE or USER_MODE.
731 @return EFI_INVALID_PARAMETER Invalid parameter.
732 @return EFI_SUCCESS Update platform mode successfully.
741 VARIABLE_POINTER_TRACK Variable
;
742 UINT8 SecureBootMode
;
743 UINT8 SecureBootEnable
;
744 UINTN VariableDataSize
;
746 Status
= FindVariable (
748 &gEfiGlobalVariableGuid
,
750 &mVariableModuleGlobal
->VariableGlobal
,
753 if (EFI_ERROR (Status
)) {
758 // Update the value of SetupMode variable by a simple mem copy, this could avoid possible
759 // variable storage reclaim at runtime.
761 mPlatformMode
= (UINT8
) Mode
;
762 CopyMem (GetVariableDataPtr (Variable
.CurrPtr
), &mPlatformMode
, sizeof(UINT8
));
766 // SecureBoot Variable indicates whether the platform firmware is operating
767 // in Secure boot mode (1) or not (0), so we should not change SecureBoot
768 // Variable in runtime.
774 // Check "SecureBoot" variable's existence.
775 // If it doesn't exist, firmware has no capability to perform driver signing verification,
776 // then set "SecureBoot" to 0.
778 Status
= FindVariable (
779 EFI_SECURE_BOOT_MODE_NAME
,
780 &gEfiGlobalVariableGuid
,
782 &mVariableModuleGlobal
->VariableGlobal
,
786 // If "SecureBoot" variable exists, then check "SetupMode" variable update.
787 // If "SetupMode" variable is USER_MODE, "SecureBoot" variable is set to 1.
788 // If "SetupMode" variable is SETUP_MODE, "SecureBoot" variable is set to 0.
790 if (Variable
.CurrPtr
== NULL
) {
791 SecureBootMode
= SECURE_BOOT_MODE_DISABLE
;
793 if (mPlatformMode
== USER_MODE
) {
794 SecureBootMode
= SECURE_BOOT_MODE_ENABLE
;
795 } else if (mPlatformMode
== SETUP_MODE
) {
796 SecureBootMode
= SECURE_BOOT_MODE_DISABLE
;
798 return EFI_NOT_FOUND
;
802 Status
= UpdateVariable (
803 EFI_SECURE_BOOT_MODE_NAME
,
804 &gEfiGlobalVariableGuid
,
807 EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
813 if (EFI_ERROR (Status
)) {
818 // Check "SecureBootEnable" variable's existence. It can enable/disable secure boot feature.
820 Status
= FindVariable (
821 EFI_SECURE_BOOT_ENABLE_NAME
,
822 &gEfiSecureBootEnableDisableGuid
,
824 &mVariableModuleGlobal
->VariableGlobal
,
828 if (SecureBootMode
== SECURE_BOOT_MODE_ENABLE
) {
830 // Create the "SecureBootEnable" variable as secure boot is enabled.
832 SecureBootEnable
= SECURE_BOOT_ENABLE
;
833 VariableDataSize
= sizeof (SecureBootEnable
);
836 // Delete the "SecureBootEnable" variable if this variable exist as "SecureBoot"
837 // variable is not in secure boot state.
839 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
842 SecureBootEnable
= SECURE_BOOT_DISABLE
;
843 VariableDataSize
= 0;
846 Status
= UpdateVariable (
847 EFI_SECURE_BOOT_ENABLE_NAME
,
848 &gEfiSecureBootEnableDisableGuid
,
851 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
861 Check input data form to make sure it is a valid EFI_SIGNATURE_LIST for PK/KEK/db/dbx/dbt variable.
863 @param[in] VariableName Name of Variable to be check.
864 @param[in] VendorGuid Variable vendor GUID.
865 @param[in] Data Point to the variable data to be checked.
866 @param[in] DataSize Size of Data.
868 @return EFI_INVALID_PARAMETER Invalid signature list format.
869 @return EFI_SUCCESS Passed signature list format check successfully.
873 CheckSignatureListFormat(
874 IN CHAR16
*VariableName
,
875 IN EFI_GUID
*VendorGuid
,
880 EFI_SIGNATURE_LIST
*SigList
;
886 EFI_SIGNATURE_DATA
*CertData
;
893 ASSERT (VariableName
!= NULL
&& VendorGuid
!= NULL
&& Data
!= NULL
);
895 if (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, EFI_PLATFORM_KEY_NAME
) == 0)){
897 } else if ((CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, EFI_KEY_EXCHANGE_KEY_NAME
) == 0)) ||
898 (CompareGuid (VendorGuid
, &gEfiImageSecurityDatabaseGuid
) &&
899 ((StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE
) == 0) || (StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE1
) == 0) ||
900 (StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE2
) == 0)))) {
907 SigList
= (EFI_SIGNATURE_LIST
*) Data
;
908 SigDataSize
= DataSize
;
912 // Walk throuth the input signature list and check the data format.
913 // If any signature is incorrectly formed, the whole check will fail.
915 while ((SigDataSize
> 0) && (SigDataSize
>= SigList
->SignatureListSize
)) {
916 for (Index
= 0; Index
< (sizeof (mSupportSigItem
) / sizeof (EFI_SIGNATURE_ITEM
)); Index
++ ) {
917 if (CompareGuid (&SigList
->SignatureType
, &mSupportSigItem
[Index
].SigType
)) {
919 // The value of SignatureSize should always be 16 (size of SignatureOwner
920 // component) add the data length according to signature type.
922 if (mSupportSigItem
[Index
].SigDataSize
!= ((UINT32
) ~0) &&
923 (SigList
->SignatureSize
- sizeof (EFI_GUID
)) != mSupportSigItem
[Index
].SigDataSize
) {
924 return EFI_INVALID_PARAMETER
;
926 if (mSupportSigItem
[Index
].SigHeaderSize
!= ((UINTN
) ~0) &&
927 SigList
->SignatureHeaderSize
!= mSupportSigItem
[Index
].SigHeaderSize
) {
928 return EFI_INVALID_PARAMETER
;
934 if (Index
== (sizeof (mSupportSigItem
) / sizeof (EFI_SIGNATURE_ITEM
))) {
936 // Undefined signature type.
938 return EFI_INVALID_PARAMETER
;
941 if (CompareGuid (&SigList
->SignatureType
, &gEfiCertX509Guid
)) {
943 // Try to retrieve the RSA public key from the X.509 certificate.
944 // If this operation fails, it's not a valid certificate.
946 RsaContext
= RsaNew ();
947 if (RsaContext
== NULL
) {
948 return EFI_INVALID_PARAMETER
;
950 CertData
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) SigList
+ sizeof (EFI_SIGNATURE_LIST
) + SigList
->SignatureHeaderSize
);
951 CertLen
= SigList
->SignatureSize
- sizeof (EFI_GUID
);
952 if (!RsaGetPublicKeyFromX509 (CertData
->SignatureData
, CertLen
, &RsaContext
)) {
953 RsaFree (RsaContext
);
954 return EFI_INVALID_PARAMETER
;
956 RsaFree (RsaContext
);
959 if ((SigList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - SigList
->SignatureHeaderSize
) % SigList
->SignatureSize
!= 0) {
960 return EFI_INVALID_PARAMETER
;
962 SigCount
+= (SigList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - SigList
->SignatureHeaderSize
) / SigList
->SignatureSize
;
964 SigDataSize
-= SigList
->SignatureListSize
;
965 SigList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) SigList
+ SigList
->SignatureListSize
);
968 if (((UINTN
) SigList
- (UINTN
) Data
) != DataSize
) {
969 return EFI_INVALID_PARAMETER
;
972 if (IsPk
&& SigCount
> 1) {
973 return EFI_INVALID_PARAMETER
;
980 Update "VendorKeys" variable to record the out of band secure boot key modification.
982 @return EFI_SUCCESS Variable is updated successfully.
983 @return Others Failed to update variable.
987 VendorKeyIsModified (
992 VARIABLE_POINTER_TRACK Variable
;
994 if (mVendorKeyState
== VENDOR_KEYS_MODIFIED
) {
997 mVendorKeyState
= VENDOR_KEYS_MODIFIED
;
999 FindVariable (EFI_VENDOR_KEYS_NV_VARIABLE_NAME
, &gEfiVendorKeysNvGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1000 Status
= UpdateVariable (
1001 EFI_VENDOR_KEYS_NV_VARIABLE_NAME
,
1002 &gEfiVendorKeysNvGuid
,
1005 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
,
1011 if (EFI_ERROR (Status
)) {
1015 FindVariable (EFI_VENDOR_KEYS_VARIABLE_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1016 return UpdateVariable (
1017 EFI_VENDOR_KEYS_VARIABLE_NAME
,
1018 &gEfiGlobalVariableGuid
,
1021 EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
1030 Process variable with platform key for verification.
1032 Caution: This function may receive untrusted input.
1033 This function may be invoked in SMM mode, and datasize and data are external input.
1034 This function will do basic validation, before parse the data.
1035 This function will parse the authentication carefully to avoid security issues, like
1036 buffer overflow, integer overflow.
1037 This function will check attribute carefully to avoid authentication bypass.
1039 @param[in] VariableName Name of Variable to be found.
1040 @param[in] VendorGuid Variable vendor GUID.
1041 @param[in] Data Data pointer.
1042 @param[in] DataSize Size of Data found. If size is less than the
1043 data, this value contains the required size.
1044 @param[in] Variable The variable information which is used to keep track of variable usage.
1045 @param[in] Attributes Attribute value of the variable
1046 @param[in] IsPk Indicate whether it is to process pk.
1048 @return EFI_INVALID_PARAMETER Invalid parameter.
1049 @return EFI_SECURITY_VIOLATION The variable does NOT pass the validation.
1050 check carried out by the firmware.
1051 @return EFI_SUCCESS Variable passed validation successfully.
1056 IN CHAR16
*VariableName
,
1057 IN EFI_GUID
*VendorGuid
,
1060 IN VARIABLE_POINTER_TRACK
*Variable
,
1061 IN UINT32 Attributes OPTIONAL
,
1070 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0 ||
1071 (Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) == 0) {
1073 // PK, KEK and db/dbx/dbt should set EFI_VARIABLE_NON_VOLATILE attribute and should be a time-based
1074 // authenticated variable.
1076 return EFI_INVALID_PARAMETER
;
1080 if ((InCustomMode() && UserPhysicalPresent()) || (mPlatformMode
== SETUP_MODE
&& !IsPk
)) {
1081 Payload
= (UINT8
*) Data
+ AUTHINFO2_SIZE (Data
);
1082 PayloadSize
= DataSize
- AUTHINFO2_SIZE (Data
);
1083 if (PayloadSize
== 0) {
1087 Status
= CheckSignatureListFormat(VariableName
, VendorGuid
, Payload
, PayloadSize
);
1088 if (EFI_ERROR (Status
)) {
1092 Status
= UpdateVariable (
1101 &((EFI_VARIABLE_AUTHENTICATION_2
*) Data
)->TimeStamp
1103 if (EFI_ERROR(Status
)) {
1107 if ((mPlatformMode
!= SETUP_MODE
) || IsPk
) {
1108 Status
= VendorKeyIsModified ();
1110 } else if (mPlatformMode
== USER_MODE
) {
1112 // Verify against X509 Cert in PK database.
1114 Status
= VerifyTimeBasedPayload (
1126 // Verify against the certificate in data payload.
1128 Status
= VerifyTimeBasedPayload (
1140 if (!EFI_ERROR(Status
) && IsPk
) {
1141 if (mPlatformMode
== SETUP_MODE
&& !Del
) {
1143 // If enroll PK in setup mode, need change to user mode.
1145 Status
= UpdatePlatformMode (USER_MODE
);
1146 } else if (mPlatformMode
== USER_MODE
&& Del
){
1148 // If delete PK in user mode, need change to setup mode.
1150 Status
= UpdatePlatformMode (SETUP_MODE
);
1158 Process variable with key exchange key for verification.
1160 Caution: This function may receive untrusted input.
1161 This function may be invoked in SMM mode, and datasize and data are external input.
1162 This function will do basic validation, before parse the data.
1163 This function will parse the authentication carefully to avoid security issues, like
1164 buffer overflow, integer overflow.
1165 This function will check attribute carefully to avoid authentication bypass.
1167 @param[in] VariableName Name of Variable to be found.
1168 @param[in] VendorGuid Variable vendor GUID.
1169 @param[in] Data Data pointer.
1170 @param[in] DataSize Size of Data found. If size is less than the
1171 data, this value contains the required size.
1172 @param[in] Variable The variable information which is used to keep track of variable usage.
1173 @param[in] Attributes Attribute value of the variable.
1175 @return EFI_INVALID_PARAMETER Invalid parameter.
1176 @return EFI_SECURITY_VIOLATION The variable does NOT pass the validation
1177 check carried out by the firmware.
1178 @return EFI_SUCCESS Variable pass validation successfully.
1183 IN CHAR16
*VariableName
,
1184 IN EFI_GUID
*VendorGuid
,
1187 IN VARIABLE_POINTER_TRACK
*Variable
,
1188 IN UINT32 Attributes OPTIONAL
1195 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0 ||
1196 (Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) == 0) {
1198 // DB, DBX and DBT should set EFI_VARIABLE_NON_VOLATILE attribute and should be a time-based
1199 // authenticated variable.
1201 return EFI_INVALID_PARAMETER
;
1204 Status
= EFI_SUCCESS
;
1205 if (mPlatformMode
== USER_MODE
&& !(InCustomMode() && UserPhysicalPresent())) {
1207 // Time-based, verify against X509 Cert KEK.
1209 return VerifyTimeBasedPayload (
1221 // If in setup mode or custom secure boot mode, no authentication needed.
1223 Payload
= (UINT8
*) Data
+ AUTHINFO2_SIZE (Data
);
1224 PayloadSize
= DataSize
- AUTHINFO2_SIZE (Data
);
1226 Status
= CheckSignatureListFormat(VariableName
, VendorGuid
, Payload
, PayloadSize
);
1227 if (EFI_ERROR (Status
)) {
1231 Status
= UpdateVariable (
1240 &((EFI_VARIABLE_AUTHENTICATION_2
*) Data
)->TimeStamp
1242 if (EFI_ERROR (Status
)) {
1246 if (mPlatformMode
!= SETUP_MODE
) {
1247 Status
= VendorKeyIsModified ();
1255 Check if it is to delete auth variable.
1257 @param[in] Data Data pointer.
1258 @param[in] DataSize Size of Data.
1259 @param[in] Variable The variable information which is used to keep track of variable usage.
1260 @param[in] Attributes Attribute value of the variable.
1262 @retval TRUE It is to delete auth variable.
1263 @retval FALSE It is not to delete auth variable.
1267 IsDeleteAuthVariable (
1270 IN VARIABLE_POINTER_TRACK
*Variable
,
1271 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 Payload
= (UINT8
*) Data
+ AUTHINFO2_SIZE (Data
);
1291 PayloadSize
= DataSize
- AUTHINFO2_SIZE (Data
);
1292 if (PayloadSize
== 0) {
1296 Payload
= (UINT8
*) Data
+ AUTHINFO_SIZE
;
1297 PayloadSize
= DataSize
- AUTHINFO_SIZE
;
1298 if (PayloadSize
== 0) {
1308 Process variable with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS/EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set
1310 Caution: This function may receive untrusted input.
1311 This function may be invoked in SMM mode, and datasize and data are external input.
1312 This function will do basic validation, before parse the data.
1313 This function will parse the authentication carefully to avoid security issues, like
1314 buffer overflow, integer overflow.
1315 This function will check attribute carefully to avoid authentication bypass.
1317 @param[in] VariableName Name of Variable to be found.
1318 @param[in] VendorGuid Variable vendor GUID.
1320 @param[in] Data Data pointer.
1321 @param[in] DataSize Size of Data.
1322 @param[in] Variable The variable information which is used to keep track of variable usage.
1323 @param[in] Attributes Attribute value of the variable.
1325 @return EFI_INVALID_PARAMETER Invalid parameter.
1326 @return EFI_WRITE_PROTECTED Variable is write-protected and needs authentication with
1327 EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS set.
1328 @return EFI_OUT_OF_RESOURCES The Database to save the public key is full.
1329 @return EFI_SECURITY_VIOLATION The variable is with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
1330 set, but the AuthInfo does NOT pass the validation
1331 check carried out by the firmware.
1332 @return EFI_SUCCESS Variable is not write-protected or pass validation successfully.
1337 IN CHAR16
*VariableName
,
1338 IN EFI_GUID
*VendorGuid
,
1341 IN VARIABLE_POINTER_TRACK
*Variable
,
1342 IN UINT32 Attributes
1347 BOOLEAN IsFirstTime
;
1349 EFI_VARIABLE_AUTHENTICATION
*CertData
;
1350 EFI_CERT_BLOCK_RSA_2048_SHA256
*CertBlock
;
1352 UINT64 MonotonicCount
;
1353 VARIABLE_ENTRY_CONSISTENCY VariableDataEntry
;
1360 Status
= EFI_SUCCESS
;
1362 if (UserPhysicalPresent()) {
1364 // Allow the delete operation of common authenticated variable at user physical presence.
1366 if (IsDeleteAuthVariable (Data
, DataSize
, Variable
, Attributes
)) {
1367 if ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) {
1368 Status
= DeleteCertsFromDb (VariableName
, VendorGuid
);
1370 if (!EFI_ERROR (Status
)) {
1371 Status
= UpdateVariable (
1386 if (NeedPhysicallyPresent(VariableName
, VendorGuid
)) {
1388 // This variable is protected, only physical present user could modify its value.
1390 return EFI_SECURITY_VIOLATION
;
1395 // A time-based authenticated variable and a count-based authenticated variable
1396 // can't be updated by each other.
1398 if (Variable
->CurrPtr
!= NULL
) {
1399 if (((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) &&
1400 ((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0)) {
1401 return EFI_SECURITY_VIOLATION
;
1404 if (((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) &&
1405 ((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0)) {
1406 return EFI_SECURITY_VIOLATION
;
1411 // Process Time-based Authenticated variable.
1413 if ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) {
1414 return VerifyTimeBasedPayload (
1427 // Determine if first time SetVariable with the EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS.
1429 if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) {
1431 // Determine current operation type.
1433 if (DataSize
== AUTHINFO_SIZE
) {
1437 // Determine whether this is the first time with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS set.
1439 if (Variable
->CurrPtr
== NULL
) {
1441 } else if ((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) == 0) {
1444 KeyIndex
= Variable
->CurrPtr
->PubKeyIndex
;
1445 IsFirstTime
= FALSE
;
1447 } else if ((Variable
->CurrPtr
!= NULL
) &&
1448 ((Variable
->CurrPtr
->Attributes
& (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
)) != 0)
1451 // If the variable is already write-protected, it always needs authentication before update.
1453 return EFI_WRITE_PROTECTED
;
1456 // If without EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS, set and attributes collision.
1457 // That means it is not authenticated variable, just update variable as usual.
1459 Status
= UpdateVariable (VariableName
, VendorGuid
, Data
, DataSize
, Attributes
, 0, 0, Variable
, NULL
);
1464 // Get PubKey and check Monotonic Count value corresponding to the variable.
1466 CertData
= (EFI_VARIABLE_AUTHENTICATION
*) Data
;
1467 CertBlock
= (EFI_CERT_BLOCK_RSA_2048_SHA256
*) (CertData
->AuthInfo
.CertData
);
1468 PubKey
= CertBlock
->PublicKey
;
1471 // Update Monotonic Count value.
1473 MonotonicCount
= CertData
->MonotonicCount
;
1477 // 2 cases need to check here
1478 // 1. Internal PubKey variable. PubKeyIndex is always 0
1479 // 2. Other counter-based AuthVariable. Check input PubKey.
1481 if (KeyIndex
== 0 || CompareMem (PubKey
, mPubKeyStore
+ (KeyIndex
- 1) * EFI_CERT_TYPE_RSA2048_SIZE
, EFI_CERT_TYPE_RSA2048_SIZE
) != 0) {
1482 return EFI_SECURITY_VIOLATION
;
1485 // Compare the current monotonic count and ensure that it is greater than the last SetVariable
1486 // operation with the EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS attribute set.
1488 if (CertData
->MonotonicCount
<= Variable
->CurrPtr
->MonotonicCount
) {
1490 // Monotonic count check fail, suspicious replay attack, return EFI_SECURITY_VIOLATION.
1492 return EFI_SECURITY_VIOLATION
;
1496 // Verify the certificate in Data payload.
1498 Status
= VerifyCounterBasedPayload (Data
, DataSize
, PubKey
);
1499 if (EFI_ERROR (Status
)) {
1504 // Now, the signature has been verified!
1506 if (IsFirstTime
&& !IsDeletion
) {
1507 VariableDataEntry
.VariableSize
= DataSize
- AUTHINFO_SIZE
;
1508 VariableDataEntry
.Guid
= VendorGuid
;
1509 VariableDataEntry
.Name
= VariableName
;
1512 // Update public key database variable if need.
1514 KeyIndex
= AddPubKeyInStore (PubKey
, &VariableDataEntry
);
1515 if (KeyIndex
== 0) {
1516 return EFI_OUT_OF_RESOURCES
;
1521 // Verification pass.
1523 return UpdateVariable (VariableName
, VendorGuid
, (UINT8
*)Data
+ AUTHINFO_SIZE
, DataSize
- AUTHINFO_SIZE
, Attributes
, KeyIndex
, MonotonicCount
, Variable
, NULL
);
1527 Merge two buffers which formatted as EFI_SIGNATURE_LIST. Only the new EFI_SIGNATURE_DATA
1528 will be appended to the original EFI_SIGNATURE_LIST, duplicate EFI_SIGNATURE_DATA
1531 @param[in, out] Data Pointer to original EFI_SIGNATURE_LIST.
1532 @param[in] DataSize Size of Data buffer.
1533 @param[in] FreeBufSize Size of free data buffer
1534 @param[in] NewData Pointer to new EFI_SIGNATURE_LIST to be appended.
1535 @param[in] NewDataSize Size of NewData buffer.
1536 @param[out] MergedBufSize Size of the merged buffer
1538 @return EFI_BUFFER_TOO_SMALL if input Data buffer overflowed
1542 AppendSignatureList (
1545 IN UINTN FreeBufSize
,
1547 IN UINTN NewDataSize
,
1548 OUT UINTN
*MergedBufSize
1551 EFI_SIGNATURE_LIST
*CertList
;
1552 EFI_SIGNATURE_DATA
*Cert
;
1554 EFI_SIGNATURE_LIST
*NewCertList
;
1555 EFI_SIGNATURE_DATA
*NewCert
;
1562 UINTN SignatureListSize
;
1565 Tail
= (UINT8
*) Data
+ DataSize
;
1567 NewCertList
= (EFI_SIGNATURE_LIST
*) NewData
;
1568 while ((NewDataSize
> 0) && (NewDataSize
>= NewCertList
->SignatureListSize
)) {
1569 NewCert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) NewCertList
+ sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
);
1570 NewCertCount
= (NewCertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - NewCertList
->SignatureHeaderSize
) / NewCertList
->SignatureSize
;
1573 for (Index
= 0; Index
< NewCertCount
; Index
++) {
1577 CertList
= (EFI_SIGNATURE_LIST
*) Data
;
1578 while ((Size
> 0) && (Size
>= CertList
->SignatureListSize
)) {
1579 if (CompareGuid (&CertList
->SignatureType
, &NewCertList
->SignatureType
) &&
1580 (CertList
->SignatureSize
== NewCertList
->SignatureSize
)) {
1581 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
1582 CertCount
= (CertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - CertList
->SignatureHeaderSize
) / CertList
->SignatureSize
;
1583 for (Index2
= 0; Index2
< CertCount
; Index2
++) {
1585 // Iterate each Signature Data in this Signature List.
1587 if (CompareMem (NewCert
, Cert
, CertList
->SignatureSize
) == 0) {
1591 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) Cert
+ CertList
->SignatureSize
);
1598 Size
-= CertList
->SignatureListSize
;
1599 CertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) CertList
+ CertList
->SignatureListSize
);
1604 // New EFI_SIGNATURE_DATA, append it.
1606 if (CopiedCount
== 0) {
1607 if (FreeBufSize
< sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
) {
1608 return EFI_BUFFER_TOO_SMALL
;
1612 // Copy EFI_SIGNATURE_LIST header for only once.
1615 CopyMem (Tail
, NewCertList
, sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
);
1616 Tail
= Tail
+ sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
;
1617 FreeBufSize
-= sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
;
1620 if (FreeBufSize
< NewCertList
->SignatureSize
) {
1621 return EFI_BUFFER_TOO_SMALL
;
1623 CopyMem (Tail
, NewCert
, NewCertList
->SignatureSize
);
1624 Tail
+= NewCertList
->SignatureSize
;
1625 FreeBufSize
-= NewCertList
->SignatureSize
;
1629 NewCert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) NewCert
+ NewCertList
->SignatureSize
);
1633 // Update SignatureListSize in newly appended EFI_SIGNATURE_LIST.
1635 if (CopiedCount
!= 0) {
1636 SignatureListSize
= sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
+ (CopiedCount
* NewCertList
->SignatureSize
);
1637 CertList
= (EFI_SIGNATURE_LIST
*) (Tail
- SignatureListSize
);
1638 CertList
->SignatureListSize
= (UINT32
) SignatureListSize
;
1641 NewDataSize
-= NewCertList
->SignatureListSize
;
1642 NewCertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) NewCertList
+ NewCertList
->SignatureListSize
);
1645 *MergedBufSize
= (Tail
- (UINT8
*) Data
);
1650 Compare two EFI_TIME data.
1653 @param FirstTime A pointer to the first EFI_TIME data.
1654 @param SecondTime A pointer to the second EFI_TIME data.
1656 @retval TRUE The FirstTime is not later than the SecondTime.
1657 @retval FALSE The FirstTime is later than the SecondTime.
1662 IN EFI_TIME
*FirstTime
,
1663 IN EFI_TIME
*SecondTime
1666 if (FirstTime
->Year
!= SecondTime
->Year
) {
1667 return (BOOLEAN
) (FirstTime
->Year
< SecondTime
->Year
);
1668 } else if (FirstTime
->Month
!= SecondTime
->Month
) {
1669 return (BOOLEAN
) (FirstTime
->Month
< SecondTime
->Month
);
1670 } else if (FirstTime
->Day
!= SecondTime
->Day
) {
1671 return (BOOLEAN
) (FirstTime
->Day
< SecondTime
->Day
);
1672 } else if (FirstTime
->Hour
!= SecondTime
->Hour
) {
1673 return (BOOLEAN
) (FirstTime
->Hour
< SecondTime
->Hour
);
1674 } else if (FirstTime
->Minute
!= SecondTime
->Minute
) {
1675 return (BOOLEAN
) (FirstTime
->Minute
< SecondTime
->Minute
);
1678 return (BOOLEAN
) (FirstTime
->Second
<= SecondTime
->Second
);
1682 Find matching signer's certificates for common authenticated variable
1683 by corresponding VariableName and VendorGuid from "certdb".
1685 The data format of "certdb":
1687 // UINT32 CertDbListSize;
1688 // /// AUTH_CERT_DB_DATA Certs1[];
1689 // /// AUTH_CERT_DB_DATA Certs2[];
1691 // /// AUTH_CERT_DB_DATA Certsn[];
1694 @param[in] VariableName Name of authenticated Variable.
1695 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1696 @param[in] Data Pointer to variable "certdb".
1697 @param[in] DataSize Size of variable "certdb".
1698 @param[out] CertOffset Offset of matching CertData, from starting of Data.
1699 @param[out] CertDataSize Length of CertData in bytes.
1700 @param[out] CertNodeOffset Offset of matching AUTH_CERT_DB_DATA , from
1702 @param[out] CertNodeSize Length of AUTH_CERT_DB_DATA in bytes.
1704 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1705 @retval EFI_NOT_FOUND Fail to find matching certs.
1706 @retval EFI_SUCCESS Find matching certs and output parameters.
1711 IN CHAR16
*VariableName
,
1712 IN EFI_GUID
*VendorGuid
,
1715 OUT UINT32
*CertOffset
, OPTIONAL
1716 OUT UINT32
*CertDataSize
, OPTIONAL
1717 OUT UINT32
*CertNodeOffset
,OPTIONAL
1718 OUT UINT32
*CertNodeSize OPTIONAL
1722 AUTH_CERT_DB_DATA
*Ptr
;
1726 UINT32 CertDbListSize
;
1728 if ((VariableName
== NULL
) || (VendorGuid
== NULL
) || (Data
== NULL
)) {
1729 return EFI_INVALID_PARAMETER
;
1733 // Check whether DataSize matches recorded CertDbListSize.
1735 if (DataSize
< sizeof (UINT32
)) {
1736 return EFI_INVALID_PARAMETER
;
1739 CertDbListSize
= ReadUnaligned32 ((UINT32
*) Data
);
1741 if (CertDbListSize
!= (UINT32
) DataSize
) {
1742 return EFI_INVALID_PARAMETER
;
1745 Offset
= sizeof (UINT32
);
1748 // Get corresponding certificates by VendorGuid and VariableName.
1750 while (Offset
< (UINT32
) DataSize
) {
1751 Ptr
= (AUTH_CERT_DB_DATA
*) (Data
+ Offset
);
1753 // Check whether VendorGuid matches.
1755 if (CompareGuid (&Ptr
->VendorGuid
, VendorGuid
)) {
1756 NodeSize
= ReadUnaligned32 (&Ptr
->CertNodeSize
);
1757 NameSize
= ReadUnaligned32 (&Ptr
->NameSize
);
1758 CertSize
= ReadUnaligned32 (&Ptr
->CertDataSize
);
1760 if (NodeSize
!= sizeof (EFI_GUID
) + sizeof (UINT32
) * 3 + CertSize
+
1761 sizeof (CHAR16
) * NameSize
) {
1762 return EFI_INVALID_PARAMETER
;
1765 Offset
= Offset
+ sizeof (EFI_GUID
) + sizeof (UINT32
) * 3;
1767 // Check whether VariableName matches.
1769 if ((NameSize
== StrLen (VariableName
)) &&
1770 (CompareMem (Data
+ Offset
, VariableName
, NameSize
* sizeof (CHAR16
)) == 0)) {
1771 Offset
= Offset
+ NameSize
* sizeof (CHAR16
);
1773 if (CertOffset
!= NULL
) {
1774 *CertOffset
= Offset
;
1777 if (CertDataSize
!= NULL
) {
1778 *CertDataSize
= CertSize
;
1781 if (CertNodeOffset
!= NULL
) {
1782 *CertNodeOffset
= (UINT32
) ((UINT8
*) Ptr
- Data
);
1785 if (CertNodeSize
!= NULL
) {
1786 *CertNodeSize
= NodeSize
;
1791 Offset
= Offset
+ NameSize
* sizeof (CHAR16
) + CertSize
;
1794 NodeSize
= ReadUnaligned32 (&Ptr
->CertNodeSize
);
1795 Offset
= Offset
+ NodeSize
;
1799 return EFI_NOT_FOUND
;
1803 Retrieve signer's certificates for common authenticated variable
1804 by corresponding VariableName and VendorGuid from "certdb".
1806 @param[in] VariableName Name of authenticated Variable.
1807 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1808 @param[out] CertData Pointer to signer's certificates.
1809 @param[out] CertDataSize Length of CertData in bytes.
1811 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1812 @retval EFI_NOT_FOUND Fail to find "certdb" or matching certs.
1813 @retval EFI_SUCCESS Get signer's certificates successfully.
1818 IN CHAR16
*VariableName
,
1819 IN EFI_GUID
*VendorGuid
,
1820 OUT UINT8
**CertData
,
1821 OUT UINT32
*CertDataSize
1824 VARIABLE_POINTER_TRACK CertDbVariable
;
1830 if ((VariableName
== NULL
) || (VendorGuid
== NULL
) || (CertData
== NULL
) || (CertDataSize
== NULL
)) {
1831 return EFI_INVALID_PARAMETER
;
1835 // Get variable "certdb".
1837 Status
= FindVariable (
1841 &mVariableModuleGlobal
->VariableGlobal
,
1844 if (EFI_ERROR (Status
)) {
1848 DataSize
= DataSizeOfVariable (CertDbVariable
.CurrPtr
);
1849 Data
= GetVariableDataPtr (CertDbVariable
.CurrPtr
);
1850 if ((DataSize
== 0) || (Data
== NULL
)) {
1852 return EFI_NOT_FOUND
;
1855 Status
= FindCertsFromDb (
1866 if (EFI_ERROR (Status
)) {
1870 *CertData
= Data
+ CertOffset
;
1875 Delete matching signer's certificates when deleting common authenticated
1876 variable by corresponding VariableName and VendorGuid from "certdb".
1878 @param[in] VariableName Name of authenticated Variable.
1879 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1881 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1882 @retval EFI_NOT_FOUND Fail to find "certdb" or matching certs.
1883 @retval EFI_OUT_OF_RESOURCES The operation is failed due to lack of resources.
1884 @retval EFI_SUCCESS The operation is completed successfully.
1889 IN CHAR16
*VariableName
,
1890 IN EFI_GUID
*VendorGuid
1893 VARIABLE_POINTER_TRACK CertDbVariable
;
1898 UINT32 CertNodeOffset
;
1899 UINT32 CertNodeSize
;
1901 UINT32 NewCertDbSize
;
1903 if ((VariableName
== NULL
) || (VendorGuid
== NULL
)) {
1904 return EFI_INVALID_PARAMETER
;
1908 // Get variable "certdb".
1910 Status
= FindVariable (
1914 &mVariableModuleGlobal
->VariableGlobal
,
1917 if (EFI_ERROR (Status
)) {
1921 DataSize
= DataSizeOfVariable (CertDbVariable
.CurrPtr
);
1922 Data
= GetVariableDataPtr (CertDbVariable
.CurrPtr
);
1923 if ((DataSize
== 0) || (Data
== NULL
)) {
1925 return EFI_NOT_FOUND
;
1928 if (DataSize
== sizeof (UINT32
)) {
1930 // There is no certs in certdb.
1936 // Get corresponding cert node from certdb.
1938 Status
= FindCertsFromDb (
1949 if (EFI_ERROR (Status
)) {
1953 if (DataSize
< (CertNodeOffset
+ CertNodeSize
)) {
1954 return EFI_NOT_FOUND
;
1958 // Construct new data content of variable "certdb".
1960 NewCertDbSize
= (UINT32
) DataSize
- CertNodeSize
;
1961 NewCertDb
= (UINT8
*) mCertDbStore
;
1964 // Copy the DB entries before deleting node.
1966 CopyMem (NewCertDb
, Data
, CertNodeOffset
);
1968 // Update CertDbListSize.
1970 CopyMem (NewCertDb
, &NewCertDbSize
, sizeof (UINT32
));
1972 // Copy the DB entries after deleting node.
1974 if (DataSize
> (CertNodeOffset
+ CertNodeSize
)) {
1976 NewCertDb
+ CertNodeOffset
,
1977 Data
+ CertNodeOffset
+ CertNodeSize
,
1978 DataSize
- CertNodeOffset
- CertNodeSize
1985 VarAttr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
1986 Status
= UpdateVariable (
2002 Insert signer's certificates for common authenticated variable with VariableName
2003 and VendorGuid in AUTH_CERT_DB_DATA to "certdb".
2005 @param[in] VariableName Name of authenticated Variable.
2006 @param[in] VendorGuid Vendor GUID of authenticated Variable.
2007 @param[in] CertData Pointer to signer's certificates.
2008 @param[in] CertDataSize Length of CertData in bytes.
2010 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
2011 @retval EFI_ACCESS_DENIED An AUTH_CERT_DB_DATA entry with same VariableName
2012 and VendorGuid already exists.
2013 @retval EFI_OUT_OF_RESOURCES The operation is failed due to lack of resources.
2014 @retval EFI_SUCCESS Insert an AUTH_CERT_DB_DATA entry to "certdb"
2019 IN CHAR16
*VariableName
,
2020 IN EFI_GUID
*VendorGuid
,
2022 IN UINTN CertDataSize
2025 VARIABLE_POINTER_TRACK CertDbVariable
;
2031 UINT32 NewCertDbSize
;
2032 UINT32 CertNodeSize
;
2034 AUTH_CERT_DB_DATA
*Ptr
;
2036 if ((VariableName
== NULL
) || (VendorGuid
== NULL
) || (CertData
== NULL
)) {
2037 return EFI_INVALID_PARAMETER
;
2041 // Get variable "certdb".
2043 Status
= FindVariable (
2047 &mVariableModuleGlobal
->VariableGlobal
,
2050 if (EFI_ERROR (Status
)) {
2054 DataSize
= DataSizeOfVariable (CertDbVariable
.CurrPtr
);
2055 Data
= GetVariableDataPtr (CertDbVariable
.CurrPtr
);
2056 if ((DataSize
== 0) || (Data
== NULL
)) {
2058 return EFI_NOT_FOUND
;
2062 // Find whether matching cert node already exists in "certdb".
2063 // If yes return error.
2065 Status
= FindCertsFromDb (
2076 if (!EFI_ERROR (Status
)) {
2078 return EFI_ACCESS_DENIED
;
2082 // Construct new data content of variable "certdb".
2084 NameSize
= (UINT32
) StrLen (VariableName
);
2085 CertNodeSize
= sizeof (AUTH_CERT_DB_DATA
) + (UINT32
) CertDataSize
+ NameSize
* sizeof (CHAR16
);
2086 NewCertDbSize
= (UINT32
) DataSize
+ CertNodeSize
;
2087 if (NewCertDbSize
> mMaxCertDbSize
) {
2088 return EFI_OUT_OF_RESOURCES
;
2090 NewCertDb
= (UINT8
*) mCertDbStore
;
2093 // Copy the DB entries before deleting node.
2095 CopyMem (NewCertDb
, Data
, DataSize
);
2097 // Update CertDbListSize.
2099 CopyMem (NewCertDb
, &NewCertDbSize
, sizeof (UINT32
));
2101 // Construct new cert node.
2103 Ptr
= (AUTH_CERT_DB_DATA
*) (NewCertDb
+ DataSize
);
2104 CopyGuid (&Ptr
->VendorGuid
, VendorGuid
);
2105 CopyMem (&Ptr
->CertNodeSize
, &CertNodeSize
, sizeof (UINT32
));
2106 CopyMem (&Ptr
->NameSize
, &NameSize
, sizeof (UINT32
));
2107 CopyMem (&Ptr
->CertDataSize
, &CertDataSize
, sizeof (UINT32
));
2110 (UINT8
*) Ptr
+ sizeof (AUTH_CERT_DB_DATA
),
2112 NameSize
* sizeof (CHAR16
)
2116 (UINT8
*) Ptr
+ sizeof (AUTH_CERT_DB_DATA
) + NameSize
* sizeof (CHAR16
),
2124 VarAttr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
2125 Status
= UpdateVariable (
2141 Process variable with EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set
2143 Caution: This function may receive untrusted input.
2144 This function may be invoked in SMM mode, and datasize and data are external input.
2145 This function will do basic validation, before parse the data.
2146 This function will parse the authentication carefully to avoid security issues, like
2147 buffer overflow, integer overflow.
2149 @param[in] VariableName Name of Variable to be found.
2150 @param[in] VendorGuid Variable vendor GUID.
2151 @param[in] Data Data pointer.
2152 @param[in] DataSize Size of Data found. If size is less than the
2153 data, this value contains the required size.
2154 @param[in] Variable The variable information which is used to keep track of variable usage.
2155 @param[in] Attributes Attribute value of the variable.
2156 @param[in] AuthVarType Verify against PK, KEK database, private database or certificate in data payload.
2157 @param[out] VarDel Delete the variable or not.
2159 @retval EFI_INVALID_PARAMETER Invalid parameter.
2160 @retval EFI_SECURITY_VIOLATION The variable does NOT pass the validation
2161 check carried out by the firmware.
2162 @retval EFI_OUT_OF_RESOURCES Failed to process variable due to lack
2164 @retval EFI_SUCCESS Variable pass validation successfully.
2168 VerifyTimeBasedPayload (
2169 IN CHAR16
*VariableName
,
2170 IN EFI_GUID
*VendorGuid
,
2173 IN VARIABLE_POINTER_TRACK
*Variable
,
2174 IN UINT32 Attributes
,
2175 IN AUTHVAR_TYPE AuthVarType
,
2189 BOOLEAN VerifyStatus
;
2191 EFI_SIGNATURE_LIST
*CertList
;
2192 EFI_SIGNATURE_DATA
*Cert
;
2193 VARIABLE_POINTER_TRACK KekVariable
;
2194 EFI_VARIABLE_AUTHENTICATION_2
*CertData
;
2197 VARIABLE_POINTER_TRACK PkVariable
;
2202 UINTN CertStackSize
;
2203 UINT8
*CertsInCertDb
;
2204 UINT32 CertsSizeinDb
;
2206 VerifyStatus
= FALSE
;
2213 CertsInCertDb
= NULL
;
2216 // When the attribute EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS is
2217 // set, then the Data buffer shall begin with an instance of a complete (and serialized)
2218 // EFI_VARIABLE_AUTHENTICATION_2 descriptor. The descriptor shall be followed by the new
2219 // variable value and DataSize shall reflect the combined size of the descriptor and the new
2220 // variable value. The authentication descriptor is not part of the variable data and is not
2221 // returned by subsequent calls to GetVariable().
2223 CertData
= (EFI_VARIABLE_AUTHENTICATION_2
*) Data
;
2226 // Verify that Pad1, Nanosecond, TimeZone, Daylight and Pad2 components of the
2227 // TimeStamp value are set to zero.
2229 if ((CertData
->TimeStamp
.Pad1
!= 0) ||
2230 (CertData
->TimeStamp
.Nanosecond
!= 0) ||
2231 (CertData
->TimeStamp
.TimeZone
!= 0) ||
2232 (CertData
->TimeStamp
.Daylight
!= 0) ||
2233 (CertData
->TimeStamp
.Pad2
!= 0)) {
2234 return EFI_SECURITY_VIOLATION
;
2237 if ((Variable
->CurrPtr
!= NULL
) && ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0)) {
2238 if (CompareTimeStamp (&CertData
->TimeStamp
, &Variable
->CurrPtr
->TimeStamp
)) {
2240 // TimeStamp check fail, suspicious replay attack, return EFI_SECURITY_VIOLATION.
2242 return EFI_SECURITY_VIOLATION
;
2247 // wCertificateType should be WIN_CERT_TYPE_EFI_GUID.
2248 // Cert type should be EFI_CERT_TYPE_PKCS7_GUID.
2250 if ((CertData
->AuthInfo
.Hdr
.wCertificateType
!= WIN_CERT_TYPE_EFI_GUID
) ||
2251 !CompareGuid (&CertData
->AuthInfo
.CertType
, &gEfiCertPkcs7Guid
)) {
2253 // Invalid AuthInfo type, return EFI_SECURITY_VIOLATION.
2255 return EFI_SECURITY_VIOLATION
;
2259 // Find out Pkcs7 SignedData which follows the EFI_VARIABLE_AUTHENTICATION_2 descriptor.
2260 // AuthInfo.Hdr.dwLength is the length of the entire certificate, including the length of the header.
2262 SigData
= CertData
->AuthInfo
.CertData
;
2263 SigDataSize
= CertData
->AuthInfo
.Hdr
.dwLength
- (UINT32
) (OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID
, CertData
));
2266 // Find out the new data payload which follows Pkcs7 SignedData directly.
2268 PayloadPtr
= SigData
+ SigDataSize
;
2269 PayloadSize
= DataSize
- OFFSET_OF_AUTHINFO2_CERT_DATA
- (UINTN
) SigDataSize
;
2272 // Construct a buffer to fill with (VariableName, VendorGuid, Attributes, TimeStamp, Data).
2274 NewDataSize
= PayloadSize
+ sizeof (EFI_TIME
) + sizeof (UINT32
) +
2275 sizeof (EFI_GUID
) + StrSize (VariableName
) - sizeof (CHAR16
);
2276 NewData
= mSerializationRuntimeBuffer
;
2279 Length
= StrLen (VariableName
) * sizeof (CHAR16
);
2280 CopyMem (Buffer
, VariableName
, Length
);
2283 Length
= sizeof (EFI_GUID
);
2284 CopyMem (Buffer
, VendorGuid
, Length
);
2287 Length
= sizeof (UINT32
);
2288 CopyMem (Buffer
, &Attr
, Length
);
2291 Length
= sizeof (EFI_TIME
);
2292 CopyMem (Buffer
, &CertData
->TimeStamp
, Length
);
2295 CopyMem (Buffer
, PayloadPtr
, PayloadSize
);
2297 if (AuthVarType
== AuthVarTypePk
) {
2299 // Verify that the signature has been made with the current Platform Key (no chaining for PK).
2300 // First, get signer's certificates from SignedData.
2302 VerifyStatus
= Pkcs7GetSigners (
2310 if (!VerifyStatus
) {
2315 // Second, get the current platform key from variable. Check whether it's identical with signer's certificates
2316 // in SignedData. If not, return error immediately.
2318 Status
= FindVariable (
2319 EFI_PLATFORM_KEY_NAME
,
2320 &gEfiGlobalVariableGuid
,
2322 &mVariableModuleGlobal
->VariableGlobal
,
2325 if (EFI_ERROR (Status
)) {
2326 VerifyStatus
= FALSE
;
2329 CertList
= (EFI_SIGNATURE_LIST
*) GetVariableDataPtr (PkVariable
.CurrPtr
);
2330 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
2331 if ((RootCertSize
!= (CertList
->SignatureSize
- (sizeof (EFI_SIGNATURE_DATA
) - 1))) ||
2332 (CompareMem (Cert
->SignatureData
, RootCert
, RootCertSize
) != 0)) {
2333 VerifyStatus
= FALSE
;
2338 // Verify Pkcs7 SignedData via Pkcs7Verify library.
2340 VerifyStatus
= Pkcs7Verify (
2349 } else if (AuthVarType
== AuthVarTypeKek
) {
2352 // Get KEK database from variable.
2354 Status
= FindVariable (
2355 EFI_KEY_EXCHANGE_KEY_NAME
,
2356 &gEfiGlobalVariableGuid
,
2358 &mVariableModuleGlobal
->VariableGlobal
,
2361 if (EFI_ERROR (Status
)) {
2366 // Ready to verify Pkcs7 SignedData. Go through KEK Signature Database to find out X.509 CertList.
2368 KekDataSize
= KekVariable
.CurrPtr
->DataSize
;
2369 CertList
= (EFI_SIGNATURE_LIST
*) GetVariableDataPtr (KekVariable
.CurrPtr
);
2370 while ((KekDataSize
> 0) && (KekDataSize
>= CertList
->SignatureListSize
)) {
2371 if (CompareGuid (&CertList
->SignatureType
, &gEfiCertX509Guid
)) {
2372 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
2373 CertCount
= (CertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - CertList
->SignatureHeaderSize
) / CertList
->SignatureSize
;
2374 for (Index
= 0; Index
< CertCount
; Index
++) {
2376 // Iterate each Signature Data Node within this CertList for a verify
2378 RootCert
= Cert
->SignatureData
;
2379 RootCertSize
= CertList
->SignatureSize
- (sizeof (EFI_SIGNATURE_DATA
) - 1);
2382 // Verify Pkcs7 SignedData via Pkcs7Verify library.
2384 VerifyStatus
= Pkcs7Verify (
2395 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) Cert
+ CertList
->SignatureSize
);
2398 KekDataSize
-= CertList
->SignatureListSize
;
2399 CertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) CertList
+ CertList
->SignatureListSize
);
2401 } else if (AuthVarType
== AuthVarTypePriv
) {
2404 // Process common authenticated variable except PK/KEK/DB/DBX/DBT.
2405 // Get signer's certificates from SignedData.
2407 VerifyStatus
= Pkcs7GetSigners (
2415 if (!VerifyStatus
) {
2420 // Get previously stored signer's certificates from certdb for existing
2421 // variable. Check whether they are identical with signer's certificates
2422 // in SignedData. If not, return error immediately.
2424 if ((Variable
->CurrPtr
!= NULL
)) {
2425 VerifyStatus
= FALSE
;
2427 Status
= GetCertsFromDb (VariableName
, VendorGuid
, &CertsInCertDb
, &CertsSizeinDb
);
2428 if (EFI_ERROR (Status
)) {
2432 if ((CertStackSize
!= CertsSizeinDb
) ||
2433 (CompareMem (SignerCerts
, CertsInCertDb
, CertsSizeinDb
) != 0)) {
2438 VerifyStatus
= Pkcs7Verify (
2446 if (!VerifyStatus
) {
2451 // Delete signer's certificates when delete the common authenticated variable.
2453 if ((PayloadSize
== 0) && (Variable
->CurrPtr
!= NULL
) && ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0)) {
2454 Status
= DeleteCertsFromDb (VariableName
, VendorGuid
);
2455 if (EFI_ERROR (Status
)) {
2456 VerifyStatus
= FALSE
;
2459 } else if (Variable
->CurrPtr
== NULL
&& PayloadSize
!= 0) {
2461 // Insert signer's certificates when adding a new common authenticated variable.
2463 Status
= InsertCertsToDb (VariableName
, VendorGuid
, SignerCerts
, CertStackSize
);
2464 if (EFI_ERROR (Status
)) {
2465 VerifyStatus
= FALSE
;
2469 } else if (AuthVarType
== AuthVarTypePayload
) {
2470 CertList
= (EFI_SIGNATURE_LIST
*) PayloadPtr
;
2471 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
2472 RootCert
= Cert
->SignatureData
;
2473 RootCertSize
= CertList
->SignatureSize
- (sizeof (EFI_SIGNATURE_DATA
) - 1);
2475 // Verify Pkcs7 SignedData via Pkcs7Verify library.
2477 VerifyStatus
= Pkcs7Verify (
2486 return EFI_SECURITY_VIOLATION
;
2491 if (AuthVarType
== AuthVarTypePk
|| AuthVarType
== AuthVarTypePriv
) {
2492 Pkcs7FreeSigners (RootCert
);
2493 Pkcs7FreeSigners (SignerCerts
);
2496 if (!VerifyStatus
) {
2497 return EFI_SECURITY_VIOLATION
;
2500 Status
= CheckSignatureListFormat(VariableName
, VendorGuid
, PayloadPtr
, PayloadSize
);
2501 if (EFI_ERROR (Status
)) {
2505 if ((PayloadSize
== 0) && (VarDel
!= NULL
)) {
2510 // Final step: Update/Append Variable if it pass Pkcs7Verify
2512 return UpdateVariable (
2521 &CertData
->TimeStamp