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 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
;
778 ASSERT (VariableName
!= NULL
&& VendorGuid
!= NULL
&& Data
!= NULL
);
780 if (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, EFI_PLATFORM_KEY_NAME
) == 0)){
782 } else if (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, EFI_KEY_EXCHANGE_KEY_NAME
) == 0)) {
789 SigList
= (EFI_SIGNATURE_LIST
*) Data
;
790 SigDataSize
= DataSize
;
793 // Walk throuth the input signature list and check the data format.
794 // If any signature is incorrectly formed, the whole check will fail.
796 while ((SigDataSize
> 0) && (SigDataSize
>= SigList
->SignatureListSize
)) {
797 for (Index
= 0; Index
< (sizeof (mSupportSigItem
) / sizeof (EFI_SIGNATURE_ITEM
)); Index
++ ) {
798 if (CompareGuid (&SigList
->SignatureType
, &mSupportSigItem
[Index
].SigType
)) {
800 // The value of SignatureSize should always be 16 (size of SignatureOwner
801 // component) add the data length according to signature type.
803 if (mSupportSigItem
[Index
].SigDataSize
!= ((UINT32
) ~0) &&
804 (SigList
->SignatureSize
- sizeof (EFI_GUID
)) != mSupportSigItem
[Index
].SigDataSize
) {
805 return EFI_INVALID_PARAMETER
;
807 if (mSupportSigItem
[Index
].SigHeaderSize
!= ((UINTN
) ~0) &&
808 SigList
->SignatureHeaderSize
!= mSupportSigItem
[Index
].SigHeaderSize
) {
809 return EFI_INVALID_PARAMETER
;
815 if (Index
== (sizeof (mSupportSigItem
) / sizeof (EFI_SIGNATURE_ITEM
))) {
817 // Undefined signature type.
819 return EFI_INVALID_PARAMETER
;
822 if ((SigList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - SigList
->SignatureHeaderSize
) % SigList
->SignatureSize
!= 0) {
823 return EFI_INVALID_PARAMETER
;
825 SigCount
+= (SigList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - SigList
->SignatureHeaderSize
) / SigList
->SignatureSize
;
827 SigDataSize
-= SigList
->SignatureListSize
;
828 SigList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) SigList
+ SigList
->SignatureListSize
);
831 if (((UINTN
) SigList
- (UINTN
) Data
) != DataSize
) {
832 return EFI_INVALID_PARAMETER
;
835 if (IsPk
&& SigCount
> 1) {
836 return EFI_INVALID_PARAMETER
;
843 Process variable with platform key for verification.
845 Caution: This function may receive untrusted input.
846 This function may be invoked in SMM mode, and datasize and data are external input.
847 This function will do basic validation, before parse the data.
848 This function will parse the authentication carefully to avoid security issues, like
849 buffer overflow, integer overflow.
850 This function will check attribute carefully to avoid authentication bypass.
852 @param[in] VariableName Name of Variable to be found.
853 @param[in] VendorGuid Variable vendor GUID.
854 @param[in] Data Data pointer.
855 @param[in] DataSize Size of Data found. If size is less than the
856 data, this value contains the required size.
857 @param[in] Variable The variable information which is used to keep track of variable usage.
858 @param[in] Attributes Attribute value of the variable
859 @param[in] IsPk Indicate whether it is to process pk.
861 @return EFI_INVALID_PARAMETER Invalid parameter.
862 @return EFI_SECURITY_VIOLATION The variable does NOT pass the validation.
863 check carried out by the firmware.
864 @return EFI_SUCCESS Variable passed validation successfully.
869 IN CHAR16
*VariableName
,
870 IN EFI_GUID
*VendorGuid
,
873 IN VARIABLE_POINTER_TRACK
*Variable
,
874 IN UINT32 Attributes OPTIONAL
,
883 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0 ||
884 (Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) == 0) {
886 // PK, KEK and db/dbx should set EFI_VARIABLE_NON_VOLATILE attribute and should be a time-based
887 // authenticated variable.
889 return EFI_INVALID_PARAMETER
;
893 if ((InCustomMode() && UserPhysicalPresent()) || (mPlatformMode
== SETUP_MODE
&& !IsPk
)) {
894 Payload
= (UINT8
*) Data
+ AUTHINFO2_SIZE (Data
);
895 PayloadSize
= DataSize
- AUTHINFO2_SIZE (Data
);
896 if (PayloadSize
== 0) {
900 Status
= CheckSignatureListFormat(VariableName
, VendorGuid
, Payload
, PayloadSize
);
901 if (EFI_ERROR (Status
)) {
905 Status
= UpdateVariable (
914 &((EFI_VARIABLE_AUTHENTICATION_2
*) Data
)->TimeStamp
916 } else if (mPlatformMode
== USER_MODE
) {
918 // Verify against X509 Cert in PK database.
920 Status
= VerifyTimeBasedPayload (
932 // Verify against the certificate in data payload.
934 Status
= VerifyTimeBasedPayload (
946 if (!EFI_ERROR(Status
) && IsPk
) {
947 if (mPlatformMode
== SETUP_MODE
&& !Del
) {
949 // If enroll PK in setup mode, need change to user mode.
951 Status
= UpdatePlatformMode (USER_MODE
);
952 } else if (mPlatformMode
== USER_MODE
&& Del
){
954 // If delete PK in user mode, need change to setup mode.
956 Status
= UpdatePlatformMode (SETUP_MODE
);
964 Process variable with key exchange key for verification.
966 Caution: This function may receive untrusted input.
967 This function may be invoked in SMM mode, and datasize and data are external input.
968 This function will do basic validation, before parse the data.
969 This function will parse the authentication carefully to avoid security issues, like
970 buffer overflow, integer overflow.
971 This function will check attribute carefully to avoid authentication bypass.
973 @param[in] VariableName Name of Variable to be found.
974 @param[in] VendorGuid Variable vendor GUID.
975 @param[in] Data Data pointer.
976 @param[in] DataSize Size of Data found. If size is less than the
977 data, this value contains the required size.
978 @param[in] Variable The variable information which is used to keep track of variable usage.
979 @param[in] Attributes Attribute value of the variable.
981 @return EFI_INVALID_PARAMETER Invalid parameter.
982 @return EFI_SECURITY_VIOLATION The variable does NOT pass the validation
983 check carried out by the firmware.
984 @return EFI_SUCCESS Variable pass validation successfully.
989 IN CHAR16
*VariableName
,
990 IN EFI_GUID
*VendorGuid
,
993 IN VARIABLE_POINTER_TRACK
*Variable
,
994 IN UINT32 Attributes OPTIONAL
1001 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0 ||
1002 (Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) == 0) {
1004 // DB and DBX should set EFI_VARIABLE_NON_VOLATILE attribute and should be a time-based
1005 // authenticated variable.
1007 return EFI_INVALID_PARAMETER
;
1010 Status
= EFI_SUCCESS
;
1011 if (mPlatformMode
== USER_MODE
&& !(InCustomMode() && UserPhysicalPresent())) {
1013 // Time-based, verify against X509 Cert KEK.
1015 return VerifyTimeBasedPayload (
1027 // If in setup mode or custom secure boot mode, no authentication needed.
1029 Payload
= (UINT8
*) Data
+ AUTHINFO2_SIZE (Data
);
1030 PayloadSize
= DataSize
- AUTHINFO2_SIZE (Data
);
1032 Status
= UpdateVariable (
1041 &((EFI_VARIABLE_AUTHENTICATION_2
*) Data
)->TimeStamp
1049 Process variable with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS/EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set
1051 Caution: This function may receive untrusted input.
1052 This function may be invoked in SMM mode, and datasize and data are external input.
1053 This function will do basic validation, before parse the data.
1054 This function will parse the authentication carefully to avoid security issues, like
1055 buffer overflow, integer overflow.
1056 This function will check attribute carefully to avoid authentication bypass.
1058 @param[in] VariableName Name of Variable to be found.
1059 @param[in] VendorGuid Variable vendor GUID.
1061 @param[in] Data Data pointer.
1062 @param[in] DataSize Size of Data found. If size is less than the
1063 data, this value contains the required size.
1064 @param[in] Variable The variable information which is used to keep track of variable usage.
1065 @param[in] Attributes Attribute value of the variable.
1067 @return EFI_INVALID_PARAMETER Invalid parameter.
1068 @return EFI_WRITE_PROTECTED Variable is write-protected and needs authentication with
1069 EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS set.
1070 @return EFI_SECURITY_VIOLATION The variable is with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
1071 set, but the AuthInfo does NOT pass the validation
1072 check carried out by the firmware.
1073 @return EFI_SUCCESS Variable is not write-protected or pass validation successfully.
1078 IN CHAR16
*VariableName
,
1079 IN EFI_GUID
*VendorGuid
,
1082 IN VARIABLE_POINTER_TRACK
*Variable
,
1083 IN UINT32 Attributes
1088 BOOLEAN IsFirstTime
;
1090 EFI_VARIABLE_AUTHENTICATION
*CertData
;
1091 EFI_CERT_BLOCK_RSA_2048_SHA256
*CertBlock
;
1093 UINT64 MonotonicCount
;
1101 if (NeedPhysicallyPresent(VariableName
, VendorGuid
) && !UserPhysicalPresent()) {
1103 // This variable is protected, only physical present user could modify its value.
1105 return EFI_SECURITY_VIOLATION
;
1109 // Process Time-based Authenticated variable.
1111 if ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) {
1112 return VerifyTimeBasedPayload (
1125 // Determine if first time SetVariable with the EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS.
1127 if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) {
1129 // Determine current operation type.
1131 if (DataSize
== AUTHINFO_SIZE
) {
1135 // Determine whether this is the first time with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS set.
1137 if (Variable
->CurrPtr
== NULL
) {
1139 } else if ((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) == 0) {
1142 KeyIndex
= Variable
->CurrPtr
->PubKeyIndex
;
1143 IsFirstTime
= FALSE
;
1145 } else if ((Variable
->CurrPtr
!= NULL
) &&
1146 ((Variable
->CurrPtr
->Attributes
& (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
)) != 0)
1149 // If the variable is already write-protected, it always needs authentication before update.
1151 return EFI_WRITE_PROTECTED
;
1154 // If without EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS, set and attributes collision.
1155 // That means it is not authenticated variable, just update variable as usual.
1157 Status
= UpdateVariable (VariableName
, VendorGuid
, Data
, DataSize
, Attributes
, 0, 0, Variable
, NULL
);
1162 // Get PubKey and check Monotonic Count value corresponding to the variable.
1164 CertData
= (EFI_VARIABLE_AUTHENTICATION
*) Data
;
1165 CertBlock
= (EFI_CERT_BLOCK_RSA_2048_SHA256
*) (CertData
->AuthInfo
.CertData
);
1166 PubKey
= CertBlock
->PublicKey
;
1169 // Update Monotonic Count value.
1171 MonotonicCount
= CertData
->MonotonicCount
;
1175 // Check input PubKey.
1177 if (CompareMem (PubKey
, mPubKeyStore
+ (KeyIndex
- 1) * EFI_CERT_TYPE_RSA2048_SIZE
, EFI_CERT_TYPE_RSA2048_SIZE
) != 0) {
1178 return EFI_SECURITY_VIOLATION
;
1181 // Compare the current monotonic count and ensure that it is greater than the last SetVariable
1182 // operation with the EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS attribute set.
1184 if (CertData
->MonotonicCount
<= Variable
->CurrPtr
->MonotonicCount
) {
1186 // Monotonic count check fail, suspicious replay attack, return EFI_SECURITY_VIOLATION.
1188 return EFI_SECURITY_VIOLATION
;
1192 // Verify the certificate in Data payload.
1194 Status
= VerifyCounterBasedPayload (Data
, DataSize
, PubKey
);
1195 if (EFI_ERROR (Status
)) {
1200 // Now, the signature has been verified!
1202 if (IsFirstTime
&& !IsDeletion
) {
1204 // Update public key database variable if need.
1206 KeyIndex
= AddPubKeyInStore (PubKey
);
1207 if (KeyIndex
== 0) {
1208 return EFI_SECURITY_VIOLATION
;
1213 // Verification pass.
1215 return UpdateVariable (VariableName
, VendorGuid
, (UINT8
*)Data
+ AUTHINFO_SIZE
, DataSize
- AUTHINFO_SIZE
, Attributes
, KeyIndex
, MonotonicCount
, Variable
, NULL
);
1219 Merge two buffers which formatted as EFI_SIGNATURE_LIST. Only the new EFI_SIGNATURE_DATA
1220 will be appended to the original EFI_SIGNATURE_LIST, duplicate EFI_SIGNATURE_DATA
1223 @param[in, out] Data Pointer to original EFI_SIGNATURE_LIST.
1224 @param[in] DataSize Size of Data buffer.
1225 @param[in] NewData Pointer to new EFI_SIGNATURE_LIST to be appended.
1226 @param[in] NewDataSize Size of NewData buffer.
1228 @return Size of the merged buffer.
1232 AppendSignatureList (
1236 IN UINTN NewDataSize
1239 EFI_SIGNATURE_LIST
*CertList
;
1240 EFI_SIGNATURE_DATA
*Cert
;
1242 EFI_SIGNATURE_LIST
*NewCertList
;
1243 EFI_SIGNATURE_DATA
*NewCert
;
1250 UINTN SignatureListSize
;
1253 Tail
= (UINT8
*) Data
+ DataSize
;
1255 NewCertList
= (EFI_SIGNATURE_LIST
*) NewData
;
1256 while ((NewDataSize
> 0) && (NewDataSize
>= NewCertList
->SignatureListSize
)) {
1257 NewCert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) NewCertList
+ sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
);
1258 NewCertCount
= (NewCertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - NewCertList
->SignatureHeaderSize
) / NewCertList
->SignatureSize
;
1261 for (Index
= 0; Index
< NewCertCount
; Index
++) {
1265 CertList
= (EFI_SIGNATURE_LIST
*) Data
;
1266 while ((Size
> 0) && (Size
>= CertList
->SignatureListSize
)) {
1267 if (CompareGuid (&CertList
->SignatureType
, &NewCertList
->SignatureType
) &&
1268 (CertList
->SignatureSize
== NewCertList
->SignatureSize
)) {
1269 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
1270 CertCount
= (CertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - CertList
->SignatureHeaderSize
) / CertList
->SignatureSize
;
1271 for (Index2
= 0; Index2
< CertCount
; Index2
++) {
1273 // Iterate each Signature Data in this Signature List.
1275 if (CompareMem (NewCert
, Cert
, CertList
->SignatureSize
) == 0) {
1279 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) Cert
+ CertList
->SignatureSize
);
1286 Size
-= CertList
->SignatureListSize
;
1287 CertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) CertList
+ CertList
->SignatureListSize
);
1292 // New EFI_SIGNATURE_DATA, append it.
1294 if (CopiedCount
== 0) {
1296 // Copy EFI_SIGNATURE_LIST header for only once.
1298 CopyMem (Tail
, NewCertList
, sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
);
1299 Tail
= Tail
+ sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
;
1302 CopyMem (Tail
, NewCert
, NewCertList
->SignatureSize
);
1303 Tail
+= NewCertList
->SignatureSize
;
1307 NewCert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) NewCert
+ NewCertList
->SignatureSize
);
1311 // Update SignatureListSize in newly appended EFI_SIGNATURE_LIST.
1313 if (CopiedCount
!= 0) {
1314 SignatureListSize
= sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
+ (CopiedCount
* NewCertList
->SignatureSize
);
1315 CertList
= (EFI_SIGNATURE_LIST
*) (Tail
- SignatureListSize
);
1316 CertList
->SignatureListSize
= (UINT32
) SignatureListSize
;
1319 NewDataSize
-= NewCertList
->SignatureListSize
;
1320 NewCertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) NewCertList
+ NewCertList
->SignatureListSize
);
1323 return (Tail
- (UINT8
*) Data
);
1327 Compare two EFI_TIME data.
1330 @param FirstTime A pointer to the first EFI_TIME data.
1331 @param SecondTime A pointer to the second EFI_TIME data.
1333 @retval TRUE The FirstTime is not later than the SecondTime.
1334 @retval FALSE The FirstTime is later than the SecondTime.
1339 IN EFI_TIME
*FirstTime
,
1340 IN EFI_TIME
*SecondTime
1343 if (FirstTime
->Year
!= SecondTime
->Year
) {
1344 return (BOOLEAN
) (FirstTime
->Year
< SecondTime
->Year
);
1345 } else if (FirstTime
->Month
!= SecondTime
->Month
) {
1346 return (BOOLEAN
) (FirstTime
->Month
< SecondTime
->Month
);
1347 } else if (FirstTime
->Day
!= SecondTime
->Day
) {
1348 return (BOOLEAN
) (FirstTime
->Day
< SecondTime
->Day
);
1349 } else if (FirstTime
->Hour
!= SecondTime
->Hour
) {
1350 return (BOOLEAN
) (FirstTime
->Hour
< SecondTime
->Hour
);
1351 } else if (FirstTime
->Minute
!= SecondTime
->Minute
) {
1352 return (BOOLEAN
) (FirstTime
->Minute
< FirstTime
->Minute
);
1355 return (BOOLEAN
) (FirstTime
->Second
<= SecondTime
->Second
);
1359 Find matching signer's certificates for common authenticated variable
1360 by corresponding VariableName and VendorGuid from "certdb".
1362 The data format of "certdb":
1364 // UINT32 CertDbListSize;
1365 // /// AUTH_CERT_DB_DATA Certs1[];
1366 // /// AUTH_CERT_DB_DATA Certs2[];
1368 // /// AUTH_CERT_DB_DATA Certsn[];
1371 @param[in] VariableName Name of authenticated Variable.
1372 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1373 @param[in] Data Pointer to variable "certdb".
1374 @param[in] DataSize Size of variable "certdb".
1375 @param[out] CertOffset Offset of matching CertData, from starting of Data.
1376 @param[out] CertDataSize Length of CertData in bytes.
1377 @param[out] CertNodeOffset Offset of matching AUTH_CERT_DB_DATA , from
1379 @param[out] CertNodeSize Length of AUTH_CERT_DB_DATA in bytes.
1381 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1382 @retval EFI_NOT_FOUND Fail to find matching certs.
1383 @retval EFI_SUCCESS Find matching certs and output parameters.
1388 IN CHAR16
*VariableName
,
1389 IN EFI_GUID
*VendorGuid
,
1392 OUT UINT32
*CertOffset
, OPTIONAL
1393 OUT UINT32
*CertDataSize
, OPTIONAL
1394 OUT UINT32
*CertNodeOffset
,OPTIONAL
1395 OUT UINT32
*CertNodeSize OPTIONAL
1399 AUTH_CERT_DB_DATA
*Ptr
;
1403 UINT32 CertDbListSize
;
1405 if ((VariableName
== NULL
) || (VendorGuid
== NULL
) || (Data
== NULL
)) {
1406 return EFI_INVALID_PARAMETER
;
1410 // Check whether DataSize matches recorded CertDbListSize.
1412 if (DataSize
< sizeof (UINT32
)) {
1413 return EFI_INVALID_PARAMETER
;
1416 CertDbListSize
= ReadUnaligned32 ((UINT32
*) Data
);
1418 if (CertDbListSize
!= (UINT32
) DataSize
) {
1419 return EFI_INVALID_PARAMETER
;
1422 Offset
= sizeof (UINT32
);
1425 // Get corresponding certificates by VendorGuid and VariableName.
1427 while (Offset
< (UINT32
) DataSize
) {
1428 Ptr
= (AUTH_CERT_DB_DATA
*) (Data
+ Offset
);
1430 // Check whether VendorGuid matches.
1432 if (CompareGuid (&Ptr
->VendorGuid
, VendorGuid
)) {
1433 NodeSize
= ReadUnaligned32 (&Ptr
->CertNodeSize
);
1434 NameSize
= ReadUnaligned32 (&Ptr
->NameSize
);
1435 CertSize
= ReadUnaligned32 (&Ptr
->CertDataSize
);
1437 if (NodeSize
!= sizeof (EFI_GUID
) + sizeof (UINT32
) * 3 + CertSize
+
1438 sizeof (CHAR16
) * NameSize
) {
1439 return EFI_INVALID_PARAMETER
;
1442 Offset
= Offset
+ sizeof (EFI_GUID
) + sizeof (UINT32
) * 3;
1444 // Check whether VariableName matches.
1446 if ((NameSize
== StrLen (VariableName
)) &&
1447 (CompareMem (Data
+ Offset
, VariableName
, NameSize
* sizeof (CHAR16
)) == 0)) {
1448 Offset
= Offset
+ NameSize
* sizeof (CHAR16
);
1450 if (CertOffset
!= NULL
) {
1451 *CertOffset
= Offset
;
1454 if (CertDataSize
!= NULL
) {
1455 *CertDataSize
= CertSize
;
1458 if (CertNodeOffset
!= NULL
) {
1459 *CertNodeOffset
= (UINT32
) ((UINT8
*) Ptr
- Data
);
1462 if (CertNodeSize
!= NULL
) {
1463 *CertNodeSize
= NodeSize
;
1468 Offset
= Offset
+ NameSize
* sizeof (CHAR16
) + CertSize
;
1471 NodeSize
= ReadUnaligned32 (&Ptr
->CertNodeSize
);
1472 Offset
= Offset
+ NodeSize
;
1476 return EFI_NOT_FOUND
;
1480 Retrieve signer's certificates for common authenticated variable
1481 by corresponding VariableName and VendorGuid from "certdb".
1483 @param[in] VariableName Name of authenticated Variable.
1484 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1485 @param[out] CertData Pointer to signer's certificates.
1486 @param[out] CertDataSize Length of CertData in bytes.
1488 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1489 @retval EFI_NOT_FOUND Fail to find "certdb" or matching certs.
1490 @retval EFI_SUCCESS Get signer's certificates successfully.
1495 IN CHAR16
*VariableName
,
1496 IN EFI_GUID
*VendorGuid
,
1497 OUT UINT8
**CertData
,
1498 OUT UINT32
*CertDataSize
1501 VARIABLE_POINTER_TRACK CertDbVariable
;
1507 if ((VariableName
== NULL
) || (VendorGuid
== NULL
) || (CertData
== NULL
) || (CertDataSize
== NULL
)) {
1508 return EFI_INVALID_PARAMETER
;
1512 // Get variable "certdb".
1514 Status
= FindVariable (
1518 &mVariableModuleGlobal
->VariableGlobal
,
1521 if (EFI_ERROR (Status
)) {
1525 DataSize
= DataSizeOfVariable (CertDbVariable
.CurrPtr
);
1526 Data
= GetVariableDataPtr (CertDbVariable
.CurrPtr
);
1527 if ((DataSize
== 0) || (Data
== NULL
)) {
1529 return EFI_NOT_FOUND
;
1532 Status
= FindCertsFromDb (
1543 if (EFI_ERROR (Status
)) {
1547 *CertData
= Data
+ CertOffset
;
1552 Delete matching signer's certificates when deleting common authenticated
1553 variable by corresponding VariableName and VendorGuid from "certdb".
1555 @param[in] VariableName Name of authenticated Variable.
1556 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1558 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1559 @retval EFI_NOT_FOUND Fail to find "certdb" or matching certs.
1560 @retval EFI_OUT_OF_RESOURCES The operation is failed due to lack of resources.
1561 @retval EFI_SUCCESS The operation is completed successfully.
1566 IN CHAR16
*VariableName
,
1567 IN EFI_GUID
*VendorGuid
1570 VARIABLE_POINTER_TRACK CertDbVariable
;
1575 UINT32 CertNodeOffset
;
1576 UINT32 CertNodeSize
;
1578 UINT32 NewCertDbSize
;
1580 if ((VariableName
== NULL
) || (VendorGuid
== NULL
)) {
1581 return EFI_INVALID_PARAMETER
;
1585 // Get variable "certdb".
1587 Status
= FindVariable (
1591 &mVariableModuleGlobal
->VariableGlobal
,
1594 if (EFI_ERROR (Status
)) {
1598 DataSize
= DataSizeOfVariable (CertDbVariable
.CurrPtr
);
1599 Data
= GetVariableDataPtr (CertDbVariable
.CurrPtr
);
1600 if ((DataSize
== 0) || (Data
== NULL
)) {
1602 return EFI_NOT_FOUND
;
1605 if (DataSize
== sizeof (UINT32
)) {
1607 // There is no certs in certdb.
1613 // Get corresponding cert node from certdb.
1615 Status
= FindCertsFromDb (
1626 if (EFI_ERROR (Status
)) {
1630 if (DataSize
< (CertNodeOffset
+ CertNodeSize
)) {
1631 return EFI_NOT_FOUND
;
1635 // Construct new data content of variable "certdb".
1637 NewCertDbSize
= (UINT32
) DataSize
- CertNodeSize
;
1638 NewCertDb
= AllocateZeroPool (NewCertDbSize
);
1639 if (NewCertDb
== NULL
) {
1640 return EFI_OUT_OF_RESOURCES
;
1644 // Copy the DB entries before deleting node.
1646 CopyMem (NewCertDb
, Data
, CertNodeOffset
);
1648 // Update CertDbListSize.
1650 CopyMem (NewCertDb
, &NewCertDbSize
, sizeof (UINT32
));
1652 // Copy the DB entries after deleting node.
1654 if (DataSize
> (CertNodeOffset
+ CertNodeSize
)) {
1656 NewCertDb
+ CertNodeOffset
,
1657 Data
+ CertNodeOffset
+ CertNodeSize
,
1658 DataSize
- CertNodeOffset
- CertNodeSize
1665 VarAttr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
1666 Status
= UpdateVariable (
1678 FreePool (NewCertDb
);
1683 Insert signer's certificates for common authenticated variable with VariableName
1684 and VendorGuid in AUTH_CERT_DB_DATA to "certdb".
1686 @param[in] VariableName Name of authenticated Variable.
1687 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1688 @param[in] CertData Pointer to signer's certificates.
1689 @param[in] CertDataSize Length of CertData in bytes.
1691 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1692 @retval EFI_ACCESS_DENIED An AUTH_CERT_DB_DATA entry with same VariableName
1693 and VendorGuid already exists.
1694 @retval EFI_OUT_OF_RESOURCES The operation is failed due to lack of resources.
1695 @retval EFI_SUCCESS Insert an AUTH_CERT_DB_DATA entry to "certdb"
1700 IN CHAR16
*VariableName
,
1701 IN EFI_GUID
*VendorGuid
,
1703 IN UINTN CertDataSize
1706 VARIABLE_POINTER_TRACK CertDbVariable
;
1712 UINT32 NewCertDbSize
;
1713 UINT32 CertNodeSize
;
1715 AUTH_CERT_DB_DATA
*Ptr
;
1717 if ((VariableName
== NULL
) || (VendorGuid
== NULL
) || (CertData
== NULL
)) {
1718 return EFI_INVALID_PARAMETER
;
1722 // Get variable "certdb".
1724 Status
= FindVariable (
1728 &mVariableModuleGlobal
->VariableGlobal
,
1731 if (EFI_ERROR (Status
)) {
1735 DataSize
= DataSizeOfVariable (CertDbVariable
.CurrPtr
);
1736 Data
= GetVariableDataPtr (CertDbVariable
.CurrPtr
);
1737 if ((DataSize
== 0) || (Data
== NULL
)) {
1739 return EFI_NOT_FOUND
;
1743 // Find whether matching cert node already exists in "certdb".
1744 // If yes return error.
1746 Status
= FindCertsFromDb (
1757 if (!EFI_ERROR (Status
)) {
1759 return EFI_ACCESS_DENIED
;
1763 // Construct new data content of variable "certdb".
1765 NameSize
= (UINT32
) StrLen (VariableName
);
1766 CertNodeSize
= sizeof (AUTH_CERT_DB_DATA
) + (UINT32
) CertDataSize
+ NameSize
* sizeof (CHAR16
);
1767 NewCertDbSize
= (UINT32
) DataSize
+ CertNodeSize
;
1768 NewCertDb
= AllocateZeroPool (NewCertDbSize
);
1769 if (NewCertDb
== NULL
) {
1770 return EFI_OUT_OF_RESOURCES
;
1774 // Copy the DB entries before deleting node.
1776 CopyMem (NewCertDb
, Data
, DataSize
);
1778 // Update CertDbListSize.
1780 CopyMem (NewCertDb
, &NewCertDbSize
, sizeof (UINT32
));
1782 // Construct new cert node.
1784 Ptr
= (AUTH_CERT_DB_DATA
*) (NewCertDb
+ DataSize
);
1785 CopyGuid (&Ptr
->VendorGuid
, VendorGuid
);
1786 CopyMem (&Ptr
->CertNodeSize
, &CertNodeSize
, sizeof (UINT32
));
1787 CopyMem (&Ptr
->NameSize
, &NameSize
, sizeof (UINT32
));
1788 CopyMem (&Ptr
->CertDataSize
, &CertDataSize
, sizeof (UINT32
));
1791 (UINT8
*) Ptr
+ sizeof (AUTH_CERT_DB_DATA
),
1793 NameSize
* sizeof (CHAR16
)
1797 (UINT8
*) Ptr
+ sizeof (AUTH_CERT_DB_DATA
) + NameSize
* sizeof (CHAR16
),
1805 VarAttr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
1806 Status
= UpdateVariable (
1818 FreePool (NewCertDb
);
1823 Process variable with EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set
1825 Caution: This function may receive untrusted input.
1826 This function may be invoked in SMM mode, and datasize and data are external input.
1827 This function will do basic validation, before parse the data.
1828 This function will parse the authentication carefully to avoid security issues, like
1829 buffer overflow, integer overflow.
1831 @param[in] VariableName Name of Variable to be found.
1832 @param[in] VendorGuid Variable vendor GUID.
1833 @param[in] Data Data pointer.
1834 @param[in] DataSize Size of Data found. If size is less than the
1835 data, this value contains the required size.
1836 @param[in] Variable The variable information which is used to keep track of variable usage.
1837 @param[in] Attributes Attribute value of the variable.
1838 @param[in] AuthVarType Verify against PK, KEK database, private database or certificate in data payload.
1839 @param[out] VarDel Delete the variable or not.
1841 @retval EFI_INVALID_PARAMETER Invalid parameter.
1842 @retval EFI_SECURITY_VIOLATION The variable does NOT pass the validation
1843 check carried out by the firmware.
1844 @retval EFI_OUT_OF_RESOURCES Failed to process variable due to lack
1846 @retval EFI_SUCCESS Variable pass validation successfully.
1850 VerifyTimeBasedPayload (
1851 IN CHAR16
*VariableName
,
1852 IN EFI_GUID
*VendorGuid
,
1855 IN VARIABLE_POINTER_TRACK
*Variable
,
1856 IN UINT32 Attributes
,
1857 IN AUTHVAR_TYPE AuthVarType
,
1871 BOOLEAN VerifyStatus
;
1873 EFI_SIGNATURE_LIST
*CertList
;
1874 EFI_SIGNATURE_DATA
*Cert
;
1875 VARIABLE_POINTER_TRACK KekVariable
;
1876 EFI_VARIABLE_AUTHENTICATION_2
*CertData
;
1879 VARIABLE_POINTER_TRACK PkVariable
;
1884 UINTN CertStackSize
;
1885 UINT8
*CertsInCertDb
;
1886 UINT32 CertsSizeinDb
;
1888 VerifyStatus
= FALSE
;
1897 // When the attribute EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS is
1898 // set, then the Data buffer shall begin with an instance of a complete (and serialized)
1899 // EFI_VARIABLE_AUTHENTICATION_2 descriptor. The descriptor shall be followed by the new
1900 // variable value and DataSize shall reflect the combined size of the descriptor and the new
1901 // variable value. The authentication descriptor is not part of the variable data and is not
1902 // returned by subsequent calls to GetVariable().
1904 CertData
= (EFI_VARIABLE_AUTHENTICATION_2
*) Data
;
1907 // Verify that Pad1, Nanosecond, TimeZone, Daylight and Pad2 components of the
1908 // TimeStamp value are set to zero.
1910 if ((CertData
->TimeStamp
.Pad1
!= 0) ||
1911 (CertData
->TimeStamp
.Nanosecond
!= 0) ||
1912 (CertData
->TimeStamp
.TimeZone
!= 0) ||
1913 (CertData
->TimeStamp
.Daylight
!= 0) ||
1914 (CertData
->TimeStamp
.Pad2
!= 0)) {
1915 return EFI_SECURITY_VIOLATION
;
1918 if ((Variable
->CurrPtr
!= NULL
) && ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0)) {
1919 if (CompareTimeStamp (&CertData
->TimeStamp
, &Variable
->CurrPtr
->TimeStamp
)) {
1921 // TimeStamp check fail, suspicious replay attack, return EFI_SECURITY_VIOLATION.
1923 return EFI_SECURITY_VIOLATION
;
1928 // wCertificateType should be WIN_CERT_TYPE_EFI_GUID.
1929 // Cert type should be EFI_CERT_TYPE_PKCS7_GUID.
1931 if ((CertData
->AuthInfo
.Hdr
.wCertificateType
!= WIN_CERT_TYPE_EFI_GUID
) ||
1932 !CompareGuid (&CertData
->AuthInfo
.CertType
, &gEfiCertPkcs7Guid
)) {
1934 // Invalid AuthInfo type, return EFI_SECURITY_VIOLATION.
1936 return EFI_SECURITY_VIOLATION
;
1940 // Find out Pkcs7 SignedData which follows the EFI_VARIABLE_AUTHENTICATION_2 descriptor.
1941 // AuthInfo.Hdr.dwLength is the length of the entire certificate, including the length of the header.
1943 SigData
= CertData
->AuthInfo
.CertData
;
1944 SigDataSize
= CertData
->AuthInfo
.Hdr
.dwLength
- (UINT32
) (OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID
, CertData
));
1947 // Find out the new data payload which follows Pkcs7 SignedData directly.
1949 PayloadPtr
= SigData
+ SigDataSize
;
1950 PayloadSize
= DataSize
- OFFSET_OF_AUTHINFO2_CERT_DATA
- (UINTN
) SigDataSize
;
1953 // Construct a buffer to fill with (VariableName, VendorGuid, Attributes, TimeStamp, Data).
1955 NewDataSize
= PayloadSize
+ sizeof (EFI_TIME
) + sizeof (UINT32
) +
1956 sizeof (EFI_GUID
) + StrSize (VariableName
) - sizeof (CHAR16
);
1957 NewData
= mSerializationRuntimeBuffer
;
1960 Length
= StrLen (VariableName
) * sizeof (CHAR16
);
1961 CopyMem (Buffer
, VariableName
, Length
);
1964 Length
= sizeof (EFI_GUID
);
1965 CopyMem (Buffer
, VendorGuid
, Length
);
1968 Length
= sizeof (UINT32
);
1969 CopyMem (Buffer
, &Attr
, Length
);
1972 Length
= sizeof (EFI_TIME
);
1973 CopyMem (Buffer
, &CertData
->TimeStamp
, Length
);
1976 CopyMem (Buffer
, PayloadPtr
, PayloadSize
);
1978 if (AuthVarType
== AuthVarTypePk
) {
1980 // Get platform key from variable.
1982 Status
= FindVariable (
1983 EFI_PLATFORM_KEY_NAME
,
1984 &gEfiGlobalVariableGuid
,
1986 &mVariableModuleGlobal
->VariableGlobal
,
1989 if (EFI_ERROR (Status
)) {
1993 CertList
= (EFI_SIGNATURE_LIST
*) GetVariableDataPtr (PkVariable
.CurrPtr
);
1994 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
1995 RootCert
= Cert
->SignatureData
;
1996 RootCertSize
= CertList
->SignatureSize
- (sizeof (EFI_SIGNATURE_DATA
) - 1);
2000 // Verify Pkcs7 SignedData via Pkcs7Verify library.
2002 VerifyStatus
= Pkcs7Verify (
2011 } else if (AuthVarType
== AuthVarTypeKek
) {
2014 // Get KEK database from variable.
2016 Status
= FindVariable (
2017 EFI_KEY_EXCHANGE_KEY_NAME
,
2018 &gEfiGlobalVariableGuid
,
2020 &mVariableModuleGlobal
->VariableGlobal
,
2023 if (EFI_ERROR (Status
)) {
2028 // Ready to verify Pkcs7 SignedData. Go through KEK Signature Database to find out X.509 CertList.
2030 KekDataSize
= KekVariable
.CurrPtr
->DataSize
;
2031 CertList
= (EFI_SIGNATURE_LIST
*) GetVariableDataPtr (KekVariable
.CurrPtr
);
2032 while ((KekDataSize
> 0) && (KekDataSize
>= CertList
->SignatureListSize
)) {
2033 if (CompareGuid (&CertList
->SignatureType
, &gEfiCertX509Guid
)) {
2034 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
2035 CertCount
= (CertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - CertList
->SignatureHeaderSize
) / CertList
->SignatureSize
;
2036 for (Index
= 0; Index
< CertCount
; Index
++) {
2038 // Iterate each Signature Data Node within this CertList for a verify
2040 RootCert
= Cert
->SignatureData
;
2041 RootCertSize
= CertList
->SignatureSize
- (sizeof (EFI_SIGNATURE_DATA
) - 1);
2044 // Verify Pkcs7 SignedData via Pkcs7Verify library.
2046 VerifyStatus
= Pkcs7Verify (
2057 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) Cert
+ CertList
->SignatureSize
);
2060 KekDataSize
-= CertList
->SignatureListSize
;
2061 CertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) CertList
+ CertList
->SignatureListSize
);
2063 } else if (AuthVarType
== AuthVarTypePriv
) {
2066 // Process common authenticated variable except PK/KEK/DB/DBX.
2067 // Get signer's certificates from SignedData.
2069 VerifyStatus
= Pkcs7GetSigners (
2077 if (!VerifyStatus
) {
2082 // Get previously stored signer's certificates from certdb for existing
2083 // variable. Check whether they are identical with signer's certificates
2084 // in SignedData. If not, return error immediately.
2086 if ((Variable
->CurrPtr
!= NULL
)) {
2087 VerifyStatus
= FALSE
;
2089 Status
= GetCertsFromDb (VariableName
, VendorGuid
, &CertsInCertDb
, &CertsSizeinDb
);
2090 if (EFI_ERROR (Status
)) {
2094 if ((CertStackSize
!= CertsSizeinDb
) ||
2095 (CompareMem (SignerCerts
, CertsInCertDb
, CertsSizeinDb
) != 0)) {
2100 VerifyStatus
= Pkcs7Verify (
2108 if (!VerifyStatus
) {
2113 // Delete signer's certificates when delete the common authenticated variable.
2115 if ((PayloadSize
== 0) && (Variable
->CurrPtr
!= NULL
)) {
2116 Status
= DeleteCertsFromDb (VariableName
, VendorGuid
);
2117 if (EFI_ERROR (Status
)) {
2118 VerifyStatus
= FALSE
;
2121 } else if (Variable
->CurrPtr
== NULL
) {
2123 // Insert signer's certificates when adding a new common authenticated variable.
2125 Status
= InsertCertsToDb (VariableName
, VendorGuid
, SignerCerts
, CertStackSize
);
2126 if (EFI_ERROR (Status
)) {
2127 VerifyStatus
= FALSE
;
2131 } else if (AuthVarType
== AuthVarTypePayload
) {
2132 CertList
= (EFI_SIGNATURE_LIST
*) PayloadPtr
;
2133 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
2134 RootCert
= Cert
->SignatureData
;
2135 RootCertSize
= CertList
->SignatureSize
- (sizeof (EFI_SIGNATURE_DATA
) - 1);
2137 // Verify Pkcs7 SignedData via Pkcs7Verify library.
2139 VerifyStatus
= Pkcs7Verify (
2148 return EFI_SECURITY_VIOLATION
;
2153 if (AuthVarType
== AuthVarTypePriv
) {
2154 Pkcs7FreeSigners (RootCert
);
2155 Pkcs7FreeSigners (SignerCerts
);
2158 if (!VerifyStatus
) {
2159 return EFI_SECURITY_VIOLATION
;
2162 Status
= CheckSignatureListFormat(VariableName
, VendorGuid
, PayloadPtr
, PayloadSize
);
2163 if (EFI_ERROR (Status
)) {
2167 if ((PayloadSize
== 0) && (VarDel
!= NULL
)) {
2172 // Final step: Update/Append Variable if it pass Pkcs7Verify
2174 return UpdateVariable (
2183 &CertData
->TimeStamp