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 - 2017, 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 };
39 CONST UINT8 mSha256OidValue
[] = { 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01 };
42 // Requirement for different signature type which have been defined in UEFI spec.
43 // These data are used to perform SignatureList format check while setting PK/KEK variable.
45 EFI_SIGNATURE_ITEM mSupportSigItem
[] = {
46 //{SigType, SigHeaderSize, SigDataSize }
47 {EFI_CERT_SHA256_GUID
, 0, 32 },
48 {EFI_CERT_RSA2048_GUID
, 0, 256 },
49 {EFI_CERT_RSA2048_SHA256_GUID
, 0, 256 },
50 {EFI_CERT_SHA1_GUID
, 0, 20 },
51 {EFI_CERT_RSA2048_SHA1_GUID
, 0, 256 },
52 {EFI_CERT_X509_GUID
, 0, ((UINT32
) ~0)},
53 {EFI_CERT_SHA224_GUID
, 0, 28 },
54 {EFI_CERT_SHA384_GUID
, 0, 48 },
55 {EFI_CERT_SHA512_GUID
, 0, 64 },
56 {EFI_CERT_X509_SHA256_GUID
, 0, 48 },
57 {EFI_CERT_X509_SHA384_GUID
, 0, 64 },
58 {EFI_CERT_X509_SHA512_GUID
, 0, 80 }
62 Finds variable in storage blocks of volatile and non-volatile storage areas.
64 This code finds variable in storage blocks of volatile and non-volatile storage areas.
65 If VariableName is an empty string, then we just return the first
66 qualified variable without comparing VariableName and VendorGuid.
68 @param[in] VariableName Name of the variable to be found.
69 @param[in] VendorGuid Variable vendor GUID to be found.
70 @param[out] Data Pointer to data address.
71 @param[out] DataSize Pointer to data size.
73 @retval EFI_INVALID_PARAMETER If VariableName is not an empty string,
74 while VendorGuid is NULL.
75 @retval EFI_SUCCESS Variable successfully found.
76 @retval EFI_NOT_FOUND Variable not found
80 AuthServiceInternalFindVariable (
81 IN CHAR16
*VariableName
,
82 IN EFI_GUID
*VendorGuid
,
88 AUTH_VARIABLE_INFO AuthVariableInfo
;
90 ZeroMem (&AuthVariableInfo
, sizeof (AuthVariableInfo
));
91 Status
= mAuthVarLibContextIn
->FindVariable (
96 *Data
= AuthVariableInfo
.Data
;
97 *DataSize
= AuthVariableInfo
.DataSize
;
102 Update the variable region with Variable information.
104 @param[in] VariableName Name of variable.
105 @param[in] VendorGuid Guid of variable.
106 @param[in] Data Data pointer.
107 @param[in] DataSize Size of Data.
108 @param[in] Attributes Attribute value of the variable.
110 @retval EFI_SUCCESS The update operation is success.
111 @retval EFI_INVALID_PARAMETER Invalid parameter.
112 @retval EFI_WRITE_PROTECTED Variable is write-protected.
113 @retval EFI_OUT_OF_RESOURCES There is not enough resource.
117 AuthServiceInternalUpdateVariable (
118 IN CHAR16
*VariableName
,
119 IN EFI_GUID
*VendorGuid
,
125 AUTH_VARIABLE_INFO AuthVariableInfo
;
127 ZeroMem (&AuthVariableInfo
, sizeof (AuthVariableInfo
));
128 AuthVariableInfo
.VariableName
= VariableName
;
129 AuthVariableInfo
.VendorGuid
= VendorGuid
;
130 AuthVariableInfo
.Data
= Data
;
131 AuthVariableInfo
.DataSize
= DataSize
;
132 AuthVariableInfo
.Attributes
= Attributes
;
134 return mAuthVarLibContextIn
->UpdateVariable (
140 Update the variable region with Variable information.
142 @param[in] VariableName Name of variable.
143 @param[in] VendorGuid Guid of variable.
144 @param[in] Data Data pointer.
145 @param[in] DataSize Size of Data.
146 @param[in] Attributes Attribute value of the variable.
147 @param[in] KeyIndex Index of associated public key.
148 @param[in] MonotonicCount Value of associated monotonic count.
150 @retval EFI_SUCCESS The update operation is success.
151 @retval EFI_INVALID_PARAMETER Invalid parameter.
152 @retval EFI_WRITE_PROTECTED Variable is write-protected.
153 @retval EFI_OUT_OF_RESOURCES There is not enough resource.
157 AuthServiceInternalUpdateVariableWithMonotonicCount (
158 IN CHAR16
*VariableName
,
159 IN EFI_GUID
*VendorGuid
,
162 IN UINT32 Attributes
,
164 IN UINT64 MonotonicCount
167 AUTH_VARIABLE_INFO AuthVariableInfo
;
169 ZeroMem (&AuthVariableInfo
, sizeof (AuthVariableInfo
));
170 AuthVariableInfo
.VariableName
= VariableName
;
171 AuthVariableInfo
.VendorGuid
= VendorGuid
;
172 AuthVariableInfo
.Data
= Data
;
173 AuthVariableInfo
.DataSize
= DataSize
;
174 AuthVariableInfo
.Attributes
= Attributes
;
175 AuthVariableInfo
.PubKeyIndex
= KeyIndex
;
176 AuthVariableInfo
.MonotonicCount
= MonotonicCount
;
178 return mAuthVarLibContextIn
->UpdateVariable (
184 Update the variable region with Variable information.
186 @param[in] VariableName Name of variable.
187 @param[in] VendorGuid Guid of variable.
188 @param[in] Data Data pointer.
189 @param[in] DataSize Size of Data.
190 @param[in] Attributes Attribute value of the variable.
191 @param[in] TimeStamp Value of associated TimeStamp.
193 @retval EFI_SUCCESS The update operation is success.
194 @retval EFI_INVALID_PARAMETER Invalid parameter.
195 @retval EFI_WRITE_PROTECTED Variable is write-protected.
196 @retval EFI_OUT_OF_RESOURCES There is not enough resource.
200 AuthServiceInternalUpdateVariableWithTimeStamp (
201 IN CHAR16
*VariableName
,
202 IN EFI_GUID
*VendorGuid
,
205 IN UINT32 Attributes
,
206 IN EFI_TIME
*TimeStamp
209 EFI_STATUS FindStatus
;
212 AUTH_VARIABLE_INFO AuthVariableInfo
;
214 FindStatus
= AuthServiceInternalFindVariable (
222 // EFI_VARIABLE_APPEND_WRITE attribute only effects for existing variable
224 if (!EFI_ERROR (FindStatus
) && ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) != 0)) {
225 if ((CompareGuid (VendorGuid
, &gEfiImageSecurityDatabaseGuid
) &&
226 ((StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE
) == 0) || (StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE1
) == 0) ||
227 (StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE2
) == 0))) ||
228 (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, EFI_KEY_EXCHANGE_KEY_NAME
) == 0))) {
230 // For variables with formatted as EFI_SIGNATURE_LIST, the driver shall not perform an append of
231 // EFI_SIGNATURE_DATA values that are already part of the existing variable value.
233 FilterSignatureList (
242 ZeroMem (&AuthVariableInfo
, sizeof (AuthVariableInfo
));
243 AuthVariableInfo
.VariableName
= VariableName
;
244 AuthVariableInfo
.VendorGuid
= VendorGuid
;
245 AuthVariableInfo
.Data
= Data
;
246 AuthVariableInfo
.DataSize
= DataSize
;
247 AuthVariableInfo
.Attributes
= Attributes
;
248 AuthVariableInfo
.TimeStamp
= TimeStamp
;
249 return mAuthVarLibContextIn
->UpdateVariable (
255 Determine whether this operation needs a physical present user.
257 @param[in] VariableName Name of the Variable.
258 @param[in] VendorGuid GUID of the Variable.
260 @retval TRUE This variable is protected, only a physical present user could set this variable.
261 @retval FALSE This variable is not protected.
265 NeedPhysicallyPresent(
266 IN CHAR16
*VariableName
,
267 IN EFI_GUID
*VendorGuid
270 if ((CompareGuid (VendorGuid
, &gEfiSecureBootEnableDisableGuid
) && (StrCmp (VariableName
, EFI_SECURE_BOOT_ENABLE_NAME
) == 0))
271 || (CompareGuid (VendorGuid
, &gEfiCustomModeEnableGuid
) && (StrCmp (VariableName
, EFI_CUSTOM_MODE_NAME
) == 0))) {
279 Determine whether the platform is operating in Custom Secure Boot mode.
281 @retval TRUE The platform is operating in Custom mode.
282 @retval FALSE The platform is operating in Standard mode.
294 Status
= AuthServiceInternalFindVariable (EFI_CUSTOM_MODE_NAME
, &gEfiCustomModeEnableGuid
, &Data
, &DataSize
);
295 if (!EFI_ERROR (Status
) && (*(UINT8
*) Data
== CUSTOM_SECURE_BOOT_MODE
)) {
303 Get available public key index.
305 @param[in] PubKey Pointer to Public Key data.
307 @return Public key index, 0 if no any public key index available.
311 GetAvailableKeyIndex (
323 AUTH_VARIABLE_INFO AuthVariableInfo
;
326 Status
= AuthServiceInternalFindVariable (
328 &gEfiAuthenticatedVariableGuid
,
332 if (EFI_ERROR (Status
)) {
333 DEBUG ((EFI_D_ERROR
, "Get public key database variable failure, Status = %r\n", Status
));
337 if (mPubKeyNumber
== mMaxKeyNumber
) {
339 AuthVariableInfo
.VariableName
= Name
;
340 ZeroMem (&VendorGuid
, sizeof (VendorGuid
));
341 AuthVariableInfo
.VendorGuid
= &VendorGuid
;
344 // Collect valid key data.
347 Status
= mAuthVarLibContextIn
->FindNextVariable (AuthVariableInfo
.VariableName
, AuthVariableInfo
.VendorGuid
, &AuthVariableInfo
);
348 if (!EFI_ERROR (Status
)) {
349 if (AuthVariableInfo
.PubKeyIndex
!= 0) {
350 for (Ptr
= Data
; Ptr
< (Data
+ DataSize
); Ptr
+= sizeof (AUTHVAR_KEY_DB_DATA
)) {
351 if (ReadUnaligned32 (&(((AUTHVAR_KEY_DB_DATA
*) Ptr
)->KeyIndex
)) == AuthVariableInfo
.PubKeyIndex
) {
353 // Check if the key data has been collected.
355 for (Index
= 0; Index
< mPubKeyNumber
; Index
++) {
356 if (ReadUnaligned32 (&(((AUTHVAR_KEY_DB_DATA
*) mPubKeyStore
+ Index
)->KeyIndex
)) == AuthVariableInfo
.PubKeyIndex
) {
360 if (Index
== mPubKeyNumber
) {
364 CopyMem ((AUTHVAR_KEY_DB_DATA
*) mPubKeyStore
+ mPubKeyNumber
, Ptr
, sizeof (AUTHVAR_KEY_DB_DATA
));
372 } while (Status
!= EFI_NOT_FOUND
);
375 // No available space to add new public key.
377 if (mPubKeyNumber
== mMaxKeyNumber
) {
383 // Find available public key index.
385 for (KeyIndex
= 1; KeyIndex
<= mMaxKeyNumber
; KeyIndex
++) {
387 for (Ptr
= mPubKeyStore
; Ptr
< (mPubKeyStore
+ mPubKeyNumber
* sizeof (AUTHVAR_KEY_DB_DATA
)); Ptr
+= sizeof (AUTHVAR_KEY_DB_DATA
)) {
388 if (ReadUnaligned32 (&(((AUTHVAR_KEY_DB_DATA
*) Ptr
)->KeyIndex
)) == KeyIndex
) {
402 Add public key in store and return its index.
404 @param[in] PubKey Input pointer to Public Key data.
405 @param[in] VariableDataEntry The variable data entry.
407 @return Index of new added public key.
413 IN VARIABLE_ENTRY_CONSISTENCY
*VariableDataEntry
418 VARIABLE_ENTRY_CONSISTENCY PublicKeyEntry
;
422 if (PubKey
== NULL
) {
427 // Check whether the public key entry does exist.
429 for (Index
= 0; Index
< mPubKeyNumber
; Index
++) {
430 if (CompareMem (((AUTHVAR_KEY_DB_DATA
*) mPubKeyStore
+ Index
)->KeyData
, PubKey
, EFI_CERT_TYPE_RSA2048_SIZE
) == 0) {
431 return ReadUnaligned32 (&(((AUTHVAR_KEY_DB_DATA
*) mPubKeyStore
+ Index
)->KeyIndex
));
435 KeyIndex
= GetAvailableKeyIndex (PubKey
);
441 // Check the variable space for both public key and variable data.
443 PublicKeyEntry
.VariableSize
= (mPubKeyNumber
+ 1) * sizeof (AUTHVAR_KEY_DB_DATA
);
444 PublicKeyEntry
.Guid
= &gEfiAuthenticatedVariableGuid
;
445 PublicKeyEntry
.Name
= AUTHVAR_KEYDB_NAME
;
446 Attributes
= VARIABLE_ATTRIBUTE_NV_BS_RT
| EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
;
448 if (!mAuthVarLibContextIn
->CheckRemainingSpaceForConsistency (Attributes
, &PublicKeyEntry
, VariableDataEntry
, NULL
)) {
450 // No enough variable space.
455 WriteUnaligned32 (&(((AUTHVAR_KEY_DB_DATA
*) mPubKeyStore
+ mPubKeyNumber
)->KeyIndex
), KeyIndex
);
456 CopyMem (((AUTHVAR_KEY_DB_DATA
*) mPubKeyStore
+ mPubKeyNumber
)->KeyData
, PubKey
, EFI_CERT_TYPE_RSA2048_SIZE
);
460 // Update public key database variable.
462 Status
= AuthServiceInternalUpdateVariable (
464 &gEfiAuthenticatedVariableGuid
,
466 mPubKeyNumber
* sizeof (AUTHVAR_KEY_DB_DATA
),
469 if (EFI_ERROR (Status
)) {
470 DEBUG ((EFI_D_ERROR
, "Update public key database variable failure, Status = %r\n", Status
));
478 Verify data payload with AuthInfo in EFI_CERT_TYPE_RSA2048_SHA256_GUID type.
479 Follow the steps in UEFI2.2.
481 Caution: This function may receive untrusted input.
482 This function may be invoked in SMM mode, and datasize and data are external input.
483 This function will do basic validation, before parse the data.
484 This function will parse the authentication carefully to avoid security issues, like
485 buffer overflow, integer overflow.
487 @param[in] Data Pointer to data with AuthInfo.
488 @param[in] DataSize Size of Data.
489 @param[in] PubKey Public key used for verification.
491 @retval EFI_INVALID_PARAMETER Invalid parameter.
492 @retval EFI_SECURITY_VIOLATION If authentication failed.
493 @retval EFI_SUCCESS Authentication successful.
497 VerifyCounterBasedPayload (
504 EFI_VARIABLE_AUTHENTICATION
*CertData
;
505 EFI_CERT_BLOCK_RSA_2048_SHA256
*CertBlock
;
506 UINT8 Digest
[SHA256_DIGEST_SIZE
];
510 PayloadSize
= DataSize
- AUTHINFO_SIZE
;
515 if (Data
== NULL
|| PubKey
== NULL
) {
516 return EFI_INVALID_PARAMETER
;
519 CertData
= (EFI_VARIABLE_AUTHENTICATION
*) Data
;
520 CertBlock
= (EFI_CERT_BLOCK_RSA_2048_SHA256
*) (CertData
->AuthInfo
.CertData
);
523 // wCertificateType should be WIN_CERT_TYPE_EFI_GUID.
524 // Cert type should be EFI_CERT_TYPE_RSA2048_SHA256_GUID.
526 if ((CertData
->AuthInfo
.Hdr
.wCertificateType
!= WIN_CERT_TYPE_EFI_GUID
) ||
527 !CompareGuid (&CertData
->AuthInfo
.CertType
, &gEfiCertTypeRsa2048Sha256Guid
)) {
529 // Invalid AuthInfo type, return EFI_SECURITY_VIOLATION.
531 return EFI_SECURITY_VIOLATION
;
534 // Hash data payload with SHA256.
536 ZeroMem (Digest
, SHA256_DIGEST_SIZE
);
537 Status
= Sha256Init (mHashCtx
);
541 Status
= Sha256Update (mHashCtx
, Data
+ AUTHINFO_SIZE
, PayloadSize
);
548 Status
= Sha256Update (mHashCtx
, &PayloadSize
, sizeof (UINTN
));
553 // Hash Monotonic Count.
555 Status
= Sha256Update (mHashCtx
, &CertData
->MonotonicCount
, sizeof (UINT64
));
559 Status
= Sha256Final (mHashCtx
, Digest
);
564 // Generate & Initialize RSA Context.
567 ASSERT (Rsa
!= NULL
);
569 // Set RSA Key Components.
570 // NOTE: Only N and E are needed to be set as RSA public key for signature verification.
572 Status
= RsaSetKey (Rsa
, RsaKeyN
, PubKey
, EFI_CERT_TYPE_RSA2048_SIZE
);
576 Status
= RsaSetKey (Rsa
, RsaKeyE
, mRsaE
, sizeof (mRsaE
));
581 // Verify the signature.
583 Status
= RsaPkcs1Verify (
587 CertBlock
->Signature
,
588 EFI_CERT_TYPE_RSA2048_SHA256_SIZE
598 return EFI_SECURITY_VIOLATION
;
603 Update platform mode.
605 @param[in] Mode SETUP_MODE or USER_MODE.
607 @return EFI_INVALID_PARAMETER Invalid parameter.
608 @return EFI_SUCCESS Update platform mode successfully.
619 UINT8 SecureBootMode
;
620 UINT8 SecureBootEnable
;
621 UINTN VariableDataSize
;
623 Status
= AuthServiceInternalFindVariable (
625 &gEfiGlobalVariableGuid
,
629 if (EFI_ERROR (Status
)) {
634 // Update the value of SetupMode variable by a simple mem copy, this could avoid possible
635 // variable storage reclaim at runtime.
637 mPlatformMode
= (UINT8
) Mode
;
638 CopyMem (Data
, &mPlatformMode
, sizeof(UINT8
));
640 if (mAuthVarLibContextIn
->AtRuntime ()) {
642 // SecureBoot Variable indicates whether the platform firmware is operating
643 // in Secure boot mode (1) or not (0), so we should not change SecureBoot
644 // Variable in runtime.
650 // Check "SecureBoot" variable's existence.
651 // If it doesn't exist, firmware has no capability to perform driver signing verification,
652 // then set "SecureBoot" to 0.
654 Status
= AuthServiceInternalFindVariable (
655 EFI_SECURE_BOOT_MODE_NAME
,
656 &gEfiGlobalVariableGuid
,
661 // If "SecureBoot" variable exists, then check "SetupMode" variable update.
662 // If "SetupMode" variable is USER_MODE, "SecureBoot" variable is set to 1.
663 // If "SetupMode" variable is SETUP_MODE, "SecureBoot" variable is set to 0.
665 if (EFI_ERROR (Status
)) {
666 SecureBootMode
= SECURE_BOOT_MODE_DISABLE
;
668 if (mPlatformMode
== USER_MODE
) {
669 SecureBootMode
= SECURE_BOOT_MODE_ENABLE
;
670 } else if (mPlatformMode
== SETUP_MODE
) {
671 SecureBootMode
= SECURE_BOOT_MODE_DISABLE
;
673 return EFI_NOT_FOUND
;
677 Status
= AuthServiceInternalUpdateVariable (
678 EFI_SECURE_BOOT_MODE_NAME
,
679 &gEfiGlobalVariableGuid
,
682 EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
684 if (EFI_ERROR (Status
)) {
689 // Check "SecureBootEnable" variable's existence. It can enable/disable secure boot feature.
691 Status
= AuthServiceInternalFindVariable (
692 EFI_SECURE_BOOT_ENABLE_NAME
,
693 &gEfiSecureBootEnableDisableGuid
,
698 if (SecureBootMode
== SECURE_BOOT_MODE_ENABLE
) {
700 // Create the "SecureBootEnable" variable as secure boot is enabled.
702 SecureBootEnable
= SECURE_BOOT_ENABLE
;
703 VariableDataSize
= sizeof (SecureBootEnable
);
706 // Delete the "SecureBootEnable" variable if this variable exist as "SecureBoot"
707 // variable is not in secure boot state.
709 if (EFI_ERROR (Status
)) {
712 SecureBootEnable
= SECURE_BOOT_DISABLE
;
713 VariableDataSize
= 0;
716 Status
= AuthServiceInternalUpdateVariable (
717 EFI_SECURE_BOOT_ENABLE_NAME
,
718 &gEfiSecureBootEnableDisableGuid
,
721 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
727 Check input data form to make sure it is a valid EFI_SIGNATURE_LIST for PK/KEK/db/dbx/dbt variable.
729 @param[in] VariableName Name of Variable to be check.
730 @param[in] VendorGuid Variable vendor GUID.
731 @param[in] Data Point to the variable data to be checked.
732 @param[in] DataSize Size of Data.
734 @return EFI_INVALID_PARAMETER Invalid signature list format.
735 @return EFI_SUCCESS Passed signature list format check successfully.
739 CheckSignatureListFormat(
740 IN CHAR16
*VariableName
,
741 IN EFI_GUID
*VendorGuid
,
746 EFI_SIGNATURE_LIST
*SigList
;
752 EFI_SIGNATURE_DATA
*CertData
;
759 ASSERT (VariableName
!= NULL
&& VendorGuid
!= NULL
&& Data
!= NULL
);
761 if (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, EFI_PLATFORM_KEY_NAME
) == 0)){
763 } else if ((CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, EFI_KEY_EXCHANGE_KEY_NAME
) == 0)) ||
764 (CompareGuid (VendorGuid
, &gEfiImageSecurityDatabaseGuid
) &&
765 ((StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE
) == 0) || (StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE1
) == 0) ||
766 (StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE2
) == 0)))) {
773 SigList
= (EFI_SIGNATURE_LIST
*) Data
;
774 SigDataSize
= DataSize
;
778 // Walk throuth the input signature list and check the data format.
779 // If any signature is incorrectly formed, the whole check will fail.
781 while ((SigDataSize
> 0) && (SigDataSize
>= SigList
->SignatureListSize
)) {
782 for (Index
= 0; Index
< (sizeof (mSupportSigItem
) / sizeof (EFI_SIGNATURE_ITEM
)); Index
++ ) {
783 if (CompareGuid (&SigList
->SignatureType
, &mSupportSigItem
[Index
].SigType
)) {
785 // The value of SignatureSize should always be 16 (size of SignatureOwner
786 // component) add the data length according to signature type.
788 if (mSupportSigItem
[Index
].SigDataSize
!= ((UINT32
) ~0) &&
789 (SigList
->SignatureSize
- sizeof (EFI_GUID
)) != mSupportSigItem
[Index
].SigDataSize
) {
790 return EFI_INVALID_PARAMETER
;
792 if (mSupportSigItem
[Index
].SigHeaderSize
!= ((UINT32
) ~0) &&
793 SigList
->SignatureHeaderSize
!= mSupportSigItem
[Index
].SigHeaderSize
) {
794 return EFI_INVALID_PARAMETER
;
800 if (Index
== (sizeof (mSupportSigItem
) / sizeof (EFI_SIGNATURE_ITEM
))) {
802 // Undefined signature type.
804 return EFI_INVALID_PARAMETER
;
807 if (CompareGuid (&SigList
->SignatureType
, &gEfiCertX509Guid
)) {
809 // Try to retrieve the RSA public key from the X.509 certificate.
810 // If this operation fails, it's not a valid certificate.
812 RsaContext
= RsaNew ();
813 if (RsaContext
== NULL
) {
814 return EFI_INVALID_PARAMETER
;
816 CertData
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) SigList
+ sizeof (EFI_SIGNATURE_LIST
) + SigList
->SignatureHeaderSize
);
817 CertLen
= SigList
->SignatureSize
- sizeof (EFI_GUID
);
818 if (!RsaGetPublicKeyFromX509 (CertData
->SignatureData
, CertLen
, &RsaContext
)) {
819 RsaFree (RsaContext
);
820 return EFI_INVALID_PARAMETER
;
822 RsaFree (RsaContext
);
825 if ((SigList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - SigList
->SignatureHeaderSize
) % SigList
->SignatureSize
!= 0) {
826 return EFI_INVALID_PARAMETER
;
828 SigCount
+= (SigList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - SigList
->SignatureHeaderSize
) / SigList
->SignatureSize
;
830 SigDataSize
-= SigList
->SignatureListSize
;
831 SigList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) SigList
+ SigList
->SignatureListSize
);
834 if (((UINTN
) SigList
- (UINTN
) Data
) != DataSize
) {
835 return EFI_INVALID_PARAMETER
;
838 if (IsPk
&& SigCount
> 1) {
839 return EFI_INVALID_PARAMETER
;
846 Update "VendorKeys" variable to record the out of band secure boot key modification.
848 @return EFI_SUCCESS Variable is updated successfully.
849 @return Others Failed to update variable.
853 VendorKeyIsModified (
859 if (mVendorKeyState
== VENDOR_KEYS_MODIFIED
) {
862 mVendorKeyState
= VENDOR_KEYS_MODIFIED
;
864 Status
= AuthServiceInternalUpdateVariable (
865 EFI_VENDOR_KEYS_NV_VARIABLE_NAME
,
866 &gEfiVendorKeysNvGuid
,
869 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
871 if (EFI_ERROR (Status
)) {
875 return AuthServiceInternalUpdateVariable (
876 EFI_VENDOR_KEYS_VARIABLE_NAME
,
877 &gEfiGlobalVariableGuid
,
880 EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
885 Process variable with platform key for verification.
887 Caution: This function may receive untrusted input.
888 This function may be invoked in SMM mode, and datasize and data are external input.
889 This function will do basic validation, before parse the data.
890 This function will parse the authentication carefully to avoid security issues, like
891 buffer overflow, integer overflow.
892 This function will check attribute carefully to avoid authentication bypass.
894 @param[in] VariableName Name of Variable to be found.
895 @param[in] VendorGuid Variable vendor GUID.
896 @param[in] Data Data pointer.
897 @param[in] DataSize Size of Data found. If size is less than the
898 data, this value contains the required size.
899 @param[in] Attributes Attribute value of the variable
900 @param[in] IsPk Indicate whether it is to process pk.
902 @return EFI_INVALID_PARAMETER Invalid parameter.
903 @return EFI_SECURITY_VIOLATION The variable does NOT pass the validation.
904 check carried out by the firmware.
905 @return EFI_SUCCESS Variable passed validation successfully.
910 IN CHAR16
*VariableName
,
911 IN EFI_GUID
*VendorGuid
,
914 IN UINT32 Attributes OPTIONAL
,
923 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0 ||
924 (Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) == 0) {
926 // PK, KEK and db/dbx/dbt should set EFI_VARIABLE_NON_VOLATILE attribute and should be a time-based
927 // authenticated variable.
929 return EFI_INVALID_PARAMETER
;
933 // Init state of Del. State may change due to secure check
936 if ((InCustomMode() && UserPhysicalPresent()) || (mPlatformMode
== SETUP_MODE
&& !IsPk
)) {
937 Payload
= (UINT8
*) Data
+ AUTHINFO2_SIZE (Data
);
938 PayloadSize
= DataSize
- AUTHINFO2_SIZE (Data
);
939 if (PayloadSize
== 0) {
943 Status
= CheckSignatureListFormat(VariableName
, VendorGuid
, Payload
, PayloadSize
);
944 if (EFI_ERROR (Status
)) {
948 Status
= AuthServiceInternalUpdateVariableWithTimeStamp (
954 &((EFI_VARIABLE_AUTHENTICATION_2
*) Data
)->TimeStamp
956 if (EFI_ERROR(Status
)) {
960 if ((mPlatformMode
!= SETUP_MODE
) || IsPk
) {
961 Status
= VendorKeyIsModified ();
963 } else if (mPlatformMode
== USER_MODE
) {
965 // Verify against X509 Cert in PK database.
967 Status
= VerifyTimeBasedPayloadAndUpdate (
978 // Verify against the certificate in data payload.
980 Status
= VerifyTimeBasedPayloadAndUpdate (
991 if (!EFI_ERROR(Status
) && IsPk
) {
992 if (mPlatformMode
== SETUP_MODE
&& !Del
) {
994 // If enroll PK in setup mode, need change to user mode.
996 Status
= UpdatePlatformMode (USER_MODE
);
997 } else if (mPlatformMode
== USER_MODE
&& Del
){
999 // If delete PK in user mode, need change to setup mode.
1001 Status
= UpdatePlatformMode (SETUP_MODE
);
1009 Process variable with key exchange key for verification.
1011 Caution: This function may receive untrusted input.
1012 This function may be invoked in SMM mode, and datasize and data are external input.
1013 This function will do basic validation, before parse the data.
1014 This function will parse the authentication carefully to avoid security issues, like
1015 buffer overflow, integer overflow.
1016 This function will check attribute carefully to avoid authentication bypass.
1018 @param[in] VariableName Name of Variable to be found.
1019 @param[in] VendorGuid Variable vendor GUID.
1020 @param[in] Data Data pointer.
1021 @param[in] DataSize Size of Data found. If size is less than the
1022 data, this value contains the required size.
1023 @param[in] Attributes Attribute value of the variable.
1025 @return EFI_INVALID_PARAMETER Invalid parameter.
1026 @return EFI_SECURITY_VIOLATION The variable does NOT pass the validation
1027 check carried out by the firmware.
1028 @return EFI_SUCCESS Variable pass validation successfully.
1033 IN CHAR16
*VariableName
,
1034 IN EFI_GUID
*VendorGuid
,
1037 IN UINT32 Attributes OPTIONAL
1044 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0 ||
1045 (Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) == 0) {
1047 // DB, DBX and DBT should set EFI_VARIABLE_NON_VOLATILE attribute and should be a time-based
1048 // authenticated variable.
1050 return EFI_INVALID_PARAMETER
;
1053 Status
= EFI_SUCCESS
;
1054 if (mPlatformMode
== USER_MODE
&& !(InCustomMode() && UserPhysicalPresent())) {
1056 // Time-based, verify against X509 Cert KEK.
1058 return VerifyTimeBasedPayloadAndUpdate (
1069 // If in setup mode or custom secure boot mode, no authentication needed.
1071 Payload
= (UINT8
*) Data
+ AUTHINFO2_SIZE (Data
);
1072 PayloadSize
= DataSize
- AUTHINFO2_SIZE (Data
);
1074 Status
= CheckSignatureListFormat(VariableName
, VendorGuid
, Payload
, PayloadSize
);
1075 if (EFI_ERROR (Status
)) {
1079 Status
= AuthServiceInternalUpdateVariableWithTimeStamp (
1085 &((EFI_VARIABLE_AUTHENTICATION_2
*) Data
)->TimeStamp
1087 if (EFI_ERROR (Status
)) {
1091 if (mPlatformMode
!= SETUP_MODE
) {
1092 Status
= VendorKeyIsModified ();
1100 Check if it is to delete auth variable.
1102 @param[in] OrgAttributes Original attribute value of the variable.
1103 @param[in] Data Data pointer.
1104 @param[in] DataSize Size of Data.
1105 @param[in] Attributes Attribute value of the variable.
1107 @retval TRUE It is to delete auth variable.
1108 @retval FALSE It is not to delete auth variable.
1112 IsDeleteAuthVariable (
1113 IN UINT32 OrgAttributes
,
1116 IN UINT32 Attributes
1125 // To delete a variable created with the EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
1126 // or the EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS attribute,
1127 // SetVariable must be used with attributes matching the existing variable
1128 // and the DataSize set to the size of the AuthInfo descriptor.
1130 if ((Attributes
== OrgAttributes
) &&
1131 ((Attributes
& (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
)) != 0)) {
1132 if ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) {
1133 PayloadSize
= DataSize
- AUTHINFO2_SIZE (Data
);
1134 if (PayloadSize
== 0) {
1138 PayloadSize
= DataSize
- AUTHINFO_SIZE
;
1139 if (PayloadSize
== 0) {
1149 Process variable with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS/EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set
1151 Caution: This function may receive untrusted input.
1152 This function may be invoked in SMM mode, and datasize and data are external input.
1153 This function will do basic validation, before parse the data.
1154 This function will parse the authentication carefully to avoid security issues, like
1155 buffer overflow, integer overflow.
1156 This function will check attribute carefully to avoid authentication bypass.
1158 @param[in] VariableName Name of the variable.
1159 @param[in] VendorGuid Variable vendor GUID.
1160 @param[in] Data Data pointer.
1161 @param[in] DataSize Size of Data.
1162 @param[in] Attributes Attribute value of the variable.
1164 @return EFI_INVALID_PARAMETER Invalid parameter.
1165 @return EFI_WRITE_PROTECTED Variable is write-protected and needs authentication with
1166 EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS set.
1167 @return EFI_OUT_OF_RESOURCES The Database to save the public key is full.
1168 @return EFI_SECURITY_VIOLATION The variable is with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
1169 set, but the AuthInfo does NOT pass the validation
1170 check carried out by the firmware.
1171 @return EFI_SUCCESS Variable is not write-protected or pass validation successfully.
1176 IN CHAR16
*VariableName
,
1177 IN EFI_GUID
*VendorGuid
,
1180 IN UINT32 Attributes
1185 BOOLEAN IsFirstTime
;
1187 EFI_VARIABLE_AUTHENTICATION
*CertData
;
1188 EFI_CERT_BLOCK_RSA_2048_SHA256
*CertBlock
;
1190 UINT64 MonotonicCount
;
1191 VARIABLE_ENTRY_CONSISTENCY VariableDataEntry
;
1193 AUTH_VARIABLE_INFO OrgVariableInfo
;
1200 Status
= EFI_SUCCESS
;
1202 ZeroMem (&OrgVariableInfo
, sizeof (OrgVariableInfo
));
1203 Status
= mAuthVarLibContextIn
->FindVariable (
1209 if ((!EFI_ERROR (Status
)) && IsDeleteAuthVariable (OrgVariableInfo
.Attributes
, Data
, DataSize
, Attributes
) && UserPhysicalPresent()) {
1211 // Allow the delete operation of common authenticated variable at user physical presence.
1213 Status
= AuthServiceInternalUpdateVariable (
1220 if (!EFI_ERROR (Status
) && ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0)) {
1221 Status
= DeleteCertsFromDb (VariableName
, VendorGuid
, Attributes
);
1227 if (NeedPhysicallyPresent (VariableName
, VendorGuid
) && !UserPhysicalPresent()) {
1229 // This variable is protected, only physical present user could modify its value.
1231 return EFI_SECURITY_VIOLATION
;
1235 // A time-based authenticated variable and a count-based authenticated variable
1236 // can't be updated by each other.
1238 if (OrgVariableInfo
.Data
!= NULL
) {
1239 if (((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) &&
1240 ((OrgVariableInfo
.Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0)) {
1241 return EFI_SECURITY_VIOLATION
;
1244 if (((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) &&
1245 ((OrgVariableInfo
.Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0)) {
1246 return EFI_SECURITY_VIOLATION
;
1251 // Process Time-based Authenticated variable.
1253 if ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) {
1254 return VerifyTimeBasedPayloadAndUpdate (
1266 // Determine if first time SetVariable with the EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS.
1268 if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) {
1270 // Determine current operation type.
1272 if (DataSize
== AUTHINFO_SIZE
) {
1276 // Determine whether this is the first time with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS set.
1278 if (OrgVariableInfo
.Data
== NULL
) {
1280 } else if ((OrgVariableInfo
.Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) == 0) {
1283 KeyIndex
= OrgVariableInfo
.PubKeyIndex
;
1284 IsFirstTime
= FALSE
;
1286 } else if ((OrgVariableInfo
.Data
!= NULL
) &&
1287 ((OrgVariableInfo
.Attributes
& (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
)) != 0)
1290 // If the variable is already write-protected, it always needs authentication before update.
1292 return EFI_WRITE_PROTECTED
;
1295 // If without EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS, set and attributes collision.
1296 // That means it is not authenticated variable, just update variable as usual.
1298 Status
= AuthServiceInternalUpdateVariable (VariableName
, VendorGuid
, Data
, DataSize
, Attributes
);
1303 // Get PubKey and check Monotonic Count value corresponding to the variable.
1305 CertData
= (EFI_VARIABLE_AUTHENTICATION
*) Data
;
1306 CertBlock
= (EFI_CERT_BLOCK_RSA_2048_SHA256
*) (CertData
->AuthInfo
.CertData
);
1307 PubKey
= CertBlock
->PublicKey
;
1310 // Update Monotonic Count value.
1312 MonotonicCount
= CertData
->MonotonicCount
;
1316 // 2 cases need to check here
1317 // 1. Internal PubKey variable. PubKeyIndex is always 0
1318 // 2. Other counter-based AuthVariable. Check input PubKey.
1320 if (KeyIndex
== 0) {
1321 return EFI_SECURITY_VIOLATION
;
1323 for (Index
= 0; Index
< mPubKeyNumber
; Index
++) {
1324 if (ReadUnaligned32 (&(((AUTHVAR_KEY_DB_DATA
*) mPubKeyStore
+ Index
)->KeyIndex
)) == KeyIndex
) {
1325 if (CompareMem (((AUTHVAR_KEY_DB_DATA
*) mPubKeyStore
+ Index
)->KeyData
, PubKey
, EFI_CERT_TYPE_RSA2048_SIZE
) == 0) {
1328 return EFI_SECURITY_VIOLATION
;
1332 if (Index
== mPubKeyNumber
) {
1333 return EFI_SECURITY_VIOLATION
;
1337 // Compare the current monotonic count and ensure that it is greater than the last SetVariable
1338 // operation with the EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS attribute set.
1340 if (MonotonicCount
<= OrgVariableInfo
.MonotonicCount
) {
1342 // Monotonic count check fail, suspicious replay attack, return EFI_SECURITY_VIOLATION.
1344 return EFI_SECURITY_VIOLATION
;
1348 // Verify the certificate in Data payload.
1350 Status
= VerifyCounterBasedPayload (Data
, DataSize
, PubKey
);
1351 if (EFI_ERROR (Status
)) {
1356 // Now, the signature has been verified!
1358 if (IsFirstTime
&& !IsDeletion
) {
1359 VariableDataEntry
.VariableSize
= DataSize
- AUTHINFO_SIZE
;
1360 VariableDataEntry
.Guid
= VendorGuid
;
1361 VariableDataEntry
.Name
= VariableName
;
1364 // Update public key database variable if need.
1366 KeyIndex
= AddPubKeyInStore (PubKey
, &VariableDataEntry
);
1367 if (KeyIndex
== 0) {
1368 return EFI_OUT_OF_RESOURCES
;
1373 // Verification pass.
1375 return AuthServiceInternalUpdateVariableWithMonotonicCount (VariableName
, VendorGuid
, (UINT8
*)Data
+ AUTHINFO_SIZE
, DataSize
- AUTHINFO_SIZE
, Attributes
, KeyIndex
, MonotonicCount
);
1379 Filter out the duplicated EFI_SIGNATURE_DATA from the new data by comparing to the original data.
1381 @param[in] Data Pointer to original EFI_SIGNATURE_LIST.
1382 @param[in] DataSize Size of Data buffer.
1383 @param[in, out] NewData Pointer to new EFI_SIGNATURE_LIST.
1384 @param[in, out] NewDataSize Size of NewData buffer.
1388 FilterSignatureList (
1391 IN OUT VOID
*NewData
,
1392 IN OUT UINTN
*NewDataSize
1395 EFI_SIGNATURE_LIST
*CertList
;
1396 EFI_SIGNATURE_DATA
*Cert
;
1398 EFI_SIGNATURE_LIST
*NewCertList
;
1399 EFI_SIGNATURE_DATA
*NewCert
;
1406 UINTN SignatureListSize
;
1412 if (*NewDataSize
== 0) {
1416 TempDataSize
= *NewDataSize
;
1417 Status
= mAuthVarLibContextIn
->GetScratchBuffer (&TempDataSize
, (VOID
**) &TempData
);
1418 if (EFI_ERROR (Status
)) {
1419 return EFI_OUT_OF_RESOURCES
;
1424 NewCertList
= (EFI_SIGNATURE_LIST
*) NewData
;
1425 while ((*NewDataSize
> 0) && (*NewDataSize
>= NewCertList
->SignatureListSize
)) {
1426 NewCert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) NewCertList
+ sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
);
1427 NewCertCount
= (NewCertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - NewCertList
->SignatureHeaderSize
) / NewCertList
->SignatureSize
;
1430 for (Index
= 0; Index
< NewCertCount
; Index
++) {
1434 CertList
= (EFI_SIGNATURE_LIST
*) Data
;
1435 while ((Size
> 0) && (Size
>= CertList
->SignatureListSize
)) {
1436 if (CompareGuid (&CertList
->SignatureType
, &NewCertList
->SignatureType
) &&
1437 (CertList
->SignatureSize
== NewCertList
->SignatureSize
)) {
1438 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
1439 CertCount
= (CertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - CertList
->SignatureHeaderSize
) / CertList
->SignatureSize
;
1440 for (Index2
= 0; Index2
< CertCount
; Index2
++) {
1442 // Iterate each Signature Data in this Signature List.
1444 if (CompareMem (NewCert
, Cert
, CertList
->SignatureSize
) == 0) {
1448 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) Cert
+ CertList
->SignatureSize
);
1455 Size
-= CertList
->SignatureListSize
;
1456 CertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) CertList
+ CertList
->SignatureListSize
);
1461 // New EFI_SIGNATURE_DATA, keep it.
1463 if (CopiedCount
== 0) {
1465 // Copy EFI_SIGNATURE_LIST header for only once.
1467 CopyMem (Tail
, NewCertList
, sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
);
1468 Tail
= Tail
+ sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
;
1471 CopyMem (Tail
, NewCert
, NewCertList
->SignatureSize
);
1472 Tail
+= NewCertList
->SignatureSize
;
1476 NewCert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) NewCert
+ NewCertList
->SignatureSize
);
1480 // Update SignatureListSize in the kept EFI_SIGNATURE_LIST.
1482 if (CopiedCount
!= 0) {
1483 SignatureListSize
= sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
+ (CopiedCount
* NewCertList
->SignatureSize
);
1484 CertList
= (EFI_SIGNATURE_LIST
*) (Tail
- SignatureListSize
);
1485 CertList
->SignatureListSize
= (UINT32
) SignatureListSize
;
1488 *NewDataSize
-= NewCertList
->SignatureListSize
;
1489 NewCertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) NewCertList
+ NewCertList
->SignatureListSize
);
1492 TempDataSize
= (Tail
- (UINT8
*) TempData
);
1494 CopyMem (NewData
, TempData
, TempDataSize
);
1495 *NewDataSize
= TempDataSize
;
1501 Compare two EFI_TIME data.
1504 @param FirstTime A pointer to the first EFI_TIME data.
1505 @param SecondTime A pointer to the second EFI_TIME data.
1507 @retval TRUE The FirstTime is not later than the SecondTime.
1508 @retval FALSE The FirstTime is later than the SecondTime.
1512 AuthServiceInternalCompareTimeStamp (
1513 IN EFI_TIME
*FirstTime
,
1514 IN EFI_TIME
*SecondTime
1517 if (FirstTime
->Year
!= SecondTime
->Year
) {
1518 return (BOOLEAN
) (FirstTime
->Year
< SecondTime
->Year
);
1519 } else if (FirstTime
->Month
!= SecondTime
->Month
) {
1520 return (BOOLEAN
) (FirstTime
->Month
< SecondTime
->Month
);
1521 } else if (FirstTime
->Day
!= SecondTime
->Day
) {
1522 return (BOOLEAN
) (FirstTime
->Day
< SecondTime
->Day
);
1523 } else if (FirstTime
->Hour
!= SecondTime
->Hour
) {
1524 return (BOOLEAN
) (FirstTime
->Hour
< SecondTime
->Hour
);
1525 } else if (FirstTime
->Minute
!= SecondTime
->Minute
) {
1526 return (BOOLEAN
) (FirstTime
->Minute
< SecondTime
->Minute
);
1529 return (BOOLEAN
) (FirstTime
->Second
<= SecondTime
->Second
);
1533 Find matching signer's certificates for common authenticated variable
1534 by corresponding VariableName and VendorGuid from "certdb" or "certdbv".
1536 The data format of "certdb" or "certdbv":
1538 // UINT32 CertDbListSize;
1539 // /// AUTH_CERT_DB_DATA Certs1[];
1540 // /// AUTH_CERT_DB_DATA Certs2[];
1542 // /// AUTH_CERT_DB_DATA Certsn[];
1545 @param[in] VariableName Name of authenticated Variable.
1546 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1547 @param[in] Data Pointer to variable "certdb" or "certdbv".
1548 @param[in] DataSize Size of variable "certdb" or "certdbv".
1549 @param[out] CertOffset Offset of matching CertData, from starting of Data.
1550 @param[out] CertDataSize Length of CertData in bytes.
1551 @param[out] CertNodeOffset Offset of matching AUTH_CERT_DB_DATA , from
1553 @param[out] CertNodeSize Length of AUTH_CERT_DB_DATA in bytes.
1555 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1556 @retval EFI_NOT_FOUND Fail to find matching certs.
1557 @retval EFI_SUCCESS Find matching certs and output parameters.
1562 IN CHAR16
*VariableName
,
1563 IN EFI_GUID
*VendorGuid
,
1566 OUT UINT32
*CertOffset
, OPTIONAL
1567 OUT UINT32
*CertDataSize
, OPTIONAL
1568 OUT UINT32
*CertNodeOffset
,OPTIONAL
1569 OUT UINT32
*CertNodeSize OPTIONAL
1573 AUTH_CERT_DB_DATA
*Ptr
;
1577 UINT32 CertDbListSize
;
1579 if ((VariableName
== NULL
) || (VendorGuid
== NULL
) || (Data
== NULL
)) {
1580 return EFI_INVALID_PARAMETER
;
1584 // Check whether DataSize matches recorded CertDbListSize.
1586 if (DataSize
< sizeof (UINT32
)) {
1587 return EFI_INVALID_PARAMETER
;
1590 CertDbListSize
= ReadUnaligned32 ((UINT32
*) Data
);
1592 if (CertDbListSize
!= (UINT32
) DataSize
) {
1593 return EFI_INVALID_PARAMETER
;
1596 Offset
= sizeof (UINT32
);
1599 // Get corresponding certificates by VendorGuid and VariableName.
1601 while (Offset
< (UINT32
) DataSize
) {
1602 Ptr
= (AUTH_CERT_DB_DATA
*) (Data
+ Offset
);
1604 // Check whether VendorGuid matches.
1606 if (CompareGuid (&Ptr
->VendorGuid
, VendorGuid
)) {
1607 NodeSize
= ReadUnaligned32 (&Ptr
->CertNodeSize
);
1608 NameSize
= ReadUnaligned32 (&Ptr
->NameSize
);
1609 CertSize
= ReadUnaligned32 (&Ptr
->CertDataSize
);
1611 if (NodeSize
!= sizeof (EFI_GUID
) + sizeof (UINT32
) * 3 + CertSize
+
1612 sizeof (CHAR16
) * NameSize
) {
1613 return EFI_INVALID_PARAMETER
;
1616 Offset
= Offset
+ sizeof (EFI_GUID
) + sizeof (UINT32
) * 3;
1618 // Check whether VariableName matches.
1620 if ((NameSize
== StrLen (VariableName
)) &&
1621 (CompareMem (Data
+ Offset
, VariableName
, NameSize
* sizeof (CHAR16
)) == 0)) {
1622 Offset
= Offset
+ NameSize
* sizeof (CHAR16
);
1624 if (CertOffset
!= NULL
) {
1625 *CertOffset
= Offset
;
1628 if (CertDataSize
!= NULL
) {
1629 *CertDataSize
= CertSize
;
1632 if (CertNodeOffset
!= NULL
) {
1633 *CertNodeOffset
= (UINT32
) ((UINT8
*) Ptr
- Data
);
1636 if (CertNodeSize
!= NULL
) {
1637 *CertNodeSize
= NodeSize
;
1642 Offset
= Offset
+ NameSize
* sizeof (CHAR16
) + CertSize
;
1645 NodeSize
= ReadUnaligned32 (&Ptr
->CertNodeSize
);
1646 Offset
= Offset
+ NodeSize
;
1650 return EFI_NOT_FOUND
;
1654 Retrieve signer's certificates for common authenticated variable
1655 by corresponding VariableName and VendorGuid from "certdb"
1656 or "certdbv" according to authenticated variable attributes.
1658 @param[in] VariableName Name of authenticated Variable.
1659 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1660 @param[in] Attributes Attributes of authenticated variable.
1661 @param[out] CertData Pointer to signer's certificates.
1662 @param[out] CertDataSize Length of CertData in bytes.
1664 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1665 @retval EFI_NOT_FOUND Fail to find "certdb"/"certdbv" or matching certs.
1666 @retval EFI_SUCCESS Get signer's certificates successfully.
1671 IN CHAR16
*VariableName
,
1672 IN EFI_GUID
*VendorGuid
,
1673 IN UINT32 Attributes
,
1674 OUT UINT8
**CertData
,
1675 OUT UINT32
*CertDataSize
1684 if ((VariableName
== NULL
) || (VendorGuid
== NULL
) || (CertData
== NULL
) || (CertDataSize
== NULL
)) {
1685 return EFI_INVALID_PARAMETER
;
1689 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
1691 // Get variable "certdb".
1693 DbName
= EFI_CERT_DB_NAME
;
1696 // Get variable "certdbv".
1698 DbName
= EFI_CERT_DB_VOLATILE_NAME
;
1702 // Get variable "certdb" or "certdbv".
1704 Status
= AuthServiceInternalFindVariable (
1710 if (EFI_ERROR (Status
)) {
1714 if ((DataSize
== 0) || (Data
== NULL
)) {
1716 return EFI_NOT_FOUND
;
1719 Status
= FindCertsFromDb (
1730 if (EFI_ERROR (Status
)) {
1734 *CertData
= Data
+ CertOffset
;
1739 Delete matching signer's certificates when deleting common authenticated
1740 variable by corresponding VariableName and VendorGuid from "certdb" or
1741 "certdbv" according to authenticated variable attributes.
1743 @param[in] VariableName Name of authenticated Variable.
1744 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1745 @param[in] Attributes Attributes of authenticated variable.
1747 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1748 @retval EFI_NOT_FOUND Fail to find "certdb"/"certdbv" or matching certs.
1749 @retval EFI_OUT_OF_RESOURCES The operation is failed due to lack of resources.
1750 @retval EFI_SUCCESS The operation is completed successfully.
1755 IN CHAR16
*VariableName
,
1756 IN EFI_GUID
*VendorGuid
,
1757 IN UINT32 Attributes
1764 UINT32 CertNodeOffset
;
1765 UINT32 CertNodeSize
;
1767 UINT32 NewCertDbSize
;
1770 if ((VariableName
== NULL
) || (VendorGuid
== NULL
)) {
1771 return EFI_INVALID_PARAMETER
;
1774 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
1776 // Get variable "certdb".
1778 DbName
= EFI_CERT_DB_NAME
;
1779 VarAttr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
1782 // Get variable "certdbv".
1784 DbName
= EFI_CERT_DB_VOLATILE_NAME
;
1785 VarAttr
= EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
1788 Status
= AuthServiceInternalFindVariable (
1795 if (EFI_ERROR (Status
)) {
1799 if ((DataSize
== 0) || (Data
== NULL
)) {
1801 return EFI_NOT_FOUND
;
1804 if (DataSize
== sizeof (UINT32
)) {
1806 // There is no certs in "certdb" or "certdbv".
1812 // Get corresponding cert node from "certdb" or "certdbv".
1814 Status
= FindCertsFromDb (
1825 if (EFI_ERROR (Status
)) {
1829 if (DataSize
< (CertNodeOffset
+ CertNodeSize
)) {
1830 return EFI_NOT_FOUND
;
1834 // Construct new data content of variable "certdb" or "certdbv".
1836 NewCertDbSize
= (UINT32
) DataSize
- CertNodeSize
;
1837 NewCertDb
= (UINT8
*) mCertDbStore
;
1840 // Copy the DB entries before deleting node.
1842 CopyMem (NewCertDb
, Data
, CertNodeOffset
);
1844 // Update CertDbListSize.
1846 CopyMem (NewCertDb
, &NewCertDbSize
, sizeof (UINT32
));
1848 // Copy the DB entries after deleting node.
1850 if (DataSize
> (CertNodeOffset
+ CertNodeSize
)) {
1852 NewCertDb
+ CertNodeOffset
,
1853 Data
+ CertNodeOffset
+ CertNodeSize
,
1854 DataSize
- CertNodeOffset
- CertNodeSize
1859 // Set "certdb" or "certdbv".
1861 Status
= AuthServiceInternalUpdateVariable (
1873 Insert signer's certificates for common authenticated variable with VariableName
1874 and VendorGuid in AUTH_CERT_DB_DATA to "certdb" or "certdbv" according to
1875 time based authenticated variable attributes.
1877 @param[in] VariableName Name of authenticated Variable.
1878 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1879 @param[in] Attributes Attributes of authenticated variable.
1880 @param[in] CertData Pointer to signer's certificates.
1881 @param[in] CertDataSize Length of CertData in bytes.
1883 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1884 @retval EFI_ACCESS_DENIED An AUTH_CERT_DB_DATA entry with same VariableName
1885 and VendorGuid already exists.
1886 @retval EFI_OUT_OF_RESOURCES The operation is failed due to lack of resources.
1887 @retval EFI_SUCCESS Insert an AUTH_CERT_DB_DATA entry to "certdb" or "certdbv"
1892 IN CHAR16
*VariableName
,
1893 IN EFI_GUID
*VendorGuid
,
1894 IN UINT32 Attributes
,
1896 IN UINTN CertDataSize
1904 UINT32 NewCertDbSize
;
1905 UINT32 CertNodeSize
;
1907 AUTH_CERT_DB_DATA
*Ptr
;
1910 if ((VariableName
== NULL
) || (VendorGuid
== NULL
) || (CertData
== NULL
)) {
1911 return EFI_INVALID_PARAMETER
;
1914 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
1916 // Get variable "certdb".
1918 DbName
= EFI_CERT_DB_NAME
;
1919 VarAttr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
1922 // Get variable "certdbv".
1924 DbName
= EFI_CERT_DB_VOLATILE_NAME
;
1925 VarAttr
= EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
1929 // Get variable "certdb" or "certdbv".
1931 Status
= AuthServiceInternalFindVariable (
1937 if (EFI_ERROR (Status
)) {
1941 if ((DataSize
== 0) || (Data
== NULL
)) {
1943 return EFI_NOT_FOUND
;
1947 // Find whether matching cert node already exists in "certdb" or "certdbv".
1948 // If yes return error.
1950 Status
= FindCertsFromDb (
1961 if (!EFI_ERROR (Status
)) {
1963 return EFI_ACCESS_DENIED
;
1967 // Construct new data content of variable "certdb" or "certdbv".
1969 NameSize
= (UINT32
) StrLen (VariableName
);
1970 CertNodeSize
= sizeof (AUTH_CERT_DB_DATA
) + (UINT32
) CertDataSize
+ NameSize
* sizeof (CHAR16
);
1971 NewCertDbSize
= (UINT32
) DataSize
+ CertNodeSize
;
1972 if (NewCertDbSize
> mMaxCertDbSize
) {
1973 return EFI_OUT_OF_RESOURCES
;
1975 NewCertDb
= (UINT8
*) mCertDbStore
;
1978 // Copy the DB entries before inserting node.
1980 CopyMem (NewCertDb
, Data
, DataSize
);
1982 // Update CertDbListSize.
1984 CopyMem (NewCertDb
, &NewCertDbSize
, sizeof (UINT32
));
1986 // Construct new cert node.
1988 Ptr
= (AUTH_CERT_DB_DATA
*) (NewCertDb
+ DataSize
);
1989 CopyGuid (&Ptr
->VendorGuid
, VendorGuid
);
1990 CopyMem (&Ptr
->CertNodeSize
, &CertNodeSize
, sizeof (UINT32
));
1991 CopyMem (&Ptr
->NameSize
, &NameSize
, sizeof (UINT32
));
1992 CopyMem (&Ptr
->CertDataSize
, &CertDataSize
, sizeof (UINT32
));
1995 (UINT8
*) Ptr
+ sizeof (AUTH_CERT_DB_DATA
),
1997 NameSize
* sizeof (CHAR16
)
2001 (UINT8
*) Ptr
+ sizeof (AUTH_CERT_DB_DATA
) + NameSize
* sizeof (CHAR16
),
2007 // Set "certdb" or "certdbv".
2009 Status
= AuthServiceInternalUpdateVariable (
2021 Clean up signer's certificates for common authenticated variable
2022 by corresponding VariableName and VendorGuid from "certdb".
2023 System may break down during Timebased Variable update & certdb update,
2024 make them inconsistent, this function is called in AuthVariable Init
2025 to ensure consistency.
2027 @retval EFI_NOT_FOUND Fail to find variable "certdb".
2028 @retval EFI_OUT_OF_RESOURCES The operation is failed due to lack of resources.
2029 @retval EFI_SUCCESS The operation is completed successfully.
2038 AUTH_CERT_DB_DATA
*Ptr
;
2041 CHAR16
*VariableName
;
2043 BOOLEAN CertCleaned
;
2046 EFI_GUID AuthVarGuid
;
2047 AUTH_VARIABLE_INFO AuthVariableInfo
;
2049 Status
= EFI_SUCCESS
;
2052 // Get corresponding certificates by VendorGuid and VariableName.
2055 CertCleaned
= FALSE
;
2058 // Get latest variable "certdb"
2060 Status
= AuthServiceInternalFindVariable (
2066 if (EFI_ERROR (Status
)) {
2070 if ((DataSize
== 0) || (Data
== NULL
)) {
2072 return EFI_NOT_FOUND
;
2075 Offset
= sizeof (UINT32
);
2077 while (Offset
< (UINT32
) DataSize
) {
2078 Ptr
= (AUTH_CERT_DB_DATA
*) (Data
+ Offset
);
2079 NodeSize
= ReadUnaligned32 (&Ptr
->CertNodeSize
);
2080 NameSize
= ReadUnaligned32 (&Ptr
->NameSize
);
2083 // Get VarName tailed with '\0'
2085 VariableName
= AllocateZeroPool((NameSize
+ 1) * sizeof(CHAR16
));
2086 if (VariableName
== NULL
) {
2087 return EFI_OUT_OF_RESOURCES
;
2089 CopyMem (VariableName
, (UINT8
*) Ptr
+ sizeof (AUTH_CERT_DB_DATA
), NameSize
* sizeof(CHAR16
));
2091 // Keep VarGuid aligned
2093 CopyMem (&AuthVarGuid
, &Ptr
->VendorGuid
, sizeof(EFI_GUID
));
2096 // Find corresponding time auth variable
2098 ZeroMem (&AuthVariableInfo
, sizeof (AuthVariableInfo
));
2099 Status
= mAuthVarLibContextIn
->FindVariable (
2105 if (EFI_ERROR(Status
) || (AuthVariableInfo
.Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) == 0) {
2106 Status
= DeleteCertsFromDb(
2109 AuthVariableInfo
.Attributes
2112 DEBUG((EFI_D_INFO
, "Recovery!! Cert for Auth Variable %s Guid %g is removed for consistency\n", VariableName
, &AuthVarGuid
));
2113 FreePool(VariableName
);
2117 FreePool(VariableName
);
2118 Offset
= Offset
+ NodeSize
;
2120 } while (CertCleaned
);
2126 Process variable with EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set
2128 Caution: This function may receive untrusted input.
2129 This function may be invoked in SMM mode, and datasize and data are external input.
2130 This function will do basic validation, before parse the data.
2131 This function will parse the authentication carefully to avoid security issues, like
2132 buffer overflow, integer overflow.
2134 @param[in] VariableName Name of Variable to be found.
2135 @param[in] VendorGuid Variable vendor GUID.
2136 @param[in] Data Data pointer.
2137 @param[in] DataSize Size of Data found. If size is less than the
2138 data, this value contains the required size.
2139 @param[in] Attributes Attribute value of the variable.
2140 @param[in] AuthVarType Verify against PK, KEK database, private database or certificate in data payload.
2141 @param[in] OrgTimeStamp Pointer to original time stamp,
2142 original variable is not found if NULL.
2143 @param[out] VarPayloadPtr Pointer to variable payload address.
2144 @param[out] VarPayloadSize Pointer to variable payload size.
2146 @retval EFI_INVALID_PARAMETER Invalid parameter.
2147 @retval EFI_SECURITY_VIOLATION The variable does NOT pass the validation
2148 check carried out by the firmware.
2149 @retval EFI_OUT_OF_RESOURCES Failed to process variable due to lack
2151 @retval EFI_SUCCESS Variable pass validation successfully.
2155 VerifyTimeBasedPayload (
2156 IN CHAR16
*VariableName
,
2157 IN EFI_GUID
*VendorGuid
,
2160 IN UINT32 Attributes
,
2161 IN AUTHVAR_TYPE AuthVarType
,
2162 IN EFI_TIME
*OrgTimeStamp
,
2163 OUT UINT8
**VarPayloadPtr
,
2164 OUT UINTN
*VarPayloadSize
2167 EFI_VARIABLE_AUTHENTICATION_2
*CertData
;
2173 BOOLEAN VerifyStatus
;
2175 EFI_SIGNATURE_LIST
*CertList
;
2176 EFI_SIGNATURE_DATA
*Cert
;
2187 UINTN CertStackSize
;
2188 UINT8
*CertsInCertDb
;
2189 UINT32 CertsSizeinDb
;
2191 VerifyStatus
= FALSE
;
2197 CertsInCertDb
= NULL
;
2200 // When the attribute EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS is
2201 // set, then the Data buffer shall begin with an instance of a complete (and serialized)
2202 // EFI_VARIABLE_AUTHENTICATION_2 descriptor. The descriptor shall be followed by the new
2203 // variable value and DataSize shall reflect the combined size of the descriptor and the new
2204 // variable value. The authentication descriptor is not part of the variable data and is not
2205 // returned by subsequent calls to GetVariable().
2207 CertData
= (EFI_VARIABLE_AUTHENTICATION_2
*) Data
;
2210 // Verify that Pad1, Nanosecond, TimeZone, Daylight and Pad2 components of the
2211 // TimeStamp value are set to zero.
2213 if ((CertData
->TimeStamp
.Pad1
!= 0) ||
2214 (CertData
->TimeStamp
.Nanosecond
!= 0) ||
2215 (CertData
->TimeStamp
.TimeZone
!= 0) ||
2216 (CertData
->TimeStamp
.Daylight
!= 0) ||
2217 (CertData
->TimeStamp
.Pad2
!= 0)) {
2218 return EFI_SECURITY_VIOLATION
;
2221 if ((OrgTimeStamp
!= NULL
) && ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0)) {
2222 if (AuthServiceInternalCompareTimeStamp (&CertData
->TimeStamp
, OrgTimeStamp
)) {
2224 // TimeStamp check fail, suspicious replay attack, return EFI_SECURITY_VIOLATION.
2226 return EFI_SECURITY_VIOLATION
;
2231 // wCertificateType should be WIN_CERT_TYPE_EFI_GUID.
2232 // Cert type should be EFI_CERT_TYPE_PKCS7_GUID.
2234 if ((CertData
->AuthInfo
.Hdr
.wCertificateType
!= WIN_CERT_TYPE_EFI_GUID
) ||
2235 !CompareGuid (&CertData
->AuthInfo
.CertType
, &gEfiCertPkcs7Guid
)) {
2237 // Invalid AuthInfo type, return EFI_SECURITY_VIOLATION.
2239 return EFI_SECURITY_VIOLATION
;
2243 // Find out Pkcs7 SignedData which follows the EFI_VARIABLE_AUTHENTICATION_2 descriptor.
2244 // AuthInfo.Hdr.dwLength is the length of the entire certificate, including the length of the header.
2246 SigData
= CertData
->AuthInfo
.CertData
;
2247 SigDataSize
= CertData
->AuthInfo
.Hdr
.dwLength
- (UINT32
) (OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID
, CertData
));
2250 // SignedData.digestAlgorithms shall contain the digest algorithm used when preparing the
2251 // signature. Only a digest algorithm of SHA-256 is accepted.
2253 // According to PKCS#7 Definition:
2254 // SignedData ::= SEQUENCE {
2256 // digestAlgorithms DigestAlgorithmIdentifiers,
2257 // contentInfo ContentInfo,
2259 // The DigestAlgorithmIdentifiers can be used to determine the hash algorithm
2260 // in VARIABLE_AUTHENTICATION_2 descriptor.
2261 // This field has the fixed offset (+13) and be calculated based on two bytes of length encoding.
2263 if ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) {
2264 if (SigDataSize
>= (13 + sizeof (mSha256OidValue
))) {
2265 if (((*(SigData
+ 1) & TWO_BYTE_ENCODE
) != TWO_BYTE_ENCODE
) ||
2266 (CompareMem (SigData
+ 13, &mSha256OidValue
, sizeof (mSha256OidValue
)) != 0)) {
2267 return EFI_SECURITY_VIOLATION
;
2273 // Find out the new data payload which follows Pkcs7 SignedData directly.
2275 PayloadPtr
= SigData
+ SigDataSize
;
2276 PayloadSize
= DataSize
- OFFSET_OF_AUTHINFO2_CERT_DATA
- (UINTN
) SigDataSize
;
2279 // Construct a serialization buffer of the values of the VariableName, VendorGuid and Attributes
2280 // parameters of the SetVariable() call and the TimeStamp component of the
2281 // EFI_VARIABLE_AUTHENTICATION_2 descriptor followed by the variable's new value
2282 // i.e. (VariableName, VendorGuid, Attributes, TimeStamp, Data)
2284 NewDataSize
= PayloadSize
+ sizeof (EFI_TIME
) + sizeof (UINT32
) +
2285 sizeof (EFI_GUID
) + StrSize (VariableName
) - sizeof (CHAR16
);
2288 // Here is to reuse scratch data area(at the end of volatile variable store)
2289 // to reduce SMRAM consumption for SMM variable driver.
2290 // The scratch buffer is enough to hold the serialized data and safe to use,
2291 // because it is only used at here to do verification temporarily first
2292 // and then used in UpdateVariable() for a time based auth variable set.
2294 Status
= mAuthVarLibContextIn
->GetScratchBuffer (&NewDataSize
, (VOID
**) &NewData
);
2295 if (EFI_ERROR (Status
)) {
2296 return EFI_OUT_OF_RESOURCES
;
2300 Length
= StrLen (VariableName
) * sizeof (CHAR16
);
2301 CopyMem (Buffer
, VariableName
, Length
);
2304 Length
= sizeof (EFI_GUID
);
2305 CopyMem (Buffer
, VendorGuid
, Length
);
2308 Length
= sizeof (UINT32
);
2309 CopyMem (Buffer
, &Attr
, Length
);
2312 Length
= sizeof (EFI_TIME
);
2313 CopyMem (Buffer
, &CertData
->TimeStamp
, Length
);
2316 CopyMem (Buffer
, PayloadPtr
, PayloadSize
);
2318 if (AuthVarType
== AuthVarTypePk
) {
2320 // Verify that the signature has been made with the current Platform Key (no chaining for PK).
2321 // First, get signer's certificates from SignedData.
2323 VerifyStatus
= Pkcs7GetSigners (
2331 if (!VerifyStatus
) {
2336 // Second, get the current platform key from variable. Check whether it's identical with signer's certificates
2337 // in SignedData. If not, return error immediately.
2339 Status
= AuthServiceInternalFindVariable (
2340 EFI_PLATFORM_KEY_NAME
,
2341 &gEfiGlobalVariableGuid
,
2345 if (EFI_ERROR (Status
)) {
2346 VerifyStatus
= FALSE
;
2349 CertList
= (EFI_SIGNATURE_LIST
*) Data
;
2350 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
2351 if ((RootCertSize
!= (CertList
->SignatureSize
- (sizeof (EFI_SIGNATURE_DATA
) - 1))) ||
2352 (CompareMem (Cert
->SignatureData
, RootCert
, RootCertSize
) != 0)) {
2353 VerifyStatus
= FALSE
;
2358 // Verify Pkcs7 SignedData via Pkcs7Verify library.
2360 VerifyStatus
= Pkcs7Verify (
2369 } else if (AuthVarType
== AuthVarTypeKek
) {
2372 // Get KEK database from variable.
2374 Status
= AuthServiceInternalFindVariable (
2375 EFI_KEY_EXCHANGE_KEY_NAME
,
2376 &gEfiGlobalVariableGuid
,
2380 if (EFI_ERROR (Status
)) {
2385 // Ready to verify Pkcs7 SignedData. Go through KEK Signature Database to find out X.509 CertList.
2387 KekDataSize
= (UINT32
) DataSize
;
2388 CertList
= (EFI_SIGNATURE_LIST
*) Data
;
2389 while ((KekDataSize
> 0) && (KekDataSize
>= CertList
->SignatureListSize
)) {
2390 if (CompareGuid (&CertList
->SignatureType
, &gEfiCertX509Guid
)) {
2391 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
2392 CertCount
= (CertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - CertList
->SignatureHeaderSize
) / CertList
->SignatureSize
;
2393 for (Index
= 0; Index
< CertCount
; Index
++) {
2395 // Iterate each Signature Data Node within this CertList for a verify
2397 RootCert
= Cert
->SignatureData
;
2398 RootCertSize
= CertList
->SignatureSize
- (sizeof (EFI_SIGNATURE_DATA
) - 1);
2401 // Verify Pkcs7 SignedData via Pkcs7Verify library.
2403 VerifyStatus
= Pkcs7Verify (
2414 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) Cert
+ CertList
->SignatureSize
);
2417 KekDataSize
-= CertList
->SignatureListSize
;
2418 CertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) CertList
+ CertList
->SignatureListSize
);
2420 } else if (AuthVarType
== AuthVarTypePriv
) {
2423 // Process common authenticated variable except PK/KEK/DB/DBX/DBT.
2424 // Get signer's certificates from SignedData.
2426 VerifyStatus
= Pkcs7GetSigners (
2434 if (!VerifyStatus
) {
2439 // Get previously stored signer's certificates from certdb or certdbv for existing
2440 // variable. Check whether they are identical with signer's certificates
2441 // in SignedData. If not, return error immediately.
2443 if (OrgTimeStamp
!= NULL
) {
2444 VerifyStatus
= FALSE
;
2446 Status
= GetCertsFromDb (VariableName
, VendorGuid
, Attributes
, &CertsInCertDb
, &CertsSizeinDb
);
2447 if (EFI_ERROR (Status
)) {
2451 if ((CertStackSize
!= CertsSizeinDb
) ||
2452 (CompareMem (SignerCerts
, CertsInCertDb
, CertsSizeinDb
) != 0)) {
2457 VerifyStatus
= Pkcs7Verify (
2465 if (!VerifyStatus
) {
2469 if ((OrgTimeStamp
== NULL
) && (PayloadSize
!= 0)) {
2471 // Insert signer's certificates when adding a new common authenticated variable.
2473 Status
= InsertCertsToDb (VariableName
, VendorGuid
, Attributes
, SignerCerts
, CertStackSize
);
2474 if (EFI_ERROR (Status
)) {
2475 VerifyStatus
= FALSE
;
2479 } else if (AuthVarType
== AuthVarTypePayload
) {
2480 CertList
= (EFI_SIGNATURE_LIST
*) PayloadPtr
;
2481 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
2482 RootCert
= Cert
->SignatureData
;
2483 RootCertSize
= CertList
->SignatureSize
- (sizeof (EFI_SIGNATURE_DATA
) - 1);
2485 // Verify Pkcs7 SignedData via Pkcs7Verify library.
2487 VerifyStatus
= Pkcs7Verify (
2496 return EFI_SECURITY_VIOLATION
;
2501 if (AuthVarType
== AuthVarTypePk
|| AuthVarType
== AuthVarTypePriv
) {
2502 Pkcs7FreeSigners (RootCert
);
2503 Pkcs7FreeSigners (SignerCerts
);
2506 if (!VerifyStatus
) {
2507 return EFI_SECURITY_VIOLATION
;
2510 Status
= CheckSignatureListFormat(VariableName
, VendorGuid
, PayloadPtr
, PayloadSize
);
2511 if (EFI_ERROR (Status
)) {
2515 *VarPayloadPtr
= PayloadPtr
;
2516 *VarPayloadSize
= PayloadSize
;
2522 Process variable with EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set
2524 Caution: This function may receive untrusted input.
2525 This function may be invoked in SMM mode, and datasize and data are external input.
2526 This function will do basic validation, before parse the data.
2527 This function will parse the authentication carefully to avoid security issues, like
2528 buffer overflow, integer overflow.
2530 @param[in] VariableName Name of Variable to be found.
2531 @param[in] VendorGuid Variable vendor GUID.
2532 @param[in] Data Data pointer.
2533 @param[in] DataSize Size of Data found. If size is less than the
2534 data, this value contains the required size.
2535 @param[in] Attributes Attribute value of the variable.
2536 @param[in] AuthVarType Verify against PK, KEK database, private database or certificate in data payload.
2537 @param[out] VarDel Delete the variable or not.
2539 @retval EFI_INVALID_PARAMETER Invalid parameter.
2540 @retval EFI_SECURITY_VIOLATION The variable does NOT pass the validation
2541 check carried out by the firmware.
2542 @retval EFI_OUT_OF_RESOURCES Failed to process variable due to lack
2544 @retval EFI_SUCCESS Variable pass validation successfully.
2548 VerifyTimeBasedPayloadAndUpdate (
2549 IN CHAR16
*VariableName
,
2550 IN EFI_GUID
*VendorGuid
,
2553 IN UINT32 Attributes
,
2554 IN AUTHVAR_TYPE AuthVarType
,
2559 EFI_STATUS FindStatus
;
2562 EFI_VARIABLE_AUTHENTICATION_2
*CertData
;
2563 AUTH_VARIABLE_INFO OrgVariableInfo
;
2566 ZeroMem (&OrgVariableInfo
, sizeof (OrgVariableInfo
));
2567 FindStatus
= mAuthVarLibContextIn
->FindVariable (
2573 Status
= VerifyTimeBasedPayload (
2580 (!EFI_ERROR (FindStatus
)) ? OrgVariableInfo
.TimeStamp
: NULL
,
2584 if (EFI_ERROR (Status
)) {
2588 if (!EFI_ERROR(FindStatus
)
2589 && (PayloadSize
== 0)
2590 && ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0)) {
2596 CertData
= (EFI_VARIABLE_AUTHENTICATION_2
*) Data
;
2599 // Final step: Update/Append Variable if it pass Pkcs7Verify
2601 Status
= AuthServiceInternalUpdateVariableWithTimeStamp (
2607 &CertData
->TimeStamp
2611 // Delete signer's certificates when delete the common authenticated variable.
2613 if (IsDel
&& AuthVarType
== AuthVarTypePriv
&& !EFI_ERROR(Status
) ) {
2614 Status
= DeleteCertsFromDb (VariableName
, VendorGuid
, Attributes
);
2617 if (VarDel
!= NULL
) {
2618 if (IsDel
&& !EFI_ERROR(Status
)) {