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.
10 The whole SMM authentication variable design relies on the integrity of flash part and SMM.
11 which is assumed to be protected by platform. All variable code and metadata in flash/SMM Memory
12 may not be modified without authorization. If platform fails to protect these resources,
13 the authentication service provided in this driver will be broken, and the behavior is undefined.
15 ProcessVarWithPk(), ProcessVarWithKek() and ProcessVariable() are the function to do
16 variable authentication.
18 VerifyTimeBasedPayload() and VerifyCounterBasedPayload() are sub function to do verification.
19 They will do basic validation for authentication data structure, then call crypto library
20 to verify the signature.
22 Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>
23 This program and the accompanying materials
24 are licensed and made available under the terms and conditions of the BSD License
25 which accompanies this distribution. The full text of the license may be found at
26 http://opensource.org/licenses/bsd-license.php
28 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
29 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
34 #include "AuthService.h"
37 /// Global database array for scratch
44 UINT32 mMaxCertDbSize
;
46 UINT8 mVendorKeyState
;
48 EFI_GUID mSignatureSupport
[] = {EFI_CERT_SHA1_GUID
, EFI_CERT_SHA256_GUID
, EFI_CERT_RSA2048_GUID
, EFI_CERT_X509_GUID
};
50 // Public Exponent of RSA Key.
52 CONST UINT8 mRsaE
[] = { 0x01, 0x00, 0x01 };
54 // Hash context pointer
56 VOID
*mHashCtx
= NULL
;
59 // The serialization of the values of the VariableName, VendorGuid and Attributes
60 // parameters of the SetVariable() call and the TimeStamp component of the
61 // EFI_VARIABLE_AUTHENTICATION_2 descriptor followed by the variable's new value
62 // i.e. (VariableName, VendorGuid, Attributes, TimeStamp, Data)
64 UINT8
*mSerializationRuntimeBuffer
= NULL
;
67 // Requirement for different signature type which have been defined in UEFI spec.
68 // These data are used to peform SignatureList format check while setting PK/KEK variable.
70 EFI_SIGNATURE_ITEM mSupportSigItem
[] = {
71 //{SigType, SigHeaderSize, SigDataSize }
72 {EFI_CERT_SHA256_GUID
, 0, 32 },
73 {EFI_CERT_RSA2048_GUID
, 0, 256 },
74 {EFI_CERT_RSA2048_SHA256_GUID
, 0, 256 },
75 {EFI_CERT_SHA1_GUID
, 0, 20 },
76 {EFI_CERT_RSA2048_SHA1_GUID
, 0, 256 },
77 {EFI_CERT_X509_GUID
, 0, ((UINT32
) ~0)},
78 {EFI_CERT_SHA224_GUID
, 0, 28 },
79 {EFI_CERT_SHA384_GUID
, 0, 48 },
80 {EFI_CERT_SHA512_GUID
, 0, 64 },
81 {EFI_CERT_X509_SHA256_GUID
, 0, 48 },
82 {EFI_CERT_X509_SHA384_GUID
, 0, 64 },
83 {EFI_CERT_X509_SHA512_GUID
, 0, 80 }
87 Determine whether this operation needs a physical present user.
89 @param[in] VariableName Name of the Variable.
90 @param[in] VendorGuid GUID of the Variable.
92 @retval TRUE This variable is protected, only a physical present user could set this variable.
93 @retval FALSE This variable is not protected.
97 NeedPhysicallyPresent(
98 IN CHAR16
*VariableName
,
99 IN EFI_GUID
*VendorGuid
102 if ((CompareGuid (VendorGuid
, &gEfiSecureBootEnableDisableGuid
) && (StrCmp (VariableName
, EFI_SECURE_BOOT_ENABLE_NAME
) == 0))
103 || (CompareGuid (VendorGuid
, &gEfiCustomModeEnableGuid
) && (StrCmp (VariableName
, EFI_CUSTOM_MODE_NAME
) == 0))) {
111 Determine whether the platform is operating in Custom Secure Boot mode.
113 @retval TRUE The platform is operating in Custom mode.
114 @retval FALSE The platform is operating in Standard mode.
122 VARIABLE_POINTER_TRACK Variable
;
124 FindVariable (EFI_CUSTOM_MODE_NAME
, &gEfiCustomModeEnableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
125 if (Variable
.CurrPtr
!= NULL
&& *(GetVariableDataPtr (Variable
.CurrPtr
)) == CUSTOM_SECURE_BOOT_MODE
) {
134 Internal function to delete a Variable given its name and GUID, no authentication
137 @param[in] VariableName Name of the Variable.
138 @param[in] VendorGuid GUID of the Variable.
140 @retval EFI_SUCCESS Variable deleted successfully.
141 @retval Others The driver failded to start the device.
146 IN CHAR16
*VariableName
,
147 IN EFI_GUID
*VendorGuid
151 VARIABLE_POINTER_TRACK Variable
;
153 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
154 if (EFI_ERROR (Status
)) {
158 ASSERT (Variable
.CurrPtr
!= NULL
);
159 return UpdateVariable (VariableName
, VendorGuid
, NULL
, 0, 0, 0, 0, &Variable
, NULL
);
163 Initializes for authenticated varibale service.
165 @retval EFI_SUCCESS Function successfully executed.
166 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resources.
170 AutenticatedVariableServiceInitialize (
175 VARIABLE_POINTER_TRACK Variable
;
176 VARIABLE_POINTER_TRACK PkVariable
;
182 UINT8 SecureBootMode
;
183 UINT8 SecureBootEnable
;
188 // Initialize hash context.
190 CtxSize
= Sha256GetContextSize ();
191 mHashCtx
= AllocateRuntimePool (CtxSize
);
192 if (mHashCtx
== NULL
) {
193 return EFI_OUT_OF_RESOURCES
;
197 // Reserve runtime buffer for public key database. The size excludes variable header and name size.
199 mMaxKeyDbSize
= PcdGet32 (PcdMaxVariableSize
) - sizeof (VARIABLE_HEADER
) - sizeof (AUTHVAR_KEYDB_NAME
);
200 mMaxKeyNumber
= mMaxKeyDbSize
/ EFI_CERT_TYPE_RSA2048_SIZE
;
201 mPubKeyStore
= AllocateRuntimePool (mMaxKeyDbSize
);
202 if (mPubKeyStore
== NULL
) {
203 return EFI_OUT_OF_RESOURCES
;
207 // Reserve runtime buffer for certificate database. The size excludes variable header and name size.
209 mMaxCertDbSize
= PcdGet32 (PcdMaxVariableSize
) - sizeof (VARIABLE_HEADER
) - sizeof (EFI_CERT_DB_NAME
);
210 mCertDbStore
= AllocateRuntimePool (mMaxCertDbSize
);
211 if (mCertDbStore
== NULL
) {
212 return EFI_OUT_OF_RESOURCES
;
216 // Prepare runtime buffer for serialized data of time-based authenticated
217 // Variable, i.e. (VariableName, VendorGuid, Attributes, TimeStamp, Data).
219 mSerializationRuntimeBuffer
= AllocateRuntimePool (PcdGet32 (PcdMaxVariableSize
) + sizeof (EFI_GUID
) + sizeof (UINT32
) + sizeof (EFI_TIME
));
220 if (mSerializationRuntimeBuffer
== NULL
) {
221 return EFI_OUT_OF_RESOURCES
;
225 // Check "AuthVarKeyDatabase" variable's existence.
226 // If it doesn't exist, create a new one with initial value of 0 and EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS set.
228 Status
= FindVariable (
230 &gEfiAuthenticatedVariableGuid
,
232 &mVariableModuleGlobal
->VariableGlobal
,
236 if (Variable
.CurrPtr
== NULL
) {
237 VarAttr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
;
240 Status
= UpdateVariable (
242 &gEfiAuthenticatedVariableGuid
,
251 if (EFI_ERROR (Status
)) {
256 // Load database in global variable for cache.
258 DataSize
= DataSizeOfVariable (Variable
.CurrPtr
);
259 Data
= GetVariableDataPtr (Variable
.CurrPtr
);
260 ASSERT ((DataSize
!= 0) && (Data
!= NULL
));
262 // "AuthVarKeyDatabase" is an internal variable. Its DataSize is always ensured not to exceed mPubKeyStore buffer size(See definition before)
263 // Therefore, there is no memory overflow in underlying CopyMem.
265 CopyMem (mPubKeyStore
, (UINT8
*) Data
, DataSize
);
266 mPubKeyNumber
= (UINT32
) (DataSize
/ EFI_CERT_TYPE_RSA2048_SIZE
);
269 FindVariable (EFI_PLATFORM_KEY_NAME
, &gEfiGlobalVariableGuid
, &PkVariable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
270 if (PkVariable
.CurrPtr
== NULL
) {
271 DEBUG ((EFI_D_INFO
, "Variable %s does not exist.\n", EFI_PLATFORM_KEY_NAME
));
273 DEBUG ((EFI_D_INFO
, "Variable %s exists.\n", EFI_PLATFORM_KEY_NAME
));
277 // Create "SetupMode" variable with BS+RT attribute set.
279 FindVariable (EFI_SETUP_MODE_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
280 if (PkVariable
.CurrPtr
== NULL
) {
281 mPlatformMode
= SETUP_MODE
;
283 mPlatformMode
= USER_MODE
;
285 Status
= UpdateVariable (
287 &gEfiGlobalVariableGuid
,
290 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
296 if (EFI_ERROR (Status
)) {
301 // Create "SignatureSupport" variable with BS+RT attribute set.
303 FindVariable (EFI_SIGNATURE_SUPPORT_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
304 Status
= UpdateVariable (
305 EFI_SIGNATURE_SUPPORT_NAME
,
306 &gEfiGlobalVariableGuid
,
308 sizeof(mSignatureSupport
),
309 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
315 if (EFI_ERROR (Status
)) {
320 // If "SecureBootEnable" variable exists, then update "SecureBoot" variable.
321 // If "SecureBootEnable" variable is SECURE_BOOT_ENABLE and in USER_MODE, Set "SecureBoot" variable to SECURE_BOOT_MODE_ENABLE.
322 // If "SecureBootEnable" variable is SECURE_BOOT_DISABLE, Set "SecureBoot" variable to SECURE_BOOT_MODE_DISABLE.
324 SecureBootEnable
= SECURE_BOOT_DISABLE
;
325 FindVariable (EFI_SECURE_BOOT_ENABLE_NAME
, &gEfiSecureBootEnableDisableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
326 if (Variable
.CurrPtr
!= NULL
) {
327 SecureBootEnable
= *(GetVariableDataPtr (Variable
.CurrPtr
));
328 } else if (mPlatformMode
== USER_MODE
) {
330 // "SecureBootEnable" not exist, initialize it in USER_MODE.
332 SecureBootEnable
= SECURE_BOOT_ENABLE
;
333 Status
= UpdateVariable (
334 EFI_SECURE_BOOT_ENABLE_NAME
,
335 &gEfiSecureBootEnableDisableGuid
,
338 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
344 if (EFI_ERROR (Status
)) {
350 // Create "SecureBoot" variable with BS+RT attribute set.
352 if (SecureBootEnable
== SECURE_BOOT_ENABLE
&& mPlatformMode
== USER_MODE
) {
353 SecureBootMode
= SECURE_BOOT_MODE_ENABLE
;
355 SecureBootMode
= SECURE_BOOT_MODE_DISABLE
;
357 FindVariable (EFI_SECURE_BOOT_MODE_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
358 Status
= UpdateVariable (
359 EFI_SECURE_BOOT_MODE_NAME
,
360 &gEfiGlobalVariableGuid
,
363 EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
369 if (EFI_ERROR (Status
)) {
373 DEBUG ((EFI_D_INFO
, "Variable %s is %x\n", EFI_SETUP_MODE_NAME
, mPlatformMode
));
374 DEBUG ((EFI_D_INFO
, "Variable %s is %x\n", EFI_SECURE_BOOT_MODE_NAME
, SecureBootMode
));
375 DEBUG ((EFI_D_INFO
, "Variable %s is %x\n", EFI_SECURE_BOOT_ENABLE_NAME
, SecureBootEnable
));
378 // Initialize "CustomMode" in STANDARD_SECURE_BOOT_MODE state.
380 FindVariable (EFI_CUSTOM_MODE_NAME
, &gEfiCustomModeEnableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
381 CustomMode
= STANDARD_SECURE_BOOT_MODE
;
382 Status
= UpdateVariable (
383 EFI_CUSTOM_MODE_NAME
,
384 &gEfiCustomModeEnableGuid
,
387 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
393 if (EFI_ERROR (Status
)) {
397 DEBUG ((EFI_D_INFO
, "Variable %s is %x\n", EFI_CUSTOM_MODE_NAME
, CustomMode
));
400 // Check "certdb" variable's existence.
401 // If it doesn't exist, then create a new one with
402 // EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set.
404 Status
= FindVariable (
408 &mVariableModuleGlobal
->VariableGlobal
,
412 if (Variable
.CurrPtr
== NULL
) {
413 VarAttr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
414 ListSize
= sizeof (UINT32
);
415 Status
= UpdateVariable (
426 if (EFI_ERROR (Status
)) {
432 // Check "VendorKeysNv" variable's existence and create "VendorKeys" variable accordingly.
434 FindVariable (EFI_VENDOR_KEYS_NV_VARIABLE_NAME
, &gEfiVendorKeysNvGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
435 if (Variable
.CurrPtr
!= NULL
) {
436 mVendorKeyState
= *(GetVariableDataPtr (Variable
.CurrPtr
));
439 // "VendorKeysNv" not exist, initialize it in VENDOR_KEYS_VALID state.
441 mVendorKeyState
= VENDOR_KEYS_VALID
;
442 Status
= UpdateVariable (
443 EFI_VENDOR_KEYS_NV_VARIABLE_NAME
,
444 &gEfiVendorKeysNvGuid
,
447 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
,
453 if (EFI_ERROR (Status
)) {
459 // Create "VendorKeys" variable with BS+RT attribute set.
461 FindVariable (EFI_VENDOR_KEYS_VARIABLE_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
462 Status
= UpdateVariable (
463 EFI_VENDOR_KEYS_VARIABLE_NAME
,
464 &gEfiGlobalVariableGuid
,
467 EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
473 if (EFI_ERROR (Status
)) {
477 DEBUG ((EFI_D_INFO
, "Variable %s is %x\n", EFI_VENDOR_KEYS_VARIABLE_NAME
, mVendorKeyState
));
483 Add public key in store and return its index.
485 @param[in] PubKey Input pointer to Public Key data
486 @param[in] VariableDataEntry The variable data entry
488 @return Index of new added item
494 IN VARIABLE_ENTRY_CONSISTENCY
*VariableDataEntry
500 VARIABLE_POINTER_TRACK Variable
;
504 VARIABLE_ENTRY_CONSISTENCY PublicKeyEntry
;
507 if (PubKey
== NULL
) {
511 Status
= FindVariable (
513 &gEfiAuthenticatedVariableGuid
,
515 &mVariableModuleGlobal
->VariableGlobal
,
518 if (EFI_ERROR (Status
)) {
519 DEBUG ((EFI_D_ERROR
, "Get public key database variable failure, Status = %r\n", Status
));
524 // Check whether the public key entry does exist.
527 for (Ptr
= mPubKeyStore
, Index
= 1; Index
<= mPubKeyNumber
; Index
++) {
528 if (CompareMem (Ptr
, PubKey
, EFI_CERT_TYPE_RSA2048_SIZE
) == 0) {
532 Ptr
+= EFI_CERT_TYPE_RSA2048_SIZE
;
537 // Add public key in database.
539 if (mPubKeyNumber
== mMaxKeyNumber
) {
541 // Public key dadatase is full, try to reclaim invalid key.
545 // NV storage can't reclaim at runtime.
551 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
552 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
559 if (EFI_ERROR (Status
)) {
563 Status
= FindVariable (
565 &gEfiAuthenticatedVariableGuid
,
567 &mVariableModuleGlobal
->VariableGlobal
,
570 if (EFI_ERROR (Status
)) {
571 DEBUG ((EFI_D_ERROR
, "Get public key database variable failure, Status = %r\n", Status
));
575 DataSize
= DataSizeOfVariable (Variable
.CurrPtr
);
576 Data
= GetVariableDataPtr (Variable
.CurrPtr
);
577 ASSERT ((DataSize
!= 0) && (Data
!= NULL
));
579 // "AuthVarKeyDatabase" is an internal used variable. Its DataSize is always ensured not to exceed mPubKeyStore buffer size(See definition before)
580 // Therefore, there is no memory overflow in underlying CopyMem.
582 CopyMem (mPubKeyStore
, (UINT8
*) Data
, DataSize
);
583 mPubKeyNumber
= (UINT32
) (DataSize
/ EFI_CERT_TYPE_RSA2048_SIZE
);
585 if (mPubKeyNumber
== mMaxKeyNumber
) {
591 // Check the variable space for both public key and variable data.
593 PublicKeyEntry
.VariableSize
= (mPubKeyNumber
+ 1) * EFI_CERT_TYPE_RSA2048_SIZE
;
594 PublicKeyEntry
.Guid
= &gEfiAuthenticatedVariableGuid
;
595 PublicKeyEntry
.Name
= AUTHVAR_KEYDB_NAME
;
596 Attributes
= VARIABLE_ATTRIBUTE_NV_BS_RT
| EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
;
598 if (!CheckRemainingSpaceForConsistency (Attributes
, &PublicKeyEntry
, VariableDataEntry
, NULL
)) {
600 // No enough variable space.
605 CopyMem (mPubKeyStore
+ mPubKeyNumber
* EFI_CERT_TYPE_RSA2048_SIZE
, PubKey
, EFI_CERT_TYPE_RSA2048_SIZE
);
606 Index
= ++mPubKeyNumber
;
608 // Update public key database variable.
610 Status
= UpdateVariable (
612 &gEfiAuthenticatedVariableGuid
,
614 mPubKeyNumber
* EFI_CERT_TYPE_RSA2048_SIZE
,
621 if (EFI_ERROR (Status
)) {
622 DEBUG ((EFI_D_ERROR
, "Update public key database variable failure, Status = %r\n", Status
));
631 Verify data payload with AuthInfo in EFI_CERT_TYPE_RSA2048_SHA256_GUID type.
632 Follow the steps in UEFI2.2.
634 Caution: This function may receive untrusted input.
635 This function may be invoked in SMM mode, and datasize and data are external input.
636 This function will do basic validation, before parse the data.
637 This function will parse the authentication carefully to avoid security issues, like
638 buffer overflow, integer overflow.
640 @param[in] Data Pointer to data with AuthInfo.
641 @param[in] DataSize Size of Data.
642 @param[in] PubKey Public key used for verification.
644 @retval EFI_INVALID_PARAMETER Invalid parameter.
645 @retval EFI_SECURITY_VIOLATION If authentication failed.
646 @retval EFI_SUCCESS Authentication successful.
650 VerifyCounterBasedPayload (
657 EFI_VARIABLE_AUTHENTICATION
*CertData
;
658 EFI_CERT_BLOCK_RSA_2048_SHA256
*CertBlock
;
659 UINT8 Digest
[SHA256_DIGEST_SIZE
];
663 PayloadSize
= DataSize
- AUTHINFO_SIZE
;
668 if (Data
== NULL
|| PubKey
== NULL
) {
669 return EFI_INVALID_PARAMETER
;
672 CertData
= (EFI_VARIABLE_AUTHENTICATION
*) Data
;
673 CertBlock
= (EFI_CERT_BLOCK_RSA_2048_SHA256
*) (CertData
->AuthInfo
.CertData
);
676 // wCertificateType should be WIN_CERT_TYPE_EFI_GUID.
677 // Cert type should be EFI_CERT_TYPE_RSA2048_SHA256_GUID.
679 if ((CertData
->AuthInfo
.Hdr
.wCertificateType
!= WIN_CERT_TYPE_EFI_GUID
) ||
680 !CompareGuid (&CertData
->AuthInfo
.CertType
, &gEfiCertTypeRsa2048Sha256Guid
)
683 // Invalid AuthInfo type, return EFI_SECURITY_VIOLATION.
685 return EFI_SECURITY_VIOLATION
;
688 // Hash data payload with SHA256.
690 ZeroMem (Digest
, SHA256_DIGEST_SIZE
);
691 Status
= Sha256Init (mHashCtx
);
695 Status
= Sha256Update (mHashCtx
, Data
+ AUTHINFO_SIZE
, PayloadSize
);
702 Status
= Sha256Update (mHashCtx
, &PayloadSize
, sizeof (UINTN
));
707 // Hash Monotonic Count.
709 Status
= Sha256Update (mHashCtx
, &CertData
->MonotonicCount
, sizeof (UINT64
));
713 Status
= Sha256Final (mHashCtx
, Digest
);
718 // Generate & Initialize RSA Context.
721 ASSERT (Rsa
!= NULL
);
723 // Set RSA Key Components.
724 // NOTE: Only N and E are needed to be set as RSA public key for signature verification.
726 Status
= RsaSetKey (Rsa
, RsaKeyN
, PubKey
, EFI_CERT_TYPE_RSA2048_SIZE
);
730 Status
= RsaSetKey (Rsa
, RsaKeyE
, mRsaE
, sizeof (mRsaE
));
735 // Verify the signature.
737 Status
= RsaPkcs1Verify (
741 CertBlock
->Signature
,
742 EFI_CERT_TYPE_RSA2048_SHA256_SIZE
752 return EFI_SECURITY_VIOLATION
;
757 Update platform mode.
759 @param[in] Mode SETUP_MODE or USER_MODE.
761 @return EFI_INVALID_PARAMETER Invalid parameter.
762 @return EFI_SUCCESS Update platform mode successfully.
771 VARIABLE_POINTER_TRACK Variable
;
772 UINT8 SecureBootMode
;
773 UINT8 SecureBootEnable
;
774 UINTN VariableDataSize
;
776 Status
= FindVariable (
778 &gEfiGlobalVariableGuid
,
780 &mVariableModuleGlobal
->VariableGlobal
,
783 if (EFI_ERROR (Status
)) {
788 // Update the value of SetupMode variable by a simple mem copy, this could avoid possible
789 // variable storage reclaim at runtime.
791 mPlatformMode
= (UINT8
) Mode
;
792 CopyMem (GetVariableDataPtr (Variable
.CurrPtr
), &mPlatformMode
, sizeof(UINT8
));
796 // SecureBoot Variable indicates whether the platform firmware is operating
797 // in Secure boot mode (1) or not (0), so we should not change SecureBoot
798 // Variable in runtime.
804 // Check "SecureBoot" variable's existence.
805 // If it doesn't exist, firmware has no capability to perform driver signing verification,
806 // then set "SecureBoot" to 0.
808 Status
= FindVariable (
809 EFI_SECURE_BOOT_MODE_NAME
,
810 &gEfiGlobalVariableGuid
,
812 &mVariableModuleGlobal
->VariableGlobal
,
816 // If "SecureBoot" variable exists, then check "SetupMode" variable update.
817 // If "SetupMode" variable is USER_MODE, "SecureBoot" variable is set to 1.
818 // If "SetupMode" variable is SETUP_MODE, "SecureBoot" variable is set to 0.
820 if (Variable
.CurrPtr
== NULL
) {
821 SecureBootMode
= SECURE_BOOT_MODE_DISABLE
;
823 if (mPlatformMode
== USER_MODE
) {
824 SecureBootMode
= SECURE_BOOT_MODE_ENABLE
;
825 } else if (mPlatformMode
== SETUP_MODE
) {
826 SecureBootMode
= SECURE_BOOT_MODE_DISABLE
;
828 return EFI_NOT_FOUND
;
832 Status
= UpdateVariable (
833 EFI_SECURE_BOOT_MODE_NAME
,
834 &gEfiGlobalVariableGuid
,
837 EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
843 if (EFI_ERROR (Status
)) {
848 // Check "SecureBootEnable" variable's existence. It can enable/disable secure boot feature.
850 Status
= FindVariable (
851 EFI_SECURE_BOOT_ENABLE_NAME
,
852 &gEfiSecureBootEnableDisableGuid
,
854 &mVariableModuleGlobal
->VariableGlobal
,
858 if (SecureBootMode
== SECURE_BOOT_MODE_ENABLE
) {
860 // Create the "SecureBootEnable" variable as secure boot is enabled.
862 SecureBootEnable
= SECURE_BOOT_ENABLE
;
863 VariableDataSize
= sizeof (SecureBootEnable
);
866 // Delete the "SecureBootEnable" variable if this variable exist as "SecureBoot"
867 // variable is not in secure boot state.
869 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
872 SecureBootEnable
= SECURE_BOOT_DISABLE
;
873 VariableDataSize
= 0;
876 Status
= UpdateVariable (
877 EFI_SECURE_BOOT_ENABLE_NAME
,
878 &gEfiSecureBootEnableDisableGuid
,
881 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
891 Check input data form to make sure it is a valid EFI_SIGNATURE_LIST for PK/KEK/db/dbx/dbt variable.
893 @param[in] VariableName Name of Variable to be check.
894 @param[in] VendorGuid Variable vendor GUID.
895 @param[in] Data Point to the variable data to be checked.
896 @param[in] DataSize Size of Data.
898 @return EFI_INVALID_PARAMETER Invalid signature list format.
899 @return EFI_SUCCESS Passed signature list format check successfully.
903 CheckSignatureListFormat(
904 IN CHAR16
*VariableName
,
905 IN EFI_GUID
*VendorGuid
,
910 EFI_SIGNATURE_LIST
*SigList
;
916 EFI_SIGNATURE_DATA
*CertData
;
923 ASSERT (VariableName
!= NULL
&& VendorGuid
!= NULL
&& Data
!= NULL
);
925 if (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, EFI_PLATFORM_KEY_NAME
) == 0)){
927 } else if ((CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, EFI_KEY_EXCHANGE_KEY_NAME
) == 0)) ||
928 (CompareGuid (VendorGuid
, &gEfiImageSecurityDatabaseGuid
) &&
929 ((StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE
) == 0) || (StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE1
) == 0) ||
930 (StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE2
) == 0)))) {
937 SigList
= (EFI_SIGNATURE_LIST
*) Data
;
938 SigDataSize
= DataSize
;
942 // Walk throuth the input signature list and check the data format.
943 // If any signature is incorrectly formed, the whole check will fail.
945 while ((SigDataSize
> 0) && (SigDataSize
>= SigList
->SignatureListSize
)) {
946 for (Index
= 0; Index
< (sizeof (mSupportSigItem
) / sizeof (EFI_SIGNATURE_ITEM
)); Index
++ ) {
947 if (CompareGuid (&SigList
->SignatureType
, &mSupportSigItem
[Index
].SigType
)) {
949 // The value of SignatureSize should always be 16 (size of SignatureOwner
950 // component) add the data length according to signature type.
952 if (mSupportSigItem
[Index
].SigDataSize
!= ((UINT32
) ~0) &&
953 (SigList
->SignatureSize
- sizeof (EFI_GUID
)) != mSupportSigItem
[Index
].SigDataSize
) {
954 return EFI_INVALID_PARAMETER
;
956 if (mSupportSigItem
[Index
].SigHeaderSize
!= ((UINTN
) ~0) &&
957 SigList
->SignatureHeaderSize
!= mSupportSigItem
[Index
].SigHeaderSize
) {
958 return EFI_INVALID_PARAMETER
;
964 if (Index
== (sizeof (mSupportSigItem
) / sizeof (EFI_SIGNATURE_ITEM
))) {
966 // Undefined signature type.
968 return EFI_INVALID_PARAMETER
;
971 if (CompareGuid (&SigList
->SignatureType
, &gEfiCertX509Guid
)) {
973 // Try to retrieve the RSA public key from the X.509 certificate.
974 // If this operation fails, it's not a valid certificate.
976 RsaContext
= RsaNew ();
977 if (RsaContext
== NULL
) {
978 return EFI_INVALID_PARAMETER
;
980 CertData
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) SigList
+ sizeof (EFI_SIGNATURE_LIST
) + SigList
->SignatureHeaderSize
);
981 CertLen
= SigList
->SignatureSize
- sizeof (EFI_GUID
);
982 if (!RsaGetPublicKeyFromX509 (CertData
->SignatureData
, CertLen
, &RsaContext
)) {
983 RsaFree (RsaContext
);
984 return EFI_INVALID_PARAMETER
;
986 RsaFree (RsaContext
);
989 if ((SigList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - SigList
->SignatureHeaderSize
) % SigList
->SignatureSize
!= 0) {
990 return EFI_INVALID_PARAMETER
;
992 SigCount
+= (SigList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - SigList
->SignatureHeaderSize
) / SigList
->SignatureSize
;
994 SigDataSize
-= SigList
->SignatureListSize
;
995 SigList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) SigList
+ SigList
->SignatureListSize
);
998 if (((UINTN
) SigList
- (UINTN
) Data
) != DataSize
) {
999 return EFI_INVALID_PARAMETER
;
1002 if (IsPk
&& SigCount
> 1) {
1003 return EFI_INVALID_PARAMETER
;
1010 Update "VendorKeys" variable to record the out of band secure boot key modification.
1012 @return EFI_SUCCESS Variable is updated successfully.
1013 @return Others Failed to update variable.
1017 VendorKeyIsModified (
1022 VARIABLE_POINTER_TRACK Variable
;
1024 if (mVendorKeyState
== VENDOR_KEYS_MODIFIED
) {
1027 mVendorKeyState
= VENDOR_KEYS_MODIFIED
;
1029 FindVariable (EFI_VENDOR_KEYS_NV_VARIABLE_NAME
, &gEfiVendorKeysNvGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1030 Status
= UpdateVariable (
1031 EFI_VENDOR_KEYS_NV_VARIABLE_NAME
,
1032 &gEfiVendorKeysNvGuid
,
1035 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
,
1041 if (EFI_ERROR (Status
)) {
1045 FindVariable (EFI_VENDOR_KEYS_VARIABLE_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1046 return UpdateVariable (
1047 EFI_VENDOR_KEYS_VARIABLE_NAME
,
1048 &gEfiGlobalVariableGuid
,
1051 EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
1060 Process variable with platform key for verification.
1062 Caution: This function may receive untrusted input.
1063 This function may be invoked in SMM mode, and datasize and data are external input.
1064 This function will do basic validation, before parse the data.
1065 This function will parse the authentication carefully to avoid security issues, like
1066 buffer overflow, integer overflow.
1067 This function will check attribute carefully to avoid authentication bypass.
1069 @param[in] VariableName Name of Variable to be found.
1070 @param[in] VendorGuid Variable vendor GUID.
1071 @param[in] Data Data pointer.
1072 @param[in] DataSize Size of Data found. If size is less than the
1073 data, this value contains the required size.
1074 @param[in] Variable The variable information which is used to keep track of variable usage.
1075 @param[in] Attributes Attribute value of the variable
1076 @param[in] IsPk Indicate whether it is to process pk.
1078 @return EFI_INVALID_PARAMETER Invalid parameter.
1079 @return EFI_SECURITY_VIOLATION The variable does NOT pass the validation.
1080 check carried out by the firmware.
1081 @return EFI_SUCCESS Variable passed validation successfully.
1086 IN CHAR16
*VariableName
,
1087 IN EFI_GUID
*VendorGuid
,
1090 IN VARIABLE_POINTER_TRACK
*Variable
,
1091 IN UINT32 Attributes OPTIONAL
,
1100 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0 ||
1101 (Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) == 0) {
1103 // PK, KEK and db/dbx/dbt should set EFI_VARIABLE_NON_VOLATILE attribute and should be a time-based
1104 // authenticated variable.
1106 return EFI_INVALID_PARAMETER
;
1110 if ((InCustomMode() && UserPhysicalPresent()) || (mPlatformMode
== SETUP_MODE
&& !IsPk
)) {
1111 Payload
= (UINT8
*) Data
+ AUTHINFO2_SIZE (Data
);
1112 PayloadSize
= DataSize
- AUTHINFO2_SIZE (Data
);
1113 if (PayloadSize
== 0) {
1117 Status
= CheckSignatureListFormat(VariableName
, VendorGuid
, Payload
, PayloadSize
);
1118 if (EFI_ERROR (Status
)) {
1122 Status
= UpdateVariable (
1131 &((EFI_VARIABLE_AUTHENTICATION_2
*) Data
)->TimeStamp
1133 if (EFI_ERROR(Status
)) {
1137 if ((mPlatformMode
!= SETUP_MODE
) || IsPk
) {
1138 Status
= VendorKeyIsModified ();
1140 } else if (mPlatformMode
== USER_MODE
) {
1142 // Verify against X509 Cert in PK database.
1144 Status
= VerifyTimeBasedPayload (
1156 // Verify against the certificate in data payload.
1158 Status
= VerifyTimeBasedPayload (
1170 if (!EFI_ERROR(Status
) && IsPk
) {
1171 if (mPlatformMode
== SETUP_MODE
&& !Del
) {
1173 // If enroll PK in setup mode, need change to user mode.
1175 Status
= UpdatePlatformMode (USER_MODE
);
1176 } else if (mPlatformMode
== USER_MODE
&& Del
){
1178 // If delete PK in user mode, need change to setup mode.
1180 Status
= UpdatePlatformMode (SETUP_MODE
);
1188 Process variable with key exchange key for verification.
1190 Caution: This function may receive untrusted input.
1191 This function may be invoked in SMM mode, and datasize and data are external input.
1192 This function will do basic validation, before parse the data.
1193 This function will parse the authentication carefully to avoid security issues, like
1194 buffer overflow, integer overflow.
1195 This function will check attribute carefully to avoid authentication bypass.
1197 @param[in] VariableName Name of Variable to be found.
1198 @param[in] VendorGuid Variable vendor GUID.
1199 @param[in] Data Data pointer.
1200 @param[in] DataSize Size of Data found. If size is less than the
1201 data, this value contains the required size.
1202 @param[in] Variable The variable information which is used to keep track of variable usage.
1203 @param[in] Attributes Attribute value of the variable.
1205 @return EFI_INVALID_PARAMETER Invalid parameter.
1206 @return EFI_SECURITY_VIOLATION The variable does NOT pass the validation
1207 check carried out by the firmware.
1208 @return EFI_SUCCESS Variable pass validation successfully.
1213 IN CHAR16
*VariableName
,
1214 IN EFI_GUID
*VendorGuid
,
1217 IN VARIABLE_POINTER_TRACK
*Variable
,
1218 IN UINT32 Attributes OPTIONAL
1225 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0 ||
1226 (Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) == 0) {
1228 // DB, DBX and DBT should set EFI_VARIABLE_NON_VOLATILE attribute and should be a time-based
1229 // authenticated variable.
1231 return EFI_INVALID_PARAMETER
;
1234 Status
= EFI_SUCCESS
;
1235 if (mPlatformMode
== USER_MODE
&& !(InCustomMode() && UserPhysicalPresent())) {
1237 // Time-based, verify against X509 Cert KEK.
1239 return VerifyTimeBasedPayload (
1251 // If in setup mode or custom secure boot mode, no authentication needed.
1253 Payload
= (UINT8
*) Data
+ AUTHINFO2_SIZE (Data
);
1254 PayloadSize
= DataSize
- AUTHINFO2_SIZE (Data
);
1256 Status
= CheckSignatureListFormat(VariableName
, VendorGuid
, Payload
, PayloadSize
);
1257 if (EFI_ERROR (Status
)) {
1261 Status
= UpdateVariable (
1270 &((EFI_VARIABLE_AUTHENTICATION_2
*) Data
)->TimeStamp
1272 if (EFI_ERROR (Status
)) {
1276 if (mPlatformMode
!= SETUP_MODE
) {
1277 Status
= VendorKeyIsModified ();
1285 Process variable with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS/EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set
1287 Caution: This function may receive untrusted input.
1288 This function may be invoked in SMM mode, and datasize and data are external input.
1289 This function will do basic validation, before parse the data.
1290 This function will parse the authentication carefully to avoid security issues, like
1291 buffer overflow, integer overflow.
1292 This function will check attribute carefully to avoid authentication bypass.
1294 @param[in] VariableName Name of Variable to be found.
1295 @param[in] VendorGuid Variable vendor GUID.
1297 @param[in] Data Data pointer.
1298 @param[in] DataSize Size of Data found. If size is less than the
1299 data, this value contains the required size.
1300 @param[in] Variable The variable information which is used to keep track of variable usage.
1301 @param[in] Attributes Attribute value of the variable.
1303 @return EFI_INVALID_PARAMETER Invalid parameter.
1304 @return EFI_WRITE_PROTECTED Variable is write-protected and needs authentication with
1305 EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS set.
1306 @return EFI_OUT_OF_RESOURCES The Database to save the public key is full.
1307 @return EFI_SECURITY_VIOLATION The variable is with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
1308 set, but the AuthInfo does NOT pass the validation
1309 check carried out by the firmware.
1310 @return EFI_SUCCESS Variable is not write-protected or pass validation successfully.
1315 IN CHAR16
*VariableName
,
1316 IN EFI_GUID
*VendorGuid
,
1319 IN VARIABLE_POINTER_TRACK
*Variable
,
1320 IN UINT32 Attributes
1325 BOOLEAN IsFirstTime
;
1327 EFI_VARIABLE_AUTHENTICATION
*CertData
;
1328 EFI_CERT_BLOCK_RSA_2048_SHA256
*CertBlock
;
1330 UINT64 MonotonicCount
;
1331 VARIABLE_ENTRY_CONSISTENCY VariableDataEntry
;
1339 if (NeedPhysicallyPresent(VariableName
, VendorGuid
) && !UserPhysicalPresent()) {
1341 // This variable is protected, only physical present user could modify its value.
1343 return EFI_SECURITY_VIOLATION
;
1347 // A time-based authenticated variable and a count-based authenticated variable
1348 // can't be updated by each other.
1350 if (Variable
->CurrPtr
!= NULL
) {
1351 if (((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) &&
1352 ((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0)) {
1353 return EFI_SECURITY_VIOLATION
;
1356 if (((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) &&
1357 ((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0)) {
1358 return EFI_SECURITY_VIOLATION
;
1363 // Process Time-based Authenticated variable.
1365 if ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) {
1366 return VerifyTimeBasedPayload (
1379 // Determine if first time SetVariable with the EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS.
1381 if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) {
1383 // Determine current operation type.
1385 if (DataSize
== AUTHINFO_SIZE
) {
1389 // Determine whether this is the first time with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS set.
1391 if (Variable
->CurrPtr
== NULL
) {
1393 } else if ((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) == 0) {
1396 KeyIndex
= Variable
->CurrPtr
->PubKeyIndex
;
1397 IsFirstTime
= FALSE
;
1399 } else if ((Variable
->CurrPtr
!= NULL
) &&
1400 ((Variable
->CurrPtr
->Attributes
& (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
)) != 0)
1403 // If the variable is already write-protected, it always needs authentication before update.
1405 return EFI_WRITE_PROTECTED
;
1408 // If without EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS, set and attributes collision.
1409 // That means it is not authenticated variable, just update variable as usual.
1411 Status
= UpdateVariable (VariableName
, VendorGuid
, Data
, DataSize
, Attributes
, 0, 0, Variable
, NULL
);
1416 // Get PubKey and check Monotonic Count value corresponding to the variable.
1418 CertData
= (EFI_VARIABLE_AUTHENTICATION
*) Data
;
1419 CertBlock
= (EFI_CERT_BLOCK_RSA_2048_SHA256
*) (CertData
->AuthInfo
.CertData
);
1420 PubKey
= CertBlock
->PublicKey
;
1423 // Update Monotonic Count value.
1425 MonotonicCount
= CertData
->MonotonicCount
;
1429 // 2 cases need to check here
1430 // 1. Internal PubKey variable. PubKeyIndex is always 0
1431 // 2. Other counter-based AuthVariable. Check input PubKey.
1433 if (KeyIndex
== 0 || CompareMem (PubKey
, mPubKeyStore
+ (KeyIndex
- 1) * EFI_CERT_TYPE_RSA2048_SIZE
, EFI_CERT_TYPE_RSA2048_SIZE
) != 0) {
1434 return EFI_SECURITY_VIOLATION
;
1437 // Compare the current monotonic count and ensure that it is greater than the last SetVariable
1438 // operation with the EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS attribute set.
1440 if (CertData
->MonotonicCount
<= Variable
->CurrPtr
->MonotonicCount
) {
1442 // Monotonic count check fail, suspicious replay attack, return EFI_SECURITY_VIOLATION.
1444 return EFI_SECURITY_VIOLATION
;
1448 // Verify the certificate in Data payload.
1450 Status
= VerifyCounterBasedPayload (Data
, DataSize
, PubKey
);
1451 if (EFI_ERROR (Status
)) {
1456 // Now, the signature has been verified!
1458 if (IsFirstTime
&& !IsDeletion
) {
1459 VariableDataEntry
.VariableSize
= DataSize
- AUTHINFO_SIZE
;
1460 VariableDataEntry
.Guid
= VendorGuid
;
1461 VariableDataEntry
.Name
= VariableName
;
1464 // Update public key database variable if need.
1466 KeyIndex
= AddPubKeyInStore (PubKey
, &VariableDataEntry
);
1467 if (KeyIndex
== 0) {
1468 return EFI_OUT_OF_RESOURCES
;
1473 // Verification pass.
1475 return UpdateVariable (VariableName
, VendorGuid
, (UINT8
*)Data
+ AUTHINFO_SIZE
, DataSize
- AUTHINFO_SIZE
, Attributes
, KeyIndex
, MonotonicCount
, Variable
, NULL
);
1479 Merge two buffers which formatted as EFI_SIGNATURE_LIST. Only the new EFI_SIGNATURE_DATA
1480 will be appended to the original EFI_SIGNATURE_LIST, duplicate EFI_SIGNATURE_DATA
1483 @param[in, out] Data Pointer to original EFI_SIGNATURE_LIST.
1484 @param[in] DataSize Size of Data buffer.
1485 @param[in] FreeBufSize Size of free data buffer
1486 @param[in] NewData Pointer to new EFI_SIGNATURE_LIST to be appended.
1487 @param[in] NewDataSize Size of NewData buffer.
1488 @param[out] MergedBufSize Size of the merged buffer
1490 @return EFI_BUFFER_TOO_SMALL if input Data buffer overflowed
1494 AppendSignatureList (
1497 IN UINTN FreeBufSize
,
1499 IN UINTN NewDataSize
,
1500 OUT UINTN
*MergedBufSize
1503 EFI_SIGNATURE_LIST
*CertList
;
1504 EFI_SIGNATURE_DATA
*Cert
;
1506 EFI_SIGNATURE_LIST
*NewCertList
;
1507 EFI_SIGNATURE_DATA
*NewCert
;
1514 UINTN SignatureListSize
;
1517 Tail
= (UINT8
*) Data
+ DataSize
;
1519 NewCertList
= (EFI_SIGNATURE_LIST
*) NewData
;
1520 while ((NewDataSize
> 0) && (NewDataSize
>= NewCertList
->SignatureListSize
)) {
1521 NewCert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) NewCertList
+ sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
);
1522 NewCertCount
= (NewCertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - NewCertList
->SignatureHeaderSize
) / NewCertList
->SignatureSize
;
1525 for (Index
= 0; Index
< NewCertCount
; Index
++) {
1529 CertList
= (EFI_SIGNATURE_LIST
*) Data
;
1530 while ((Size
> 0) && (Size
>= CertList
->SignatureListSize
)) {
1531 if (CompareGuid (&CertList
->SignatureType
, &NewCertList
->SignatureType
) &&
1532 (CertList
->SignatureSize
== NewCertList
->SignatureSize
)) {
1533 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
1534 CertCount
= (CertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - CertList
->SignatureHeaderSize
) / CertList
->SignatureSize
;
1535 for (Index2
= 0; Index2
< CertCount
; Index2
++) {
1537 // Iterate each Signature Data in this Signature List.
1539 if (CompareMem (NewCert
, Cert
, CertList
->SignatureSize
) == 0) {
1543 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) Cert
+ CertList
->SignatureSize
);
1550 Size
-= CertList
->SignatureListSize
;
1551 CertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) CertList
+ CertList
->SignatureListSize
);
1556 // New EFI_SIGNATURE_DATA, append it.
1558 if (CopiedCount
== 0) {
1559 if (FreeBufSize
< sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
) {
1560 return EFI_BUFFER_TOO_SMALL
;
1564 // Copy EFI_SIGNATURE_LIST header for only once.
1567 CopyMem (Tail
, NewCertList
, sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
);
1568 Tail
= Tail
+ sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
;
1569 FreeBufSize
-= sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
;
1572 if (FreeBufSize
< NewCertList
->SignatureSize
) {
1573 return EFI_BUFFER_TOO_SMALL
;
1575 CopyMem (Tail
, NewCert
, NewCertList
->SignatureSize
);
1576 Tail
+= NewCertList
->SignatureSize
;
1577 FreeBufSize
-= NewCertList
->SignatureSize
;
1581 NewCert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) NewCert
+ NewCertList
->SignatureSize
);
1585 // Update SignatureListSize in newly appended EFI_SIGNATURE_LIST.
1587 if (CopiedCount
!= 0) {
1588 SignatureListSize
= sizeof (EFI_SIGNATURE_LIST
) + NewCertList
->SignatureHeaderSize
+ (CopiedCount
* NewCertList
->SignatureSize
);
1589 CertList
= (EFI_SIGNATURE_LIST
*) (Tail
- SignatureListSize
);
1590 CertList
->SignatureListSize
= (UINT32
) SignatureListSize
;
1593 NewDataSize
-= NewCertList
->SignatureListSize
;
1594 NewCertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) NewCertList
+ NewCertList
->SignatureListSize
);
1597 *MergedBufSize
= (Tail
- (UINT8
*) Data
);
1602 Compare two EFI_TIME data.
1605 @param FirstTime A pointer to the first EFI_TIME data.
1606 @param SecondTime A pointer to the second EFI_TIME data.
1608 @retval TRUE The FirstTime is not later than the SecondTime.
1609 @retval FALSE The FirstTime is later than the SecondTime.
1614 IN EFI_TIME
*FirstTime
,
1615 IN EFI_TIME
*SecondTime
1618 if (FirstTime
->Year
!= SecondTime
->Year
) {
1619 return (BOOLEAN
) (FirstTime
->Year
< SecondTime
->Year
);
1620 } else if (FirstTime
->Month
!= SecondTime
->Month
) {
1621 return (BOOLEAN
) (FirstTime
->Month
< SecondTime
->Month
);
1622 } else if (FirstTime
->Day
!= SecondTime
->Day
) {
1623 return (BOOLEAN
) (FirstTime
->Day
< SecondTime
->Day
);
1624 } else if (FirstTime
->Hour
!= SecondTime
->Hour
) {
1625 return (BOOLEAN
) (FirstTime
->Hour
< SecondTime
->Hour
);
1626 } else if (FirstTime
->Minute
!= SecondTime
->Minute
) {
1627 return (BOOLEAN
) (FirstTime
->Minute
< SecondTime
->Minute
);
1630 return (BOOLEAN
) (FirstTime
->Second
<= SecondTime
->Second
);
1634 Find matching signer's certificates for common authenticated variable
1635 by corresponding VariableName and VendorGuid from "certdb".
1637 The data format of "certdb":
1639 // UINT32 CertDbListSize;
1640 // /// AUTH_CERT_DB_DATA Certs1[];
1641 // /// AUTH_CERT_DB_DATA Certs2[];
1643 // /// AUTH_CERT_DB_DATA Certsn[];
1646 @param[in] VariableName Name of authenticated Variable.
1647 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1648 @param[in] Data Pointer to variable "certdb".
1649 @param[in] DataSize Size of variable "certdb".
1650 @param[out] CertOffset Offset of matching CertData, from starting of Data.
1651 @param[out] CertDataSize Length of CertData in bytes.
1652 @param[out] CertNodeOffset Offset of matching AUTH_CERT_DB_DATA , from
1654 @param[out] CertNodeSize Length of AUTH_CERT_DB_DATA in bytes.
1656 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1657 @retval EFI_NOT_FOUND Fail to find matching certs.
1658 @retval EFI_SUCCESS Find matching certs and output parameters.
1663 IN CHAR16
*VariableName
,
1664 IN EFI_GUID
*VendorGuid
,
1667 OUT UINT32
*CertOffset
, OPTIONAL
1668 OUT UINT32
*CertDataSize
, OPTIONAL
1669 OUT UINT32
*CertNodeOffset
,OPTIONAL
1670 OUT UINT32
*CertNodeSize OPTIONAL
1674 AUTH_CERT_DB_DATA
*Ptr
;
1678 UINT32 CertDbListSize
;
1680 if ((VariableName
== NULL
) || (VendorGuid
== NULL
) || (Data
== NULL
)) {
1681 return EFI_INVALID_PARAMETER
;
1685 // Check whether DataSize matches recorded CertDbListSize.
1687 if (DataSize
< sizeof (UINT32
)) {
1688 return EFI_INVALID_PARAMETER
;
1691 CertDbListSize
= ReadUnaligned32 ((UINT32
*) Data
);
1693 if (CertDbListSize
!= (UINT32
) DataSize
) {
1694 return EFI_INVALID_PARAMETER
;
1697 Offset
= sizeof (UINT32
);
1700 // Get corresponding certificates by VendorGuid and VariableName.
1702 while (Offset
< (UINT32
) DataSize
) {
1703 Ptr
= (AUTH_CERT_DB_DATA
*) (Data
+ Offset
);
1705 // Check whether VendorGuid matches.
1707 if (CompareGuid (&Ptr
->VendorGuid
, VendorGuid
)) {
1708 NodeSize
= ReadUnaligned32 (&Ptr
->CertNodeSize
);
1709 NameSize
= ReadUnaligned32 (&Ptr
->NameSize
);
1710 CertSize
= ReadUnaligned32 (&Ptr
->CertDataSize
);
1712 if (NodeSize
!= sizeof (EFI_GUID
) + sizeof (UINT32
) * 3 + CertSize
+
1713 sizeof (CHAR16
) * NameSize
) {
1714 return EFI_INVALID_PARAMETER
;
1717 Offset
= Offset
+ sizeof (EFI_GUID
) + sizeof (UINT32
) * 3;
1719 // Check whether VariableName matches.
1721 if ((NameSize
== StrLen (VariableName
)) &&
1722 (CompareMem (Data
+ Offset
, VariableName
, NameSize
* sizeof (CHAR16
)) == 0)) {
1723 Offset
= Offset
+ NameSize
* sizeof (CHAR16
);
1725 if (CertOffset
!= NULL
) {
1726 *CertOffset
= Offset
;
1729 if (CertDataSize
!= NULL
) {
1730 *CertDataSize
= CertSize
;
1733 if (CertNodeOffset
!= NULL
) {
1734 *CertNodeOffset
= (UINT32
) ((UINT8
*) Ptr
- Data
);
1737 if (CertNodeSize
!= NULL
) {
1738 *CertNodeSize
= NodeSize
;
1743 Offset
= Offset
+ NameSize
* sizeof (CHAR16
) + CertSize
;
1746 NodeSize
= ReadUnaligned32 (&Ptr
->CertNodeSize
);
1747 Offset
= Offset
+ NodeSize
;
1751 return EFI_NOT_FOUND
;
1755 Retrieve signer's certificates for common authenticated variable
1756 by corresponding VariableName and VendorGuid from "certdb".
1758 @param[in] VariableName Name of authenticated Variable.
1759 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1760 @param[out] CertData Pointer to signer's certificates.
1761 @param[out] CertDataSize Length of CertData in bytes.
1763 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1764 @retval EFI_NOT_FOUND Fail to find "certdb" or matching certs.
1765 @retval EFI_SUCCESS Get signer's certificates successfully.
1770 IN CHAR16
*VariableName
,
1771 IN EFI_GUID
*VendorGuid
,
1772 OUT UINT8
**CertData
,
1773 OUT UINT32
*CertDataSize
1776 VARIABLE_POINTER_TRACK CertDbVariable
;
1782 if ((VariableName
== NULL
) || (VendorGuid
== NULL
) || (CertData
== NULL
) || (CertDataSize
== NULL
)) {
1783 return EFI_INVALID_PARAMETER
;
1787 // Get variable "certdb".
1789 Status
= FindVariable (
1793 &mVariableModuleGlobal
->VariableGlobal
,
1796 if (EFI_ERROR (Status
)) {
1800 DataSize
= DataSizeOfVariable (CertDbVariable
.CurrPtr
);
1801 Data
= GetVariableDataPtr (CertDbVariable
.CurrPtr
);
1802 if ((DataSize
== 0) || (Data
== NULL
)) {
1804 return EFI_NOT_FOUND
;
1807 Status
= FindCertsFromDb (
1818 if (EFI_ERROR (Status
)) {
1822 *CertData
= Data
+ CertOffset
;
1827 Delete matching signer's certificates when deleting common authenticated
1828 variable by corresponding VariableName and VendorGuid from "certdb".
1830 @param[in] VariableName Name of authenticated Variable.
1831 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1833 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1834 @retval EFI_NOT_FOUND Fail to find "certdb" or matching certs.
1835 @retval EFI_OUT_OF_RESOURCES The operation is failed due to lack of resources.
1836 @retval EFI_SUCCESS The operation is completed successfully.
1841 IN CHAR16
*VariableName
,
1842 IN EFI_GUID
*VendorGuid
1845 VARIABLE_POINTER_TRACK CertDbVariable
;
1850 UINT32 CertNodeOffset
;
1851 UINT32 CertNodeSize
;
1853 UINT32 NewCertDbSize
;
1855 if ((VariableName
== NULL
) || (VendorGuid
== NULL
)) {
1856 return EFI_INVALID_PARAMETER
;
1860 // Get variable "certdb".
1862 Status
= FindVariable (
1866 &mVariableModuleGlobal
->VariableGlobal
,
1869 if (EFI_ERROR (Status
)) {
1873 DataSize
= DataSizeOfVariable (CertDbVariable
.CurrPtr
);
1874 Data
= GetVariableDataPtr (CertDbVariable
.CurrPtr
);
1875 if ((DataSize
== 0) || (Data
== NULL
)) {
1877 return EFI_NOT_FOUND
;
1880 if (DataSize
== sizeof (UINT32
)) {
1882 // There is no certs in certdb.
1888 // Get corresponding cert node from certdb.
1890 Status
= FindCertsFromDb (
1901 if (EFI_ERROR (Status
)) {
1905 if (DataSize
< (CertNodeOffset
+ CertNodeSize
)) {
1906 return EFI_NOT_FOUND
;
1910 // Construct new data content of variable "certdb".
1912 NewCertDbSize
= (UINT32
) DataSize
- CertNodeSize
;
1913 NewCertDb
= (UINT8
*) mCertDbStore
;
1916 // Copy the DB entries before deleting node.
1918 CopyMem (NewCertDb
, Data
, CertNodeOffset
);
1920 // Update CertDbListSize.
1922 CopyMem (NewCertDb
, &NewCertDbSize
, sizeof (UINT32
));
1924 // Copy the DB entries after deleting node.
1926 if (DataSize
> (CertNodeOffset
+ CertNodeSize
)) {
1928 NewCertDb
+ CertNodeOffset
,
1929 Data
+ CertNodeOffset
+ CertNodeSize
,
1930 DataSize
- CertNodeOffset
- CertNodeSize
1937 VarAttr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
1938 Status
= UpdateVariable (
1954 Insert signer's certificates for common authenticated variable with VariableName
1955 and VendorGuid in AUTH_CERT_DB_DATA to "certdb".
1957 @param[in] VariableName Name of authenticated Variable.
1958 @param[in] VendorGuid Vendor GUID of authenticated Variable.
1959 @param[in] CertData Pointer to signer's certificates.
1960 @param[in] CertDataSize Length of CertData in bytes.
1962 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1963 @retval EFI_ACCESS_DENIED An AUTH_CERT_DB_DATA entry with same VariableName
1964 and VendorGuid already exists.
1965 @retval EFI_OUT_OF_RESOURCES The operation is failed due to lack of resources.
1966 @retval EFI_SUCCESS Insert an AUTH_CERT_DB_DATA entry to "certdb"
1971 IN CHAR16
*VariableName
,
1972 IN EFI_GUID
*VendorGuid
,
1974 IN UINTN CertDataSize
1977 VARIABLE_POINTER_TRACK CertDbVariable
;
1983 UINT32 NewCertDbSize
;
1984 UINT32 CertNodeSize
;
1986 AUTH_CERT_DB_DATA
*Ptr
;
1988 if ((VariableName
== NULL
) || (VendorGuid
== NULL
) || (CertData
== NULL
)) {
1989 return EFI_INVALID_PARAMETER
;
1993 // Get variable "certdb".
1995 Status
= FindVariable (
1999 &mVariableModuleGlobal
->VariableGlobal
,
2002 if (EFI_ERROR (Status
)) {
2006 DataSize
= DataSizeOfVariable (CertDbVariable
.CurrPtr
);
2007 Data
= GetVariableDataPtr (CertDbVariable
.CurrPtr
);
2008 if ((DataSize
== 0) || (Data
== NULL
)) {
2010 return EFI_NOT_FOUND
;
2014 // Find whether matching cert node already exists in "certdb".
2015 // If yes return error.
2017 Status
= FindCertsFromDb (
2028 if (!EFI_ERROR (Status
)) {
2030 return EFI_ACCESS_DENIED
;
2034 // Construct new data content of variable "certdb".
2036 NameSize
= (UINT32
) StrLen (VariableName
);
2037 CertNodeSize
= sizeof (AUTH_CERT_DB_DATA
) + (UINT32
) CertDataSize
+ NameSize
* sizeof (CHAR16
);
2038 NewCertDbSize
= (UINT32
) DataSize
+ CertNodeSize
;
2039 if (NewCertDbSize
> mMaxCertDbSize
) {
2040 return EFI_OUT_OF_RESOURCES
;
2042 NewCertDb
= (UINT8
*) mCertDbStore
;
2045 // Copy the DB entries before deleting node.
2047 CopyMem (NewCertDb
, Data
, DataSize
);
2049 // Update CertDbListSize.
2051 CopyMem (NewCertDb
, &NewCertDbSize
, sizeof (UINT32
));
2053 // Construct new cert node.
2055 Ptr
= (AUTH_CERT_DB_DATA
*) (NewCertDb
+ DataSize
);
2056 CopyGuid (&Ptr
->VendorGuid
, VendorGuid
);
2057 CopyMem (&Ptr
->CertNodeSize
, &CertNodeSize
, sizeof (UINT32
));
2058 CopyMem (&Ptr
->NameSize
, &NameSize
, sizeof (UINT32
));
2059 CopyMem (&Ptr
->CertDataSize
, &CertDataSize
, sizeof (UINT32
));
2062 (UINT8
*) Ptr
+ sizeof (AUTH_CERT_DB_DATA
),
2064 NameSize
* sizeof (CHAR16
)
2068 (UINT8
*) Ptr
+ sizeof (AUTH_CERT_DB_DATA
) + NameSize
* sizeof (CHAR16
),
2076 VarAttr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
2077 Status
= UpdateVariable (
2093 Process variable with EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set
2095 Caution: This function may receive untrusted input.
2096 This function may be invoked in SMM mode, and datasize and data are external input.
2097 This function will do basic validation, before parse the data.
2098 This function will parse the authentication carefully to avoid security issues, like
2099 buffer overflow, integer overflow.
2101 @param[in] VariableName Name of Variable to be found.
2102 @param[in] VendorGuid Variable vendor GUID.
2103 @param[in] Data Data pointer.
2104 @param[in] DataSize Size of Data found. If size is less than the
2105 data, this value contains the required size.
2106 @param[in] Variable The variable information which is used to keep track of variable usage.
2107 @param[in] Attributes Attribute value of the variable.
2108 @param[in] AuthVarType Verify against PK, KEK database, private database or certificate in data payload.
2109 @param[out] VarDel Delete the variable or not.
2111 @retval EFI_INVALID_PARAMETER Invalid parameter.
2112 @retval EFI_SECURITY_VIOLATION The variable does NOT pass the validation
2113 check carried out by the firmware.
2114 @retval EFI_OUT_OF_RESOURCES Failed to process variable due to lack
2116 @retval EFI_SUCCESS Variable pass validation successfully.
2120 VerifyTimeBasedPayload (
2121 IN CHAR16
*VariableName
,
2122 IN EFI_GUID
*VendorGuid
,
2125 IN VARIABLE_POINTER_TRACK
*Variable
,
2126 IN UINT32 Attributes
,
2127 IN AUTHVAR_TYPE AuthVarType
,
2141 BOOLEAN VerifyStatus
;
2143 EFI_SIGNATURE_LIST
*CertList
;
2144 EFI_SIGNATURE_DATA
*Cert
;
2145 VARIABLE_POINTER_TRACK KekVariable
;
2146 EFI_VARIABLE_AUTHENTICATION_2
*CertData
;
2149 VARIABLE_POINTER_TRACK PkVariable
;
2154 UINTN CertStackSize
;
2155 UINT8
*CertsInCertDb
;
2156 UINT32 CertsSizeinDb
;
2158 VerifyStatus
= FALSE
;
2165 CertsInCertDb
= NULL
;
2168 // When the attribute EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS is
2169 // set, then the Data buffer shall begin with an instance of a complete (and serialized)
2170 // EFI_VARIABLE_AUTHENTICATION_2 descriptor. The descriptor shall be followed by the new
2171 // variable value and DataSize shall reflect the combined size of the descriptor and the new
2172 // variable value. The authentication descriptor is not part of the variable data and is not
2173 // returned by subsequent calls to GetVariable().
2175 CertData
= (EFI_VARIABLE_AUTHENTICATION_2
*) Data
;
2178 // Verify that Pad1, Nanosecond, TimeZone, Daylight and Pad2 components of the
2179 // TimeStamp value are set to zero.
2181 if ((CertData
->TimeStamp
.Pad1
!= 0) ||
2182 (CertData
->TimeStamp
.Nanosecond
!= 0) ||
2183 (CertData
->TimeStamp
.TimeZone
!= 0) ||
2184 (CertData
->TimeStamp
.Daylight
!= 0) ||
2185 (CertData
->TimeStamp
.Pad2
!= 0)) {
2186 return EFI_SECURITY_VIOLATION
;
2189 if ((Variable
->CurrPtr
!= NULL
) && ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0)) {
2190 if (CompareTimeStamp (&CertData
->TimeStamp
, &Variable
->CurrPtr
->TimeStamp
)) {
2192 // TimeStamp check fail, suspicious replay attack, return EFI_SECURITY_VIOLATION.
2194 return EFI_SECURITY_VIOLATION
;
2199 // wCertificateType should be WIN_CERT_TYPE_EFI_GUID.
2200 // Cert type should be EFI_CERT_TYPE_PKCS7_GUID.
2202 if ((CertData
->AuthInfo
.Hdr
.wCertificateType
!= WIN_CERT_TYPE_EFI_GUID
) ||
2203 !CompareGuid (&CertData
->AuthInfo
.CertType
, &gEfiCertPkcs7Guid
)) {
2205 // Invalid AuthInfo type, return EFI_SECURITY_VIOLATION.
2207 return EFI_SECURITY_VIOLATION
;
2211 // Find out Pkcs7 SignedData which follows the EFI_VARIABLE_AUTHENTICATION_2 descriptor.
2212 // AuthInfo.Hdr.dwLength is the length of the entire certificate, including the length of the header.
2214 SigData
= CertData
->AuthInfo
.CertData
;
2215 SigDataSize
= CertData
->AuthInfo
.Hdr
.dwLength
- (UINT32
) (OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID
, CertData
));
2218 // Find out the new data payload which follows Pkcs7 SignedData directly.
2220 PayloadPtr
= SigData
+ SigDataSize
;
2221 PayloadSize
= DataSize
- OFFSET_OF_AUTHINFO2_CERT_DATA
- (UINTN
) SigDataSize
;
2224 // Construct a buffer to fill with (VariableName, VendorGuid, Attributes, TimeStamp, Data).
2226 NewDataSize
= PayloadSize
+ sizeof (EFI_TIME
) + sizeof (UINT32
) +
2227 sizeof (EFI_GUID
) + StrSize (VariableName
) - sizeof (CHAR16
);
2228 NewData
= mSerializationRuntimeBuffer
;
2231 Length
= StrLen (VariableName
) * sizeof (CHAR16
);
2232 CopyMem (Buffer
, VariableName
, Length
);
2235 Length
= sizeof (EFI_GUID
);
2236 CopyMem (Buffer
, VendorGuid
, Length
);
2239 Length
= sizeof (UINT32
);
2240 CopyMem (Buffer
, &Attr
, Length
);
2243 Length
= sizeof (EFI_TIME
);
2244 CopyMem (Buffer
, &CertData
->TimeStamp
, Length
);
2247 CopyMem (Buffer
, PayloadPtr
, PayloadSize
);
2249 if (AuthVarType
== AuthVarTypePk
) {
2251 // Verify that the signature has been made with the current Platform Key (no chaining for PK).
2252 // First, get signer's certificates from SignedData.
2254 VerifyStatus
= Pkcs7GetSigners (
2262 if (!VerifyStatus
) {
2267 // Second, get the current platform key from variable. Check whether it's identical with signer's certificates
2268 // in SignedData. If not, return error immediately.
2270 Status
= FindVariable (
2271 EFI_PLATFORM_KEY_NAME
,
2272 &gEfiGlobalVariableGuid
,
2274 &mVariableModuleGlobal
->VariableGlobal
,
2277 if (EFI_ERROR (Status
)) {
2278 VerifyStatus
= FALSE
;
2281 CertList
= (EFI_SIGNATURE_LIST
*) GetVariableDataPtr (PkVariable
.CurrPtr
);
2282 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
2283 if ((RootCertSize
!= (CertList
->SignatureSize
- (sizeof (EFI_SIGNATURE_DATA
) - 1))) ||
2284 (CompareMem (Cert
->SignatureData
, RootCert
, RootCertSize
) != 0)) {
2285 VerifyStatus
= FALSE
;
2290 // Verify Pkcs7 SignedData via Pkcs7Verify library.
2292 VerifyStatus
= Pkcs7Verify (
2301 } else if (AuthVarType
== AuthVarTypeKek
) {
2304 // Get KEK database from variable.
2306 Status
= FindVariable (
2307 EFI_KEY_EXCHANGE_KEY_NAME
,
2308 &gEfiGlobalVariableGuid
,
2310 &mVariableModuleGlobal
->VariableGlobal
,
2313 if (EFI_ERROR (Status
)) {
2318 // Ready to verify Pkcs7 SignedData. Go through KEK Signature Database to find out X.509 CertList.
2320 KekDataSize
= KekVariable
.CurrPtr
->DataSize
;
2321 CertList
= (EFI_SIGNATURE_LIST
*) GetVariableDataPtr (KekVariable
.CurrPtr
);
2322 while ((KekDataSize
> 0) && (KekDataSize
>= CertList
->SignatureListSize
)) {
2323 if (CompareGuid (&CertList
->SignatureType
, &gEfiCertX509Guid
)) {
2324 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
2325 CertCount
= (CertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - CertList
->SignatureHeaderSize
) / CertList
->SignatureSize
;
2326 for (Index
= 0; Index
< CertCount
; Index
++) {
2328 // Iterate each Signature Data Node within this CertList for a verify
2330 RootCert
= Cert
->SignatureData
;
2331 RootCertSize
= CertList
->SignatureSize
- (sizeof (EFI_SIGNATURE_DATA
) - 1);
2334 // Verify Pkcs7 SignedData via Pkcs7Verify library.
2336 VerifyStatus
= Pkcs7Verify (
2347 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) Cert
+ CertList
->SignatureSize
);
2350 KekDataSize
-= CertList
->SignatureListSize
;
2351 CertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) CertList
+ CertList
->SignatureListSize
);
2353 } else if (AuthVarType
== AuthVarTypePriv
) {
2356 // Process common authenticated variable except PK/KEK/DB/DBX/DBT.
2357 // Get signer's certificates from SignedData.
2359 VerifyStatus
= Pkcs7GetSigners (
2367 if (!VerifyStatus
) {
2372 // Get previously stored signer's certificates from certdb for existing
2373 // variable. Check whether they are identical with signer's certificates
2374 // in SignedData. If not, return error immediately.
2376 if ((Variable
->CurrPtr
!= NULL
)) {
2377 VerifyStatus
= FALSE
;
2379 Status
= GetCertsFromDb (VariableName
, VendorGuid
, &CertsInCertDb
, &CertsSizeinDb
);
2380 if (EFI_ERROR (Status
)) {
2384 if ((CertStackSize
!= CertsSizeinDb
) ||
2385 (CompareMem (SignerCerts
, CertsInCertDb
, CertsSizeinDb
) != 0)) {
2390 VerifyStatus
= Pkcs7Verify (
2398 if (!VerifyStatus
) {
2403 // Delete signer's certificates when delete the common authenticated variable.
2405 if ((PayloadSize
== 0) && (Variable
->CurrPtr
!= NULL
) && ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0)) {
2406 Status
= DeleteCertsFromDb (VariableName
, VendorGuid
);
2407 if (EFI_ERROR (Status
)) {
2408 VerifyStatus
= FALSE
;
2411 } else if (Variable
->CurrPtr
== NULL
&& PayloadSize
!= 0) {
2413 // Insert signer's certificates when adding a new common authenticated variable.
2415 Status
= InsertCertsToDb (VariableName
, VendorGuid
, SignerCerts
, CertStackSize
);
2416 if (EFI_ERROR (Status
)) {
2417 VerifyStatus
= FALSE
;
2421 } else if (AuthVarType
== AuthVarTypePayload
) {
2422 CertList
= (EFI_SIGNATURE_LIST
*) PayloadPtr
;
2423 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
2424 RootCert
= Cert
->SignatureData
;
2425 RootCertSize
= CertList
->SignatureSize
- (sizeof (EFI_SIGNATURE_DATA
) - 1);
2427 // Verify Pkcs7 SignedData via Pkcs7Verify library.
2429 VerifyStatus
= Pkcs7Verify (
2438 return EFI_SECURITY_VIOLATION
;
2443 if (AuthVarType
== AuthVarTypePk
|| AuthVarType
== AuthVarTypePriv
) {
2444 Pkcs7FreeSigners (RootCert
);
2445 Pkcs7FreeSigners (SignerCerts
);
2448 if (!VerifyStatus
) {
2449 return EFI_SECURITY_VIOLATION
;
2452 Status
= CheckSignatureListFormat(VariableName
, VendorGuid
, PayloadPtr
, PayloadSize
);
2453 if (EFI_ERROR (Status
)) {
2457 if ((PayloadSize
== 0) && (VarDel
!= NULL
)) {
2462 // Final step: Update/Append Variable if it pass Pkcs7Verify
2464 return UpdateVariable (
2473 &CertData
->TimeStamp