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 - 2012, 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 EFI_GUID mSignatureSupport
[] = {EFI_CERT_SHA1_GUID
, EFI_CERT_SHA256_GUID
, EFI_CERT_RSA2048_GUID
, EFI_CERT_X509_GUID
};
41 // Public Exponent of RSA Key.
43 CONST UINT8 mRsaE
[] = { 0x01, 0x00, 0x01 };
45 // Hash context pointer
47 VOID
*mHashCtx
= NULL
;
50 // Pointer to runtime buffer.
51 // For "Append" operation to an existing variable, a read/modify/write operation
52 // is supported by firmware internally. Reserve runtime buffer to cache previous
53 // variable data in runtime phase because memory allocation is forbidden in virtual mode.
55 VOID
*mStorageArea
= NULL
;
58 // The serialization of the values of the VariableName, VendorGuid and Attributes
59 // parameters of the SetVariable() call and the TimeStamp component of the
60 // EFI_VARIABLE_AUTHENTICATION_2 descriptor followed by the variable's new value
61 // i.e. (VariableName, VendorGuid, Attributes, TimeStamp, Data)
63 UINT8
*mSerializationRuntimeBuffer
= NULL
;
66 // Requirement for different signature type which have been defined in UEFI spec.
67 // These data are used to peform SignatureList format check while setting PK/KEK variable.
69 EFI_SIGNATURE_ITEM mSupportSigItem
[] = {
70 //{SigType, SigHeaderSize, SigDataSize }
71 {EFI_CERT_SHA256_GUID
, 0, 32 },
72 {EFI_CERT_RSA2048_GUID
, 0, 256 },
73 {EFI_CERT_RSA2048_SHA256_GUID
, 0, 256 },
74 {EFI_CERT_SHA1_GUID
, 0, 20 },
75 {EFI_CERT_RSA2048_SHA1_GUID
, 0, 256 },
76 {EFI_CERT_X509_GUID
, 0, ((UINT32
) ~0)},
77 {EFI_CERT_SHA224_GUID
, 0, 28 },
78 {EFI_CERT_SHA384_GUID
, 0, 48 },
79 {EFI_CERT_SHA512_GUID
, 0, 64 }
83 Determine whether this operation needs a physical present user.
85 @param[in] VariableName Name of the Variable.
86 @param[in] VendorGuid GUID of the Variable.
88 @retval TRUE This variable is protected, only a physical present user could set this variable.
89 @retval FALSE This variable is not protected.
93 NeedPhysicallyPresent(
94 IN CHAR16
*VariableName
,
95 IN EFI_GUID
*VendorGuid
98 if ((CompareGuid (VendorGuid
, &gEfiSecureBootEnableDisableGuid
) && (StrCmp (VariableName
, EFI_SECURE_BOOT_ENABLE_NAME
) == 0))
99 || (CompareGuid (VendorGuid
, &gEfiCustomModeEnableGuid
) && (StrCmp (VariableName
, EFI_CUSTOM_MODE_NAME
) == 0))) {
107 Determine whether the platform is operating in Custom Secure Boot mode.
109 @retval TRUE The platform is operating in Custom mode.
110 @retval FALSE The platform is operating in Standard mode.
118 VARIABLE_POINTER_TRACK Variable
;
120 FindVariable (EFI_CUSTOM_MODE_NAME
, &gEfiCustomModeEnableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
121 if (Variable
.CurrPtr
!= NULL
&& *(GetVariableDataPtr (Variable
.CurrPtr
)) == CUSTOM_SECURE_BOOT_MODE
) {
130 Internal function to delete a Variable given its name and GUID, no authentication
133 @param[in] VariableName Name of the Variable.
134 @param[in] VendorGuid GUID of the Variable.
136 @retval EFI_SUCCESS Variable deleted successfully.
137 @retval Others The driver failded to start the device.
142 IN CHAR16
*VariableName
,
143 IN EFI_GUID
*VendorGuid
147 VARIABLE_POINTER_TRACK Variable
;
149 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
150 if (EFI_ERROR (Status
)) {
154 ASSERT (Variable
.CurrPtr
!= NULL
);
155 return UpdateVariable (VariableName
, VendorGuid
, NULL
, 0, 0, 0, 0, &Variable
, NULL
);
159 Initializes for authenticated varibale service.
161 @retval EFI_SUCCESS Function successfully executed.
162 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resources.
166 AutenticatedVariableServiceInitialize (
171 VARIABLE_POINTER_TRACK Variable
;
172 VARIABLE_POINTER_TRACK PkVariable
;
178 UINT8 SecureBootMode
;
179 UINT8 SecureBootEnable
;
184 // Initialize hash context.
186 CtxSize
= Sha256GetContextSize ();
187 mHashCtx
= AllocateRuntimePool (CtxSize
);
188 if (mHashCtx
== NULL
) {
189 return EFI_OUT_OF_RESOURCES
;
193 // Reserved runtime buffer for "Append" operation in virtual mode.
195 mStorageArea
= AllocateRuntimePool (PcdGet32 (PcdMaxVariableSize
));
196 if (mStorageArea
== NULL
) {
197 return EFI_OUT_OF_RESOURCES
;
201 // Prepare runtime buffer for serialized data of time-based authenticated
202 // Variable, i.e. (VariableName, VendorGuid, Attributes, TimeStamp, Data).
204 mSerializationRuntimeBuffer
= AllocateRuntimePool (PcdGet32 (PcdMaxVariableSize
) + sizeof (EFI_GUID
) + sizeof (UINT32
) + sizeof (EFI_TIME
));
205 if (mSerializationRuntimeBuffer
== NULL
) {
206 return EFI_OUT_OF_RESOURCES
;
210 // Check "AuthVarKeyDatabase" variable's existence.
211 // If it doesn't exist, create a new one with initial value of 0 and EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS set.
213 Status
= FindVariable (
215 &gEfiAuthenticatedVariableGuid
,
217 &mVariableModuleGlobal
->VariableGlobal
,
221 if (Variable
.CurrPtr
== NULL
) {
222 VarAttr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
;
225 Status
= UpdateVariable (
227 &gEfiAuthenticatedVariableGuid
,
236 if (EFI_ERROR (Status
)) {
241 // Load database in global variable for cache.
243 DataSize
= DataSizeOfVariable (Variable
.CurrPtr
);
244 Data
= GetVariableDataPtr (Variable
.CurrPtr
);
245 ASSERT ((DataSize
!= 0) && (Data
!= NULL
));
246 CopyMem (mPubKeyStore
, (UINT8
*) Data
, DataSize
);
247 mPubKeyNumber
= (UINT32
) (DataSize
/ EFI_CERT_TYPE_RSA2048_SIZE
);
250 FindVariable (EFI_PLATFORM_KEY_NAME
, &gEfiGlobalVariableGuid
, &PkVariable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
251 if (PkVariable
.CurrPtr
== NULL
) {
252 DEBUG ((EFI_D_INFO
, "Variable %s does not exist.\n", EFI_PLATFORM_KEY_NAME
));
254 DEBUG ((EFI_D_INFO
, "Variable %s exists.\n", EFI_PLATFORM_KEY_NAME
));
258 // Create "SetupMode" varable with BS+RT attribute set.
260 FindVariable (EFI_SETUP_MODE_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
261 if (PkVariable
.CurrPtr
== NULL
) {
262 mPlatformMode
= SETUP_MODE
;
264 mPlatformMode
= USER_MODE
;
266 Status
= UpdateVariable (
268 &gEfiGlobalVariableGuid
,
271 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
277 if (EFI_ERROR (Status
)) {
282 // Create "SignatureSupport" varable with BS+RT attribute set.
284 FindVariable (EFI_SIGNATURE_SUPPORT_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
285 Status
= UpdateVariable (
286 EFI_SIGNATURE_SUPPORT_NAME
,
287 &gEfiGlobalVariableGuid
,
289 sizeof(mSignatureSupport
),
290 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
296 if (EFI_ERROR (Status
)) {
301 // If "SecureBootEnable" variable exists, then update "SecureBoot" variable.
302 // If "SecureBootEnable" variable is SECURE_BOOT_ENABLE and in USER_MODE, Set "SecureBoot" variable to SECURE_BOOT_MODE_ENABLE.
303 // If "SecureBootEnable" variable is SECURE_BOOT_DISABLE, Set "SecureBoot" variable to SECURE_BOOT_MODE_DISABLE.
305 SecureBootEnable
= SECURE_BOOT_DISABLE
;
306 FindVariable (EFI_SECURE_BOOT_ENABLE_NAME
, &gEfiSecureBootEnableDisableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
307 if (Variable
.CurrPtr
!= NULL
) {
308 SecureBootEnable
= *(GetVariableDataPtr (Variable
.CurrPtr
));
309 } else if (mPlatformMode
== USER_MODE
) {
311 // "SecureBootEnable" not exist, initialize it in USER_MODE.
313 SecureBootEnable
= SECURE_BOOT_ENABLE
;
314 Status
= UpdateVariable (
315 EFI_SECURE_BOOT_ENABLE_NAME
,
316 &gEfiSecureBootEnableDisableGuid
,
319 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
325 if (EFI_ERROR (Status
)) {
331 // Create "SecureBoot" varable with BS+RT attribute set.
333 if (SecureBootEnable
== SECURE_BOOT_ENABLE
&& mPlatformMode
== USER_MODE
) {
334 SecureBootMode
= SECURE_BOOT_MODE_ENABLE
;
336 SecureBootMode
= SECURE_BOOT_MODE_DISABLE
;
338 FindVariable (EFI_SECURE_BOOT_MODE_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
339 Status
= UpdateVariable (
340 EFI_SECURE_BOOT_MODE_NAME
,
341 &gEfiGlobalVariableGuid
,
344 EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
350 if (EFI_ERROR (Status
)) {
354 DEBUG ((EFI_D_INFO
, "Variable %s is %x\n", EFI_SETUP_MODE_NAME
, mPlatformMode
));
355 DEBUG ((EFI_D_INFO
, "Variable %s is %x\n", EFI_SECURE_BOOT_MODE_NAME
, SecureBootMode
));
356 DEBUG ((EFI_D_INFO
, "Variable %s is %x\n", EFI_SECURE_BOOT_ENABLE_NAME
, SecureBootEnable
));
359 // Check "CustomMode" variable's existence.
361 FindVariable (EFI_CUSTOM_MODE_NAME
, &gEfiCustomModeEnableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
362 if (Variable
.CurrPtr
!= NULL
) {
363 CustomMode
= *(GetVariableDataPtr (Variable
.CurrPtr
));
366 // "CustomMode" not exist, initialize it in STANDARD_SECURE_BOOT_MODE.
368 CustomMode
= STANDARD_SECURE_BOOT_MODE
;
369 Status
= UpdateVariable (
370 EFI_CUSTOM_MODE_NAME
,
371 &gEfiCustomModeEnableGuid
,
374 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
380 if (EFI_ERROR (Status
)) {
385 DEBUG ((EFI_D_INFO
, "Variable %s is %x\n", EFI_CUSTOM_MODE_NAME
, CustomMode
));
388 // Check "certdb" variable's existence.
389 // If it doesn't exist, then create a new one with
390 // EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set.
392 Status
= FindVariable (
396 &mVariableModuleGlobal
->VariableGlobal
,
400 if (Variable
.CurrPtr
== NULL
) {
401 VarAttr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
402 ListSize
= sizeof (UINT32
);
403 Status
= UpdateVariable (
414 if (EFI_ERROR (Status
)) {
423 Add public key in store and return its index.
425 @param[in] PubKey Input pointer to Public Key data
427 @return Index of new added item
438 VARIABLE_POINTER_TRACK Variable
;
441 if (PubKey
== NULL
) {
445 Status
= FindVariable (
447 &gEfiAuthenticatedVariableGuid
,
449 &mVariableModuleGlobal
->VariableGlobal
,
452 ASSERT_EFI_ERROR (Status
);
454 // Check whether the public key entry does exist.
457 for (Ptr
= mPubKeyStore
, Index
= 1; Index
<= mPubKeyNumber
; Index
++) {
458 if (CompareMem (Ptr
, PubKey
, EFI_CERT_TYPE_RSA2048_SIZE
) == 0) {
462 Ptr
+= EFI_CERT_TYPE_RSA2048_SIZE
;
467 // Add public key in database.
469 if (mPubKeyNumber
== MAX_KEY_NUM
) {
471 // Notes: Database is full, need enhancement here, currently just return 0.
476 CopyMem (mPubKeyStore
+ mPubKeyNumber
* EFI_CERT_TYPE_RSA2048_SIZE
, PubKey
, EFI_CERT_TYPE_RSA2048_SIZE
);
477 Index
= ++mPubKeyNumber
;
479 // Update public key database variable.
481 Status
= UpdateVariable (
483 &gEfiAuthenticatedVariableGuid
,
485 mPubKeyNumber
* EFI_CERT_TYPE_RSA2048_SIZE
,
486 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
,
492 ASSERT_EFI_ERROR (Status
);
499 Verify data payload with AuthInfo in EFI_CERT_TYPE_RSA2048_SHA256_GUID type.
500 Follow the steps in UEFI2.2.
502 Caution: This function may receive untrusted input.
503 This function may be invoked in SMM mode, and datasize and data are external input.
504 This function will do basic validation, before parse the data.
505 This function will parse the authentication carefully to avoid security issues, like
506 buffer overflow, integer overflow.
508 @param[in] Data Pointer to data with AuthInfo.
509 @param[in] DataSize Size of Data.
510 @param[in] PubKey Public key used for verification.
512 @retval EFI_INVALID_PARAMETER Invalid parameter.
513 @retval EFI_SECURITY_VIOLATION If authentication failed.
514 @retval EFI_SUCCESS Authentication successful.
518 VerifyCounterBasedPayload (
525 EFI_VARIABLE_AUTHENTICATION
*CertData
;
526 EFI_CERT_BLOCK_RSA_2048_SHA256
*CertBlock
;
527 UINT8 Digest
[SHA256_DIGEST_SIZE
];
534 if (Data
== NULL
|| PubKey
== NULL
) {
535 return EFI_INVALID_PARAMETER
;
538 CertData
= (EFI_VARIABLE_AUTHENTICATION
*) Data
;
539 CertBlock
= (EFI_CERT_BLOCK_RSA_2048_SHA256
*) (CertData
->AuthInfo
.CertData
);
542 // wCertificateType should be WIN_CERT_TYPE_EFI_GUID.
543 // Cert type should be EFI_CERT_TYPE_RSA2048_SHA256_GUID.
545 if ((CertData
->AuthInfo
.Hdr
.wCertificateType
!= WIN_CERT_TYPE_EFI_GUID
) ||
546 !CompareGuid (&CertData
->AuthInfo
.CertType
, &gEfiCertTypeRsa2048Sha256Guid
)
549 // Invalid AuthInfo type, return EFI_SECURITY_VIOLATION.
551 return EFI_SECURITY_VIOLATION
;
554 // Hash data payload with SHA256.
556 ZeroMem (Digest
, SHA256_DIGEST_SIZE
);
557 Status
= Sha256Init (mHashCtx
);
561 Status
= Sha256Update (mHashCtx
, Data
+ AUTHINFO_SIZE
, (UINTN
) (DataSize
- AUTHINFO_SIZE
));
566 // Hash Monotonic Count.
568 Status
= Sha256Update (mHashCtx
, &CertData
->MonotonicCount
, sizeof (UINT64
));
572 Status
= Sha256Final (mHashCtx
, Digest
);
577 // Generate & Initialize RSA Context.
580 ASSERT (Rsa
!= NULL
);
582 // Set RSA Key Components.
583 // NOTE: Only N and E are needed to be set as RSA public key for signature verification.
585 Status
= RsaSetKey (Rsa
, RsaKeyN
, PubKey
, EFI_CERT_TYPE_RSA2048_SIZE
);
589 Status
= RsaSetKey (Rsa
, RsaKeyE
, mRsaE
, sizeof (mRsaE
));
594 // Verify the signature.
596 Status
= RsaPkcs1Verify (
600 CertBlock
->Signature
,
601 EFI_CERT_TYPE_RSA2048_SHA256_SIZE
611 return EFI_SECURITY_VIOLATION
;
616 Update platform mode.
618 @param[in] Mode SETUP_MODE or USER_MODE.
620 @return EFI_INVALID_PARAMETER Invalid parameter.
621 @return EFI_SUCCESS Update platform mode successfully.
630 VARIABLE_POINTER_TRACK Variable
;
632 UINT8 SecureBootMode
;
633 UINT8 SecureBootEnable
;
634 UINTN VariableDataSize
;
636 Status
= FindVariable (
638 &gEfiGlobalVariableGuid
,
640 &mVariableModuleGlobal
->VariableGlobal
,
643 if (EFI_ERROR (Status
)) {
648 // Update the value of SetupMode variable by a simple mem copy, this could avoid possible
649 // variable storage reclaim at runtime.
651 mPlatformMode
= (UINT8
) Mode
;
652 CopyMem (GetVariableDataPtr (Variable
.CurrPtr
), &mPlatformMode
, sizeof(UINT8
));
656 // SecureBoot Variable indicates whether the platform firmware is operating
657 // in Secure boot mode (1) or not (0), so we should not change SecureBoot
658 // Variable in runtime.
664 // Check "SecureBoot" variable's existence.
665 // If it doesn't exist, firmware has no capability to perform driver signing verification,
666 // then set "SecureBoot" to 0.
668 Status
= FindVariable (
669 EFI_SECURE_BOOT_MODE_NAME
,
670 &gEfiGlobalVariableGuid
,
672 &mVariableModuleGlobal
->VariableGlobal
,
676 // If "SecureBoot" variable exists, then check "SetupMode" variable update.
677 // If "SetupMode" variable is USER_MODE, "SecureBoot" variable is set to 1.
678 // If "SetupMode" variable is SETUP_MODE, "SecureBoot" variable is set to 0.
680 if (Variable
.CurrPtr
== NULL
) {
681 SecureBootMode
= SECURE_BOOT_MODE_DISABLE
;
683 if (mPlatformMode
== USER_MODE
) {
684 SecureBootMode
= SECURE_BOOT_MODE_ENABLE
;
685 } else if (mPlatformMode
== SETUP_MODE
) {
686 SecureBootMode
= SECURE_BOOT_MODE_DISABLE
;
688 return EFI_NOT_FOUND
;
692 VarAttr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
;
693 Status
= UpdateVariable (
694 EFI_SECURE_BOOT_MODE_NAME
,
695 &gEfiGlobalVariableGuid
,
704 if (EFI_ERROR (Status
)) {
709 // Check "SecureBootEnable" variable's existence. It can enable/disable secure boot feature.
711 Status
= FindVariable (
712 EFI_SECURE_BOOT_ENABLE_NAME
,
713 &gEfiSecureBootEnableDisableGuid
,
715 &mVariableModuleGlobal
->VariableGlobal
,
719 if (SecureBootMode
== SECURE_BOOT_MODE_ENABLE
) {
721 // Create the "SecureBootEnable" variable as secure boot is enabled.
723 SecureBootEnable
= SECURE_BOOT_ENABLE
;
724 VariableDataSize
= sizeof (SecureBootEnable
);
727 // Delete the "SecureBootEnable" variable if this variable exist as "SecureBoot"
728 // variable is not in secure boot state.
730 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
733 SecureBootEnable
= SECURE_BOOT_DISABLE
;
734 VariableDataSize
= 0;
737 Status
= UpdateVariable (
738 EFI_SECURE_BOOT_ENABLE_NAME
,
739 &gEfiSecureBootEnableDisableGuid
,
742 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
752 Check input data form to make sure it is a valid EFI_SIGNATURE_LIST for PK/KEK/db/dbx variable.
754 @param[in] VariableName Name of Variable to be check.
755 @param[in] VendorGuid Variable vendor GUID.
756 @param[in] Data Point to the variable data to be checked.
757 @param[in] DataSize Size of Data.
759 @return EFI_INVALID_PARAMETER Invalid signature list format.
760 @return EFI_SUCCESS Passed signature list format check successfully.
764 CheckSignatureListFormat(
765 IN CHAR16
*VariableName
,
766 IN EFI_GUID
*VendorGuid
,
771 EFI_SIGNATURE_LIST
*SigList
;
777 EFI_SIGNATURE_DATA
*CertData
;
784 ASSERT (VariableName
!= NULL
&& VendorGuid
!= NULL
&& Data
!= NULL
);
786 if (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, EFI_PLATFORM_KEY_NAME
) == 0)){
788 } else if ((CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && StrCmp (VariableName
, EFI_KEY_EXCHANGE_KEY_NAME
) == 0) ||
789 (CompareGuid (VendorGuid
, &gEfiImageSecurityDatabaseGuid
) &&
790 (StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE
) == 0 || StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE1
) == 0))){
797 SigList
= (EFI_SIGNATURE_LIST
*) Data
;
798 SigDataSize
= DataSize
;
802 // Walk throuth the input signature list and check the data format.
803 // If any signature is incorrectly formed, the whole check will fail.
805 while ((SigDataSize
> 0) && (SigDataSize
>= SigList
->SignatureListSize
)) {
806 for (Index
= 0; Index
< (sizeof (mSupportSigItem
) / sizeof (EFI_SIGNATURE_ITEM
)); Index
++ ) {
807 if (CompareGuid (&SigList
->SignatureType
, &mSupportSigItem
[Index
].SigType
)) {
809 // The value of SignatureSize should always be 16 (size of SignatureOwner
810 // component) add the data length according to signature type.
812 if (mSupportSigItem
[Index
].SigDataSize
!= ((UINT32
) ~0) &&
813 (SigList
->SignatureSize
- sizeof (EFI_GUID
)) != mSupportSigItem
[Index
].SigDataSize
) {
814 return EFI_INVALID_PARAMETER
;
816 if (mSupportSigItem
[Index
].SigHeaderSize
!= ((UINTN
) ~0) &&
817 SigList
->SignatureHeaderSize
!= mSupportSigItem
[Index
].SigHeaderSize
) {
818 return EFI_INVALID_PARAMETER
;
824 if (Index
== (sizeof (mSupportSigItem
) / sizeof (EFI_SIGNATURE_ITEM
))) {
826 // Undefined signature type.
828 return EFI_INVALID_PARAMETER
;
831 if (CompareGuid (&SigList
->SignatureType
, &gEfiCertX509Guid
)) {
833 // Try to retrieve the RSA public key from the X.509 certificate.
834 // If this operation fails, it's not a valid certificate.
836 RsaContext
= RsaNew ();
837 if (RsaContext
== NULL
) {
838 return EFI_INVALID_PARAMETER
;
840 CertData
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) SigList
+ sizeof (EFI_SIGNATURE_LIST
) + SigList
->SignatureHeaderSize
);
841 CertLen
= SigList
->SignatureSize
- sizeof (EFI_GUID
);
842 if (!RsaGetPublicKeyFromX509 (CertData
->SignatureData
, CertLen
, &RsaContext
)) {
843 RsaFree (RsaContext
);
844 return EFI_INVALID_PARAMETER
;
846 RsaFree (RsaContext
);
849 if ((SigList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - SigList
->SignatureHeaderSize
) % SigList
->SignatureSize
!= 0) {
850 return EFI_INVALID_PARAMETER
;
852 SigCount
+= (SigList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - SigList
->SignatureHeaderSize
) / SigList
->SignatureSize
;
854 SigDataSize
-= SigList
->SignatureListSize
;
855 SigList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) SigList
+ SigList
->SignatureListSize
);
858 if (((UINTN
) SigList
- (UINTN
) Data
) != DataSize
) {
859 return EFI_INVALID_PARAMETER
;
862 if (IsPk
&& SigCount
> 1) {
863 return EFI_INVALID_PARAMETER
;
870 Process variable with platform key for verification.
872 Caution: This function may receive untrusted input.
873 This function may be invoked in SMM mode, and datasize and data are external input.
874 This function will do basic validation, before parse the data.
875 This function will parse the authentication carefully to avoid security issues, like
876 buffer overflow, integer overflow.
877 This function will check attribute carefully to avoid authentication bypass.
879 @param[in] VariableName Name of Variable to be found.
880 @param[in] VendorGuid Variable vendor GUID.
881 @param[in] Data Data pointer.
882 @param[in] DataSize Size of Data found. If size is less than the
883 data, this value contains the required size.
884 @param[in] Variable The variable information which is used to keep track of variable usage.
885 @param[in] Attributes Attribute value of the variable
886 @param[in] IsPk Indicate whether it is to process pk.
888 @return EFI_INVALID_PARAMETER Invalid parameter.
889 @return EFI_SECURITY_VIOLATION The variable does NOT pass the validation.
890 check carried out by the firmware.
891 @return EFI_SUCCESS Variable passed validation successfully.
896 IN CHAR16
*VariableName
,
897 IN EFI_GUID
*VendorGuid
,
900 IN VARIABLE_POINTER_TRACK
*Variable
,
901 IN UINT32 Attributes OPTIONAL
,
910 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0 ||
911 (Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) == 0) {
913 // PK, KEK and db/dbx should set EFI_VARIABLE_NON_VOLATILE attribute and should be a time-based
914 // authenticated variable.
916 return EFI_INVALID_PARAMETER
;
920 if ((InCustomMode() && UserPhysicalPresent()) || (mPlatformMode
== SETUP_MODE
&& !IsPk
)) {
921 Payload
= (UINT8
*) Data
+ AUTHINFO2_SIZE (Data
);
922 PayloadSize
= DataSize
- AUTHINFO2_SIZE (Data
);
923 if (PayloadSize
== 0) {
927 Status
= CheckSignatureListFormat(VariableName
, VendorGuid
, Payload
, PayloadSize
);
928 if (EFI_ERROR (Status
)) {
932 Status
= UpdateVariable (
941 &((EFI_VARIABLE_AUTHENTICATION_2
*) Data
)->TimeStamp
943 } else if (mPlatformMode
== USER_MODE
) {
945 // Verify against X509 Cert in PK database.
947 Status
= VerifyTimeBasedPayload (
959 // Verify against the certificate in data payload.
961 Status
= VerifyTimeBasedPayload (
973 if (!EFI_ERROR(Status
) && IsPk
) {
974 if (mPlatformMode
== SETUP_MODE
&& !Del
) {
976 // If enroll PK in setup mode, need change to user mode.
978 Status
= UpdatePlatformMode (USER_MODE
);
979 } else if (mPlatformMode
== USER_MODE
&& Del
){
981 // If delete PK in user mode, need change to setup mode.
983 Status
= UpdatePlatformMode (SETUP_MODE
);
991 Process variable with key exchange key for verification.
993 Caution: This function may receive untrusted input.
994 This function may be invoked in SMM mode, and datasize and data are external input.
995 This function will do basic validation, before parse the data.
996 This function will parse the authentication carefully to avoid security issues, like
997 buffer overflow, integer overflow.
998 This function will check attribute carefully to avoid authentication bypass.
1000 @param[in] VariableName Name of Variable to be found.
1001 @param[in] VendorGuid Variable vendor GUID.
1002 @param[in] Data Data pointer.
1003 @param[in] DataSize Size of Data found. If size is less than the
1004 data, this value contains the required size.
1005 @param[in] Variable The variable information which is used to keep track of variable usage.
1006 @param[in] Attributes Attribute value of the variable.
1008 @return EFI_INVALID_PARAMETER Invalid parameter.
1009 @return EFI_SECURITY_VIOLATION The variable does NOT pass the validation
1010 check carried out by the firmware.
1011 @return EFI_SUCCESS Variable pass validation successfully.
1016 IN CHAR16
*VariableName
,
1017 IN EFI_GUID
*VendorGuid
,
1020 IN VARIABLE_POINTER_TRACK
*Variable
,
1021 IN UINT32 Attributes OPTIONAL
1028 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0 ||
1029 (Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) == 0) {
1031 // DB and DBX should set EFI_VARIABLE_NON_VOLATILE attribute and should be a time-based
1032 // authenticated variable.
1034 return EFI_INVALID_PARAMETER
;
1037 Status
= EFI_SUCCESS
;
1038 if (mPlatformMode
== USER_MODE
&& !(InCustomMode() && UserPhysicalPresent())) {
1040 // Time-based, verify against X509 Cert KEK.
1042 return VerifyTimeBasedPayload (
1054 // If in setup mode or custom secure boot mode, no authentication needed.
1056 Payload
= (UINT8
*) Data
+ AUTHINFO2_SIZE (Data
);
1057 PayloadSize
= DataSize
- AUTHINFO2_SIZE (Data
);
1059 Status
= CheckSignatureListFormat(VariableName
, VendorGuid
, Payload
, PayloadSize
);
1060 if (EFI_ERROR (Status
)) {
1064 Status
= UpdateVariable (
1073 &((EFI_VARIABLE_AUTHENTICATION_2
*) Data
)->TimeStamp
1081 Process variable with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS/EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set
1083 Caution: This function may receive untrusted input.
1084 This function may be invoked in SMM mode, and datasize and data are external input.
1085 This function will do basic validation, before parse the data.
1086 This function will parse the authentication carefully to avoid security issues, like
1087 buffer overflow, integer overflow.
1088 This function will check attribute carefully to avoid authentication bypass.
1090 @param[in] VariableName Name of Variable to be found.
1091 @param[in] VendorGuid Variable vendor GUID.
1093 @param[in] Data Data pointer.
1094 @param[in] DataSize Size of Data found. If size is less than the
1095 data, this value contains the required size.
1096 @param[in] Variable The variable information which is used to keep track of variable usage.
1097 @param[in] Attributes Attribute value of the variable.
1099 @return EFI_INVALID_PARAMETER Invalid parameter.
1100 @return EFI_WRITE_PROTECTED Variable is write-protected and needs authentication with
1101 EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS set.
1102 @return EFI_SECURITY_VIOLATION The variable is with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
1103 set, but the AuthInfo does NOT pass the validation
1104 check carried out by the firmware.
1105 @return EFI_SUCCESS Variable is not write-protected or pass validation successfully.
1110 IN CHAR16
*VariableName
,
1111 IN EFI_GUID
*VendorGuid
,
1114 IN VARIABLE_POINTER_TRACK
*Variable
,
1115 IN UINT32 Attributes
1120 BOOLEAN IsFirstTime
;
1122 EFI_VARIABLE_AUTHENTICATION
*CertData
;
1123 EFI_CERT_BLOCK_RSA_2048_SHA256
*CertBlock
;
1125 UINT64 MonotonicCount
;
1133 if (NeedPhysicallyPresent(VariableName
, VendorGuid
) && !UserPhysicalPresent()) {
1135 // This variable is protected, only physical present user could modify its value.
1137 return EFI_SECURITY_VIOLATION
;
1141 // A time-based authenticated variable and a count-based authenticated variable
1142 // can't be updated by each other.
1144 if (Variable
->CurrPtr
!= NULL
) {
1145 if (((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) &&
1146 ((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0)) {
1147 return EFI_SECURITY_VIOLATION
;
1150 if (((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) &&
1151 ((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0)) {
1152 return EFI_SECURITY_VIOLATION
;
1157 // Process Time-based Authenticated variable.
1159 if ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) {
1160 return VerifyTimeBasedPayload (
1173 // Determine if first time SetVariable with the EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS.
1175 if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) {
1177 // Determine current operation type.
1179 if (DataSize
== AUTHINFO_SIZE
) {
1183 // Determine whether this is the first time with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS set.
1185 if (Variable
->CurrPtr
== NULL
) {
1187 } else if ((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) == 0) {
1190 KeyIndex
= Variable
->CurrPtr
->PubKeyIndex
;
1191 IsFirstTime
= FALSE
;
1193 } else if ((Variable
->CurrPtr
!= NULL
) &&
1194 ((Variable
->CurrPtr
->Attributes
& (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
)) != 0)
1197 // If the variable is already write-protected, it always needs authentication before update.
1199 return EFI_WRITE_PROTECTED
;
1202 // If without EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS, set and attributes collision.
1203 // That means it is not authenticated variable, just update variable as usual.
1205 Status
= UpdateVariable (VariableName
, VendorGuid
, Data
, DataSize
, Attributes
, 0, 0, Variable
, NULL
);
1210 // Get PubKey and check Monotonic Count value corresponding to the variable.
1212 CertData
= (EFI_VARIABLE_AUTHENTICATION
*) Data
;
1213 CertBlock
= (EFI_CERT_BLOCK_RSA_2048_SHA256
*) (CertData
->AuthInfo
.CertData
);
1214 PubKey
= CertBlock
->PublicKey
;
1217 // Update Monotonic Count value.
1219 MonotonicCount
= CertData
->MonotonicCount
;
1223 // Check input PubKey.
1225 if (CompareMem (PubKey
, mPubKeyStore
+ (KeyIndex
- 1) * EFI_CERT_TYPE_RSA2048_SIZE
, EFI_CERT_TYPE_RSA2048_SIZE
) != 0) {
1226 return EFI_SECURITY_VIOLATION
;
1229 // Compare the current monotonic count and ensure that it is greater than the last SetVariable
1230 // operation with the EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS attribute set.
1232 if (CertData
->MonotonicCount
<= Variable
->CurrPtr
->MonotonicCount
) {
1234 // Monotonic count check fail, suspicious replay attack, return EFI_SECURITY_VIOLATION.
1236 return EFI_SECURITY_VIOLATION
;
1240 // Verify the certificate in Data payload.
1242 Status
= VerifyCounterBasedPayload (Data
, DataSize
, PubKey
);
1243 if (EFI_ERROR (Status
)) {
1248 // Now, the signature has been verified!
1250 if (IsFirstTime
&& !IsDeletion
) {
1252 // Update public key database variable if need.
1254 KeyIndex
= AddPubKeyInStore (PubKey
);
1255 if (KeyIndex
== 0) {
1256 return EFI_SECURITY_VIOLATION
;
1261 // Verification pass.
1263 return UpdateVariable (VariableName
, VendorGuid
, (UINT8
*)Data
+ AUTHINFO_SIZE
, DataSize
- AUTHINFO_SIZE
, Attributes
, KeyIndex
, MonotonicCount
, Variable
, NULL
);
1267 Merge two buffers which formatted as EFI_SIGNATURE_LIST. Only the new EFI_SIGNATURE_DATA
1268 will be appended to the original EFI_SIGNATURE_LIST, duplicate EFI_SIGNATURE_DATA
1271 @param[in, out] Data Pointer to original EFI_SIGNATURE_LIST.
1272 @param[in] DataSize Size of Data buffer.
1273 @param[in] NewData Pointer to new EFI_SIGNATURE_LIST to be appended.
1274 @param[in] NewDataSize Size of NewData buffer.
1276 @return Size of the merged buffer.
1280 AppendSignatureList (
1284 IN UINTN NewDataSize
1287 EFI_SIGNATURE_LIST
*CertList
;
1288 EFI_SIGNATURE_DATA
*Cert
;
1290 EFI_SIGNATURE_LIST
*NewCertList
;
1291 EFI_SIGNATURE_DATA
*NewCert
;
1298 UINTN SignatureListSize
;
1301 Tail
= (UINT8
*) Data
+ DataSize
;
1303 NewCertList
= (EFI_SIGNATURE_LIST
*) NewData
;
1304 while ((NewDataSize
> 0) && (NewDataSize
>= NewCertList
->SignatureListSize
)) {
1305 NewCert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) NewCertList
+ sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
);
1306 NewCertCount
= (NewCertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - NewCertList
->SignatureHeaderSize
) / NewCertList
->SignatureSize
;
1309 for (Index
= 0; Index
< NewCertCount
; Index
++) {
1313 CertList
= (EFI_SIGNATURE_LIST
*) Data
;
1314 while ((Size
> 0) && (Size
>= CertList
->SignatureListSize
)) {
1315 if (CompareGuid (&CertList
->SignatureType
, &NewCertList
->SignatureType
) &&
1316 (CertList
->SignatureSize
== NewCertList
->SignatureSize
)) {
1317 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
1318 CertCount
= (CertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - CertList
->SignatureHeaderSize
) / CertList
->SignatureSize
;
1319 for (Index2
= 0; Index2
< CertCount
; Index2
++) {
1321 // Iterate each Signature Data in this Signature List.
1323 if (CompareMem (NewCert
, Cert
, CertList
->SignatureSize
) == 0) {
1327 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) Cert
+ CertList
->SignatureSize
);
1334 Size
-= CertList
->SignatureListSize
;
1335 CertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) CertList
+ CertList
->SignatureListSize
);
1340 // New EFI_SIGNATURE_DATA, append it.
1342 if (CopiedCount
== 0) {
1344 // Copy EFI_SIGNATURE_LIST header for only once.
1346 CopyMem (Tail
, NewCertList
, sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
);
1347 Tail
= Tail
+ sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
;
1350 CopyMem (Tail
, NewCert
, NewCertList
->SignatureSize
);
1351 Tail
+= NewCertList
->SignatureSize
;
1355 NewCert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) NewCert
+ NewCertList
->SignatureSize
);
1359 // Update SignatureListSize in newly appended EFI_SIGNATURE_LIST.
1361 if (CopiedCount
!= 0) {
1362 SignatureListSize
= sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
+ (CopiedCount
* NewCertList
->SignatureSize
);
1363 CertList
= (EFI_SIGNATURE_LIST
*) (Tail
- SignatureListSize
);
1364 CertList
->SignatureListSize
= (UINT32
) SignatureListSize
;
1367 NewDataSize
-= NewCertList
->SignatureListSize
;
1368 NewCertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) NewCertList
+ NewCertList
->SignatureListSize
);
1371 return (Tail
- (UINT8
*) Data
);
1375 Compare two EFI_TIME data.
1378 @param FirstTime A pointer to the first EFI_TIME data.
1379 @param SecondTime A pointer to the second EFI_TIME data.
1381 @retval TRUE The FirstTime is not later than the SecondTime.
1382 @retval FALSE The FirstTime is later than the SecondTime.
1387 IN EFI_TIME
*FirstTime
,
1388 IN EFI_TIME
*SecondTime
1391 if (FirstTime
->Year
!= SecondTime
->Year
) {
1392 return (BOOLEAN
) (FirstTime
->Year
< SecondTime
->Year
);
1393 } else if (FirstTime
->Month
!= SecondTime
->Month
) {
1394 return (BOOLEAN
) (FirstTime
->Month
< SecondTime
->Month
);
1395 } else if (FirstTime
->Day
!= SecondTime
->Day
) {
1396 return (BOOLEAN
) (FirstTime
->Day
< SecondTime
->Day
);
1397 } else if (FirstTime
->Hour
!= SecondTime
->Hour
) {
1398 return (BOOLEAN
) (FirstTime
->Hour
< SecondTime
->Hour
);
1399 } else if (FirstTime
->Minute
!= SecondTime
->Minute
) {
1400 return (BOOLEAN
) (FirstTime
->Minute
< SecondTime
->Minute
);
1403 return (BOOLEAN
) (FirstTime
->Second
<= SecondTime
->Second
);
1407 Find matching signer's certificates for common authenticated variable
1408 by corresponding VariableName and VendorGuid from "certdb".
1410 The data format of "certdb":
1412 // UINT32 CertDbListSize;
1413 // /// AUTH_CERT_DB_DATA Certs1[];
1414 // /// AUTH_CERT_DB_DATA Certs2[];
1416 // /// AUTH_CERT_DB_DATA Certsn[];
1419 @param[in] VariableName Name of authenticated Variable.
1420 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1421 @param[in] Data Pointer to variable "certdb".
1422 @param[in] DataSize Size of variable "certdb".
1423 @param[out] CertOffset Offset of matching CertData, from starting of Data.
1424 @param[out] CertDataSize Length of CertData in bytes.
1425 @param[out] CertNodeOffset Offset of matching AUTH_CERT_DB_DATA , from
1427 @param[out] CertNodeSize Length of AUTH_CERT_DB_DATA in bytes.
1429 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1430 @retval EFI_NOT_FOUND Fail to find matching certs.
1431 @retval EFI_SUCCESS Find matching certs and output parameters.
1436 IN CHAR16
*VariableName
,
1437 IN EFI_GUID
*VendorGuid
,
1440 OUT UINT32
*CertOffset
, OPTIONAL
1441 OUT UINT32
*CertDataSize
, OPTIONAL
1442 OUT UINT32
*CertNodeOffset
,OPTIONAL
1443 OUT UINT32
*CertNodeSize OPTIONAL
1447 AUTH_CERT_DB_DATA
*Ptr
;
1451 UINT32 CertDbListSize
;
1453 if ((VariableName
== NULL
) || (VendorGuid
== NULL
) || (Data
== NULL
)) {
1454 return EFI_INVALID_PARAMETER
;
1458 // Check whether DataSize matches recorded CertDbListSize.
1460 if (DataSize
< sizeof (UINT32
)) {
1461 return EFI_INVALID_PARAMETER
;
1464 CertDbListSize
= ReadUnaligned32 ((UINT32
*) Data
);
1466 if (CertDbListSize
!= (UINT32
) DataSize
) {
1467 return EFI_INVALID_PARAMETER
;
1470 Offset
= sizeof (UINT32
);
1473 // Get corresponding certificates by VendorGuid and VariableName.
1475 while (Offset
< (UINT32
) DataSize
) {
1476 Ptr
= (AUTH_CERT_DB_DATA
*) (Data
+ Offset
);
1478 // Check whether VendorGuid matches.
1480 if (CompareGuid (&Ptr
->VendorGuid
, VendorGuid
)) {
1481 NodeSize
= ReadUnaligned32 (&Ptr
->CertNodeSize
);
1482 NameSize
= ReadUnaligned32 (&Ptr
->NameSize
);
1483 CertSize
= ReadUnaligned32 (&Ptr
->CertDataSize
);
1485 if (NodeSize
!= sizeof (EFI_GUID
) + sizeof (UINT32
) * 3 + CertSize
+
1486 sizeof (CHAR16
) * NameSize
) {
1487 return EFI_INVALID_PARAMETER
;
1490 Offset
= Offset
+ sizeof (EFI_GUID
) + sizeof (UINT32
) * 3;
1492 // Check whether VariableName matches.
1494 if ((NameSize
== StrLen (VariableName
)) &&
1495 (CompareMem (Data
+ Offset
, VariableName
, NameSize
* sizeof (CHAR16
)) == 0)) {
1496 Offset
= Offset
+ NameSize
* sizeof (CHAR16
);
1498 if (CertOffset
!= NULL
) {
1499 *CertOffset
= Offset
;
1502 if (CertDataSize
!= NULL
) {
1503 *CertDataSize
= CertSize
;
1506 if (CertNodeOffset
!= NULL
) {
1507 *CertNodeOffset
= (UINT32
) ((UINT8
*) Ptr
- Data
);
1510 if (CertNodeSize
!= NULL
) {
1511 *CertNodeSize
= NodeSize
;
1516 Offset
= Offset
+ NameSize
* sizeof (CHAR16
) + CertSize
;
1519 NodeSize
= ReadUnaligned32 (&Ptr
->CertNodeSize
);
1520 Offset
= Offset
+ NodeSize
;
1524 return EFI_NOT_FOUND
;
1528 Retrieve signer's certificates for common authenticated variable
1529 by corresponding VariableName and VendorGuid from "certdb".
1531 @param[in] VariableName Name of authenticated Variable.
1532 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1533 @param[out] CertData Pointer to signer's certificates.
1534 @param[out] CertDataSize Length of CertData in bytes.
1536 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1537 @retval EFI_NOT_FOUND Fail to find "certdb" or matching certs.
1538 @retval EFI_SUCCESS Get signer's certificates successfully.
1543 IN CHAR16
*VariableName
,
1544 IN EFI_GUID
*VendorGuid
,
1545 OUT UINT8
**CertData
,
1546 OUT UINT32
*CertDataSize
1549 VARIABLE_POINTER_TRACK CertDbVariable
;
1555 if ((VariableName
== NULL
) || (VendorGuid
== NULL
) || (CertData
== NULL
) || (CertDataSize
== NULL
)) {
1556 return EFI_INVALID_PARAMETER
;
1560 // Get variable "certdb".
1562 Status
= FindVariable (
1566 &mVariableModuleGlobal
->VariableGlobal
,
1569 if (EFI_ERROR (Status
)) {
1573 DataSize
= DataSizeOfVariable (CertDbVariable
.CurrPtr
);
1574 Data
= GetVariableDataPtr (CertDbVariable
.CurrPtr
);
1575 if ((DataSize
== 0) || (Data
== NULL
)) {
1577 return EFI_NOT_FOUND
;
1580 Status
= FindCertsFromDb (
1591 if (EFI_ERROR (Status
)) {
1595 *CertData
= Data
+ CertOffset
;
1600 Delete matching signer's certificates when deleting common authenticated
1601 variable by corresponding VariableName and VendorGuid from "certdb".
1603 @param[in] VariableName Name of authenticated Variable.
1604 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1606 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1607 @retval EFI_NOT_FOUND Fail to find "certdb" or matching certs.
1608 @retval EFI_OUT_OF_RESOURCES The operation is failed due to lack of resources.
1609 @retval EFI_SUCCESS The operation is completed successfully.
1614 IN CHAR16
*VariableName
,
1615 IN EFI_GUID
*VendorGuid
1618 VARIABLE_POINTER_TRACK CertDbVariable
;
1623 UINT32 CertNodeOffset
;
1624 UINT32 CertNodeSize
;
1626 UINT32 NewCertDbSize
;
1628 if ((VariableName
== NULL
) || (VendorGuid
== NULL
)) {
1629 return EFI_INVALID_PARAMETER
;
1633 // Get variable "certdb".
1635 Status
= FindVariable (
1639 &mVariableModuleGlobal
->VariableGlobal
,
1642 if (EFI_ERROR (Status
)) {
1646 DataSize
= DataSizeOfVariable (CertDbVariable
.CurrPtr
);
1647 Data
= GetVariableDataPtr (CertDbVariable
.CurrPtr
);
1648 if ((DataSize
== 0) || (Data
== NULL
)) {
1650 return EFI_NOT_FOUND
;
1653 if (DataSize
== sizeof (UINT32
)) {
1655 // There is no certs in certdb.
1661 // Get corresponding cert node from certdb.
1663 Status
= FindCertsFromDb (
1674 if (EFI_ERROR (Status
)) {
1678 if (DataSize
< (CertNodeOffset
+ CertNodeSize
)) {
1679 return EFI_NOT_FOUND
;
1683 // Construct new data content of variable "certdb".
1685 NewCertDbSize
= (UINT32
) DataSize
- CertNodeSize
;
1686 NewCertDb
= (UINT8
*) mCertDbStore
;
1689 // Copy the DB entries before deleting node.
1691 CopyMem (NewCertDb
, Data
, CertNodeOffset
);
1693 // Update CertDbListSize.
1695 CopyMem (NewCertDb
, &NewCertDbSize
, sizeof (UINT32
));
1697 // Copy the DB entries after deleting node.
1699 if (DataSize
> (CertNodeOffset
+ CertNodeSize
)) {
1701 NewCertDb
+ CertNodeOffset
,
1702 Data
+ CertNodeOffset
+ CertNodeSize
,
1703 DataSize
- CertNodeOffset
- CertNodeSize
1710 VarAttr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
1711 Status
= UpdateVariable (
1727 Insert signer's certificates for common authenticated variable with VariableName
1728 and VendorGuid in AUTH_CERT_DB_DATA to "certdb".
1730 @param[in] VariableName Name of authenticated Variable.
1731 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1732 @param[in] CertData Pointer to signer's certificates.
1733 @param[in] CertDataSize Length of CertData in bytes.
1735 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1736 @retval EFI_ACCESS_DENIED An AUTH_CERT_DB_DATA entry with same VariableName
1737 and VendorGuid already exists.
1738 @retval EFI_OUT_OF_RESOURCES The operation is failed due to lack of resources.
1739 @retval EFI_SUCCESS Insert an AUTH_CERT_DB_DATA entry to "certdb"
1744 IN CHAR16
*VariableName
,
1745 IN EFI_GUID
*VendorGuid
,
1747 IN UINTN CertDataSize
1750 VARIABLE_POINTER_TRACK CertDbVariable
;
1756 UINT32 NewCertDbSize
;
1757 UINT32 CertNodeSize
;
1759 AUTH_CERT_DB_DATA
*Ptr
;
1761 if ((VariableName
== NULL
) || (VendorGuid
== NULL
) || (CertData
== NULL
)) {
1762 return EFI_INVALID_PARAMETER
;
1766 // Get variable "certdb".
1768 Status
= FindVariable (
1772 &mVariableModuleGlobal
->VariableGlobal
,
1775 if (EFI_ERROR (Status
)) {
1779 DataSize
= DataSizeOfVariable (CertDbVariable
.CurrPtr
);
1780 Data
= GetVariableDataPtr (CertDbVariable
.CurrPtr
);
1781 if ((DataSize
== 0) || (Data
== NULL
)) {
1783 return EFI_NOT_FOUND
;
1787 // Find whether matching cert node already exists in "certdb".
1788 // If yes return error.
1790 Status
= FindCertsFromDb (
1801 if (!EFI_ERROR (Status
)) {
1803 return EFI_ACCESS_DENIED
;
1807 // Construct new data content of variable "certdb".
1809 NameSize
= (UINT32
) StrLen (VariableName
);
1810 CertNodeSize
= sizeof (AUTH_CERT_DB_DATA
) + (UINT32
) CertDataSize
+ NameSize
* sizeof (CHAR16
);
1811 NewCertDbSize
= (UINT32
) DataSize
+ CertNodeSize
;
1812 if (NewCertDbSize
> MAX_CERTDB_SIZE
) {
1813 return EFI_OUT_OF_RESOURCES
;
1815 NewCertDb
= (UINT8
*) mCertDbStore
;
1818 // Copy the DB entries before deleting node.
1820 CopyMem (NewCertDb
, Data
, DataSize
);
1822 // Update CertDbListSize.
1824 CopyMem (NewCertDb
, &NewCertDbSize
, sizeof (UINT32
));
1826 // Construct new cert node.
1828 Ptr
= (AUTH_CERT_DB_DATA
*) (NewCertDb
+ DataSize
);
1829 CopyGuid (&Ptr
->VendorGuid
, VendorGuid
);
1830 CopyMem (&Ptr
->CertNodeSize
, &CertNodeSize
, sizeof (UINT32
));
1831 CopyMem (&Ptr
->NameSize
, &NameSize
, sizeof (UINT32
));
1832 CopyMem (&Ptr
->CertDataSize
, &CertDataSize
, sizeof (UINT32
));
1835 (UINT8
*) Ptr
+ sizeof (AUTH_CERT_DB_DATA
),
1837 NameSize
* sizeof (CHAR16
)
1841 (UINT8
*) Ptr
+ sizeof (AUTH_CERT_DB_DATA
) + NameSize
* sizeof (CHAR16
),
1849 VarAttr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
1850 Status
= UpdateVariable (
1866 Process variable with EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set
1868 Caution: This function may receive untrusted input.
1869 This function may be invoked in SMM mode, and datasize and data are external input.
1870 This function will do basic validation, before parse the data.
1871 This function will parse the authentication carefully to avoid security issues, like
1872 buffer overflow, integer overflow.
1874 @param[in] VariableName Name of Variable to be found.
1875 @param[in] VendorGuid Variable vendor GUID.
1876 @param[in] Data Data pointer.
1877 @param[in] DataSize Size of Data found. If size is less than the
1878 data, this value contains the required size.
1879 @param[in] Variable The variable information which is used to keep track of variable usage.
1880 @param[in] Attributes Attribute value of the variable.
1881 @param[in] AuthVarType Verify against PK, KEK database, private database or certificate in data payload.
1882 @param[out] VarDel Delete the variable or not.
1884 @retval EFI_INVALID_PARAMETER Invalid parameter.
1885 @retval EFI_SECURITY_VIOLATION The variable does NOT pass the validation
1886 check carried out by the firmware.
1887 @retval EFI_OUT_OF_RESOURCES Failed to process variable due to lack
1889 @retval EFI_SUCCESS Variable pass validation successfully.
1893 VerifyTimeBasedPayload (
1894 IN CHAR16
*VariableName
,
1895 IN EFI_GUID
*VendorGuid
,
1898 IN VARIABLE_POINTER_TRACK
*Variable
,
1899 IN UINT32 Attributes
,
1900 IN AUTHVAR_TYPE AuthVarType
,
1914 BOOLEAN VerifyStatus
;
1916 EFI_SIGNATURE_LIST
*CertList
;
1917 EFI_SIGNATURE_DATA
*Cert
;
1918 VARIABLE_POINTER_TRACK KekVariable
;
1919 EFI_VARIABLE_AUTHENTICATION_2
*CertData
;
1922 VARIABLE_POINTER_TRACK PkVariable
;
1927 UINTN CertStackSize
;
1928 UINT8
*CertsInCertDb
;
1929 UINT32 CertsSizeinDb
;
1931 VerifyStatus
= FALSE
;
1940 // When the attribute EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS is
1941 // set, then the Data buffer shall begin with an instance of a complete (and serialized)
1942 // EFI_VARIABLE_AUTHENTICATION_2 descriptor. The descriptor shall be followed by the new
1943 // variable value and DataSize shall reflect the combined size of the descriptor and the new
1944 // variable value. The authentication descriptor is not part of the variable data and is not
1945 // returned by subsequent calls to GetVariable().
1947 CertData
= (EFI_VARIABLE_AUTHENTICATION_2
*) Data
;
1950 // Verify that Pad1, Nanosecond, TimeZone, Daylight and Pad2 components of the
1951 // TimeStamp value are set to zero.
1953 if ((CertData
->TimeStamp
.Pad1
!= 0) ||
1954 (CertData
->TimeStamp
.Nanosecond
!= 0) ||
1955 (CertData
->TimeStamp
.TimeZone
!= 0) ||
1956 (CertData
->TimeStamp
.Daylight
!= 0) ||
1957 (CertData
->TimeStamp
.Pad2
!= 0)) {
1958 return EFI_SECURITY_VIOLATION
;
1961 if ((Variable
->CurrPtr
!= NULL
) && ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0)) {
1962 if (CompareTimeStamp (&CertData
->TimeStamp
, &Variable
->CurrPtr
->TimeStamp
)) {
1964 // TimeStamp check fail, suspicious replay attack, return EFI_SECURITY_VIOLATION.
1966 return EFI_SECURITY_VIOLATION
;
1971 // wCertificateType should be WIN_CERT_TYPE_EFI_GUID.
1972 // Cert type should be EFI_CERT_TYPE_PKCS7_GUID.
1974 if ((CertData
->AuthInfo
.Hdr
.wCertificateType
!= WIN_CERT_TYPE_EFI_GUID
) ||
1975 !CompareGuid (&CertData
->AuthInfo
.CertType
, &gEfiCertPkcs7Guid
)) {
1977 // Invalid AuthInfo type, return EFI_SECURITY_VIOLATION.
1979 return EFI_SECURITY_VIOLATION
;
1983 // Find out Pkcs7 SignedData which follows the EFI_VARIABLE_AUTHENTICATION_2 descriptor.
1984 // AuthInfo.Hdr.dwLength is the length of the entire certificate, including the length of the header.
1986 SigData
= CertData
->AuthInfo
.CertData
;
1987 SigDataSize
= CertData
->AuthInfo
.Hdr
.dwLength
- (UINT32
) (OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID
, CertData
));
1990 // Find out the new data payload which follows Pkcs7 SignedData directly.
1992 PayloadPtr
= SigData
+ SigDataSize
;
1993 PayloadSize
= DataSize
- OFFSET_OF_AUTHINFO2_CERT_DATA
- (UINTN
) SigDataSize
;
1996 // Construct a buffer to fill with (VariableName, VendorGuid, Attributes, TimeStamp, Data).
1998 NewDataSize
= PayloadSize
+ sizeof (EFI_TIME
) + sizeof (UINT32
) +
1999 sizeof (EFI_GUID
) + StrSize (VariableName
) - sizeof (CHAR16
);
2000 NewData
= mSerializationRuntimeBuffer
;
2003 Length
= StrLen (VariableName
) * sizeof (CHAR16
);
2004 CopyMem (Buffer
, VariableName
, Length
);
2007 Length
= sizeof (EFI_GUID
);
2008 CopyMem (Buffer
, VendorGuid
, Length
);
2011 Length
= sizeof (UINT32
);
2012 CopyMem (Buffer
, &Attr
, Length
);
2015 Length
= sizeof (EFI_TIME
);
2016 CopyMem (Buffer
, &CertData
->TimeStamp
, Length
);
2019 CopyMem (Buffer
, PayloadPtr
, PayloadSize
);
2021 if (AuthVarType
== AuthVarTypePk
) {
2023 // Get platform key from variable.
2025 Status
= FindVariable (
2026 EFI_PLATFORM_KEY_NAME
,
2027 &gEfiGlobalVariableGuid
,
2029 &mVariableModuleGlobal
->VariableGlobal
,
2032 if (EFI_ERROR (Status
)) {
2036 CertList
= (EFI_SIGNATURE_LIST
*) GetVariableDataPtr (PkVariable
.CurrPtr
);
2037 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
2038 RootCert
= Cert
->SignatureData
;
2039 RootCertSize
= CertList
->SignatureSize
- (sizeof (EFI_SIGNATURE_DATA
) - 1);
2043 // Verify Pkcs7 SignedData via Pkcs7Verify library.
2045 VerifyStatus
= Pkcs7Verify (
2054 } else if (AuthVarType
== AuthVarTypeKek
) {
2057 // Get KEK database from variable.
2059 Status
= FindVariable (
2060 EFI_KEY_EXCHANGE_KEY_NAME
,
2061 &gEfiGlobalVariableGuid
,
2063 &mVariableModuleGlobal
->VariableGlobal
,
2066 if (EFI_ERROR (Status
)) {
2071 // Ready to verify Pkcs7 SignedData. Go through KEK Signature Database to find out X.509 CertList.
2073 KekDataSize
= KekVariable
.CurrPtr
->DataSize
;
2074 CertList
= (EFI_SIGNATURE_LIST
*) GetVariableDataPtr (KekVariable
.CurrPtr
);
2075 while ((KekDataSize
> 0) && (KekDataSize
>= CertList
->SignatureListSize
)) {
2076 if (CompareGuid (&CertList
->SignatureType
, &gEfiCertX509Guid
)) {
2077 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
2078 CertCount
= (CertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - CertList
->SignatureHeaderSize
) / CertList
->SignatureSize
;
2079 for (Index
= 0; Index
< CertCount
; Index
++) {
2081 // Iterate each Signature Data Node within this CertList for a verify
2083 RootCert
= Cert
->SignatureData
;
2084 RootCertSize
= CertList
->SignatureSize
- (sizeof (EFI_SIGNATURE_DATA
) - 1);
2087 // Verify Pkcs7 SignedData via Pkcs7Verify library.
2089 VerifyStatus
= Pkcs7Verify (
2100 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) Cert
+ CertList
->SignatureSize
);
2103 KekDataSize
-= CertList
->SignatureListSize
;
2104 CertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) CertList
+ CertList
->SignatureListSize
);
2106 } else if (AuthVarType
== AuthVarTypePriv
) {
2109 // Process common authenticated variable except PK/KEK/DB/DBX.
2110 // Get signer's certificates from SignedData.
2112 VerifyStatus
= Pkcs7GetSigners (
2120 if (!VerifyStatus
) {
2125 // Get previously stored signer's certificates from certdb for existing
2126 // variable. Check whether they are identical with signer's certificates
2127 // in SignedData. If not, return error immediately.
2129 if ((Variable
->CurrPtr
!= NULL
)) {
2130 VerifyStatus
= FALSE
;
2132 Status
= GetCertsFromDb (VariableName
, VendorGuid
, &CertsInCertDb
, &CertsSizeinDb
);
2133 if (EFI_ERROR (Status
)) {
2137 if ((CertStackSize
!= CertsSizeinDb
) ||
2138 (CompareMem (SignerCerts
, CertsInCertDb
, CertsSizeinDb
) != 0)) {
2143 VerifyStatus
= Pkcs7Verify (
2151 if (!VerifyStatus
) {
2156 // Delete signer's certificates when delete the common authenticated variable.
2158 if ((PayloadSize
== 0) && (Variable
->CurrPtr
!= NULL
)) {
2159 Status
= DeleteCertsFromDb (VariableName
, VendorGuid
);
2160 if (EFI_ERROR (Status
)) {
2161 VerifyStatus
= FALSE
;
2164 } else if (Variable
->CurrPtr
== NULL
) {
2166 // Insert signer's certificates when adding a new common authenticated variable.
2168 Status
= InsertCertsToDb (VariableName
, VendorGuid
, SignerCerts
, CertStackSize
);
2169 if (EFI_ERROR (Status
)) {
2170 VerifyStatus
= FALSE
;
2174 } else if (AuthVarType
== AuthVarTypePayload
) {
2175 CertList
= (EFI_SIGNATURE_LIST
*) PayloadPtr
;
2176 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
2177 RootCert
= Cert
->SignatureData
;
2178 RootCertSize
= CertList
->SignatureSize
- (sizeof (EFI_SIGNATURE_DATA
) - 1);
2180 // Verify Pkcs7 SignedData via Pkcs7Verify library.
2182 VerifyStatus
= Pkcs7Verify (
2191 return EFI_SECURITY_VIOLATION
;
2196 if (AuthVarType
== AuthVarTypePriv
) {
2197 Pkcs7FreeSigners (RootCert
);
2198 Pkcs7FreeSigners (SignerCerts
);
2201 if (!VerifyStatus
) {
2202 return EFI_SECURITY_VIOLATION
;
2205 Status
= CheckSignatureListFormat(VariableName
, VendorGuid
, PayloadPtr
, PayloadSize
);
2206 if (EFI_ERROR (Status
)) {
2210 if ((PayloadSize
== 0) && (VarDel
!= NULL
)) {
2215 // Final step: Update/Append Variable if it pass Pkcs7Verify
2217 return UpdateVariable (
2226 &CertData
->TimeStamp