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
];
38 EFI_GUID mSignatureSupport
[] = {EFI_CERT_SHA1_GUID
, EFI_CERT_SHA256_GUID
, EFI_CERT_RSA2048_GUID
, EFI_CERT_X509_GUID
};
40 // Public Exponent of RSA Key.
42 CONST UINT8 mRsaE
[] = { 0x01, 0x00, 0x01 };
44 // Hash context pointer
46 VOID
*mHashCtx
= NULL
;
49 // Pointer to runtime buffer.
50 // For "Append" operation to an existing variable, a read/modify/write operation
51 // is supported by firmware internally. Reserve runtime buffer to cache previous
52 // variable data in runtime phase because memory allocation is forbidden in virtual mode.
54 VOID
*mStorageArea
= NULL
;
57 // The serialization of the values of the VariableName, VendorGuid and Attributes
58 // parameters of the SetVariable() call and the TimeStamp component of the
59 // EFI_VARIABLE_AUTHENTICATION_2 descriptor followed by the variable's new value
60 // i.e. (VariableName, VendorGuid, Attributes, TimeStamp, Data)
62 UINT8
*mSerializationRuntimeBuffer
= NULL
;
65 // Requirement for different signature type which have been defined in UEFI spec.
66 // These data are used to peform SignatureList format check while setting PK/KEK variable.
68 EFI_SIGNATURE_ITEM mSupportSigItem
[] = {
69 //{SigType, SigHeaderSize, SigDataSize }
70 {EFI_CERT_SHA256_GUID
, 0, 32 },
71 {EFI_CERT_RSA2048_GUID
, 0, 256 },
72 {EFI_CERT_RSA2048_SHA256_GUID
, 0, 256 },
73 {EFI_CERT_SHA1_GUID
, 0, 20 },
74 {EFI_CERT_RSA2048_SHA1_GUID
, 0, 256 },
75 {EFI_CERT_X509_GUID
, 0, ((UINT32
) ~0)},
76 {EFI_CERT_SHA224_GUID
, 0, 28 },
77 {EFI_CERT_SHA384_GUID
, 0, 48 },
78 {EFI_CERT_SHA512_GUID
, 0, 64 }
82 Determine whether this operation needs a physical present user.
84 @param[in] VariableName Name of the Variable.
85 @param[in] VendorGuid GUID of the Variable.
87 @retval TRUE This variable is protected, only a physical present user could set this variable.
88 @retval FALSE This variable is not protected.
92 NeedPhysicallyPresent(
93 IN CHAR16
*VariableName
,
94 IN EFI_GUID
*VendorGuid
97 if ((CompareGuid (VendorGuid
, &gEfiSecureBootEnableDisableGuid
) && (StrCmp (VariableName
, EFI_SECURE_BOOT_ENABLE_NAME
) == 0))
98 || (CompareGuid (VendorGuid
, &gEfiCustomModeEnableGuid
) && (StrCmp (VariableName
, EFI_CUSTOM_MODE_NAME
) == 0))) {
106 Determine whether the platform is operating in Custom Secure Boot mode.
108 @retval TRUE The platform is operating in Custom mode.
109 @retval FALSE The platform is operating in Standard mode.
117 VARIABLE_POINTER_TRACK Variable
;
119 FindVariable (EFI_CUSTOM_MODE_NAME
, &gEfiCustomModeEnableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
120 if (Variable
.CurrPtr
!= NULL
&& *(GetVariableDataPtr (Variable
.CurrPtr
)) == CUSTOM_SECURE_BOOT_MODE
) {
129 Internal function to delete a Variable given its name and GUID, no authentication
132 @param[in] VariableName Name of the Variable.
133 @param[in] VendorGuid GUID of the Variable.
135 @retval EFI_SUCCESS Variable deleted successfully.
136 @retval Others The driver failded to start the device.
141 IN CHAR16
*VariableName
,
142 IN EFI_GUID
*VendorGuid
146 VARIABLE_POINTER_TRACK Variable
;
148 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
149 if (EFI_ERROR (Status
)) {
153 ASSERT (Variable
.CurrPtr
!= NULL
);
154 return UpdateVariable (VariableName
, VendorGuid
, NULL
, 0, 0, 0, 0, &Variable
, NULL
);
158 Initializes for authenticated varibale service.
160 @retval EFI_SUCCESS Function successfully executed.
161 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resources.
165 AutenticatedVariableServiceInitialize (
170 VARIABLE_POINTER_TRACK Variable
;
171 VARIABLE_POINTER_TRACK PkVariable
;
177 UINT8 SecureBootMode
;
178 UINT8 SecureBootEnable
;
183 // Initialize hash context.
185 CtxSize
= Sha256GetContextSize ();
186 mHashCtx
= AllocateRuntimePool (CtxSize
);
187 if (mHashCtx
== NULL
) {
188 return EFI_OUT_OF_RESOURCES
;
192 // Reserved runtime buffer for "Append" operation in virtual mode.
194 mStorageArea
= AllocateRuntimePool (PcdGet32 (PcdMaxVariableSize
));
195 if (mStorageArea
== NULL
) {
196 return EFI_OUT_OF_RESOURCES
;
200 // Prepare runtime buffer for serialized data of time-based authenticated
201 // Variable, i.e. (VariableName, VendorGuid, Attributes, TimeStamp, Data).
203 mSerializationRuntimeBuffer
= AllocateRuntimePool (PcdGet32 (PcdMaxVariableSize
) + sizeof (EFI_GUID
) + sizeof (UINT32
) + sizeof (EFI_TIME
));
204 if (mSerializationRuntimeBuffer
== NULL
) {
205 return EFI_OUT_OF_RESOURCES
;
209 // Check "AuthVarKeyDatabase" variable's existence.
210 // If it doesn't exist, create a new one with initial value of 0 and EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS set.
212 Status
= FindVariable (
214 &gEfiAuthenticatedVariableGuid
,
216 &mVariableModuleGlobal
->VariableGlobal
,
220 if (Variable
.CurrPtr
== NULL
) {
221 VarAttr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
;
224 Status
= UpdateVariable (
226 &gEfiAuthenticatedVariableGuid
,
235 if (EFI_ERROR (Status
)) {
240 // Load database in global variable for cache.
242 DataSize
= DataSizeOfVariable (Variable
.CurrPtr
);
243 Data
= GetVariableDataPtr (Variable
.CurrPtr
);
244 ASSERT ((DataSize
!= 0) && (Data
!= NULL
));
245 CopyMem (mPubKeyStore
, (UINT8
*) Data
, DataSize
);
246 mPubKeyNumber
= (UINT32
) (DataSize
/ EFI_CERT_TYPE_RSA2048_SIZE
);
249 FindVariable (EFI_PLATFORM_KEY_NAME
, &gEfiGlobalVariableGuid
, &PkVariable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
250 if (PkVariable
.CurrPtr
== NULL
) {
251 DEBUG ((EFI_D_INFO
, "Variable %s does not exist.\n", EFI_PLATFORM_KEY_NAME
));
253 DEBUG ((EFI_D_INFO
, "Variable %s exists.\n", EFI_PLATFORM_KEY_NAME
));
257 // Create "SetupMode" varable with BS+RT attribute set.
259 FindVariable (EFI_SETUP_MODE_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
260 if (PkVariable
.CurrPtr
== NULL
) {
261 mPlatformMode
= SETUP_MODE
;
263 mPlatformMode
= USER_MODE
;
265 Status
= UpdateVariable (
267 &gEfiGlobalVariableGuid
,
270 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
276 if (EFI_ERROR (Status
)) {
281 // Create "SignatureSupport" varable with BS+RT attribute set.
283 FindVariable (EFI_SIGNATURE_SUPPORT_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
284 Status
= UpdateVariable (
285 EFI_SIGNATURE_SUPPORT_NAME
,
286 &gEfiGlobalVariableGuid
,
288 sizeof(mSignatureSupport
),
289 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
295 if (EFI_ERROR (Status
)) {
300 // If "SecureBootEnable" variable exists, then update "SecureBoot" variable.
301 // If "SecureBootEnable" variable is SECURE_BOOT_ENABLE and in USER_MODE, Set "SecureBoot" variable to SECURE_BOOT_MODE_ENABLE.
302 // If "SecureBootEnable" variable is SECURE_BOOT_DISABLE, Set "SecureBoot" variable to SECURE_BOOT_MODE_DISABLE.
304 SecureBootEnable
= SECURE_BOOT_DISABLE
;
305 FindVariable (EFI_SECURE_BOOT_ENABLE_NAME
, &gEfiSecureBootEnableDisableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
306 if (Variable
.CurrPtr
!= NULL
) {
307 SecureBootEnable
= *(GetVariableDataPtr (Variable
.CurrPtr
));
308 } else if (mPlatformMode
== USER_MODE
) {
310 // "SecureBootEnable" not exist, initialize it in USER_MODE.
312 SecureBootEnable
= SECURE_BOOT_ENABLE
;
313 Status
= UpdateVariable (
314 EFI_SECURE_BOOT_ENABLE_NAME
,
315 &gEfiSecureBootEnableDisableGuid
,
318 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
324 if (EFI_ERROR (Status
)) {
330 // Create "SecureBoot" varable with BS+RT attribute set.
332 if (SecureBootEnable
== SECURE_BOOT_ENABLE
&& mPlatformMode
== USER_MODE
) {
333 SecureBootMode
= SECURE_BOOT_MODE_ENABLE
;
335 SecureBootMode
= SECURE_BOOT_MODE_DISABLE
;
337 FindVariable (EFI_SECURE_BOOT_MODE_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
338 Status
= UpdateVariable (
339 EFI_SECURE_BOOT_MODE_NAME
,
340 &gEfiGlobalVariableGuid
,
343 EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
349 if (EFI_ERROR (Status
)) {
353 DEBUG ((EFI_D_INFO
, "Variable %s is %x\n", EFI_SETUP_MODE_NAME
, mPlatformMode
));
354 DEBUG ((EFI_D_INFO
, "Variable %s is %x\n", EFI_SECURE_BOOT_MODE_NAME
, SecureBootMode
));
355 DEBUG ((EFI_D_INFO
, "Variable %s is %x\n", EFI_SECURE_BOOT_ENABLE_NAME
, SecureBootEnable
));
358 // Check "CustomMode" variable's existence.
360 FindVariable (EFI_CUSTOM_MODE_NAME
, &gEfiCustomModeEnableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
361 if (Variable
.CurrPtr
!= NULL
) {
362 CustomMode
= *(GetVariableDataPtr (Variable
.CurrPtr
));
365 // "CustomMode" not exist, initialize it in STANDARD_SECURE_BOOT_MODE.
367 CustomMode
= STANDARD_SECURE_BOOT_MODE
;
368 Status
= UpdateVariable (
369 EFI_CUSTOM_MODE_NAME
,
370 &gEfiCustomModeEnableGuid
,
373 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
379 if (EFI_ERROR (Status
)) {
384 DEBUG ((EFI_D_INFO
, "Variable %s is %x\n", EFI_CUSTOM_MODE_NAME
, CustomMode
));
387 // Check "certdb" variable's existence.
388 // If it doesn't exist, then create a new one with
389 // EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set.
391 Status
= FindVariable (
395 &mVariableModuleGlobal
->VariableGlobal
,
399 if (Variable
.CurrPtr
== NULL
) {
400 VarAttr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
402 Status
= UpdateVariable (
420 Add public key in store and return its index.
422 @param[in] PubKey Input pointer to Public Key data
424 @return Index of new added item
435 VARIABLE_POINTER_TRACK Variable
;
438 if (PubKey
== NULL
) {
442 Status
= FindVariable (
444 &gEfiAuthenticatedVariableGuid
,
446 &mVariableModuleGlobal
->VariableGlobal
,
449 ASSERT_EFI_ERROR (Status
);
451 // Check whether the public key entry does exist.
454 for (Ptr
= mPubKeyStore
, Index
= 1; Index
<= mPubKeyNumber
; Index
++) {
455 if (CompareMem (Ptr
, PubKey
, EFI_CERT_TYPE_RSA2048_SIZE
) == 0) {
459 Ptr
+= EFI_CERT_TYPE_RSA2048_SIZE
;
464 // Add public key in database.
466 if (mPubKeyNumber
== MAX_KEY_NUM
) {
468 // Notes: Database is full, need enhancement here, currently just return 0.
473 CopyMem (mPubKeyStore
+ mPubKeyNumber
* EFI_CERT_TYPE_RSA2048_SIZE
, PubKey
, EFI_CERT_TYPE_RSA2048_SIZE
);
474 Index
= ++mPubKeyNumber
;
476 // Update public key database variable.
478 Status
= UpdateVariable (
480 &gEfiAuthenticatedVariableGuid
,
482 mPubKeyNumber
* EFI_CERT_TYPE_RSA2048_SIZE
,
483 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
,
489 ASSERT_EFI_ERROR (Status
);
496 Verify data payload with AuthInfo in EFI_CERT_TYPE_RSA2048_SHA256_GUID type.
497 Follow the steps in UEFI2.2.
499 Caution: This function may receive untrusted input.
500 This function may be invoked in SMM mode, and datasize and data are external input.
501 This function will do basic validation, before parse the data.
502 This function will parse the authentication carefully to avoid security issues, like
503 buffer overflow, integer overflow.
505 @param[in] Data Pointer to data with AuthInfo.
506 @param[in] DataSize Size of Data.
507 @param[in] PubKey Public key used for verification.
509 @retval EFI_INVALID_PARAMETER Invalid parameter.
510 @retval EFI_SECURITY_VIOLATION If authentication failed.
511 @retval EFI_SUCCESS Authentication successful.
515 VerifyCounterBasedPayload (
522 EFI_VARIABLE_AUTHENTICATION
*CertData
;
523 EFI_CERT_BLOCK_RSA_2048_SHA256
*CertBlock
;
524 UINT8 Digest
[SHA256_DIGEST_SIZE
];
531 if (Data
== NULL
|| PubKey
== NULL
) {
532 return EFI_INVALID_PARAMETER
;
535 CertData
= (EFI_VARIABLE_AUTHENTICATION
*) Data
;
536 CertBlock
= (EFI_CERT_BLOCK_RSA_2048_SHA256
*) (CertData
->AuthInfo
.CertData
);
539 // wCertificateType should be WIN_CERT_TYPE_EFI_GUID.
540 // Cert type should be EFI_CERT_TYPE_RSA2048_SHA256_GUID.
542 if ((CertData
->AuthInfo
.Hdr
.wCertificateType
!= WIN_CERT_TYPE_EFI_GUID
) ||
543 !CompareGuid (&CertData
->AuthInfo
.CertType
, &gEfiCertTypeRsa2048Sha256Guid
)
546 // Invalid AuthInfo type, return EFI_SECURITY_VIOLATION.
548 return EFI_SECURITY_VIOLATION
;
551 // Hash data payload with SHA256.
553 ZeroMem (Digest
, SHA256_DIGEST_SIZE
);
554 Status
= Sha256Init (mHashCtx
);
558 Status
= Sha256Update (mHashCtx
, Data
+ AUTHINFO_SIZE
, (UINTN
) (DataSize
- AUTHINFO_SIZE
));
563 // Hash Monotonic Count.
565 Status
= Sha256Update (mHashCtx
, &CertData
->MonotonicCount
, sizeof (UINT64
));
569 Status
= Sha256Final (mHashCtx
, Digest
);
574 // Generate & Initialize RSA Context.
577 ASSERT (Rsa
!= NULL
);
579 // Set RSA Key Components.
580 // NOTE: Only N and E are needed to be set as RSA public key for signature verification.
582 Status
= RsaSetKey (Rsa
, RsaKeyN
, PubKey
, EFI_CERT_TYPE_RSA2048_SIZE
);
586 Status
= RsaSetKey (Rsa
, RsaKeyE
, mRsaE
, sizeof (mRsaE
));
591 // Verify the signature.
593 Status
= RsaPkcs1Verify (
597 CertBlock
->Signature
,
598 EFI_CERT_TYPE_RSA2048_SHA256_SIZE
608 return EFI_SECURITY_VIOLATION
;
613 Update platform mode.
615 @param[in] Mode SETUP_MODE or USER_MODE.
617 @return EFI_INVALID_PARAMETER Invalid parameter.
618 @return EFI_SUCCESS Update platform mode successfully.
627 VARIABLE_POINTER_TRACK Variable
;
629 UINT8 SecureBootMode
;
630 UINT8 SecureBootEnable
;
631 UINTN VariableDataSize
;
633 Status
= FindVariable (
635 &gEfiGlobalVariableGuid
,
637 &mVariableModuleGlobal
->VariableGlobal
,
640 if (EFI_ERROR (Status
)) {
645 // Update the value of SetupMode variable by a simple mem copy, this could avoid possible
646 // variable storage reclaim at runtime.
648 mPlatformMode
= (UINT8
) Mode
;
649 CopyMem (GetVariableDataPtr (Variable
.CurrPtr
), &mPlatformMode
, sizeof(UINT8
));
653 // SecureBoot Variable indicates whether the platform firmware is operating
654 // in Secure boot mode (1) or not (0), so we should not change SecureBoot
655 // Variable in runtime.
661 // Check "SecureBoot" variable's existence.
662 // If it doesn't exist, firmware has no capability to perform driver signing verification,
663 // then set "SecureBoot" to 0.
665 Status
= FindVariable (
666 EFI_SECURE_BOOT_MODE_NAME
,
667 &gEfiGlobalVariableGuid
,
669 &mVariableModuleGlobal
->VariableGlobal
,
673 // If "SecureBoot" variable exists, then check "SetupMode" variable update.
674 // If "SetupMode" variable is USER_MODE, "SecureBoot" variable is set to 1.
675 // If "SetupMode" variable is SETUP_MODE, "SecureBoot" variable is set to 0.
677 if (Variable
.CurrPtr
== NULL
) {
678 SecureBootMode
= SECURE_BOOT_MODE_DISABLE
;
680 if (mPlatformMode
== USER_MODE
) {
681 SecureBootMode
= SECURE_BOOT_MODE_ENABLE
;
682 } else if (mPlatformMode
== SETUP_MODE
) {
683 SecureBootMode
= SECURE_BOOT_MODE_DISABLE
;
685 return EFI_NOT_FOUND
;
689 VarAttr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
;
690 Status
= UpdateVariable (
691 EFI_SECURE_BOOT_MODE_NAME
,
692 &gEfiGlobalVariableGuid
,
701 if (EFI_ERROR (Status
)) {
706 // Check "SecureBootEnable" variable's existence. It can enable/disable secure boot feature.
708 Status
= FindVariable (
709 EFI_SECURE_BOOT_ENABLE_NAME
,
710 &gEfiSecureBootEnableDisableGuid
,
712 &mVariableModuleGlobal
->VariableGlobal
,
716 if (SecureBootMode
== SECURE_BOOT_MODE_ENABLE
) {
718 // Create the "SecureBootEnable" variable as secure boot is enabled.
720 SecureBootEnable
= SECURE_BOOT_ENABLE
;
721 VariableDataSize
= sizeof (SecureBootEnable
);
724 // Delete the "SecureBootEnable" variable if this variable exist as "SecureBoot"
725 // variable is not in secure boot state.
727 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
730 SecureBootEnable
= SECURE_BOOT_DISABLE
;
731 VariableDataSize
= 0;
734 Status
= UpdateVariable (
735 EFI_SECURE_BOOT_ENABLE_NAME
,
736 &gEfiSecureBootEnableDisableGuid
,
739 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
749 Check input data form to make sure it is a valid EFI_SIGNATURE_LIST for PK/KEK/db/dbx variable.
751 @param[in] VariableName Name of Variable to be check.
752 @param[in] VendorGuid Variable vendor GUID.
753 @param[in] Data Point to the variable data to be checked.
754 @param[in] DataSize Size of Data.
756 @return EFI_INVALID_PARAMETER Invalid signature list format.
757 @return EFI_SUCCESS Passed signature list format check successfully.
761 CheckSignatureListFormat(
762 IN CHAR16
*VariableName
,
763 IN EFI_GUID
*VendorGuid
,
768 EFI_SIGNATURE_LIST
*SigList
;
774 EFI_SIGNATURE_DATA
*CertData
;
781 ASSERT (VariableName
!= NULL
&& VendorGuid
!= NULL
&& Data
!= NULL
);
783 if (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, EFI_PLATFORM_KEY_NAME
) == 0)){
785 } else if ((CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && StrCmp (VariableName
, EFI_KEY_EXCHANGE_KEY_NAME
) == 0) ||
786 (CompareGuid (VendorGuid
, &gEfiImageSecurityDatabaseGuid
) &&
787 (StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE
) == 0 || StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE1
) == 0))){
794 SigList
= (EFI_SIGNATURE_LIST
*) Data
;
795 SigDataSize
= DataSize
;
799 // Walk throuth the input signature list and check the data format.
800 // If any signature is incorrectly formed, the whole check will fail.
802 while ((SigDataSize
> 0) && (SigDataSize
>= SigList
->SignatureListSize
)) {
803 for (Index
= 0; Index
< (sizeof (mSupportSigItem
) / sizeof (EFI_SIGNATURE_ITEM
)); Index
++ ) {
804 if (CompareGuid (&SigList
->SignatureType
, &mSupportSigItem
[Index
].SigType
)) {
806 // The value of SignatureSize should always be 16 (size of SignatureOwner
807 // component) add the data length according to signature type.
809 if (mSupportSigItem
[Index
].SigDataSize
!= ((UINT32
) ~0) &&
810 (SigList
->SignatureSize
- sizeof (EFI_GUID
)) != mSupportSigItem
[Index
].SigDataSize
) {
811 return EFI_INVALID_PARAMETER
;
813 if (mSupportSigItem
[Index
].SigHeaderSize
!= ((UINTN
) ~0) &&
814 SigList
->SignatureHeaderSize
!= mSupportSigItem
[Index
].SigHeaderSize
) {
815 return EFI_INVALID_PARAMETER
;
821 if (Index
== (sizeof (mSupportSigItem
) / sizeof (EFI_SIGNATURE_ITEM
))) {
823 // Undefined signature type.
825 return EFI_INVALID_PARAMETER
;
828 if (CompareGuid (&SigList
->SignatureType
, &gEfiCertX509Guid
)) {
830 // Try to retrieve the RSA public key from the X.509 certificate.
831 // If this operation fails, it's not a valid certificate.
833 RsaContext
= RsaNew ();
834 if (RsaContext
== NULL
) {
835 return EFI_INVALID_PARAMETER
;
837 CertData
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) SigList
+ sizeof (EFI_SIGNATURE_LIST
) + SigList
->SignatureHeaderSize
);
838 CertLen
= SigList
->SignatureSize
- sizeof (EFI_GUID
);
839 if (!RsaGetPublicKeyFromX509 (CertData
->SignatureData
, CertLen
, &RsaContext
)) {
840 RsaFree (RsaContext
);
841 return EFI_INVALID_PARAMETER
;
843 RsaFree (RsaContext
);
846 if ((SigList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - SigList
->SignatureHeaderSize
) % SigList
->SignatureSize
!= 0) {
847 return EFI_INVALID_PARAMETER
;
849 SigCount
+= (SigList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - SigList
->SignatureHeaderSize
) / SigList
->SignatureSize
;
851 SigDataSize
-= SigList
->SignatureListSize
;
852 SigList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) SigList
+ SigList
->SignatureListSize
);
855 if (((UINTN
) SigList
- (UINTN
) Data
) != DataSize
) {
856 return EFI_INVALID_PARAMETER
;
859 if (IsPk
&& SigCount
> 1) {
860 return EFI_INVALID_PARAMETER
;
867 Process variable with platform key for verification.
869 Caution: This function may receive untrusted input.
870 This function may be invoked in SMM mode, and datasize and data are external input.
871 This function will do basic validation, before parse the data.
872 This function will parse the authentication carefully to avoid security issues, like
873 buffer overflow, integer overflow.
874 This function will check attribute carefully to avoid authentication bypass.
876 @param[in] VariableName Name of Variable to be found.
877 @param[in] VendorGuid Variable vendor GUID.
878 @param[in] Data Data pointer.
879 @param[in] DataSize Size of Data found. If size is less than the
880 data, this value contains the required size.
881 @param[in] Variable The variable information which is used to keep track of variable usage.
882 @param[in] Attributes Attribute value of the variable
883 @param[in] IsPk Indicate whether it is to process pk.
885 @return EFI_INVALID_PARAMETER Invalid parameter.
886 @return EFI_SECURITY_VIOLATION The variable does NOT pass the validation.
887 check carried out by the firmware.
888 @return EFI_SUCCESS Variable passed validation successfully.
893 IN CHAR16
*VariableName
,
894 IN EFI_GUID
*VendorGuid
,
897 IN VARIABLE_POINTER_TRACK
*Variable
,
898 IN UINT32 Attributes OPTIONAL
,
907 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0 ||
908 (Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) == 0) {
910 // PK, KEK and db/dbx should set EFI_VARIABLE_NON_VOLATILE attribute and should be a time-based
911 // authenticated variable.
913 return EFI_INVALID_PARAMETER
;
917 if ((InCustomMode() && UserPhysicalPresent()) || (mPlatformMode
== SETUP_MODE
&& !IsPk
)) {
918 Payload
= (UINT8
*) Data
+ AUTHINFO2_SIZE (Data
);
919 PayloadSize
= DataSize
- AUTHINFO2_SIZE (Data
);
920 if (PayloadSize
== 0) {
924 Status
= CheckSignatureListFormat(VariableName
, VendorGuid
, Payload
, PayloadSize
);
925 if (EFI_ERROR (Status
)) {
929 Status
= UpdateVariable (
938 &((EFI_VARIABLE_AUTHENTICATION_2
*) Data
)->TimeStamp
940 } else if (mPlatformMode
== USER_MODE
) {
942 // Verify against X509 Cert in PK database.
944 Status
= VerifyTimeBasedPayload (
956 // Verify against the certificate in data payload.
958 Status
= VerifyTimeBasedPayload (
970 if (!EFI_ERROR(Status
) && IsPk
) {
971 if (mPlatformMode
== SETUP_MODE
&& !Del
) {
973 // If enroll PK in setup mode, need change to user mode.
975 Status
= UpdatePlatformMode (USER_MODE
);
976 } else if (mPlatformMode
== USER_MODE
&& Del
){
978 // If delete PK in user mode, need change to setup mode.
980 Status
= UpdatePlatformMode (SETUP_MODE
);
988 Process variable with key exchange key for verification.
990 Caution: This function may receive untrusted input.
991 This function may be invoked in SMM mode, and datasize and data are external input.
992 This function will do basic validation, before parse the data.
993 This function will parse the authentication carefully to avoid security issues, like
994 buffer overflow, integer overflow.
995 This function will check attribute carefully to avoid authentication bypass.
997 @param[in] VariableName Name of Variable to be found.
998 @param[in] VendorGuid Variable vendor GUID.
999 @param[in] Data Data pointer.
1000 @param[in] DataSize Size of Data found. If size is less than the
1001 data, this value contains the required size.
1002 @param[in] Variable The variable information which is used to keep track of variable usage.
1003 @param[in] Attributes Attribute value of the variable.
1005 @return EFI_INVALID_PARAMETER Invalid parameter.
1006 @return EFI_SECURITY_VIOLATION The variable does NOT pass the validation
1007 check carried out by the firmware.
1008 @return EFI_SUCCESS Variable pass validation successfully.
1013 IN CHAR16
*VariableName
,
1014 IN EFI_GUID
*VendorGuid
,
1017 IN VARIABLE_POINTER_TRACK
*Variable
,
1018 IN UINT32 Attributes OPTIONAL
1025 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0 ||
1026 (Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) == 0) {
1028 // DB and DBX should set EFI_VARIABLE_NON_VOLATILE attribute and should be a time-based
1029 // authenticated variable.
1031 return EFI_INVALID_PARAMETER
;
1034 Status
= EFI_SUCCESS
;
1035 if (mPlatformMode
== USER_MODE
&& !(InCustomMode() && UserPhysicalPresent())) {
1037 // Time-based, verify against X509 Cert KEK.
1039 return VerifyTimeBasedPayload (
1051 // If in setup mode or custom secure boot mode, no authentication needed.
1053 Payload
= (UINT8
*) Data
+ AUTHINFO2_SIZE (Data
);
1054 PayloadSize
= DataSize
- AUTHINFO2_SIZE (Data
);
1056 Status
= CheckSignatureListFormat(VariableName
, VendorGuid
, Payload
, PayloadSize
);
1057 if (EFI_ERROR (Status
)) {
1061 Status
= UpdateVariable (
1070 &((EFI_VARIABLE_AUTHENTICATION_2
*) Data
)->TimeStamp
1078 Process variable with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS/EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set
1080 Caution: This function may receive untrusted input.
1081 This function may be invoked in SMM mode, and datasize and data are external input.
1082 This function will do basic validation, before parse the data.
1083 This function will parse the authentication carefully to avoid security issues, like
1084 buffer overflow, integer overflow.
1085 This function will check attribute carefully to avoid authentication bypass.
1087 @param[in] VariableName Name of Variable to be found.
1088 @param[in] VendorGuid Variable vendor GUID.
1090 @param[in] Data Data pointer.
1091 @param[in] DataSize Size of Data found. If size is less than the
1092 data, this value contains the required size.
1093 @param[in] Variable The variable information which is used to keep track of variable usage.
1094 @param[in] Attributes Attribute value of the variable.
1096 @return EFI_INVALID_PARAMETER Invalid parameter.
1097 @return EFI_WRITE_PROTECTED Variable is write-protected and needs authentication with
1098 EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS set.
1099 @return EFI_SECURITY_VIOLATION The variable is with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
1100 set, but the AuthInfo does NOT pass the validation
1101 check carried out by the firmware.
1102 @return EFI_SUCCESS Variable is not write-protected or pass validation successfully.
1107 IN CHAR16
*VariableName
,
1108 IN EFI_GUID
*VendorGuid
,
1111 IN VARIABLE_POINTER_TRACK
*Variable
,
1112 IN UINT32 Attributes
1117 BOOLEAN IsFirstTime
;
1119 EFI_VARIABLE_AUTHENTICATION
*CertData
;
1120 EFI_CERT_BLOCK_RSA_2048_SHA256
*CertBlock
;
1122 UINT64 MonotonicCount
;
1130 if (NeedPhysicallyPresent(VariableName
, VendorGuid
) && !UserPhysicalPresent()) {
1132 // This variable is protected, only physical present user could modify its value.
1134 return EFI_SECURITY_VIOLATION
;
1138 // Process Time-based Authenticated variable.
1140 if ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) {
1141 return VerifyTimeBasedPayload (
1154 // Determine if first time SetVariable with the EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS.
1156 if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) {
1158 // Determine current operation type.
1160 if (DataSize
== AUTHINFO_SIZE
) {
1164 // Determine whether this is the first time with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS set.
1166 if (Variable
->CurrPtr
== NULL
) {
1168 } else if ((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) == 0) {
1171 KeyIndex
= Variable
->CurrPtr
->PubKeyIndex
;
1172 IsFirstTime
= FALSE
;
1174 } else if ((Variable
->CurrPtr
!= NULL
) &&
1175 ((Variable
->CurrPtr
->Attributes
& (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
)) != 0)
1178 // If the variable is already write-protected, it always needs authentication before update.
1180 return EFI_WRITE_PROTECTED
;
1183 // If without EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS, set and attributes collision.
1184 // That means it is not authenticated variable, just update variable as usual.
1186 Status
= UpdateVariable (VariableName
, VendorGuid
, Data
, DataSize
, Attributes
, 0, 0, Variable
, NULL
);
1191 // Get PubKey and check Monotonic Count value corresponding to the variable.
1193 CertData
= (EFI_VARIABLE_AUTHENTICATION
*) Data
;
1194 CertBlock
= (EFI_CERT_BLOCK_RSA_2048_SHA256
*) (CertData
->AuthInfo
.CertData
);
1195 PubKey
= CertBlock
->PublicKey
;
1198 // Update Monotonic Count value.
1200 MonotonicCount
= CertData
->MonotonicCount
;
1204 // Check input PubKey.
1206 if (CompareMem (PubKey
, mPubKeyStore
+ (KeyIndex
- 1) * EFI_CERT_TYPE_RSA2048_SIZE
, EFI_CERT_TYPE_RSA2048_SIZE
) != 0) {
1207 return EFI_SECURITY_VIOLATION
;
1210 // Compare the current monotonic count and ensure that it is greater than the last SetVariable
1211 // operation with the EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS attribute set.
1213 if (CertData
->MonotonicCount
<= Variable
->CurrPtr
->MonotonicCount
) {
1215 // Monotonic count check fail, suspicious replay attack, return EFI_SECURITY_VIOLATION.
1217 return EFI_SECURITY_VIOLATION
;
1221 // Verify the certificate in Data payload.
1223 Status
= VerifyCounterBasedPayload (Data
, DataSize
, PubKey
);
1224 if (EFI_ERROR (Status
)) {
1229 // Now, the signature has been verified!
1231 if (IsFirstTime
&& !IsDeletion
) {
1233 // Update public key database variable if need.
1235 KeyIndex
= AddPubKeyInStore (PubKey
);
1236 if (KeyIndex
== 0) {
1237 return EFI_SECURITY_VIOLATION
;
1242 // Verification pass.
1244 return UpdateVariable (VariableName
, VendorGuid
, (UINT8
*)Data
+ AUTHINFO_SIZE
, DataSize
- AUTHINFO_SIZE
, Attributes
, KeyIndex
, MonotonicCount
, Variable
, NULL
);
1248 Merge two buffers which formatted as EFI_SIGNATURE_LIST. Only the new EFI_SIGNATURE_DATA
1249 will be appended to the original EFI_SIGNATURE_LIST, duplicate EFI_SIGNATURE_DATA
1252 @param[in, out] Data Pointer to original EFI_SIGNATURE_LIST.
1253 @param[in] DataSize Size of Data buffer.
1254 @param[in] NewData Pointer to new EFI_SIGNATURE_LIST to be appended.
1255 @param[in] NewDataSize Size of NewData buffer.
1257 @return Size of the merged buffer.
1261 AppendSignatureList (
1265 IN UINTN NewDataSize
1268 EFI_SIGNATURE_LIST
*CertList
;
1269 EFI_SIGNATURE_DATA
*Cert
;
1271 EFI_SIGNATURE_LIST
*NewCertList
;
1272 EFI_SIGNATURE_DATA
*NewCert
;
1279 UINTN SignatureListSize
;
1282 Tail
= (UINT8
*) Data
+ DataSize
;
1284 NewCertList
= (EFI_SIGNATURE_LIST
*) NewData
;
1285 while ((NewDataSize
> 0) && (NewDataSize
>= NewCertList
->SignatureListSize
)) {
1286 NewCert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) NewCertList
+ sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
);
1287 NewCertCount
= (NewCertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - NewCertList
->SignatureHeaderSize
) / NewCertList
->SignatureSize
;
1290 for (Index
= 0; Index
< NewCertCount
; Index
++) {
1294 CertList
= (EFI_SIGNATURE_LIST
*) Data
;
1295 while ((Size
> 0) && (Size
>= CertList
->SignatureListSize
)) {
1296 if (CompareGuid (&CertList
->SignatureType
, &NewCertList
->SignatureType
) &&
1297 (CertList
->SignatureSize
== NewCertList
->SignatureSize
)) {
1298 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
1299 CertCount
= (CertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - CertList
->SignatureHeaderSize
) / CertList
->SignatureSize
;
1300 for (Index2
= 0; Index2
< CertCount
; Index2
++) {
1302 // Iterate each Signature Data in this Signature List.
1304 if (CompareMem (NewCert
, Cert
, CertList
->SignatureSize
) == 0) {
1308 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) Cert
+ CertList
->SignatureSize
);
1315 Size
-= CertList
->SignatureListSize
;
1316 CertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) CertList
+ CertList
->SignatureListSize
);
1321 // New EFI_SIGNATURE_DATA, append it.
1323 if (CopiedCount
== 0) {
1325 // Copy EFI_SIGNATURE_LIST header for only once.
1327 CopyMem (Tail
, NewCertList
, sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
);
1328 Tail
= Tail
+ sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
;
1331 CopyMem (Tail
, NewCert
, NewCertList
->SignatureSize
);
1332 Tail
+= NewCertList
->SignatureSize
;
1336 NewCert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) NewCert
+ NewCertList
->SignatureSize
);
1340 // Update SignatureListSize in newly appended EFI_SIGNATURE_LIST.
1342 if (CopiedCount
!= 0) {
1343 SignatureListSize
= sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
+ (CopiedCount
* NewCertList
->SignatureSize
);
1344 CertList
= (EFI_SIGNATURE_LIST
*) (Tail
- SignatureListSize
);
1345 CertList
->SignatureListSize
= (UINT32
) SignatureListSize
;
1348 NewDataSize
-= NewCertList
->SignatureListSize
;
1349 NewCertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) NewCertList
+ NewCertList
->SignatureListSize
);
1352 return (Tail
- (UINT8
*) Data
);
1356 Compare two EFI_TIME data.
1359 @param FirstTime A pointer to the first EFI_TIME data.
1360 @param SecondTime A pointer to the second EFI_TIME data.
1362 @retval TRUE The FirstTime is not later than the SecondTime.
1363 @retval FALSE The FirstTime is later than the SecondTime.
1368 IN EFI_TIME
*FirstTime
,
1369 IN EFI_TIME
*SecondTime
1372 if (FirstTime
->Year
!= SecondTime
->Year
) {
1373 return (BOOLEAN
) (FirstTime
->Year
< SecondTime
->Year
);
1374 } else if (FirstTime
->Month
!= SecondTime
->Month
) {
1375 return (BOOLEAN
) (FirstTime
->Month
< SecondTime
->Month
);
1376 } else if (FirstTime
->Day
!= SecondTime
->Day
) {
1377 return (BOOLEAN
) (FirstTime
->Day
< SecondTime
->Day
);
1378 } else if (FirstTime
->Hour
!= SecondTime
->Hour
) {
1379 return (BOOLEAN
) (FirstTime
->Hour
< SecondTime
->Hour
);
1380 } else if (FirstTime
->Minute
!= SecondTime
->Minute
) {
1381 return (BOOLEAN
) (FirstTime
->Minute
< FirstTime
->Minute
);
1384 return (BOOLEAN
) (FirstTime
->Second
<= SecondTime
->Second
);
1388 Find matching signer's certificates for common authenticated variable
1389 by corresponding VariableName and VendorGuid from "certdb".
1391 The data format of "certdb":
1393 // UINT32 CertDbListSize;
1394 // /// AUTH_CERT_DB_DATA Certs1[];
1395 // /// AUTH_CERT_DB_DATA Certs2[];
1397 // /// AUTH_CERT_DB_DATA Certsn[];
1400 @param[in] VariableName Name of authenticated Variable.
1401 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1402 @param[in] Data Pointer to variable "certdb".
1403 @param[in] DataSize Size of variable "certdb".
1404 @param[out] CertOffset Offset of matching CertData, from starting of Data.
1405 @param[out] CertDataSize Length of CertData in bytes.
1406 @param[out] CertNodeOffset Offset of matching AUTH_CERT_DB_DATA , from
1408 @param[out] CertNodeSize Length of AUTH_CERT_DB_DATA in bytes.
1410 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1411 @retval EFI_NOT_FOUND Fail to find matching certs.
1412 @retval EFI_SUCCESS Find matching certs and output parameters.
1417 IN CHAR16
*VariableName
,
1418 IN EFI_GUID
*VendorGuid
,
1421 OUT UINT32
*CertOffset
, OPTIONAL
1422 OUT UINT32
*CertDataSize
, OPTIONAL
1423 OUT UINT32
*CertNodeOffset
,OPTIONAL
1424 OUT UINT32
*CertNodeSize OPTIONAL
1428 AUTH_CERT_DB_DATA
*Ptr
;
1432 UINT32 CertDbListSize
;
1434 if ((VariableName
== NULL
) || (VendorGuid
== NULL
) || (Data
== NULL
)) {
1435 return EFI_INVALID_PARAMETER
;
1439 // Check whether DataSize matches recorded CertDbListSize.
1441 if (DataSize
< sizeof (UINT32
)) {
1442 return EFI_INVALID_PARAMETER
;
1445 CertDbListSize
= ReadUnaligned32 ((UINT32
*) Data
);
1447 if (CertDbListSize
!= (UINT32
) DataSize
) {
1448 return EFI_INVALID_PARAMETER
;
1451 Offset
= sizeof (UINT32
);
1454 // Get corresponding certificates by VendorGuid and VariableName.
1456 while (Offset
< (UINT32
) DataSize
) {
1457 Ptr
= (AUTH_CERT_DB_DATA
*) (Data
+ Offset
);
1459 // Check whether VendorGuid matches.
1461 if (CompareGuid (&Ptr
->VendorGuid
, VendorGuid
)) {
1462 NodeSize
= ReadUnaligned32 (&Ptr
->CertNodeSize
);
1463 NameSize
= ReadUnaligned32 (&Ptr
->NameSize
);
1464 CertSize
= ReadUnaligned32 (&Ptr
->CertDataSize
);
1466 if (NodeSize
!= sizeof (EFI_GUID
) + sizeof (UINT32
) * 3 + CertSize
+
1467 sizeof (CHAR16
) * NameSize
) {
1468 return EFI_INVALID_PARAMETER
;
1471 Offset
= Offset
+ sizeof (EFI_GUID
) + sizeof (UINT32
) * 3;
1473 // Check whether VariableName matches.
1475 if ((NameSize
== StrLen (VariableName
)) &&
1476 (CompareMem (Data
+ Offset
, VariableName
, NameSize
* sizeof (CHAR16
)) == 0)) {
1477 Offset
= Offset
+ NameSize
* sizeof (CHAR16
);
1479 if (CertOffset
!= NULL
) {
1480 *CertOffset
= Offset
;
1483 if (CertDataSize
!= NULL
) {
1484 *CertDataSize
= CertSize
;
1487 if (CertNodeOffset
!= NULL
) {
1488 *CertNodeOffset
= (UINT32
) ((UINT8
*) Ptr
- Data
);
1491 if (CertNodeSize
!= NULL
) {
1492 *CertNodeSize
= NodeSize
;
1497 Offset
= Offset
+ NameSize
* sizeof (CHAR16
) + CertSize
;
1500 NodeSize
= ReadUnaligned32 (&Ptr
->CertNodeSize
);
1501 Offset
= Offset
+ NodeSize
;
1505 return EFI_NOT_FOUND
;
1509 Retrieve signer's certificates for common authenticated variable
1510 by corresponding VariableName and VendorGuid from "certdb".
1512 @param[in] VariableName Name of authenticated Variable.
1513 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1514 @param[out] CertData Pointer to signer's certificates.
1515 @param[out] CertDataSize Length of CertData in bytes.
1517 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1518 @retval EFI_NOT_FOUND Fail to find "certdb" or matching certs.
1519 @retval EFI_SUCCESS Get signer's certificates successfully.
1524 IN CHAR16
*VariableName
,
1525 IN EFI_GUID
*VendorGuid
,
1526 OUT UINT8
**CertData
,
1527 OUT UINT32
*CertDataSize
1530 VARIABLE_POINTER_TRACK CertDbVariable
;
1536 if ((VariableName
== NULL
) || (VendorGuid
== NULL
) || (CertData
== NULL
) || (CertDataSize
== NULL
)) {
1537 return EFI_INVALID_PARAMETER
;
1541 // Get variable "certdb".
1543 Status
= FindVariable (
1547 &mVariableModuleGlobal
->VariableGlobal
,
1550 if (EFI_ERROR (Status
)) {
1554 DataSize
= DataSizeOfVariable (CertDbVariable
.CurrPtr
);
1555 Data
= GetVariableDataPtr (CertDbVariable
.CurrPtr
);
1556 if ((DataSize
== 0) || (Data
== NULL
)) {
1558 return EFI_NOT_FOUND
;
1561 Status
= FindCertsFromDb (
1572 if (EFI_ERROR (Status
)) {
1576 *CertData
= Data
+ CertOffset
;
1581 Delete matching signer's certificates when deleting common authenticated
1582 variable by corresponding VariableName and VendorGuid from "certdb".
1584 @param[in] VariableName Name of authenticated Variable.
1585 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1587 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1588 @retval EFI_NOT_FOUND Fail to find "certdb" or matching certs.
1589 @retval EFI_OUT_OF_RESOURCES The operation is failed due to lack of resources.
1590 @retval EFI_SUCCESS The operation is completed successfully.
1595 IN CHAR16
*VariableName
,
1596 IN EFI_GUID
*VendorGuid
1599 VARIABLE_POINTER_TRACK CertDbVariable
;
1604 UINT32 CertNodeOffset
;
1605 UINT32 CertNodeSize
;
1607 UINT32 NewCertDbSize
;
1609 if ((VariableName
== NULL
) || (VendorGuid
== NULL
)) {
1610 return EFI_INVALID_PARAMETER
;
1614 // Get variable "certdb".
1616 Status
= FindVariable (
1620 &mVariableModuleGlobal
->VariableGlobal
,
1623 if (EFI_ERROR (Status
)) {
1627 DataSize
= DataSizeOfVariable (CertDbVariable
.CurrPtr
);
1628 Data
= GetVariableDataPtr (CertDbVariable
.CurrPtr
);
1629 if ((DataSize
== 0) || (Data
== NULL
)) {
1631 return EFI_NOT_FOUND
;
1634 if (DataSize
== sizeof (UINT32
)) {
1636 // There is no certs in certdb.
1642 // Get corresponding cert node from certdb.
1644 Status
= FindCertsFromDb (
1655 if (EFI_ERROR (Status
)) {
1659 if (DataSize
< (CertNodeOffset
+ CertNodeSize
)) {
1660 return EFI_NOT_FOUND
;
1664 // Construct new data content of variable "certdb".
1666 NewCertDbSize
= (UINT32
) DataSize
- CertNodeSize
;
1667 NewCertDb
= AllocateZeroPool (NewCertDbSize
);
1668 if (NewCertDb
== NULL
) {
1669 return EFI_OUT_OF_RESOURCES
;
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 (
1707 FreePool (NewCertDb
);
1712 Insert signer's certificates for common authenticated variable with VariableName
1713 and VendorGuid in AUTH_CERT_DB_DATA to "certdb".
1715 @param[in] VariableName Name of authenticated Variable.
1716 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1717 @param[in] CertData Pointer to signer's certificates.
1718 @param[in] CertDataSize Length of CertData in bytes.
1720 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1721 @retval EFI_ACCESS_DENIED An AUTH_CERT_DB_DATA entry with same VariableName
1722 and VendorGuid already exists.
1723 @retval EFI_OUT_OF_RESOURCES The operation is failed due to lack of resources.
1724 @retval EFI_SUCCESS Insert an AUTH_CERT_DB_DATA entry to "certdb"
1729 IN CHAR16
*VariableName
,
1730 IN EFI_GUID
*VendorGuid
,
1732 IN UINTN CertDataSize
1735 VARIABLE_POINTER_TRACK CertDbVariable
;
1741 UINT32 NewCertDbSize
;
1742 UINT32 CertNodeSize
;
1744 AUTH_CERT_DB_DATA
*Ptr
;
1746 if ((VariableName
== NULL
) || (VendorGuid
== NULL
) || (CertData
== NULL
)) {
1747 return EFI_INVALID_PARAMETER
;
1751 // Get variable "certdb".
1753 Status
= FindVariable (
1757 &mVariableModuleGlobal
->VariableGlobal
,
1760 if (EFI_ERROR (Status
)) {
1764 DataSize
= DataSizeOfVariable (CertDbVariable
.CurrPtr
);
1765 Data
= GetVariableDataPtr (CertDbVariable
.CurrPtr
);
1766 if ((DataSize
== 0) || (Data
== NULL
)) {
1768 return EFI_NOT_FOUND
;
1772 // Find whether matching cert node already exists in "certdb".
1773 // If yes return error.
1775 Status
= FindCertsFromDb (
1786 if (!EFI_ERROR (Status
)) {
1788 return EFI_ACCESS_DENIED
;
1792 // Construct new data content of variable "certdb".
1794 NameSize
= (UINT32
) StrLen (VariableName
);
1795 CertNodeSize
= sizeof (AUTH_CERT_DB_DATA
) + (UINT32
) CertDataSize
+ NameSize
* sizeof (CHAR16
);
1796 NewCertDbSize
= (UINT32
) DataSize
+ CertNodeSize
;
1797 NewCertDb
= AllocateZeroPool (NewCertDbSize
);
1798 if (NewCertDb
== NULL
) {
1799 return EFI_OUT_OF_RESOURCES
;
1803 // Copy the DB entries before deleting node.
1805 CopyMem (NewCertDb
, Data
, DataSize
);
1807 // Update CertDbListSize.
1809 CopyMem (NewCertDb
, &NewCertDbSize
, sizeof (UINT32
));
1811 // Construct new cert node.
1813 Ptr
= (AUTH_CERT_DB_DATA
*) (NewCertDb
+ DataSize
);
1814 CopyGuid (&Ptr
->VendorGuid
, VendorGuid
);
1815 CopyMem (&Ptr
->CertNodeSize
, &CertNodeSize
, sizeof (UINT32
));
1816 CopyMem (&Ptr
->NameSize
, &NameSize
, sizeof (UINT32
));
1817 CopyMem (&Ptr
->CertDataSize
, &CertDataSize
, sizeof (UINT32
));
1820 (UINT8
*) Ptr
+ sizeof (AUTH_CERT_DB_DATA
),
1822 NameSize
* sizeof (CHAR16
)
1826 (UINT8
*) Ptr
+ sizeof (AUTH_CERT_DB_DATA
) + NameSize
* sizeof (CHAR16
),
1834 VarAttr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
1835 Status
= UpdateVariable (
1847 FreePool (NewCertDb
);
1852 Process variable with EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set
1854 Caution: This function may receive untrusted input.
1855 This function may be invoked in SMM mode, and datasize and data are external input.
1856 This function will do basic validation, before parse the data.
1857 This function will parse the authentication carefully to avoid security issues, like
1858 buffer overflow, integer overflow.
1860 @param[in] VariableName Name of Variable to be found.
1861 @param[in] VendorGuid Variable vendor GUID.
1862 @param[in] Data Data pointer.
1863 @param[in] DataSize Size of Data found. If size is less than the
1864 data, this value contains the required size.
1865 @param[in] Variable The variable information which is used to keep track of variable usage.
1866 @param[in] Attributes Attribute value of the variable.
1867 @param[in] AuthVarType Verify against PK, KEK database, private database or certificate in data payload.
1868 @param[out] VarDel Delete the variable or not.
1870 @retval EFI_INVALID_PARAMETER Invalid parameter.
1871 @retval EFI_SECURITY_VIOLATION The variable does NOT pass the validation
1872 check carried out by the firmware.
1873 @retval EFI_OUT_OF_RESOURCES Failed to process variable due to lack
1875 @retval EFI_SUCCESS Variable pass validation successfully.
1879 VerifyTimeBasedPayload (
1880 IN CHAR16
*VariableName
,
1881 IN EFI_GUID
*VendorGuid
,
1884 IN VARIABLE_POINTER_TRACK
*Variable
,
1885 IN UINT32 Attributes
,
1886 IN AUTHVAR_TYPE AuthVarType
,
1900 BOOLEAN VerifyStatus
;
1902 EFI_SIGNATURE_LIST
*CertList
;
1903 EFI_SIGNATURE_DATA
*Cert
;
1904 VARIABLE_POINTER_TRACK KekVariable
;
1905 EFI_VARIABLE_AUTHENTICATION_2
*CertData
;
1908 VARIABLE_POINTER_TRACK PkVariable
;
1913 UINTN CertStackSize
;
1914 UINT8
*CertsInCertDb
;
1915 UINT32 CertsSizeinDb
;
1917 VerifyStatus
= FALSE
;
1926 // When the attribute EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS is
1927 // set, then the Data buffer shall begin with an instance of a complete (and serialized)
1928 // EFI_VARIABLE_AUTHENTICATION_2 descriptor. The descriptor shall be followed by the new
1929 // variable value and DataSize shall reflect the combined size of the descriptor and the new
1930 // variable value. The authentication descriptor is not part of the variable data and is not
1931 // returned by subsequent calls to GetVariable().
1933 CertData
= (EFI_VARIABLE_AUTHENTICATION_2
*) Data
;
1936 // Verify that Pad1, Nanosecond, TimeZone, Daylight and Pad2 components of the
1937 // TimeStamp value are set to zero.
1939 if ((CertData
->TimeStamp
.Pad1
!= 0) ||
1940 (CertData
->TimeStamp
.Nanosecond
!= 0) ||
1941 (CertData
->TimeStamp
.TimeZone
!= 0) ||
1942 (CertData
->TimeStamp
.Daylight
!= 0) ||
1943 (CertData
->TimeStamp
.Pad2
!= 0)) {
1944 return EFI_SECURITY_VIOLATION
;
1947 if ((Variable
->CurrPtr
!= NULL
) && ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0)) {
1948 if (CompareTimeStamp (&CertData
->TimeStamp
, &Variable
->CurrPtr
->TimeStamp
)) {
1950 // TimeStamp check fail, suspicious replay attack, return EFI_SECURITY_VIOLATION.
1952 return EFI_SECURITY_VIOLATION
;
1957 // wCertificateType should be WIN_CERT_TYPE_EFI_GUID.
1958 // Cert type should be EFI_CERT_TYPE_PKCS7_GUID.
1960 if ((CertData
->AuthInfo
.Hdr
.wCertificateType
!= WIN_CERT_TYPE_EFI_GUID
) ||
1961 !CompareGuid (&CertData
->AuthInfo
.CertType
, &gEfiCertPkcs7Guid
)) {
1963 // Invalid AuthInfo type, return EFI_SECURITY_VIOLATION.
1965 return EFI_SECURITY_VIOLATION
;
1969 // Find out Pkcs7 SignedData which follows the EFI_VARIABLE_AUTHENTICATION_2 descriptor.
1970 // AuthInfo.Hdr.dwLength is the length of the entire certificate, including the length of the header.
1972 SigData
= CertData
->AuthInfo
.CertData
;
1973 SigDataSize
= CertData
->AuthInfo
.Hdr
.dwLength
- (UINT32
) (OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID
, CertData
));
1976 // Find out the new data payload which follows Pkcs7 SignedData directly.
1978 PayloadPtr
= SigData
+ SigDataSize
;
1979 PayloadSize
= DataSize
- OFFSET_OF_AUTHINFO2_CERT_DATA
- (UINTN
) SigDataSize
;
1982 // Construct a buffer to fill with (VariableName, VendorGuid, Attributes, TimeStamp, Data).
1984 NewDataSize
= PayloadSize
+ sizeof (EFI_TIME
) + sizeof (UINT32
) +
1985 sizeof (EFI_GUID
) + StrSize (VariableName
) - sizeof (CHAR16
);
1986 NewData
= mSerializationRuntimeBuffer
;
1989 Length
= StrLen (VariableName
) * sizeof (CHAR16
);
1990 CopyMem (Buffer
, VariableName
, Length
);
1993 Length
= sizeof (EFI_GUID
);
1994 CopyMem (Buffer
, VendorGuid
, Length
);
1997 Length
= sizeof (UINT32
);
1998 CopyMem (Buffer
, &Attr
, Length
);
2001 Length
= sizeof (EFI_TIME
);
2002 CopyMem (Buffer
, &CertData
->TimeStamp
, Length
);
2005 CopyMem (Buffer
, PayloadPtr
, PayloadSize
);
2007 if (AuthVarType
== AuthVarTypePk
) {
2009 // Get platform key from variable.
2011 Status
= FindVariable (
2012 EFI_PLATFORM_KEY_NAME
,
2013 &gEfiGlobalVariableGuid
,
2015 &mVariableModuleGlobal
->VariableGlobal
,
2018 if (EFI_ERROR (Status
)) {
2022 CertList
= (EFI_SIGNATURE_LIST
*) GetVariableDataPtr (PkVariable
.CurrPtr
);
2023 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
2024 RootCert
= Cert
->SignatureData
;
2025 RootCertSize
= CertList
->SignatureSize
- (sizeof (EFI_SIGNATURE_DATA
) - 1);
2029 // Verify Pkcs7 SignedData via Pkcs7Verify library.
2031 VerifyStatus
= Pkcs7Verify (
2040 } else if (AuthVarType
== AuthVarTypeKek
) {
2043 // Get KEK database from variable.
2045 Status
= FindVariable (
2046 EFI_KEY_EXCHANGE_KEY_NAME
,
2047 &gEfiGlobalVariableGuid
,
2049 &mVariableModuleGlobal
->VariableGlobal
,
2052 if (EFI_ERROR (Status
)) {
2057 // Ready to verify Pkcs7 SignedData. Go through KEK Signature Database to find out X.509 CertList.
2059 KekDataSize
= KekVariable
.CurrPtr
->DataSize
;
2060 CertList
= (EFI_SIGNATURE_LIST
*) GetVariableDataPtr (KekVariable
.CurrPtr
);
2061 while ((KekDataSize
> 0) && (KekDataSize
>= CertList
->SignatureListSize
)) {
2062 if (CompareGuid (&CertList
->SignatureType
, &gEfiCertX509Guid
)) {
2063 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
2064 CertCount
= (CertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - CertList
->SignatureHeaderSize
) / CertList
->SignatureSize
;
2065 for (Index
= 0; Index
< CertCount
; Index
++) {
2067 // Iterate each Signature Data Node within this CertList for a verify
2069 RootCert
= Cert
->SignatureData
;
2070 RootCertSize
= CertList
->SignatureSize
- (sizeof (EFI_SIGNATURE_DATA
) - 1);
2073 // Verify Pkcs7 SignedData via Pkcs7Verify library.
2075 VerifyStatus
= Pkcs7Verify (
2086 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) Cert
+ CertList
->SignatureSize
);
2089 KekDataSize
-= CertList
->SignatureListSize
;
2090 CertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) CertList
+ CertList
->SignatureListSize
);
2092 } else if (AuthVarType
== AuthVarTypePriv
) {
2095 // Process common authenticated variable except PK/KEK/DB/DBX.
2096 // Get signer's certificates from SignedData.
2098 VerifyStatus
= Pkcs7GetSigners (
2106 if (!VerifyStatus
) {
2111 // Get previously stored signer's certificates from certdb for existing
2112 // variable. Check whether they are identical with signer's certificates
2113 // in SignedData. If not, return error immediately.
2115 if ((Variable
->CurrPtr
!= NULL
)) {
2116 VerifyStatus
= FALSE
;
2118 Status
= GetCertsFromDb (VariableName
, VendorGuid
, &CertsInCertDb
, &CertsSizeinDb
);
2119 if (EFI_ERROR (Status
)) {
2123 if ((CertStackSize
!= CertsSizeinDb
) ||
2124 (CompareMem (SignerCerts
, CertsInCertDb
, CertsSizeinDb
) != 0)) {
2129 VerifyStatus
= Pkcs7Verify (
2137 if (!VerifyStatus
) {
2142 // Delete signer's certificates when delete the common authenticated variable.
2144 if ((PayloadSize
== 0) && (Variable
->CurrPtr
!= NULL
)) {
2145 Status
= DeleteCertsFromDb (VariableName
, VendorGuid
);
2146 if (EFI_ERROR (Status
)) {
2147 VerifyStatus
= FALSE
;
2150 } else if (Variable
->CurrPtr
== NULL
) {
2152 // Insert signer's certificates when adding a new common authenticated variable.
2154 Status
= InsertCertsToDb (VariableName
, VendorGuid
, SignerCerts
, CertStackSize
);
2155 if (EFI_ERROR (Status
)) {
2156 VerifyStatus
= FALSE
;
2160 } else if (AuthVarType
== AuthVarTypePayload
) {
2161 CertList
= (EFI_SIGNATURE_LIST
*) PayloadPtr
;
2162 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
2163 RootCert
= Cert
->SignatureData
;
2164 RootCertSize
= CertList
->SignatureSize
- (sizeof (EFI_SIGNATURE_DATA
) - 1);
2166 // Verify Pkcs7 SignedData via Pkcs7Verify library.
2168 VerifyStatus
= Pkcs7Verify (
2177 return EFI_SECURITY_VIOLATION
;
2182 if (AuthVarType
== AuthVarTypePriv
) {
2183 Pkcs7FreeSigners (RootCert
);
2184 Pkcs7FreeSigners (SignerCerts
);
2187 if (!VerifyStatus
) {
2188 return EFI_SECURITY_VIOLATION
;
2191 Status
= CheckSignatureListFormat(VariableName
, VendorGuid
, PayloadPtr
, PayloadSize
);
2192 if (EFI_ERROR (Status
)) {
2196 if ((PayloadSize
== 0) && (VarDel
!= NULL
)) {
2201 // Final step: Update/Append Variable if it pass Pkcs7Verify
2203 return UpdateVariable (
2212 &CertData
->TimeStamp