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 // Process Time-based Authenticated variable.
1143 if ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) {
1144 return VerifyTimeBasedPayload (
1157 // Determine if first time SetVariable with the EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS.
1159 if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) {
1161 // Determine current operation type.
1163 if (DataSize
== AUTHINFO_SIZE
) {
1167 // Determine whether this is the first time with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS set.
1169 if (Variable
->CurrPtr
== NULL
) {
1171 } else if ((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) == 0) {
1174 KeyIndex
= Variable
->CurrPtr
->PubKeyIndex
;
1175 IsFirstTime
= FALSE
;
1177 } else if ((Variable
->CurrPtr
!= NULL
) &&
1178 ((Variable
->CurrPtr
->Attributes
& (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
)) != 0)
1181 // If the variable is already write-protected, it always needs authentication before update.
1183 return EFI_WRITE_PROTECTED
;
1186 // If without EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS, set and attributes collision.
1187 // That means it is not authenticated variable, just update variable as usual.
1189 Status
= UpdateVariable (VariableName
, VendorGuid
, Data
, DataSize
, Attributes
, 0, 0, Variable
, NULL
);
1194 // Get PubKey and check Monotonic Count value corresponding to the variable.
1196 CertData
= (EFI_VARIABLE_AUTHENTICATION
*) Data
;
1197 CertBlock
= (EFI_CERT_BLOCK_RSA_2048_SHA256
*) (CertData
->AuthInfo
.CertData
);
1198 PubKey
= CertBlock
->PublicKey
;
1201 // Update Monotonic Count value.
1203 MonotonicCount
= CertData
->MonotonicCount
;
1207 // Check input PubKey.
1209 if (CompareMem (PubKey
, mPubKeyStore
+ (KeyIndex
- 1) * EFI_CERT_TYPE_RSA2048_SIZE
, EFI_CERT_TYPE_RSA2048_SIZE
) != 0) {
1210 return EFI_SECURITY_VIOLATION
;
1213 // Compare the current monotonic count and ensure that it is greater than the last SetVariable
1214 // operation with the EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS attribute set.
1216 if (CertData
->MonotonicCount
<= Variable
->CurrPtr
->MonotonicCount
) {
1218 // Monotonic count check fail, suspicious replay attack, return EFI_SECURITY_VIOLATION.
1220 return EFI_SECURITY_VIOLATION
;
1224 // Verify the certificate in Data payload.
1226 Status
= VerifyCounterBasedPayload (Data
, DataSize
, PubKey
);
1227 if (EFI_ERROR (Status
)) {
1232 // Now, the signature has been verified!
1234 if (IsFirstTime
&& !IsDeletion
) {
1236 // Update public key database variable if need.
1238 KeyIndex
= AddPubKeyInStore (PubKey
);
1239 if (KeyIndex
== 0) {
1240 return EFI_SECURITY_VIOLATION
;
1245 // Verification pass.
1247 return UpdateVariable (VariableName
, VendorGuid
, (UINT8
*)Data
+ AUTHINFO_SIZE
, DataSize
- AUTHINFO_SIZE
, Attributes
, KeyIndex
, MonotonicCount
, Variable
, NULL
);
1251 Merge two buffers which formatted as EFI_SIGNATURE_LIST. Only the new EFI_SIGNATURE_DATA
1252 will be appended to the original EFI_SIGNATURE_LIST, duplicate EFI_SIGNATURE_DATA
1255 @param[in, out] Data Pointer to original EFI_SIGNATURE_LIST.
1256 @param[in] DataSize Size of Data buffer.
1257 @param[in] NewData Pointer to new EFI_SIGNATURE_LIST to be appended.
1258 @param[in] NewDataSize Size of NewData buffer.
1260 @return Size of the merged buffer.
1264 AppendSignatureList (
1268 IN UINTN NewDataSize
1271 EFI_SIGNATURE_LIST
*CertList
;
1272 EFI_SIGNATURE_DATA
*Cert
;
1274 EFI_SIGNATURE_LIST
*NewCertList
;
1275 EFI_SIGNATURE_DATA
*NewCert
;
1282 UINTN SignatureListSize
;
1285 Tail
= (UINT8
*) Data
+ DataSize
;
1287 NewCertList
= (EFI_SIGNATURE_LIST
*) NewData
;
1288 while ((NewDataSize
> 0) && (NewDataSize
>= NewCertList
->SignatureListSize
)) {
1289 NewCert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) NewCertList
+ sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
);
1290 NewCertCount
= (NewCertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - NewCertList
->SignatureHeaderSize
) / NewCertList
->SignatureSize
;
1293 for (Index
= 0; Index
< NewCertCount
; Index
++) {
1297 CertList
= (EFI_SIGNATURE_LIST
*) Data
;
1298 while ((Size
> 0) && (Size
>= CertList
->SignatureListSize
)) {
1299 if (CompareGuid (&CertList
->SignatureType
, &NewCertList
->SignatureType
) &&
1300 (CertList
->SignatureSize
== NewCertList
->SignatureSize
)) {
1301 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
1302 CertCount
= (CertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - CertList
->SignatureHeaderSize
) / CertList
->SignatureSize
;
1303 for (Index2
= 0; Index2
< CertCount
; Index2
++) {
1305 // Iterate each Signature Data in this Signature List.
1307 if (CompareMem (NewCert
, Cert
, CertList
->SignatureSize
) == 0) {
1311 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) Cert
+ CertList
->SignatureSize
);
1318 Size
-= CertList
->SignatureListSize
;
1319 CertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) CertList
+ CertList
->SignatureListSize
);
1324 // New EFI_SIGNATURE_DATA, append it.
1326 if (CopiedCount
== 0) {
1328 // Copy EFI_SIGNATURE_LIST header for only once.
1330 CopyMem (Tail
, NewCertList
, sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
);
1331 Tail
= Tail
+ sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
;
1334 CopyMem (Tail
, NewCert
, NewCertList
->SignatureSize
);
1335 Tail
+= NewCertList
->SignatureSize
;
1339 NewCert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) NewCert
+ NewCertList
->SignatureSize
);
1343 // Update SignatureListSize in newly appended EFI_SIGNATURE_LIST.
1345 if (CopiedCount
!= 0) {
1346 SignatureListSize
= sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
+ (CopiedCount
* NewCertList
->SignatureSize
);
1347 CertList
= (EFI_SIGNATURE_LIST
*) (Tail
- SignatureListSize
);
1348 CertList
->SignatureListSize
= (UINT32
) SignatureListSize
;
1351 NewDataSize
-= NewCertList
->SignatureListSize
;
1352 NewCertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) NewCertList
+ NewCertList
->SignatureListSize
);
1355 return (Tail
- (UINT8
*) Data
);
1359 Compare two EFI_TIME data.
1362 @param FirstTime A pointer to the first EFI_TIME data.
1363 @param SecondTime A pointer to the second EFI_TIME data.
1365 @retval TRUE The FirstTime is not later than the SecondTime.
1366 @retval FALSE The FirstTime is later than the SecondTime.
1371 IN EFI_TIME
*FirstTime
,
1372 IN EFI_TIME
*SecondTime
1375 if (FirstTime
->Year
!= SecondTime
->Year
) {
1376 return (BOOLEAN
) (FirstTime
->Year
< SecondTime
->Year
);
1377 } else if (FirstTime
->Month
!= SecondTime
->Month
) {
1378 return (BOOLEAN
) (FirstTime
->Month
< SecondTime
->Month
);
1379 } else if (FirstTime
->Day
!= SecondTime
->Day
) {
1380 return (BOOLEAN
) (FirstTime
->Day
< SecondTime
->Day
);
1381 } else if (FirstTime
->Hour
!= SecondTime
->Hour
) {
1382 return (BOOLEAN
) (FirstTime
->Hour
< SecondTime
->Hour
);
1383 } else if (FirstTime
->Minute
!= SecondTime
->Minute
) {
1384 return (BOOLEAN
) (FirstTime
->Minute
< FirstTime
->Minute
);
1387 return (BOOLEAN
) (FirstTime
->Second
<= SecondTime
->Second
);
1391 Find matching signer's certificates for common authenticated variable
1392 by corresponding VariableName and VendorGuid from "certdb".
1394 The data format of "certdb":
1396 // UINT32 CertDbListSize;
1397 // /// AUTH_CERT_DB_DATA Certs1[];
1398 // /// AUTH_CERT_DB_DATA Certs2[];
1400 // /// AUTH_CERT_DB_DATA Certsn[];
1403 @param[in] VariableName Name of authenticated Variable.
1404 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1405 @param[in] Data Pointer to variable "certdb".
1406 @param[in] DataSize Size of variable "certdb".
1407 @param[out] CertOffset Offset of matching CertData, from starting of Data.
1408 @param[out] CertDataSize Length of CertData in bytes.
1409 @param[out] CertNodeOffset Offset of matching AUTH_CERT_DB_DATA , from
1411 @param[out] CertNodeSize Length of AUTH_CERT_DB_DATA in bytes.
1413 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1414 @retval EFI_NOT_FOUND Fail to find matching certs.
1415 @retval EFI_SUCCESS Find matching certs and output parameters.
1420 IN CHAR16
*VariableName
,
1421 IN EFI_GUID
*VendorGuid
,
1424 OUT UINT32
*CertOffset
, OPTIONAL
1425 OUT UINT32
*CertDataSize
, OPTIONAL
1426 OUT UINT32
*CertNodeOffset
,OPTIONAL
1427 OUT UINT32
*CertNodeSize OPTIONAL
1431 AUTH_CERT_DB_DATA
*Ptr
;
1435 UINT32 CertDbListSize
;
1437 if ((VariableName
== NULL
) || (VendorGuid
== NULL
) || (Data
== NULL
)) {
1438 return EFI_INVALID_PARAMETER
;
1442 // Check whether DataSize matches recorded CertDbListSize.
1444 if (DataSize
< sizeof (UINT32
)) {
1445 return EFI_INVALID_PARAMETER
;
1448 CertDbListSize
= ReadUnaligned32 ((UINT32
*) Data
);
1450 if (CertDbListSize
!= (UINT32
) DataSize
) {
1451 return EFI_INVALID_PARAMETER
;
1454 Offset
= sizeof (UINT32
);
1457 // Get corresponding certificates by VendorGuid and VariableName.
1459 while (Offset
< (UINT32
) DataSize
) {
1460 Ptr
= (AUTH_CERT_DB_DATA
*) (Data
+ Offset
);
1462 // Check whether VendorGuid matches.
1464 if (CompareGuid (&Ptr
->VendorGuid
, VendorGuid
)) {
1465 NodeSize
= ReadUnaligned32 (&Ptr
->CertNodeSize
);
1466 NameSize
= ReadUnaligned32 (&Ptr
->NameSize
);
1467 CertSize
= ReadUnaligned32 (&Ptr
->CertDataSize
);
1469 if (NodeSize
!= sizeof (EFI_GUID
) + sizeof (UINT32
) * 3 + CertSize
+
1470 sizeof (CHAR16
) * NameSize
) {
1471 return EFI_INVALID_PARAMETER
;
1474 Offset
= Offset
+ sizeof (EFI_GUID
) + sizeof (UINT32
) * 3;
1476 // Check whether VariableName matches.
1478 if ((NameSize
== StrLen (VariableName
)) &&
1479 (CompareMem (Data
+ Offset
, VariableName
, NameSize
* sizeof (CHAR16
)) == 0)) {
1480 Offset
= Offset
+ NameSize
* sizeof (CHAR16
);
1482 if (CertOffset
!= NULL
) {
1483 *CertOffset
= Offset
;
1486 if (CertDataSize
!= NULL
) {
1487 *CertDataSize
= CertSize
;
1490 if (CertNodeOffset
!= NULL
) {
1491 *CertNodeOffset
= (UINT32
) ((UINT8
*) Ptr
- Data
);
1494 if (CertNodeSize
!= NULL
) {
1495 *CertNodeSize
= NodeSize
;
1500 Offset
= Offset
+ NameSize
* sizeof (CHAR16
) + CertSize
;
1503 NodeSize
= ReadUnaligned32 (&Ptr
->CertNodeSize
);
1504 Offset
= Offset
+ NodeSize
;
1508 return EFI_NOT_FOUND
;
1512 Retrieve signer's certificates for common authenticated variable
1513 by corresponding VariableName and VendorGuid from "certdb".
1515 @param[in] VariableName Name of authenticated Variable.
1516 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1517 @param[out] CertData Pointer to signer's certificates.
1518 @param[out] CertDataSize Length of CertData in bytes.
1520 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1521 @retval EFI_NOT_FOUND Fail to find "certdb" or matching certs.
1522 @retval EFI_SUCCESS Get signer's certificates successfully.
1527 IN CHAR16
*VariableName
,
1528 IN EFI_GUID
*VendorGuid
,
1529 OUT UINT8
**CertData
,
1530 OUT UINT32
*CertDataSize
1533 VARIABLE_POINTER_TRACK CertDbVariable
;
1539 if ((VariableName
== NULL
) || (VendorGuid
== NULL
) || (CertData
== NULL
) || (CertDataSize
== NULL
)) {
1540 return EFI_INVALID_PARAMETER
;
1544 // Get variable "certdb".
1546 Status
= FindVariable (
1550 &mVariableModuleGlobal
->VariableGlobal
,
1553 if (EFI_ERROR (Status
)) {
1557 DataSize
= DataSizeOfVariable (CertDbVariable
.CurrPtr
);
1558 Data
= GetVariableDataPtr (CertDbVariable
.CurrPtr
);
1559 if ((DataSize
== 0) || (Data
== NULL
)) {
1561 return EFI_NOT_FOUND
;
1564 Status
= FindCertsFromDb (
1575 if (EFI_ERROR (Status
)) {
1579 *CertData
= Data
+ CertOffset
;
1584 Delete matching signer's certificates when deleting common authenticated
1585 variable by corresponding VariableName and VendorGuid from "certdb".
1587 @param[in] VariableName Name of authenticated Variable.
1588 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1590 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1591 @retval EFI_NOT_FOUND Fail to find "certdb" or matching certs.
1592 @retval EFI_OUT_OF_RESOURCES The operation is failed due to lack of resources.
1593 @retval EFI_SUCCESS The operation is completed successfully.
1598 IN CHAR16
*VariableName
,
1599 IN EFI_GUID
*VendorGuid
1602 VARIABLE_POINTER_TRACK CertDbVariable
;
1607 UINT32 CertNodeOffset
;
1608 UINT32 CertNodeSize
;
1610 UINT32 NewCertDbSize
;
1612 if ((VariableName
== NULL
) || (VendorGuid
== NULL
)) {
1613 return EFI_INVALID_PARAMETER
;
1617 // Get variable "certdb".
1619 Status
= FindVariable (
1623 &mVariableModuleGlobal
->VariableGlobal
,
1626 if (EFI_ERROR (Status
)) {
1630 DataSize
= DataSizeOfVariable (CertDbVariable
.CurrPtr
);
1631 Data
= GetVariableDataPtr (CertDbVariable
.CurrPtr
);
1632 if ((DataSize
== 0) || (Data
== NULL
)) {
1634 return EFI_NOT_FOUND
;
1637 if (DataSize
== sizeof (UINT32
)) {
1639 // There is no certs in certdb.
1645 // Get corresponding cert node from certdb.
1647 Status
= FindCertsFromDb (
1658 if (EFI_ERROR (Status
)) {
1662 if (DataSize
< (CertNodeOffset
+ CertNodeSize
)) {
1663 return EFI_NOT_FOUND
;
1667 // Construct new data content of variable "certdb".
1669 NewCertDbSize
= (UINT32
) DataSize
- CertNodeSize
;
1670 NewCertDb
= (UINT8
*) mCertDbStore
;
1673 // Copy the DB entries before deleting node.
1675 CopyMem (NewCertDb
, Data
, CertNodeOffset
);
1677 // Update CertDbListSize.
1679 CopyMem (NewCertDb
, &NewCertDbSize
, sizeof (UINT32
));
1681 // Copy the DB entries after deleting node.
1683 if (DataSize
> (CertNodeOffset
+ CertNodeSize
)) {
1685 NewCertDb
+ CertNodeOffset
,
1686 Data
+ CertNodeOffset
+ CertNodeSize
,
1687 DataSize
- CertNodeOffset
- CertNodeSize
1694 VarAttr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
1695 Status
= UpdateVariable (
1711 Insert signer's certificates for common authenticated variable with VariableName
1712 and VendorGuid in AUTH_CERT_DB_DATA to "certdb".
1714 @param[in] VariableName Name of authenticated Variable.
1715 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1716 @param[in] CertData Pointer to signer's certificates.
1717 @param[in] CertDataSize Length of CertData in bytes.
1719 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1720 @retval EFI_ACCESS_DENIED An AUTH_CERT_DB_DATA entry with same VariableName
1721 and VendorGuid already exists.
1722 @retval EFI_OUT_OF_RESOURCES The operation is failed due to lack of resources.
1723 @retval EFI_SUCCESS Insert an AUTH_CERT_DB_DATA entry to "certdb"
1728 IN CHAR16
*VariableName
,
1729 IN EFI_GUID
*VendorGuid
,
1731 IN UINTN CertDataSize
1734 VARIABLE_POINTER_TRACK CertDbVariable
;
1740 UINT32 NewCertDbSize
;
1741 UINT32 CertNodeSize
;
1743 AUTH_CERT_DB_DATA
*Ptr
;
1745 if ((VariableName
== NULL
) || (VendorGuid
== NULL
) || (CertData
== NULL
)) {
1746 return EFI_INVALID_PARAMETER
;
1750 // Get variable "certdb".
1752 Status
= FindVariable (
1756 &mVariableModuleGlobal
->VariableGlobal
,
1759 if (EFI_ERROR (Status
)) {
1763 DataSize
= DataSizeOfVariable (CertDbVariable
.CurrPtr
);
1764 Data
= GetVariableDataPtr (CertDbVariable
.CurrPtr
);
1765 if ((DataSize
== 0) || (Data
== NULL
)) {
1767 return EFI_NOT_FOUND
;
1771 // Find whether matching cert node already exists in "certdb".
1772 // If yes return error.
1774 Status
= FindCertsFromDb (
1785 if (!EFI_ERROR (Status
)) {
1787 return EFI_ACCESS_DENIED
;
1791 // Construct new data content of variable "certdb".
1793 NameSize
= (UINT32
) StrLen (VariableName
);
1794 CertNodeSize
= sizeof (AUTH_CERT_DB_DATA
) + (UINT32
) CertDataSize
+ NameSize
* sizeof (CHAR16
);
1795 NewCertDbSize
= (UINT32
) DataSize
+ CertNodeSize
;
1796 if (NewCertDbSize
> MAX_CERTDB_SIZE
) {
1797 return EFI_OUT_OF_RESOURCES
;
1799 NewCertDb
= (UINT8
*) mCertDbStore
;
1802 // Copy the DB entries before deleting node.
1804 CopyMem (NewCertDb
, Data
, DataSize
);
1806 // Update CertDbListSize.
1808 CopyMem (NewCertDb
, &NewCertDbSize
, sizeof (UINT32
));
1810 // Construct new cert node.
1812 Ptr
= (AUTH_CERT_DB_DATA
*) (NewCertDb
+ DataSize
);
1813 CopyGuid (&Ptr
->VendorGuid
, VendorGuid
);
1814 CopyMem (&Ptr
->CertNodeSize
, &CertNodeSize
, sizeof (UINT32
));
1815 CopyMem (&Ptr
->NameSize
, &NameSize
, sizeof (UINT32
));
1816 CopyMem (&Ptr
->CertDataSize
, &CertDataSize
, sizeof (UINT32
));
1819 (UINT8
*) Ptr
+ sizeof (AUTH_CERT_DB_DATA
),
1821 NameSize
* sizeof (CHAR16
)
1825 (UINT8
*) Ptr
+ sizeof (AUTH_CERT_DB_DATA
) + NameSize
* sizeof (CHAR16
),
1833 VarAttr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
1834 Status
= UpdateVariable (
1850 Process variable with EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set
1852 Caution: This function may receive untrusted input.
1853 This function may be invoked in SMM mode, and datasize and data are external input.
1854 This function will do basic validation, before parse the data.
1855 This function will parse the authentication carefully to avoid security issues, like
1856 buffer overflow, integer overflow.
1858 @param[in] VariableName Name of Variable to be found.
1859 @param[in] VendorGuid Variable vendor GUID.
1860 @param[in] Data Data pointer.
1861 @param[in] DataSize Size of Data found. If size is less than the
1862 data, this value contains the required size.
1863 @param[in] Variable The variable information which is used to keep track of variable usage.
1864 @param[in] Attributes Attribute value of the variable.
1865 @param[in] AuthVarType Verify against PK, KEK database, private database or certificate in data payload.
1866 @param[out] VarDel Delete the variable or not.
1868 @retval EFI_INVALID_PARAMETER Invalid parameter.
1869 @retval EFI_SECURITY_VIOLATION The variable does NOT pass the validation
1870 check carried out by the firmware.
1871 @retval EFI_OUT_OF_RESOURCES Failed to process variable due to lack
1873 @retval EFI_SUCCESS Variable pass validation successfully.
1877 VerifyTimeBasedPayload (
1878 IN CHAR16
*VariableName
,
1879 IN EFI_GUID
*VendorGuid
,
1882 IN VARIABLE_POINTER_TRACK
*Variable
,
1883 IN UINT32 Attributes
,
1884 IN AUTHVAR_TYPE AuthVarType
,
1898 BOOLEAN VerifyStatus
;
1900 EFI_SIGNATURE_LIST
*CertList
;
1901 EFI_SIGNATURE_DATA
*Cert
;
1902 VARIABLE_POINTER_TRACK KekVariable
;
1903 EFI_VARIABLE_AUTHENTICATION_2
*CertData
;
1906 VARIABLE_POINTER_TRACK PkVariable
;
1911 UINTN CertStackSize
;
1912 UINT8
*CertsInCertDb
;
1913 UINT32 CertsSizeinDb
;
1915 VerifyStatus
= FALSE
;
1924 // When the attribute EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS is
1925 // set, then the Data buffer shall begin with an instance of a complete (and serialized)
1926 // EFI_VARIABLE_AUTHENTICATION_2 descriptor. The descriptor shall be followed by the new
1927 // variable value and DataSize shall reflect the combined size of the descriptor and the new
1928 // variable value. The authentication descriptor is not part of the variable data and is not
1929 // returned by subsequent calls to GetVariable().
1931 CertData
= (EFI_VARIABLE_AUTHENTICATION_2
*) Data
;
1934 // Verify that Pad1, Nanosecond, TimeZone, Daylight and Pad2 components of the
1935 // TimeStamp value are set to zero.
1937 if ((CertData
->TimeStamp
.Pad1
!= 0) ||
1938 (CertData
->TimeStamp
.Nanosecond
!= 0) ||
1939 (CertData
->TimeStamp
.TimeZone
!= 0) ||
1940 (CertData
->TimeStamp
.Daylight
!= 0) ||
1941 (CertData
->TimeStamp
.Pad2
!= 0)) {
1942 return EFI_SECURITY_VIOLATION
;
1945 if ((Variable
->CurrPtr
!= NULL
) && ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0)) {
1946 if (CompareTimeStamp (&CertData
->TimeStamp
, &Variable
->CurrPtr
->TimeStamp
)) {
1948 // TimeStamp check fail, suspicious replay attack, return EFI_SECURITY_VIOLATION.
1950 return EFI_SECURITY_VIOLATION
;
1955 // wCertificateType should be WIN_CERT_TYPE_EFI_GUID.
1956 // Cert type should be EFI_CERT_TYPE_PKCS7_GUID.
1958 if ((CertData
->AuthInfo
.Hdr
.wCertificateType
!= WIN_CERT_TYPE_EFI_GUID
) ||
1959 !CompareGuid (&CertData
->AuthInfo
.CertType
, &gEfiCertPkcs7Guid
)) {
1961 // Invalid AuthInfo type, return EFI_SECURITY_VIOLATION.
1963 return EFI_SECURITY_VIOLATION
;
1967 // Find out Pkcs7 SignedData which follows the EFI_VARIABLE_AUTHENTICATION_2 descriptor.
1968 // AuthInfo.Hdr.dwLength is the length of the entire certificate, including the length of the header.
1970 SigData
= CertData
->AuthInfo
.CertData
;
1971 SigDataSize
= CertData
->AuthInfo
.Hdr
.dwLength
- (UINT32
) (OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID
, CertData
));
1974 // Find out the new data payload which follows Pkcs7 SignedData directly.
1976 PayloadPtr
= SigData
+ SigDataSize
;
1977 PayloadSize
= DataSize
- OFFSET_OF_AUTHINFO2_CERT_DATA
- (UINTN
) SigDataSize
;
1980 // Construct a buffer to fill with (VariableName, VendorGuid, Attributes, TimeStamp, Data).
1982 NewDataSize
= PayloadSize
+ sizeof (EFI_TIME
) + sizeof (UINT32
) +
1983 sizeof (EFI_GUID
) + StrSize (VariableName
) - sizeof (CHAR16
);
1984 NewData
= mSerializationRuntimeBuffer
;
1987 Length
= StrLen (VariableName
) * sizeof (CHAR16
);
1988 CopyMem (Buffer
, VariableName
, Length
);
1991 Length
= sizeof (EFI_GUID
);
1992 CopyMem (Buffer
, VendorGuid
, Length
);
1995 Length
= sizeof (UINT32
);
1996 CopyMem (Buffer
, &Attr
, Length
);
1999 Length
= sizeof (EFI_TIME
);
2000 CopyMem (Buffer
, &CertData
->TimeStamp
, Length
);
2003 CopyMem (Buffer
, PayloadPtr
, PayloadSize
);
2005 if (AuthVarType
== AuthVarTypePk
) {
2007 // Get platform key from variable.
2009 Status
= FindVariable (
2010 EFI_PLATFORM_KEY_NAME
,
2011 &gEfiGlobalVariableGuid
,
2013 &mVariableModuleGlobal
->VariableGlobal
,
2016 if (EFI_ERROR (Status
)) {
2020 CertList
= (EFI_SIGNATURE_LIST
*) GetVariableDataPtr (PkVariable
.CurrPtr
);
2021 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
2022 RootCert
= Cert
->SignatureData
;
2023 RootCertSize
= CertList
->SignatureSize
- (sizeof (EFI_SIGNATURE_DATA
) - 1);
2027 // Verify Pkcs7 SignedData via Pkcs7Verify library.
2029 VerifyStatus
= Pkcs7Verify (
2038 } else if (AuthVarType
== AuthVarTypeKek
) {
2041 // Get KEK database from variable.
2043 Status
= FindVariable (
2044 EFI_KEY_EXCHANGE_KEY_NAME
,
2045 &gEfiGlobalVariableGuid
,
2047 &mVariableModuleGlobal
->VariableGlobal
,
2050 if (EFI_ERROR (Status
)) {
2055 // Ready to verify Pkcs7 SignedData. Go through KEK Signature Database to find out X.509 CertList.
2057 KekDataSize
= KekVariable
.CurrPtr
->DataSize
;
2058 CertList
= (EFI_SIGNATURE_LIST
*) GetVariableDataPtr (KekVariable
.CurrPtr
);
2059 while ((KekDataSize
> 0) && (KekDataSize
>= CertList
->SignatureListSize
)) {
2060 if (CompareGuid (&CertList
->SignatureType
, &gEfiCertX509Guid
)) {
2061 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
2062 CertCount
= (CertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - CertList
->SignatureHeaderSize
) / CertList
->SignatureSize
;
2063 for (Index
= 0; Index
< CertCount
; Index
++) {
2065 // Iterate each Signature Data Node within this CertList for a verify
2067 RootCert
= Cert
->SignatureData
;
2068 RootCertSize
= CertList
->SignatureSize
- (sizeof (EFI_SIGNATURE_DATA
) - 1);
2071 // Verify Pkcs7 SignedData via Pkcs7Verify library.
2073 VerifyStatus
= Pkcs7Verify (
2084 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) Cert
+ CertList
->SignatureSize
);
2087 KekDataSize
-= CertList
->SignatureListSize
;
2088 CertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) CertList
+ CertList
->SignatureListSize
);
2090 } else if (AuthVarType
== AuthVarTypePriv
) {
2093 // Process common authenticated variable except PK/KEK/DB/DBX.
2094 // Get signer's certificates from SignedData.
2096 VerifyStatus
= Pkcs7GetSigners (
2104 if (!VerifyStatus
) {
2109 // Get previously stored signer's certificates from certdb for existing
2110 // variable. Check whether they are identical with signer's certificates
2111 // in SignedData. If not, return error immediately.
2113 if ((Variable
->CurrPtr
!= NULL
)) {
2114 VerifyStatus
= FALSE
;
2116 Status
= GetCertsFromDb (VariableName
, VendorGuid
, &CertsInCertDb
, &CertsSizeinDb
);
2117 if (EFI_ERROR (Status
)) {
2121 if ((CertStackSize
!= CertsSizeinDb
) ||
2122 (CompareMem (SignerCerts
, CertsInCertDb
, CertsSizeinDb
) != 0)) {
2127 VerifyStatus
= Pkcs7Verify (
2135 if (!VerifyStatus
) {
2140 // Delete signer's certificates when delete the common authenticated variable.
2142 if ((PayloadSize
== 0) && (Variable
->CurrPtr
!= NULL
)) {
2143 Status
= DeleteCertsFromDb (VariableName
, VendorGuid
);
2144 if (EFI_ERROR (Status
)) {
2145 VerifyStatus
= FALSE
;
2148 } else if (Variable
->CurrPtr
== NULL
) {
2150 // Insert signer's certificates when adding a new common authenticated variable.
2152 Status
= InsertCertsToDb (VariableName
, VendorGuid
, SignerCerts
, CertStackSize
);
2153 if (EFI_ERROR (Status
)) {
2154 VerifyStatus
= FALSE
;
2158 } else if (AuthVarType
== AuthVarTypePayload
) {
2159 CertList
= (EFI_SIGNATURE_LIST
*) PayloadPtr
;
2160 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
2161 RootCert
= Cert
->SignatureData
;
2162 RootCertSize
= CertList
->SignatureSize
- (sizeof (EFI_SIGNATURE_DATA
) - 1);
2164 // Verify Pkcs7 SignedData via Pkcs7Verify library.
2166 VerifyStatus
= Pkcs7Verify (
2175 return EFI_SECURITY_VIOLATION
;
2180 if (AuthVarType
== AuthVarTypePriv
) {
2181 Pkcs7FreeSigners (RootCert
);
2182 Pkcs7FreeSigners (SignerCerts
);
2185 if (!VerifyStatus
) {
2186 return EFI_SECURITY_VIOLATION
;
2189 Status
= CheckSignatureListFormat(VariableName
, VendorGuid
, PayloadPtr
, PayloadSize
);
2190 if (EFI_ERROR (Status
)) {
2194 if ((PayloadSize
== 0) && (VarDel
!= NULL
)) {
2199 // Final step: Update/Append Variable if it pass Pkcs7Verify
2201 return UpdateVariable (
2210 &CertData
->TimeStamp