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 - 2016, 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 // Init state of Del. State may change due to secure check
934 if ((InCustomMode() && UserPhysicalPresent()) || (mPlatformMode
== SETUP_MODE
&& !IsPk
)) {
935 Payload
= (UINT8
*) Data
+ AUTHINFO2_SIZE (Data
);
936 PayloadSize
= DataSize
- AUTHINFO2_SIZE (Data
);
937 if (PayloadSize
== 0) {
941 Status
= CheckSignatureListFormat(VariableName
, VendorGuid
, Payload
, PayloadSize
);
942 if (EFI_ERROR (Status
)) {
946 Status
= AuthServiceInternalUpdateVariableWithTimeStamp (
952 &((EFI_VARIABLE_AUTHENTICATION_2
*) Data
)->TimeStamp
954 if (EFI_ERROR(Status
)) {
958 if ((mPlatformMode
!= SETUP_MODE
) || IsPk
) {
959 Status
= VendorKeyIsModified ();
961 } else if (mPlatformMode
== USER_MODE
) {
963 // Verify against X509 Cert in PK database.
965 Status
= VerifyTimeBasedPayloadAndUpdate (
976 // Verify against the certificate in data payload.
978 Status
= VerifyTimeBasedPayloadAndUpdate (
989 if (!EFI_ERROR(Status
) && IsPk
) {
990 if (mPlatformMode
== SETUP_MODE
&& !Del
) {
992 // If enroll PK in setup mode, need change to user mode.
994 Status
= UpdatePlatformMode (USER_MODE
);
995 } else if (mPlatformMode
== USER_MODE
&& Del
){
997 // If delete PK in user mode, need change to setup mode.
999 Status
= UpdatePlatformMode (SETUP_MODE
);
1007 Process variable with key exchange key for verification.
1009 Caution: This function may receive untrusted input.
1010 This function may be invoked in SMM mode, and datasize and data are external input.
1011 This function will do basic validation, before parse the data.
1012 This function will parse the authentication carefully to avoid security issues, like
1013 buffer overflow, integer overflow.
1014 This function will check attribute carefully to avoid authentication bypass.
1016 @param[in] VariableName Name of Variable to be found.
1017 @param[in] VendorGuid Variable vendor GUID.
1018 @param[in] Data Data pointer.
1019 @param[in] DataSize Size of Data found. If size is less than the
1020 data, this value contains the required size.
1021 @param[in] Attributes Attribute value of the variable.
1023 @return EFI_INVALID_PARAMETER Invalid parameter.
1024 @return EFI_SECURITY_VIOLATION The variable does NOT pass the validation
1025 check carried out by the firmware.
1026 @return EFI_SUCCESS Variable pass validation successfully.
1031 IN CHAR16
*VariableName
,
1032 IN EFI_GUID
*VendorGuid
,
1035 IN UINT32 Attributes OPTIONAL
1042 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0 ||
1043 (Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) == 0) {
1045 // DB, DBX and DBT should set EFI_VARIABLE_NON_VOLATILE attribute and should be a time-based
1046 // authenticated variable.
1048 return EFI_INVALID_PARAMETER
;
1051 Status
= EFI_SUCCESS
;
1052 if (mPlatformMode
== USER_MODE
&& !(InCustomMode() && UserPhysicalPresent())) {
1054 // Time-based, verify against X509 Cert KEK.
1056 return VerifyTimeBasedPayloadAndUpdate (
1067 // If in setup mode or custom secure boot mode, no authentication needed.
1069 Payload
= (UINT8
*) Data
+ AUTHINFO2_SIZE (Data
);
1070 PayloadSize
= DataSize
- AUTHINFO2_SIZE (Data
);
1072 Status
= CheckSignatureListFormat(VariableName
, VendorGuid
, Payload
, PayloadSize
);
1073 if (EFI_ERROR (Status
)) {
1077 Status
= AuthServiceInternalUpdateVariableWithTimeStamp (
1083 &((EFI_VARIABLE_AUTHENTICATION_2
*) Data
)->TimeStamp
1085 if (EFI_ERROR (Status
)) {
1089 if (mPlatformMode
!= SETUP_MODE
) {
1090 Status
= VendorKeyIsModified ();
1098 Check if it is to delete auth variable.
1100 @param[in] OrgAttributes Original attribute value of the variable.
1101 @param[in] Data Data pointer.
1102 @param[in] DataSize Size of Data.
1103 @param[in] Attributes Attribute value of the variable.
1105 @retval TRUE It is to delete auth variable.
1106 @retval FALSE It is not to delete auth variable.
1110 IsDeleteAuthVariable (
1111 IN UINT32 OrgAttributes
,
1114 IN UINT32 Attributes
1123 // To delete a variable created with the EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
1124 // or the EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS attribute,
1125 // SetVariable must be used with attributes matching the existing variable
1126 // and the DataSize set to the size of the AuthInfo descriptor.
1128 if ((Attributes
== OrgAttributes
) &&
1129 ((Attributes
& (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
)) != 0)) {
1130 if ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) {
1131 PayloadSize
= DataSize
- AUTHINFO2_SIZE (Data
);
1132 if (PayloadSize
== 0) {
1136 PayloadSize
= DataSize
- AUTHINFO_SIZE
;
1137 if (PayloadSize
== 0) {
1147 Process variable with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS/EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set
1149 Caution: This function may receive untrusted input.
1150 This function may be invoked in SMM mode, and datasize and data are external input.
1151 This function will do basic validation, before parse the data.
1152 This function will parse the authentication carefully to avoid security issues, like
1153 buffer overflow, integer overflow.
1154 This function will check attribute carefully to avoid authentication bypass.
1156 @param[in] VariableName Name of the variable.
1157 @param[in] VendorGuid Variable vendor GUID.
1158 @param[in] Data Data pointer.
1159 @param[in] DataSize Size of Data.
1160 @param[in] Attributes Attribute value of the variable.
1162 @return EFI_INVALID_PARAMETER Invalid parameter.
1163 @return EFI_WRITE_PROTECTED Variable is write-protected and needs authentication with
1164 EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS set.
1165 @return EFI_OUT_OF_RESOURCES The Database to save the public key is full.
1166 @return EFI_SECURITY_VIOLATION The variable is with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
1167 set, but the AuthInfo does NOT pass the validation
1168 check carried out by the firmware.
1169 @return EFI_SUCCESS Variable is not write-protected or pass validation successfully.
1174 IN CHAR16
*VariableName
,
1175 IN EFI_GUID
*VendorGuid
,
1178 IN UINT32 Attributes
1183 BOOLEAN IsFirstTime
;
1185 EFI_VARIABLE_AUTHENTICATION
*CertData
;
1186 EFI_CERT_BLOCK_RSA_2048_SHA256
*CertBlock
;
1188 UINT64 MonotonicCount
;
1189 VARIABLE_ENTRY_CONSISTENCY VariableDataEntry
;
1191 AUTH_VARIABLE_INFO OrgVariableInfo
;
1198 Status
= EFI_SUCCESS
;
1200 ZeroMem (&OrgVariableInfo
, sizeof (OrgVariableInfo
));
1201 Status
= mAuthVarLibContextIn
->FindVariable (
1207 if ((!EFI_ERROR (Status
)) && IsDeleteAuthVariable (OrgVariableInfo
.Attributes
, Data
, DataSize
, Attributes
) && UserPhysicalPresent()) {
1209 // Allow the delete operation of common authenticated variable at user physical presence.
1211 Status
= AuthServiceInternalUpdateVariable (
1218 if (!EFI_ERROR (Status
) && ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0)) {
1219 Status
= DeleteCertsFromDb (VariableName
, VendorGuid
, Attributes
);
1225 if (NeedPhysicallyPresent (VariableName
, VendorGuid
) && !UserPhysicalPresent()) {
1227 // This variable is protected, only physical present user could modify its value.
1229 return EFI_SECURITY_VIOLATION
;
1233 // A time-based authenticated variable and a count-based authenticated variable
1234 // can't be updated by each other.
1236 if (OrgVariableInfo
.Data
!= NULL
) {
1237 if (((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) &&
1238 ((OrgVariableInfo
.Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0)) {
1239 return EFI_SECURITY_VIOLATION
;
1242 if (((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) &&
1243 ((OrgVariableInfo
.Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0)) {
1244 return EFI_SECURITY_VIOLATION
;
1249 // Process Time-based Authenticated variable.
1251 if ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) {
1252 return VerifyTimeBasedPayloadAndUpdate (
1264 // Determine if first time SetVariable with the EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS.
1266 if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) {
1268 // Determine current operation type.
1270 if (DataSize
== AUTHINFO_SIZE
) {
1274 // Determine whether this is the first time with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS set.
1276 if (OrgVariableInfo
.Data
== NULL
) {
1278 } else if ((OrgVariableInfo
.Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) == 0) {
1281 KeyIndex
= OrgVariableInfo
.PubKeyIndex
;
1282 IsFirstTime
= FALSE
;
1284 } else if ((OrgVariableInfo
.Data
!= NULL
) &&
1285 ((OrgVariableInfo
.Attributes
& (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
)) != 0)
1288 // If the variable is already write-protected, it always needs authentication before update.
1290 return EFI_WRITE_PROTECTED
;
1293 // If without EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS, set and attributes collision.
1294 // That means it is not authenticated variable, just update variable as usual.
1296 Status
= AuthServiceInternalUpdateVariable (VariableName
, VendorGuid
, Data
, DataSize
, Attributes
);
1301 // Get PubKey and check Monotonic Count value corresponding to the variable.
1303 CertData
= (EFI_VARIABLE_AUTHENTICATION
*) Data
;
1304 CertBlock
= (EFI_CERT_BLOCK_RSA_2048_SHA256
*) (CertData
->AuthInfo
.CertData
);
1305 PubKey
= CertBlock
->PublicKey
;
1308 // Update Monotonic Count value.
1310 MonotonicCount
= CertData
->MonotonicCount
;
1314 // 2 cases need to check here
1315 // 1. Internal PubKey variable. PubKeyIndex is always 0
1316 // 2. Other counter-based AuthVariable. Check input PubKey.
1318 if (KeyIndex
== 0) {
1319 return EFI_SECURITY_VIOLATION
;
1321 for (Index
= 0; Index
< mPubKeyNumber
; Index
++) {
1322 if (ReadUnaligned32 (&(((AUTHVAR_KEY_DB_DATA
*) mPubKeyStore
+ Index
)->KeyIndex
)) == KeyIndex
) {
1323 if (CompareMem (((AUTHVAR_KEY_DB_DATA
*) mPubKeyStore
+ Index
)->KeyData
, PubKey
, EFI_CERT_TYPE_RSA2048_SIZE
) == 0) {
1326 return EFI_SECURITY_VIOLATION
;
1330 if (Index
== mPubKeyNumber
) {
1331 return EFI_SECURITY_VIOLATION
;
1335 // Compare the current monotonic count and ensure that it is greater than the last SetVariable
1336 // operation with the EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS attribute set.
1338 if (MonotonicCount
<= OrgVariableInfo
.MonotonicCount
) {
1340 // Monotonic count check fail, suspicious replay attack, return EFI_SECURITY_VIOLATION.
1342 return EFI_SECURITY_VIOLATION
;
1346 // Verify the certificate in Data payload.
1348 Status
= VerifyCounterBasedPayload (Data
, DataSize
, PubKey
);
1349 if (EFI_ERROR (Status
)) {
1354 // Now, the signature has been verified!
1356 if (IsFirstTime
&& !IsDeletion
) {
1357 VariableDataEntry
.VariableSize
= DataSize
- AUTHINFO_SIZE
;
1358 VariableDataEntry
.Guid
= VendorGuid
;
1359 VariableDataEntry
.Name
= VariableName
;
1362 // Update public key database variable if need.
1364 KeyIndex
= AddPubKeyInStore (PubKey
, &VariableDataEntry
);
1365 if (KeyIndex
== 0) {
1366 return EFI_OUT_OF_RESOURCES
;
1371 // Verification pass.
1373 return AuthServiceInternalUpdateVariableWithMonotonicCount (VariableName
, VendorGuid
, (UINT8
*)Data
+ AUTHINFO_SIZE
, DataSize
- AUTHINFO_SIZE
, Attributes
, KeyIndex
, MonotonicCount
);
1377 Filter out the duplicated EFI_SIGNATURE_DATA from the new data by comparing to the original data.
1379 @param[in] Data Pointer to original EFI_SIGNATURE_LIST.
1380 @param[in] DataSize Size of Data buffer.
1381 @param[in, out] NewData Pointer to new EFI_SIGNATURE_LIST.
1382 @param[in, out] NewDataSize Size of NewData buffer.
1386 FilterSignatureList (
1389 IN OUT VOID
*NewData
,
1390 IN OUT UINTN
*NewDataSize
1393 EFI_SIGNATURE_LIST
*CertList
;
1394 EFI_SIGNATURE_DATA
*Cert
;
1396 EFI_SIGNATURE_LIST
*NewCertList
;
1397 EFI_SIGNATURE_DATA
*NewCert
;
1404 UINTN SignatureListSize
;
1410 if (*NewDataSize
== 0) {
1414 TempDataSize
= *NewDataSize
;
1415 Status
= mAuthVarLibContextIn
->GetScratchBuffer (&TempDataSize
, (VOID
**) &TempData
);
1416 if (EFI_ERROR (Status
)) {
1417 return EFI_OUT_OF_RESOURCES
;
1422 NewCertList
= (EFI_SIGNATURE_LIST
*) NewData
;
1423 while ((*NewDataSize
> 0) && (*NewDataSize
>= NewCertList
->SignatureListSize
)) {
1424 NewCert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) NewCertList
+ sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
);
1425 NewCertCount
= (NewCertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - NewCertList
->SignatureHeaderSize
) / NewCertList
->SignatureSize
;
1428 for (Index
= 0; Index
< NewCertCount
; Index
++) {
1432 CertList
= (EFI_SIGNATURE_LIST
*) Data
;
1433 while ((Size
> 0) && (Size
>= CertList
->SignatureListSize
)) {
1434 if (CompareGuid (&CertList
->SignatureType
, &NewCertList
->SignatureType
) &&
1435 (CertList
->SignatureSize
== NewCertList
->SignatureSize
)) {
1436 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
1437 CertCount
= (CertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - CertList
->SignatureHeaderSize
) / CertList
->SignatureSize
;
1438 for (Index2
= 0; Index2
< CertCount
; Index2
++) {
1440 // Iterate each Signature Data in this Signature List.
1442 if (CompareMem (NewCert
, Cert
, CertList
->SignatureSize
) == 0) {
1446 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) Cert
+ CertList
->SignatureSize
);
1453 Size
-= CertList
->SignatureListSize
;
1454 CertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) CertList
+ CertList
->SignatureListSize
);
1459 // New EFI_SIGNATURE_DATA, keep it.
1461 if (CopiedCount
== 0) {
1463 // Copy EFI_SIGNATURE_LIST header for only once.
1465 CopyMem (Tail
, NewCertList
, sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
);
1466 Tail
= Tail
+ sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
;
1469 CopyMem (Tail
, NewCert
, NewCertList
->SignatureSize
);
1470 Tail
+= NewCertList
->SignatureSize
;
1474 NewCert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) NewCert
+ NewCertList
->SignatureSize
);
1478 // Update SignatureListSize in the kept EFI_SIGNATURE_LIST.
1480 if (CopiedCount
!= 0) {
1481 SignatureListSize
= sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
+ (CopiedCount
* NewCertList
->SignatureSize
);
1482 CertList
= (EFI_SIGNATURE_LIST
*) (Tail
- SignatureListSize
);
1483 CertList
->SignatureListSize
= (UINT32
) SignatureListSize
;
1486 *NewDataSize
-= NewCertList
->SignatureListSize
;
1487 NewCertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) NewCertList
+ NewCertList
->SignatureListSize
);
1490 TempDataSize
= (Tail
- (UINT8
*) TempData
);
1492 CopyMem (NewData
, TempData
, TempDataSize
);
1493 *NewDataSize
= TempDataSize
;
1499 Compare two EFI_TIME data.
1502 @param FirstTime A pointer to the first EFI_TIME data.
1503 @param SecondTime A pointer to the second EFI_TIME data.
1505 @retval TRUE The FirstTime is not later than the SecondTime.
1506 @retval FALSE The FirstTime is later than the SecondTime.
1510 AuthServiceInternalCompareTimeStamp (
1511 IN EFI_TIME
*FirstTime
,
1512 IN EFI_TIME
*SecondTime
1515 if (FirstTime
->Year
!= SecondTime
->Year
) {
1516 return (BOOLEAN
) (FirstTime
->Year
< SecondTime
->Year
);
1517 } else if (FirstTime
->Month
!= SecondTime
->Month
) {
1518 return (BOOLEAN
) (FirstTime
->Month
< SecondTime
->Month
);
1519 } else if (FirstTime
->Day
!= SecondTime
->Day
) {
1520 return (BOOLEAN
) (FirstTime
->Day
< SecondTime
->Day
);
1521 } else if (FirstTime
->Hour
!= SecondTime
->Hour
) {
1522 return (BOOLEAN
) (FirstTime
->Hour
< SecondTime
->Hour
);
1523 } else if (FirstTime
->Minute
!= SecondTime
->Minute
) {
1524 return (BOOLEAN
) (FirstTime
->Minute
< SecondTime
->Minute
);
1527 return (BOOLEAN
) (FirstTime
->Second
<= SecondTime
->Second
);
1531 Find matching signer's certificates for common authenticated variable
1532 by corresponding VariableName and VendorGuid from "certdb" or "certdbv".
1534 The data format of "certdb" or "certdbv":
1536 // UINT32 CertDbListSize;
1537 // /// AUTH_CERT_DB_DATA Certs1[];
1538 // /// AUTH_CERT_DB_DATA Certs2[];
1540 // /// AUTH_CERT_DB_DATA Certsn[];
1543 @param[in] VariableName Name of authenticated Variable.
1544 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1545 @param[in] Data Pointer to variable "certdb" or "certdbv".
1546 @param[in] DataSize Size of variable "certdb" or "certdbv".
1547 @param[out] CertOffset Offset of matching CertData, from starting of Data.
1548 @param[out] CertDataSize Length of CertData in bytes.
1549 @param[out] CertNodeOffset Offset of matching AUTH_CERT_DB_DATA , from
1551 @param[out] CertNodeSize Length of AUTH_CERT_DB_DATA in bytes.
1553 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1554 @retval EFI_NOT_FOUND Fail to find matching certs.
1555 @retval EFI_SUCCESS Find matching certs and output parameters.
1560 IN CHAR16
*VariableName
,
1561 IN EFI_GUID
*VendorGuid
,
1564 OUT UINT32
*CertOffset
, OPTIONAL
1565 OUT UINT32
*CertDataSize
, OPTIONAL
1566 OUT UINT32
*CertNodeOffset
,OPTIONAL
1567 OUT UINT32
*CertNodeSize OPTIONAL
1571 AUTH_CERT_DB_DATA
*Ptr
;
1575 UINT32 CertDbListSize
;
1577 if ((VariableName
== NULL
) || (VendorGuid
== NULL
) || (Data
== NULL
)) {
1578 return EFI_INVALID_PARAMETER
;
1582 // Check whether DataSize matches recorded CertDbListSize.
1584 if (DataSize
< sizeof (UINT32
)) {
1585 return EFI_INVALID_PARAMETER
;
1588 CertDbListSize
= ReadUnaligned32 ((UINT32
*) Data
);
1590 if (CertDbListSize
!= (UINT32
) DataSize
) {
1591 return EFI_INVALID_PARAMETER
;
1594 Offset
= sizeof (UINT32
);
1597 // Get corresponding certificates by VendorGuid and VariableName.
1599 while (Offset
< (UINT32
) DataSize
) {
1600 Ptr
= (AUTH_CERT_DB_DATA
*) (Data
+ Offset
);
1602 // Check whether VendorGuid matches.
1604 if (CompareGuid (&Ptr
->VendorGuid
, VendorGuid
)) {
1605 NodeSize
= ReadUnaligned32 (&Ptr
->CertNodeSize
);
1606 NameSize
= ReadUnaligned32 (&Ptr
->NameSize
);
1607 CertSize
= ReadUnaligned32 (&Ptr
->CertDataSize
);
1609 if (NodeSize
!= sizeof (EFI_GUID
) + sizeof (UINT32
) * 3 + CertSize
+
1610 sizeof (CHAR16
) * NameSize
) {
1611 return EFI_INVALID_PARAMETER
;
1614 Offset
= Offset
+ sizeof (EFI_GUID
) + sizeof (UINT32
) * 3;
1616 // Check whether VariableName matches.
1618 if ((NameSize
== StrLen (VariableName
)) &&
1619 (CompareMem (Data
+ Offset
, VariableName
, NameSize
* sizeof (CHAR16
)) == 0)) {
1620 Offset
= Offset
+ NameSize
* sizeof (CHAR16
);
1622 if (CertOffset
!= NULL
) {
1623 *CertOffset
= Offset
;
1626 if (CertDataSize
!= NULL
) {
1627 *CertDataSize
= CertSize
;
1630 if (CertNodeOffset
!= NULL
) {
1631 *CertNodeOffset
= (UINT32
) ((UINT8
*) Ptr
- Data
);
1634 if (CertNodeSize
!= NULL
) {
1635 *CertNodeSize
= NodeSize
;
1640 Offset
= Offset
+ NameSize
* sizeof (CHAR16
) + CertSize
;
1643 NodeSize
= ReadUnaligned32 (&Ptr
->CertNodeSize
);
1644 Offset
= Offset
+ NodeSize
;
1648 return EFI_NOT_FOUND
;
1652 Retrieve signer's certificates for common authenticated variable
1653 by corresponding VariableName and VendorGuid from "certdb"
1654 or "certdbv" according to authenticated variable attributes.
1656 @param[in] VariableName Name of authenticated Variable.
1657 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1658 @param[in] Attributes Attributes of authenticated variable.
1659 @param[out] CertData Pointer to signer's certificates.
1660 @param[out] CertDataSize Length of CertData in bytes.
1662 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1663 @retval EFI_NOT_FOUND Fail to find "certdb"/"certdbv" or matching certs.
1664 @retval EFI_SUCCESS Get signer's certificates successfully.
1669 IN CHAR16
*VariableName
,
1670 IN EFI_GUID
*VendorGuid
,
1671 IN UINT32 Attributes
,
1672 OUT UINT8
**CertData
,
1673 OUT UINT32
*CertDataSize
1682 if ((VariableName
== NULL
) || (VendorGuid
== NULL
) || (CertData
== NULL
) || (CertDataSize
== NULL
)) {
1683 return EFI_INVALID_PARAMETER
;
1687 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
1689 // Get variable "certdb".
1691 DbName
= EFI_CERT_DB_NAME
;
1694 // Get variable "certdbv".
1696 DbName
= EFI_CERT_DB_VOLATILE_NAME
;
1700 // Get variable "certdb" or "certdbv".
1702 Status
= AuthServiceInternalFindVariable (
1708 if (EFI_ERROR (Status
)) {
1712 if ((DataSize
== 0) || (Data
== NULL
)) {
1714 return EFI_NOT_FOUND
;
1717 Status
= FindCertsFromDb (
1728 if (EFI_ERROR (Status
)) {
1732 *CertData
= Data
+ CertOffset
;
1737 Delete matching signer's certificates when deleting common authenticated
1738 variable by corresponding VariableName and VendorGuid from "certdb" or
1739 "certdbv" according to authenticated variable attributes.
1741 @param[in] VariableName Name of authenticated Variable.
1742 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1743 @param[in] Attributes Attributes of authenticated variable.
1745 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1746 @retval EFI_NOT_FOUND Fail to find "certdb"/"certdbv" or matching certs.
1747 @retval EFI_OUT_OF_RESOURCES The operation is failed due to lack of resources.
1748 @retval EFI_SUCCESS The operation is completed successfully.
1753 IN CHAR16
*VariableName
,
1754 IN EFI_GUID
*VendorGuid
,
1755 IN UINT32 Attributes
1762 UINT32 CertNodeOffset
;
1763 UINT32 CertNodeSize
;
1765 UINT32 NewCertDbSize
;
1768 if ((VariableName
== NULL
) || (VendorGuid
== NULL
)) {
1769 return EFI_INVALID_PARAMETER
;
1772 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
1774 // Get variable "certdb".
1776 DbName
= EFI_CERT_DB_NAME
;
1777 VarAttr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
1780 // Get variable "certdbv".
1782 DbName
= EFI_CERT_DB_VOLATILE_NAME
;
1783 VarAttr
= EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
1786 Status
= AuthServiceInternalFindVariable (
1793 if (EFI_ERROR (Status
)) {
1797 if ((DataSize
== 0) || (Data
== NULL
)) {
1799 return EFI_NOT_FOUND
;
1802 if (DataSize
== sizeof (UINT32
)) {
1804 // There is no certs in "certdb" or "certdbv".
1810 // Get corresponding cert node from "certdb" or "certdbv".
1812 Status
= FindCertsFromDb (
1823 if (EFI_ERROR (Status
)) {
1827 if (DataSize
< (CertNodeOffset
+ CertNodeSize
)) {
1828 return EFI_NOT_FOUND
;
1832 // Construct new data content of variable "certdb" or "certdbv".
1834 NewCertDbSize
= (UINT32
) DataSize
- CertNodeSize
;
1835 NewCertDb
= (UINT8
*) mCertDbStore
;
1838 // Copy the DB entries before deleting node.
1840 CopyMem (NewCertDb
, Data
, CertNodeOffset
);
1842 // Update CertDbListSize.
1844 CopyMem (NewCertDb
, &NewCertDbSize
, sizeof (UINT32
));
1846 // Copy the DB entries after deleting node.
1848 if (DataSize
> (CertNodeOffset
+ CertNodeSize
)) {
1850 NewCertDb
+ CertNodeOffset
,
1851 Data
+ CertNodeOffset
+ CertNodeSize
,
1852 DataSize
- CertNodeOffset
- CertNodeSize
1857 // Set "certdb" or "certdbv".
1859 Status
= AuthServiceInternalUpdateVariable (
1871 Insert signer's certificates for common authenticated variable with VariableName
1872 and VendorGuid in AUTH_CERT_DB_DATA to "certdb" or "certdbv" according to
1873 time based authenticated variable attributes.
1875 @param[in] VariableName Name of authenticated Variable.
1876 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1877 @param[in] Attributes Attributes of authenticated variable.
1878 @param[in] CertData Pointer to signer's certificates.
1879 @param[in] CertDataSize Length of CertData in bytes.
1881 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1882 @retval EFI_ACCESS_DENIED An AUTH_CERT_DB_DATA entry with same VariableName
1883 and VendorGuid already exists.
1884 @retval EFI_OUT_OF_RESOURCES The operation is failed due to lack of resources.
1885 @retval EFI_SUCCESS Insert an AUTH_CERT_DB_DATA entry to "certdb" or "certdbv"
1890 IN CHAR16
*VariableName
,
1891 IN EFI_GUID
*VendorGuid
,
1892 IN UINT32 Attributes
,
1894 IN UINTN CertDataSize
1902 UINT32 NewCertDbSize
;
1903 UINT32 CertNodeSize
;
1905 AUTH_CERT_DB_DATA
*Ptr
;
1908 if ((VariableName
== NULL
) || (VendorGuid
== NULL
) || (CertData
== NULL
)) {
1909 return EFI_INVALID_PARAMETER
;
1912 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
1914 // Get variable "certdb".
1916 DbName
= EFI_CERT_DB_NAME
;
1917 VarAttr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
1920 // Get variable "certdbv".
1922 DbName
= EFI_CERT_DB_VOLATILE_NAME
;
1923 VarAttr
= EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
1927 // Get variable "certdb" or "certdbv".
1929 Status
= AuthServiceInternalFindVariable (
1935 if (EFI_ERROR (Status
)) {
1939 if ((DataSize
== 0) || (Data
== NULL
)) {
1941 return EFI_NOT_FOUND
;
1945 // Find whether matching cert node already exists in "certdb" or "certdbv".
1946 // If yes return error.
1948 Status
= FindCertsFromDb (
1959 if (!EFI_ERROR (Status
)) {
1961 return EFI_ACCESS_DENIED
;
1965 // Construct new data content of variable "certdb" or "certdbv".
1967 NameSize
= (UINT32
) StrLen (VariableName
);
1968 CertNodeSize
= sizeof (AUTH_CERT_DB_DATA
) + (UINT32
) CertDataSize
+ NameSize
* sizeof (CHAR16
);
1969 NewCertDbSize
= (UINT32
) DataSize
+ CertNodeSize
;
1970 if (NewCertDbSize
> mMaxCertDbSize
) {
1971 return EFI_OUT_OF_RESOURCES
;
1973 NewCertDb
= (UINT8
*) mCertDbStore
;
1976 // Copy the DB entries before inserting node.
1978 CopyMem (NewCertDb
, Data
, DataSize
);
1980 // Update CertDbListSize.
1982 CopyMem (NewCertDb
, &NewCertDbSize
, sizeof (UINT32
));
1984 // Construct new cert node.
1986 Ptr
= (AUTH_CERT_DB_DATA
*) (NewCertDb
+ DataSize
);
1987 CopyGuid (&Ptr
->VendorGuid
, VendorGuid
);
1988 CopyMem (&Ptr
->CertNodeSize
, &CertNodeSize
, sizeof (UINT32
));
1989 CopyMem (&Ptr
->NameSize
, &NameSize
, sizeof (UINT32
));
1990 CopyMem (&Ptr
->CertDataSize
, &CertDataSize
, sizeof (UINT32
));
1993 (UINT8
*) Ptr
+ sizeof (AUTH_CERT_DB_DATA
),
1995 NameSize
* sizeof (CHAR16
)
1999 (UINT8
*) Ptr
+ sizeof (AUTH_CERT_DB_DATA
) + NameSize
* sizeof (CHAR16
),
2005 // Set "certdb" or "certdbv".
2007 Status
= AuthServiceInternalUpdateVariable (
2019 Clean up signer's certificates for common authenticated variable
2020 by corresponding VariableName and VendorGuid from "certdb".
2021 System may break down during Timebased Variable update & certdb update,
2022 make them inconsistent, this function is called in AuthVariable Init
2023 to ensure consistency.
2025 @retval EFI_NOT_FOUND Fail to find variable "certdb".
2026 @retval EFI_OUT_OF_RESOURCES The operation is failed due to lack of resources.
2027 @retval EFI_SUCCESS The operation is completed successfully.
2036 AUTH_CERT_DB_DATA
*Ptr
;
2039 CHAR16
*VariableName
;
2041 BOOLEAN CertCleaned
;
2044 EFI_GUID AuthVarGuid
;
2045 AUTH_VARIABLE_INFO AuthVariableInfo
;
2047 Status
= EFI_SUCCESS
;
2050 // Get corresponding certificates by VendorGuid and VariableName.
2053 CertCleaned
= FALSE
;
2056 // Get latest variable "certdb"
2058 Status
= AuthServiceInternalFindVariable (
2064 if (EFI_ERROR (Status
)) {
2068 if ((DataSize
== 0) || (Data
== NULL
)) {
2070 return EFI_NOT_FOUND
;
2073 Offset
= sizeof (UINT32
);
2075 while (Offset
< (UINT32
) DataSize
) {
2076 Ptr
= (AUTH_CERT_DB_DATA
*) (Data
+ Offset
);
2077 NodeSize
= ReadUnaligned32 (&Ptr
->CertNodeSize
);
2078 NameSize
= ReadUnaligned32 (&Ptr
->NameSize
);
2081 // Get VarName tailed with '\0'
2083 VariableName
= AllocateZeroPool((NameSize
+ 1) * sizeof(CHAR16
));
2084 if (VariableName
== NULL
) {
2085 return EFI_OUT_OF_RESOURCES
;
2087 CopyMem (VariableName
, (UINT8
*) Ptr
+ sizeof (AUTH_CERT_DB_DATA
), NameSize
* sizeof(CHAR16
));
2089 // Keep VarGuid aligned
2091 CopyMem (&AuthVarGuid
, &Ptr
->VendorGuid
, sizeof(EFI_GUID
));
2094 // Find corresponding time auth variable
2096 ZeroMem (&AuthVariableInfo
, sizeof (AuthVariableInfo
));
2097 Status
= mAuthVarLibContextIn
->FindVariable (
2103 if (EFI_ERROR(Status
)) {
2104 Status
= DeleteCertsFromDb(
2107 AuthVariableInfo
.Attributes
2110 DEBUG((EFI_D_INFO
, "Recovery!! Cert for Auth Variable %s Guid %g is removed for consistency\n", VariableName
, &AuthVarGuid
));
2111 FreePool(VariableName
);
2115 FreePool(VariableName
);
2116 Offset
= Offset
+ NodeSize
;
2118 } while (CertCleaned
);
2124 Process variable with EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set
2126 Caution: This function may receive untrusted input.
2127 This function may be invoked in SMM mode, and datasize and data are external input.
2128 This function will do basic validation, before parse the data.
2129 This function will parse the authentication carefully to avoid security issues, like
2130 buffer overflow, integer overflow.
2132 @param[in] VariableName Name of Variable to be found.
2133 @param[in] VendorGuid Variable vendor GUID.
2134 @param[in] Data Data pointer.
2135 @param[in] DataSize Size of Data found. If size is less than the
2136 data, this value contains the required size.
2137 @param[in] Attributes Attribute value of the variable.
2138 @param[in] AuthVarType Verify against PK, KEK database, private database or certificate in data payload.
2139 @param[in] OrgTimeStamp Pointer to original time stamp,
2140 original variable is not found if NULL.
2141 @param[out] VarPayloadPtr Pointer to variable payload address.
2142 @param[out] VarPayloadSize Pointer to variable payload size.
2144 @retval EFI_INVALID_PARAMETER Invalid parameter.
2145 @retval EFI_SECURITY_VIOLATION The variable does NOT pass the validation
2146 check carried out by the firmware.
2147 @retval EFI_OUT_OF_RESOURCES Failed to process variable due to lack
2149 @retval EFI_SUCCESS Variable pass validation successfully.
2153 VerifyTimeBasedPayload (
2154 IN CHAR16
*VariableName
,
2155 IN EFI_GUID
*VendorGuid
,
2158 IN UINT32 Attributes
,
2159 IN AUTHVAR_TYPE AuthVarType
,
2160 IN EFI_TIME
*OrgTimeStamp
,
2161 OUT UINT8
**VarPayloadPtr
,
2162 OUT UINTN
*VarPayloadSize
2165 EFI_VARIABLE_AUTHENTICATION_2
*CertData
;
2171 BOOLEAN VerifyStatus
;
2173 EFI_SIGNATURE_LIST
*CertList
;
2174 EFI_SIGNATURE_DATA
*Cert
;
2185 UINTN CertStackSize
;
2186 UINT8
*CertsInCertDb
;
2187 UINT32 CertsSizeinDb
;
2189 VerifyStatus
= FALSE
;
2195 CertsInCertDb
= NULL
;
2198 // When the attribute EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS is
2199 // set, then the Data buffer shall begin with an instance of a complete (and serialized)
2200 // EFI_VARIABLE_AUTHENTICATION_2 descriptor. The descriptor shall be followed by the new
2201 // variable value and DataSize shall reflect the combined size of the descriptor and the new
2202 // variable value. The authentication descriptor is not part of the variable data and is not
2203 // returned by subsequent calls to GetVariable().
2205 CertData
= (EFI_VARIABLE_AUTHENTICATION_2
*) Data
;
2208 // Verify that Pad1, Nanosecond, TimeZone, Daylight and Pad2 components of the
2209 // TimeStamp value are set to zero.
2211 if ((CertData
->TimeStamp
.Pad1
!= 0) ||
2212 (CertData
->TimeStamp
.Nanosecond
!= 0) ||
2213 (CertData
->TimeStamp
.TimeZone
!= 0) ||
2214 (CertData
->TimeStamp
.Daylight
!= 0) ||
2215 (CertData
->TimeStamp
.Pad2
!= 0)) {
2216 return EFI_SECURITY_VIOLATION
;
2219 if ((OrgTimeStamp
!= NULL
) && ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0)) {
2220 if (AuthServiceInternalCompareTimeStamp (&CertData
->TimeStamp
, OrgTimeStamp
)) {
2222 // TimeStamp check fail, suspicious replay attack, return EFI_SECURITY_VIOLATION.
2224 return EFI_SECURITY_VIOLATION
;
2229 // wCertificateType should be WIN_CERT_TYPE_EFI_GUID.
2230 // Cert type should be EFI_CERT_TYPE_PKCS7_GUID.
2232 if ((CertData
->AuthInfo
.Hdr
.wCertificateType
!= WIN_CERT_TYPE_EFI_GUID
) ||
2233 !CompareGuid (&CertData
->AuthInfo
.CertType
, &gEfiCertPkcs7Guid
)) {
2235 // Invalid AuthInfo type, return EFI_SECURITY_VIOLATION.
2237 return EFI_SECURITY_VIOLATION
;
2241 // Find out Pkcs7 SignedData which follows the EFI_VARIABLE_AUTHENTICATION_2 descriptor.
2242 // AuthInfo.Hdr.dwLength is the length of the entire certificate, including the length of the header.
2244 SigData
= CertData
->AuthInfo
.CertData
;
2245 SigDataSize
= CertData
->AuthInfo
.Hdr
.dwLength
- (UINT32
) (OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID
, CertData
));
2248 // Find out the new data payload which follows Pkcs7 SignedData directly.
2250 PayloadPtr
= SigData
+ SigDataSize
;
2251 PayloadSize
= DataSize
- OFFSET_OF_AUTHINFO2_CERT_DATA
- (UINTN
) SigDataSize
;
2254 // Construct a serialization buffer of the values of the VariableName, VendorGuid and Attributes
2255 // parameters of the SetVariable() call and the TimeStamp component of the
2256 // EFI_VARIABLE_AUTHENTICATION_2 descriptor followed by the variable's new value
2257 // i.e. (VariableName, VendorGuid, Attributes, TimeStamp, Data)
2259 NewDataSize
= PayloadSize
+ sizeof (EFI_TIME
) + sizeof (UINT32
) +
2260 sizeof (EFI_GUID
) + StrSize (VariableName
) - sizeof (CHAR16
);
2263 // Here is to reuse scratch data area(at the end of volatile variable store)
2264 // to reduce SMRAM consumption for SMM variable driver.
2265 // The scratch buffer is enough to hold the serialized data and safe to use,
2266 // because it is only used at here to do verification temporarily first
2267 // and then used in UpdateVariable() for a time based auth variable set.
2269 Status
= mAuthVarLibContextIn
->GetScratchBuffer (&NewDataSize
, (VOID
**) &NewData
);
2270 if (EFI_ERROR (Status
)) {
2271 return EFI_OUT_OF_RESOURCES
;
2275 Length
= StrLen (VariableName
) * sizeof (CHAR16
);
2276 CopyMem (Buffer
, VariableName
, Length
);
2279 Length
= sizeof (EFI_GUID
);
2280 CopyMem (Buffer
, VendorGuid
, Length
);
2283 Length
= sizeof (UINT32
);
2284 CopyMem (Buffer
, &Attr
, Length
);
2287 Length
= sizeof (EFI_TIME
);
2288 CopyMem (Buffer
, &CertData
->TimeStamp
, Length
);
2291 CopyMem (Buffer
, PayloadPtr
, PayloadSize
);
2293 if (AuthVarType
== AuthVarTypePk
) {
2295 // Verify that the signature has been made with the current Platform Key (no chaining for PK).
2296 // First, get signer's certificates from SignedData.
2298 VerifyStatus
= Pkcs7GetSigners (
2306 if (!VerifyStatus
) {
2311 // Second, get the current platform key from variable. Check whether it's identical with signer's certificates
2312 // in SignedData. If not, return error immediately.
2314 Status
= AuthServiceInternalFindVariable (
2315 EFI_PLATFORM_KEY_NAME
,
2316 &gEfiGlobalVariableGuid
,
2320 if (EFI_ERROR (Status
)) {
2321 VerifyStatus
= FALSE
;
2324 CertList
= (EFI_SIGNATURE_LIST
*) Data
;
2325 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
2326 if ((RootCertSize
!= (CertList
->SignatureSize
- (sizeof (EFI_SIGNATURE_DATA
) - 1))) ||
2327 (CompareMem (Cert
->SignatureData
, RootCert
, RootCertSize
) != 0)) {
2328 VerifyStatus
= FALSE
;
2333 // Verify Pkcs7 SignedData via Pkcs7Verify library.
2335 VerifyStatus
= Pkcs7Verify (
2344 } else if (AuthVarType
== AuthVarTypeKek
) {
2347 // Get KEK database from variable.
2349 Status
= AuthServiceInternalFindVariable (
2350 EFI_KEY_EXCHANGE_KEY_NAME
,
2351 &gEfiGlobalVariableGuid
,
2355 if (EFI_ERROR (Status
)) {
2360 // Ready to verify Pkcs7 SignedData. Go through KEK Signature Database to find out X.509 CertList.
2362 KekDataSize
= (UINT32
) DataSize
;
2363 CertList
= (EFI_SIGNATURE_LIST
*) Data
;
2364 while ((KekDataSize
> 0) && (KekDataSize
>= CertList
->SignatureListSize
)) {
2365 if (CompareGuid (&CertList
->SignatureType
, &gEfiCertX509Guid
)) {
2366 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
2367 CertCount
= (CertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - CertList
->SignatureHeaderSize
) / CertList
->SignatureSize
;
2368 for (Index
= 0; Index
< CertCount
; Index
++) {
2370 // Iterate each Signature Data Node within this CertList for a verify
2372 RootCert
= Cert
->SignatureData
;
2373 RootCertSize
= CertList
->SignatureSize
- (sizeof (EFI_SIGNATURE_DATA
) - 1);
2376 // Verify Pkcs7 SignedData via Pkcs7Verify library.
2378 VerifyStatus
= Pkcs7Verify (
2389 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) Cert
+ CertList
->SignatureSize
);
2392 KekDataSize
-= CertList
->SignatureListSize
;
2393 CertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) CertList
+ CertList
->SignatureListSize
);
2395 } else if (AuthVarType
== AuthVarTypePriv
) {
2398 // Process common authenticated variable except PK/KEK/DB/DBX/DBT.
2399 // Get signer's certificates from SignedData.
2401 VerifyStatus
= Pkcs7GetSigners (
2409 if (!VerifyStatus
) {
2414 // Get previously stored signer's certificates from certdb or certdbv for existing
2415 // variable. Check whether they are identical with signer's certificates
2416 // in SignedData. If not, return error immediately.
2418 if (OrgTimeStamp
!= NULL
) {
2419 VerifyStatus
= FALSE
;
2421 Status
= GetCertsFromDb (VariableName
, VendorGuid
, Attributes
, &CertsInCertDb
, &CertsSizeinDb
);
2422 if (EFI_ERROR (Status
)) {
2426 if ((CertStackSize
!= CertsSizeinDb
) ||
2427 (CompareMem (SignerCerts
, CertsInCertDb
, CertsSizeinDb
) != 0)) {
2432 VerifyStatus
= Pkcs7Verify (
2440 if (!VerifyStatus
) {
2444 if ((OrgTimeStamp
== NULL
) && (PayloadSize
!= 0)) {
2446 // Insert signer's certificates when adding a new common authenticated variable.
2448 Status
= InsertCertsToDb (VariableName
, VendorGuid
, Attributes
, SignerCerts
, CertStackSize
);
2449 if (EFI_ERROR (Status
)) {
2450 VerifyStatus
= FALSE
;
2454 } else if (AuthVarType
== AuthVarTypePayload
) {
2455 CertList
= (EFI_SIGNATURE_LIST
*) PayloadPtr
;
2456 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
2457 RootCert
= Cert
->SignatureData
;
2458 RootCertSize
= CertList
->SignatureSize
- (sizeof (EFI_SIGNATURE_DATA
) - 1);
2460 // Verify Pkcs7 SignedData via Pkcs7Verify library.
2462 VerifyStatus
= Pkcs7Verify (
2471 return EFI_SECURITY_VIOLATION
;
2476 if (AuthVarType
== AuthVarTypePk
|| AuthVarType
== AuthVarTypePriv
) {
2477 Pkcs7FreeSigners (RootCert
);
2478 Pkcs7FreeSigners (SignerCerts
);
2481 if (!VerifyStatus
) {
2482 return EFI_SECURITY_VIOLATION
;
2485 Status
= CheckSignatureListFormat(VariableName
, VendorGuid
, PayloadPtr
, PayloadSize
);
2486 if (EFI_ERROR (Status
)) {
2490 *VarPayloadPtr
= PayloadPtr
;
2491 *VarPayloadSize
= PayloadSize
;
2497 Process variable with EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set
2499 Caution: This function may receive untrusted input.
2500 This function may be invoked in SMM mode, and datasize and data are external input.
2501 This function will do basic validation, before parse the data.
2502 This function will parse the authentication carefully to avoid security issues, like
2503 buffer overflow, integer overflow.
2505 @param[in] VariableName Name of Variable to be found.
2506 @param[in] VendorGuid Variable vendor GUID.
2507 @param[in] Data Data pointer.
2508 @param[in] DataSize Size of Data found. If size is less than the
2509 data, this value contains the required size.
2510 @param[in] Attributes Attribute value of the variable.
2511 @param[in] AuthVarType Verify against PK, KEK database, private database or certificate in data payload.
2512 @param[out] VarDel Delete the variable or not.
2514 @retval EFI_INVALID_PARAMETER Invalid parameter.
2515 @retval EFI_SECURITY_VIOLATION The variable does NOT pass the validation
2516 check carried out by the firmware.
2517 @retval EFI_OUT_OF_RESOURCES Failed to process variable due to lack
2519 @retval EFI_SUCCESS Variable pass validation successfully.
2523 VerifyTimeBasedPayloadAndUpdate (
2524 IN CHAR16
*VariableName
,
2525 IN EFI_GUID
*VendorGuid
,
2528 IN UINT32 Attributes
,
2529 IN AUTHVAR_TYPE AuthVarType
,
2534 EFI_STATUS FindStatus
;
2537 EFI_VARIABLE_AUTHENTICATION_2
*CertData
;
2538 AUTH_VARIABLE_INFO OrgVariableInfo
;
2541 ZeroMem (&OrgVariableInfo
, sizeof (OrgVariableInfo
));
2542 FindStatus
= mAuthVarLibContextIn
->FindVariable (
2548 Status
= VerifyTimeBasedPayload (
2555 (!EFI_ERROR (FindStatus
)) ? OrgVariableInfo
.TimeStamp
: NULL
,
2559 if (EFI_ERROR (Status
)) {
2563 if (!EFI_ERROR(FindStatus
)
2564 && (PayloadSize
== 0)
2565 && ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0)) {
2571 CertData
= (EFI_VARIABLE_AUTHENTICATION_2
*) Data
;
2574 // Final step: Update/Append Variable if it pass Pkcs7Verify
2576 Status
= AuthServiceInternalUpdateVariableWithTimeStamp (
2582 &CertData
->TimeStamp
2586 // Delete signer's certificates when delete the common authenticated variable.
2588 if (IsDel
&& AuthVarType
== AuthVarTypePriv
&& !EFI_ERROR(Status
) ) {
2589 Status
= DeleteCertsFromDb (VariableName
, VendorGuid
, Attributes
);
2592 if (VarDel
!= NULL
) {
2593 if (IsDel
&& !EFI_ERROR(Status
)) {