2 Implement authentication services for the authenticated variables.
4 Caution: This module requires additional review when modified.
5 This driver will have external input - variable data. It may be input in SMM mode.
6 This external input must be validated carefully to avoid security issue like
7 buffer overflow, integer overflow.
8 Variable attribute should also be checked to avoid authentication bypass.
9 The whole SMM authentication variable design relies on the integrity of flash part and SMM.
10 which is assumed to be protected by platform. All variable code and metadata in flash/SMM Memory
11 may not be modified without authorization. If platform fails to protect these resources,
12 the authentication service provided in this driver will be broken, and the behavior is undefined.
14 Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
15 This program and the accompanying materials
16 are licensed and made available under the terms and conditions of the BSD License
17 which accompanies this distribution. The full text of the license may be found at
18 http://opensource.org/licenses/bsd-license.php
20 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
21 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
25 #include "AuthServiceInternal.h"
28 /// Global database array for scratch
35 UINT32 mMaxCertDbSize
;
37 UINT8 mVendorKeyState
;
39 EFI_GUID mSignatureSupport
[] = {EFI_CERT_SHA1_GUID
, EFI_CERT_SHA256_GUID
, EFI_CERT_RSA2048_GUID
, EFI_CERT_X509_GUID
};
42 // Hash context pointer
44 VOID
*mHashCtx
= NULL
;
46 VARIABLE_ENTRY_PROPERTY mAuthVarEntry
[] = {
48 &gEfiSecureBootEnableDisableGuid
,
49 EFI_SECURE_BOOT_ENABLE_NAME
,
51 VAR_CHECK_VARIABLE_PROPERTY_REVISION
,
53 VARIABLE_ATTRIBUTE_NV_BS
,
59 &gEfiCustomModeEnableGuid
,
62 VAR_CHECK_VARIABLE_PROPERTY_REVISION
,
64 VARIABLE_ATTRIBUTE_NV_BS
,
70 &gEfiVendorKeysNvGuid
,
71 EFI_VENDOR_KEYS_NV_VARIABLE_NAME
,
73 VAR_CHECK_VARIABLE_PROPERTY_REVISION
,
74 VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY
,
75 VARIABLE_ATTRIBUTE_NV_BS_RT_AT
,
81 &gEfiAuthenticatedVariableGuid
,
84 VAR_CHECK_VARIABLE_PROPERTY_REVISION
,
85 VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY
,
86 VARIABLE_ATTRIBUTE_NV_BS_RT_AW
,
95 VAR_CHECK_VARIABLE_PROPERTY_REVISION
,
96 VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY
,
97 VARIABLE_ATTRIBUTE_NV_BS_RT_AT
,
104 VOID
**mAuthVarAddressPointer
[10];
106 AUTH_VAR_LIB_CONTEXT_IN
*mAuthVarLibContextIn
= NULL
;
109 Initialization for authenticated varibale services.
110 If this initialization returns error status, other APIs will not work
111 and expect to be not called then.
113 @param[in] AuthVarLibContextIn Pointer to input auth variable lib context.
114 @param[out] AuthVarLibContextOut Pointer to output auth variable lib context.
116 @retval EFI_SUCCESS Function successfully executed.
117 @retval EFI_INVALID_PARAMETER If AuthVarLibContextIn == NULL or AuthVarLibContextOut == NULL.
118 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough resource.
119 @retval EFI_UNSUPPORTED Unsupported to process authenticated variable.
124 AuthVariableLibInitialize (
125 IN AUTH_VAR_LIB_CONTEXT_IN
*AuthVarLibContextIn
,
126 OUT AUTH_VAR_LIB_CONTEXT_OUT
*AuthVarLibContextOut
135 UINT8 SecureBootMode
;
136 UINT8 SecureBootEnable
;
140 if ((AuthVarLibContextIn
== NULL
) || (AuthVarLibContextOut
== NULL
)) {
141 return EFI_INVALID_PARAMETER
;
144 mAuthVarLibContextIn
= AuthVarLibContextIn
;
147 // Initialize hash context.
149 CtxSize
= Sha256GetContextSize ();
150 mHashCtx
= AllocateRuntimePool (CtxSize
);
151 if (mHashCtx
== NULL
) {
152 return EFI_OUT_OF_RESOURCES
;
156 // Reserve runtime buffer for public key database. The size excludes variable header and name size.
158 mMaxKeyDbSize
= (UINT32
) (mAuthVarLibContextIn
->MaxAuthVariableSize
- sizeof (AUTHVAR_KEYDB_NAME
));
159 mMaxKeyNumber
= mMaxKeyDbSize
/ sizeof (AUTHVAR_KEY_DB_DATA
);
160 mPubKeyStore
= AllocateRuntimePool (mMaxKeyDbSize
);
161 if (mPubKeyStore
== NULL
) {
162 return EFI_OUT_OF_RESOURCES
;
166 // Reserve runtime buffer for certificate database. The size excludes variable header and name size.
168 mMaxCertDbSize
= (UINT32
) (mAuthVarLibContextIn
->MaxAuthVariableSize
- sizeof (EFI_CERT_DB_NAME
));
169 mCertDbStore
= AllocateRuntimePool (mMaxCertDbSize
);
170 if (mCertDbStore
== NULL
) {
171 return EFI_OUT_OF_RESOURCES
;
175 // Check "AuthVarKeyDatabase" variable's existence.
176 // If it doesn't exist, create a new one with initial value of 0 and EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS set.
178 Status
= AuthServiceInternalFindVariable (
180 &gEfiAuthenticatedVariableGuid
,
184 if (EFI_ERROR (Status
)) {
185 VarAttr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
;
188 Status
= AuthServiceInternalUpdateVariable (
190 &gEfiAuthenticatedVariableGuid
,
195 if (EFI_ERROR (Status
)) {
200 // Load database in global variable for cache.
202 ASSERT ((DataSize
!= 0) && (Data
!= NULL
));
204 // "AuthVarKeyDatabase" is an internal variable. Its DataSize is always ensured not to exceed mPubKeyStore buffer size(See definition before)
205 // Therefore, there is no memory overflow in underlying CopyMem.
207 CopyMem (mPubKeyStore
, (UINT8
*) Data
, DataSize
);
208 mPubKeyNumber
= (UINT32
) (DataSize
/ sizeof (AUTHVAR_KEY_DB_DATA
));
211 Status
= AuthServiceInternalFindVariable (EFI_PLATFORM_KEY_NAME
, &gEfiGlobalVariableGuid
, (VOID
**) &Data
, &DataSize
);
212 if (EFI_ERROR (Status
)) {
213 DEBUG ((EFI_D_INFO
, "Variable %s does not exist.\n", EFI_PLATFORM_KEY_NAME
));
215 DEBUG ((EFI_D_INFO
, "Variable %s exists.\n", EFI_PLATFORM_KEY_NAME
));
219 // Create "SetupMode" variable with BS+RT attribute set.
221 if (EFI_ERROR (Status
)) {
222 mPlatformMode
= SETUP_MODE
;
224 mPlatformMode
= USER_MODE
;
226 Status
= AuthServiceInternalUpdateVariable (
228 &gEfiGlobalVariableGuid
,
231 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
233 if (EFI_ERROR (Status
)) {
238 // Create "SignatureSupport" variable with BS+RT attribute set.
240 Status
= AuthServiceInternalUpdateVariable (
241 EFI_SIGNATURE_SUPPORT_NAME
,
242 &gEfiGlobalVariableGuid
,
244 sizeof(mSignatureSupport
),
245 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
247 if (EFI_ERROR (Status
)) {
252 // If "SecureBootEnable" variable exists, then update "SecureBoot" variable.
253 // If "SecureBootEnable" variable is SECURE_BOOT_ENABLE and in USER_MODE, Set "SecureBoot" variable to SECURE_BOOT_MODE_ENABLE.
254 // If "SecureBootEnable" variable is SECURE_BOOT_DISABLE, Set "SecureBoot" variable to SECURE_BOOT_MODE_DISABLE.
256 SecureBootEnable
= SECURE_BOOT_DISABLE
;
257 Status
= AuthServiceInternalFindVariable (EFI_SECURE_BOOT_ENABLE_NAME
, &gEfiSecureBootEnableDisableGuid
, (VOID
**) &Data
, &DataSize
);
258 if (!EFI_ERROR (Status
)) {
259 if (mPlatformMode
== SETUP_MODE
){
261 // PK is cleared in runtime. "SecureBootMode" is not updated before reboot
262 // Delete "SecureBootMode" in SetupMode
264 Status
= AuthServiceInternalUpdateVariable (
265 EFI_SECURE_BOOT_ENABLE_NAME
,
266 &gEfiSecureBootEnableDisableGuid
,
269 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
272 SecureBootEnable
= *(UINT8
*) Data
;
274 } else if (mPlatformMode
== USER_MODE
) {
276 // "SecureBootEnable" not exist, initialize it in USER_MODE.
278 SecureBootEnable
= SECURE_BOOT_ENABLE
;
279 Status
= AuthServiceInternalUpdateVariable (
280 EFI_SECURE_BOOT_ENABLE_NAME
,
281 &gEfiSecureBootEnableDisableGuid
,
284 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
286 if (EFI_ERROR (Status
)) {
292 // Create "SecureBoot" variable with BS+RT attribute set.
294 if (SecureBootEnable
== SECURE_BOOT_ENABLE
&& mPlatformMode
== USER_MODE
) {
295 SecureBootMode
= SECURE_BOOT_MODE_ENABLE
;
297 SecureBootMode
= SECURE_BOOT_MODE_DISABLE
;
299 Status
= AuthServiceInternalUpdateVariable (
300 EFI_SECURE_BOOT_MODE_NAME
,
301 &gEfiGlobalVariableGuid
,
304 EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
306 if (EFI_ERROR (Status
)) {
310 DEBUG ((EFI_D_INFO
, "Variable %s is %x\n", EFI_SETUP_MODE_NAME
, mPlatformMode
));
311 DEBUG ((EFI_D_INFO
, "Variable %s is %x\n", EFI_SECURE_BOOT_MODE_NAME
, SecureBootMode
));
312 DEBUG ((EFI_D_INFO
, "Variable %s is %x\n", EFI_SECURE_BOOT_ENABLE_NAME
, SecureBootEnable
));
315 // Initialize "CustomMode" in STANDARD_SECURE_BOOT_MODE state.
317 CustomMode
= STANDARD_SECURE_BOOT_MODE
;
318 Status
= AuthServiceInternalUpdateVariable (
319 EFI_CUSTOM_MODE_NAME
,
320 &gEfiCustomModeEnableGuid
,
323 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
325 if (EFI_ERROR (Status
)) {
329 DEBUG ((EFI_D_INFO
, "Variable %s is %x\n", EFI_CUSTOM_MODE_NAME
, CustomMode
));
332 // Check "certdb" variable's existence.
333 // If it doesn't exist, then create a new one with
334 // EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set.
336 Status
= AuthServiceInternalFindVariable (
342 if (EFI_ERROR (Status
)) {
343 VarAttr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
344 ListSize
= sizeof (UINT32
);
345 Status
= AuthServiceInternalUpdateVariable (
352 if (EFI_ERROR (Status
)) {
357 // Clean up Certs to make certDB & Time based auth variable consistent
359 Status
= CleanCertsFromDb();
360 if (EFI_ERROR (Status
)) {
361 DEBUG ((EFI_D_INFO
, "Clean up CertDB fail! Status %x\n", Status
));
367 // Check "VendorKeysNv" variable's existence and create "VendorKeys" variable accordingly.
369 Status
= AuthServiceInternalFindVariable (EFI_VENDOR_KEYS_NV_VARIABLE_NAME
, &gEfiVendorKeysNvGuid
, (VOID
**) &Data
, &DataSize
);
370 if (!EFI_ERROR (Status
)) {
371 mVendorKeyState
= *(UINT8
*)Data
;
374 // "VendorKeysNv" not exist, initialize it in VENDOR_KEYS_VALID state.
376 mVendorKeyState
= VENDOR_KEYS_VALID
;
377 Status
= AuthServiceInternalUpdateVariable (
378 EFI_VENDOR_KEYS_NV_VARIABLE_NAME
,
379 &gEfiVendorKeysNvGuid
,
382 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
384 if (EFI_ERROR (Status
)) {
390 // Create "VendorKeys" variable with BS+RT attribute set.
392 Status
= AuthServiceInternalUpdateVariable (
393 EFI_VENDOR_KEYS_VARIABLE_NAME
,
394 &gEfiGlobalVariableGuid
,
397 EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
399 if (EFI_ERROR (Status
)) {
403 DEBUG ((EFI_D_INFO
, "Variable %s is %x\n", EFI_VENDOR_KEYS_VARIABLE_NAME
, mVendorKeyState
));
405 AuthVarLibContextOut
->StructVersion
= AUTH_VAR_LIB_CONTEXT_OUT_STRUCT_VERSION
;
406 AuthVarLibContextOut
->StructSize
= sizeof (AUTH_VAR_LIB_CONTEXT_OUT
);
407 AuthVarLibContextOut
->AuthVarEntry
= mAuthVarEntry
;
408 AuthVarLibContextOut
->AuthVarEntryCount
= sizeof (mAuthVarEntry
) / sizeof (mAuthVarEntry
[0]);
409 mAuthVarAddressPointer
[0] = (VOID
**) &mPubKeyStore
;
410 mAuthVarAddressPointer
[1] = (VOID
**) &mCertDbStore
;
411 mAuthVarAddressPointer
[2] = (VOID
**) &mHashCtx
;
412 mAuthVarAddressPointer
[3] = (VOID
**) &mAuthVarLibContextIn
;
413 mAuthVarAddressPointer
[4] = (VOID
**) &(mAuthVarLibContextIn
->FindVariable
),
414 mAuthVarAddressPointer
[5] = (VOID
**) &(mAuthVarLibContextIn
->FindNextVariable
),
415 mAuthVarAddressPointer
[6] = (VOID
**) &(mAuthVarLibContextIn
->UpdateVariable
),
416 mAuthVarAddressPointer
[7] = (VOID
**) &(mAuthVarLibContextIn
->GetScratchBuffer
),
417 mAuthVarAddressPointer
[8] = (VOID
**) &(mAuthVarLibContextIn
->CheckRemainingSpaceForConsistency
),
418 mAuthVarAddressPointer
[9] = (VOID
**) &(mAuthVarLibContextIn
->AtRuntime
),
419 AuthVarLibContextOut
->AddressPointer
= mAuthVarAddressPointer
;
420 AuthVarLibContextOut
->AddressPointerCount
= sizeof (mAuthVarAddressPointer
) / sizeof (mAuthVarAddressPointer
[0]);
426 Process variable with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS/EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set.
428 @param[in] VariableName Name of the variable.
429 @param[in] VendorGuid Variable vendor GUID.
430 @param[in] Data Data pointer.
431 @param[in] DataSize Size of Data.
432 @param[in] Attributes Attribute value of the variable.
434 @retval EFI_SUCCESS The firmware has successfully stored the variable and its data as
435 defined by the Attributes.
436 @retval EFI_INVALID_PARAMETER Invalid parameter.
437 @retval EFI_WRITE_PROTECTED Variable is write-protected.
438 @retval EFI_OUT_OF_RESOURCES There is not enough resource.
439 @retval EFI_SECURITY_VIOLATION The variable is with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
440 or EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACESS
441 set, but the AuthInfo does NOT pass the validation
442 check carried out by the firmware.
443 @retval EFI_UNSUPPORTED Unsupported to process authenticated variable.
448 AuthVariableLibProcessVariable (
449 IN CHAR16
*VariableName
,
450 IN EFI_GUID
*VendorGuid
,
458 if (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, EFI_PLATFORM_KEY_NAME
) == 0)){
459 Status
= ProcessVarWithPk (VariableName
, VendorGuid
, Data
, DataSize
, Attributes
, TRUE
);
460 } else if (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, EFI_KEY_EXCHANGE_KEY_NAME
) == 0)) {
461 Status
= ProcessVarWithPk (VariableName
, VendorGuid
, Data
, DataSize
, Attributes
, FALSE
);
462 } else if (CompareGuid (VendorGuid
, &gEfiImageSecurityDatabaseGuid
) &&
463 ((StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE
) == 0) ||
464 (StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE1
) == 0) ||
465 (StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE2
) == 0)
467 Status
= ProcessVarWithPk (VariableName
, VendorGuid
, Data
, DataSize
, Attributes
, FALSE
);
468 if (EFI_ERROR (Status
)) {
469 Status
= ProcessVarWithKek (VariableName
, VendorGuid
, Data
, DataSize
, Attributes
);
472 Status
= ProcessVariable (VariableName
, VendorGuid
, Data
, DataSize
, Attributes
);