2 Implement authentication services for the authenticated variable
5 Caution: This module requires additional review when modified.
6 This driver will have external input - variable data. It may be input in SMM mode.
7 This external input must be validated carefully to avoid security issue like
8 buffer overflow, integer overflow.
9 Variable attribute should also be checked to avoid authentication bypass.
11 ProcessVarWithPk(), ProcessVarWithKek() and ProcessVariable() are the function to do
12 variable authentication.
14 VerifyTimeBasedPayload() and VerifyCounterBasedPayload() are sub function to do verification.
15 They will do basic validation for authentication data structure, then call crypto library
16 to verify the signature.
18 Copyright (c) 2009 - 2013, Intel Corporation. All rights reserved.<BR>
19 This program and the accompanying materials
20 are licensed and made available under the terms and conditions of the BSD License
21 which accompanies this distribution. The full text of the license may be found at
22 http://opensource.org/licenses/bsd-license.php
24 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
25 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
30 #include "AuthService.h"
33 /// Global database array for scratch
35 UINT8 mPubKeyStore
[MAX_KEYDB_SIZE
];
37 UINT8 mCertDbStore
[MAX_CERTDB_SIZE
];
39 EFI_GUID mSignatureSupport
[] = {EFI_CERT_SHA1_GUID
, EFI_CERT_SHA256_GUID
, EFI_CERT_RSA2048_GUID
, EFI_CERT_X509_GUID
};
41 // Public Exponent of RSA Key.
43 CONST UINT8 mRsaE
[] = { 0x01, 0x00, 0x01 };
45 // Hash context pointer
47 VOID
*mHashCtx
= NULL
;
50 // Pointer to runtime buffer.
51 // For "Append" operation to an existing variable, a read/modify/write operation
52 // is supported by firmware internally. Reserve runtime buffer to cache previous
53 // variable data in runtime phase because memory allocation is forbidden in virtual mode.
55 VOID
*mStorageArea
= NULL
;
58 // The serialization of the values of the VariableName, VendorGuid and Attributes
59 // parameters of the SetVariable() call and the TimeStamp component of the
60 // EFI_VARIABLE_AUTHENTICATION_2 descriptor followed by the variable's new value
61 // i.e. (VariableName, VendorGuid, Attributes, TimeStamp, Data)
63 UINT8
*mSerializationRuntimeBuffer
= NULL
;
66 // Requirement for different signature type which have been defined in UEFI spec.
67 // These data are used to peform SignatureList format check while setting PK/KEK variable.
69 EFI_SIGNATURE_ITEM mSupportSigItem
[] = {
70 //{SigType, SigHeaderSize, SigDataSize }
71 {EFI_CERT_SHA256_GUID
, 0, 32 },
72 {EFI_CERT_RSA2048_GUID
, 0, 256 },
73 {EFI_CERT_RSA2048_SHA256_GUID
, 0, 256 },
74 {EFI_CERT_SHA1_GUID
, 0, 20 },
75 {EFI_CERT_RSA2048_SHA1_GUID
, 0, 256 },
76 {EFI_CERT_X509_GUID
, 0, ((UINT32
) ~0)},
77 {EFI_CERT_SHA224_GUID
, 0, 28 },
78 {EFI_CERT_SHA384_GUID
, 0, 48 },
79 {EFI_CERT_SHA512_GUID
, 0, 64 }
83 Determine whether this operation needs a physical present user.
85 @param[in] VariableName Name of the Variable.
86 @param[in] VendorGuid GUID of the Variable.
88 @retval TRUE This variable is protected, only a physical present user could set this variable.
89 @retval FALSE This variable is not protected.
93 NeedPhysicallyPresent(
94 IN CHAR16
*VariableName
,
95 IN EFI_GUID
*VendorGuid
98 if ((CompareGuid (VendorGuid
, &gEfiSecureBootEnableDisableGuid
) && (StrCmp (VariableName
, EFI_SECURE_BOOT_ENABLE_NAME
) == 0))
99 || (CompareGuid (VendorGuid
, &gEfiCustomModeEnableGuid
) && (StrCmp (VariableName
, EFI_CUSTOM_MODE_NAME
) == 0))) {
107 Determine whether the platform is operating in Custom Secure Boot mode.
109 @retval TRUE The platform is operating in Custom mode.
110 @retval FALSE The platform is operating in Standard mode.
118 VARIABLE_POINTER_TRACK Variable
;
120 FindVariable (EFI_CUSTOM_MODE_NAME
, &gEfiCustomModeEnableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
121 if (Variable
.CurrPtr
!= NULL
&& *(GetVariableDataPtr (Variable
.CurrPtr
)) == CUSTOM_SECURE_BOOT_MODE
) {
130 Internal function to delete a Variable given its name and GUID, no authentication
133 @param[in] VariableName Name of the Variable.
134 @param[in] VendorGuid GUID of the Variable.
136 @retval EFI_SUCCESS Variable deleted successfully.
137 @retval Others The driver failded to start the device.
142 IN CHAR16
*VariableName
,
143 IN EFI_GUID
*VendorGuid
147 VARIABLE_POINTER_TRACK Variable
;
149 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
150 if (EFI_ERROR (Status
)) {
154 ASSERT (Variable
.CurrPtr
!= NULL
);
155 return UpdateVariable (VariableName
, VendorGuid
, NULL
, 0, 0, 0, 0, &Variable
, NULL
);
159 Initializes for authenticated varibale service.
161 @retval EFI_SUCCESS Function successfully executed.
162 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resources.
166 AutenticatedVariableServiceInitialize (
171 VARIABLE_POINTER_TRACK Variable
;
172 VARIABLE_POINTER_TRACK PkVariable
;
178 UINT8 SecureBootMode
;
179 UINT8 SecureBootEnable
;
184 // Initialize hash context.
186 CtxSize
= Sha256GetContextSize ();
187 mHashCtx
= AllocateRuntimePool (CtxSize
);
188 if (mHashCtx
== NULL
) {
189 return EFI_OUT_OF_RESOURCES
;
193 // Reserved runtime buffer for "Append" operation in virtual mode.
195 mStorageArea
= AllocateRuntimePool (PcdGet32 (PcdMaxVariableSize
));
196 if (mStorageArea
== NULL
) {
197 return EFI_OUT_OF_RESOURCES
;
201 // Prepare runtime buffer for serialized data of time-based authenticated
202 // Variable, i.e. (VariableName, VendorGuid, Attributes, TimeStamp, Data).
204 mSerializationRuntimeBuffer
= AllocateRuntimePool (PcdGet32 (PcdMaxVariableSize
) + sizeof (EFI_GUID
) + sizeof (UINT32
) + sizeof (EFI_TIME
));
205 if (mSerializationRuntimeBuffer
== NULL
) {
206 return EFI_OUT_OF_RESOURCES
;
210 // Check "AuthVarKeyDatabase" variable's existence.
211 // If it doesn't exist, create a new one with initial value of 0 and EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS set.
213 Status
= FindVariable (
215 &gEfiAuthenticatedVariableGuid
,
217 &mVariableModuleGlobal
->VariableGlobal
,
221 if (Variable
.CurrPtr
== NULL
) {
222 VarAttr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
;
225 Status
= UpdateVariable (
227 &gEfiAuthenticatedVariableGuid
,
236 if (EFI_ERROR (Status
)) {
241 // Load database in global variable for cache.
243 DataSize
= DataSizeOfVariable (Variable
.CurrPtr
);
244 Data
= GetVariableDataPtr (Variable
.CurrPtr
);
245 ASSERT ((DataSize
!= 0) && (Data
!= NULL
));
246 CopyMem (mPubKeyStore
, (UINT8
*) Data
, DataSize
);
247 mPubKeyNumber
= (UINT32
) (DataSize
/ EFI_CERT_TYPE_RSA2048_SIZE
);
250 FindVariable (EFI_PLATFORM_KEY_NAME
, &gEfiGlobalVariableGuid
, &PkVariable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
251 if (PkVariable
.CurrPtr
== NULL
) {
252 DEBUG ((EFI_D_INFO
, "Variable %s does not exist.\n", EFI_PLATFORM_KEY_NAME
));
254 DEBUG ((EFI_D_INFO
, "Variable %s exists.\n", EFI_PLATFORM_KEY_NAME
));
258 // Create "SetupMode" varable with BS+RT attribute set.
260 FindVariable (EFI_SETUP_MODE_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
261 if (PkVariable
.CurrPtr
== NULL
) {
262 mPlatformMode
= SETUP_MODE
;
264 mPlatformMode
= USER_MODE
;
266 Status
= UpdateVariable (
268 &gEfiGlobalVariableGuid
,
271 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
277 if (EFI_ERROR (Status
)) {
282 // Create "SignatureSupport" varable with BS+RT attribute set.
284 FindVariable (EFI_SIGNATURE_SUPPORT_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
285 Status
= UpdateVariable (
286 EFI_SIGNATURE_SUPPORT_NAME
,
287 &gEfiGlobalVariableGuid
,
289 sizeof(mSignatureSupport
),
290 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
296 if (EFI_ERROR (Status
)) {
301 // If "SecureBootEnable" variable exists, then update "SecureBoot" variable.
302 // If "SecureBootEnable" variable is SECURE_BOOT_ENABLE and in USER_MODE, Set "SecureBoot" variable to SECURE_BOOT_MODE_ENABLE.
303 // If "SecureBootEnable" variable is SECURE_BOOT_DISABLE, Set "SecureBoot" variable to SECURE_BOOT_MODE_DISABLE.
305 SecureBootEnable
= SECURE_BOOT_DISABLE
;
306 FindVariable (EFI_SECURE_BOOT_ENABLE_NAME
, &gEfiSecureBootEnableDisableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
307 if (Variable
.CurrPtr
!= NULL
) {
308 SecureBootEnable
= *(GetVariableDataPtr (Variable
.CurrPtr
));
309 } else if (mPlatformMode
== USER_MODE
) {
311 // "SecureBootEnable" not exist, initialize it in USER_MODE.
313 SecureBootEnable
= SECURE_BOOT_ENABLE
;
314 Status
= UpdateVariable (
315 EFI_SECURE_BOOT_ENABLE_NAME
,
316 &gEfiSecureBootEnableDisableGuid
,
319 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
325 if (EFI_ERROR (Status
)) {
331 // Create "SecureBoot" varable with BS+RT attribute set.
333 if (SecureBootEnable
== SECURE_BOOT_ENABLE
&& mPlatformMode
== USER_MODE
) {
334 SecureBootMode
= SECURE_BOOT_MODE_ENABLE
;
336 SecureBootMode
= SECURE_BOOT_MODE_DISABLE
;
338 FindVariable (EFI_SECURE_BOOT_MODE_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
339 Status
= UpdateVariable (
340 EFI_SECURE_BOOT_MODE_NAME
,
341 &gEfiGlobalVariableGuid
,
344 EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
350 if (EFI_ERROR (Status
)) {
354 DEBUG ((EFI_D_INFO
, "Variable %s is %x\n", EFI_SETUP_MODE_NAME
, mPlatformMode
));
355 DEBUG ((EFI_D_INFO
, "Variable %s is %x\n", EFI_SECURE_BOOT_MODE_NAME
, SecureBootMode
));
356 DEBUG ((EFI_D_INFO
, "Variable %s is %x\n", EFI_SECURE_BOOT_ENABLE_NAME
, SecureBootEnable
));
359 // Check "CustomMode" variable's existence.
361 FindVariable (EFI_CUSTOM_MODE_NAME
, &gEfiCustomModeEnableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
362 if (Variable
.CurrPtr
!= NULL
) {
363 CustomMode
= *(GetVariableDataPtr (Variable
.CurrPtr
));
366 // "CustomMode" not exist, initialize it in STANDARD_SECURE_BOOT_MODE.
368 CustomMode
= STANDARD_SECURE_BOOT_MODE
;
369 Status
= UpdateVariable (
370 EFI_CUSTOM_MODE_NAME
,
371 &gEfiCustomModeEnableGuid
,
374 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
380 if (EFI_ERROR (Status
)) {
385 DEBUG ((EFI_D_INFO
, "Variable %s is %x\n", EFI_CUSTOM_MODE_NAME
, CustomMode
));
388 // Check "certdb" variable's existence.
389 // If it doesn't exist, then create a new one with
390 // EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set.
392 Status
= FindVariable (
396 &mVariableModuleGlobal
->VariableGlobal
,
400 if (Variable
.CurrPtr
== NULL
) {
401 VarAttr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
402 ListSize
= sizeof (UINT32
);
403 Status
= UpdateVariable (
414 if (EFI_ERROR (Status
)) {
423 Add public key in store and return its index.
425 @param[in] PubKey Input pointer to Public Key data
427 @return Index of new added item
438 VARIABLE_POINTER_TRACK Variable
;
443 if (PubKey
== NULL
) {
447 Status
= FindVariable (
449 &gEfiAuthenticatedVariableGuid
,
451 &mVariableModuleGlobal
->VariableGlobal
,
454 ASSERT_EFI_ERROR (Status
);
456 // Check whether the public key entry does exist.
459 for (Ptr
= mPubKeyStore
, Index
= 1; Index
<= mPubKeyNumber
; Index
++) {
460 if (CompareMem (Ptr
, PubKey
, EFI_CERT_TYPE_RSA2048_SIZE
) == 0) {
464 Ptr
+= EFI_CERT_TYPE_RSA2048_SIZE
;
469 // Add public key in database.
471 if (mPubKeyNumber
== MAX_KEY_NUM
) {
473 // Public key dadatase is full, try to reclaim invalid key.
477 // NV storage can't reclaim at runtime.
483 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
484 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
490 if (EFI_ERROR (Status
)) {
494 Status
= FindVariable (
496 &gEfiAuthenticatedVariableGuid
,
498 &mVariableModuleGlobal
->VariableGlobal
,
501 ASSERT_EFI_ERROR (Status
);
503 DataSize
= DataSizeOfVariable (Variable
.CurrPtr
);
504 Data
= GetVariableDataPtr (Variable
.CurrPtr
);
505 ASSERT ((DataSize
!= 0) && (Data
!= NULL
));
506 CopyMem (mPubKeyStore
, (UINT8
*) Data
, DataSize
);
507 mPubKeyNumber
= (UINT32
) (DataSize
/ EFI_CERT_TYPE_RSA2048_SIZE
);
509 if (mPubKeyNumber
== MAX_KEY_NUM
) {
514 CopyMem (mPubKeyStore
+ mPubKeyNumber
* EFI_CERT_TYPE_RSA2048_SIZE
, PubKey
, EFI_CERT_TYPE_RSA2048_SIZE
);
515 Index
= ++mPubKeyNumber
;
517 // Update public key database variable.
519 Status
= UpdateVariable (
521 &gEfiAuthenticatedVariableGuid
,
523 mPubKeyNumber
* EFI_CERT_TYPE_RSA2048_SIZE
,
524 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
,
530 ASSERT_EFI_ERROR (Status
);
537 Verify data payload with AuthInfo in EFI_CERT_TYPE_RSA2048_SHA256_GUID type.
538 Follow the steps in UEFI2.2.
540 Caution: This function may receive untrusted input.
541 This function may be invoked in SMM mode, and datasize and data are external input.
542 This function will do basic validation, before parse the data.
543 This function will parse the authentication carefully to avoid security issues, like
544 buffer overflow, integer overflow.
546 @param[in] Data Pointer to data with AuthInfo.
547 @param[in] DataSize Size of Data.
548 @param[in] PubKey Public key used for verification.
550 @retval EFI_INVALID_PARAMETER Invalid parameter.
551 @retval EFI_SECURITY_VIOLATION If authentication failed.
552 @retval EFI_SUCCESS Authentication successful.
556 VerifyCounterBasedPayload (
563 EFI_VARIABLE_AUTHENTICATION
*CertData
;
564 EFI_CERT_BLOCK_RSA_2048_SHA256
*CertBlock
;
565 UINT8 Digest
[SHA256_DIGEST_SIZE
];
569 PayloadSize
= DataSize
- AUTHINFO_SIZE
;
574 if (Data
== NULL
|| PubKey
== NULL
) {
575 return EFI_INVALID_PARAMETER
;
578 CertData
= (EFI_VARIABLE_AUTHENTICATION
*) Data
;
579 CertBlock
= (EFI_CERT_BLOCK_RSA_2048_SHA256
*) (CertData
->AuthInfo
.CertData
);
582 // wCertificateType should be WIN_CERT_TYPE_EFI_GUID.
583 // Cert type should be EFI_CERT_TYPE_RSA2048_SHA256_GUID.
585 if ((CertData
->AuthInfo
.Hdr
.wCertificateType
!= WIN_CERT_TYPE_EFI_GUID
) ||
586 !CompareGuid (&CertData
->AuthInfo
.CertType
, &gEfiCertTypeRsa2048Sha256Guid
)
589 // Invalid AuthInfo type, return EFI_SECURITY_VIOLATION.
591 return EFI_SECURITY_VIOLATION
;
594 // Hash data payload with SHA256.
596 ZeroMem (Digest
, SHA256_DIGEST_SIZE
);
597 Status
= Sha256Init (mHashCtx
);
601 Status
= Sha256Update (mHashCtx
, Data
+ AUTHINFO_SIZE
, PayloadSize
);
608 Status
= Sha256Update (mHashCtx
, &PayloadSize
, sizeof (UINTN
));
613 // Hash Monotonic Count.
615 Status
= Sha256Update (mHashCtx
, &CertData
->MonotonicCount
, sizeof (UINT64
));
619 Status
= Sha256Final (mHashCtx
, Digest
);
624 // Generate & Initialize RSA Context.
627 ASSERT (Rsa
!= NULL
);
629 // Set RSA Key Components.
630 // NOTE: Only N and E are needed to be set as RSA public key for signature verification.
632 Status
= RsaSetKey (Rsa
, RsaKeyN
, PubKey
, EFI_CERT_TYPE_RSA2048_SIZE
);
636 Status
= RsaSetKey (Rsa
, RsaKeyE
, mRsaE
, sizeof (mRsaE
));
641 // Verify the signature.
643 Status
= RsaPkcs1Verify (
647 CertBlock
->Signature
,
648 EFI_CERT_TYPE_RSA2048_SHA256_SIZE
658 return EFI_SECURITY_VIOLATION
;
663 Update platform mode.
665 @param[in] Mode SETUP_MODE or USER_MODE.
667 @return EFI_INVALID_PARAMETER Invalid parameter.
668 @return EFI_SUCCESS Update platform mode successfully.
677 VARIABLE_POINTER_TRACK Variable
;
679 UINT8 SecureBootMode
;
680 UINT8 SecureBootEnable
;
681 UINTN VariableDataSize
;
683 Status
= FindVariable (
685 &gEfiGlobalVariableGuid
,
687 &mVariableModuleGlobal
->VariableGlobal
,
690 if (EFI_ERROR (Status
)) {
695 // Update the value of SetupMode variable by a simple mem copy, this could avoid possible
696 // variable storage reclaim at runtime.
698 mPlatformMode
= (UINT8
) Mode
;
699 CopyMem (GetVariableDataPtr (Variable
.CurrPtr
), &mPlatformMode
, sizeof(UINT8
));
703 // SecureBoot Variable indicates whether the platform firmware is operating
704 // in Secure boot mode (1) or not (0), so we should not change SecureBoot
705 // Variable in runtime.
711 // Check "SecureBoot" variable's existence.
712 // If it doesn't exist, firmware has no capability to perform driver signing verification,
713 // then set "SecureBoot" to 0.
715 Status
= FindVariable (
716 EFI_SECURE_BOOT_MODE_NAME
,
717 &gEfiGlobalVariableGuid
,
719 &mVariableModuleGlobal
->VariableGlobal
,
723 // If "SecureBoot" variable exists, then check "SetupMode" variable update.
724 // If "SetupMode" variable is USER_MODE, "SecureBoot" variable is set to 1.
725 // If "SetupMode" variable is SETUP_MODE, "SecureBoot" variable is set to 0.
727 if (Variable
.CurrPtr
== NULL
) {
728 SecureBootMode
= SECURE_BOOT_MODE_DISABLE
;
730 if (mPlatformMode
== USER_MODE
) {
731 SecureBootMode
= SECURE_BOOT_MODE_ENABLE
;
732 } else if (mPlatformMode
== SETUP_MODE
) {
733 SecureBootMode
= SECURE_BOOT_MODE_DISABLE
;
735 return EFI_NOT_FOUND
;
739 VarAttr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
;
740 Status
= UpdateVariable (
741 EFI_SECURE_BOOT_MODE_NAME
,
742 &gEfiGlobalVariableGuid
,
751 if (EFI_ERROR (Status
)) {
756 // Check "SecureBootEnable" variable's existence. It can enable/disable secure boot feature.
758 Status
= FindVariable (
759 EFI_SECURE_BOOT_ENABLE_NAME
,
760 &gEfiSecureBootEnableDisableGuid
,
762 &mVariableModuleGlobal
->VariableGlobal
,
766 if (SecureBootMode
== SECURE_BOOT_MODE_ENABLE
) {
768 // Create the "SecureBootEnable" variable as secure boot is enabled.
770 SecureBootEnable
= SECURE_BOOT_ENABLE
;
771 VariableDataSize
= sizeof (SecureBootEnable
);
774 // Delete the "SecureBootEnable" variable if this variable exist as "SecureBoot"
775 // variable is not in secure boot state.
777 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
780 SecureBootEnable
= SECURE_BOOT_DISABLE
;
781 VariableDataSize
= 0;
784 Status
= UpdateVariable (
785 EFI_SECURE_BOOT_ENABLE_NAME
,
786 &gEfiSecureBootEnableDisableGuid
,
789 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
799 Check input data form to make sure it is a valid EFI_SIGNATURE_LIST for PK/KEK/db/dbx variable.
801 @param[in] VariableName Name of Variable to be check.
802 @param[in] VendorGuid Variable vendor GUID.
803 @param[in] Data Point to the variable data to be checked.
804 @param[in] DataSize Size of Data.
806 @return EFI_INVALID_PARAMETER Invalid signature list format.
807 @return EFI_SUCCESS Passed signature list format check successfully.
811 CheckSignatureListFormat(
812 IN CHAR16
*VariableName
,
813 IN EFI_GUID
*VendorGuid
,
818 EFI_SIGNATURE_LIST
*SigList
;
824 EFI_SIGNATURE_DATA
*CertData
;
831 ASSERT (VariableName
!= NULL
&& VendorGuid
!= NULL
&& Data
!= NULL
);
833 if (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, EFI_PLATFORM_KEY_NAME
) == 0)){
835 } else if ((CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && StrCmp (VariableName
, EFI_KEY_EXCHANGE_KEY_NAME
) == 0) ||
836 (CompareGuid (VendorGuid
, &gEfiImageSecurityDatabaseGuid
) &&
837 (StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE
) == 0 || StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE1
) == 0))){
844 SigList
= (EFI_SIGNATURE_LIST
*) Data
;
845 SigDataSize
= DataSize
;
849 // Walk throuth the input signature list and check the data format.
850 // If any signature is incorrectly formed, the whole check will fail.
852 while ((SigDataSize
> 0) && (SigDataSize
>= SigList
->SignatureListSize
)) {
853 for (Index
= 0; Index
< (sizeof (mSupportSigItem
) / sizeof (EFI_SIGNATURE_ITEM
)); Index
++ ) {
854 if (CompareGuid (&SigList
->SignatureType
, &mSupportSigItem
[Index
].SigType
)) {
856 // The value of SignatureSize should always be 16 (size of SignatureOwner
857 // component) add the data length according to signature type.
859 if (mSupportSigItem
[Index
].SigDataSize
!= ((UINT32
) ~0) &&
860 (SigList
->SignatureSize
- sizeof (EFI_GUID
)) != mSupportSigItem
[Index
].SigDataSize
) {
861 return EFI_INVALID_PARAMETER
;
863 if (mSupportSigItem
[Index
].SigHeaderSize
!= ((UINTN
) ~0) &&
864 SigList
->SignatureHeaderSize
!= mSupportSigItem
[Index
].SigHeaderSize
) {
865 return EFI_INVALID_PARAMETER
;
871 if (Index
== (sizeof (mSupportSigItem
) / sizeof (EFI_SIGNATURE_ITEM
))) {
873 // Undefined signature type.
875 return EFI_INVALID_PARAMETER
;
878 if (CompareGuid (&SigList
->SignatureType
, &gEfiCertX509Guid
)) {
880 // Try to retrieve the RSA public key from the X.509 certificate.
881 // If this operation fails, it's not a valid certificate.
883 RsaContext
= RsaNew ();
884 if (RsaContext
== NULL
) {
885 return EFI_INVALID_PARAMETER
;
887 CertData
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) SigList
+ sizeof (EFI_SIGNATURE_LIST
) + SigList
->SignatureHeaderSize
);
888 CertLen
= SigList
->SignatureSize
- sizeof (EFI_GUID
);
889 if (!RsaGetPublicKeyFromX509 (CertData
->SignatureData
, CertLen
, &RsaContext
)) {
890 RsaFree (RsaContext
);
891 return EFI_INVALID_PARAMETER
;
893 RsaFree (RsaContext
);
896 if ((SigList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - SigList
->SignatureHeaderSize
) % SigList
->SignatureSize
!= 0) {
897 return EFI_INVALID_PARAMETER
;
899 SigCount
+= (SigList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - SigList
->SignatureHeaderSize
) / SigList
->SignatureSize
;
901 SigDataSize
-= SigList
->SignatureListSize
;
902 SigList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) SigList
+ SigList
->SignatureListSize
);
905 if (((UINTN
) SigList
- (UINTN
) Data
) != DataSize
) {
906 return EFI_INVALID_PARAMETER
;
909 if (IsPk
&& SigCount
> 1) {
910 return EFI_INVALID_PARAMETER
;
917 Process variable with platform key for verification.
919 Caution: This function may receive untrusted input.
920 This function may be invoked in SMM mode, and datasize and data are external input.
921 This function will do basic validation, before parse the data.
922 This function will parse the authentication carefully to avoid security issues, like
923 buffer overflow, integer overflow.
924 This function will check attribute carefully to avoid authentication bypass.
926 @param[in] VariableName Name of Variable to be found.
927 @param[in] VendorGuid Variable vendor GUID.
928 @param[in] Data Data pointer.
929 @param[in] DataSize Size of Data found. If size is less than the
930 data, this value contains the required size.
931 @param[in] Variable The variable information which is used to keep track of variable usage.
932 @param[in] Attributes Attribute value of the variable
933 @param[in] IsPk Indicate whether it is to process pk.
935 @return EFI_INVALID_PARAMETER Invalid parameter.
936 @return EFI_SECURITY_VIOLATION The variable does NOT pass the validation.
937 check carried out by the firmware.
938 @return EFI_SUCCESS Variable passed validation successfully.
943 IN CHAR16
*VariableName
,
944 IN EFI_GUID
*VendorGuid
,
947 IN VARIABLE_POINTER_TRACK
*Variable
,
948 IN UINT32 Attributes OPTIONAL
,
957 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0 ||
958 (Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) == 0) {
960 // PK, KEK and db/dbx should set EFI_VARIABLE_NON_VOLATILE attribute and should be a time-based
961 // authenticated variable.
963 return EFI_INVALID_PARAMETER
;
967 if ((InCustomMode() && UserPhysicalPresent()) || (mPlatformMode
== SETUP_MODE
&& !IsPk
)) {
968 Payload
= (UINT8
*) Data
+ AUTHINFO2_SIZE (Data
);
969 PayloadSize
= DataSize
- AUTHINFO2_SIZE (Data
);
970 if (PayloadSize
== 0) {
974 Status
= CheckSignatureListFormat(VariableName
, VendorGuid
, Payload
, PayloadSize
);
975 if (EFI_ERROR (Status
)) {
979 Status
= UpdateVariable (
988 &((EFI_VARIABLE_AUTHENTICATION_2
*) Data
)->TimeStamp
990 } else if (mPlatformMode
== USER_MODE
) {
992 // Verify against X509 Cert in PK database.
994 Status
= VerifyTimeBasedPayload (
1006 // Verify against the certificate in data payload.
1008 Status
= VerifyTimeBasedPayload (
1020 if (!EFI_ERROR(Status
) && IsPk
) {
1021 if (mPlatformMode
== SETUP_MODE
&& !Del
) {
1023 // If enroll PK in setup mode, need change to user mode.
1025 Status
= UpdatePlatformMode (USER_MODE
);
1026 } else if (mPlatformMode
== USER_MODE
&& Del
){
1028 // If delete PK in user mode, need change to setup mode.
1030 Status
= UpdatePlatformMode (SETUP_MODE
);
1038 Process variable with key exchange key for verification.
1040 Caution: This function may receive untrusted input.
1041 This function may be invoked in SMM mode, and datasize and data are external input.
1042 This function will do basic validation, before parse the data.
1043 This function will parse the authentication carefully to avoid security issues, like
1044 buffer overflow, integer overflow.
1045 This function will check attribute carefully to avoid authentication bypass.
1047 @param[in] VariableName Name of Variable to be found.
1048 @param[in] VendorGuid Variable vendor GUID.
1049 @param[in] Data Data pointer.
1050 @param[in] DataSize Size of Data found. If size is less than the
1051 data, this value contains the required size.
1052 @param[in] Variable The variable information which is used to keep track of variable usage.
1053 @param[in] Attributes Attribute value of the variable.
1055 @return EFI_INVALID_PARAMETER Invalid parameter.
1056 @return EFI_SECURITY_VIOLATION The variable does NOT pass the validation
1057 check carried out by the firmware.
1058 @return EFI_SUCCESS Variable pass validation successfully.
1063 IN CHAR16
*VariableName
,
1064 IN EFI_GUID
*VendorGuid
,
1067 IN VARIABLE_POINTER_TRACK
*Variable
,
1068 IN UINT32 Attributes OPTIONAL
1075 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0 ||
1076 (Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) == 0) {
1078 // DB and DBX should set EFI_VARIABLE_NON_VOLATILE attribute and should be a time-based
1079 // authenticated variable.
1081 return EFI_INVALID_PARAMETER
;
1084 Status
= EFI_SUCCESS
;
1085 if (mPlatformMode
== USER_MODE
&& !(InCustomMode() && UserPhysicalPresent())) {
1087 // Time-based, verify against X509 Cert KEK.
1089 return VerifyTimeBasedPayload (
1101 // If in setup mode or custom secure boot mode, no authentication needed.
1103 Payload
= (UINT8
*) Data
+ AUTHINFO2_SIZE (Data
);
1104 PayloadSize
= DataSize
- AUTHINFO2_SIZE (Data
);
1106 Status
= CheckSignatureListFormat(VariableName
, VendorGuid
, Payload
, PayloadSize
);
1107 if (EFI_ERROR (Status
)) {
1111 Status
= UpdateVariable (
1120 &((EFI_VARIABLE_AUTHENTICATION_2
*) Data
)->TimeStamp
1128 Process variable with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS/EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set
1130 Caution: This function may receive untrusted input.
1131 This function may be invoked in SMM mode, and datasize and data are external input.
1132 This function will do basic validation, before parse the data.
1133 This function will parse the authentication carefully to avoid security issues, like
1134 buffer overflow, integer overflow.
1135 This function will check attribute carefully to avoid authentication bypass.
1137 @param[in] VariableName Name of Variable to be found.
1138 @param[in] VendorGuid Variable vendor GUID.
1140 @param[in] Data Data pointer.
1141 @param[in] DataSize Size of Data found. If size is less than the
1142 data, this value contains the required size.
1143 @param[in] Variable The variable information which is used to keep track of variable usage.
1144 @param[in] Attributes Attribute value of the variable.
1146 @return EFI_INVALID_PARAMETER Invalid parameter.
1147 @return EFI_WRITE_PROTECTED Variable is write-protected and needs authentication with
1148 EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS set.
1149 @return EFI_OUT_OF_RESOURCES The Database to save the public key is full.
1150 @return EFI_SECURITY_VIOLATION The variable is with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
1151 set, but the AuthInfo does NOT pass the validation
1152 check carried out by the firmware.
1153 @return EFI_SUCCESS Variable is not write-protected or pass validation successfully.
1158 IN CHAR16
*VariableName
,
1159 IN EFI_GUID
*VendorGuid
,
1162 IN VARIABLE_POINTER_TRACK
*Variable
,
1163 IN UINT32 Attributes
1168 BOOLEAN IsFirstTime
;
1170 EFI_VARIABLE_AUTHENTICATION
*CertData
;
1171 EFI_CERT_BLOCK_RSA_2048_SHA256
*CertBlock
;
1173 UINT64 MonotonicCount
;
1181 if (NeedPhysicallyPresent(VariableName
, VendorGuid
) && !UserPhysicalPresent()) {
1183 // This variable is protected, only physical present user could modify its value.
1185 return EFI_SECURITY_VIOLATION
;
1189 // A time-based authenticated variable and a count-based authenticated variable
1190 // can't be updated by each other.
1192 if (Variable
->CurrPtr
!= NULL
) {
1193 if (((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) &&
1194 ((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0)) {
1195 return EFI_SECURITY_VIOLATION
;
1198 if (((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) &&
1199 ((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0)) {
1200 return EFI_SECURITY_VIOLATION
;
1205 // Process Time-based Authenticated variable.
1207 if ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) {
1208 return VerifyTimeBasedPayload (
1221 // Determine if first time SetVariable with the EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS.
1223 if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) {
1225 // Determine current operation type.
1227 if (DataSize
== AUTHINFO_SIZE
) {
1231 // Determine whether this is the first time with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS set.
1233 if (Variable
->CurrPtr
== NULL
) {
1235 } else if ((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) == 0) {
1238 KeyIndex
= Variable
->CurrPtr
->PubKeyIndex
;
1239 IsFirstTime
= FALSE
;
1241 } else if ((Variable
->CurrPtr
!= NULL
) &&
1242 ((Variable
->CurrPtr
->Attributes
& (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
)) != 0)
1245 // If the variable is already write-protected, it always needs authentication before update.
1247 return EFI_WRITE_PROTECTED
;
1250 // If without EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS, set and attributes collision.
1251 // That means it is not authenticated variable, just update variable as usual.
1253 Status
= UpdateVariable (VariableName
, VendorGuid
, Data
, DataSize
, Attributes
, 0, 0, Variable
, NULL
);
1258 // Get PubKey and check Monotonic Count value corresponding to the variable.
1260 CertData
= (EFI_VARIABLE_AUTHENTICATION
*) Data
;
1261 CertBlock
= (EFI_CERT_BLOCK_RSA_2048_SHA256
*) (CertData
->AuthInfo
.CertData
);
1262 PubKey
= CertBlock
->PublicKey
;
1265 // Update Monotonic Count value.
1267 MonotonicCount
= CertData
->MonotonicCount
;
1271 // Check input PubKey.
1273 if (CompareMem (PubKey
, mPubKeyStore
+ (KeyIndex
- 1) * EFI_CERT_TYPE_RSA2048_SIZE
, EFI_CERT_TYPE_RSA2048_SIZE
) != 0) {
1274 return EFI_SECURITY_VIOLATION
;
1277 // Compare the current monotonic count and ensure that it is greater than the last SetVariable
1278 // operation with the EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS attribute set.
1280 if (CertData
->MonotonicCount
<= Variable
->CurrPtr
->MonotonicCount
) {
1282 // Monotonic count check fail, suspicious replay attack, return EFI_SECURITY_VIOLATION.
1284 return EFI_SECURITY_VIOLATION
;
1288 // Verify the certificate in Data payload.
1290 Status
= VerifyCounterBasedPayload (Data
, DataSize
, PubKey
);
1291 if (EFI_ERROR (Status
)) {
1296 // Now, the signature has been verified!
1298 if (IsFirstTime
&& !IsDeletion
) {
1300 // Update public key database variable if need.
1302 KeyIndex
= AddPubKeyInStore (PubKey
);
1303 if (KeyIndex
== 0) {
1304 return EFI_OUT_OF_RESOURCES
;
1309 // Verification pass.
1311 return UpdateVariable (VariableName
, VendorGuid
, (UINT8
*)Data
+ AUTHINFO_SIZE
, DataSize
- AUTHINFO_SIZE
, Attributes
, KeyIndex
, MonotonicCount
, Variable
, NULL
);
1315 Merge two buffers which formatted as EFI_SIGNATURE_LIST. Only the new EFI_SIGNATURE_DATA
1316 will be appended to the original EFI_SIGNATURE_LIST, duplicate EFI_SIGNATURE_DATA
1319 @param[in, out] Data Pointer to original EFI_SIGNATURE_LIST.
1320 @param[in] DataSize Size of Data buffer.
1321 @param[in] NewData Pointer to new EFI_SIGNATURE_LIST to be appended.
1322 @param[in] NewDataSize Size of NewData buffer.
1324 @return Size of the merged buffer.
1328 AppendSignatureList (
1332 IN UINTN NewDataSize
1335 EFI_SIGNATURE_LIST
*CertList
;
1336 EFI_SIGNATURE_DATA
*Cert
;
1338 EFI_SIGNATURE_LIST
*NewCertList
;
1339 EFI_SIGNATURE_DATA
*NewCert
;
1346 UINTN SignatureListSize
;
1349 Tail
= (UINT8
*) Data
+ DataSize
;
1351 NewCertList
= (EFI_SIGNATURE_LIST
*) NewData
;
1352 while ((NewDataSize
> 0) && (NewDataSize
>= NewCertList
->SignatureListSize
)) {
1353 NewCert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) NewCertList
+ sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
);
1354 NewCertCount
= (NewCertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - NewCertList
->SignatureHeaderSize
) / NewCertList
->SignatureSize
;
1357 for (Index
= 0; Index
< NewCertCount
; Index
++) {
1361 CertList
= (EFI_SIGNATURE_LIST
*) Data
;
1362 while ((Size
> 0) && (Size
>= CertList
->SignatureListSize
)) {
1363 if (CompareGuid (&CertList
->SignatureType
, &NewCertList
->SignatureType
) &&
1364 (CertList
->SignatureSize
== NewCertList
->SignatureSize
)) {
1365 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
1366 CertCount
= (CertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - CertList
->SignatureHeaderSize
) / CertList
->SignatureSize
;
1367 for (Index2
= 0; Index2
< CertCount
; Index2
++) {
1369 // Iterate each Signature Data in this Signature List.
1371 if (CompareMem (NewCert
, Cert
, CertList
->SignatureSize
) == 0) {
1375 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) Cert
+ CertList
->SignatureSize
);
1382 Size
-= CertList
->SignatureListSize
;
1383 CertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) CertList
+ CertList
->SignatureListSize
);
1388 // New EFI_SIGNATURE_DATA, append it.
1390 if (CopiedCount
== 0) {
1392 // Copy EFI_SIGNATURE_LIST header for only once.
1394 CopyMem (Tail
, NewCertList
, sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
);
1395 Tail
= Tail
+ sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
;
1398 CopyMem (Tail
, NewCert
, NewCertList
->SignatureSize
);
1399 Tail
+= NewCertList
->SignatureSize
;
1403 NewCert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) NewCert
+ NewCertList
->SignatureSize
);
1407 // Update SignatureListSize in newly appended EFI_SIGNATURE_LIST.
1409 if (CopiedCount
!= 0) {
1410 SignatureListSize
= sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
+ (CopiedCount
* NewCertList
->SignatureSize
);
1411 CertList
= (EFI_SIGNATURE_LIST
*) (Tail
- SignatureListSize
);
1412 CertList
->SignatureListSize
= (UINT32
) SignatureListSize
;
1415 NewDataSize
-= NewCertList
->SignatureListSize
;
1416 NewCertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) NewCertList
+ NewCertList
->SignatureListSize
);
1419 return (Tail
- (UINT8
*) Data
);
1423 Compare two EFI_TIME data.
1426 @param FirstTime A pointer to the first EFI_TIME data.
1427 @param SecondTime A pointer to the second EFI_TIME data.
1429 @retval TRUE The FirstTime is not later than the SecondTime.
1430 @retval FALSE The FirstTime is later than the SecondTime.
1435 IN EFI_TIME
*FirstTime
,
1436 IN EFI_TIME
*SecondTime
1439 if (FirstTime
->Year
!= SecondTime
->Year
) {
1440 return (BOOLEAN
) (FirstTime
->Year
< SecondTime
->Year
);
1441 } else if (FirstTime
->Month
!= SecondTime
->Month
) {
1442 return (BOOLEAN
) (FirstTime
->Month
< SecondTime
->Month
);
1443 } else if (FirstTime
->Day
!= SecondTime
->Day
) {
1444 return (BOOLEAN
) (FirstTime
->Day
< SecondTime
->Day
);
1445 } else if (FirstTime
->Hour
!= SecondTime
->Hour
) {
1446 return (BOOLEAN
) (FirstTime
->Hour
< SecondTime
->Hour
);
1447 } else if (FirstTime
->Minute
!= SecondTime
->Minute
) {
1448 return (BOOLEAN
) (FirstTime
->Minute
< SecondTime
->Minute
);
1451 return (BOOLEAN
) (FirstTime
->Second
<= SecondTime
->Second
);
1455 Find matching signer's certificates for common authenticated variable
1456 by corresponding VariableName and VendorGuid from "certdb".
1458 The data format of "certdb":
1460 // UINT32 CertDbListSize;
1461 // /// AUTH_CERT_DB_DATA Certs1[];
1462 // /// AUTH_CERT_DB_DATA Certs2[];
1464 // /// AUTH_CERT_DB_DATA Certsn[];
1467 @param[in] VariableName Name of authenticated Variable.
1468 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1469 @param[in] Data Pointer to variable "certdb".
1470 @param[in] DataSize Size of variable "certdb".
1471 @param[out] CertOffset Offset of matching CertData, from starting of Data.
1472 @param[out] CertDataSize Length of CertData in bytes.
1473 @param[out] CertNodeOffset Offset of matching AUTH_CERT_DB_DATA , from
1475 @param[out] CertNodeSize Length of AUTH_CERT_DB_DATA in bytes.
1477 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1478 @retval EFI_NOT_FOUND Fail to find matching certs.
1479 @retval EFI_SUCCESS Find matching certs and output parameters.
1484 IN CHAR16
*VariableName
,
1485 IN EFI_GUID
*VendorGuid
,
1488 OUT UINT32
*CertOffset
, OPTIONAL
1489 OUT UINT32
*CertDataSize
, OPTIONAL
1490 OUT UINT32
*CertNodeOffset
,OPTIONAL
1491 OUT UINT32
*CertNodeSize OPTIONAL
1495 AUTH_CERT_DB_DATA
*Ptr
;
1499 UINT32 CertDbListSize
;
1501 if ((VariableName
== NULL
) || (VendorGuid
== NULL
) || (Data
== NULL
)) {
1502 return EFI_INVALID_PARAMETER
;
1506 // Check whether DataSize matches recorded CertDbListSize.
1508 if (DataSize
< sizeof (UINT32
)) {
1509 return EFI_INVALID_PARAMETER
;
1512 CertDbListSize
= ReadUnaligned32 ((UINT32
*) Data
);
1514 if (CertDbListSize
!= (UINT32
) DataSize
) {
1515 return EFI_INVALID_PARAMETER
;
1518 Offset
= sizeof (UINT32
);
1521 // Get corresponding certificates by VendorGuid and VariableName.
1523 while (Offset
< (UINT32
) DataSize
) {
1524 Ptr
= (AUTH_CERT_DB_DATA
*) (Data
+ Offset
);
1526 // Check whether VendorGuid matches.
1528 if (CompareGuid (&Ptr
->VendorGuid
, VendorGuid
)) {
1529 NodeSize
= ReadUnaligned32 (&Ptr
->CertNodeSize
);
1530 NameSize
= ReadUnaligned32 (&Ptr
->NameSize
);
1531 CertSize
= ReadUnaligned32 (&Ptr
->CertDataSize
);
1533 if (NodeSize
!= sizeof (EFI_GUID
) + sizeof (UINT32
) * 3 + CertSize
+
1534 sizeof (CHAR16
) * NameSize
) {
1535 return EFI_INVALID_PARAMETER
;
1538 Offset
= Offset
+ sizeof (EFI_GUID
) + sizeof (UINT32
) * 3;
1540 // Check whether VariableName matches.
1542 if ((NameSize
== StrLen (VariableName
)) &&
1543 (CompareMem (Data
+ Offset
, VariableName
, NameSize
* sizeof (CHAR16
)) == 0)) {
1544 Offset
= Offset
+ NameSize
* sizeof (CHAR16
);
1546 if (CertOffset
!= NULL
) {
1547 *CertOffset
= Offset
;
1550 if (CertDataSize
!= NULL
) {
1551 *CertDataSize
= CertSize
;
1554 if (CertNodeOffset
!= NULL
) {
1555 *CertNodeOffset
= (UINT32
) ((UINT8
*) Ptr
- Data
);
1558 if (CertNodeSize
!= NULL
) {
1559 *CertNodeSize
= NodeSize
;
1564 Offset
= Offset
+ NameSize
* sizeof (CHAR16
) + CertSize
;
1567 NodeSize
= ReadUnaligned32 (&Ptr
->CertNodeSize
);
1568 Offset
= Offset
+ NodeSize
;
1572 return EFI_NOT_FOUND
;
1576 Retrieve signer's certificates for common authenticated variable
1577 by corresponding VariableName and VendorGuid from "certdb".
1579 @param[in] VariableName Name of authenticated Variable.
1580 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1581 @param[out] CertData Pointer to signer's certificates.
1582 @param[out] CertDataSize Length of CertData in bytes.
1584 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1585 @retval EFI_NOT_FOUND Fail to find "certdb" or matching certs.
1586 @retval EFI_SUCCESS Get signer's certificates successfully.
1591 IN CHAR16
*VariableName
,
1592 IN EFI_GUID
*VendorGuid
,
1593 OUT UINT8
**CertData
,
1594 OUT UINT32
*CertDataSize
1597 VARIABLE_POINTER_TRACK CertDbVariable
;
1603 if ((VariableName
== NULL
) || (VendorGuid
== NULL
) || (CertData
== NULL
) || (CertDataSize
== NULL
)) {
1604 return EFI_INVALID_PARAMETER
;
1608 // Get variable "certdb".
1610 Status
= FindVariable (
1614 &mVariableModuleGlobal
->VariableGlobal
,
1617 if (EFI_ERROR (Status
)) {
1621 DataSize
= DataSizeOfVariable (CertDbVariable
.CurrPtr
);
1622 Data
= GetVariableDataPtr (CertDbVariable
.CurrPtr
);
1623 if ((DataSize
== 0) || (Data
== NULL
)) {
1625 return EFI_NOT_FOUND
;
1628 Status
= FindCertsFromDb (
1639 if (EFI_ERROR (Status
)) {
1643 *CertData
= Data
+ CertOffset
;
1648 Delete matching signer's certificates when deleting common authenticated
1649 variable by corresponding VariableName and VendorGuid from "certdb".
1651 @param[in] VariableName Name of authenticated Variable.
1652 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1654 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1655 @retval EFI_NOT_FOUND Fail to find "certdb" or matching certs.
1656 @retval EFI_OUT_OF_RESOURCES The operation is failed due to lack of resources.
1657 @retval EFI_SUCCESS The operation is completed successfully.
1662 IN CHAR16
*VariableName
,
1663 IN EFI_GUID
*VendorGuid
1666 VARIABLE_POINTER_TRACK CertDbVariable
;
1671 UINT32 CertNodeOffset
;
1672 UINT32 CertNodeSize
;
1674 UINT32 NewCertDbSize
;
1676 if ((VariableName
== NULL
) || (VendorGuid
== NULL
)) {
1677 return EFI_INVALID_PARAMETER
;
1681 // Get variable "certdb".
1683 Status
= FindVariable (
1687 &mVariableModuleGlobal
->VariableGlobal
,
1690 if (EFI_ERROR (Status
)) {
1694 DataSize
= DataSizeOfVariable (CertDbVariable
.CurrPtr
);
1695 Data
= GetVariableDataPtr (CertDbVariable
.CurrPtr
);
1696 if ((DataSize
== 0) || (Data
== NULL
)) {
1698 return EFI_NOT_FOUND
;
1701 if (DataSize
== sizeof (UINT32
)) {
1703 // There is no certs in certdb.
1709 // Get corresponding cert node from certdb.
1711 Status
= FindCertsFromDb (
1722 if (EFI_ERROR (Status
)) {
1726 if (DataSize
< (CertNodeOffset
+ CertNodeSize
)) {
1727 return EFI_NOT_FOUND
;
1731 // Construct new data content of variable "certdb".
1733 NewCertDbSize
= (UINT32
) DataSize
- CertNodeSize
;
1734 NewCertDb
= (UINT8
*) mCertDbStore
;
1737 // Copy the DB entries before deleting node.
1739 CopyMem (NewCertDb
, Data
, CertNodeOffset
);
1741 // Update CertDbListSize.
1743 CopyMem (NewCertDb
, &NewCertDbSize
, sizeof (UINT32
));
1745 // Copy the DB entries after deleting node.
1747 if (DataSize
> (CertNodeOffset
+ CertNodeSize
)) {
1749 NewCertDb
+ CertNodeOffset
,
1750 Data
+ CertNodeOffset
+ CertNodeSize
,
1751 DataSize
- CertNodeOffset
- CertNodeSize
1758 VarAttr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
1759 Status
= UpdateVariable (
1775 Insert signer's certificates for common authenticated variable with VariableName
1776 and VendorGuid in AUTH_CERT_DB_DATA to "certdb".
1778 @param[in] VariableName Name of authenticated Variable.
1779 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1780 @param[in] CertData Pointer to signer's certificates.
1781 @param[in] CertDataSize Length of CertData in bytes.
1783 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1784 @retval EFI_ACCESS_DENIED An AUTH_CERT_DB_DATA entry with same VariableName
1785 and VendorGuid already exists.
1786 @retval EFI_OUT_OF_RESOURCES The operation is failed due to lack of resources.
1787 @retval EFI_SUCCESS Insert an AUTH_CERT_DB_DATA entry to "certdb"
1792 IN CHAR16
*VariableName
,
1793 IN EFI_GUID
*VendorGuid
,
1795 IN UINTN CertDataSize
1798 VARIABLE_POINTER_TRACK CertDbVariable
;
1804 UINT32 NewCertDbSize
;
1805 UINT32 CertNodeSize
;
1807 AUTH_CERT_DB_DATA
*Ptr
;
1809 if ((VariableName
== NULL
) || (VendorGuid
== NULL
) || (CertData
== NULL
)) {
1810 return EFI_INVALID_PARAMETER
;
1814 // Get variable "certdb".
1816 Status
= FindVariable (
1820 &mVariableModuleGlobal
->VariableGlobal
,
1823 if (EFI_ERROR (Status
)) {
1827 DataSize
= DataSizeOfVariable (CertDbVariable
.CurrPtr
);
1828 Data
= GetVariableDataPtr (CertDbVariable
.CurrPtr
);
1829 if ((DataSize
== 0) || (Data
== NULL
)) {
1831 return EFI_NOT_FOUND
;
1835 // Find whether matching cert node already exists in "certdb".
1836 // If yes return error.
1838 Status
= FindCertsFromDb (
1849 if (!EFI_ERROR (Status
)) {
1851 return EFI_ACCESS_DENIED
;
1855 // Construct new data content of variable "certdb".
1857 NameSize
= (UINT32
) StrLen (VariableName
);
1858 CertNodeSize
= sizeof (AUTH_CERT_DB_DATA
) + (UINT32
) CertDataSize
+ NameSize
* sizeof (CHAR16
);
1859 NewCertDbSize
= (UINT32
) DataSize
+ CertNodeSize
;
1860 if (NewCertDbSize
> MAX_CERTDB_SIZE
) {
1861 return EFI_OUT_OF_RESOURCES
;
1863 NewCertDb
= (UINT8
*) mCertDbStore
;
1866 // Copy the DB entries before deleting node.
1868 CopyMem (NewCertDb
, Data
, DataSize
);
1870 // Update CertDbListSize.
1872 CopyMem (NewCertDb
, &NewCertDbSize
, sizeof (UINT32
));
1874 // Construct new cert node.
1876 Ptr
= (AUTH_CERT_DB_DATA
*) (NewCertDb
+ DataSize
);
1877 CopyGuid (&Ptr
->VendorGuid
, VendorGuid
);
1878 CopyMem (&Ptr
->CertNodeSize
, &CertNodeSize
, sizeof (UINT32
));
1879 CopyMem (&Ptr
->NameSize
, &NameSize
, sizeof (UINT32
));
1880 CopyMem (&Ptr
->CertDataSize
, &CertDataSize
, sizeof (UINT32
));
1883 (UINT8
*) Ptr
+ sizeof (AUTH_CERT_DB_DATA
),
1885 NameSize
* sizeof (CHAR16
)
1889 (UINT8
*) Ptr
+ sizeof (AUTH_CERT_DB_DATA
) + NameSize
* sizeof (CHAR16
),
1897 VarAttr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
1898 Status
= UpdateVariable (
1914 Process variable with EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set
1916 Caution: This function may receive untrusted input.
1917 This function may be invoked in SMM mode, and datasize and data are external input.
1918 This function will do basic validation, before parse the data.
1919 This function will parse the authentication carefully to avoid security issues, like
1920 buffer overflow, integer overflow.
1922 @param[in] VariableName Name of Variable to be found.
1923 @param[in] VendorGuid Variable vendor GUID.
1924 @param[in] Data Data pointer.
1925 @param[in] DataSize Size of Data found. If size is less than the
1926 data, this value contains the required size.
1927 @param[in] Variable The variable information which is used to keep track of variable usage.
1928 @param[in] Attributes Attribute value of the variable.
1929 @param[in] AuthVarType Verify against PK, KEK database, private database or certificate in data payload.
1930 @param[out] VarDel Delete the variable or not.
1932 @retval EFI_INVALID_PARAMETER Invalid parameter.
1933 @retval EFI_SECURITY_VIOLATION The variable does NOT pass the validation
1934 check carried out by the firmware.
1935 @retval EFI_OUT_OF_RESOURCES Failed to process variable due to lack
1937 @retval EFI_SUCCESS Variable pass validation successfully.
1941 VerifyTimeBasedPayload (
1942 IN CHAR16
*VariableName
,
1943 IN EFI_GUID
*VendorGuid
,
1946 IN VARIABLE_POINTER_TRACK
*Variable
,
1947 IN UINT32 Attributes
,
1948 IN AUTHVAR_TYPE AuthVarType
,
1962 BOOLEAN VerifyStatus
;
1964 EFI_SIGNATURE_LIST
*CertList
;
1965 EFI_SIGNATURE_DATA
*Cert
;
1966 VARIABLE_POINTER_TRACK KekVariable
;
1967 EFI_VARIABLE_AUTHENTICATION_2
*CertData
;
1970 VARIABLE_POINTER_TRACK PkVariable
;
1975 UINTN CertStackSize
;
1976 UINT8
*CertsInCertDb
;
1977 UINT32 CertsSizeinDb
;
1979 VerifyStatus
= FALSE
;
1988 // When the attribute EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS is
1989 // set, then the Data buffer shall begin with an instance of a complete (and serialized)
1990 // EFI_VARIABLE_AUTHENTICATION_2 descriptor. The descriptor shall be followed by the new
1991 // variable value and DataSize shall reflect the combined size of the descriptor and the new
1992 // variable value. The authentication descriptor is not part of the variable data and is not
1993 // returned by subsequent calls to GetVariable().
1995 CertData
= (EFI_VARIABLE_AUTHENTICATION_2
*) Data
;
1998 // Verify that Pad1, Nanosecond, TimeZone, Daylight and Pad2 components of the
1999 // TimeStamp value are set to zero.
2001 if ((CertData
->TimeStamp
.Pad1
!= 0) ||
2002 (CertData
->TimeStamp
.Nanosecond
!= 0) ||
2003 (CertData
->TimeStamp
.TimeZone
!= 0) ||
2004 (CertData
->TimeStamp
.Daylight
!= 0) ||
2005 (CertData
->TimeStamp
.Pad2
!= 0)) {
2006 return EFI_SECURITY_VIOLATION
;
2009 if ((Variable
->CurrPtr
!= NULL
) && ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0)) {
2010 if (CompareTimeStamp (&CertData
->TimeStamp
, &Variable
->CurrPtr
->TimeStamp
)) {
2012 // TimeStamp check fail, suspicious replay attack, return EFI_SECURITY_VIOLATION.
2014 return EFI_SECURITY_VIOLATION
;
2019 // wCertificateType should be WIN_CERT_TYPE_EFI_GUID.
2020 // Cert type should be EFI_CERT_TYPE_PKCS7_GUID.
2022 if ((CertData
->AuthInfo
.Hdr
.wCertificateType
!= WIN_CERT_TYPE_EFI_GUID
) ||
2023 !CompareGuid (&CertData
->AuthInfo
.CertType
, &gEfiCertPkcs7Guid
)) {
2025 // Invalid AuthInfo type, return EFI_SECURITY_VIOLATION.
2027 return EFI_SECURITY_VIOLATION
;
2031 // Find out Pkcs7 SignedData which follows the EFI_VARIABLE_AUTHENTICATION_2 descriptor.
2032 // AuthInfo.Hdr.dwLength is the length of the entire certificate, including the length of the header.
2034 SigData
= CertData
->AuthInfo
.CertData
;
2035 SigDataSize
= CertData
->AuthInfo
.Hdr
.dwLength
- (UINT32
) (OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID
, CertData
));
2038 // Find out the new data payload which follows Pkcs7 SignedData directly.
2040 PayloadPtr
= SigData
+ SigDataSize
;
2041 PayloadSize
= DataSize
- OFFSET_OF_AUTHINFO2_CERT_DATA
- (UINTN
) SigDataSize
;
2044 // Construct a buffer to fill with (VariableName, VendorGuid, Attributes, TimeStamp, Data).
2046 NewDataSize
= PayloadSize
+ sizeof (EFI_TIME
) + sizeof (UINT32
) +
2047 sizeof (EFI_GUID
) + StrSize (VariableName
) - sizeof (CHAR16
);
2048 NewData
= mSerializationRuntimeBuffer
;
2051 Length
= StrLen (VariableName
) * sizeof (CHAR16
);
2052 CopyMem (Buffer
, VariableName
, Length
);
2055 Length
= sizeof (EFI_GUID
);
2056 CopyMem (Buffer
, VendorGuid
, Length
);
2059 Length
= sizeof (UINT32
);
2060 CopyMem (Buffer
, &Attr
, Length
);
2063 Length
= sizeof (EFI_TIME
);
2064 CopyMem (Buffer
, &CertData
->TimeStamp
, Length
);
2067 CopyMem (Buffer
, PayloadPtr
, PayloadSize
);
2069 if (AuthVarType
== AuthVarTypePk
) {
2071 // Get platform key from variable.
2073 Status
= FindVariable (
2074 EFI_PLATFORM_KEY_NAME
,
2075 &gEfiGlobalVariableGuid
,
2077 &mVariableModuleGlobal
->VariableGlobal
,
2080 if (EFI_ERROR (Status
)) {
2084 CertList
= (EFI_SIGNATURE_LIST
*) GetVariableDataPtr (PkVariable
.CurrPtr
);
2085 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
2086 RootCert
= Cert
->SignatureData
;
2087 RootCertSize
= CertList
->SignatureSize
- (sizeof (EFI_SIGNATURE_DATA
) - 1);
2091 // Verify Pkcs7 SignedData via Pkcs7Verify library.
2093 VerifyStatus
= Pkcs7Verify (
2102 } else if (AuthVarType
== AuthVarTypeKek
) {
2105 // Get KEK database from variable.
2107 Status
= FindVariable (
2108 EFI_KEY_EXCHANGE_KEY_NAME
,
2109 &gEfiGlobalVariableGuid
,
2111 &mVariableModuleGlobal
->VariableGlobal
,
2114 if (EFI_ERROR (Status
)) {
2119 // Ready to verify Pkcs7 SignedData. Go through KEK Signature Database to find out X.509 CertList.
2121 KekDataSize
= KekVariable
.CurrPtr
->DataSize
;
2122 CertList
= (EFI_SIGNATURE_LIST
*) GetVariableDataPtr (KekVariable
.CurrPtr
);
2123 while ((KekDataSize
> 0) && (KekDataSize
>= CertList
->SignatureListSize
)) {
2124 if (CompareGuid (&CertList
->SignatureType
, &gEfiCertX509Guid
)) {
2125 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
2126 CertCount
= (CertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - CertList
->SignatureHeaderSize
) / CertList
->SignatureSize
;
2127 for (Index
= 0; Index
< CertCount
; Index
++) {
2129 // Iterate each Signature Data Node within this CertList for a verify
2131 RootCert
= Cert
->SignatureData
;
2132 RootCertSize
= CertList
->SignatureSize
- (sizeof (EFI_SIGNATURE_DATA
) - 1);
2135 // Verify Pkcs7 SignedData via Pkcs7Verify library.
2137 VerifyStatus
= Pkcs7Verify (
2148 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) Cert
+ CertList
->SignatureSize
);
2151 KekDataSize
-= CertList
->SignatureListSize
;
2152 CertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) CertList
+ CertList
->SignatureListSize
);
2154 } else if (AuthVarType
== AuthVarTypePriv
) {
2157 // Process common authenticated variable except PK/KEK/DB/DBX.
2158 // Get signer's certificates from SignedData.
2160 VerifyStatus
= Pkcs7GetSigners (
2168 if (!VerifyStatus
) {
2173 // Get previously stored signer's certificates from certdb for existing
2174 // variable. Check whether they are identical with signer's certificates
2175 // in SignedData. If not, return error immediately.
2177 if ((Variable
->CurrPtr
!= NULL
)) {
2178 VerifyStatus
= FALSE
;
2180 Status
= GetCertsFromDb (VariableName
, VendorGuid
, &CertsInCertDb
, &CertsSizeinDb
);
2181 if (EFI_ERROR (Status
)) {
2185 if ((CertStackSize
!= CertsSizeinDb
) ||
2186 (CompareMem (SignerCerts
, CertsInCertDb
, CertsSizeinDb
) != 0)) {
2191 VerifyStatus
= Pkcs7Verify (
2199 if (!VerifyStatus
) {
2204 // Delete signer's certificates when delete the common authenticated variable.
2206 if ((PayloadSize
== 0) && (Variable
->CurrPtr
!= NULL
) && ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0)) {
2207 Status
= DeleteCertsFromDb (VariableName
, VendorGuid
);
2208 if (EFI_ERROR (Status
)) {
2209 VerifyStatus
= FALSE
;
2212 } else if (Variable
->CurrPtr
== NULL
&& PayloadSize
!= 0) {
2214 // Insert signer's certificates when adding a new common authenticated variable.
2216 Status
= InsertCertsToDb (VariableName
, VendorGuid
, SignerCerts
, CertStackSize
);
2217 if (EFI_ERROR (Status
)) {
2218 VerifyStatus
= FALSE
;
2222 } else if (AuthVarType
== AuthVarTypePayload
) {
2223 CertList
= (EFI_SIGNATURE_LIST
*) PayloadPtr
;
2224 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
2225 RootCert
= Cert
->SignatureData
;
2226 RootCertSize
= CertList
->SignatureSize
- (sizeof (EFI_SIGNATURE_DATA
) - 1);
2228 // Verify Pkcs7 SignedData via Pkcs7Verify library.
2230 VerifyStatus
= Pkcs7Verify (
2239 return EFI_SECURITY_VIOLATION
;
2244 if (AuthVarType
== AuthVarTypePriv
) {
2245 Pkcs7FreeSigners (RootCert
);
2246 Pkcs7FreeSigners (SignerCerts
);
2249 if (!VerifyStatus
) {
2250 return EFI_SECURITY_VIOLATION
;
2253 Status
= CheckSignatureListFormat(VariableName
, VendorGuid
, PayloadPtr
, PayloadSize
);
2254 if (EFI_ERROR (Status
)) {
2258 if ((PayloadSize
== 0) && (VarDel
!= NULL
)) {
2263 // Final step: Update/Append Variable if it pass Pkcs7Verify
2265 return UpdateVariable (
2274 &CertData
->TimeStamp