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
*mAddressPointer
[3];
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
)) {
358 // Check "VendorKeysNv" variable's existence and create "VendorKeys" variable accordingly.
360 Status
= AuthServiceInternalFindVariable (EFI_VENDOR_KEYS_NV_VARIABLE_NAME
, &gEfiVendorKeysNvGuid
, (VOID
**) &Data
, &DataSize
);
361 if (!EFI_ERROR (Status
)) {
362 mVendorKeyState
= *(UINT8
*)Data
;
365 // "VendorKeysNv" not exist, initialize it in VENDOR_KEYS_VALID state.
367 mVendorKeyState
= VENDOR_KEYS_VALID
;
368 Status
= AuthServiceInternalUpdateVariable (
369 EFI_VENDOR_KEYS_NV_VARIABLE_NAME
,
370 &gEfiVendorKeysNvGuid
,
373 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
375 if (EFI_ERROR (Status
)) {
381 // Create "VendorKeys" variable with BS+RT attribute set.
383 Status
= AuthServiceInternalUpdateVariable (
384 EFI_VENDOR_KEYS_VARIABLE_NAME
,
385 &gEfiGlobalVariableGuid
,
388 EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
390 if (EFI_ERROR (Status
)) {
394 DEBUG ((EFI_D_INFO
, "Variable %s is %x\n", EFI_VENDOR_KEYS_VARIABLE_NAME
, mVendorKeyState
));
396 AuthVarLibContextOut
->StructVersion
= AUTH_VAR_LIB_CONTEXT_OUT_STRUCT_VERSION
;
397 AuthVarLibContextOut
->StructSize
= sizeof (AUTH_VAR_LIB_CONTEXT_OUT
);
398 AuthVarLibContextOut
->AuthVarEntry
= mAuthVarEntry
;
399 AuthVarLibContextOut
->AuthVarEntryCount
= sizeof (mAuthVarEntry
) / sizeof (mAuthVarEntry
[0]);
400 mAddressPointer
[0] = mHashCtx
;
401 mAddressPointer
[1] = mPubKeyStore
;
402 mAddressPointer
[2] = mCertDbStore
;
403 AuthVarLibContextOut
->AddressPointer
= mAddressPointer
;
404 AuthVarLibContextOut
->AddressPointerCount
= sizeof (mAddressPointer
) / sizeof (mAddressPointer
[0]);
410 Process variable with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS/EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set.
412 @param[in] VariableName Name of the variable.
413 @param[in] VendorGuid Variable vendor GUID.
414 @param[in] Data Data pointer.
415 @param[in] DataSize Size of Data.
416 @param[in] Attributes Attribute value of the variable.
418 @retval EFI_SUCCESS The firmware has successfully stored the variable and its data as
419 defined by the Attributes.
420 @retval EFI_INVALID_PARAMETER Invalid parameter.
421 @retval EFI_WRITE_PROTECTED Variable is write-protected.
422 @retval EFI_OUT_OF_RESOURCES There is not enough resource.
423 @retval EFI_SECURITY_VIOLATION The variable is with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
424 or EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACESS
425 set, but the AuthInfo does NOT pass the validation
426 check carried out by the firmware.
427 @retval EFI_UNSUPPORTED Unsupported to process authenticated variable.
432 AuthVariableLibProcessVariable (
433 IN CHAR16
*VariableName
,
434 IN EFI_GUID
*VendorGuid
,
442 if (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, EFI_PLATFORM_KEY_NAME
) == 0)){
443 Status
= ProcessVarWithPk (VariableName
, VendorGuid
, Data
, DataSize
, Attributes
, TRUE
);
444 } else if (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, EFI_KEY_EXCHANGE_KEY_NAME
) == 0)) {
445 Status
= ProcessVarWithPk (VariableName
, VendorGuid
, Data
, DataSize
, Attributes
, FALSE
);
446 } else if (CompareGuid (VendorGuid
, &gEfiImageSecurityDatabaseGuid
) &&
447 ((StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE
) == 0) ||
448 (StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE1
) == 0) ||
449 (StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE2
) == 0)
451 Status
= ProcessVarWithPk (VariableName
, VendorGuid
, Data
, DataSize
, Attributes
, FALSE
);
452 if (EFI_ERROR (Status
)) {
453 Status
= ProcessVarWithKek (VariableName
, VendorGuid
, Data
, DataSize
, Attributes
);
456 Status
= ProcessVariable (VariableName
, VendorGuid
, Data
, DataSize
, Attributes
);