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 - 2016, 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 EFI_CERT_DB_VOLATILE_NAME
,
106 VAR_CHECK_VARIABLE_PROPERTY_REVISION
,
107 VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY
,
108 VARIABLE_ATTRIBUTE_BS_RT_AT
,
115 VOID
**mAuthVarAddressPointer
[10];
117 AUTH_VAR_LIB_CONTEXT_IN
*mAuthVarLibContextIn
= NULL
;
120 Initialization for authenticated varibale services.
121 If this initialization returns error status, other APIs will not work
122 and expect to be not called then.
124 @param[in] AuthVarLibContextIn Pointer to input auth variable lib context.
125 @param[out] AuthVarLibContextOut Pointer to output auth variable lib context.
127 @retval EFI_SUCCESS Function successfully executed.
128 @retval EFI_INVALID_PARAMETER If AuthVarLibContextIn == NULL or AuthVarLibContextOut == NULL.
129 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough resource.
130 @retval EFI_UNSUPPORTED Unsupported to process authenticated variable.
135 AuthVariableLibInitialize (
136 IN AUTH_VAR_LIB_CONTEXT_IN
*AuthVarLibContextIn
,
137 OUT AUTH_VAR_LIB_CONTEXT_OUT
*AuthVarLibContextOut
146 UINT8 SecureBootMode
;
147 UINT8 SecureBootEnable
;
151 if ((AuthVarLibContextIn
== NULL
) || (AuthVarLibContextOut
== NULL
)) {
152 return EFI_INVALID_PARAMETER
;
155 mAuthVarLibContextIn
= AuthVarLibContextIn
;
158 // Initialize hash context.
160 CtxSize
= Sha256GetContextSize ();
161 mHashCtx
= AllocateRuntimePool (CtxSize
);
162 if (mHashCtx
== NULL
) {
163 return EFI_OUT_OF_RESOURCES
;
167 // Reserve runtime buffer for public key database. The size excludes variable header and name size.
169 mMaxKeyDbSize
= (UINT32
) (mAuthVarLibContextIn
->MaxAuthVariableSize
- sizeof (AUTHVAR_KEYDB_NAME
));
170 mMaxKeyNumber
= mMaxKeyDbSize
/ sizeof (AUTHVAR_KEY_DB_DATA
);
171 mPubKeyStore
= AllocateRuntimePool (mMaxKeyDbSize
);
172 if (mPubKeyStore
== NULL
) {
173 return EFI_OUT_OF_RESOURCES
;
177 // Reserve runtime buffer for certificate database. The size excludes variable header and name size.
178 // Use EFI_CERT_DB_VOLATILE_NAME size since it is longer.
180 mMaxCertDbSize
= (UINT32
) (mAuthVarLibContextIn
->MaxAuthVariableSize
- sizeof (EFI_CERT_DB_VOLATILE_NAME
));
181 mCertDbStore
= AllocateRuntimePool (mMaxCertDbSize
);
182 if (mCertDbStore
== NULL
) {
183 return EFI_OUT_OF_RESOURCES
;
187 // Check "AuthVarKeyDatabase" variable's existence.
188 // If it doesn't exist, create a new one with initial value of 0 and EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS set.
190 Status
= AuthServiceInternalFindVariable (
192 &gEfiAuthenticatedVariableGuid
,
196 if (EFI_ERROR (Status
)) {
197 VarAttr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
;
200 Status
= AuthServiceInternalUpdateVariable (
202 &gEfiAuthenticatedVariableGuid
,
207 if (EFI_ERROR (Status
)) {
212 // Load database in global variable for cache.
214 ASSERT ((DataSize
!= 0) && (Data
!= NULL
));
216 // "AuthVarKeyDatabase" is an internal variable. Its DataSize is always ensured not to exceed mPubKeyStore buffer size(See definition before)
217 // Therefore, there is no memory overflow in underlying CopyMem.
219 CopyMem (mPubKeyStore
, (UINT8
*) Data
, DataSize
);
220 mPubKeyNumber
= (UINT32
) (DataSize
/ sizeof (AUTHVAR_KEY_DB_DATA
));
223 Status
= AuthServiceInternalFindVariable (EFI_PLATFORM_KEY_NAME
, &gEfiGlobalVariableGuid
, (VOID
**) &Data
, &DataSize
);
224 if (EFI_ERROR (Status
)) {
225 DEBUG ((EFI_D_INFO
, "Variable %s does not exist.\n", EFI_PLATFORM_KEY_NAME
));
227 DEBUG ((EFI_D_INFO
, "Variable %s exists.\n", EFI_PLATFORM_KEY_NAME
));
231 // Create "SetupMode" variable with BS+RT attribute set.
233 if (EFI_ERROR (Status
)) {
234 mPlatformMode
= SETUP_MODE
;
236 mPlatformMode
= USER_MODE
;
238 Status
= AuthServiceInternalUpdateVariable (
240 &gEfiGlobalVariableGuid
,
243 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
245 if (EFI_ERROR (Status
)) {
250 // Create "SignatureSupport" variable with BS+RT attribute set.
252 Status
= AuthServiceInternalUpdateVariable (
253 EFI_SIGNATURE_SUPPORT_NAME
,
254 &gEfiGlobalVariableGuid
,
256 sizeof(mSignatureSupport
),
257 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
259 if (EFI_ERROR (Status
)) {
264 // If "SecureBootEnable" variable exists, then update "SecureBoot" variable.
265 // If "SecureBootEnable" variable is SECURE_BOOT_ENABLE and in USER_MODE, Set "SecureBoot" variable to SECURE_BOOT_MODE_ENABLE.
266 // If "SecureBootEnable" variable is SECURE_BOOT_DISABLE, Set "SecureBoot" variable to SECURE_BOOT_MODE_DISABLE.
268 SecureBootEnable
= SECURE_BOOT_DISABLE
;
269 Status
= AuthServiceInternalFindVariable (EFI_SECURE_BOOT_ENABLE_NAME
, &gEfiSecureBootEnableDisableGuid
, (VOID
**) &Data
, &DataSize
);
270 if (!EFI_ERROR (Status
)) {
271 if (mPlatformMode
== USER_MODE
){
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_ERROR
, "Clean up CertDB fail! Status %x\n", Status
));
367 // Create "certdbv" variable with RT+BS+AT set.
369 VarAttr
= EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
370 ListSize
= sizeof (UINT32
);
371 Status
= AuthServiceInternalUpdateVariable (
372 EFI_CERT_DB_VOLATILE_NAME
,
378 if (EFI_ERROR (Status
)) {
383 // Check "VendorKeysNv" variable's existence and create "VendorKeys" variable accordingly.
385 Status
= AuthServiceInternalFindVariable (EFI_VENDOR_KEYS_NV_VARIABLE_NAME
, &gEfiVendorKeysNvGuid
, (VOID
**) &Data
, &DataSize
);
386 if (!EFI_ERROR (Status
)) {
387 mVendorKeyState
= *(UINT8
*)Data
;
390 // "VendorKeysNv" not exist, initialize it in VENDOR_KEYS_VALID state.
392 mVendorKeyState
= VENDOR_KEYS_VALID
;
393 Status
= AuthServiceInternalUpdateVariable (
394 EFI_VENDOR_KEYS_NV_VARIABLE_NAME
,
395 &gEfiVendorKeysNvGuid
,
398 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
400 if (EFI_ERROR (Status
)) {
406 // Create "VendorKeys" variable with BS+RT attribute set.
408 Status
= AuthServiceInternalUpdateVariable (
409 EFI_VENDOR_KEYS_VARIABLE_NAME
,
410 &gEfiGlobalVariableGuid
,
413 EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
415 if (EFI_ERROR (Status
)) {
419 DEBUG ((EFI_D_INFO
, "Variable %s is %x\n", EFI_VENDOR_KEYS_VARIABLE_NAME
, mVendorKeyState
));
421 AuthVarLibContextOut
->StructVersion
= AUTH_VAR_LIB_CONTEXT_OUT_STRUCT_VERSION
;
422 AuthVarLibContextOut
->StructSize
= sizeof (AUTH_VAR_LIB_CONTEXT_OUT
);
423 AuthVarLibContextOut
->AuthVarEntry
= mAuthVarEntry
;
424 AuthVarLibContextOut
->AuthVarEntryCount
= ARRAY_SIZE (mAuthVarEntry
);
425 mAuthVarAddressPointer
[0] = (VOID
**) &mPubKeyStore
;
426 mAuthVarAddressPointer
[1] = (VOID
**) &mCertDbStore
;
427 mAuthVarAddressPointer
[2] = (VOID
**) &mHashCtx
;
428 mAuthVarAddressPointer
[3] = (VOID
**) &mAuthVarLibContextIn
;
429 mAuthVarAddressPointer
[4] = (VOID
**) &(mAuthVarLibContextIn
->FindVariable
),
430 mAuthVarAddressPointer
[5] = (VOID
**) &(mAuthVarLibContextIn
->FindNextVariable
),
431 mAuthVarAddressPointer
[6] = (VOID
**) &(mAuthVarLibContextIn
->UpdateVariable
),
432 mAuthVarAddressPointer
[7] = (VOID
**) &(mAuthVarLibContextIn
->GetScratchBuffer
),
433 mAuthVarAddressPointer
[8] = (VOID
**) &(mAuthVarLibContextIn
->CheckRemainingSpaceForConsistency
),
434 mAuthVarAddressPointer
[9] = (VOID
**) &(mAuthVarLibContextIn
->AtRuntime
),
435 AuthVarLibContextOut
->AddressPointer
= mAuthVarAddressPointer
;
436 AuthVarLibContextOut
->AddressPointerCount
= ARRAY_SIZE (mAuthVarAddressPointer
);
442 Process variable with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS/EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set.
444 @param[in] VariableName Name of the variable.
445 @param[in] VendorGuid Variable vendor GUID.
446 @param[in] Data Data pointer.
447 @param[in] DataSize Size of Data.
448 @param[in] Attributes Attribute value of the variable.
450 @retval EFI_SUCCESS The firmware has successfully stored the variable and its data as
451 defined by the Attributes.
452 @retval EFI_INVALID_PARAMETER Invalid parameter.
453 @retval EFI_WRITE_PROTECTED Variable is write-protected.
454 @retval EFI_OUT_OF_RESOURCES There is not enough resource.
455 @retval EFI_SECURITY_VIOLATION The variable is with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
456 or EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACESS
457 set, but the AuthInfo does NOT pass the validation
458 check carried out by the firmware.
459 @retval EFI_UNSUPPORTED Unsupported to process authenticated variable.
464 AuthVariableLibProcessVariable (
465 IN CHAR16
*VariableName
,
466 IN EFI_GUID
*VendorGuid
,
474 if (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, EFI_PLATFORM_KEY_NAME
) == 0)){
475 Status
= ProcessVarWithPk (VariableName
, VendorGuid
, Data
, DataSize
, Attributes
, TRUE
);
476 } else if (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, EFI_KEY_EXCHANGE_KEY_NAME
) == 0)) {
477 Status
= ProcessVarWithPk (VariableName
, VendorGuid
, Data
, DataSize
, Attributes
, FALSE
);
478 } else if (CompareGuid (VendorGuid
, &gEfiImageSecurityDatabaseGuid
) &&
479 ((StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE
) == 0) ||
480 (StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE1
) == 0) ||
481 (StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE2
) == 0)
483 Status
= ProcessVarWithPk (VariableName
, VendorGuid
, Data
, DataSize
, Attributes
, FALSE
);
484 if (EFI_ERROR (Status
)) {
485 Status
= ProcessVarWithKek (VariableName
, VendorGuid
, Data
, DataSize
, Attributes
);
488 Status
= ProcessVariable (VariableName
, VendorGuid
, Data
, DataSize
, Attributes
);