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.
11 ProcessVarWithPk(), ProcessVarWithKek() and ProcessVariable() are the function to do
12 variable authentication.
14 VerifyTimeBasedPayload() and VerifyCounterBasedPayload() are sub function to do verification.
15 They will do basic validation for authentication data structure, then call crypto library
16 to verify the signature.
18 Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>
19 This program and the accompanying materials
20 are licensed and made available under the terms and conditions of the BSD License
21 which accompanies this distribution. The full text of the license may be found at
22 http://opensource.org/licenses/bsd-license.php
24 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
25 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
30 #include "AuthService.h"
33 /// Global database array for scratch
35 UINT8 mPubKeyStore
[MAX_KEYDB_SIZE
];
37 UINT8 mCertDbStore
[MAX_CERTDB_SIZE
];
39 UINT8 mVendorKeyState
;
41 EFI_GUID mSignatureSupport
[] = {EFI_CERT_SHA1_GUID
, EFI_CERT_SHA256_GUID
, EFI_CERT_RSA2048_GUID
, EFI_CERT_X509_GUID
};
43 // Public Exponent of RSA Key.
45 CONST UINT8 mRsaE
[] = { 0x01, 0x00, 0x01 };
47 // Hash context pointer
49 VOID
*mHashCtx
= NULL
;
52 // The serialization of the values of the VariableName, VendorGuid and Attributes
53 // parameters of the SetVariable() call and the TimeStamp component of the
54 // EFI_VARIABLE_AUTHENTICATION_2 descriptor followed by the variable's new value
55 // i.e. (VariableName, VendorGuid, Attributes, TimeStamp, Data)
57 UINT8
*mSerializationRuntimeBuffer
= NULL
;
60 // Requirement for different signature type which have been defined in UEFI spec.
61 // These data are used to peform SignatureList format check while setting PK/KEK variable.
63 EFI_SIGNATURE_ITEM mSupportSigItem
[] = {
64 //{SigType, SigHeaderSize, SigDataSize }
65 {EFI_CERT_SHA256_GUID
, 0, 32 },
66 {EFI_CERT_RSA2048_GUID
, 0, 256 },
67 {EFI_CERT_RSA2048_SHA256_GUID
, 0, 256 },
68 {EFI_CERT_SHA1_GUID
, 0, 20 },
69 {EFI_CERT_RSA2048_SHA1_GUID
, 0, 256 },
70 {EFI_CERT_X509_GUID
, 0, ((UINT32
) ~0)},
71 {EFI_CERT_SHA224_GUID
, 0, 28 },
72 {EFI_CERT_SHA384_GUID
, 0, 48 },
73 {EFI_CERT_SHA512_GUID
, 0, 64 }
77 Determine whether this operation needs a physical present user.
79 @param[in] VariableName Name of the Variable.
80 @param[in] VendorGuid GUID of the Variable.
82 @retval TRUE This variable is protected, only a physical present user could set this variable.
83 @retval FALSE This variable is not protected.
87 NeedPhysicallyPresent(
88 IN CHAR16
*VariableName
,
89 IN EFI_GUID
*VendorGuid
92 if ((CompareGuid (VendorGuid
, &gEfiSecureBootEnableDisableGuid
) && (StrCmp (VariableName
, EFI_SECURE_BOOT_ENABLE_NAME
) == 0))
93 || (CompareGuid (VendorGuid
, &gEfiCustomModeEnableGuid
) && (StrCmp (VariableName
, EFI_CUSTOM_MODE_NAME
) == 0))) {
101 Determine whether the platform is operating in Custom Secure Boot mode.
103 @retval TRUE The platform is operating in Custom mode.
104 @retval FALSE The platform is operating in Standard mode.
112 VARIABLE_POINTER_TRACK Variable
;
114 FindVariable (EFI_CUSTOM_MODE_NAME
, &gEfiCustomModeEnableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
115 if (Variable
.CurrPtr
!= NULL
&& *(GetVariableDataPtr (Variable
.CurrPtr
)) == CUSTOM_SECURE_BOOT_MODE
) {
124 Internal function to delete a Variable given its name and GUID, no authentication
127 @param[in] VariableName Name of the Variable.
128 @param[in] VendorGuid GUID of the Variable.
130 @retval EFI_SUCCESS Variable deleted successfully.
131 @retval Others The driver failded to start the device.
136 IN CHAR16
*VariableName
,
137 IN EFI_GUID
*VendorGuid
141 VARIABLE_POINTER_TRACK Variable
;
143 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
144 if (EFI_ERROR (Status
)) {
148 ASSERT (Variable
.CurrPtr
!= NULL
);
149 return UpdateVariable (VariableName
, VendorGuid
, NULL
, 0, 0, 0, 0, &Variable
, NULL
);
153 Initializes for authenticated varibale service.
155 @retval EFI_SUCCESS Function successfully executed.
156 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resources.
160 AutenticatedVariableServiceInitialize (
165 VARIABLE_POINTER_TRACK Variable
;
166 VARIABLE_POINTER_TRACK PkVariable
;
172 UINT8 SecureBootMode
;
173 UINT8 SecureBootEnable
;
178 // Initialize hash context.
180 CtxSize
= Sha256GetContextSize ();
181 mHashCtx
= AllocateRuntimePool (CtxSize
);
182 if (mHashCtx
== NULL
) {
183 return EFI_OUT_OF_RESOURCES
;
187 // Prepare runtime buffer for serialized data of time-based authenticated
188 // Variable, i.e. (VariableName, VendorGuid, Attributes, TimeStamp, Data).
190 mSerializationRuntimeBuffer
= AllocateRuntimePool (PcdGet32 (PcdMaxVariableSize
) + sizeof (EFI_GUID
) + sizeof (UINT32
) + sizeof (EFI_TIME
));
191 if (mSerializationRuntimeBuffer
== NULL
) {
192 return EFI_OUT_OF_RESOURCES
;
196 // Check "AuthVarKeyDatabase" variable's existence.
197 // If it doesn't exist, create a new one with initial value of 0 and EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS set.
199 Status
= FindVariable (
201 &gEfiAuthenticatedVariableGuid
,
203 &mVariableModuleGlobal
->VariableGlobal
,
207 if (Variable
.CurrPtr
== NULL
) {
208 VarAttr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
;
211 Status
= UpdateVariable (
213 &gEfiAuthenticatedVariableGuid
,
222 if (EFI_ERROR (Status
)) {
227 // Load database in global variable for cache.
229 DataSize
= DataSizeOfVariable (Variable
.CurrPtr
);
230 Data
= GetVariableDataPtr (Variable
.CurrPtr
);
231 ASSERT ((DataSize
!= 0) && (Data
!= NULL
));
232 CopyMem (mPubKeyStore
, (UINT8
*) Data
, DataSize
);
233 mPubKeyNumber
= (UINT32
) (DataSize
/ EFI_CERT_TYPE_RSA2048_SIZE
);
236 FindVariable (EFI_PLATFORM_KEY_NAME
, &gEfiGlobalVariableGuid
, &PkVariable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
237 if (PkVariable
.CurrPtr
== NULL
) {
238 DEBUG ((EFI_D_INFO
, "Variable %s does not exist.\n", EFI_PLATFORM_KEY_NAME
));
240 DEBUG ((EFI_D_INFO
, "Variable %s exists.\n", EFI_PLATFORM_KEY_NAME
));
244 // Create "SetupMode" variable with BS+RT attribute set.
246 FindVariable (EFI_SETUP_MODE_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
247 if (PkVariable
.CurrPtr
== NULL
) {
248 mPlatformMode
= SETUP_MODE
;
250 mPlatformMode
= USER_MODE
;
252 Status
= UpdateVariable (
254 &gEfiGlobalVariableGuid
,
257 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
263 if (EFI_ERROR (Status
)) {
268 // Create "SignatureSupport" variable with BS+RT attribute set.
270 FindVariable (EFI_SIGNATURE_SUPPORT_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
271 Status
= UpdateVariable (
272 EFI_SIGNATURE_SUPPORT_NAME
,
273 &gEfiGlobalVariableGuid
,
275 sizeof(mSignatureSupport
),
276 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
282 if (EFI_ERROR (Status
)) {
287 // If "SecureBootEnable" variable exists, then update "SecureBoot" variable.
288 // If "SecureBootEnable" variable is SECURE_BOOT_ENABLE and in USER_MODE, Set "SecureBoot" variable to SECURE_BOOT_MODE_ENABLE.
289 // If "SecureBootEnable" variable is SECURE_BOOT_DISABLE, Set "SecureBoot" variable to SECURE_BOOT_MODE_DISABLE.
291 SecureBootEnable
= SECURE_BOOT_DISABLE
;
292 FindVariable (EFI_SECURE_BOOT_ENABLE_NAME
, &gEfiSecureBootEnableDisableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
293 if (Variable
.CurrPtr
!= NULL
) {
294 SecureBootEnable
= *(GetVariableDataPtr (Variable
.CurrPtr
));
295 } else if (mPlatformMode
== USER_MODE
) {
297 // "SecureBootEnable" not exist, initialize it in USER_MODE.
299 SecureBootEnable
= SECURE_BOOT_ENABLE
;
300 Status
= UpdateVariable (
301 EFI_SECURE_BOOT_ENABLE_NAME
,
302 &gEfiSecureBootEnableDisableGuid
,
305 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
311 if (EFI_ERROR (Status
)) {
317 // Create "SecureBoot" variable with BS+RT attribute set.
319 if (SecureBootEnable
== SECURE_BOOT_ENABLE
&& mPlatformMode
== USER_MODE
) {
320 SecureBootMode
= SECURE_BOOT_MODE_ENABLE
;
322 SecureBootMode
= SECURE_BOOT_MODE_DISABLE
;
324 FindVariable (EFI_SECURE_BOOT_MODE_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
325 Status
= UpdateVariable (
326 EFI_SECURE_BOOT_MODE_NAME
,
327 &gEfiGlobalVariableGuid
,
330 EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
336 if (EFI_ERROR (Status
)) {
340 DEBUG ((EFI_D_INFO
, "Variable %s is %x\n", EFI_SETUP_MODE_NAME
, mPlatformMode
));
341 DEBUG ((EFI_D_INFO
, "Variable %s is %x\n", EFI_SECURE_BOOT_MODE_NAME
, SecureBootMode
));
342 DEBUG ((EFI_D_INFO
, "Variable %s is %x\n", EFI_SECURE_BOOT_ENABLE_NAME
, SecureBootEnable
));
345 // Initialize "CustomMode" in STANDARD_SECURE_BOOT_MODE state.
347 FindVariable (EFI_CUSTOM_MODE_NAME
, &gEfiCustomModeEnableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
348 CustomMode
= STANDARD_SECURE_BOOT_MODE
;
349 Status
= UpdateVariable (
350 EFI_CUSTOM_MODE_NAME
,
351 &gEfiCustomModeEnableGuid
,
354 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
360 if (EFI_ERROR (Status
)) {
364 DEBUG ((EFI_D_INFO
, "Variable %s is %x\n", EFI_CUSTOM_MODE_NAME
, CustomMode
));
367 // Check "certdb" variable's existence.
368 // If it doesn't exist, then create a new one with
369 // EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set.
371 Status
= FindVariable (
375 &mVariableModuleGlobal
->VariableGlobal
,
379 if (Variable
.CurrPtr
== NULL
) {
380 VarAttr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
381 ListSize
= sizeof (UINT32
);
382 Status
= UpdateVariable (
393 if (EFI_ERROR (Status
)) {
399 // Check "VendorKeysNv" variable's existence and create "VendorKeys" variable accordingly.
401 FindVariable (EFI_VENDOR_KEYS_NV_VARIABLE_NAME
, &gEfiVendorKeysNvGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
402 if (Variable
.CurrPtr
!= NULL
) {
403 mVendorKeyState
= *(GetVariableDataPtr (Variable
.CurrPtr
));
406 // "VendorKeysNv" not exist, initialize it in VENDOR_KEYS_VALID state.
408 mVendorKeyState
= VENDOR_KEYS_VALID
;
409 Status
= UpdateVariable (
410 EFI_VENDOR_KEYS_NV_VARIABLE_NAME
,
411 &gEfiVendorKeysNvGuid
,
414 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
,
420 if (EFI_ERROR (Status
)) {
426 // Create "VendorKeys" variable with BS+RT attribute set.
428 FindVariable (EFI_VENDOR_KEYS_VARIABLE_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
429 Status
= UpdateVariable (
430 EFI_VENDOR_KEYS_VARIABLE_NAME
,
431 &gEfiGlobalVariableGuid
,
434 EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
440 if (EFI_ERROR (Status
)) {
444 DEBUG ((EFI_D_INFO
, "Variable %s is %x\n", EFI_VENDOR_KEYS_VARIABLE_NAME
, mVendorKeyState
));
450 Add public key in store and return its index.
452 @param[in] PubKey Input pointer to Public Key data
454 @return Index of new added item
465 VARIABLE_POINTER_TRACK Variable
;
470 if (PubKey
== NULL
) {
474 Status
= FindVariable (
476 &gEfiAuthenticatedVariableGuid
,
478 &mVariableModuleGlobal
->VariableGlobal
,
481 if (EFI_ERROR (Status
)) {
482 DEBUG ((EFI_D_ERROR
, "Get public key database variable failure, Status = %r\n", Status
));
487 // Check whether the public key entry does exist.
490 for (Ptr
= mPubKeyStore
, Index
= 1; Index
<= mPubKeyNumber
; Index
++) {
491 if (CompareMem (Ptr
, PubKey
, EFI_CERT_TYPE_RSA2048_SIZE
) == 0) {
495 Ptr
+= EFI_CERT_TYPE_RSA2048_SIZE
;
500 // Add public key in database.
502 if (mPubKeyNumber
== MAX_KEY_NUM
) {
504 // Public key dadatase is full, try to reclaim invalid key.
508 // NV storage can't reclaim at runtime.
514 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
515 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
522 if (EFI_ERROR (Status
)) {
526 Status
= FindVariable (
528 &gEfiAuthenticatedVariableGuid
,
530 &mVariableModuleGlobal
->VariableGlobal
,
533 if (EFI_ERROR (Status
)) {
534 DEBUG ((EFI_D_ERROR
, "Get public key database variable failure, Status = %r\n", Status
));
538 DataSize
= DataSizeOfVariable (Variable
.CurrPtr
);
539 Data
= GetVariableDataPtr (Variable
.CurrPtr
);
540 ASSERT ((DataSize
!= 0) && (Data
!= NULL
));
541 CopyMem (mPubKeyStore
, (UINT8
*) Data
, DataSize
);
542 mPubKeyNumber
= (UINT32
) (DataSize
/ EFI_CERT_TYPE_RSA2048_SIZE
);
544 if (mPubKeyNumber
== MAX_KEY_NUM
) {
549 CopyMem (mPubKeyStore
+ mPubKeyNumber
* EFI_CERT_TYPE_RSA2048_SIZE
, PubKey
, EFI_CERT_TYPE_RSA2048_SIZE
);
550 Index
= ++mPubKeyNumber
;
552 // Update public key database variable.
554 Status
= UpdateVariable (
556 &gEfiAuthenticatedVariableGuid
,
558 mPubKeyNumber
* EFI_CERT_TYPE_RSA2048_SIZE
,
559 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
,
565 if (EFI_ERROR (Status
)) {
566 DEBUG ((EFI_D_ERROR
, "Update public key database variable failure, Status = %r\n", Status
));
575 Verify data payload with AuthInfo in EFI_CERT_TYPE_RSA2048_SHA256_GUID type.
576 Follow the steps in UEFI2.2.
578 Caution: This function may receive untrusted input.
579 This function may be invoked in SMM mode, and datasize and data are external input.
580 This function will do basic validation, before parse the data.
581 This function will parse the authentication carefully to avoid security issues, like
582 buffer overflow, integer overflow.
584 @param[in] Data Pointer to data with AuthInfo.
585 @param[in] DataSize Size of Data.
586 @param[in] PubKey Public key used for verification.
588 @retval EFI_INVALID_PARAMETER Invalid parameter.
589 @retval EFI_SECURITY_VIOLATION If authentication failed.
590 @retval EFI_SUCCESS Authentication successful.
594 VerifyCounterBasedPayload (
601 EFI_VARIABLE_AUTHENTICATION
*CertData
;
602 EFI_CERT_BLOCK_RSA_2048_SHA256
*CertBlock
;
603 UINT8 Digest
[SHA256_DIGEST_SIZE
];
607 PayloadSize
= DataSize
- AUTHINFO_SIZE
;
612 if (Data
== NULL
|| PubKey
== NULL
) {
613 return EFI_INVALID_PARAMETER
;
616 CertData
= (EFI_VARIABLE_AUTHENTICATION
*) Data
;
617 CertBlock
= (EFI_CERT_BLOCK_RSA_2048_SHA256
*) (CertData
->AuthInfo
.CertData
);
620 // wCertificateType should be WIN_CERT_TYPE_EFI_GUID.
621 // Cert type should be EFI_CERT_TYPE_RSA2048_SHA256_GUID.
623 if ((CertData
->AuthInfo
.Hdr
.wCertificateType
!= WIN_CERT_TYPE_EFI_GUID
) ||
624 !CompareGuid (&CertData
->AuthInfo
.CertType
, &gEfiCertTypeRsa2048Sha256Guid
)
627 // Invalid AuthInfo type, return EFI_SECURITY_VIOLATION.
629 return EFI_SECURITY_VIOLATION
;
632 // Hash data payload with SHA256.
634 ZeroMem (Digest
, SHA256_DIGEST_SIZE
);
635 Status
= Sha256Init (mHashCtx
);
639 Status
= Sha256Update (mHashCtx
, Data
+ AUTHINFO_SIZE
, PayloadSize
);
646 Status
= Sha256Update (mHashCtx
, &PayloadSize
, sizeof (UINTN
));
651 // Hash Monotonic Count.
653 Status
= Sha256Update (mHashCtx
, &CertData
->MonotonicCount
, sizeof (UINT64
));
657 Status
= Sha256Final (mHashCtx
, Digest
);
662 // Generate & Initialize RSA Context.
665 ASSERT (Rsa
!= NULL
);
667 // Set RSA Key Components.
668 // NOTE: Only N and E are needed to be set as RSA public key for signature verification.
670 Status
= RsaSetKey (Rsa
, RsaKeyN
, PubKey
, EFI_CERT_TYPE_RSA2048_SIZE
);
674 Status
= RsaSetKey (Rsa
, RsaKeyE
, mRsaE
, sizeof (mRsaE
));
679 // Verify the signature.
681 Status
= RsaPkcs1Verify (
685 CertBlock
->Signature
,
686 EFI_CERT_TYPE_RSA2048_SHA256_SIZE
696 return EFI_SECURITY_VIOLATION
;
701 Update platform mode.
703 @param[in] Mode SETUP_MODE or USER_MODE.
705 @return EFI_INVALID_PARAMETER Invalid parameter.
706 @return EFI_SUCCESS Update platform mode successfully.
715 VARIABLE_POINTER_TRACK Variable
;
716 UINT8 SecureBootMode
;
717 UINT8 SecureBootEnable
;
718 UINTN VariableDataSize
;
720 Status
= FindVariable (
722 &gEfiGlobalVariableGuid
,
724 &mVariableModuleGlobal
->VariableGlobal
,
727 if (EFI_ERROR (Status
)) {
732 // Update the value of SetupMode variable by a simple mem copy, this could avoid possible
733 // variable storage reclaim at runtime.
735 mPlatformMode
= (UINT8
) Mode
;
736 CopyMem (GetVariableDataPtr (Variable
.CurrPtr
), &mPlatformMode
, sizeof(UINT8
));
740 // SecureBoot Variable indicates whether the platform firmware is operating
741 // in Secure boot mode (1) or not (0), so we should not change SecureBoot
742 // Variable in runtime.
748 // Check "SecureBoot" variable's existence.
749 // If it doesn't exist, firmware has no capability to perform driver signing verification,
750 // then set "SecureBoot" to 0.
752 Status
= FindVariable (
753 EFI_SECURE_BOOT_MODE_NAME
,
754 &gEfiGlobalVariableGuid
,
756 &mVariableModuleGlobal
->VariableGlobal
,
760 // If "SecureBoot" variable exists, then check "SetupMode" variable update.
761 // If "SetupMode" variable is USER_MODE, "SecureBoot" variable is set to 1.
762 // If "SetupMode" variable is SETUP_MODE, "SecureBoot" variable is set to 0.
764 if (Variable
.CurrPtr
== NULL
) {
765 SecureBootMode
= SECURE_BOOT_MODE_DISABLE
;
767 if (mPlatformMode
== USER_MODE
) {
768 SecureBootMode
= SECURE_BOOT_MODE_ENABLE
;
769 } else if (mPlatformMode
== SETUP_MODE
) {
770 SecureBootMode
= SECURE_BOOT_MODE_DISABLE
;
772 return EFI_NOT_FOUND
;
776 Status
= UpdateVariable (
777 EFI_SECURE_BOOT_MODE_NAME
,
778 &gEfiGlobalVariableGuid
,
781 EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
787 if (EFI_ERROR (Status
)) {
792 // Check "SecureBootEnable" variable's existence. It can enable/disable secure boot feature.
794 Status
= FindVariable (
795 EFI_SECURE_BOOT_ENABLE_NAME
,
796 &gEfiSecureBootEnableDisableGuid
,
798 &mVariableModuleGlobal
->VariableGlobal
,
802 if (SecureBootMode
== SECURE_BOOT_MODE_ENABLE
) {
804 // Create the "SecureBootEnable" variable as secure boot is enabled.
806 SecureBootEnable
= SECURE_BOOT_ENABLE
;
807 VariableDataSize
= sizeof (SecureBootEnable
);
810 // Delete the "SecureBootEnable" variable if this variable exist as "SecureBoot"
811 // variable is not in secure boot state.
813 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
816 SecureBootEnable
= SECURE_BOOT_DISABLE
;
817 VariableDataSize
= 0;
820 Status
= UpdateVariable (
821 EFI_SECURE_BOOT_ENABLE_NAME
,
822 &gEfiSecureBootEnableDisableGuid
,
825 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
835 Check input data form to make sure it is a valid EFI_SIGNATURE_LIST for PK/KEK/db/dbx variable.
837 @param[in] VariableName Name of Variable to be check.
838 @param[in] VendorGuid Variable vendor GUID.
839 @param[in] Data Point to the variable data to be checked.
840 @param[in] DataSize Size of Data.
842 @return EFI_INVALID_PARAMETER Invalid signature list format.
843 @return EFI_SUCCESS Passed signature list format check successfully.
847 CheckSignatureListFormat(
848 IN CHAR16
*VariableName
,
849 IN EFI_GUID
*VendorGuid
,
854 EFI_SIGNATURE_LIST
*SigList
;
860 EFI_SIGNATURE_DATA
*CertData
;
867 ASSERT (VariableName
!= NULL
&& VendorGuid
!= NULL
&& Data
!= NULL
);
869 if (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, EFI_PLATFORM_KEY_NAME
) == 0)){
871 } else if ((CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && StrCmp (VariableName
, EFI_KEY_EXCHANGE_KEY_NAME
) == 0) ||
872 (CompareGuid (VendorGuid
, &gEfiImageSecurityDatabaseGuid
) &&
873 (StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE
) == 0 || StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE1
) == 0))){
880 SigList
= (EFI_SIGNATURE_LIST
*) Data
;
881 SigDataSize
= DataSize
;
885 // Walk throuth the input signature list and check the data format.
886 // If any signature is incorrectly formed, the whole check will fail.
888 while ((SigDataSize
> 0) && (SigDataSize
>= SigList
->SignatureListSize
)) {
889 for (Index
= 0; Index
< (sizeof (mSupportSigItem
) / sizeof (EFI_SIGNATURE_ITEM
)); Index
++ ) {
890 if (CompareGuid (&SigList
->SignatureType
, &mSupportSigItem
[Index
].SigType
)) {
892 // The value of SignatureSize should always be 16 (size of SignatureOwner
893 // component) add the data length according to signature type.
895 if (mSupportSigItem
[Index
].SigDataSize
!= ((UINT32
) ~0) &&
896 (SigList
->SignatureSize
- sizeof (EFI_GUID
)) != mSupportSigItem
[Index
].SigDataSize
) {
897 return EFI_INVALID_PARAMETER
;
899 if (mSupportSigItem
[Index
].SigHeaderSize
!= ((UINTN
) ~0) &&
900 SigList
->SignatureHeaderSize
!= mSupportSigItem
[Index
].SigHeaderSize
) {
901 return EFI_INVALID_PARAMETER
;
907 if (Index
== (sizeof (mSupportSigItem
) / sizeof (EFI_SIGNATURE_ITEM
))) {
909 // Undefined signature type.
911 return EFI_INVALID_PARAMETER
;
914 if (CompareGuid (&SigList
->SignatureType
, &gEfiCertX509Guid
)) {
916 // Try to retrieve the RSA public key from the X.509 certificate.
917 // If this operation fails, it's not a valid certificate.
919 RsaContext
= RsaNew ();
920 if (RsaContext
== NULL
) {
921 return EFI_INVALID_PARAMETER
;
923 CertData
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) SigList
+ sizeof (EFI_SIGNATURE_LIST
) + SigList
->SignatureHeaderSize
);
924 CertLen
= SigList
->SignatureSize
- sizeof (EFI_GUID
);
925 if (!RsaGetPublicKeyFromX509 (CertData
->SignatureData
, CertLen
, &RsaContext
)) {
926 RsaFree (RsaContext
);
927 return EFI_INVALID_PARAMETER
;
929 RsaFree (RsaContext
);
932 if ((SigList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - SigList
->SignatureHeaderSize
) % SigList
->SignatureSize
!= 0) {
933 return EFI_INVALID_PARAMETER
;
935 SigCount
+= (SigList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - SigList
->SignatureHeaderSize
) / SigList
->SignatureSize
;
937 SigDataSize
-= SigList
->SignatureListSize
;
938 SigList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) SigList
+ SigList
->SignatureListSize
);
941 if (((UINTN
) SigList
- (UINTN
) Data
) != DataSize
) {
942 return EFI_INVALID_PARAMETER
;
945 if (IsPk
&& SigCount
> 1) {
946 return EFI_INVALID_PARAMETER
;
953 Update "VendorKeys" variable to record the out of band secure boot key modification.
955 @return EFI_SUCCESS Variable is updated successfully.
956 @return Others Failed to update variable.
960 VendorKeyIsModified (
965 VARIABLE_POINTER_TRACK Variable
;
967 if (mVendorKeyState
== VENDOR_KEYS_MODIFIED
) {
970 mVendorKeyState
= VENDOR_KEYS_MODIFIED
;
972 FindVariable (EFI_VENDOR_KEYS_NV_VARIABLE_NAME
, &gEfiVendorKeysNvGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
973 Status
= UpdateVariable (
974 EFI_VENDOR_KEYS_NV_VARIABLE_NAME
,
975 &gEfiVendorKeysNvGuid
,
978 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
,
984 if (EFI_ERROR (Status
)) {
988 FindVariable (EFI_VENDOR_KEYS_VARIABLE_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
989 return UpdateVariable (
990 EFI_VENDOR_KEYS_VARIABLE_NAME
,
991 &gEfiGlobalVariableGuid
,
994 EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
1003 Process variable with platform key for verification.
1005 Caution: This function may receive untrusted input.
1006 This function may be invoked in SMM mode, and datasize and data are external input.
1007 This function will do basic validation, before parse the data.
1008 This function will parse the authentication carefully to avoid security issues, like
1009 buffer overflow, integer overflow.
1010 This function will check attribute carefully to avoid authentication bypass.
1012 @param[in] VariableName Name of Variable to be found.
1013 @param[in] VendorGuid Variable vendor GUID.
1014 @param[in] Data Data pointer.
1015 @param[in] DataSize Size of Data found. If size is less than the
1016 data, this value contains the required size.
1017 @param[in] Variable The variable information which is used to keep track of variable usage.
1018 @param[in] Attributes Attribute value of the variable
1019 @param[in] IsPk Indicate whether it is to process pk.
1021 @return EFI_INVALID_PARAMETER Invalid parameter.
1022 @return EFI_SECURITY_VIOLATION The variable does NOT pass the validation.
1023 check carried out by the firmware.
1024 @return EFI_SUCCESS Variable passed validation successfully.
1029 IN CHAR16
*VariableName
,
1030 IN EFI_GUID
*VendorGuid
,
1033 IN VARIABLE_POINTER_TRACK
*Variable
,
1034 IN UINT32 Attributes OPTIONAL
,
1043 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0 ||
1044 (Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) == 0) {
1046 // PK, KEK and db/dbx should set EFI_VARIABLE_NON_VOLATILE attribute and should be a time-based
1047 // authenticated variable.
1049 return EFI_INVALID_PARAMETER
;
1053 if ((InCustomMode() && UserPhysicalPresent()) || (mPlatformMode
== SETUP_MODE
&& !IsPk
)) {
1054 Payload
= (UINT8
*) Data
+ AUTHINFO2_SIZE (Data
);
1055 PayloadSize
= DataSize
- AUTHINFO2_SIZE (Data
);
1056 if (PayloadSize
== 0) {
1060 Status
= CheckSignatureListFormat(VariableName
, VendorGuid
, Payload
, PayloadSize
);
1061 if (EFI_ERROR (Status
)) {
1065 Status
= UpdateVariable (
1074 &((EFI_VARIABLE_AUTHENTICATION_2
*) Data
)->TimeStamp
1076 if (EFI_ERROR(Status
)) {
1080 if ((mPlatformMode
!= SETUP_MODE
) || IsPk
) {
1081 Status
= VendorKeyIsModified ();
1083 } else if (mPlatformMode
== USER_MODE
) {
1085 // Verify against X509 Cert in PK database.
1087 Status
= VerifyTimeBasedPayload (
1099 // Verify against the certificate in data payload.
1101 Status
= VerifyTimeBasedPayload (
1113 if (!EFI_ERROR(Status
) && IsPk
) {
1114 if (mPlatformMode
== SETUP_MODE
&& !Del
) {
1116 // If enroll PK in setup mode, need change to user mode.
1118 Status
= UpdatePlatformMode (USER_MODE
);
1119 } else if (mPlatformMode
== USER_MODE
&& Del
){
1121 // If delete PK in user mode, need change to setup mode.
1123 Status
= UpdatePlatformMode (SETUP_MODE
);
1131 Process variable with key exchange key for verification.
1133 Caution: This function may receive untrusted input.
1134 This function may be invoked in SMM mode, and datasize and data are external input.
1135 This function will do basic validation, before parse the data.
1136 This function will parse the authentication carefully to avoid security issues, like
1137 buffer overflow, integer overflow.
1138 This function will check attribute carefully to avoid authentication bypass.
1140 @param[in] VariableName Name of Variable to be found.
1141 @param[in] VendorGuid Variable vendor GUID.
1142 @param[in] Data Data pointer.
1143 @param[in] DataSize Size of Data found. If size is less than the
1144 data, this value contains the required size.
1145 @param[in] Variable The variable information which is used to keep track of variable usage.
1146 @param[in] Attributes Attribute value of the variable.
1148 @return EFI_INVALID_PARAMETER Invalid parameter.
1149 @return EFI_SECURITY_VIOLATION The variable does NOT pass the validation
1150 check carried out by the firmware.
1151 @return EFI_SUCCESS Variable pass validation successfully.
1156 IN CHAR16
*VariableName
,
1157 IN EFI_GUID
*VendorGuid
,
1160 IN VARIABLE_POINTER_TRACK
*Variable
,
1161 IN UINT32 Attributes OPTIONAL
1168 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0 ||
1169 (Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) == 0) {
1171 // DB and DBX should set EFI_VARIABLE_NON_VOLATILE attribute and should be a time-based
1172 // authenticated variable.
1174 return EFI_INVALID_PARAMETER
;
1177 Status
= EFI_SUCCESS
;
1178 if (mPlatformMode
== USER_MODE
&& !(InCustomMode() && UserPhysicalPresent())) {
1180 // Time-based, verify against X509 Cert KEK.
1182 return VerifyTimeBasedPayload (
1194 // If in setup mode or custom secure boot mode, no authentication needed.
1196 Payload
= (UINT8
*) Data
+ AUTHINFO2_SIZE (Data
);
1197 PayloadSize
= DataSize
- AUTHINFO2_SIZE (Data
);
1199 Status
= CheckSignatureListFormat(VariableName
, VendorGuid
, Payload
, PayloadSize
);
1200 if (EFI_ERROR (Status
)) {
1204 Status
= UpdateVariable (
1213 &((EFI_VARIABLE_AUTHENTICATION_2
*) Data
)->TimeStamp
1215 if (EFI_ERROR (Status
)) {
1219 if (mPlatformMode
!= SETUP_MODE
) {
1220 Status
= VendorKeyIsModified ();
1228 Process variable with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS/EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set
1230 Caution: This function may receive untrusted input.
1231 This function may be invoked in SMM mode, and datasize and data are external input.
1232 This function will do basic validation, before parse the data.
1233 This function will parse the authentication carefully to avoid security issues, like
1234 buffer overflow, integer overflow.
1235 This function will check attribute carefully to avoid authentication bypass.
1237 @param[in] VariableName Name of Variable to be found.
1238 @param[in] VendorGuid Variable vendor GUID.
1240 @param[in] Data Data pointer.
1241 @param[in] DataSize Size of Data found. If size is less than the
1242 data, this value contains the required size.
1243 @param[in] Variable The variable information which is used to keep track of variable usage.
1244 @param[in] Attributes Attribute value of the variable.
1246 @return EFI_INVALID_PARAMETER Invalid parameter.
1247 @return EFI_WRITE_PROTECTED Variable is write-protected and needs authentication with
1248 EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS set.
1249 @return EFI_OUT_OF_RESOURCES The Database to save the public key is full.
1250 @return EFI_SECURITY_VIOLATION The variable is with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
1251 set, but the AuthInfo does NOT pass the validation
1252 check carried out by the firmware.
1253 @return EFI_SUCCESS Variable is not write-protected or pass validation successfully.
1258 IN CHAR16
*VariableName
,
1259 IN EFI_GUID
*VendorGuid
,
1262 IN VARIABLE_POINTER_TRACK
*Variable
,
1263 IN UINT32 Attributes
1268 BOOLEAN IsFirstTime
;
1270 EFI_VARIABLE_AUTHENTICATION
*CertData
;
1271 EFI_CERT_BLOCK_RSA_2048_SHA256
*CertBlock
;
1273 UINT64 MonotonicCount
;
1281 if (NeedPhysicallyPresent(VariableName
, VendorGuid
) && !UserPhysicalPresent()) {
1283 // This variable is protected, only physical present user could modify its value.
1285 return EFI_SECURITY_VIOLATION
;
1289 // A time-based authenticated variable and a count-based authenticated variable
1290 // can't be updated by each other.
1292 if (Variable
->CurrPtr
!= NULL
) {
1293 if (((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) &&
1294 ((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0)) {
1295 return EFI_SECURITY_VIOLATION
;
1298 if (((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) &&
1299 ((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0)) {
1300 return EFI_SECURITY_VIOLATION
;
1305 // Process Time-based Authenticated variable.
1307 if ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) {
1308 return VerifyTimeBasedPayload (
1321 // Determine if first time SetVariable with the EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS.
1323 if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) {
1325 // Determine current operation type.
1327 if (DataSize
== AUTHINFO_SIZE
) {
1331 // Determine whether this is the first time with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS set.
1333 if (Variable
->CurrPtr
== NULL
) {
1335 } else if ((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) == 0) {
1338 KeyIndex
= Variable
->CurrPtr
->PubKeyIndex
;
1339 IsFirstTime
= FALSE
;
1341 } else if ((Variable
->CurrPtr
!= NULL
) &&
1342 ((Variable
->CurrPtr
->Attributes
& (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
)) != 0)
1345 // If the variable is already write-protected, it always needs authentication before update.
1347 return EFI_WRITE_PROTECTED
;
1350 // If without EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS, set and attributes collision.
1351 // That means it is not authenticated variable, just update variable as usual.
1353 Status
= UpdateVariable (VariableName
, VendorGuid
, Data
, DataSize
, Attributes
, 0, 0, Variable
, NULL
);
1358 // Get PubKey and check Monotonic Count value corresponding to the variable.
1360 CertData
= (EFI_VARIABLE_AUTHENTICATION
*) Data
;
1361 CertBlock
= (EFI_CERT_BLOCK_RSA_2048_SHA256
*) (CertData
->AuthInfo
.CertData
);
1362 PubKey
= CertBlock
->PublicKey
;
1365 // Update Monotonic Count value.
1367 MonotonicCount
= CertData
->MonotonicCount
;
1371 // Check input PubKey.
1373 if (CompareMem (PubKey
, mPubKeyStore
+ (KeyIndex
- 1) * EFI_CERT_TYPE_RSA2048_SIZE
, EFI_CERT_TYPE_RSA2048_SIZE
) != 0) {
1374 return EFI_SECURITY_VIOLATION
;
1377 // Compare the current monotonic count and ensure that it is greater than the last SetVariable
1378 // operation with the EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS attribute set.
1380 if (CertData
->MonotonicCount
<= Variable
->CurrPtr
->MonotonicCount
) {
1382 // Monotonic count check fail, suspicious replay attack, return EFI_SECURITY_VIOLATION.
1384 return EFI_SECURITY_VIOLATION
;
1388 // Verify the certificate in Data payload.
1390 Status
= VerifyCounterBasedPayload (Data
, DataSize
, PubKey
);
1391 if (EFI_ERROR (Status
)) {
1396 // Now, the signature has been verified!
1398 if (IsFirstTime
&& !IsDeletion
) {
1400 // Update public key database variable if need.
1402 KeyIndex
= AddPubKeyInStore (PubKey
);
1403 if (KeyIndex
== 0) {
1404 return EFI_OUT_OF_RESOURCES
;
1409 // Verification pass.
1411 return UpdateVariable (VariableName
, VendorGuid
, (UINT8
*)Data
+ AUTHINFO_SIZE
, DataSize
- AUTHINFO_SIZE
, Attributes
, KeyIndex
, MonotonicCount
, Variable
, NULL
);
1415 Merge two buffers which formatted as EFI_SIGNATURE_LIST. Only the new EFI_SIGNATURE_DATA
1416 will be appended to the original EFI_SIGNATURE_LIST, duplicate EFI_SIGNATURE_DATA
1419 @param[in, out] Data Pointer to original EFI_SIGNATURE_LIST.
1420 @param[in] DataSize Size of Data buffer.
1421 @param[in] FreeBufSize Size of free data buffer
1422 @param[in] NewData Pointer to new EFI_SIGNATURE_LIST to be appended.
1423 @param[in] NewDataSize Size of NewData buffer.
1424 @param[out] MergedBufSize Size of the merged buffer
1426 @return EFI_BUFFER_TOO_SMALL if input Data buffer overflowed
1430 AppendSignatureList (
1433 IN UINTN FreeBufSize
,
1435 IN UINTN NewDataSize
,
1436 OUT UINTN
*MergedBufSize
1439 EFI_SIGNATURE_LIST
*CertList
;
1440 EFI_SIGNATURE_DATA
*Cert
;
1442 EFI_SIGNATURE_LIST
*NewCertList
;
1443 EFI_SIGNATURE_DATA
*NewCert
;
1450 UINTN SignatureListSize
;
1453 Tail
= (UINT8
*) Data
+ DataSize
;
1455 NewCertList
= (EFI_SIGNATURE_LIST
*) NewData
;
1456 while ((NewDataSize
> 0) && (NewDataSize
>= NewCertList
->SignatureListSize
)) {
1457 NewCert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) NewCertList
+ sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
);
1458 NewCertCount
= (NewCertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - NewCertList
->SignatureHeaderSize
) / NewCertList
->SignatureSize
;
1461 for (Index
= 0; Index
< NewCertCount
; Index
++) {
1465 CertList
= (EFI_SIGNATURE_LIST
*) Data
;
1466 while ((Size
> 0) && (Size
>= CertList
->SignatureListSize
)) {
1467 if (CompareGuid (&CertList
->SignatureType
, &NewCertList
->SignatureType
) &&
1468 (CertList
->SignatureSize
== NewCertList
->SignatureSize
)) {
1469 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
1470 CertCount
= (CertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - CertList
->SignatureHeaderSize
) / CertList
->SignatureSize
;
1471 for (Index2
= 0; Index2
< CertCount
; Index2
++) {
1473 // Iterate each Signature Data in this Signature List.
1475 if (CompareMem (NewCert
, Cert
, CertList
->SignatureSize
) == 0) {
1479 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) Cert
+ CertList
->SignatureSize
);
1486 Size
-= CertList
->SignatureListSize
;
1487 CertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) CertList
+ CertList
->SignatureListSize
);
1492 // New EFI_SIGNATURE_DATA, append it.
1494 if (CopiedCount
== 0) {
1495 if (FreeBufSize
< sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
) {
1496 return EFI_BUFFER_TOO_SMALL
;
1500 // Copy EFI_SIGNATURE_LIST header for only once.
1503 CopyMem (Tail
, NewCertList
, sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
);
1504 Tail
= Tail
+ sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
;
1505 FreeBufSize
-= sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
;
1508 if (FreeBufSize
< NewCertList
->SignatureSize
) {
1509 return EFI_BUFFER_TOO_SMALL
;
1511 CopyMem (Tail
, NewCert
, NewCertList
->SignatureSize
);
1512 Tail
+= NewCertList
->SignatureSize
;
1513 FreeBufSize
-= NewCertList
->SignatureSize
;
1517 NewCert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) NewCert
+ NewCertList
->SignatureSize
);
1521 // Update SignatureListSize in newly appended EFI_SIGNATURE_LIST.
1523 if (CopiedCount
!= 0) {
1524 SignatureListSize
= sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
+ (CopiedCount
* NewCertList
->SignatureSize
);
1525 CertList
= (EFI_SIGNATURE_LIST
*) (Tail
- SignatureListSize
);
1526 CertList
->SignatureListSize
= (UINT32
) SignatureListSize
;
1529 NewDataSize
-= NewCertList
->SignatureListSize
;
1530 NewCertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) NewCertList
+ NewCertList
->SignatureListSize
);
1533 *MergedBufSize
= (Tail
- (UINT8
*) Data
);
1538 Compare two EFI_TIME data.
1541 @param FirstTime A pointer to the first EFI_TIME data.
1542 @param SecondTime A pointer to the second EFI_TIME data.
1544 @retval TRUE The FirstTime is not later than the SecondTime.
1545 @retval FALSE The FirstTime is later than the SecondTime.
1550 IN EFI_TIME
*FirstTime
,
1551 IN EFI_TIME
*SecondTime
1554 if (FirstTime
->Year
!= SecondTime
->Year
) {
1555 return (BOOLEAN
) (FirstTime
->Year
< SecondTime
->Year
);
1556 } else if (FirstTime
->Month
!= SecondTime
->Month
) {
1557 return (BOOLEAN
) (FirstTime
->Month
< SecondTime
->Month
);
1558 } else if (FirstTime
->Day
!= SecondTime
->Day
) {
1559 return (BOOLEAN
) (FirstTime
->Day
< SecondTime
->Day
);
1560 } else if (FirstTime
->Hour
!= SecondTime
->Hour
) {
1561 return (BOOLEAN
) (FirstTime
->Hour
< SecondTime
->Hour
);
1562 } else if (FirstTime
->Minute
!= SecondTime
->Minute
) {
1563 return (BOOLEAN
) (FirstTime
->Minute
< SecondTime
->Minute
);
1566 return (BOOLEAN
) (FirstTime
->Second
<= SecondTime
->Second
);
1570 Find matching signer's certificates for common authenticated variable
1571 by corresponding VariableName and VendorGuid from "certdb".
1573 The data format of "certdb":
1575 // UINT32 CertDbListSize;
1576 // /// AUTH_CERT_DB_DATA Certs1[];
1577 // /// AUTH_CERT_DB_DATA Certs2[];
1579 // /// AUTH_CERT_DB_DATA Certsn[];
1582 @param[in] VariableName Name of authenticated Variable.
1583 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1584 @param[in] Data Pointer to variable "certdb".
1585 @param[in] DataSize Size of variable "certdb".
1586 @param[out] CertOffset Offset of matching CertData, from starting of Data.
1587 @param[out] CertDataSize Length of CertData in bytes.
1588 @param[out] CertNodeOffset Offset of matching AUTH_CERT_DB_DATA , from
1590 @param[out] CertNodeSize Length of AUTH_CERT_DB_DATA in bytes.
1592 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1593 @retval EFI_NOT_FOUND Fail to find matching certs.
1594 @retval EFI_SUCCESS Find matching certs and output parameters.
1599 IN CHAR16
*VariableName
,
1600 IN EFI_GUID
*VendorGuid
,
1603 OUT UINT32
*CertOffset
, OPTIONAL
1604 OUT UINT32
*CertDataSize
, OPTIONAL
1605 OUT UINT32
*CertNodeOffset
,OPTIONAL
1606 OUT UINT32
*CertNodeSize OPTIONAL
1610 AUTH_CERT_DB_DATA
*Ptr
;
1614 UINT32 CertDbListSize
;
1616 if ((VariableName
== NULL
) || (VendorGuid
== NULL
) || (Data
== NULL
)) {
1617 return EFI_INVALID_PARAMETER
;
1621 // Check whether DataSize matches recorded CertDbListSize.
1623 if (DataSize
< sizeof (UINT32
)) {
1624 return EFI_INVALID_PARAMETER
;
1627 CertDbListSize
= ReadUnaligned32 ((UINT32
*) Data
);
1629 if (CertDbListSize
!= (UINT32
) DataSize
) {
1630 return EFI_INVALID_PARAMETER
;
1633 Offset
= sizeof (UINT32
);
1636 // Get corresponding certificates by VendorGuid and VariableName.
1638 while (Offset
< (UINT32
) DataSize
) {
1639 Ptr
= (AUTH_CERT_DB_DATA
*) (Data
+ Offset
);
1641 // Check whether VendorGuid matches.
1643 if (CompareGuid (&Ptr
->VendorGuid
, VendorGuid
)) {
1644 NodeSize
= ReadUnaligned32 (&Ptr
->CertNodeSize
);
1645 NameSize
= ReadUnaligned32 (&Ptr
->NameSize
);
1646 CertSize
= ReadUnaligned32 (&Ptr
->CertDataSize
);
1648 if (NodeSize
!= sizeof (EFI_GUID
) + sizeof (UINT32
) * 3 + CertSize
+
1649 sizeof (CHAR16
) * NameSize
) {
1650 return EFI_INVALID_PARAMETER
;
1653 Offset
= Offset
+ sizeof (EFI_GUID
) + sizeof (UINT32
) * 3;
1655 // Check whether VariableName matches.
1657 if ((NameSize
== StrLen (VariableName
)) &&
1658 (CompareMem (Data
+ Offset
, VariableName
, NameSize
* sizeof (CHAR16
)) == 0)) {
1659 Offset
= Offset
+ NameSize
* sizeof (CHAR16
);
1661 if (CertOffset
!= NULL
) {
1662 *CertOffset
= Offset
;
1665 if (CertDataSize
!= NULL
) {
1666 *CertDataSize
= CertSize
;
1669 if (CertNodeOffset
!= NULL
) {
1670 *CertNodeOffset
= (UINT32
) ((UINT8
*) Ptr
- Data
);
1673 if (CertNodeSize
!= NULL
) {
1674 *CertNodeSize
= NodeSize
;
1679 Offset
= Offset
+ NameSize
* sizeof (CHAR16
) + CertSize
;
1682 NodeSize
= ReadUnaligned32 (&Ptr
->CertNodeSize
);
1683 Offset
= Offset
+ NodeSize
;
1687 return EFI_NOT_FOUND
;
1691 Retrieve signer's certificates for common authenticated variable
1692 by corresponding VariableName and VendorGuid from "certdb".
1694 @param[in] VariableName Name of authenticated Variable.
1695 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1696 @param[out] CertData Pointer to signer's certificates.
1697 @param[out] CertDataSize Length of CertData in bytes.
1699 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1700 @retval EFI_NOT_FOUND Fail to find "certdb" or matching certs.
1701 @retval EFI_SUCCESS Get signer's certificates successfully.
1706 IN CHAR16
*VariableName
,
1707 IN EFI_GUID
*VendorGuid
,
1708 OUT UINT8
**CertData
,
1709 OUT UINT32
*CertDataSize
1712 VARIABLE_POINTER_TRACK CertDbVariable
;
1718 if ((VariableName
== NULL
) || (VendorGuid
== NULL
) || (CertData
== NULL
) || (CertDataSize
== NULL
)) {
1719 return EFI_INVALID_PARAMETER
;
1723 // Get variable "certdb".
1725 Status
= FindVariable (
1729 &mVariableModuleGlobal
->VariableGlobal
,
1732 if (EFI_ERROR (Status
)) {
1736 DataSize
= DataSizeOfVariable (CertDbVariable
.CurrPtr
);
1737 Data
= GetVariableDataPtr (CertDbVariable
.CurrPtr
);
1738 if ((DataSize
== 0) || (Data
== NULL
)) {
1740 return EFI_NOT_FOUND
;
1743 Status
= FindCertsFromDb (
1754 if (EFI_ERROR (Status
)) {
1758 *CertData
= Data
+ CertOffset
;
1763 Delete matching signer's certificates when deleting common authenticated
1764 variable by corresponding VariableName and VendorGuid from "certdb".
1766 @param[in] VariableName Name of authenticated Variable.
1767 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1769 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1770 @retval EFI_NOT_FOUND Fail to find "certdb" or matching certs.
1771 @retval EFI_OUT_OF_RESOURCES The operation is failed due to lack of resources.
1772 @retval EFI_SUCCESS The operation is completed successfully.
1777 IN CHAR16
*VariableName
,
1778 IN EFI_GUID
*VendorGuid
1781 VARIABLE_POINTER_TRACK CertDbVariable
;
1786 UINT32 CertNodeOffset
;
1787 UINT32 CertNodeSize
;
1789 UINT32 NewCertDbSize
;
1791 if ((VariableName
== NULL
) || (VendorGuid
== NULL
)) {
1792 return EFI_INVALID_PARAMETER
;
1796 // Get variable "certdb".
1798 Status
= FindVariable (
1802 &mVariableModuleGlobal
->VariableGlobal
,
1805 if (EFI_ERROR (Status
)) {
1809 DataSize
= DataSizeOfVariable (CertDbVariable
.CurrPtr
);
1810 Data
= GetVariableDataPtr (CertDbVariable
.CurrPtr
);
1811 if ((DataSize
== 0) || (Data
== NULL
)) {
1813 return EFI_NOT_FOUND
;
1816 if (DataSize
== sizeof (UINT32
)) {
1818 // There is no certs in certdb.
1824 // Get corresponding cert node from certdb.
1826 Status
= FindCertsFromDb (
1837 if (EFI_ERROR (Status
)) {
1841 if (DataSize
< (CertNodeOffset
+ CertNodeSize
)) {
1842 return EFI_NOT_FOUND
;
1846 // Construct new data content of variable "certdb".
1848 NewCertDbSize
= (UINT32
) DataSize
- CertNodeSize
;
1849 NewCertDb
= (UINT8
*) mCertDbStore
;
1852 // Copy the DB entries before deleting node.
1854 CopyMem (NewCertDb
, Data
, CertNodeOffset
);
1856 // Update CertDbListSize.
1858 CopyMem (NewCertDb
, &NewCertDbSize
, sizeof (UINT32
));
1860 // Copy the DB entries after deleting node.
1862 if (DataSize
> (CertNodeOffset
+ CertNodeSize
)) {
1864 NewCertDb
+ CertNodeOffset
,
1865 Data
+ CertNodeOffset
+ CertNodeSize
,
1866 DataSize
- CertNodeOffset
- CertNodeSize
1873 VarAttr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
1874 Status
= UpdateVariable (
1890 Insert signer's certificates for common authenticated variable with VariableName
1891 and VendorGuid in AUTH_CERT_DB_DATA to "certdb".
1893 @param[in] VariableName Name of authenticated Variable.
1894 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1895 @param[in] CertData Pointer to signer's certificates.
1896 @param[in] CertDataSize Length of CertData in bytes.
1898 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1899 @retval EFI_ACCESS_DENIED An AUTH_CERT_DB_DATA entry with same VariableName
1900 and VendorGuid already exists.
1901 @retval EFI_OUT_OF_RESOURCES The operation is failed due to lack of resources.
1902 @retval EFI_SUCCESS Insert an AUTH_CERT_DB_DATA entry to "certdb"
1907 IN CHAR16
*VariableName
,
1908 IN EFI_GUID
*VendorGuid
,
1910 IN UINTN CertDataSize
1913 VARIABLE_POINTER_TRACK CertDbVariable
;
1919 UINT32 NewCertDbSize
;
1920 UINT32 CertNodeSize
;
1922 AUTH_CERT_DB_DATA
*Ptr
;
1924 if ((VariableName
== NULL
) || (VendorGuid
== NULL
) || (CertData
== NULL
)) {
1925 return EFI_INVALID_PARAMETER
;
1929 // Get variable "certdb".
1931 Status
= FindVariable (
1935 &mVariableModuleGlobal
->VariableGlobal
,
1938 if (EFI_ERROR (Status
)) {
1942 DataSize
= DataSizeOfVariable (CertDbVariable
.CurrPtr
);
1943 Data
= GetVariableDataPtr (CertDbVariable
.CurrPtr
);
1944 if ((DataSize
== 0) || (Data
== NULL
)) {
1946 return EFI_NOT_FOUND
;
1950 // Find whether matching cert node already exists in "certdb".
1951 // If yes return error.
1953 Status
= FindCertsFromDb (
1964 if (!EFI_ERROR (Status
)) {
1966 return EFI_ACCESS_DENIED
;
1970 // Construct new data content of variable "certdb".
1972 NameSize
= (UINT32
) StrLen (VariableName
);
1973 CertNodeSize
= sizeof (AUTH_CERT_DB_DATA
) + (UINT32
) CertDataSize
+ NameSize
* sizeof (CHAR16
);
1974 NewCertDbSize
= (UINT32
) DataSize
+ CertNodeSize
;
1975 if (NewCertDbSize
> MAX_CERTDB_SIZE
) {
1976 return EFI_OUT_OF_RESOURCES
;
1978 NewCertDb
= (UINT8
*) mCertDbStore
;
1981 // Copy the DB entries before deleting node.
1983 CopyMem (NewCertDb
, Data
, DataSize
);
1985 // Update CertDbListSize.
1987 CopyMem (NewCertDb
, &NewCertDbSize
, sizeof (UINT32
));
1989 // Construct new cert node.
1991 Ptr
= (AUTH_CERT_DB_DATA
*) (NewCertDb
+ DataSize
);
1992 CopyGuid (&Ptr
->VendorGuid
, VendorGuid
);
1993 CopyMem (&Ptr
->CertNodeSize
, &CertNodeSize
, sizeof (UINT32
));
1994 CopyMem (&Ptr
->NameSize
, &NameSize
, sizeof (UINT32
));
1995 CopyMem (&Ptr
->CertDataSize
, &CertDataSize
, sizeof (UINT32
));
1998 (UINT8
*) Ptr
+ sizeof (AUTH_CERT_DB_DATA
),
2000 NameSize
* sizeof (CHAR16
)
2004 (UINT8
*) Ptr
+ sizeof (AUTH_CERT_DB_DATA
) + NameSize
* sizeof (CHAR16
),
2012 VarAttr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
2013 Status
= UpdateVariable (
2029 Process variable with EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set
2031 Caution: This function may receive untrusted input.
2032 This function may be invoked in SMM mode, and datasize and data are external input.
2033 This function will do basic validation, before parse the data.
2034 This function will parse the authentication carefully to avoid security issues, like
2035 buffer overflow, integer overflow.
2037 @param[in] VariableName Name of Variable to be found.
2038 @param[in] VendorGuid Variable vendor GUID.
2039 @param[in] Data Data pointer.
2040 @param[in] DataSize Size of Data found. If size is less than the
2041 data, this value contains the required size.
2042 @param[in] Variable The variable information which is used to keep track of variable usage.
2043 @param[in] Attributes Attribute value of the variable.
2044 @param[in] AuthVarType Verify against PK, KEK database, private database or certificate in data payload.
2045 @param[out] VarDel Delete the variable or not.
2047 @retval EFI_INVALID_PARAMETER Invalid parameter.
2048 @retval EFI_SECURITY_VIOLATION The variable does NOT pass the validation
2049 check carried out by the firmware.
2050 @retval EFI_OUT_OF_RESOURCES Failed to process variable due to lack
2052 @retval EFI_SUCCESS Variable pass validation successfully.
2056 VerifyTimeBasedPayload (
2057 IN CHAR16
*VariableName
,
2058 IN EFI_GUID
*VendorGuid
,
2061 IN VARIABLE_POINTER_TRACK
*Variable
,
2062 IN UINT32 Attributes
,
2063 IN AUTHVAR_TYPE AuthVarType
,
2077 BOOLEAN VerifyStatus
;
2079 EFI_SIGNATURE_LIST
*CertList
;
2080 EFI_SIGNATURE_DATA
*Cert
;
2081 VARIABLE_POINTER_TRACK KekVariable
;
2082 EFI_VARIABLE_AUTHENTICATION_2
*CertData
;
2085 VARIABLE_POINTER_TRACK PkVariable
;
2090 UINTN CertStackSize
;
2091 UINT8
*CertsInCertDb
;
2092 UINT32 CertsSizeinDb
;
2094 VerifyStatus
= FALSE
;
2103 // When the attribute EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS is
2104 // set, then the Data buffer shall begin with an instance of a complete (and serialized)
2105 // EFI_VARIABLE_AUTHENTICATION_2 descriptor. The descriptor shall be followed by the new
2106 // variable value and DataSize shall reflect the combined size of the descriptor and the new
2107 // variable value. The authentication descriptor is not part of the variable data and is not
2108 // returned by subsequent calls to GetVariable().
2110 CertData
= (EFI_VARIABLE_AUTHENTICATION_2
*) Data
;
2113 // Verify that Pad1, Nanosecond, TimeZone, Daylight and Pad2 components of the
2114 // TimeStamp value are set to zero.
2116 if ((CertData
->TimeStamp
.Pad1
!= 0) ||
2117 (CertData
->TimeStamp
.Nanosecond
!= 0) ||
2118 (CertData
->TimeStamp
.TimeZone
!= 0) ||
2119 (CertData
->TimeStamp
.Daylight
!= 0) ||
2120 (CertData
->TimeStamp
.Pad2
!= 0)) {
2121 return EFI_SECURITY_VIOLATION
;
2124 if ((Variable
->CurrPtr
!= NULL
) && ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0)) {
2125 if (CompareTimeStamp (&CertData
->TimeStamp
, &Variable
->CurrPtr
->TimeStamp
)) {
2127 // TimeStamp check fail, suspicious replay attack, return EFI_SECURITY_VIOLATION.
2129 return EFI_SECURITY_VIOLATION
;
2134 // wCertificateType should be WIN_CERT_TYPE_EFI_GUID.
2135 // Cert type should be EFI_CERT_TYPE_PKCS7_GUID.
2137 if ((CertData
->AuthInfo
.Hdr
.wCertificateType
!= WIN_CERT_TYPE_EFI_GUID
) ||
2138 !CompareGuid (&CertData
->AuthInfo
.CertType
, &gEfiCertPkcs7Guid
)) {
2140 // Invalid AuthInfo type, return EFI_SECURITY_VIOLATION.
2142 return EFI_SECURITY_VIOLATION
;
2146 // Find out Pkcs7 SignedData which follows the EFI_VARIABLE_AUTHENTICATION_2 descriptor.
2147 // AuthInfo.Hdr.dwLength is the length of the entire certificate, including the length of the header.
2149 SigData
= CertData
->AuthInfo
.CertData
;
2150 SigDataSize
= CertData
->AuthInfo
.Hdr
.dwLength
- (UINT32
) (OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID
, CertData
));
2153 // Find out the new data payload which follows Pkcs7 SignedData directly.
2155 PayloadPtr
= SigData
+ SigDataSize
;
2156 PayloadSize
= DataSize
- OFFSET_OF_AUTHINFO2_CERT_DATA
- (UINTN
) SigDataSize
;
2159 // Construct a buffer to fill with (VariableName, VendorGuid, Attributes, TimeStamp, Data).
2161 NewDataSize
= PayloadSize
+ sizeof (EFI_TIME
) + sizeof (UINT32
) +
2162 sizeof (EFI_GUID
) + StrSize (VariableName
) - sizeof (CHAR16
);
2163 NewData
= mSerializationRuntimeBuffer
;
2166 Length
= StrLen (VariableName
) * sizeof (CHAR16
);
2167 CopyMem (Buffer
, VariableName
, Length
);
2170 Length
= sizeof (EFI_GUID
);
2171 CopyMem (Buffer
, VendorGuid
, Length
);
2174 Length
= sizeof (UINT32
);
2175 CopyMem (Buffer
, &Attr
, Length
);
2178 Length
= sizeof (EFI_TIME
);
2179 CopyMem (Buffer
, &CertData
->TimeStamp
, Length
);
2182 CopyMem (Buffer
, PayloadPtr
, PayloadSize
);
2184 if (AuthVarType
== AuthVarTypePk
) {
2186 // Verify that the signature has been made with the current Platform Key (no chaining for PK).
2187 // First, get signer's certificates from SignedData.
2189 VerifyStatus
= Pkcs7GetSigners (
2197 if (!VerifyStatus
) {
2202 // Second, get the current platform key from variable. Check whether it's identical with signer's certificates
2203 // in SignedData. If not, return error immediately.
2205 Status
= FindVariable (
2206 EFI_PLATFORM_KEY_NAME
,
2207 &gEfiGlobalVariableGuid
,
2209 &mVariableModuleGlobal
->VariableGlobal
,
2212 if (EFI_ERROR (Status
)) {
2213 VerifyStatus
= FALSE
;
2216 CertList
= (EFI_SIGNATURE_LIST
*) GetVariableDataPtr (PkVariable
.CurrPtr
);
2217 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
2218 if ((RootCertSize
!= (CertList
->SignatureSize
- (sizeof (EFI_SIGNATURE_DATA
) - 1))) ||
2219 (CompareMem (Cert
->SignatureData
, RootCert
, RootCertSize
) != 0)) {
2220 VerifyStatus
= FALSE
;
2225 // Verify Pkcs7 SignedData via Pkcs7Verify library.
2227 VerifyStatus
= Pkcs7Verify (
2236 } else if (AuthVarType
== AuthVarTypeKek
) {
2239 // Get KEK database from variable.
2241 Status
= FindVariable (
2242 EFI_KEY_EXCHANGE_KEY_NAME
,
2243 &gEfiGlobalVariableGuid
,
2245 &mVariableModuleGlobal
->VariableGlobal
,
2248 if (EFI_ERROR (Status
)) {
2253 // Ready to verify Pkcs7 SignedData. Go through KEK Signature Database to find out X.509 CertList.
2255 KekDataSize
= KekVariable
.CurrPtr
->DataSize
;
2256 CertList
= (EFI_SIGNATURE_LIST
*) GetVariableDataPtr (KekVariable
.CurrPtr
);
2257 while ((KekDataSize
> 0) && (KekDataSize
>= CertList
->SignatureListSize
)) {
2258 if (CompareGuid (&CertList
->SignatureType
, &gEfiCertX509Guid
)) {
2259 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
2260 CertCount
= (CertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - CertList
->SignatureHeaderSize
) / CertList
->SignatureSize
;
2261 for (Index
= 0; Index
< CertCount
; Index
++) {
2263 // Iterate each Signature Data Node within this CertList for a verify
2265 RootCert
= Cert
->SignatureData
;
2266 RootCertSize
= CertList
->SignatureSize
- (sizeof (EFI_SIGNATURE_DATA
) - 1);
2269 // Verify Pkcs7 SignedData via Pkcs7Verify library.
2271 VerifyStatus
= Pkcs7Verify (
2282 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) Cert
+ CertList
->SignatureSize
);
2285 KekDataSize
-= CertList
->SignatureListSize
;
2286 CertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) CertList
+ CertList
->SignatureListSize
);
2288 } else if (AuthVarType
== AuthVarTypePriv
) {
2291 // Process common authenticated variable except PK/KEK/DB/DBX.
2292 // Get signer's certificates from SignedData.
2294 VerifyStatus
= Pkcs7GetSigners (
2302 if (!VerifyStatus
) {
2307 // Get previously stored signer's certificates from certdb for existing
2308 // variable. Check whether they are identical with signer's certificates
2309 // in SignedData. If not, return error immediately.
2311 if ((Variable
->CurrPtr
!= NULL
)) {
2312 VerifyStatus
= FALSE
;
2314 Status
= GetCertsFromDb (VariableName
, VendorGuid
, &CertsInCertDb
, &CertsSizeinDb
);
2315 if (EFI_ERROR (Status
)) {
2319 if ((CertStackSize
!= CertsSizeinDb
) ||
2320 (CompareMem (SignerCerts
, CertsInCertDb
, CertsSizeinDb
) != 0)) {
2325 VerifyStatus
= Pkcs7Verify (
2333 if (!VerifyStatus
) {
2338 // Delete signer's certificates when delete the common authenticated variable.
2340 if ((PayloadSize
== 0) && (Variable
->CurrPtr
!= NULL
) && ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0)) {
2341 Status
= DeleteCertsFromDb (VariableName
, VendorGuid
);
2342 if (EFI_ERROR (Status
)) {
2343 VerifyStatus
= FALSE
;
2346 } else if (Variable
->CurrPtr
== NULL
&& PayloadSize
!= 0) {
2348 // Insert signer's certificates when adding a new common authenticated variable.
2350 Status
= InsertCertsToDb (VariableName
, VendorGuid
, SignerCerts
, CertStackSize
);
2351 if (EFI_ERROR (Status
)) {
2352 VerifyStatus
= FALSE
;
2356 } else if (AuthVarType
== AuthVarTypePayload
) {
2357 CertList
= (EFI_SIGNATURE_LIST
*) PayloadPtr
;
2358 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
2359 RootCert
= Cert
->SignatureData
;
2360 RootCertSize
= CertList
->SignatureSize
- (sizeof (EFI_SIGNATURE_DATA
) - 1);
2362 // Verify Pkcs7 SignedData via Pkcs7Verify library.
2364 VerifyStatus
= Pkcs7Verify (
2373 return EFI_SECURITY_VIOLATION
;
2378 if (AuthVarType
== AuthVarTypePk
|| AuthVarType
== AuthVarTypePriv
) {
2379 Pkcs7FreeSigners (RootCert
);
2380 Pkcs7FreeSigners (SignerCerts
);
2383 if (!VerifyStatus
) {
2384 return EFI_SECURITY_VIOLATION
;
2387 Status
= CheckSignatureListFormat(VariableName
, VendorGuid
, PayloadPtr
, PayloadSize
);
2388 if (EFI_ERROR (Status
)) {
2392 if ((PayloadSize
== 0) && (VarDel
!= NULL
)) {
2397 // Final step: Update/Append Variable if it pass Pkcs7Verify
2399 return UpdateVariable (
2408 &CertData
->TimeStamp