2 Implement authentication services for the authenticated variables.
4 Caution: This module requires additional review when modified.
5 This driver will have external input - variable data. It may be input in SMM mode.
6 This external input must be validated carefully to avoid security issue like
7 buffer overflow, integer overflow.
8 Variable attribute should also be checked to avoid authentication bypass.
9 The whole SMM authentication variable design relies on the integrity of flash part and SMM.
10 which is assumed to be protected by platform. All variable code and metadata in flash/SMM Memory
11 may not be modified without authorization. If platform fails to protect these resources,
12 the authentication service provided in this driver will be broken, and the behavior is undefined.
14 ProcessVarWithPk(), ProcessVarWithKek() and ProcessVariable() are the function to do
15 variable authentication.
17 VerifyTimeBasedPayloadAndUpdate() and VerifyCounterBasedPayload() are sub function to do verification.
18 They will do basic validation for authentication data structure, then call crypto library
19 to verify the signature.
21 Copyright (c) 2009 - 2015, Intel Corporation. All rights reserved.<BR>
22 This program and the accompanying materials
23 are licensed and made available under the terms and conditions of the BSD License
24 which accompanies this distribution. The full text of the license may be found at
25 http://opensource.org/licenses/bsd-license.php
27 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
28 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
32 #include "AuthServiceInternal.h"
35 // Public Exponent of RSA Key.
37 CONST UINT8 mRsaE
[] = { 0x01, 0x00, 0x01 };
40 // Requirement for different signature type which have been defined in UEFI spec.
41 // These data are used to perform SignatureList format check while setting PK/KEK variable.
43 EFI_SIGNATURE_ITEM mSupportSigItem
[] = {
44 //{SigType, SigHeaderSize, SigDataSize }
45 {EFI_CERT_SHA256_GUID
, 0, 32 },
46 {EFI_CERT_RSA2048_GUID
, 0, 256 },
47 {EFI_CERT_RSA2048_SHA256_GUID
, 0, 256 },
48 {EFI_CERT_SHA1_GUID
, 0, 20 },
49 {EFI_CERT_RSA2048_SHA1_GUID
, 0, 256 },
50 {EFI_CERT_X509_GUID
, 0, ((UINT32
) ~0)},
51 {EFI_CERT_SHA224_GUID
, 0, 28 },
52 {EFI_CERT_SHA384_GUID
, 0, 48 },
53 {EFI_CERT_SHA512_GUID
, 0, 64 },
54 {EFI_CERT_X509_SHA256_GUID
, 0, 48 },
55 {EFI_CERT_X509_SHA384_GUID
, 0, 64 },
56 {EFI_CERT_X509_SHA512_GUID
, 0, 80 }
60 Finds variable in storage blocks of volatile and non-volatile storage areas.
62 This code finds variable in storage blocks of volatile and non-volatile storage areas.
63 If VariableName is an empty string, then we just return the first
64 qualified variable without comparing VariableName and VendorGuid.
66 @param[in] VariableName Name of the variable to be found.
67 @param[in] VendorGuid Variable vendor GUID to be found.
68 @param[out] Data Pointer to data address.
69 @param[out] DataSize Pointer to data size.
71 @retval EFI_INVALID_PARAMETER If VariableName is not an empty string,
72 while VendorGuid is NULL.
73 @retval EFI_SUCCESS Variable successfully found.
74 @retval EFI_NOT_FOUND Variable not found
78 AuthServiceInternalFindVariable (
79 IN CHAR16
*VariableName
,
80 IN EFI_GUID
*VendorGuid
,
86 AUTH_VARIABLE_INFO AuthVariableInfo
;
88 ZeroMem (&AuthVariableInfo
, sizeof (AuthVariableInfo
));
89 Status
= mAuthVarLibContextIn
->FindVariable (
94 *Data
= AuthVariableInfo
.Data
;
95 *DataSize
= AuthVariableInfo
.DataSize
;
100 Update the variable region with Variable information.
102 @param[in] VariableName Name of variable.
103 @param[in] VendorGuid Guid of variable.
104 @param[in] Data Data pointer.
105 @param[in] DataSize Size of Data.
106 @param[in] Attributes Attribute value of the variable.
108 @retval EFI_SUCCESS The update operation is success.
109 @retval EFI_INVALID_PARAMETER Invalid parameter.
110 @retval EFI_WRITE_PROTECTED Variable is write-protected.
111 @retval EFI_OUT_OF_RESOURCES There is not enough resource.
115 AuthServiceInternalUpdateVariable (
116 IN CHAR16
*VariableName
,
117 IN EFI_GUID
*VendorGuid
,
123 AUTH_VARIABLE_INFO AuthVariableInfo
;
125 ZeroMem (&AuthVariableInfo
, sizeof (AuthVariableInfo
));
126 AuthVariableInfo
.VariableName
= VariableName
;
127 AuthVariableInfo
.VendorGuid
= VendorGuid
;
128 AuthVariableInfo
.Data
= Data
;
129 AuthVariableInfo
.DataSize
= DataSize
;
130 AuthVariableInfo
.Attributes
= Attributes
;
132 return mAuthVarLibContextIn
->UpdateVariable (
138 Update the variable region with Variable information.
140 @param[in] VariableName Name of variable.
141 @param[in] VendorGuid Guid of variable.
142 @param[in] Data Data pointer.
143 @param[in] DataSize Size of Data.
144 @param[in] Attributes Attribute value of the variable.
145 @param[in] KeyIndex Index of associated public key.
146 @param[in] MonotonicCount Value of associated monotonic count.
148 @retval EFI_SUCCESS The update operation is success.
149 @retval EFI_INVALID_PARAMETER Invalid parameter.
150 @retval EFI_WRITE_PROTECTED Variable is write-protected.
151 @retval EFI_OUT_OF_RESOURCES There is not enough resource.
155 AuthServiceInternalUpdateVariableWithMonotonicCount (
156 IN CHAR16
*VariableName
,
157 IN EFI_GUID
*VendorGuid
,
160 IN UINT32 Attributes
,
162 IN UINT64 MonotonicCount
165 AUTH_VARIABLE_INFO AuthVariableInfo
;
167 ZeroMem (&AuthVariableInfo
, sizeof (AuthVariableInfo
));
168 AuthVariableInfo
.VariableName
= VariableName
;
169 AuthVariableInfo
.VendorGuid
= VendorGuid
;
170 AuthVariableInfo
.Data
= Data
;
171 AuthVariableInfo
.DataSize
= DataSize
;
172 AuthVariableInfo
.Attributes
= Attributes
;
173 AuthVariableInfo
.PubKeyIndex
= KeyIndex
;
174 AuthVariableInfo
.MonotonicCount
= MonotonicCount
;
176 return mAuthVarLibContextIn
->UpdateVariable (
182 Update the variable region with Variable information.
184 @param[in] VariableName Name of variable.
185 @param[in] VendorGuid Guid of variable.
186 @param[in] Data Data pointer.
187 @param[in] DataSize Size of Data.
188 @param[in] Attributes Attribute value of the variable.
189 @param[in] TimeStamp Value of associated TimeStamp.
191 @retval EFI_SUCCESS The update operation is success.
192 @retval EFI_INVALID_PARAMETER Invalid parameter.
193 @retval EFI_WRITE_PROTECTED Variable is write-protected.
194 @retval EFI_OUT_OF_RESOURCES There is not enough resource.
198 AuthServiceInternalUpdateVariableWithTimeStamp (
199 IN CHAR16
*VariableName
,
200 IN EFI_GUID
*VendorGuid
,
203 IN UINT32 Attributes
,
204 IN EFI_TIME
*TimeStamp
207 EFI_STATUS FindStatus
;
210 AUTH_VARIABLE_INFO AuthVariableInfo
;
212 FindStatus
= AuthServiceInternalFindVariable (
220 // EFI_VARIABLE_APPEND_WRITE attribute only effects for existing variable
222 if (!EFI_ERROR (FindStatus
) && ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) != 0)) {
223 if ((CompareGuid (VendorGuid
, &gEfiImageSecurityDatabaseGuid
) &&
224 ((StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE
) == 0) || (StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE1
) == 0) ||
225 (StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE2
) == 0))) ||
226 (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, EFI_KEY_EXCHANGE_KEY_NAME
) == 0))) {
228 // For variables with formatted as EFI_SIGNATURE_LIST, the driver shall not perform an append of
229 // EFI_SIGNATURE_DATA values that are already part of the existing variable value.
231 FilterSignatureList (
240 ZeroMem (&AuthVariableInfo
, sizeof (AuthVariableInfo
));
241 AuthVariableInfo
.VariableName
= VariableName
;
242 AuthVariableInfo
.VendorGuid
= VendorGuid
;
243 AuthVariableInfo
.Data
= Data
;
244 AuthVariableInfo
.DataSize
= DataSize
;
245 AuthVariableInfo
.Attributes
= Attributes
;
246 AuthVariableInfo
.TimeStamp
= TimeStamp
;
247 return mAuthVarLibContextIn
->UpdateVariable (
253 Determine whether this operation needs a physical present user.
255 @param[in] VariableName Name of the Variable.
256 @param[in] VendorGuid GUID of the Variable.
258 @retval TRUE This variable is protected, only a physical present user could set this variable.
259 @retval FALSE This variable is not protected.
263 NeedPhysicallyPresent(
264 IN CHAR16
*VariableName
,
265 IN EFI_GUID
*VendorGuid
268 if ((CompareGuid (VendorGuid
, &gEfiSecureBootEnableDisableGuid
) && (StrCmp (VariableName
, EFI_SECURE_BOOT_ENABLE_NAME
) == 0))
269 || (CompareGuid (VendorGuid
, &gEfiCustomModeEnableGuid
) && (StrCmp (VariableName
, EFI_CUSTOM_MODE_NAME
) == 0))) {
277 Determine whether the platform is operating in Custom Secure Boot mode.
279 @retval TRUE The platform is operating in Custom mode.
280 @retval FALSE The platform is operating in Standard mode.
292 Status
= AuthServiceInternalFindVariable (EFI_CUSTOM_MODE_NAME
, &gEfiCustomModeEnableGuid
, &Data
, &DataSize
);
293 if (!EFI_ERROR (Status
) && (*(UINT8
*) Data
== CUSTOM_SECURE_BOOT_MODE
)) {
301 Get available public key index.
303 @param[in] PubKey Pointer to Public Key data.
305 @return Public key index, 0 if no any public key index available.
309 GetAvailableKeyIndex (
321 AUTH_VARIABLE_INFO AuthVariableInfo
;
324 Status
= AuthServiceInternalFindVariable (
326 &gEfiAuthenticatedVariableGuid
,
330 if (EFI_ERROR (Status
)) {
331 DEBUG ((EFI_D_ERROR
, "Get public key database variable failure, Status = %r\n", Status
));
335 if (mPubKeyNumber
== mMaxKeyNumber
) {
337 AuthVariableInfo
.VariableName
= Name
;
338 ZeroMem (&VendorGuid
, sizeof (VendorGuid
));
339 AuthVariableInfo
.VendorGuid
= &VendorGuid
;
342 // Collect valid key data.
345 Status
= mAuthVarLibContextIn
->FindNextVariable (AuthVariableInfo
.VariableName
, AuthVariableInfo
.VendorGuid
, &AuthVariableInfo
);
346 if (!EFI_ERROR (Status
)) {
347 if (AuthVariableInfo
.PubKeyIndex
!= 0) {
348 for (Ptr
= Data
; Ptr
< (Data
+ DataSize
); Ptr
+= sizeof (AUTHVAR_KEY_DB_DATA
)) {
349 if (ReadUnaligned32 (&(((AUTHVAR_KEY_DB_DATA
*) Ptr
)->KeyIndex
)) == AuthVariableInfo
.PubKeyIndex
) {
351 // Check if the key data has been collected.
353 for (Index
= 0; Index
< mPubKeyNumber
; Index
++) {
354 if (ReadUnaligned32 (&(((AUTHVAR_KEY_DB_DATA
*) mPubKeyStore
+ Index
)->KeyIndex
)) == AuthVariableInfo
.PubKeyIndex
) {
358 if (Index
== mPubKeyNumber
) {
362 CopyMem ((AUTHVAR_KEY_DB_DATA
*) mPubKeyStore
+ mPubKeyNumber
, Ptr
, sizeof (AUTHVAR_KEY_DB_DATA
));
370 } while (Status
!= EFI_NOT_FOUND
);
373 // No available space to add new public key.
375 if (mPubKeyNumber
== mMaxKeyNumber
) {
381 // Find available public key index.
383 for (KeyIndex
= 1; KeyIndex
<= mMaxKeyNumber
; KeyIndex
++) {
385 for (Ptr
= mPubKeyStore
; Ptr
< (mPubKeyStore
+ mPubKeyNumber
* sizeof (AUTHVAR_KEY_DB_DATA
)); Ptr
+= sizeof (AUTHVAR_KEY_DB_DATA
)) {
386 if (ReadUnaligned32 (&(((AUTHVAR_KEY_DB_DATA
*) Ptr
)->KeyIndex
)) == KeyIndex
) {
400 Add public key in store and return its index.
402 @param[in] PubKey Input pointer to Public Key data.
403 @param[in] VariableDataEntry The variable data entry.
405 @return Index of new added public key.
411 IN VARIABLE_ENTRY_CONSISTENCY
*VariableDataEntry
416 VARIABLE_ENTRY_CONSISTENCY PublicKeyEntry
;
420 if (PubKey
== NULL
) {
425 // Check whether the public key entry does exist.
427 for (Index
= 0; Index
< mPubKeyNumber
; Index
++) {
428 if (CompareMem (((AUTHVAR_KEY_DB_DATA
*) mPubKeyStore
+ Index
)->KeyData
, PubKey
, EFI_CERT_TYPE_RSA2048_SIZE
) == 0) {
429 return ReadUnaligned32 (&(((AUTHVAR_KEY_DB_DATA
*) mPubKeyStore
+ Index
)->KeyIndex
));
433 KeyIndex
= GetAvailableKeyIndex (PubKey
);
439 // Check the variable space for both public key and variable data.
441 PublicKeyEntry
.VariableSize
= (mPubKeyNumber
+ 1) * sizeof (AUTHVAR_KEY_DB_DATA
);
442 PublicKeyEntry
.Guid
= &gEfiAuthenticatedVariableGuid
;
443 PublicKeyEntry
.Name
= AUTHVAR_KEYDB_NAME
;
444 Attributes
= VARIABLE_ATTRIBUTE_NV_BS_RT
| EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
;
446 if (!mAuthVarLibContextIn
->CheckRemainingSpaceForConsistency (Attributes
, &PublicKeyEntry
, VariableDataEntry
, NULL
)) {
448 // No enough variable space.
453 WriteUnaligned32 (&(((AUTHVAR_KEY_DB_DATA
*) mPubKeyStore
+ mPubKeyNumber
)->KeyIndex
), KeyIndex
);
454 CopyMem (((AUTHVAR_KEY_DB_DATA
*) mPubKeyStore
+ mPubKeyNumber
)->KeyData
, PubKey
, EFI_CERT_TYPE_RSA2048_SIZE
);
458 // Update public key database variable.
460 Status
= AuthServiceInternalUpdateVariable (
462 &gEfiAuthenticatedVariableGuid
,
464 mPubKeyNumber
* sizeof (AUTHVAR_KEY_DB_DATA
),
467 if (EFI_ERROR (Status
)) {
468 DEBUG ((EFI_D_ERROR
, "Update public key database variable failure, Status = %r\n", Status
));
476 Verify data payload with AuthInfo in EFI_CERT_TYPE_RSA2048_SHA256_GUID type.
477 Follow the steps in UEFI2.2.
479 Caution: This function may receive untrusted input.
480 This function may be invoked in SMM mode, and datasize and data are external input.
481 This function will do basic validation, before parse the data.
482 This function will parse the authentication carefully to avoid security issues, like
483 buffer overflow, integer overflow.
485 @param[in] Data Pointer to data with AuthInfo.
486 @param[in] DataSize Size of Data.
487 @param[in] PubKey Public key used for verification.
489 @retval EFI_INVALID_PARAMETER Invalid parameter.
490 @retval EFI_SECURITY_VIOLATION If authentication failed.
491 @retval EFI_SUCCESS Authentication successful.
495 VerifyCounterBasedPayload (
502 EFI_VARIABLE_AUTHENTICATION
*CertData
;
503 EFI_CERT_BLOCK_RSA_2048_SHA256
*CertBlock
;
504 UINT8 Digest
[SHA256_DIGEST_SIZE
];
508 PayloadSize
= DataSize
- AUTHINFO_SIZE
;
513 if (Data
== NULL
|| PubKey
== NULL
) {
514 return EFI_INVALID_PARAMETER
;
517 CertData
= (EFI_VARIABLE_AUTHENTICATION
*) Data
;
518 CertBlock
= (EFI_CERT_BLOCK_RSA_2048_SHA256
*) (CertData
->AuthInfo
.CertData
);
521 // wCertificateType should be WIN_CERT_TYPE_EFI_GUID.
522 // Cert type should be EFI_CERT_TYPE_RSA2048_SHA256_GUID.
524 if ((CertData
->AuthInfo
.Hdr
.wCertificateType
!= WIN_CERT_TYPE_EFI_GUID
) ||
525 !CompareGuid (&CertData
->AuthInfo
.CertType
, &gEfiCertTypeRsa2048Sha256Guid
)) {
527 // Invalid AuthInfo type, return EFI_SECURITY_VIOLATION.
529 return EFI_SECURITY_VIOLATION
;
532 // Hash data payload with SHA256.
534 ZeroMem (Digest
, SHA256_DIGEST_SIZE
);
535 Status
= Sha256Init (mHashCtx
);
539 Status
= Sha256Update (mHashCtx
, Data
+ AUTHINFO_SIZE
, PayloadSize
);
546 Status
= Sha256Update (mHashCtx
, &PayloadSize
, sizeof (UINTN
));
551 // Hash Monotonic Count.
553 Status
= Sha256Update (mHashCtx
, &CertData
->MonotonicCount
, sizeof (UINT64
));
557 Status
= Sha256Final (mHashCtx
, Digest
);
562 // Generate & Initialize RSA Context.
565 ASSERT (Rsa
!= NULL
);
567 // Set RSA Key Components.
568 // NOTE: Only N and E are needed to be set as RSA public key for signature verification.
570 Status
= RsaSetKey (Rsa
, RsaKeyN
, PubKey
, EFI_CERT_TYPE_RSA2048_SIZE
);
574 Status
= RsaSetKey (Rsa
, RsaKeyE
, mRsaE
, sizeof (mRsaE
));
579 // Verify the signature.
581 Status
= RsaPkcs1Verify (
585 CertBlock
->Signature
,
586 EFI_CERT_TYPE_RSA2048_SHA256_SIZE
596 return EFI_SECURITY_VIOLATION
;
601 Update platform mode.
603 @param[in] Mode SETUP_MODE or USER_MODE.
605 @return EFI_INVALID_PARAMETER Invalid parameter.
606 @return EFI_SUCCESS Update platform mode successfully.
617 UINT8 SecureBootMode
;
618 UINT8 SecureBootEnable
;
619 UINTN VariableDataSize
;
621 Status
= AuthServiceInternalFindVariable (
623 &gEfiGlobalVariableGuid
,
627 if (EFI_ERROR (Status
)) {
632 // Update the value of SetupMode variable by a simple mem copy, this could avoid possible
633 // variable storage reclaim at runtime.
635 mPlatformMode
= (UINT8
) Mode
;
636 CopyMem (Data
, &mPlatformMode
, sizeof(UINT8
));
638 if (mAuthVarLibContextIn
->AtRuntime ()) {
640 // SecureBoot Variable indicates whether the platform firmware is operating
641 // in Secure boot mode (1) or not (0), so we should not change SecureBoot
642 // Variable in runtime.
648 // Check "SecureBoot" variable's existence.
649 // If it doesn't exist, firmware has no capability to perform driver signing verification,
650 // then set "SecureBoot" to 0.
652 Status
= AuthServiceInternalFindVariable (
653 EFI_SECURE_BOOT_MODE_NAME
,
654 &gEfiGlobalVariableGuid
,
659 // If "SecureBoot" variable exists, then check "SetupMode" variable update.
660 // If "SetupMode" variable is USER_MODE, "SecureBoot" variable is set to 1.
661 // If "SetupMode" variable is SETUP_MODE, "SecureBoot" variable is set to 0.
663 if (EFI_ERROR (Status
)) {
664 SecureBootMode
= SECURE_BOOT_MODE_DISABLE
;
666 if (mPlatformMode
== USER_MODE
) {
667 SecureBootMode
= SECURE_BOOT_MODE_ENABLE
;
668 } else if (mPlatformMode
== SETUP_MODE
) {
669 SecureBootMode
= SECURE_BOOT_MODE_DISABLE
;
671 return EFI_NOT_FOUND
;
675 Status
= AuthServiceInternalUpdateVariable (
676 EFI_SECURE_BOOT_MODE_NAME
,
677 &gEfiGlobalVariableGuid
,
680 EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
682 if (EFI_ERROR (Status
)) {
687 // Check "SecureBootEnable" variable's existence. It can enable/disable secure boot feature.
689 Status
= AuthServiceInternalFindVariable (
690 EFI_SECURE_BOOT_ENABLE_NAME
,
691 &gEfiSecureBootEnableDisableGuid
,
696 if (SecureBootMode
== SECURE_BOOT_MODE_ENABLE
) {
698 // Create the "SecureBootEnable" variable as secure boot is enabled.
700 SecureBootEnable
= SECURE_BOOT_ENABLE
;
701 VariableDataSize
= sizeof (SecureBootEnable
);
704 // Delete the "SecureBootEnable" variable if this variable exist as "SecureBoot"
705 // variable is not in secure boot state.
707 if (EFI_ERROR (Status
)) {
710 SecureBootEnable
= SECURE_BOOT_DISABLE
;
711 VariableDataSize
= 0;
714 Status
= AuthServiceInternalUpdateVariable (
715 EFI_SECURE_BOOT_ENABLE_NAME
,
716 &gEfiSecureBootEnableDisableGuid
,
719 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
725 Check input data form to make sure it is a valid EFI_SIGNATURE_LIST for PK/KEK/db/dbx/dbt variable.
727 @param[in] VariableName Name of Variable to be check.
728 @param[in] VendorGuid Variable vendor GUID.
729 @param[in] Data Point to the variable data to be checked.
730 @param[in] DataSize Size of Data.
732 @return EFI_INVALID_PARAMETER Invalid signature list format.
733 @return EFI_SUCCESS Passed signature list format check successfully.
737 CheckSignatureListFormat(
738 IN CHAR16
*VariableName
,
739 IN EFI_GUID
*VendorGuid
,
744 EFI_SIGNATURE_LIST
*SigList
;
750 EFI_SIGNATURE_DATA
*CertData
;
757 ASSERT (VariableName
!= NULL
&& VendorGuid
!= NULL
&& Data
!= NULL
);
759 if (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, EFI_PLATFORM_KEY_NAME
) == 0)){
761 } else if ((CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, EFI_KEY_EXCHANGE_KEY_NAME
) == 0)) ||
762 (CompareGuid (VendorGuid
, &gEfiImageSecurityDatabaseGuid
) &&
763 ((StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE
) == 0) || (StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE1
) == 0) ||
764 (StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE2
) == 0)))) {
771 SigList
= (EFI_SIGNATURE_LIST
*) Data
;
772 SigDataSize
= DataSize
;
776 // Walk throuth the input signature list and check the data format.
777 // If any signature is incorrectly formed, the whole check will fail.
779 while ((SigDataSize
> 0) && (SigDataSize
>= SigList
->SignatureListSize
)) {
780 for (Index
= 0; Index
< (sizeof (mSupportSigItem
) / sizeof (EFI_SIGNATURE_ITEM
)); Index
++ ) {
781 if (CompareGuid (&SigList
->SignatureType
, &mSupportSigItem
[Index
].SigType
)) {
783 // The value of SignatureSize should always be 16 (size of SignatureOwner
784 // component) add the data length according to signature type.
786 if (mSupportSigItem
[Index
].SigDataSize
!= ((UINT32
) ~0) &&
787 (SigList
->SignatureSize
- sizeof (EFI_GUID
)) != mSupportSigItem
[Index
].SigDataSize
) {
788 return EFI_INVALID_PARAMETER
;
790 if (mSupportSigItem
[Index
].SigHeaderSize
!= ((UINT32
) ~0) &&
791 SigList
->SignatureHeaderSize
!= mSupportSigItem
[Index
].SigHeaderSize
) {
792 return EFI_INVALID_PARAMETER
;
798 if (Index
== (sizeof (mSupportSigItem
) / sizeof (EFI_SIGNATURE_ITEM
))) {
800 // Undefined signature type.
802 return EFI_INVALID_PARAMETER
;
805 if (CompareGuid (&SigList
->SignatureType
, &gEfiCertX509Guid
)) {
807 // Try to retrieve the RSA public key from the X.509 certificate.
808 // If this operation fails, it's not a valid certificate.
810 RsaContext
= RsaNew ();
811 if (RsaContext
== NULL
) {
812 return EFI_INVALID_PARAMETER
;
814 CertData
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) SigList
+ sizeof (EFI_SIGNATURE_LIST
) + SigList
->SignatureHeaderSize
);
815 CertLen
= SigList
->SignatureSize
- sizeof (EFI_GUID
);
816 if (!RsaGetPublicKeyFromX509 (CertData
->SignatureData
, CertLen
, &RsaContext
)) {
817 RsaFree (RsaContext
);
818 return EFI_INVALID_PARAMETER
;
820 RsaFree (RsaContext
);
823 if ((SigList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - SigList
->SignatureHeaderSize
) % SigList
->SignatureSize
!= 0) {
824 return EFI_INVALID_PARAMETER
;
826 SigCount
+= (SigList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - SigList
->SignatureHeaderSize
) / SigList
->SignatureSize
;
828 SigDataSize
-= SigList
->SignatureListSize
;
829 SigList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) SigList
+ SigList
->SignatureListSize
);
832 if (((UINTN
) SigList
- (UINTN
) Data
) != DataSize
) {
833 return EFI_INVALID_PARAMETER
;
836 if (IsPk
&& SigCount
> 1) {
837 return EFI_INVALID_PARAMETER
;
844 Update "VendorKeys" variable to record the out of band secure boot key modification.
846 @return EFI_SUCCESS Variable is updated successfully.
847 @return Others Failed to update variable.
851 VendorKeyIsModified (
857 if (mVendorKeyState
== VENDOR_KEYS_MODIFIED
) {
860 mVendorKeyState
= VENDOR_KEYS_MODIFIED
;
862 Status
= AuthServiceInternalUpdateVariable (
863 EFI_VENDOR_KEYS_NV_VARIABLE_NAME
,
864 &gEfiVendorKeysNvGuid
,
867 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
869 if (EFI_ERROR (Status
)) {
873 return AuthServiceInternalUpdateVariable (
874 EFI_VENDOR_KEYS_VARIABLE_NAME
,
875 &gEfiGlobalVariableGuid
,
878 EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
883 Process variable with platform key for verification.
885 Caution: This function may receive untrusted input.
886 This function may be invoked in SMM mode, and datasize and data are external input.
887 This function will do basic validation, before parse the data.
888 This function will parse the authentication carefully to avoid security issues, like
889 buffer overflow, integer overflow.
890 This function will check attribute carefully to avoid authentication bypass.
892 @param[in] VariableName Name of Variable to be found.
893 @param[in] VendorGuid Variable vendor GUID.
894 @param[in] Data Data pointer.
895 @param[in] DataSize Size of Data found. If size is less than the
896 data, this value contains the required size.
897 @param[in] Attributes Attribute value of the variable
898 @param[in] IsPk Indicate whether it is to process pk.
900 @return EFI_INVALID_PARAMETER Invalid parameter.
901 @return EFI_SECURITY_VIOLATION The variable does NOT pass the validation.
902 check carried out by the firmware.
903 @return EFI_SUCCESS Variable passed validation successfully.
908 IN CHAR16
*VariableName
,
909 IN EFI_GUID
*VendorGuid
,
912 IN UINT32 Attributes OPTIONAL
,
921 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0 ||
922 (Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) == 0) {
924 // PK, KEK and db/dbx/dbt should set EFI_VARIABLE_NON_VOLATILE attribute and should be a time-based
925 // authenticated variable.
927 return EFI_INVALID_PARAMETER
;
931 if ((InCustomMode() && UserPhysicalPresent()) || (mPlatformMode
== SETUP_MODE
&& !IsPk
)) {
932 Payload
= (UINT8
*) Data
+ AUTHINFO2_SIZE (Data
);
933 PayloadSize
= DataSize
- AUTHINFO2_SIZE (Data
);
934 if (PayloadSize
== 0) {
938 Status
= CheckSignatureListFormat(VariableName
, VendorGuid
, Payload
, PayloadSize
);
939 if (EFI_ERROR (Status
)) {
943 Status
= AuthServiceInternalUpdateVariableWithTimeStamp (
949 &((EFI_VARIABLE_AUTHENTICATION_2
*) Data
)->TimeStamp
951 if (EFI_ERROR(Status
)) {
955 if ((mPlatformMode
!= SETUP_MODE
) || IsPk
) {
956 Status
= VendorKeyIsModified ();
958 } else if (mPlatformMode
== USER_MODE
) {
960 // Verify against X509 Cert in PK database.
962 Status
= VerifyTimeBasedPayloadAndUpdate (
973 // Verify against the certificate in data payload.
975 Status
= VerifyTimeBasedPayloadAndUpdate (
986 if (!EFI_ERROR(Status
) && IsPk
) {
987 if (mPlatformMode
== SETUP_MODE
&& !Del
) {
989 // If enroll PK in setup mode, need change to user mode.
991 Status
= UpdatePlatformMode (USER_MODE
);
992 } else if (mPlatformMode
== USER_MODE
&& Del
){
994 // If delete PK in user mode, need change to setup mode.
996 Status
= UpdatePlatformMode (SETUP_MODE
);
1004 Process variable with key exchange key for verification.
1006 Caution: This function may receive untrusted input.
1007 This function may be invoked in SMM mode, and datasize and data are external input.
1008 This function will do basic validation, before parse the data.
1009 This function will parse the authentication carefully to avoid security issues, like
1010 buffer overflow, integer overflow.
1011 This function will check attribute carefully to avoid authentication bypass.
1013 @param[in] VariableName Name of Variable to be found.
1014 @param[in] VendorGuid Variable vendor GUID.
1015 @param[in] Data Data pointer.
1016 @param[in] DataSize Size of Data found. If size is less than the
1017 data, this value contains the required size.
1018 @param[in] Attributes Attribute value of the variable.
1020 @return EFI_INVALID_PARAMETER Invalid parameter.
1021 @return EFI_SECURITY_VIOLATION The variable does NOT pass the validation
1022 check carried out by the firmware.
1023 @return EFI_SUCCESS Variable pass validation successfully.
1028 IN CHAR16
*VariableName
,
1029 IN EFI_GUID
*VendorGuid
,
1032 IN UINT32 Attributes OPTIONAL
1039 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0 ||
1040 (Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) == 0) {
1042 // DB, DBX and DBT should set EFI_VARIABLE_NON_VOLATILE attribute and should be a time-based
1043 // authenticated variable.
1045 return EFI_INVALID_PARAMETER
;
1048 Status
= EFI_SUCCESS
;
1049 if (mPlatformMode
== USER_MODE
&& !(InCustomMode() && UserPhysicalPresent())) {
1051 // Time-based, verify against X509 Cert KEK.
1053 return VerifyTimeBasedPayloadAndUpdate (
1064 // If in setup mode or custom secure boot mode, no authentication needed.
1066 Payload
= (UINT8
*) Data
+ AUTHINFO2_SIZE (Data
);
1067 PayloadSize
= DataSize
- AUTHINFO2_SIZE (Data
);
1069 Status
= CheckSignatureListFormat(VariableName
, VendorGuid
, Payload
, PayloadSize
);
1070 if (EFI_ERROR (Status
)) {
1074 Status
= AuthServiceInternalUpdateVariableWithTimeStamp (
1080 &((EFI_VARIABLE_AUTHENTICATION_2
*) Data
)->TimeStamp
1082 if (EFI_ERROR (Status
)) {
1086 if (mPlatformMode
!= SETUP_MODE
) {
1087 Status
= VendorKeyIsModified ();
1095 Check if it is to delete auth variable.
1097 @param[in] OrgAttributes Original attribute value of the variable.
1098 @param[in] Data Data pointer.
1099 @param[in] DataSize Size of Data.
1100 @param[in] Attributes Attribute value of the variable.
1102 @retval TRUE It is to delete auth variable.
1103 @retval FALSE It is not to delete auth variable.
1107 IsDeleteAuthVariable (
1108 IN UINT32 OrgAttributes
,
1111 IN UINT32 Attributes
1120 // To delete a variable created with the EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
1121 // or the EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS attribute,
1122 // SetVariable must be used with attributes matching the existing variable
1123 // and the DataSize set to the size of the AuthInfo descriptor.
1125 if ((Attributes
== OrgAttributes
) &&
1126 ((Attributes
& (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
)) != 0)) {
1127 if ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) {
1128 PayloadSize
= DataSize
- AUTHINFO2_SIZE (Data
);
1129 if (PayloadSize
== 0) {
1133 PayloadSize
= DataSize
- AUTHINFO_SIZE
;
1134 if (PayloadSize
== 0) {
1144 Process variable with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS/EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set
1146 Caution: This function may receive untrusted input.
1147 This function may be invoked in SMM mode, and datasize and data are external input.
1148 This function will do basic validation, before parse the data.
1149 This function will parse the authentication carefully to avoid security issues, like
1150 buffer overflow, integer overflow.
1151 This function will check attribute carefully to avoid authentication bypass.
1153 @param[in] VariableName Name of the variable.
1154 @param[in] VendorGuid Variable vendor GUID.
1155 @param[in] Data Data pointer.
1156 @param[in] DataSize Size of Data.
1157 @param[in] Attributes Attribute value of the variable.
1159 @return EFI_INVALID_PARAMETER Invalid parameter.
1160 @return EFI_WRITE_PROTECTED Variable is write-protected and needs authentication with
1161 EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS set.
1162 @return EFI_OUT_OF_RESOURCES The Database to save the public key is full.
1163 @return EFI_SECURITY_VIOLATION The variable is with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
1164 set, but the AuthInfo does NOT pass the validation
1165 check carried out by the firmware.
1166 @return EFI_SUCCESS Variable is not write-protected or pass validation successfully.
1171 IN CHAR16
*VariableName
,
1172 IN EFI_GUID
*VendorGuid
,
1175 IN UINT32 Attributes OPTIONAL
1180 BOOLEAN IsFirstTime
;
1182 EFI_VARIABLE_AUTHENTICATION
*CertData
;
1183 EFI_CERT_BLOCK_RSA_2048_SHA256
*CertBlock
;
1185 UINT64 MonotonicCount
;
1186 VARIABLE_ENTRY_CONSISTENCY VariableDataEntry
;
1188 AUTH_VARIABLE_INFO OrgVariableInfo
;
1195 Status
= EFI_SUCCESS
;
1197 ZeroMem (&OrgVariableInfo
, sizeof (OrgVariableInfo
));
1198 Status
= mAuthVarLibContextIn
->FindVariable (
1204 if ((!EFI_ERROR (Status
)) && IsDeleteAuthVariable (OrgVariableInfo
.Attributes
, Data
, DataSize
, Attributes
) && UserPhysicalPresent()) {
1206 // Allow the delete operation of common authenticated variable at user physical presence.
1208 if ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) {
1209 Status
= DeleteCertsFromDb (VariableName
, VendorGuid
);
1211 if (!EFI_ERROR (Status
)) {
1212 Status
= AuthServiceInternalUpdateVariable (
1223 if (NeedPhysicallyPresent (VariableName
, VendorGuid
) && !UserPhysicalPresent()) {
1225 // This variable is protected, only physical present user could modify its value.
1227 return EFI_SECURITY_VIOLATION
;
1231 // A time-based authenticated variable and a count-based authenticated variable
1232 // can't be updated by each other.
1234 if (OrgVariableInfo
.Data
!= NULL
) {
1235 if (((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) &&
1236 ((OrgVariableInfo
.Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0)) {
1237 return EFI_SECURITY_VIOLATION
;
1240 if (((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) &&
1241 ((OrgVariableInfo
.Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0)) {
1242 return EFI_SECURITY_VIOLATION
;
1247 // Process Time-based Authenticated variable.
1249 if ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) {
1250 return VerifyTimeBasedPayloadAndUpdate (
1262 // Determine if first time SetVariable with the EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS.
1264 if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) {
1266 // Determine current operation type.
1268 if (DataSize
== AUTHINFO_SIZE
) {
1272 // Determine whether this is the first time with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS set.
1274 if (OrgVariableInfo
.Data
== NULL
) {
1276 } else if ((OrgVariableInfo
.Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) == 0) {
1279 KeyIndex
= OrgVariableInfo
.PubKeyIndex
;
1280 IsFirstTime
= FALSE
;
1282 } else if ((OrgVariableInfo
.Data
!= NULL
) &&
1283 ((OrgVariableInfo
.Attributes
& (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
)) != 0)
1286 // If the variable is already write-protected, it always needs authentication before update.
1288 return EFI_WRITE_PROTECTED
;
1291 // If without EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS, set and attributes collision.
1292 // That means it is not authenticated variable, just update variable as usual.
1294 Status
= AuthServiceInternalUpdateVariable (VariableName
, VendorGuid
, Data
, DataSize
, Attributes
);
1299 // Get PubKey and check Monotonic Count value corresponding to the variable.
1301 CertData
= (EFI_VARIABLE_AUTHENTICATION
*) Data
;
1302 CertBlock
= (EFI_CERT_BLOCK_RSA_2048_SHA256
*) (CertData
->AuthInfo
.CertData
);
1303 PubKey
= CertBlock
->PublicKey
;
1306 // Update Monotonic Count value.
1308 MonotonicCount
= CertData
->MonotonicCount
;
1312 // 2 cases need to check here
1313 // 1. Internal PubKey variable. PubKeyIndex is always 0
1314 // 2. Other counter-based AuthVariable. Check input PubKey.
1316 if (KeyIndex
== 0) {
1317 return EFI_SECURITY_VIOLATION
;
1319 for (Index
= 0; Index
< mPubKeyNumber
; Index
++) {
1320 if (ReadUnaligned32 (&(((AUTHVAR_KEY_DB_DATA
*) mPubKeyStore
+ Index
)->KeyIndex
)) == KeyIndex
) {
1321 if (CompareMem (((AUTHVAR_KEY_DB_DATA
*) mPubKeyStore
+ Index
)->KeyData
, PubKey
, EFI_CERT_TYPE_RSA2048_SIZE
) == 0) {
1324 return EFI_SECURITY_VIOLATION
;
1328 if (Index
== mPubKeyNumber
) {
1329 return EFI_SECURITY_VIOLATION
;
1333 // Compare the current monotonic count and ensure that it is greater than the last SetVariable
1334 // operation with the EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS attribute set.
1336 if (MonotonicCount
<= OrgVariableInfo
.MonotonicCount
) {
1338 // Monotonic count check fail, suspicious replay attack, return EFI_SECURITY_VIOLATION.
1340 return EFI_SECURITY_VIOLATION
;
1344 // Verify the certificate in Data payload.
1346 Status
= VerifyCounterBasedPayload (Data
, DataSize
, PubKey
);
1347 if (EFI_ERROR (Status
)) {
1352 // Now, the signature has been verified!
1354 if (IsFirstTime
&& !IsDeletion
) {
1355 VariableDataEntry
.VariableSize
= DataSize
- AUTHINFO_SIZE
;
1356 VariableDataEntry
.Guid
= VendorGuid
;
1357 VariableDataEntry
.Name
= VariableName
;
1360 // Update public key database variable if need.
1362 KeyIndex
= AddPubKeyInStore (PubKey
, &VariableDataEntry
);
1363 if (KeyIndex
== 0) {
1364 return EFI_OUT_OF_RESOURCES
;
1369 // Verification pass.
1371 return AuthServiceInternalUpdateVariableWithMonotonicCount (VariableName
, VendorGuid
, (UINT8
*)Data
+ AUTHINFO_SIZE
, DataSize
- AUTHINFO_SIZE
, Attributes
, KeyIndex
, MonotonicCount
);
1375 Filter out the duplicated EFI_SIGNATURE_DATA from the new data by comparing to the original data.
1377 @param[in] Data Pointer to original EFI_SIGNATURE_LIST.
1378 @param[in] DataSize Size of Data buffer.
1379 @param[in, out] NewData Pointer to new EFI_SIGNATURE_LIST.
1380 @param[in, out] NewDataSize Size of NewData buffer.
1384 FilterSignatureList (
1387 IN OUT VOID
*NewData
,
1388 IN OUT UINTN
*NewDataSize
1391 EFI_SIGNATURE_LIST
*CertList
;
1392 EFI_SIGNATURE_DATA
*Cert
;
1394 EFI_SIGNATURE_LIST
*NewCertList
;
1395 EFI_SIGNATURE_DATA
*NewCert
;
1402 UINTN SignatureListSize
;
1408 if (*NewDataSize
== 0) {
1412 TempDataSize
= *NewDataSize
;
1413 Status
= mAuthVarLibContextIn
->GetScratchBuffer (&TempDataSize
, (VOID
**) &TempData
);
1414 if (EFI_ERROR (Status
)) {
1415 return EFI_OUT_OF_RESOURCES
;
1420 NewCertList
= (EFI_SIGNATURE_LIST
*) NewData
;
1421 while ((*NewDataSize
> 0) && (*NewDataSize
>= NewCertList
->SignatureListSize
)) {
1422 NewCert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) NewCertList
+ sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
);
1423 NewCertCount
= (NewCertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - NewCertList
->SignatureHeaderSize
) / NewCertList
->SignatureSize
;
1426 for (Index
= 0; Index
< NewCertCount
; Index
++) {
1430 CertList
= (EFI_SIGNATURE_LIST
*) Data
;
1431 while ((Size
> 0) && (Size
>= CertList
->SignatureListSize
)) {
1432 if (CompareGuid (&CertList
->SignatureType
, &NewCertList
->SignatureType
) &&
1433 (CertList
->SignatureSize
== NewCertList
->SignatureSize
)) {
1434 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
1435 CertCount
= (CertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - CertList
->SignatureHeaderSize
) / CertList
->SignatureSize
;
1436 for (Index2
= 0; Index2
< CertCount
; Index2
++) {
1438 // Iterate each Signature Data in this Signature List.
1440 if (CompareMem (NewCert
, Cert
, CertList
->SignatureSize
) == 0) {
1444 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) Cert
+ CertList
->SignatureSize
);
1451 Size
-= CertList
->SignatureListSize
;
1452 CertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) CertList
+ CertList
->SignatureListSize
);
1457 // New EFI_SIGNATURE_DATA, keep it.
1459 if (CopiedCount
== 0) {
1461 // Copy EFI_SIGNATURE_LIST header for only once.
1463 CopyMem (Tail
, NewCertList
, sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
);
1464 Tail
= Tail
+ sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
;
1467 CopyMem (Tail
, NewCert
, NewCertList
->SignatureSize
);
1468 Tail
+= NewCertList
->SignatureSize
;
1472 NewCert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) NewCert
+ NewCertList
->SignatureSize
);
1476 // Update SignatureListSize in the kept EFI_SIGNATURE_LIST.
1478 if (CopiedCount
!= 0) {
1479 SignatureListSize
= sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
+ (CopiedCount
* NewCertList
->SignatureSize
);
1480 CertList
= (EFI_SIGNATURE_LIST
*) (Tail
- SignatureListSize
);
1481 CertList
->SignatureListSize
= (UINT32
) SignatureListSize
;
1484 *NewDataSize
-= NewCertList
->SignatureListSize
;
1485 NewCertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) NewCertList
+ NewCertList
->SignatureListSize
);
1488 TempDataSize
= (Tail
- (UINT8
*) TempData
);
1490 CopyMem (NewData
, TempData
, TempDataSize
);
1491 *NewDataSize
= TempDataSize
;
1497 Compare two EFI_TIME data.
1500 @param FirstTime A pointer to the first EFI_TIME data.
1501 @param SecondTime A pointer to the second EFI_TIME data.
1503 @retval TRUE The FirstTime is not later than the SecondTime.
1504 @retval FALSE The FirstTime is later than the SecondTime.
1508 AuthServiceInternalCompareTimeStamp (
1509 IN EFI_TIME
*FirstTime
,
1510 IN EFI_TIME
*SecondTime
1513 if (FirstTime
->Year
!= SecondTime
->Year
) {
1514 return (BOOLEAN
) (FirstTime
->Year
< SecondTime
->Year
);
1515 } else if (FirstTime
->Month
!= SecondTime
->Month
) {
1516 return (BOOLEAN
) (FirstTime
->Month
< SecondTime
->Month
);
1517 } else if (FirstTime
->Day
!= SecondTime
->Day
) {
1518 return (BOOLEAN
) (FirstTime
->Day
< SecondTime
->Day
);
1519 } else if (FirstTime
->Hour
!= SecondTime
->Hour
) {
1520 return (BOOLEAN
) (FirstTime
->Hour
< SecondTime
->Hour
);
1521 } else if (FirstTime
->Minute
!= SecondTime
->Minute
) {
1522 return (BOOLEAN
) (FirstTime
->Minute
< SecondTime
->Minute
);
1525 return (BOOLEAN
) (FirstTime
->Second
<= SecondTime
->Second
);
1529 Find matching signer's certificates for common authenticated variable
1530 by corresponding VariableName and VendorGuid from "certdb".
1532 The data format of "certdb":
1534 // UINT32 CertDbListSize;
1535 // /// AUTH_CERT_DB_DATA Certs1[];
1536 // /// AUTH_CERT_DB_DATA Certs2[];
1538 // /// AUTH_CERT_DB_DATA Certsn[];
1541 @param[in] VariableName Name of authenticated Variable.
1542 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1543 @param[in] Data Pointer to variable "certdb".
1544 @param[in] DataSize Size of variable "certdb".
1545 @param[out] CertOffset Offset of matching CertData, from starting of Data.
1546 @param[out] CertDataSize Length of CertData in bytes.
1547 @param[out] CertNodeOffset Offset of matching AUTH_CERT_DB_DATA , from
1549 @param[out] CertNodeSize Length of AUTH_CERT_DB_DATA in bytes.
1551 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1552 @retval EFI_NOT_FOUND Fail to find matching certs.
1553 @retval EFI_SUCCESS Find matching certs and output parameters.
1558 IN CHAR16
*VariableName
,
1559 IN EFI_GUID
*VendorGuid
,
1562 OUT UINT32
*CertOffset
, OPTIONAL
1563 OUT UINT32
*CertDataSize
, OPTIONAL
1564 OUT UINT32
*CertNodeOffset
,OPTIONAL
1565 OUT UINT32
*CertNodeSize OPTIONAL
1569 AUTH_CERT_DB_DATA
*Ptr
;
1573 UINT32 CertDbListSize
;
1575 if ((VariableName
== NULL
) || (VendorGuid
== NULL
) || (Data
== NULL
)) {
1576 return EFI_INVALID_PARAMETER
;
1580 // Check whether DataSize matches recorded CertDbListSize.
1582 if (DataSize
< sizeof (UINT32
)) {
1583 return EFI_INVALID_PARAMETER
;
1586 CertDbListSize
= ReadUnaligned32 ((UINT32
*) Data
);
1588 if (CertDbListSize
!= (UINT32
) DataSize
) {
1589 return EFI_INVALID_PARAMETER
;
1592 Offset
= sizeof (UINT32
);
1595 // Get corresponding certificates by VendorGuid and VariableName.
1597 while (Offset
< (UINT32
) DataSize
) {
1598 Ptr
= (AUTH_CERT_DB_DATA
*) (Data
+ Offset
);
1600 // Check whether VendorGuid matches.
1602 if (CompareGuid (&Ptr
->VendorGuid
, VendorGuid
)) {
1603 NodeSize
= ReadUnaligned32 (&Ptr
->CertNodeSize
);
1604 NameSize
= ReadUnaligned32 (&Ptr
->NameSize
);
1605 CertSize
= ReadUnaligned32 (&Ptr
->CertDataSize
);
1607 if (NodeSize
!= sizeof (EFI_GUID
) + sizeof (UINT32
) * 3 + CertSize
+
1608 sizeof (CHAR16
) * NameSize
) {
1609 return EFI_INVALID_PARAMETER
;
1612 Offset
= Offset
+ sizeof (EFI_GUID
) + sizeof (UINT32
) * 3;
1614 // Check whether VariableName matches.
1616 if ((NameSize
== StrLen (VariableName
)) &&
1617 (CompareMem (Data
+ Offset
, VariableName
, NameSize
* sizeof (CHAR16
)) == 0)) {
1618 Offset
= Offset
+ NameSize
* sizeof (CHAR16
);
1620 if (CertOffset
!= NULL
) {
1621 *CertOffset
= Offset
;
1624 if (CertDataSize
!= NULL
) {
1625 *CertDataSize
= CertSize
;
1628 if (CertNodeOffset
!= NULL
) {
1629 *CertNodeOffset
= (UINT32
) ((UINT8
*) Ptr
- Data
);
1632 if (CertNodeSize
!= NULL
) {
1633 *CertNodeSize
= NodeSize
;
1638 Offset
= Offset
+ NameSize
* sizeof (CHAR16
) + CertSize
;
1641 NodeSize
= ReadUnaligned32 (&Ptr
->CertNodeSize
);
1642 Offset
= Offset
+ NodeSize
;
1646 return EFI_NOT_FOUND
;
1650 Retrieve signer's certificates for common authenticated variable
1651 by corresponding VariableName and VendorGuid from "certdb".
1653 @param[in] VariableName Name of authenticated Variable.
1654 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1655 @param[out] CertData Pointer to signer's certificates.
1656 @param[out] CertDataSize Length of CertData in bytes.
1658 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1659 @retval EFI_NOT_FOUND Fail to find "certdb" or matching certs.
1660 @retval EFI_SUCCESS Get signer's certificates successfully.
1665 IN CHAR16
*VariableName
,
1666 IN EFI_GUID
*VendorGuid
,
1667 OUT UINT8
**CertData
,
1668 OUT UINT32
*CertDataSize
1676 if ((VariableName
== NULL
) || (VendorGuid
== NULL
) || (CertData
== NULL
) || (CertDataSize
== NULL
)) {
1677 return EFI_INVALID_PARAMETER
;
1681 // Get variable "certdb".
1683 Status
= AuthServiceInternalFindVariable (
1689 if (EFI_ERROR (Status
)) {
1693 if ((DataSize
== 0) || (Data
== NULL
)) {
1695 return EFI_NOT_FOUND
;
1698 Status
= FindCertsFromDb (
1709 if (EFI_ERROR (Status
)) {
1713 *CertData
= Data
+ CertOffset
;
1718 Delete matching signer's certificates when deleting common authenticated
1719 variable by corresponding VariableName and VendorGuid from "certdb".
1721 @param[in] VariableName Name of authenticated Variable.
1722 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1724 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1725 @retval EFI_NOT_FOUND Fail to find "certdb" or matching certs.
1726 @retval EFI_OUT_OF_RESOURCES The operation is failed due to lack of resources.
1727 @retval EFI_SUCCESS The operation is completed successfully.
1732 IN CHAR16
*VariableName
,
1733 IN EFI_GUID
*VendorGuid
1740 UINT32 CertNodeOffset
;
1741 UINT32 CertNodeSize
;
1743 UINT32 NewCertDbSize
;
1745 if ((VariableName
== NULL
) || (VendorGuid
== NULL
)) {
1746 return EFI_INVALID_PARAMETER
;
1750 // Get variable "certdb".
1752 Status
= AuthServiceInternalFindVariable (
1758 if (EFI_ERROR (Status
)) {
1762 if ((DataSize
== 0) || (Data
== NULL
)) {
1764 return EFI_NOT_FOUND
;
1767 if (DataSize
== sizeof (UINT32
)) {
1769 // There is no certs in certdb.
1775 // Get corresponding cert node from certdb.
1777 Status
= FindCertsFromDb (
1788 if (EFI_ERROR (Status
)) {
1792 if (DataSize
< (CertNodeOffset
+ CertNodeSize
)) {
1793 return EFI_NOT_FOUND
;
1797 // Construct new data content of variable "certdb".
1799 NewCertDbSize
= (UINT32
) DataSize
- CertNodeSize
;
1800 NewCertDb
= (UINT8
*) mCertDbStore
;
1803 // Copy the DB entries before deleting node.
1805 CopyMem (NewCertDb
, Data
, CertNodeOffset
);
1807 // Update CertDbListSize.
1809 CopyMem (NewCertDb
, &NewCertDbSize
, sizeof (UINT32
));
1811 // Copy the DB entries after deleting node.
1813 if (DataSize
> (CertNodeOffset
+ CertNodeSize
)) {
1815 NewCertDb
+ CertNodeOffset
,
1816 Data
+ CertNodeOffset
+ CertNodeSize
,
1817 DataSize
- CertNodeOffset
- CertNodeSize
1824 VarAttr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
1825 Status
= AuthServiceInternalUpdateVariable (
1837 Insert signer's certificates for common authenticated variable with VariableName
1838 and VendorGuid in AUTH_CERT_DB_DATA to "certdb".
1840 @param[in] VariableName Name of authenticated Variable.
1841 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1842 @param[in] CertData Pointer to signer's certificates.
1843 @param[in] CertDataSize Length of CertData in bytes.
1845 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1846 @retval EFI_ACCESS_DENIED An AUTH_CERT_DB_DATA entry with same VariableName
1847 and VendorGuid already exists.
1848 @retval EFI_OUT_OF_RESOURCES The operation is failed due to lack of resources.
1849 @retval EFI_SUCCESS Insert an AUTH_CERT_DB_DATA entry to "certdb"
1854 IN CHAR16
*VariableName
,
1855 IN EFI_GUID
*VendorGuid
,
1857 IN UINTN CertDataSize
1865 UINT32 NewCertDbSize
;
1866 UINT32 CertNodeSize
;
1868 AUTH_CERT_DB_DATA
*Ptr
;
1870 if ((VariableName
== NULL
) || (VendorGuid
== NULL
) || (CertData
== NULL
)) {
1871 return EFI_INVALID_PARAMETER
;
1875 // Get variable "certdb".
1877 Status
= AuthServiceInternalFindVariable (
1883 if (EFI_ERROR (Status
)) {
1887 if ((DataSize
== 0) || (Data
== NULL
)) {
1889 return EFI_NOT_FOUND
;
1893 // Find whether matching cert node already exists in "certdb".
1894 // If yes return error.
1896 Status
= FindCertsFromDb (
1907 if (!EFI_ERROR (Status
)) {
1909 return EFI_ACCESS_DENIED
;
1913 // Construct new data content of variable "certdb".
1915 NameSize
= (UINT32
) StrLen (VariableName
);
1916 CertNodeSize
= sizeof (AUTH_CERT_DB_DATA
) + (UINT32
) CertDataSize
+ NameSize
* sizeof (CHAR16
);
1917 NewCertDbSize
= (UINT32
) DataSize
+ CertNodeSize
;
1918 if (NewCertDbSize
> mMaxCertDbSize
) {
1919 return EFI_OUT_OF_RESOURCES
;
1921 NewCertDb
= (UINT8
*) mCertDbStore
;
1924 // Copy the DB entries before inserting node.
1926 CopyMem (NewCertDb
, Data
, DataSize
);
1928 // Update CertDbListSize.
1930 CopyMem (NewCertDb
, &NewCertDbSize
, sizeof (UINT32
));
1932 // Construct new cert node.
1934 Ptr
= (AUTH_CERT_DB_DATA
*) (NewCertDb
+ DataSize
);
1935 CopyGuid (&Ptr
->VendorGuid
, VendorGuid
);
1936 CopyMem (&Ptr
->CertNodeSize
, &CertNodeSize
, sizeof (UINT32
));
1937 CopyMem (&Ptr
->NameSize
, &NameSize
, sizeof (UINT32
));
1938 CopyMem (&Ptr
->CertDataSize
, &CertDataSize
, sizeof (UINT32
));
1941 (UINT8
*) Ptr
+ sizeof (AUTH_CERT_DB_DATA
),
1943 NameSize
* sizeof (CHAR16
)
1947 (UINT8
*) Ptr
+ sizeof (AUTH_CERT_DB_DATA
) + NameSize
* sizeof (CHAR16
),
1955 VarAttr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
1956 Status
= AuthServiceInternalUpdateVariable (
1968 Process variable with EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set
1970 Caution: This function may receive untrusted input.
1971 This function may be invoked in SMM mode, and datasize and data are external input.
1972 This function will do basic validation, before parse the data.
1973 This function will parse the authentication carefully to avoid security issues, like
1974 buffer overflow, integer overflow.
1976 @param[in] VariableName Name of Variable to be found.
1977 @param[in] VendorGuid Variable vendor GUID.
1978 @param[in] Data Data pointer.
1979 @param[in] DataSize Size of Data found. If size is less than the
1980 data, this value contains the required size.
1981 @param[in] Attributes Attribute value of the variable.
1982 @param[in] AuthVarType Verify against PK, KEK database, private database or certificate in data payload.
1983 @param[in] OrgTimeStamp Pointer to original time stamp,
1984 original variable is not found if NULL.
1985 @param[out] VarPayloadPtr Pointer to variable payload address.
1986 @param[out] VarPayloadSize Pointer to variable payload size.
1988 @retval EFI_INVALID_PARAMETER Invalid parameter.
1989 @retval EFI_SECURITY_VIOLATION The variable does NOT pass the validation
1990 check carried out by the firmware.
1991 @retval EFI_OUT_OF_RESOURCES Failed to process variable due to lack
1993 @retval EFI_SUCCESS Variable pass validation successfully.
1997 VerifyTimeBasedPayload (
1998 IN CHAR16
*VariableName
,
1999 IN EFI_GUID
*VendorGuid
,
2002 IN UINT32 Attributes
,
2003 IN AUTHVAR_TYPE AuthVarType
,
2004 IN EFI_TIME
*OrgTimeStamp
,
2005 OUT UINT8
**VarPayloadPtr
,
2006 OUT UINTN
*VarPayloadSize
2009 EFI_VARIABLE_AUTHENTICATION_2
*CertData
;
2015 BOOLEAN VerifyStatus
;
2017 EFI_SIGNATURE_LIST
*CertList
;
2018 EFI_SIGNATURE_DATA
*Cert
;
2029 UINTN CertStackSize
;
2030 UINT8
*CertsInCertDb
;
2031 UINT32 CertsSizeinDb
;
2033 VerifyStatus
= FALSE
;
2039 CertsInCertDb
= NULL
;
2042 // When the attribute EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS is
2043 // set, then the Data buffer shall begin with an instance of a complete (and serialized)
2044 // EFI_VARIABLE_AUTHENTICATION_2 descriptor. The descriptor shall be followed by the new
2045 // variable value and DataSize shall reflect the combined size of the descriptor and the new
2046 // variable value. The authentication descriptor is not part of the variable data and is not
2047 // returned by subsequent calls to GetVariable().
2049 CertData
= (EFI_VARIABLE_AUTHENTICATION_2
*) Data
;
2052 // Verify that Pad1, Nanosecond, TimeZone, Daylight and Pad2 components of the
2053 // TimeStamp value are set to zero.
2055 if ((CertData
->TimeStamp
.Pad1
!= 0) ||
2056 (CertData
->TimeStamp
.Nanosecond
!= 0) ||
2057 (CertData
->TimeStamp
.TimeZone
!= 0) ||
2058 (CertData
->TimeStamp
.Daylight
!= 0) ||
2059 (CertData
->TimeStamp
.Pad2
!= 0)) {
2060 return EFI_SECURITY_VIOLATION
;
2063 if ((OrgTimeStamp
!= NULL
) && ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0)) {
2064 if (AuthServiceInternalCompareTimeStamp (&CertData
->TimeStamp
, OrgTimeStamp
)) {
2066 // TimeStamp check fail, suspicious replay attack, return EFI_SECURITY_VIOLATION.
2068 return EFI_SECURITY_VIOLATION
;
2073 // wCertificateType should be WIN_CERT_TYPE_EFI_GUID.
2074 // Cert type should be EFI_CERT_TYPE_PKCS7_GUID.
2076 if ((CertData
->AuthInfo
.Hdr
.wCertificateType
!= WIN_CERT_TYPE_EFI_GUID
) ||
2077 !CompareGuid (&CertData
->AuthInfo
.CertType
, &gEfiCertPkcs7Guid
)) {
2079 // Invalid AuthInfo type, return EFI_SECURITY_VIOLATION.
2081 return EFI_SECURITY_VIOLATION
;
2085 // Find out Pkcs7 SignedData which follows the EFI_VARIABLE_AUTHENTICATION_2 descriptor.
2086 // AuthInfo.Hdr.dwLength is the length of the entire certificate, including the length of the header.
2088 SigData
= CertData
->AuthInfo
.CertData
;
2089 SigDataSize
= CertData
->AuthInfo
.Hdr
.dwLength
- (UINT32
) (OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID
, CertData
));
2092 // Find out the new data payload which follows Pkcs7 SignedData directly.
2094 PayloadPtr
= SigData
+ SigDataSize
;
2095 PayloadSize
= DataSize
- OFFSET_OF_AUTHINFO2_CERT_DATA
- (UINTN
) SigDataSize
;
2098 // Construct a serialization buffer of the values of the VariableName, VendorGuid and Attributes
2099 // parameters of the SetVariable() call and the TimeStamp component of the
2100 // EFI_VARIABLE_AUTHENTICATION_2 descriptor followed by the variable's new value
2101 // i.e. (VariableName, VendorGuid, Attributes, TimeStamp, Data)
2103 NewDataSize
= PayloadSize
+ sizeof (EFI_TIME
) + sizeof (UINT32
) +
2104 sizeof (EFI_GUID
) + StrSize (VariableName
) - sizeof (CHAR16
);
2107 // Here is to reuse scratch data area(at the end of volatile variable store)
2108 // to reduce SMRAM consumption for SMM variable driver.
2109 // The scratch buffer is enough to hold the serialized data and safe to use,
2110 // because it is only used at here to do verification temporarily first
2111 // and then used in UpdateVariable() for a time based auth variable set.
2113 Status
= mAuthVarLibContextIn
->GetScratchBuffer (&NewDataSize
, (VOID
**) &NewData
);
2114 if (EFI_ERROR (Status
)) {
2115 return EFI_OUT_OF_RESOURCES
;
2119 Length
= StrLen (VariableName
) * sizeof (CHAR16
);
2120 CopyMem (Buffer
, VariableName
, Length
);
2123 Length
= sizeof (EFI_GUID
);
2124 CopyMem (Buffer
, VendorGuid
, Length
);
2127 Length
= sizeof (UINT32
);
2128 CopyMem (Buffer
, &Attr
, Length
);
2131 Length
= sizeof (EFI_TIME
);
2132 CopyMem (Buffer
, &CertData
->TimeStamp
, Length
);
2135 CopyMem (Buffer
, PayloadPtr
, PayloadSize
);
2137 if (AuthVarType
== AuthVarTypePk
) {
2139 // Verify that the signature has been made with the current Platform Key (no chaining for PK).
2140 // First, get signer's certificates from SignedData.
2142 VerifyStatus
= Pkcs7GetSigners (
2150 if (!VerifyStatus
) {
2155 // Second, get the current platform key from variable. Check whether it's identical with signer's certificates
2156 // in SignedData. If not, return error immediately.
2158 Status
= AuthServiceInternalFindVariable (
2159 EFI_PLATFORM_KEY_NAME
,
2160 &gEfiGlobalVariableGuid
,
2164 if (EFI_ERROR (Status
)) {
2165 VerifyStatus
= FALSE
;
2168 CertList
= (EFI_SIGNATURE_LIST
*) Data
;
2169 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
2170 if ((RootCertSize
!= (CertList
->SignatureSize
- (sizeof (EFI_SIGNATURE_DATA
) - 1))) ||
2171 (CompareMem (Cert
->SignatureData
, RootCert
, RootCertSize
) != 0)) {
2172 VerifyStatus
= FALSE
;
2177 // Verify Pkcs7 SignedData via Pkcs7Verify library.
2179 VerifyStatus
= Pkcs7Verify (
2188 } else if (AuthVarType
== AuthVarTypeKek
) {
2191 // Get KEK database from variable.
2193 Status
= AuthServiceInternalFindVariable (
2194 EFI_KEY_EXCHANGE_KEY_NAME
,
2195 &gEfiGlobalVariableGuid
,
2199 if (EFI_ERROR (Status
)) {
2204 // Ready to verify Pkcs7 SignedData. Go through KEK Signature Database to find out X.509 CertList.
2206 KekDataSize
= (UINT32
) DataSize
;
2207 CertList
= (EFI_SIGNATURE_LIST
*) Data
;
2208 while ((KekDataSize
> 0) && (KekDataSize
>= CertList
->SignatureListSize
)) {
2209 if (CompareGuid (&CertList
->SignatureType
, &gEfiCertX509Guid
)) {
2210 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
2211 CertCount
= (CertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - CertList
->SignatureHeaderSize
) / CertList
->SignatureSize
;
2212 for (Index
= 0; Index
< CertCount
; Index
++) {
2214 // Iterate each Signature Data Node within this CertList for a verify
2216 RootCert
= Cert
->SignatureData
;
2217 RootCertSize
= CertList
->SignatureSize
- (sizeof (EFI_SIGNATURE_DATA
) - 1);
2220 // Verify Pkcs7 SignedData via Pkcs7Verify library.
2222 VerifyStatus
= Pkcs7Verify (
2233 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) Cert
+ CertList
->SignatureSize
);
2236 KekDataSize
-= CertList
->SignatureListSize
;
2237 CertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) CertList
+ CertList
->SignatureListSize
);
2239 } else if (AuthVarType
== AuthVarTypePriv
) {
2242 // Process common authenticated variable except PK/KEK/DB/DBX/DBT.
2243 // Get signer's certificates from SignedData.
2245 VerifyStatus
= Pkcs7GetSigners (
2253 if (!VerifyStatus
) {
2258 // Get previously stored signer's certificates from certdb for existing
2259 // variable. Check whether they are identical with signer's certificates
2260 // in SignedData. If not, return error immediately.
2262 if (OrgTimeStamp
!= NULL
) {
2263 VerifyStatus
= FALSE
;
2265 Status
= GetCertsFromDb (VariableName
, VendorGuid
, &CertsInCertDb
, &CertsSizeinDb
);
2266 if (EFI_ERROR (Status
)) {
2270 if ((CertStackSize
!= CertsSizeinDb
) ||
2271 (CompareMem (SignerCerts
, CertsInCertDb
, CertsSizeinDb
) != 0)) {
2276 VerifyStatus
= Pkcs7Verify (
2284 if (!VerifyStatus
) {
2289 // Delete signer's certificates when delete the common authenticated variable.
2291 if ((PayloadSize
== 0) && (OrgTimeStamp
!= NULL
) && ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0)) {
2292 Status
= DeleteCertsFromDb (VariableName
, VendorGuid
);
2293 if (EFI_ERROR (Status
)) {
2294 VerifyStatus
= FALSE
;
2297 } else if ((OrgTimeStamp
== NULL
) && (PayloadSize
!= 0)) {
2299 // Insert signer's certificates when adding a new common authenticated variable.
2301 Status
= InsertCertsToDb (VariableName
, VendorGuid
, SignerCerts
, CertStackSize
);
2302 if (EFI_ERROR (Status
)) {
2303 VerifyStatus
= FALSE
;
2307 } else if (AuthVarType
== AuthVarTypePayload
) {
2308 CertList
= (EFI_SIGNATURE_LIST
*) PayloadPtr
;
2309 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
2310 RootCert
= Cert
->SignatureData
;
2311 RootCertSize
= CertList
->SignatureSize
- (sizeof (EFI_SIGNATURE_DATA
) - 1);
2313 // Verify Pkcs7 SignedData via Pkcs7Verify library.
2315 VerifyStatus
= Pkcs7Verify (
2324 return EFI_SECURITY_VIOLATION
;
2329 if (AuthVarType
== AuthVarTypePk
|| AuthVarType
== AuthVarTypePriv
) {
2330 Pkcs7FreeSigners (RootCert
);
2331 Pkcs7FreeSigners (SignerCerts
);
2334 if (!VerifyStatus
) {
2335 return EFI_SECURITY_VIOLATION
;
2338 Status
= CheckSignatureListFormat(VariableName
, VendorGuid
, PayloadPtr
, PayloadSize
);
2339 if (EFI_ERROR (Status
)) {
2343 *VarPayloadPtr
= PayloadPtr
;
2344 *VarPayloadSize
= PayloadSize
;
2350 Process variable with EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set
2352 Caution: This function may receive untrusted input.
2353 This function may be invoked in SMM mode, and datasize and data are external input.
2354 This function will do basic validation, before parse the data.
2355 This function will parse the authentication carefully to avoid security issues, like
2356 buffer overflow, integer overflow.
2358 @param[in] VariableName Name of Variable to be found.
2359 @param[in] VendorGuid Variable vendor GUID.
2360 @param[in] Data Data pointer.
2361 @param[in] DataSize Size of Data found. If size is less than the
2362 data, this value contains the required size.
2363 @param[in] Attributes Attribute value of the variable.
2364 @param[in] AuthVarType Verify against PK, KEK database, private database or certificate in data payload.
2365 @param[out] VarDel Delete the variable or not.
2367 @retval EFI_INVALID_PARAMETER Invalid parameter.
2368 @retval EFI_SECURITY_VIOLATION The variable does NOT pass the validation
2369 check carried out by the firmware.
2370 @retval EFI_OUT_OF_RESOURCES Failed to process variable due to lack
2372 @retval EFI_SUCCESS Variable pass validation successfully.
2376 VerifyTimeBasedPayloadAndUpdate (
2377 IN CHAR16
*VariableName
,
2378 IN EFI_GUID
*VendorGuid
,
2381 IN UINT32 Attributes
,
2382 IN AUTHVAR_TYPE AuthVarType
,
2387 EFI_STATUS FindStatus
;
2390 EFI_VARIABLE_AUTHENTICATION_2
*CertData
;
2391 AUTH_VARIABLE_INFO OrgVariableInfo
;
2393 ZeroMem (&OrgVariableInfo
, sizeof (OrgVariableInfo
));
2394 FindStatus
= mAuthVarLibContextIn
->FindVariable (
2400 Status
= VerifyTimeBasedPayload (
2407 (!EFI_ERROR (FindStatus
)) ? OrgVariableInfo
.TimeStamp
: NULL
,
2411 if (EFI_ERROR (Status
)) {
2415 if ((PayloadSize
== 0) && (VarDel
!= NULL
)) {
2419 CertData
= (EFI_VARIABLE_AUTHENTICATION_2
*) Data
;
2422 // Final step: Update/Append Variable if it pass Pkcs7Verify
2424 return AuthServiceInternalUpdateVariableWithTimeStamp (
2430 &CertData
->TimeStamp