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 Status
= AuthServiceInternalUpdateVariable (
1215 if (!EFI_ERROR (Status
) && ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0)) {
1216 Status
= DeleteCertsFromDb (VariableName
, VendorGuid
);
1222 if (NeedPhysicallyPresent (VariableName
, VendorGuid
) && !UserPhysicalPresent()) {
1224 // This variable is protected, only physical present user could modify its value.
1226 return EFI_SECURITY_VIOLATION
;
1230 // A time-based authenticated variable and a count-based authenticated variable
1231 // can't be updated by each other.
1233 if (OrgVariableInfo
.Data
!= NULL
) {
1234 if (((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) &&
1235 ((OrgVariableInfo
.Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0)) {
1236 return EFI_SECURITY_VIOLATION
;
1239 if (((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) &&
1240 ((OrgVariableInfo
.Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0)) {
1241 return EFI_SECURITY_VIOLATION
;
1246 // Process Time-based Authenticated variable.
1248 if ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) {
1249 return VerifyTimeBasedPayloadAndUpdate (
1261 // Determine if first time SetVariable with the EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS.
1263 if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) {
1265 // Determine current operation type.
1267 if (DataSize
== AUTHINFO_SIZE
) {
1271 // Determine whether this is the first time with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS set.
1273 if (OrgVariableInfo
.Data
== NULL
) {
1275 } else if ((OrgVariableInfo
.Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) == 0) {
1278 KeyIndex
= OrgVariableInfo
.PubKeyIndex
;
1279 IsFirstTime
= FALSE
;
1281 } else if ((OrgVariableInfo
.Data
!= NULL
) &&
1282 ((OrgVariableInfo
.Attributes
& (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
)) != 0)
1285 // If the variable is already write-protected, it always needs authentication before update.
1287 return EFI_WRITE_PROTECTED
;
1290 // If without EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS, set and attributes collision.
1291 // That means it is not authenticated variable, just update variable as usual.
1293 Status
= AuthServiceInternalUpdateVariable (VariableName
, VendorGuid
, Data
, DataSize
, Attributes
);
1298 // Get PubKey and check Monotonic Count value corresponding to the variable.
1300 CertData
= (EFI_VARIABLE_AUTHENTICATION
*) Data
;
1301 CertBlock
= (EFI_CERT_BLOCK_RSA_2048_SHA256
*) (CertData
->AuthInfo
.CertData
);
1302 PubKey
= CertBlock
->PublicKey
;
1305 // Update Monotonic Count value.
1307 MonotonicCount
= CertData
->MonotonicCount
;
1311 // 2 cases need to check here
1312 // 1. Internal PubKey variable. PubKeyIndex is always 0
1313 // 2. Other counter-based AuthVariable. Check input PubKey.
1315 if (KeyIndex
== 0) {
1316 return EFI_SECURITY_VIOLATION
;
1318 for (Index
= 0; Index
< mPubKeyNumber
; Index
++) {
1319 if (ReadUnaligned32 (&(((AUTHVAR_KEY_DB_DATA
*) mPubKeyStore
+ Index
)->KeyIndex
)) == KeyIndex
) {
1320 if (CompareMem (((AUTHVAR_KEY_DB_DATA
*) mPubKeyStore
+ Index
)->KeyData
, PubKey
, EFI_CERT_TYPE_RSA2048_SIZE
) == 0) {
1323 return EFI_SECURITY_VIOLATION
;
1327 if (Index
== mPubKeyNumber
) {
1328 return EFI_SECURITY_VIOLATION
;
1332 // Compare the current monotonic count and ensure that it is greater than the last SetVariable
1333 // operation with the EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS attribute set.
1335 if (MonotonicCount
<= OrgVariableInfo
.MonotonicCount
) {
1337 // Monotonic count check fail, suspicious replay attack, return EFI_SECURITY_VIOLATION.
1339 return EFI_SECURITY_VIOLATION
;
1343 // Verify the certificate in Data payload.
1345 Status
= VerifyCounterBasedPayload (Data
, DataSize
, PubKey
);
1346 if (EFI_ERROR (Status
)) {
1351 // Now, the signature has been verified!
1353 if (IsFirstTime
&& !IsDeletion
) {
1354 VariableDataEntry
.VariableSize
= DataSize
- AUTHINFO_SIZE
;
1355 VariableDataEntry
.Guid
= VendorGuid
;
1356 VariableDataEntry
.Name
= VariableName
;
1359 // Update public key database variable if need.
1361 KeyIndex
= AddPubKeyInStore (PubKey
, &VariableDataEntry
);
1362 if (KeyIndex
== 0) {
1363 return EFI_OUT_OF_RESOURCES
;
1368 // Verification pass.
1370 return AuthServiceInternalUpdateVariableWithMonotonicCount (VariableName
, VendorGuid
, (UINT8
*)Data
+ AUTHINFO_SIZE
, DataSize
- AUTHINFO_SIZE
, Attributes
, KeyIndex
, MonotonicCount
);
1374 Filter out the duplicated EFI_SIGNATURE_DATA from the new data by comparing to the original data.
1376 @param[in] Data Pointer to original EFI_SIGNATURE_LIST.
1377 @param[in] DataSize Size of Data buffer.
1378 @param[in, out] NewData Pointer to new EFI_SIGNATURE_LIST.
1379 @param[in, out] NewDataSize Size of NewData buffer.
1383 FilterSignatureList (
1386 IN OUT VOID
*NewData
,
1387 IN OUT UINTN
*NewDataSize
1390 EFI_SIGNATURE_LIST
*CertList
;
1391 EFI_SIGNATURE_DATA
*Cert
;
1393 EFI_SIGNATURE_LIST
*NewCertList
;
1394 EFI_SIGNATURE_DATA
*NewCert
;
1401 UINTN SignatureListSize
;
1407 if (*NewDataSize
== 0) {
1411 TempDataSize
= *NewDataSize
;
1412 Status
= mAuthVarLibContextIn
->GetScratchBuffer (&TempDataSize
, (VOID
**) &TempData
);
1413 if (EFI_ERROR (Status
)) {
1414 return EFI_OUT_OF_RESOURCES
;
1419 NewCertList
= (EFI_SIGNATURE_LIST
*) NewData
;
1420 while ((*NewDataSize
> 0) && (*NewDataSize
>= NewCertList
->SignatureListSize
)) {
1421 NewCert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) NewCertList
+ sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
);
1422 NewCertCount
= (NewCertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - NewCertList
->SignatureHeaderSize
) / NewCertList
->SignatureSize
;
1425 for (Index
= 0; Index
< NewCertCount
; Index
++) {
1429 CertList
= (EFI_SIGNATURE_LIST
*) Data
;
1430 while ((Size
> 0) && (Size
>= CertList
->SignatureListSize
)) {
1431 if (CompareGuid (&CertList
->SignatureType
, &NewCertList
->SignatureType
) &&
1432 (CertList
->SignatureSize
== NewCertList
->SignatureSize
)) {
1433 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
1434 CertCount
= (CertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - CertList
->SignatureHeaderSize
) / CertList
->SignatureSize
;
1435 for (Index2
= 0; Index2
< CertCount
; Index2
++) {
1437 // Iterate each Signature Data in this Signature List.
1439 if (CompareMem (NewCert
, Cert
, CertList
->SignatureSize
) == 0) {
1443 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) Cert
+ CertList
->SignatureSize
);
1450 Size
-= CertList
->SignatureListSize
;
1451 CertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) CertList
+ CertList
->SignatureListSize
);
1456 // New EFI_SIGNATURE_DATA, keep it.
1458 if (CopiedCount
== 0) {
1460 // Copy EFI_SIGNATURE_LIST header for only once.
1462 CopyMem (Tail
, NewCertList
, sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
);
1463 Tail
= Tail
+ sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
;
1466 CopyMem (Tail
, NewCert
, NewCertList
->SignatureSize
);
1467 Tail
+= NewCertList
->SignatureSize
;
1471 NewCert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) NewCert
+ NewCertList
->SignatureSize
);
1475 // Update SignatureListSize in the kept EFI_SIGNATURE_LIST.
1477 if (CopiedCount
!= 0) {
1478 SignatureListSize
= sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
+ (CopiedCount
* NewCertList
->SignatureSize
);
1479 CertList
= (EFI_SIGNATURE_LIST
*) (Tail
- SignatureListSize
);
1480 CertList
->SignatureListSize
= (UINT32
) SignatureListSize
;
1483 *NewDataSize
-= NewCertList
->SignatureListSize
;
1484 NewCertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) NewCertList
+ NewCertList
->SignatureListSize
);
1487 TempDataSize
= (Tail
- (UINT8
*) TempData
);
1489 CopyMem (NewData
, TempData
, TempDataSize
);
1490 *NewDataSize
= TempDataSize
;
1496 Compare two EFI_TIME data.
1499 @param FirstTime A pointer to the first EFI_TIME data.
1500 @param SecondTime A pointer to the second EFI_TIME data.
1502 @retval TRUE The FirstTime is not later than the SecondTime.
1503 @retval FALSE The FirstTime is later than the SecondTime.
1507 AuthServiceInternalCompareTimeStamp (
1508 IN EFI_TIME
*FirstTime
,
1509 IN EFI_TIME
*SecondTime
1512 if (FirstTime
->Year
!= SecondTime
->Year
) {
1513 return (BOOLEAN
) (FirstTime
->Year
< SecondTime
->Year
);
1514 } else if (FirstTime
->Month
!= SecondTime
->Month
) {
1515 return (BOOLEAN
) (FirstTime
->Month
< SecondTime
->Month
);
1516 } else if (FirstTime
->Day
!= SecondTime
->Day
) {
1517 return (BOOLEAN
) (FirstTime
->Day
< SecondTime
->Day
);
1518 } else if (FirstTime
->Hour
!= SecondTime
->Hour
) {
1519 return (BOOLEAN
) (FirstTime
->Hour
< SecondTime
->Hour
);
1520 } else if (FirstTime
->Minute
!= SecondTime
->Minute
) {
1521 return (BOOLEAN
) (FirstTime
->Minute
< SecondTime
->Minute
);
1524 return (BOOLEAN
) (FirstTime
->Second
<= SecondTime
->Second
);
1528 Find matching signer's certificates for common authenticated variable
1529 by corresponding VariableName and VendorGuid from "certdb".
1531 The data format of "certdb":
1533 // UINT32 CertDbListSize;
1534 // /// AUTH_CERT_DB_DATA Certs1[];
1535 // /// AUTH_CERT_DB_DATA Certs2[];
1537 // /// AUTH_CERT_DB_DATA Certsn[];
1540 @param[in] VariableName Name of authenticated Variable.
1541 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1542 @param[in] Data Pointer to variable "certdb".
1543 @param[in] DataSize Size of variable "certdb".
1544 @param[out] CertOffset Offset of matching CertData, from starting of Data.
1545 @param[out] CertDataSize Length of CertData in bytes.
1546 @param[out] CertNodeOffset Offset of matching AUTH_CERT_DB_DATA , from
1548 @param[out] CertNodeSize Length of AUTH_CERT_DB_DATA in bytes.
1550 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1551 @retval EFI_NOT_FOUND Fail to find matching certs.
1552 @retval EFI_SUCCESS Find matching certs and output parameters.
1557 IN CHAR16
*VariableName
,
1558 IN EFI_GUID
*VendorGuid
,
1561 OUT UINT32
*CertOffset
, OPTIONAL
1562 OUT UINT32
*CertDataSize
, OPTIONAL
1563 OUT UINT32
*CertNodeOffset
,OPTIONAL
1564 OUT UINT32
*CertNodeSize OPTIONAL
1568 AUTH_CERT_DB_DATA
*Ptr
;
1572 UINT32 CertDbListSize
;
1574 if ((VariableName
== NULL
) || (VendorGuid
== NULL
) || (Data
== NULL
)) {
1575 return EFI_INVALID_PARAMETER
;
1579 // Check whether DataSize matches recorded CertDbListSize.
1581 if (DataSize
< sizeof (UINT32
)) {
1582 return EFI_INVALID_PARAMETER
;
1585 CertDbListSize
= ReadUnaligned32 ((UINT32
*) Data
);
1587 if (CertDbListSize
!= (UINT32
) DataSize
) {
1588 return EFI_INVALID_PARAMETER
;
1591 Offset
= sizeof (UINT32
);
1594 // Get corresponding certificates by VendorGuid and VariableName.
1596 while (Offset
< (UINT32
) DataSize
) {
1597 Ptr
= (AUTH_CERT_DB_DATA
*) (Data
+ Offset
);
1599 // Check whether VendorGuid matches.
1601 if (CompareGuid (&Ptr
->VendorGuid
, VendorGuid
)) {
1602 NodeSize
= ReadUnaligned32 (&Ptr
->CertNodeSize
);
1603 NameSize
= ReadUnaligned32 (&Ptr
->NameSize
);
1604 CertSize
= ReadUnaligned32 (&Ptr
->CertDataSize
);
1606 if (NodeSize
!= sizeof (EFI_GUID
) + sizeof (UINT32
) * 3 + CertSize
+
1607 sizeof (CHAR16
) * NameSize
) {
1608 return EFI_INVALID_PARAMETER
;
1611 Offset
= Offset
+ sizeof (EFI_GUID
) + sizeof (UINT32
) * 3;
1613 // Check whether VariableName matches.
1615 if ((NameSize
== StrLen (VariableName
)) &&
1616 (CompareMem (Data
+ Offset
, VariableName
, NameSize
* sizeof (CHAR16
)) == 0)) {
1617 Offset
= Offset
+ NameSize
* sizeof (CHAR16
);
1619 if (CertOffset
!= NULL
) {
1620 *CertOffset
= Offset
;
1623 if (CertDataSize
!= NULL
) {
1624 *CertDataSize
= CertSize
;
1627 if (CertNodeOffset
!= NULL
) {
1628 *CertNodeOffset
= (UINT32
) ((UINT8
*) Ptr
- Data
);
1631 if (CertNodeSize
!= NULL
) {
1632 *CertNodeSize
= NodeSize
;
1637 Offset
= Offset
+ NameSize
* sizeof (CHAR16
) + CertSize
;
1640 NodeSize
= ReadUnaligned32 (&Ptr
->CertNodeSize
);
1641 Offset
= Offset
+ NodeSize
;
1645 return EFI_NOT_FOUND
;
1649 Retrieve signer's certificates for common authenticated variable
1650 by corresponding VariableName and VendorGuid from "certdb".
1652 @param[in] VariableName Name of authenticated Variable.
1653 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1654 @param[out] CertData Pointer to signer's certificates.
1655 @param[out] CertDataSize Length of CertData in bytes.
1657 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1658 @retval EFI_NOT_FOUND Fail to find "certdb" or matching certs.
1659 @retval EFI_SUCCESS Get signer's certificates successfully.
1664 IN CHAR16
*VariableName
,
1665 IN EFI_GUID
*VendorGuid
,
1666 OUT UINT8
**CertData
,
1667 OUT UINT32
*CertDataSize
1675 if ((VariableName
== NULL
) || (VendorGuid
== NULL
) || (CertData
== NULL
) || (CertDataSize
== NULL
)) {
1676 return EFI_INVALID_PARAMETER
;
1680 // Get variable "certdb".
1682 Status
= AuthServiceInternalFindVariable (
1688 if (EFI_ERROR (Status
)) {
1692 if ((DataSize
== 0) || (Data
== NULL
)) {
1694 return EFI_NOT_FOUND
;
1697 Status
= FindCertsFromDb (
1708 if (EFI_ERROR (Status
)) {
1712 *CertData
= Data
+ CertOffset
;
1717 Delete matching signer's certificates when deleting common authenticated
1718 variable by corresponding VariableName and VendorGuid from "certdb".
1720 @param[in] VariableName Name of authenticated Variable.
1721 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1723 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1724 @retval EFI_NOT_FOUND Fail to find "certdb" or matching certs.
1725 @retval EFI_OUT_OF_RESOURCES The operation is failed due to lack of resources.
1726 @retval EFI_SUCCESS The operation is completed successfully.
1731 IN CHAR16
*VariableName
,
1732 IN EFI_GUID
*VendorGuid
1739 UINT32 CertNodeOffset
;
1740 UINT32 CertNodeSize
;
1742 UINT32 NewCertDbSize
;
1744 if ((VariableName
== NULL
) || (VendorGuid
== NULL
)) {
1745 return EFI_INVALID_PARAMETER
;
1749 // Get variable "certdb".
1751 Status
= AuthServiceInternalFindVariable (
1757 if (EFI_ERROR (Status
)) {
1761 if ((DataSize
== 0) || (Data
== NULL
)) {
1763 return EFI_NOT_FOUND
;
1766 if (DataSize
== sizeof (UINT32
)) {
1768 // There is no certs in certdb.
1774 // Get corresponding cert node from certdb.
1776 Status
= FindCertsFromDb (
1787 if (EFI_ERROR (Status
)) {
1791 if (DataSize
< (CertNodeOffset
+ CertNodeSize
)) {
1792 return EFI_NOT_FOUND
;
1796 // Construct new data content of variable "certdb".
1798 NewCertDbSize
= (UINT32
) DataSize
- CertNodeSize
;
1799 NewCertDb
= (UINT8
*) mCertDbStore
;
1802 // Copy the DB entries before deleting node.
1804 CopyMem (NewCertDb
, Data
, CertNodeOffset
);
1806 // Update CertDbListSize.
1808 CopyMem (NewCertDb
, &NewCertDbSize
, sizeof (UINT32
));
1810 // Copy the DB entries after deleting node.
1812 if (DataSize
> (CertNodeOffset
+ CertNodeSize
)) {
1814 NewCertDb
+ CertNodeOffset
,
1815 Data
+ CertNodeOffset
+ CertNodeSize
,
1816 DataSize
- CertNodeOffset
- CertNodeSize
1823 VarAttr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
1824 Status
= AuthServiceInternalUpdateVariable (
1836 Insert signer's certificates for common authenticated variable with VariableName
1837 and VendorGuid in AUTH_CERT_DB_DATA to "certdb".
1839 @param[in] VariableName Name of authenticated Variable.
1840 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1841 @param[in] CertData Pointer to signer's certificates.
1842 @param[in] CertDataSize Length of CertData in bytes.
1844 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1845 @retval EFI_ACCESS_DENIED An AUTH_CERT_DB_DATA entry with same VariableName
1846 and VendorGuid already exists.
1847 @retval EFI_OUT_OF_RESOURCES The operation is failed due to lack of resources.
1848 @retval EFI_SUCCESS Insert an AUTH_CERT_DB_DATA entry to "certdb"
1853 IN CHAR16
*VariableName
,
1854 IN EFI_GUID
*VendorGuid
,
1856 IN UINTN CertDataSize
1864 UINT32 NewCertDbSize
;
1865 UINT32 CertNodeSize
;
1867 AUTH_CERT_DB_DATA
*Ptr
;
1869 if ((VariableName
== NULL
) || (VendorGuid
== NULL
) || (CertData
== NULL
)) {
1870 return EFI_INVALID_PARAMETER
;
1874 // Get variable "certdb".
1876 Status
= AuthServiceInternalFindVariable (
1882 if (EFI_ERROR (Status
)) {
1886 if ((DataSize
== 0) || (Data
== NULL
)) {
1888 return EFI_NOT_FOUND
;
1892 // Find whether matching cert node already exists in "certdb".
1893 // If yes return error.
1895 Status
= FindCertsFromDb (
1906 if (!EFI_ERROR (Status
)) {
1908 return EFI_ACCESS_DENIED
;
1912 // Construct new data content of variable "certdb".
1914 NameSize
= (UINT32
) StrLen (VariableName
);
1915 CertNodeSize
= sizeof (AUTH_CERT_DB_DATA
) + (UINT32
) CertDataSize
+ NameSize
* sizeof (CHAR16
);
1916 NewCertDbSize
= (UINT32
) DataSize
+ CertNodeSize
;
1917 if (NewCertDbSize
> mMaxCertDbSize
) {
1918 return EFI_OUT_OF_RESOURCES
;
1920 NewCertDb
= (UINT8
*) mCertDbStore
;
1923 // Copy the DB entries before inserting node.
1925 CopyMem (NewCertDb
, Data
, DataSize
);
1927 // Update CertDbListSize.
1929 CopyMem (NewCertDb
, &NewCertDbSize
, sizeof (UINT32
));
1931 // Construct new cert node.
1933 Ptr
= (AUTH_CERT_DB_DATA
*) (NewCertDb
+ DataSize
);
1934 CopyGuid (&Ptr
->VendorGuid
, VendorGuid
);
1935 CopyMem (&Ptr
->CertNodeSize
, &CertNodeSize
, sizeof (UINT32
));
1936 CopyMem (&Ptr
->NameSize
, &NameSize
, sizeof (UINT32
));
1937 CopyMem (&Ptr
->CertDataSize
, &CertDataSize
, sizeof (UINT32
));
1940 (UINT8
*) Ptr
+ sizeof (AUTH_CERT_DB_DATA
),
1942 NameSize
* sizeof (CHAR16
)
1946 (UINT8
*) Ptr
+ sizeof (AUTH_CERT_DB_DATA
) + NameSize
* sizeof (CHAR16
),
1954 VarAttr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
1955 Status
= AuthServiceInternalUpdateVariable (
1967 Clean up signer's certificates for common authenticated variable
1968 by corresponding VariableName and VendorGuid from "certdb".
1969 Sytem may break down during Timebased Variable update & certdb update,
1970 make them inconsistent, this function is called in AuthVariable Init to ensure
1973 @retval EFI_NOT_FOUND Fail to find matching certs.
1974 @retval EFI_SUCCESS Find matching certs and output parameters.
1983 AUTH_CERT_DB_DATA
*Ptr
;
1986 CHAR16
*VariableName
;
1988 BOOLEAN CertCleaned
;
1992 UINTN AuthVarDataSize
;
1993 EFI_GUID AuthVarGuid
;
1995 Status
= EFI_SUCCESS
;
1998 // Get corresponding certificates by VendorGuid and VariableName.
2001 CertCleaned
= FALSE
;
2004 // Get latest variable "certdb"
2006 Status
= AuthServiceInternalFindVariable (
2012 if (EFI_ERROR (Status
)) {
2016 if ((DataSize
== 0) || (Data
== NULL
)) {
2018 return EFI_NOT_FOUND
;
2021 Offset
= sizeof (UINT32
);
2023 while (Offset
< (UINT32
) DataSize
) {
2024 Ptr
= (AUTH_CERT_DB_DATA
*) (Data
+ Offset
);
2026 // Check whether VendorGuid matches.
2028 NodeSize
= ReadUnaligned32 (&Ptr
->CertNodeSize
);
2029 NameSize
= ReadUnaligned32 (&Ptr
->NameSize
);
2032 // Get VarName tailed with '\0'
2034 VariableName
= AllocateZeroPool((NameSize
+ 1) * sizeof(CHAR16
));
2035 if (VariableName
== NULL
) {
2036 return EFI_OUT_OF_RESOURCES
;
2038 CopyMem (VariableName
, (UINT8
*) Ptr
+ sizeof (AUTH_CERT_DB_DATA
), NameSize
* sizeof(CHAR16
));
2040 // Keep VarGuid aligned
2042 CopyMem (&AuthVarGuid
, &Ptr
->VendorGuid
, sizeof(EFI_GUID
));
2045 // Find corresponding time auth variable
2047 Status
= AuthServiceInternalFindVariable (
2050 (VOID
**) &AuthVarData
,
2054 if (EFI_ERROR(Status
)) {
2055 Status
= DeleteCertsFromDb(VariableName
, &AuthVarGuid
);
2057 DEBUG((EFI_D_INFO
, "Recovery!! Cert for Auth Variable %s Guid %g is removed for consistency\n", VariableName
, &AuthVarGuid
));
2058 FreePool(VariableName
);
2062 FreePool(VariableName
);
2063 Offset
= Offset
+ NodeSize
;
2065 } while (CertCleaned
);
2071 Process variable with EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set
2073 Caution: This function may receive untrusted input.
2074 This function may be invoked in SMM mode, and datasize and data are external input.
2075 This function will do basic validation, before parse the data.
2076 This function will parse the authentication carefully to avoid security issues, like
2077 buffer overflow, integer overflow.
2079 @param[in] VariableName Name of Variable to be found.
2080 @param[in] VendorGuid Variable vendor GUID.
2081 @param[in] Data Data pointer.
2082 @param[in] DataSize Size of Data found. If size is less than the
2083 data, this value contains the required size.
2084 @param[in] Attributes Attribute value of the variable.
2085 @param[in] AuthVarType Verify against PK, KEK database, private database or certificate in data payload.
2086 @param[in] OrgTimeStamp Pointer to original time stamp,
2087 original variable is not found if NULL.
2088 @param[out] VarPayloadPtr Pointer to variable payload address.
2089 @param[out] VarPayloadSize Pointer to variable payload size.
2091 @retval EFI_INVALID_PARAMETER Invalid parameter.
2092 @retval EFI_SECURITY_VIOLATION The variable does NOT pass the validation
2093 check carried out by the firmware.
2094 @retval EFI_OUT_OF_RESOURCES Failed to process variable due to lack
2096 @retval EFI_SUCCESS Variable pass validation successfully.
2100 VerifyTimeBasedPayload (
2101 IN CHAR16
*VariableName
,
2102 IN EFI_GUID
*VendorGuid
,
2105 IN UINT32 Attributes
,
2106 IN AUTHVAR_TYPE AuthVarType
,
2107 IN EFI_TIME
*OrgTimeStamp
,
2108 OUT UINT8
**VarPayloadPtr
,
2109 OUT UINTN
*VarPayloadSize
2112 EFI_VARIABLE_AUTHENTICATION_2
*CertData
;
2118 BOOLEAN VerifyStatus
;
2120 EFI_SIGNATURE_LIST
*CertList
;
2121 EFI_SIGNATURE_DATA
*Cert
;
2132 UINTN CertStackSize
;
2133 UINT8
*CertsInCertDb
;
2134 UINT32 CertsSizeinDb
;
2136 VerifyStatus
= FALSE
;
2142 CertsInCertDb
= NULL
;
2145 // When the attribute EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS is
2146 // set, then the Data buffer shall begin with an instance of a complete (and serialized)
2147 // EFI_VARIABLE_AUTHENTICATION_2 descriptor. The descriptor shall be followed by the new
2148 // variable value and DataSize shall reflect the combined size of the descriptor and the new
2149 // variable value. The authentication descriptor is not part of the variable data and is not
2150 // returned by subsequent calls to GetVariable().
2152 CertData
= (EFI_VARIABLE_AUTHENTICATION_2
*) Data
;
2155 // Verify that Pad1, Nanosecond, TimeZone, Daylight and Pad2 components of the
2156 // TimeStamp value are set to zero.
2158 if ((CertData
->TimeStamp
.Pad1
!= 0) ||
2159 (CertData
->TimeStamp
.Nanosecond
!= 0) ||
2160 (CertData
->TimeStamp
.TimeZone
!= 0) ||
2161 (CertData
->TimeStamp
.Daylight
!= 0) ||
2162 (CertData
->TimeStamp
.Pad2
!= 0)) {
2163 return EFI_SECURITY_VIOLATION
;
2166 if ((OrgTimeStamp
!= NULL
) && ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0)) {
2167 if (AuthServiceInternalCompareTimeStamp (&CertData
->TimeStamp
, OrgTimeStamp
)) {
2169 // TimeStamp check fail, suspicious replay attack, return EFI_SECURITY_VIOLATION.
2171 return EFI_SECURITY_VIOLATION
;
2176 // wCertificateType should be WIN_CERT_TYPE_EFI_GUID.
2177 // Cert type should be EFI_CERT_TYPE_PKCS7_GUID.
2179 if ((CertData
->AuthInfo
.Hdr
.wCertificateType
!= WIN_CERT_TYPE_EFI_GUID
) ||
2180 !CompareGuid (&CertData
->AuthInfo
.CertType
, &gEfiCertPkcs7Guid
)) {
2182 // Invalid AuthInfo type, return EFI_SECURITY_VIOLATION.
2184 return EFI_SECURITY_VIOLATION
;
2188 // Find out Pkcs7 SignedData which follows the EFI_VARIABLE_AUTHENTICATION_2 descriptor.
2189 // AuthInfo.Hdr.dwLength is the length of the entire certificate, including the length of the header.
2191 SigData
= CertData
->AuthInfo
.CertData
;
2192 SigDataSize
= CertData
->AuthInfo
.Hdr
.dwLength
- (UINT32
) (OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID
, CertData
));
2195 // Find out the new data payload which follows Pkcs7 SignedData directly.
2197 PayloadPtr
= SigData
+ SigDataSize
;
2198 PayloadSize
= DataSize
- OFFSET_OF_AUTHINFO2_CERT_DATA
- (UINTN
) SigDataSize
;
2201 // Construct a serialization buffer of the values of the VariableName, VendorGuid and Attributes
2202 // parameters of the SetVariable() call and the TimeStamp component of the
2203 // EFI_VARIABLE_AUTHENTICATION_2 descriptor followed by the variable's new value
2204 // i.e. (VariableName, VendorGuid, Attributes, TimeStamp, Data)
2206 NewDataSize
= PayloadSize
+ sizeof (EFI_TIME
) + sizeof (UINT32
) +
2207 sizeof (EFI_GUID
) + StrSize (VariableName
) - sizeof (CHAR16
);
2210 // Here is to reuse scratch data area(at the end of volatile variable store)
2211 // to reduce SMRAM consumption for SMM variable driver.
2212 // The scratch buffer is enough to hold the serialized data and safe to use,
2213 // because it is only used at here to do verification temporarily first
2214 // and then used in UpdateVariable() for a time based auth variable set.
2216 Status
= mAuthVarLibContextIn
->GetScratchBuffer (&NewDataSize
, (VOID
**) &NewData
);
2217 if (EFI_ERROR (Status
)) {
2218 return EFI_OUT_OF_RESOURCES
;
2222 Length
= StrLen (VariableName
) * sizeof (CHAR16
);
2223 CopyMem (Buffer
, VariableName
, Length
);
2226 Length
= sizeof (EFI_GUID
);
2227 CopyMem (Buffer
, VendorGuid
, Length
);
2230 Length
= sizeof (UINT32
);
2231 CopyMem (Buffer
, &Attr
, Length
);
2234 Length
= sizeof (EFI_TIME
);
2235 CopyMem (Buffer
, &CertData
->TimeStamp
, Length
);
2238 CopyMem (Buffer
, PayloadPtr
, PayloadSize
);
2240 if (AuthVarType
== AuthVarTypePk
) {
2242 // Verify that the signature has been made with the current Platform Key (no chaining for PK).
2243 // First, get signer's certificates from SignedData.
2245 VerifyStatus
= Pkcs7GetSigners (
2253 if (!VerifyStatus
) {
2258 // Second, get the current platform key from variable. Check whether it's identical with signer's certificates
2259 // in SignedData. If not, return error immediately.
2261 Status
= AuthServiceInternalFindVariable (
2262 EFI_PLATFORM_KEY_NAME
,
2263 &gEfiGlobalVariableGuid
,
2267 if (EFI_ERROR (Status
)) {
2268 VerifyStatus
= FALSE
;
2271 CertList
= (EFI_SIGNATURE_LIST
*) Data
;
2272 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
2273 if ((RootCertSize
!= (CertList
->SignatureSize
- (sizeof (EFI_SIGNATURE_DATA
) - 1))) ||
2274 (CompareMem (Cert
->SignatureData
, RootCert
, RootCertSize
) != 0)) {
2275 VerifyStatus
= FALSE
;
2280 // Verify Pkcs7 SignedData via Pkcs7Verify library.
2282 VerifyStatus
= Pkcs7Verify (
2291 } else if (AuthVarType
== AuthVarTypeKek
) {
2294 // Get KEK database from variable.
2296 Status
= AuthServiceInternalFindVariable (
2297 EFI_KEY_EXCHANGE_KEY_NAME
,
2298 &gEfiGlobalVariableGuid
,
2302 if (EFI_ERROR (Status
)) {
2307 // Ready to verify Pkcs7 SignedData. Go through KEK Signature Database to find out X.509 CertList.
2309 KekDataSize
= (UINT32
) DataSize
;
2310 CertList
= (EFI_SIGNATURE_LIST
*) Data
;
2311 while ((KekDataSize
> 0) && (KekDataSize
>= CertList
->SignatureListSize
)) {
2312 if (CompareGuid (&CertList
->SignatureType
, &gEfiCertX509Guid
)) {
2313 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
2314 CertCount
= (CertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - CertList
->SignatureHeaderSize
) / CertList
->SignatureSize
;
2315 for (Index
= 0; Index
< CertCount
; Index
++) {
2317 // Iterate each Signature Data Node within this CertList for a verify
2319 RootCert
= Cert
->SignatureData
;
2320 RootCertSize
= CertList
->SignatureSize
- (sizeof (EFI_SIGNATURE_DATA
) - 1);
2323 // Verify Pkcs7 SignedData via Pkcs7Verify library.
2325 VerifyStatus
= Pkcs7Verify (
2336 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) Cert
+ CertList
->SignatureSize
);
2339 KekDataSize
-= CertList
->SignatureListSize
;
2340 CertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) CertList
+ CertList
->SignatureListSize
);
2342 } else if (AuthVarType
== AuthVarTypePriv
) {
2345 // Process common authenticated variable except PK/KEK/DB/DBX/DBT.
2346 // Get signer's certificates from SignedData.
2348 VerifyStatus
= Pkcs7GetSigners (
2356 if (!VerifyStatus
) {
2361 // Get previously stored signer's certificates from certdb for existing
2362 // variable. Check whether they are identical with signer's certificates
2363 // in SignedData. If not, return error immediately.
2365 if (OrgTimeStamp
!= NULL
) {
2366 VerifyStatus
= FALSE
;
2368 Status
= GetCertsFromDb (VariableName
, VendorGuid
, &CertsInCertDb
, &CertsSizeinDb
);
2369 if (EFI_ERROR (Status
)) {
2373 if ((CertStackSize
!= CertsSizeinDb
) ||
2374 (CompareMem (SignerCerts
, CertsInCertDb
, CertsSizeinDb
) != 0)) {
2379 VerifyStatus
= Pkcs7Verify (
2387 if (!VerifyStatus
) {
2391 if ((OrgTimeStamp
== NULL
) && (PayloadSize
!= 0)) {
2393 // Insert signer's certificates when adding a new common authenticated variable.
2395 Status
= InsertCertsToDb (VariableName
, VendorGuid
, SignerCerts
, CertStackSize
);
2396 if (EFI_ERROR (Status
)) {
2397 VerifyStatus
= FALSE
;
2401 } else if (AuthVarType
== AuthVarTypePayload
) {
2402 CertList
= (EFI_SIGNATURE_LIST
*) PayloadPtr
;
2403 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
2404 RootCert
= Cert
->SignatureData
;
2405 RootCertSize
= CertList
->SignatureSize
- (sizeof (EFI_SIGNATURE_DATA
) - 1);
2407 // Verify Pkcs7 SignedData via Pkcs7Verify library.
2409 VerifyStatus
= Pkcs7Verify (
2418 return EFI_SECURITY_VIOLATION
;
2423 if (AuthVarType
== AuthVarTypePk
|| AuthVarType
== AuthVarTypePriv
) {
2424 Pkcs7FreeSigners (RootCert
);
2425 Pkcs7FreeSigners (SignerCerts
);
2428 if (!VerifyStatus
) {
2429 return EFI_SECURITY_VIOLATION
;
2432 Status
= CheckSignatureListFormat(VariableName
, VendorGuid
, PayloadPtr
, PayloadSize
);
2433 if (EFI_ERROR (Status
)) {
2437 *VarPayloadPtr
= PayloadPtr
;
2438 *VarPayloadSize
= PayloadSize
;
2444 Process variable with EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set
2446 Caution: This function may receive untrusted input.
2447 This function may be invoked in SMM mode, and datasize and data are external input.
2448 This function will do basic validation, before parse the data.
2449 This function will parse the authentication carefully to avoid security issues, like
2450 buffer overflow, integer overflow.
2452 @param[in] VariableName Name of Variable to be found.
2453 @param[in] VendorGuid Variable vendor GUID.
2454 @param[in] Data Data pointer.
2455 @param[in] DataSize Size of Data found. If size is less than the
2456 data, this value contains the required size.
2457 @param[in] Attributes Attribute value of the variable.
2458 @param[in] AuthVarType Verify against PK, KEK database, private database or certificate in data payload.
2459 @param[out] VarDel Delete the variable or not.
2461 @retval EFI_INVALID_PARAMETER Invalid parameter.
2462 @retval EFI_SECURITY_VIOLATION The variable does NOT pass the validation
2463 check carried out by the firmware.
2464 @retval EFI_OUT_OF_RESOURCES Failed to process variable due to lack
2466 @retval EFI_SUCCESS Variable pass validation successfully.
2470 VerifyTimeBasedPayloadAndUpdate (
2471 IN CHAR16
*VariableName
,
2472 IN EFI_GUID
*VendorGuid
,
2475 IN UINT32 Attributes
,
2476 IN AUTHVAR_TYPE AuthVarType
,
2481 EFI_STATUS FindStatus
;
2484 EFI_VARIABLE_AUTHENTICATION_2
*CertData
;
2485 AUTH_VARIABLE_INFO OrgVariableInfo
;
2488 ZeroMem (&OrgVariableInfo
, sizeof (OrgVariableInfo
));
2489 FindStatus
= mAuthVarLibContextIn
->FindVariable (
2495 Status
= VerifyTimeBasedPayload (
2502 (!EFI_ERROR (FindStatus
)) ? OrgVariableInfo
.TimeStamp
: NULL
,
2506 if (EFI_ERROR (Status
)) {
2510 if (!EFI_ERROR(FindStatus
)
2511 && (PayloadSize
== 0)
2512 && ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0)) {
2518 CertData
= (EFI_VARIABLE_AUTHENTICATION_2
*) Data
;
2521 // Final step: Update/Append Variable if it pass Pkcs7Verify
2523 Status
= AuthServiceInternalUpdateVariableWithTimeStamp (
2529 &CertData
->TimeStamp
2533 // Delete signer's certificates when delete the common authenticated variable.
2535 if (IsDel
&& AuthVarType
== AuthVarTypePriv
&& !EFI_ERROR(Status
) ) {
2536 Status
= DeleteCertsFromDb (VariableName
, VendorGuid
);
2539 if (VarDel
!= NULL
) {
2540 if (IsDel
&& !EFI_ERROR(Status
)) {