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
40 UINT32 mMaxCertDbSize
;
42 UINT8 mVendorKeyState
;
44 EFI_GUID mSignatureSupport
[] = {EFI_CERT_SHA1_GUID
, EFI_CERT_SHA256_GUID
, EFI_CERT_RSA2048_GUID
, EFI_CERT_X509_GUID
};
46 // Public Exponent of RSA Key.
48 CONST UINT8 mRsaE
[] = { 0x01, 0x00, 0x01 };
50 // Hash context pointer
52 VOID
*mHashCtx
= NULL
;
55 // The serialization of the values of the VariableName, VendorGuid and Attributes
56 // parameters of the SetVariable() call and the TimeStamp component of the
57 // EFI_VARIABLE_AUTHENTICATION_2 descriptor followed by the variable's new value
58 // i.e. (VariableName, VendorGuid, Attributes, TimeStamp, Data)
60 UINT8
*mSerializationRuntimeBuffer
= NULL
;
63 // Requirement for different signature type which have been defined in UEFI spec.
64 // These data are used to peform SignatureList format check while setting PK/KEK variable.
66 EFI_SIGNATURE_ITEM mSupportSigItem
[] = {
67 //{SigType, SigHeaderSize, SigDataSize }
68 {EFI_CERT_SHA256_GUID
, 0, 32 },
69 {EFI_CERT_RSA2048_GUID
, 0, 256 },
70 {EFI_CERT_RSA2048_SHA256_GUID
, 0, 256 },
71 {EFI_CERT_SHA1_GUID
, 0, 20 },
72 {EFI_CERT_RSA2048_SHA1_GUID
, 0, 256 },
73 {EFI_CERT_X509_GUID
, 0, ((UINT32
) ~0)},
74 {EFI_CERT_SHA224_GUID
, 0, 28 },
75 {EFI_CERT_SHA384_GUID
, 0, 48 },
76 {EFI_CERT_SHA512_GUID
, 0, 64 }
80 Determine whether this operation needs a physical present user.
82 @param[in] VariableName Name of the Variable.
83 @param[in] VendorGuid GUID of the Variable.
85 @retval TRUE This variable is protected, only a physical present user could set this variable.
86 @retval FALSE This variable is not protected.
90 NeedPhysicallyPresent(
91 IN CHAR16
*VariableName
,
92 IN EFI_GUID
*VendorGuid
95 if ((CompareGuid (VendorGuid
, &gEfiSecureBootEnableDisableGuid
) && (StrCmp (VariableName
, EFI_SECURE_BOOT_ENABLE_NAME
) == 0))
96 || (CompareGuid (VendorGuid
, &gEfiCustomModeEnableGuid
) && (StrCmp (VariableName
, EFI_CUSTOM_MODE_NAME
) == 0))) {
104 Determine whether the platform is operating in Custom Secure Boot mode.
106 @retval TRUE The platform is operating in Custom mode.
107 @retval FALSE The platform is operating in Standard mode.
115 VARIABLE_POINTER_TRACK Variable
;
117 FindVariable (EFI_CUSTOM_MODE_NAME
, &gEfiCustomModeEnableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
118 if (Variable
.CurrPtr
!= NULL
&& *(GetVariableDataPtr (Variable
.CurrPtr
)) == CUSTOM_SECURE_BOOT_MODE
) {
127 Internal function to delete a Variable given its name and GUID, no authentication
130 @param[in] VariableName Name of the Variable.
131 @param[in] VendorGuid GUID of the Variable.
133 @retval EFI_SUCCESS Variable deleted successfully.
134 @retval Others The driver failded to start the device.
139 IN CHAR16
*VariableName
,
140 IN EFI_GUID
*VendorGuid
144 VARIABLE_POINTER_TRACK Variable
;
146 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
147 if (EFI_ERROR (Status
)) {
151 ASSERT (Variable
.CurrPtr
!= NULL
);
152 return UpdateVariable (VariableName
, VendorGuid
, NULL
, 0, 0, 0, 0, &Variable
, NULL
);
156 Initializes for authenticated varibale service.
158 @retval EFI_SUCCESS Function successfully executed.
159 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resources.
163 AutenticatedVariableServiceInitialize (
168 VARIABLE_POINTER_TRACK Variable
;
169 VARIABLE_POINTER_TRACK PkVariable
;
175 UINT8 SecureBootMode
;
176 UINT8 SecureBootEnable
;
181 // Initialize hash context.
183 CtxSize
= Sha256GetContextSize ();
184 mHashCtx
= AllocateRuntimePool (CtxSize
);
185 if (mHashCtx
== NULL
) {
186 return EFI_OUT_OF_RESOURCES
;
190 // Reserve runtime buffer for public key database. The size excludes variable header and name size.
192 mMaxKeyDbSize
= PcdGet32 (PcdMaxVariableSize
) - sizeof (VARIABLE_HEADER
) - sizeof (AUTHVAR_KEYDB_NAME
);
193 mMaxKeyNumber
= mMaxKeyDbSize
/ EFI_CERT_TYPE_RSA2048_SIZE
;
194 mPubKeyStore
= AllocateRuntimePool (mMaxKeyDbSize
);
195 if (mPubKeyStore
== NULL
) {
196 return EFI_OUT_OF_RESOURCES
;
200 // Reserve runtime buffer for certificate database. The size excludes variable header and name size.
202 mMaxCertDbSize
= PcdGet32 (PcdMaxVariableSize
) - sizeof (VARIABLE_HEADER
) - sizeof (EFI_CERT_DB_NAME
);
203 mCertDbStore
= AllocateRuntimePool (mMaxCertDbSize
);
204 if (mCertDbStore
== NULL
) {
205 return EFI_OUT_OF_RESOURCES
;
209 // Prepare runtime buffer for serialized data of time-based authenticated
210 // Variable, i.e. (VariableName, VendorGuid, Attributes, TimeStamp, Data).
212 mSerializationRuntimeBuffer
= AllocateRuntimePool (PcdGet32 (PcdMaxVariableSize
) + sizeof (EFI_GUID
) + sizeof (UINT32
) + sizeof (EFI_TIME
));
213 if (mSerializationRuntimeBuffer
== NULL
) {
214 return EFI_OUT_OF_RESOURCES
;
218 // Check "AuthVarKeyDatabase" variable's existence.
219 // If it doesn't exist, create a new one with initial value of 0 and EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS set.
221 Status
= FindVariable (
223 &gEfiAuthenticatedVariableGuid
,
225 &mVariableModuleGlobal
->VariableGlobal
,
229 if (Variable
.CurrPtr
== NULL
) {
230 VarAttr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
;
233 Status
= UpdateVariable (
235 &gEfiAuthenticatedVariableGuid
,
244 if (EFI_ERROR (Status
)) {
249 // Load database in global variable for cache.
251 DataSize
= DataSizeOfVariable (Variable
.CurrPtr
);
252 Data
= GetVariableDataPtr (Variable
.CurrPtr
);
253 ASSERT ((DataSize
!= 0) && (Data
!= NULL
));
254 CopyMem (mPubKeyStore
, (UINT8
*) Data
, DataSize
);
255 mPubKeyNumber
= (UINT32
) (DataSize
/ EFI_CERT_TYPE_RSA2048_SIZE
);
258 FindVariable (EFI_PLATFORM_KEY_NAME
, &gEfiGlobalVariableGuid
, &PkVariable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
259 if (PkVariable
.CurrPtr
== NULL
) {
260 DEBUG ((EFI_D_INFO
, "Variable %s does not exist.\n", EFI_PLATFORM_KEY_NAME
));
262 DEBUG ((EFI_D_INFO
, "Variable %s exists.\n", EFI_PLATFORM_KEY_NAME
));
266 // Create "SetupMode" variable with BS+RT attribute set.
268 FindVariable (EFI_SETUP_MODE_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
269 if (PkVariable
.CurrPtr
== NULL
) {
270 mPlatformMode
= SETUP_MODE
;
272 mPlatformMode
= USER_MODE
;
274 Status
= UpdateVariable (
276 &gEfiGlobalVariableGuid
,
279 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
285 if (EFI_ERROR (Status
)) {
290 // Create "SignatureSupport" variable with BS+RT attribute set.
292 FindVariable (EFI_SIGNATURE_SUPPORT_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
293 Status
= UpdateVariable (
294 EFI_SIGNATURE_SUPPORT_NAME
,
295 &gEfiGlobalVariableGuid
,
297 sizeof(mSignatureSupport
),
298 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
304 if (EFI_ERROR (Status
)) {
309 // If "SecureBootEnable" variable exists, then update "SecureBoot" variable.
310 // If "SecureBootEnable" variable is SECURE_BOOT_ENABLE and in USER_MODE, Set "SecureBoot" variable to SECURE_BOOT_MODE_ENABLE.
311 // If "SecureBootEnable" variable is SECURE_BOOT_DISABLE, Set "SecureBoot" variable to SECURE_BOOT_MODE_DISABLE.
313 SecureBootEnable
= SECURE_BOOT_DISABLE
;
314 FindVariable (EFI_SECURE_BOOT_ENABLE_NAME
, &gEfiSecureBootEnableDisableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
315 if (Variable
.CurrPtr
!= NULL
) {
316 SecureBootEnable
= *(GetVariableDataPtr (Variable
.CurrPtr
));
317 } else if (mPlatformMode
== USER_MODE
) {
319 // "SecureBootEnable" not exist, initialize it in USER_MODE.
321 SecureBootEnable
= SECURE_BOOT_ENABLE
;
322 Status
= UpdateVariable (
323 EFI_SECURE_BOOT_ENABLE_NAME
,
324 &gEfiSecureBootEnableDisableGuid
,
327 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
333 if (EFI_ERROR (Status
)) {
339 // Create "SecureBoot" variable with BS+RT attribute set.
341 if (SecureBootEnable
== SECURE_BOOT_ENABLE
&& mPlatformMode
== USER_MODE
) {
342 SecureBootMode
= SECURE_BOOT_MODE_ENABLE
;
344 SecureBootMode
= SECURE_BOOT_MODE_DISABLE
;
346 FindVariable (EFI_SECURE_BOOT_MODE_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
347 Status
= UpdateVariable (
348 EFI_SECURE_BOOT_MODE_NAME
,
349 &gEfiGlobalVariableGuid
,
352 EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
358 if (EFI_ERROR (Status
)) {
362 DEBUG ((EFI_D_INFO
, "Variable %s is %x\n", EFI_SETUP_MODE_NAME
, mPlatformMode
));
363 DEBUG ((EFI_D_INFO
, "Variable %s is %x\n", EFI_SECURE_BOOT_MODE_NAME
, SecureBootMode
));
364 DEBUG ((EFI_D_INFO
, "Variable %s is %x\n", EFI_SECURE_BOOT_ENABLE_NAME
, SecureBootEnable
));
367 // Initialize "CustomMode" in STANDARD_SECURE_BOOT_MODE state.
369 FindVariable (EFI_CUSTOM_MODE_NAME
, &gEfiCustomModeEnableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
370 CustomMode
= STANDARD_SECURE_BOOT_MODE
;
371 Status
= UpdateVariable (
372 EFI_CUSTOM_MODE_NAME
,
373 &gEfiCustomModeEnableGuid
,
376 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
382 if (EFI_ERROR (Status
)) {
386 DEBUG ((EFI_D_INFO
, "Variable %s is %x\n", EFI_CUSTOM_MODE_NAME
, CustomMode
));
389 // Check "certdb" variable's existence.
390 // If it doesn't exist, then create a new one with
391 // EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set.
393 Status
= FindVariable (
397 &mVariableModuleGlobal
->VariableGlobal
,
401 if (Variable
.CurrPtr
== NULL
) {
402 VarAttr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
403 ListSize
= sizeof (UINT32
);
404 Status
= UpdateVariable (
415 if (EFI_ERROR (Status
)) {
421 // Check "VendorKeysNv" variable's existence and create "VendorKeys" variable accordingly.
423 FindVariable (EFI_VENDOR_KEYS_NV_VARIABLE_NAME
, &gEfiVendorKeysNvGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
424 if (Variable
.CurrPtr
!= NULL
) {
425 mVendorKeyState
= *(GetVariableDataPtr (Variable
.CurrPtr
));
428 // "VendorKeysNv" not exist, initialize it in VENDOR_KEYS_VALID state.
430 mVendorKeyState
= VENDOR_KEYS_VALID
;
431 Status
= UpdateVariable (
432 EFI_VENDOR_KEYS_NV_VARIABLE_NAME
,
433 &gEfiVendorKeysNvGuid
,
436 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
,
442 if (EFI_ERROR (Status
)) {
448 // Create "VendorKeys" variable with BS+RT attribute set.
450 FindVariable (EFI_VENDOR_KEYS_VARIABLE_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
451 Status
= UpdateVariable (
452 EFI_VENDOR_KEYS_VARIABLE_NAME
,
453 &gEfiGlobalVariableGuid
,
456 EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
462 if (EFI_ERROR (Status
)) {
466 DEBUG ((EFI_D_INFO
, "Variable %s is %x\n", EFI_VENDOR_KEYS_VARIABLE_NAME
, mVendorKeyState
));
472 Add public key in store and return its index.
474 @param[in] PubKey Input pointer to Public Key data
475 @param[in] VariableDataEntry The variable data entry
477 @return Index of new added item
483 IN VARIABLE_ENTRY_CONSISTENCY
*VariableDataEntry
489 VARIABLE_POINTER_TRACK Variable
;
493 VARIABLE_ENTRY_CONSISTENCY PublicKeyEntry
;
496 if (PubKey
== NULL
) {
500 Status
= FindVariable (
502 &gEfiAuthenticatedVariableGuid
,
504 &mVariableModuleGlobal
->VariableGlobal
,
507 if (EFI_ERROR (Status
)) {
508 DEBUG ((EFI_D_ERROR
, "Get public key database variable failure, Status = %r\n", Status
));
513 // Check whether the public key entry does exist.
516 for (Ptr
= mPubKeyStore
, Index
= 1; Index
<= mPubKeyNumber
; Index
++) {
517 if (CompareMem (Ptr
, PubKey
, EFI_CERT_TYPE_RSA2048_SIZE
) == 0) {
521 Ptr
+= EFI_CERT_TYPE_RSA2048_SIZE
;
526 // Add public key in database.
528 if (mPubKeyNumber
== mMaxKeyNumber
) {
530 // Public key dadatase is full, try to reclaim invalid key.
534 // NV storage can't reclaim at runtime.
540 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
541 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
548 if (EFI_ERROR (Status
)) {
552 Status
= FindVariable (
554 &gEfiAuthenticatedVariableGuid
,
556 &mVariableModuleGlobal
->VariableGlobal
,
559 if (EFI_ERROR (Status
)) {
560 DEBUG ((EFI_D_ERROR
, "Get public key database variable failure, Status = %r\n", Status
));
564 DataSize
= DataSizeOfVariable (Variable
.CurrPtr
);
565 Data
= GetVariableDataPtr (Variable
.CurrPtr
);
566 ASSERT ((DataSize
!= 0) && (Data
!= NULL
));
567 CopyMem (mPubKeyStore
, (UINT8
*) Data
, DataSize
);
568 mPubKeyNumber
= (UINT32
) (DataSize
/ EFI_CERT_TYPE_RSA2048_SIZE
);
570 if (mPubKeyNumber
== mMaxKeyNumber
) {
576 // Check the variable space for both public key and variable data.
578 PublicKeyEntry
.VariableSize
= (mPubKeyNumber
+ 1) * EFI_CERT_TYPE_RSA2048_SIZE
;
579 PublicKeyEntry
.Guid
= &gEfiAuthenticatedVariableGuid
;
580 PublicKeyEntry
.Name
= AUTHVAR_KEYDB_NAME
;
581 Attributes
= VARIABLE_ATTRIBUTE_NV_BS_RT
| EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
;
583 if (!CheckRemainingSpaceForConsistency (Attributes
, &PublicKeyEntry
, VariableDataEntry
, NULL
)) {
585 // No enough variable space.
590 CopyMem (mPubKeyStore
+ mPubKeyNumber
* EFI_CERT_TYPE_RSA2048_SIZE
, PubKey
, EFI_CERT_TYPE_RSA2048_SIZE
);
591 Index
= ++mPubKeyNumber
;
593 // Update public key database variable.
595 Status
= UpdateVariable (
597 &gEfiAuthenticatedVariableGuid
,
599 mPubKeyNumber
* EFI_CERT_TYPE_RSA2048_SIZE
,
606 if (EFI_ERROR (Status
)) {
607 DEBUG ((EFI_D_ERROR
, "Update public key database variable failure, Status = %r\n", Status
));
616 Verify data payload with AuthInfo in EFI_CERT_TYPE_RSA2048_SHA256_GUID type.
617 Follow the steps in UEFI2.2.
619 Caution: This function may receive untrusted input.
620 This function may be invoked in SMM mode, and datasize and data are external input.
621 This function will do basic validation, before parse the data.
622 This function will parse the authentication carefully to avoid security issues, like
623 buffer overflow, integer overflow.
625 @param[in] Data Pointer to data with AuthInfo.
626 @param[in] DataSize Size of Data.
627 @param[in] PubKey Public key used for verification.
629 @retval EFI_INVALID_PARAMETER Invalid parameter.
630 @retval EFI_SECURITY_VIOLATION If authentication failed.
631 @retval EFI_SUCCESS Authentication successful.
635 VerifyCounterBasedPayload (
642 EFI_VARIABLE_AUTHENTICATION
*CertData
;
643 EFI_CERT_BLOCK_RSA_2048_SHA256
*CertBlock
;
644 UINT8 Digest
[SHA256_DIGEST_SIZE
];
648 PayloadSize
= DataSize
- AUTHINFO_SIZE
;
653 if (Data
== NULL
|| PubKey
== NULL
) {
654 return EFI_INVALID_PARAMETER
;
657 CertData
= (EFI_VARIABLE_AUTHENTICATION
*) Data
;
658 CertBlock
= (EFI_CERT_BLOCK_RSA_2048_SHA256
*) (CertData
->AuthInfo
.CertData
);
661 // wCertificateType should be WIN_CERT_TYPE_EFI_GUID.
662 // Cert type should be EFI_CERT_TYPE_RSA2048_SHA256_GUID.
664 if ((CertData
->AuthInfo
.Hdr
.wCertificateType
!= WIN_CERT_TYPE_EFI_GUID
) ||
665 !CompareGuid (&CertData
->AuthInfo
.CertType
, &gEfiCertTypeRsa2048Sha256Guid
)
668 // Invalid AuthInfo type, return EFI_SECURITY_VIOLATION.
670 return EFI_SECURITY_VIOLATION
;
673 // Hash data payload with SHA256.
675 ZeroMem (Digest
, SHA256_DIGEST_SIZE
);
676 Status
= Sha256Init (mHashCtx
);
680 Status
= Sha256Update (mHashCtx
, Data
+ AUTHINFO_SIZE
, PayloadSize
);
687 Status
= Sha256Update (mHashCtx
, &PayloadSize
, sizeof (UINTN
));
692 // Hash Monotonic Count.
694 Status
= Sha256Update (mHashCtx
, &CertData
->MonotonicCount
, sizeof (UINT64
));
698 Status
= Sha256Final (mHashCtx
, Digest
);
703 // Generate & Initialize RSA Context.
706 ASSERT (Rsa
!= NULL
);
708 // Set RSA Key Components.
709 // NOTE: Only N and E are needed to be set as RSA public key for signature verification.
711 Status
= RsaSetKey (Rsa
, RsaKeyN
, PubKey
, EFI_CERT_TYPE_RSA2048_SIZE
);
715 Status
= RsaSetKey (Rsa
, RsaKeyE
, mRsaE
, sizeof (mRsaE
));
720 // Verify the signature.
722 Status
= RsaPkcs1Verify (
726 CertBlock
->Signature
,
727 EFI_CERT_TYPE_RSA2048_SHA256_SIZE
737 return EFI_SECURITY_VIOLATION
;
742 Update platform mode.
744 @param[in] Mode SETUP_MODE or USER_MODE.
746 @return EFI_INVALID_PARAMETER Invalid parameter.
747 @return EFI_SUCCESS Update platform mode successfully.
756 VARIABLE_POINTER_TRACK Variable
;
757 UINT8 SecureBootMode
;
758 UINT8 SecureBootEnable
;
759 UINTN VariableDataSize
;
761 Status
= FindVariable (
763 &gEfiGlobalVariableGuid
,
765 &mVariableModuleGlobal
->VariableGlobal
,
768 if (EFI_ERROR (Status
)) {
773 // Update the value of SetupMode variable by a simple mem copy, this could avoid possible
774 // variable storage reclaim at runtime.
776 mPlatformMode
= (UINT8
) Mode
;
777 CopyMem (GetVariableDataPtr (Variable
.CurrPtr
), &mPlatformMode
, sizeof(UINT8
));
781 // SecureBoot Variable indicates whether the platform firmware is operating
782 // in Secure boot mode (1) or not (0), so we should not change SecureBoot
783 // Variable in runtime.
789 // Check "SecureBoot" variable's existence.
790 // If it doesn't exist, firmware has no capability to perform driver signing verification,
791 // then set "SecureBoot" to 0.
793 Status
= FindVariable (
794 EFI_SECURE_BOOT_MODE_NAME
,
795 &gEfiGlobalVariableGuid
,
797 &mVariableModuleGlobal
->VariableGlobal
,
801 // If "SecureBoot" variable exists, then check "SetupMode" variable update.
802 // If "SetupMode" variable is USER_MODE, "SecureBoot" variable is set to 1.
803 // If "SetupMode" variable is SETUP_MODE, "SecureBoot" variable is set to 0.
805 if (Variable
.CurrPtr
== NULL
) {
806 SecureBootMode
= SECURE_BOOT_MODE_DISABLE
;
808 if (mPlatformMode
== USER_MODE
) {
809 SecureBootMode
= SECURE_BOOT_MODE_ENABLE
;
810 } else if (mPlatformMode
== SETUP_MODE
) {
811 SecureBootMode
= SECURE_BOOT_MODE_DISABLE
;
813 return EFI_NOT_FOUND
;
817 Status
= UpdateVariable (
818 EFI_SECURE_BOOT_MODE_NAME
,
819 &gEfiGlobalVariableGuid
,
822 EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
828 if (EFI_ERROR (Status
)) {
833 // Check "SecureBootEnable" variable's existence. It can enable/disable secure boot feature.
835 Status
= FindVariable (
836 EFI_SECURE_BOOT_ENABLE_NAME
,
837 &gEfiSecureBootEnableDisableGuid
,
839 &mVariableModuleGlobal
->VariableGlobal
,
843 if (SecureBootMode
== SECURE_BOOT_MODE_ENABLE
) {
845 // Create the "SecureBootEnable" variable as secure boot is enabled.
847 SecureBootEnable
= SECURE_BOOT_ENABLE
;
848 VariableDataSize
= sizeof (SecureBootEnable
);
851 // Delete the "SecureBootEnable" variable if this variable exist as "SecureBoot"
852 // variable is not in secure boot state.
854 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
857 SecureBootEnable
= SECURE_BOOT_DISABLE
;
858 VariableDataSize
= 0;
861 Status
= UpdateVariable (
862 EFI_SECURE_BOOT_ENABLE_NAME
,
863 &gEfiSecureBootEnableDisableGuid
,
866 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
876 Check input data form to make sure it is a valid EFI_SIGNATURE_LIST for PK/KEK/db/dbx variable.
878 @param[in] VariableName Name of Variable to be check.
879 @param[in] VendorGuid Variable vendor GUID.
880 @param[in] Data Point to the variable data to be checked.
881 @param[in] DataSize Size of Data.
883 @return EFI_INVALID_PARAMETER Invalid signature list format.
884 @return EFI_SUCCESS Passed signature list format check successfully.
888 CheckSignatureListFormat(
889 IN CHAR16
*VariableName
,
890 IN EFI_GUID
*VendorGuid
,
895 EFI_SIGNATURE_LIST
*SigList
;
901 EFI_SIGNATURE_DATA
*CertData
;
908 ASSERT (VariableName
!= NULL
&& VendorGuid
!= NULL
&& Data
!= NULL
);
910 if (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, EFI_PLATFORM_KEY_NAME
) == 0)){
912 } else if ((CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && StrCmp (VariableName
, EFI_KEY_EXCHANGE_KEY_NAME
) == 0) ||
913 (CompareGuid (VendorGuid
, &gEfiImageSecurityDatabaseGuid
) &&
914 (StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE
) == 0 || StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE1
) == 0))){
921 SigList
= (EFI_SIGNATURE_LIST
*) Data
;
922 SigDataSize
= DataSize
;
926 // Walk throuth the input signature list and check the data format.
927 // If any signature is incorrectly formed, the whole check will fail.
929 while ((SigDataSize
> 0) && (SigDataSize
>= SigList
->SignatureListSize
)) {
930 for (Index
= 0; Index
< (sizeof (mSupportSigItem
) / sizeof (EFI_SIGNATURE_ITEM
)); Index
++ ) {
931 if (CompareGuid (&SigList
->SignatureType
, &mSupportSigItem
[Index
].SigType
)) {
933 // The value of SignatureSize should always be 16 (size of SignatureOwner
934 // component) add the data length according to signature type.
936 if (mSupportSigItem
[Index
].SigDataSize
!= ((UINT32
) ~0) &&
937 (SigList
->SignatureSize
- sizeof (EFI_GUID
)) != mSupportSigItem
[Index
].SigDataSize
) {
938 return EFI_INVALID_PARAMETER
;
940 if (mSupportSigItem
[Index
].SigHeaderSize
!= ((UINTN
) ~0) &&
941 SigList
->SignatureHeaderSize
!= mSupportSigItem
[Index
].SigHeaderSize
) {
942 return EFI_INVALID_PARAMETER
;
948 if (Index
== (sizeof (mSupportSigItem
) / sizeof (EFI_SIGNATURE_ITEM
))) {
950 // Undefined signature type.
952 return EFI_INVALID_PARAMETER
;
955 if (CompareGuid (&SigList
->SignatureType
, &gEfiCertX509Guid
)) {
957 // Try to retrieve the RSA public key from the X.509 certificate.
958 // If this operation fails, it's not a valid certificate.
960 RsaContext
= RsaNew ();
961 if (RsaContext
== NULL
) {
962 return EFI_INVALID_PARAMETER
;
964 CertData
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) SigList
+ sizeof (EFI_SIGNATURE_LIST
) + SigList
->SignatureHeaderSize
);
965 CertLen
= SigList
->SignatureSize
- sizeof (EFI_GUID
);
966 if (!RsaGetPublicKeyFromX509 (CertData
->SignatureData
, CertLen
, &RsaContext
)) {
967 RsaFree (RsaContext
);
968 return EFI_INVALID_PARAMETER
;
970 RsaFree (RsaContext
);
973 if ((SigList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - SigList
->SignatureHeaderSize
) % SigList
->SignatureSize
!= 0) {
974 return EFI_INVALID_PARAMETER
;
976 SigCount
+= (SigList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - SigList
->SignatureHeaderSize
) / SigList
->SignatureSize
;
978 SigDataSize
-= SigList
->SignatureListSize
;
979 SigList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) SigList
+ SigList
->SignatureListSize
);
982 if (((UINTN
) SigList
- (UINTN
) Data
) != DataSize
) {
983 return EFI_INVALID_PARAMETER
;
986 if (IsPk
&& SigCount
> 1) {
987 return EFI_INVALID_PARAMETER
;
994 Update "VendorKeys" variable to record the out of band secure boot key modification.
996 @return EFI_SUCCESS Variable is updated successfully.
997 @return Others Failed to update variable.
1001 VendorKeyIsModified (
1006 VARIABLE_POINTER_TRACK Variable
;
1008 if (mVendorKeyState
== VENDOR_KEYS_MODIFIED
) {
1011 mVendorKeyState
= VENDOR_KEYS_MODIFIED
;
1013 FindVariable (EFI_VENDOR_KEYS_NV_VARIABLE_NAME
, &gEfiVendorKeysNvGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1014 Status
= UpdateVariable (
1015 EFI_VENDOR_KEYS_NV_VARIABLE_NAME
,
1016 &gEfiVendorKeysNvGuid
,
1019 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
,
1025 if (EFI_ERROR (Status
)) {
1029 FindVariable (EFI_VENDOR_KEYS_VARIABLE_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1030 return UpdateVariable (
1031 EFI_VENDOR_KEYS_VARIABLE_NAME
,
1032 &gEfiGlobalVariableGuid
,
1035 EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
1044 Process variable with platform key for verification.
1046 Caution: This function may receive untrusted input.
1047 This function may be invoked in SMM mode, and datasize and data are external input.
1048 This function will do basic validation, before parse the data.
1049 This function will parse the authentication carefully to avoid security issues, like
1050 buffer overflow, integer overflow.
1051 This function will check attribute carefully to avoid authentication bypass.
1053 @param[in] VariableName Name of Variable to be found.
1054 @param[in] VendorGuid Variable vendor GUID.
1055 @param[in] Data Data pointer.
1056 @param[in] DataSize Size of Data found. If size is less than the
1057 data, this value contains the required size.
1058 @param[in] Variable The variable information which is used to keep track of variable usage.
1059 @param[in] Attributes Attribute value of the variable
1060 @param[in] IsPk Indicate whether it is to process pk.
1062 @return EFI_INVALID_PARAMETER Invalid parameter.
1063 @return EFI_SECURITY_VIOLATION The variable does NOT pass the validation.
1064 check carried out by the firmware.
1065 @return EFI_SUCCESS Variable passed validation successfully.
1070 IN CHAR16
*VariableName
,
1071 IN EFI_GUID
*VendorGuid
,
1074 IN VARIABLE_POINTER_TRACK
*Variable
,
1075 IN UINT32 Attributes OPTIONAL
,
1084 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0 ||
1085 (Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) == 0) {
1087 // PK, KEK and db/dbx should set EFI_VARIABLE_NON_VOLATILE attribute and should be a time-based
1088 // authenticated variable.
1090 return EFI_INVALID_PARAMETER
;
1094 if ((InCustomMode() && UserPhysicalPresent()) || (mPlatformMode
== SETUP_MODE
&& !IsPk
)) {
1095 Payload
= (UINT8
*) Data
+ AUTHINFO2_SIZE (Data
);
1096 PayloadSize
= DataSize
- AUTHINFO2_SIZE (Data
);
1097 if (PayloadSize
== 0) {
1101 Status
= CheckSignatureListFormat(VariableName
, VendorGuid
, Payload
, PayloadSize
);
1102 if (EFI_ERROR (Status
)) {
1106 Status
= UpdateVariable (
1115 &((EFI_VARIABLE_AUTHENTICATION_2
*) Data
)->TimeStamp
1117 if (EFI_ERROR(Status
)) {
1121 if ((mPlatformMode
!= SETUP_MODE
) || IsPk
) {
1122 Status
= VendorKeyIsModified ();
1124 } else if (mPlatformMode
== USER_MODE
) {
1126 // Verify against X509 Cert in PK database.
1128 Status
= VerifyTimeBasedPayload (
1140 // Verify against the certificate in data payload.
1142 Status
= VerifyTimeBasedPayload (
1154 if (!EFI_ERROR(Status
) && IsPk
) {
1155 if (mPlatformMode
== SETUP_MODE
&& !Del
) {
1157 // If enroll PK in setup mode, need change to user mode.
1159 Status
= UpdatePlatformMode (USER_MODE
);
1160 } else if (mPlatformMode
== USER_MODE
&& Del
){
1162 // If delete PK in user mode, need change to setup mode.
1164 Status
= UpdatePlatformMode (SETUP_MODE
);
1172 Process variable with key exchange key for verification.
1174 Caution: This function may receive untrusted input.
1175 This function may be invoked in SMM mode, and datasize and data are external input.
1176 This function will do basic validation, before parse the data.
1177 This function will parse the authentication carefully to avoid security issues, like
1178 buffer overflow, integer overflow.
1179 This function will check attribute carefully to avoid authentication bypass.
1181 @param[in] VariableName Name of Variable to be found.
1182 @param[in] VendorGuid Variable vendor GUID.
1183 @param[in] Data Data pointer.
1184 @param[in] DataSize Size of Data found. If size is less than the
1185 data, this value contains the required size.
1186 @param[in] Variable The variable information which is used to keep track of variable usage.
1187 @param[in] Attributes Attribute value of the variable.
1189 @return EFI_INVALID_PARAMETER Invalid parameter.
1190 @return EFI_SECURITY_VIOLATION The variable does NOT pass the validation
1191 check carried out by the firmware.
1192 @return EFI_SUCCESS Variable pass validation successfully.
1197 IN CHAR16
*VariableName
,
1198 IN EFI_GUID
*VendorGuid
,
1201 IN VARIABLE_POINTER_TRACK
*Variable
,
1202 IN UINT32 Attributes OPTIONAL
1209 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0 ||
1210 (Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) == 0) {
1212 // DB and DBX should set EFI_VARIABLE_NON_VOLATILE attribute and should be a time-based
1213 // authenticated variable.
1215 return EFI_INVALID_PARAMETER
;
1218 Status
= EFI_SUCCESS
;
1219 if (mPlatformMode
== USER_MODE
&& !(InCustomMode() && UserPhysicalPresent())) {
1221 // Time-based, verify against X509 Cert KEK.
1223 return VerifyTimeBasedPayload (
1235 // If in setup mode or custom secure boot mode, no authentication needed.
1237 Payload
= (UINT8
*) Data
+ AUTHINFO2_SIZE (Data
);
1238 PayloadSize
= DataSize
- AUTHINFO2_SIZE (Data
);
1240 Status
= CheckSignatureListFormat(VariableName
, VendorGuid
, Payload
, PayloadSize
);
1241 if (EFI_ERROR (Status
)) {
1245 Status
= UpdateVariable (
1254 &((EFI_VARIABLE_AUTHENTICATION_2
*) Data
)->TimeStamp
1256 if (EFI_ERROR (Status
)) {
1260 if (mPlatformMode
!= SETUP_MODE
) {
1261 Status
= VendorKeyIsModified ();
1269 Process variable with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS/EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set
1271 Caution: This function may receive untrusted input.
1272 This function may be invoked in SMM mode, and datasize and data are external input.
1273 This function will do basic validation, before parse the data.
1274 This function will parse the authentication carefully to avoid security issues, like
1275 buffer overflow, integer overflow.
1276 This function will check attribute carefully to avoid authentication bypass.
1278 @param[in] VariableName Name of Variable to be found.
1279 @param[in] VendorGuid Variable vendor GUID.
1281 @param[in] Data Data pointer.
1282 @param[in] DataSize Size of Data found. If size is less than the
1283 data, this value contains the required size.
1284 @param[in] Variable The variable information which is used to keep track of variable usage.
1285 @param[in] Attributes Attribute value of the variable.
1287 @return EFI_INVALID_PARAMETER Invalid parameter.
1288 @return EFI_WRITE_PROTECTED Variable is write-protected and needs authentication with
1289 EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS set.
1290 @return EFI_OUT_OF_RESOURCES The Database to save the public key is full.
1291 @return EFI_SECURITY_VIOLATION The variable is with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
1292 set, but the AuthInfo does NOT pass the validation
1293 check carried out by the firmware.
1294 @return EFI_SUCCESS Variable is not write-protected or pass validation successfully.
1299 IN CHAR16
*VariableName
,
1300 IN EFI_GUID
*VendorGuid
,
1303 IN VARIABLE_POINTER_TRACK
*Variable
,
1304 IN UINT32 Attributes
1309 BOOLEAN IsFirstTime
;
1311 EFI_VARIABLE_AUTHENTICATION
*CertData
;
1312 EFI_CERT_BLOCK_RSA_2048_SHA256
*CertBlock
;
1314 UINT64 MonotonicCount
;
1315 VARIABLE_ENTRY_CONSISTENCY VariableDataEntry
;
1323 if (NeedPhysicallyPresent(VariableName
, VendorGuid
) && !UserPhysicalPresent()) {
1325 // This variable is protected, only physical present user could modify its value.
1327 return EFI_SECURITY_VIOLATION
;
1331 // A time-based authenticated variable and a count-based authenticated variable
1332 // can't be updated by each other.
1334 if (Variable
->CurrPtr
!= NULL
) {
1335 if (((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) &&
1336 ((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0)) {
1337 return EFI_SECURITY_VIOLATION
;
1340 if (((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) &&
1341 ((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0)) {
1342 return EFI_SECURITY_VIOLATION
;
1347 // Process Time-based Authenticated variable.
1349 if ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) {
1350 return VerifyTimeBasedPayload (
1363 // Determine if first time SetVariable with the EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS.
1365 if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) {
1367 // Determine current operation type.
1369 if (DataSize
== AUTHINFO_SIZE
) {
1373 // Determine whether this is the first time with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS set.
1375 if (Variable
->CurrPtr
== NULL
) {
1377 } else if ((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) == 0) {
1380 KeyIndex
= Variable
->CurrPtr
->PubKeyIndex
;
1381 IsFirstTime
= FALSE
;
1383 } else if ((Variable
->CurrPtr
!= NULL
) &&
1384 ((Variable
->CurrPtr
->Attributes
& (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
)) != 0)
1387 // If the variable is already write-protected, it always needs authentication before update.
1389 return EFI_WRITE_PROTECTED
;
1392 // If without EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS, set and attributes collision.
1393 // That means it is not authenticated variable, just update variable as usual.
1395 Status
= UpdateVariable (VariableName
, VendorGuid
, Data
, DataSize
, Attributes
, 0, 0, Variable
, NULL
);
1400 // Get PubKey and check Monotonic Count value corresponding to the variable.
1402 CertData
= (EFI_VARIABLE_AUTHENTICATION
*) Data
;
1403 CertBlock
= (EFI_CERT_BLOCK_RSA_2048_SHA256
*) (CertData
->AuthInfo
.CertData
);
1404 PubKey
= CertBlock
->PublicKey
;
1407 // Update Monotonic Count value.
1409 MonotonicCount
= CertData
->MonotonicCount
;
1413 // 2 cases need to check here
1414 // 1. Internal PubKey variable. PubKeyIndex is always 0
1415 // 2. Other counter-based AuthVariable. Check input PubKey.
1417 if (KeyIndex
== 0 || CompareMem (PubKey
, mPubKeyStore
+ (KeyIndex
- 1) * EFI_CERT_TYPE_RSA2048_SIZE
, EFI_CERT_TYPE_RSA2048_SIZE
) != 0) {
1418 return EFI_SECURITY_VIOLATION
;
1421 // Compare the current monotonic count and ensure that it is greater than the last SetVariable
1422 // operation with the EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS attribute set.
1424 if (CertData
->MonotonicCount
<= Variable
->CurrPtr
->MonotonicCount
) {
1426 // Monotonic count check fail, suspicious replay attack, return EFI_SECURITY_VIOLATION.
1428 return EFI_SECURITY_VIOLATION
;
1432 // Verify the certificate in Data payload.
1434 Status
= VerifyCounterBasedPayload (Data
, DataSize
, PubKey
);
1435 if (EFI_ERROR (Status
)) {
1440 // Now, the signature has been verified!
1442 if (IsFirstTime
&& !IsDeletion
) {
1443 VariableDataEntry
.VariableSize
= DataSize
- AUTHINFO_SIZE
;
1444 VariableDataEntry
.Guid
= VendorGuid
;
1445 VariableDataEntry
.Name
= VariableName
;
1448 // Update public key database variable if need.
1450 KeyIndex
= AddPubKeyInStore (PubKey
, &VariableDataEntry
);
1451 if (KeyIndex
== 0) {
1452 return EFI_OUT_OF_RESOURCES
;
1457 // Verification pass.
1459 return UpdateVariable (VariableName
, VendorGuid
, (UINT8
*)Data
+ AUTHINFO_SIZE
, DataSize
- AUTHINFO_SIZE
, Attributes
, KeyIndex
, MonotonicCount
, Variable
, NULL
);
1463 Merge two buffers which formatted as EFI_SIGNATURE_LIST. Only the new EFI_SIGNATURE_DATA
1464 will be appended to the original EFI_SIGNATURE_LIST, duplicate EFI_SIGNATURE_DATA
1467 @param[in, out] Data Pointer to original EFI_SIGNATURE_LIST.
1468 @param[in] DataSize Size of Data buffer.
1469 @param[in] FreeBufSize Size of free data buffer
1470 @param[in] NewData Pointer to new EFI_SIGNATURE_LIST to be appended.
1471 @param[in] NewDataSize Size of NewData buffer.
1472 @param[out] MergedBufSize Size of the merged buffer
1474 @return EFI_BUFFER_TOO_SMALL if input Data buffer overflowed
1478 AppendSignatureList (
1481 IN UINTN FreeBufSize
,
1483 IN UINTN NewDataSize
,
1484 OUT UINTN
*MergedBufSize
1487 EFI_SIGNATURE_LIST
*CertList
;
1488 EFI_SIGNATURE_DATA
*Cert
;
1490 EFI_SIGNATURE_LIST
*NewCertList
;
1491 EFI_SIGNATURE_DATA
*NewCert
;
1498 UINTN SignatureListSize
;
1501 Tail
= (UINT8
*) Data
+ DataSize
;
1503 NewCertList
= (EFI_SIGNATURE_LIST
*) NewData
;
1504 while ((NewDataSize
> 0) && (NewDataSize
>= NewCertList
->SignatureListSize
)) {
1505 NewCert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) NewCertList
+ sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
);
1506 NewCertCount
= (NewCertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - NewCertList
->SignatureHeaderSize
) / NewCertList
->SignatureSize
;
1509 for (Index
= 0; Index
< NewCertCount
; Index
++) {
1513 CertList
= (EFI_SIGNATURE_LIST
*) Data
;
1514 while ((Size
> 0) && (Size
>= CertList
->SignatureListSize
)) {
1515 if (CompareGuid (&CertList
->SignatureType
, &NewCertList
->SignatureType
) &&
1516 (CertList
->SignatureSize
== NewCertList
->SignatureSize
)) {
1517 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
1518 CertCount
= (CertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - CertList
->SignatureHeaderSize
) / CertList
->SignatureSize
;
1519 for (Index2
= 0; Index2
< CertCount
; Index2
++) {
1521 // Iterate each Signature Data in this Signature List.
1523 if (CompareMem (NewCert
, Cert
, CertList
->SignatureSize
) == 0) {
1527 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) Cert
+ CertList
->SignatureSize
);
1534 Size
-= CertList
->SignatureListSize
;
1535 CertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) CertList
+ CertList
->SignatureListSize
);
1540 // New EFI_SIGNATURE_DATA, append it.
1542 if (CopiedCount
== 0) {
1543 if (FreeBufSize
< sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
) {
1544 return EFI_BUFFER_TOO_SMALL
;
1548 // Copy EFI_SIGNATURE_LIST header for only once.
1551 CopyMem (Tail
, NewCertList
, sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
);
1552 Tail
= Tail
+ sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
;
1553 FreeBufSize
-= sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
;
1556 if (FreeBufSize
< NewCertList
->SignatureSize
) {
1557 return EFI_BUFFER_TOO_SMALL
;
1559 CopyMem (Tail
, NewCert
, NewCertList
->SignatureSize
);
1560 Tail
+= NewCertList
->SignatureSize
;
1561 FreeBufSize
-= NewCertList
->SignatureSize
;
1565 NewCert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) NewCert
+ NewCertList
->SignatureSize
);
1569 // Update SignatureListSize in newly appended EFI_SIGNATURE_LIST.
1571 if (CopiedCount
!= 0) {
1572 SignatureListSize
= sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
+ (CopiedCount
* NewCertList
->SignatureSize
);
1573 CertList
= (EFI_SIGNATURE_LIST
*) (Tail
- SignatureListSize
);
1574 CertList
->SignatureListSize
= (UINT32
) SignatureListSize
;
1577 NewDataSize
-= NewCertList
->SignatureListSize
;
1578 NewCertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) NewCertList
+ NewCertList
->SignatureListSize
);
1581 *MergedBufSize
= (Tail
- (UINT8
*) Data
);
1586 Compare two EFI_TIME data.
1589 @param FirstTime A pointer to the first EFI_TIME data.
1590 @param SecondTime A pointer to the second EFI_TIME data.
1592 @retval TRUE The FirstTime is not later than the SecondTime.
1593 @retval FALSE The FirstTime is later than the SecondTime.
1598 IN EFI_TIME
*FirstTime
,
1599 IN EFI_TIME
*SecondTime
1602 if (FirstTime
->Year
!= SecondTime
->Year
) {
1603 return (BOOLEAN
) (FirstTime
->Year
< SecondTime
->Year
);
1604 } else if (FirstTime
->Month
!= SecondTime
->Month
) {
1605 return (BOOLEAN
) (FirstTime
->Month
< SecondTime
->Month
);
1606 } else if (FirstTime
->Day
!= SecondTime
->Day
) {
1607 return (BOOLEAN
) (FirstTime
->Day
< SecondTime
->Day
);
1608 } else if (FirstTime
->Hour
!= SecondTime
->Hour
) {
1609 return (BOOLEAN
) (FirstTime
->Hour
< SecondTime
->Hour
);
1610 } else if (FirstTime
->Minute
!= SecondTime
->Minute
) {
1611 return (BOOLEAN
) (FirstTime
->Minute
< SecondTime
->Minute
);
1614 return (BOOLEAN
) (FirstTime
->Second
<= SecondTime
->Second
);
1618 Find matching signer's certificates for common authenticated variable
1619 by corresponding VariableName and VendorGuid from "certdb".
1621 The data format of "certdb":
1623 // UINT32 CertDbListSize;
1624 // /// AUTH_CERT_DB_DATA Certs1[];
1625 // /// AUTH_CERT_DB_DATA Certs2[];
1627 // /// AUTH_CERT_DB_DATA Certsn[];
1630 @param[in] VariableName Name of authenticated Variable.
1631 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1632 @param[in] Data Pointer to variable "certdb".
1633 @param[in] DataSize Size of variable "certdb".
1634 @param[out] CertOffset Offset of matching CertData, from starting of Data.
1635 @param[out] CertDataSize Length of CertData in bytes.
1636 @param[out] CertNodeOffset Offset of matching AUTH_CERT_DB_DATA , from
1638 @param[out] CertNodeSize Length of AUTH_CERT_DB_DATA in bytes.
1640 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1641 @retval EFI_NOT_FOUND Fail to find matching certs.
1642 @retval EFI_SUCCESS Find matching certs and output parameters.
1647 IN CHAR16
*VariableName
,
1648 IN EFI_GUID
*VendorGuid
,
1651 OUT UINT32
*CertOffset
, OPTIONAL
1652 OUT UINT32
*CertDataSize
, OPTIONAL
1653 OUT UINT32
*CertNodeOffset
,OPTIONAL
1654 OUT UINT32
*CertNodeSize OPTIONAL
1658 AUTH_CERT_DB_DATA
*Ptr
;
1662 UINT32 CertDbListSize
;
1664 if ((VariableName
== NULL
) || (VendorGuid
== NULL
) || (Data
== NULL
)) {
1665 return EFI_INVALID_PARAMETER
;
1669 // Check whether DataSize matches recorded CertDbListSize.
1671 if (DataSize
< sizeof (UINT32
)) {
1672 return EFI_INVALID_PARAMETER
;
1675 CertDbListSize
= ReadUnaligned32 ((UINT32
*) Data
);
1677 if (CertDbListSize
!= (UINT32
) DataSize
) {
1678 return EFI_INVALID_PARAMETER
;
1681 Offset
= sizeof (UINT32
);
1684 // Get corresponding certificates by VendorGuid and VariableName.
1686 while (Offset
< (UINT32
) DataSize
) {
1687 Ptr
= (AUTH_CERT_DB_DATA
*) (Data
+ Offset
);
1689 // Check whether VendorGuid matches.
1691 if (CompareGuid (&Ptr
->VendorGuid
, VendorGuid
)) {
1692 NodeSize
= ReadUnaligned32 (&Ptr
->CertNodeSize
);
1693 NameSize
= ReadUnaligned32 (&Ptr
->NameSize
);
1694 CertSize
= ReadUnaligned32 (&Ptr
->CertDataSize
);
1696 if (NodeSize
!= sizeof (EFI_GUID
) + sizeof (UINT32
) * 3 + CertSize
+
1697 sizeof (CHAR16
) * NameSize
) {
1698 return EFI_INVALID_PARAMETER
;
1701 Offset
= Offset
+ sizeof (EFI_GUID
) + sizeof (UINT32
) * 3;
1703 // Check whether VariableName matches.
1705 if ((NameSize
== StrLen (VariableName
)) &&
1706 (CompareMem (Data
+ Offset
, VariableName
, NameSize
* sizeof (CHAR16
)) == 0)) {
1707 Offset
= Offset
+ NameSize
* sizeof (CHAR16
);
1709 if (CertOffset
!= NULL
) {
1710 *CertOffset
= Offset
;
1713 if (CertDataSize
!= NULL
) {
1714 *CertDataSize
= CertSize
;
1717 if (CertNodeOffset
!= NULL
) {
1718 *CertNodeOffset
= (UINT32
) ((UINT8
*) Ptr
- Data
);
1721 if (CertNodeSize
!= NULL
) {
1722 *CertNodeSize
= NodeSize
;
1727 Offset
= Offset
+ NameSize
* sizeof (CHAR16
) + CertSize
;
1730 NodeSize
= ReadUnaligned32 (&Ptr
->CertNodeSize
);
1731 Offset
= Offset
+ NodeSize
;
1735 return EFI_NOT_FOUND
;
1739 Retrieve signer's certificates for common authenticated variable
1740 by corresponding VariableName and VendorGuid from "certdb".
1742 @param[in] VariableName Name of authenticated Variable.
1743 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1744 @param[out] CertData Pointer to signer's certificates.
1745 @param[out] CertDataSize Length of CertData in bytes.
1747 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1748 @retval EFI_NOT_FOUND Fail to find "certdb" or matching certs.
1749 @retval EFI_SUCCESS Get signer's certificates successfully.
1754 IN CHAR16
*VariableName
,
1755 IN EFI_GUID
*VendorGuid
,
1756 OUT UINT8
**CertData
,
1757 OUT UINT32
*CertDataSize
1760 VARIABLE_POINTER_TRACK CertDbVariable
;
1766 if ((VariableName
== NULL
) || (VendorGuid
== NULL
) || (CertData
== NULL
) || (CertDataSize
== NULL
)) {
1767 return EFI_INVALID_PARAMETER
;
1771 // Get variable "certdb".
1773 Status
= FindVariable (
1777 &mVariableModuleGlobal
->VariableGlobal
,
1780 if (EFI_ERROR (Status
)) {
1784 DataSize
= DataSizeOfVariable (CertDbVariable
.CurrPtr
);
1785 Data
= GetVariableDataPtr (CertDbVariable
.CurrPtr
);
1786 if ((DataSize
== 0) || (Data
== NULL
)) {
1788 return EFI_NOT_FOUND
;
1791 Status
= FindCertsFromDb (
1802 if (EFI_ERROR (Status
)) {
1806 *CertData
= Data
+ CertOffset
;
1811 Delete matching signer's certificates when deleting common authenticated
1812 variable by corresponding VariableName and VendorGuid from "certdb".
1814 @param[in] VariableName Name of authenticated Variable.
1815 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1817 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1818 @retval EFI_NOT_FOUND Fail to find "certdb" or matching certs.
1819 @retval EFI_OUT_OF_RESOURCES The operation is failed due to lack of resources.
1820 @retval EFI_SUCCESS The operation is completed successfully.
1825 IN CHAR16
*VariableName
,
1826 IN EFI_GUID
*VendorGuid
1829 VARIABLE_POINTER_TRACK CertDbVariable
;
1834 UINT32 CertNodeOffset
;
1835 UINT32 CertNodeSize
;
1837 UINT32 NewCertDbSize
;
1839 if ((VariableName
== NULL
) || (VendorGuid
== NULL
)) {
1840 return EFI_INVALID_PARAMETER
;
1844 // Get variable "certdb".
1846 Status
= FindVariable (
1850 &mVariableModuleGlobal
->VariableGlobal
,
1853 if (EFI_ERROR (Status
)) {
1857 DataSize
= DataSizeOfVariable (CertDbVariable
.CurrPtr
);
1858 Data
= GetVariableDataPtr (CertDbVariable
.CurrPtr
);
1859 if ((DataSize
== 0) || (Data
== NULL
)) {
1861 return EFI_NOT_FOUND
;
1864 if (DataSize
== sizeof (UINT32
)) {
1866 // There is no certs in certdb.
1872 // Get corresponding cert node from certdb.
1874 Status
= FindCertsFromDb (
1885 if (EFI_ERROR (Status
)) {
1889 if (DataSize
< (CertNodeOffset
+ CertNodeSize
)) {
1890 return EFI_NOT_FOUND
;
1894 // Construct new data content of variable "certdb".
1896 NewCertDbSize
= (UINT32
) DataSize
- CertNodeSize
;
1897 NewCertDb
= (UINT8
*) mCertDbStore
;
1900 // Copy the DB entries before deleting node.
1902 CopyMem (NewCertDb
, Data
, CertNodeOffset
);
1904 // Update CertDbListSize.
1906 CopyMem (NewCertDb
, &NewCertDbSize
, sizeof (UINT32
));
1908 // Copy the DB entries after deleting node.
1910 if (DataSize
> (CertNodeOffset
+ CertNodeSize
)) {
1912 NewCertDb
+ CertNodeOffset
,
1913 Data
+ CertNodeOffset
+ CertNodeSize
,
1914 DataSize
- CertNodeOffset
- CertNodeSize
1921 VarAttr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
1922 Status
= UpdateVariable (
1938 Insert signer's certificates for common authenticated variable with VariableName
1939 and VendorGuid in AUTH_CERT_DB_DATA to "certdb".
1941 @param[in] VariableName Name of authenticated Variable.
1942 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1943 @param[in] CertData Pointer to signer's certificates.
1944 @param[in] CertDataSize Length of CertData in bytes.
1946 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1947 @retval EFI_ACCESS_DENIED An AUTH_CERT_DB_DATA entry with same VariableName
1948 and VendorGuid already exists.
1949 @retval EFI_OUT_OF_RESOURCES The operation is failed due to lack of resources.
1950 @retval EFI_SUCCESS Insert an AUTH_CERT_DB_DATA entry to "certdb"
1955 IN CHAR16
*VariableName
,
1956 IN EFI_GUID
*VendorGuid
,
1958 IN UINTN CertDataSize
1961 VARIABLE_POINTER_TRACK CertDbVariable
;
1967 UINT32 NewCertDbSize
;
1968 UINT32 CertNodeSize
;
1970 AUTH_CERT_DB_DATA
*Ptr
;
1972 if ((VariableName
== NULL
) || (VendorGuid
== NULL
) || (CertData
== NULL
)) {
1973 return EFI_INVALID_PARAMETER
;
1977 // Get variable "certdb".
1979 Status
= FindVariable (
1983 &mVariableModuleGlobal
->VariableGlobal
,
1986 if (EFI_ERROR (Status
)) {
1990 DataSize
= DataSizeOfVariable (CertDbVariable
.CurrPtr
);
1991 Data
= GetVariableDataPtr (CertDbVariable
.CurrPtr
);
1992 if ((DataSize
== 0) || (Data
== NULL
)) {
1994 return EFI_NOT_FOUND
;
1998 // Find whether matching cert node already exists in "certdb".
1999 // If yes return error.
2001 Status
= FindCertsFromDb (
2012 if (!EFI_ERROR (Status
)) {
2014 return EFI_ACCESS_DENIED
;
2018 // Construct new data content of variable "certdb".
2020 NameSize
= (UINT32
) StrLen (VariableName
);
2021 CertNodeSize
= sizeof (AUTH_CERT_DB_DATA
) + (UINT32
) CertDataSize
+ NameSize
* sizeof (CHAR16
);
2022 NewCertDbSize
= (UINT32
) DataSize
+ CertNodeSize
;
2023 if (NewCertDbSize
> mMaxCertDbSize
) {
2024 return EFI_OUT_OF_RESOURCES
;
2026 NewCertDb
= (UINT8
*) mCertDbStore
;
2029 // Copy the DB entries before deleting node.
2031 CopyMem (NewCertDb
, Data
, DataSize
);
2033 // Update CertDbListSize.
2035 CopyMem (NewCertDb
, &NewCertDbSize
, sizeof (UINT32
));
2037 // Construct new cert node.
2039 Ptr
= (AUTH_CERT_DB_DATA
*) (NewCertDb
+ DataSize
);
2040 CopyGuid (&Ptr
->VendorGuid
, VendorGuid
);
2041 CopyMem (&Ptr
->CertNodeSize
, &CertNodeSize
, sizeof (UINT32
));
2042 CopyMem (&Ptr
->NameSize
, &NameSize
, sizeof (UINT32
));
2043 CopyMem (&Ptr
->CertDataSize
, &CertDataSize
, sizeof (UINT32
));
2046 (UINT8
*) Ptr
+ sizeof (AUTH_CERT_DB_DATA
),
2048 NameSize
* sizeof (CHAR16
)
2052 (UINT8
*) Ptr
+ sizeof (AUTH_CERT_DB_DATA
) + NameSize
* sizeof (CHAR16
),
2060 VarAttr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
2061 Status
= UpdateVariable (
2077 Process variable with EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set
2079 Caution: This function may receive untrusted input.
2080 This function may be invoked in SMM mode, and datasize and data are external input.
2081 This function will do basic validation, before parse the data.
2082 This function will parse the authentication carefully to avoid security issues, like
2083 buffer overflow, integer overflow.
2085 @param[in] VariableName Name of Variable to be found.
2086 @param[in] VendorGuid Variable vendor GUID.
2087 @param[in] Data Data pointer.
2088 @param[in] DataSize Size of Data found. If size is less than the
2089 data, this value contains the required size.
2090 @param[in] Variable The variable information which is used to keep track of variable usage.
2091 @param[in] Attributes Attribute value of the variable.
2092 @param[in] AuthVarType Verify against PK, KEK database, private database or certificate in data payload.
2093 @param[out] VarDel Delete the variable or not.
2095 @retval EFI_INVALID_PARAMETER Invalid parameter.
2096 @retval EFI_SECURITY_VIOLATION The variable does NOT pass the validation
2097 check carried out by the firmware.
2098 @retval EFI_OUT_OF_RESOURCES Failed to process variable due to lack
2100 @retval EFI_SUCCESS Variable pass validation successfully.
2104 VerifyTimeBasedPayload (
2105 IN CHAR16
*VariableName
,
2106 IN EFI_GUID
*VendorGuid
,
2109 IN VARIABLE_POINTER_TRACK
*Variable
,
2110 IN UINT32 Attributes
,
2111 IN AUTHVAR_TYPE AuthVarType
,
2125 BOOLEAN VerifyStatus
;
2127 EFI_SIGNATURE_LIST
*CertList
;
2128 EFI_SIGNATURE_DATA
*Cert
;
2129 VARIABLE_POINTER_TRACK KekVariable
;
2130 EFI_VARIABLE_AUTHENTICATION_2
*CertData
;
2133 VARIABLE_POINTER_TRACK PkVariable
;
2138 UINTN CertStackSize
;
2139 UINT8
*CertsInCertDb
;
2140 UINT32 CertsSizeinDb
;
2142 VerifyStatus
= FALSE
;
2149 CertsInCertDb
= NULL
;
2152 // When the attribute EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS is
2153 // set, then the Data buffer shall begin with an instance of a complete (and serialized)
2154 // EFI_VARIABLE_AUTHENTICATION_2 descriptor. The descriptor shall be followed by the new
2155 // variable value and DataSize shall reflect the combined size of the descriptor and the new
2156 // variable value. The authentication descriptor is not part of the variable data and is not
2157 // returned by subsequent calls to GetVariable().
2159 CertData
= (EFI_VARIABLE_AUTHENTICATION_2
*) Data
;
2162 // Verify that Pad1, Nanosecond, TimeZone, Daylight and Pad2 components of the
2163 // TimeStamp value are set to zero.
2165 if ((CertData
->TimeStamp
.Pad1
!= 0) ||
2166 (CertData
->TimeStamp
.Nanosecond
!= 0) ||
2167 (CertData
->TimeStamp
.TimeZone
!= 0) ||
2168 (CertData
->TimeStamp
.Daylight
!= 0) ||
2169 (CertData
->TimeStamp
.Pad2
!= 0)) {
2170 return EFI_SECURITY_VIOLATION
;
2173 if ((Variable
->CurrPtr
!= NULL
) && ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0)) {
2174 if (CompareTimeStamp (&CertData
->TimeStamp
, &Variable
->CurrPtr
->TimeStamp
)) {
2176 // TimeStamp check fail, suspicious replay attack, return EFI_SECURITY_VIOLATION.
2178 return EFI_SECURITY_VIOLATION
;
2183 // wCertificateType should be WIN_CERT_TYPE_EFI_GUID.
2184 // Cert type should be EFI_CERT_TYPE_PKCS7_GUID.
2186 if ((CertData
->AuthInfo
.Hdr
.wCertificateType
!= WIN_CERT_TYPE_EFI_GUID
) ||
2187 !CompareGuid (&CertData
->AuthInfo
.CertType
, &gEfiCertPkcs7Guid
)) {
2189 // Invalid AuthInfo type, return EFI_SECURITY_VIOLATION.
2191 return EFI_SECURITY_VIOLATION
;
2195 // Find out Pkcs7 SignedData which follows the EFI_VARIABLE_AUTHENTICATION_2 descriptor.
2196 // AuthInfo.Hdr.dwLength is the length of the entire certificate, including the length of the header.
2198 SigData
= CertData
->AuthInfo
.CertData
;
2199 SigDataSize
= CertData
->AuthInfo
.Hdr
.dwLength
- (UINT32
) (OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID
, CertData
));
2202 // Find out the new data payload which follows Pkcs7 SignedData directly.
2204 PayloadPtr
= SigData
+ SigDataSize
;
2205 PayloadSize
= DataSize
- OFFSET_OF_AUTHINFO2_CERT_DATA
- (UINTN
) SigDataSize
;
2208 // Construct a buffer to fill with (VariableName, VendorGuid, Attributes, TimeStamp, Data).
2210 NewDataSize
= PayloadSize
+ sizeof (EFI_TIME
) + sizeof (UINT32
) +
2211 sizeof (EFI_GUID
) + StrSize (VariableName
) - sizeof (CHAR16
);
2212 NewData
= mSerializationRuntimeBuffer
;
2215 Length
= StrLen (VariableName
) * sizeof (CHAR16
);
2216 CopyMem (Buffer
, VariableName
, Length
);
2219 Length
= sizeof (EFI_GUID
);
2220 CopyMem (Buffer
, VendorGuid
, Length
);
2223 Length
= sizeof (UINT32
);
2224 CopyMem (Buffer
, &Attr
, Length
);
2227 Length
= sizeof (EFI_TIME
);
2228 CopyMem (Buffer
, &CertData
->TimeStamp
, Length
);
2231 CopyMem (Buffer
, PayloadPtr
, PayloadSize
);
2233 if (AuthVarType
== AuthVarTypePk
) {
2235 // Verify that the signature has been made with the current Platform Key (no chaining for PK).
2236 // First, get signer's certificates from SignedData.
2238 VerifyStatus
= Pkcs7GetSigners (
2246 if (!VerifyStatus
) {
2251 // Second, get the current platform key from variable. Check whether it's identical with signer's certificates
2252 // in SignedData. If not, return error immediately.
2254 Status
= FindVariable (
2255 EFI_PLATFORM_KEY_NAME
,
2256 &gEfiGlobalVariableGuid
,
2258 &mVariableModuleGlobal
->VariableGlobal
,
2261 if (EFI_ERROR (Status
)) {
2262 VerifyStatus
= FALSE
;
2265 CertList
= (EFI_SIGNATURE_LIST
*) GetVariableDataPtr (PkVariable
.CurrPtr
);
2266 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
2267 if ((RootCertSize
!= (CertList
->SignatureSize
- (sizeof (EFI_SIGNATURE_DATA
) - 1))) ||
2268 (CompareMem (Cert
->SignatureData
, RootCert
, RootCertSize
) != 0)) {
2269 VerifyStatus
= FALSE
;
2274 // Verify Pkcs7 SignedData via Pkcs7Verify library.
2276 VerifyStatus
= Pkcs7Verify (
2285 } else if (AuthVarType
== AuthVarTypeKek
) {
2288 // Get KEK database from variable.
2290 Status
= FindVariable (
2291 EFI_KEY_EXCHANGE_KEY_NAME
,
2292 &gEfiGlobalVariableGuid
,
2294 &mVariableModuleGlobal
->VariableGlobal
,
2297 if (EFI_ERROR (Status
)) {
2302 // Ready to verify Pkcs7 SignedData. Go through KEK Signature Database to find out X.509 CertList.
2304 KekDataSize
= KekVariable
.CurrPtr
->DataSize
;
2305 CertList
= (EFI_SIGNATURE_LIST
*) GetVariableDataPtr (KekVariable
.CurrPtr
);
2306 while ((KekDataSize
> 0) && (KekDataSize
>= CertList
->SignatureListSize
)) {
2307 if (CompareGuid (&CertList
->SignatureType
, &gEfiCertX509Guid
)) {
2308 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
2309 CertCount
= (CertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - CertList
->SignatureHeaderSize
) / CertList
->SignatureSize
;
2310 for (Index
= 0; Index
< CertCount
; Index
++) {
2312 // Iterate each Signature Data Node within this CertList for a verify
2314 RootCert
= Cert
->SignatureData
;
2315 RootCertSize
= CertList
->SignatureSize
- (sizeof (EFI_SIGNATURE_DATA
) - 1);
2318 // Verify Pkcs7 SignedData via Pkcs7Verify library.
2320 VerifyStatus
= Pkcs7Verify (
2331 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) Cert
+ CertList
->SignatureSize
);
2334 KekDataSize
-= CertList
->SignatureListSize
;
2335 CertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) CertList
+ CertList
->SignatureListSize
);
2337 } else if (AuthVarType
== AuthVarTypePriv
) {
2340 // Process common authenticated variable except PK/KEK/DB/DBX.
2341 // Get signer's certificates from SignedData.
2343 VerifyStatus
= Pkcs7GetSigners (
2351 if (!VerifyStatus
) {
2356 // Get previously stored signer's certificates from certdb for existing
2357 // variable. Check whether they are identical with signer's certificates
2358 // in SignedData. If not, return error immediately.
2360 if ((Variable
->CurrPtr
!= NULL
)) {
2361 VerifyStatus
= FALSE
;
2363 Status
= GetCertsFromDb (VariableName
, VendorGuid
, &CertsInCertDb
, &CertsSizeinDb
);
2364 if (EFI_ERROR (Status
)) {
2368 if ((CertStackSize
!= CertsSizeinDb
) ||
2369 (CompareMem (SignerCerts
, CertsInCertDb
, CertsSizeinDb
) != 0)) {
2374 VerifyStatus
= Pkcs7Verify (
2382 if (!VerifyStatus
) {
2387 // Delete signer's certificates when delete the common authenticated variable.
2389 if ((PayloadSize
== 0) && (Variable
->CurrPtr
!= NULL
) && ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0)) {
2390 Status
= DeleteCertsFromDb (VariableName
, VendorGuid
);
2391 if (EFI_ERROR (Status
)) {
2392 VerifyStatus
= FALSE
;
2395 } else if (Variable
->CurrPtr
== NULL
&& PayloadSize
!= 0) {
2397 // Insert signer's certificates when adding a new common authenticated variable.
2399 Status
= InsertCertsToDb (VariableName
, VendorGuid
, SignerCerts
, CertStackSize
);
2400 if (EFI_ERROR (Status
)) {
2401 VerifyStatus
= FALSE
;
2405 } else if (AuthVarType
== AuthVarTypePayload
) {
2406 CertList
= (EFI_SIGNATURE_LIST
*) PayloadPtr
;
2407 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
2408 RootCert
= Cert
->SignatureData
;
2409 RootCertSize
= CertList
->SignatureSize
- (sizeof (EFI_SIGNATURE_DATA
) - 1);
2411 // Verify Pkcs7 SignedData via Pkcs7Verify library.
2413 VerifyStatus
= Pkcs7Verify (
2422 return EFI_SECURITY_VIOLATION
;
2427 if (AuthVarType
== AuthVarTypePk
|| AuthVarType
== AuthVarTypePriv
) {
2428 Pkcs7FreeSigners (RootCert
);
2429 Pkcs7FreeSigners (SignerCerts
);
2432 if (!VerifyStatus
) {
2433 return EFI_SECURITY_VIOLATION
;
2436 Status
= CheckSignatureListFormat(VariableName
, VendorGuid
, PayloadPtr
, PayloadSize
);
2437 if (EFI_ERROR (Status
)) {
2441 if ((PayloadSize
== 0) && (VarDel
!= NULL
)) {
2446 // Final step: Update/Append Variable if it pass Pkcs7Verify
2448 return UpdateVariable (
2457 &CertData
->TimeStamp