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
;
36 UINT8 mVendorKeyState
;
38 EFI_GUID mSignatureSupport
[] = {EFI_CERT_SHA1_GUID
, EFI_CERT_SHA256_GUID
, EFI_CERT_RSA2048_GUID
, EFI_CERT_X509_GUID
};
41 // Hash context pointer
43 VOID
*mHashCtx
= NULL
;
45 VARIABLE_ENTRY_PROPERTY mAuthVarEntry
[] = {
47 &gEfiSecureBootEnableDisableGuid
,
48 EFI_SECURE_BOOT_ENABLE_NAME
,
50 VAR_CHECK_VARIABLE_PROPERTY_REVISION
,
52 VARIABLE_ATTRIBUTE_NV_BS
,
58 &gEfiCustomModeEnableGuid
,
61 VAR_CHECK_VARIABLE_PROPERTY_REVISION
,
63 VARIABLE_ATTRIBUTE_NV_BS
,
69 &gEfiVendorKeysNvGuid
,
70 EFI_VENDOR_KEYS_NV_VARIABLE_NAME
,
72 VAR_CHECK_VARIABLE_PROPERTY_REVISION
,
73 VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY
,
74 VARIABLE_ATTRIBUTE_NV_BS_RT_AT
,
80 &gEfiAuthenticatedVariableGuid
,
83 VAR_CHECK_VARIABLE_PROPERTY_REVISION
,
84 VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY
,
85 VARIABLE_ATTRIBUTE_NV_BS_RT_AW
,
94 VAR_CHECK_VARIABLE_PROPERTY_REVISION
,
95 VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY
,
96 VARIABLE_ATTRIBUTE_NV_BS_RT_AT
,
103 EFI_CERT_DB_VOLATILE_NAME
,
105 VAR_CHECK_VARIABLE_PROPERTY_REVISION
,
106 VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY
,
107 VARIABLE_ATTRIBUTE_BS_RT_AT
,
113 &gEdkiiSecureBootModeGuid
,
116 VAR_CHECK_VARIABLE_PROPERTY_REVISION
,
117 VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY
,
118 VARIABLE_ATTRIBUTE_NV_BS_RT
,
125 VOID
**mAuthVarAddressPointer
[10];
127 AUTH_VAR_LIB_CONTEXT_IN
*mAuthVarLibContextIn
= NULL
;
130 Initialization for authenticated varibale services.
131 If this initialization returns error status, other APIs will not work
132 and expect to be not called then.
134 @param[in] AuthVarLibContextIn Pointer to input auth variable lib context.
135 @param[out] AuthVarLibContextOut Pointer to output auth variable lib context.
137 @retval EFI_SUCCESS Function successfully executed.
138 @retval EFI_INVALID_PARAMETER If AuthVarLibContextIn == NULL or AuthVarLibContextOut == NULL.
139 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough resource.
140 @retval EFI_UNSUPPORTED Unsupported to process authenticated variable.
145 AuthVariableLibInitialize (
146 IN AUTH_VAR_LIB_CONTEXT_IN
*AuthVarLibContextIn
,
147 OUT AUTH_VAR_LIB_CONTEXT_OUT
*AuthVarLibContextOut
159 if ((AuthVarLibContextIn
== NULL
) || (AuthVarLibContextOut
== NULL
)) {
160 return EFI_INVALID_PARAMETER
;
163 mAuthVarLibContextIn
= AuthVarLibContextIn
;
166 // Initialize hash context.
168 CtxSize
= Sha256GetContextSize ();
169 mHashCtx
= AllocateRuntimePool (CtxSize
);
170 if (mHashCtx
== NULL
) {
171 return EFI_OUT_OF_RESOURCES
;
175 // Reserve runtime buffer for public key database. The size excludes variable header and name size.
177 mMaxKeyDbSize
= (UINT32
) (mAuthVarLibContextIn
->MaxAuthVariableSize
- sizeof (AUTHVAR_KEYDB_NAME
));
178 mMaxKeyNumber
= mMaxKeyDbSize
/ sizeof (AUTHVAR_KEY_DB_DATA
);
179 mPubKeyStore
= AllocateRuntimePool (mMaxKeyDbSize
);
180 if (mPubKeyStore
== NULL
) {
181 return EFI_OUT_OF_RESOURCES
;
185 // Reserve runtime buffer for certificate database. The size excludes variable header and name size.
186 // Use EFI_CERT_DB_VOLATILE_NAME size since it is longer.
188 mMaxCertDbSize
= (UINT32
) (mAuthVarLibContextIn
->MaxAuthVariableSize
- sizeof (EFI_CERT_DB_VOLATILE_NAME
));
189 mCertDbStore
= AllocateRuntimePool (mMaxCertDbSize
);
190 if (mCertDbStore
== NULL
) {
191 return EFI_OUT_OF_RESOURCES
;
195 // Check "AuthVarKeyDatabase" variable's existence.
196 // If it doesn't exist, create a new one with initial value of 0 and EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS set.
198 Status
= AuthServiceInternalFindVariable (
200 &gEfiAuthenticatedVariableGuid
,
204 if (EFI_ERROR (Status
)) {
205 VarAttr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
;
208 Status
= AuthServiceInternalUpdateVariable (
210 &gEfiAuthenticatedVariableGuid
,
215 if (EFI_ERROR (Status
)) {
220 // Load database in global variable for cache.
222 ASSERT ((DataSize
!= 0) && (Data
!= NULL
));
224 // "AuthVarKeyDatabase" is an internal variable. Its DataSize is always ensured not to exceed mPubKeyStore buffer size(See definition before)
225 // Therefore, there is no memory overflow in underlying CopyMem.
227 CopyMem (mPubKeyStore
, (UINT8
*) Data
, DataSize
);
228 mPubKeyNumber
= (UINT32
) (DataSize
/ sizeof (AUTHVAR_KEY_DB_DATA
));
232 // Init Secure Boot variables
234 Status
= InitSecureBootVariables ();
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 // Initialize "CustomMode" in STANDARD_SECURE_BOOT_MODE state.
254 CustomMode
= STANDARD_SECURE_BOOT_MODE
;
255 Status
= AuthServiceInternalUpdateVariable (
256 EFI_CUSTOM_MODE_NAME
,
257 &gEfiCustomModeEnableGuid
,
260 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
262 if (EFI_ERROR (Status
)) {
266 DEBUG ((EFI_D_INFO
, "Variable %s is %x\n", EFI_CUSTOM_MODE_NAME
, CustomMode
));
269 // Check "certdb" variable's existence.
270 // If it doesn't exist, then create a new one with
271 // EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set.
273 Status
= AuthServiceInternalFindVariable (
279 if (EFI_ERROR (Status
)) {
280 VarAttr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
281 ListSize
= sizeof (UINT32
);
282 Status
= AuthServiceInternalUpdateVariable (
289 if (EFI_ERROR (Status
)) {
294 // Clean up Certs to make certDB & Time based auth variable consistent
296 Status
= CleanCertsFromDb();
297 if (EFI_ERROR (Status
)) {
298 DEBUG ((EFI_D_ERROR
, "Clean up CertDB fail! Status %x\n", Status
));
304 // Create "certdbv" variable with RT+BS+AT set.
306 VarAttr
= EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
307 ListSize
= sizeof (UINT32
);
308 Status
= AuthServiceInternalUpdateVariable (
309 EFI_CERT_DB_VOLATILE_NAME
,
315 if (EFI_ERROR (Status
)) {
320 // Check "VendorKeysNv" variable's existence and create "VendorKeys" variable accordingly.
322 Status
= AuthServiceInternalFindVariable (EFI_VENDOR_KEYS_NV_VARIABLE_NAME
, &gEfiVendorKeysNvGuid
, (VOID
**) &Data
, &DataSize
);
323 if (!EFI_ERROR (Status
)) {
324 mVendorKeyState
= *(UINT8
*)Data
;
327 // "VendorKeysNv" not exist, initialize it in VENDOR_KEYS_VALID state.
329 mVendorKeyState
= VENDOR_KEYS_VALID
;
330 Status
= AuthServiceInternalUpdateVariable (
331 EFI_VENDOR_KEYS_NV_VARIABLE_NAME
,
332 &gEfiVendorKeysNvGuid
,
335 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
337 if (EFI_ERROR (Status
)) {
343 // Create "VendorKeys" variable with BS+RT attribute set.
345 Status
= AuthServiceInternalUpdateVariable (
346 EFI_VENDOR_KEYS_VARIABLE_NAME
,
347 &gEfiGlobalVariableGuid
,
350 EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
352 if (EFI_ERROR (Status
)) {
356 DEBUG ((EFI_D_INFO
, "Variable %s is %x\n", EFI_VENDOR_KEYS_VARIABLE_NAME
, mVendorKeyState
));
358 AuthVarLibContextOut
->StructVersion
= AUTH_VAR_LIB_CONTEXT_OUT_STRUCT_VERSION
;
359 AuthVarLibContextOut
->StructSize
= sizeof (AUTH_VAR_LIB_CONTEXT_OUT
);
360 AuthVarLibContextOut
->AuthVarEntry
= mAuthVarEntry
;
361 AuthVarLibContextOut
->AuthVarEntryCount
= sizeof (mAuthVarEntry
) / sizeof (mAuthVarEntry
[0]);
362 mAuthVarAddressPointer
[0] = (VOID
**) &mPubKeyStore
;
363 mAuthVarAddressPointer
[1] = (VOID
**) &mCertDbStore
;
364 mAuthVarAddressPointer
[2] = (VOID
**) &mHashCtx
;
365 mAuthVarAddressPointer
[3] = (VOID
**) &mAuthVarLibContextIn
;
366 mAuthVarAddressPointer
[4] = (VOID
**) &(mAuthVarLibContextIn
->FindVariable
),
367 mAuthVarAddressPointer
[5] = (VOID
**) &(mAuthVarLibContextIn
->FindNextVariable
),
368 mAuthVarAddressPointer
[6] = (VOID
**) &(mAuthVarLibContextIn
->UpdateVariable
),
369 mAuthVarAddressPointer
[7] = (VOID
**) &(mAuthVarLibContextIn
->GetScratchBuffer
),
370 mAuthVarAddressPointer
[8] = (VOID
**) &(mAuthVarLibContextIn
->CheckRemainingSpaceForConsistency
),
371 mAuthVarAddressPointer
[9] = (VOID
**) &(mAuthVarLibContextIn
->AtRuntime
),
372 AuthVarLibContextOut
->AddressPointer
= mAuthVarAddressPointer
;
373 AuthVarLibContextOut
->AddressPointerCount
= sizeof (mAuthVarAddressPointer
) / sizeof (mAuthVarAddressPointer
[0]);
379 Process variable with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS/EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set.
381 @param[in] VariableName Name of the variable.
382 @param[in] VendorGuid Variable vendor GUID.
383 @param[in] Data Data pointer.
384 @param[in] DataSize Size of Data.
385 @param[in] Attributes Attribute value of the variable.
387 @retval EFI_SUCCESS The firmware has successfully stored the variable and its data as
388 defined by the Attributes.
389 @retval EFI_INVALID_PARAMETER Invalid parameter.
390 @retval EFI_WRITE_PROTECTED Variable is write-protected.
391 @retval EFI_OUT_OF_RESOURCES There is not enough resource.
392 @retval EFI_SECURITY_VIOLATION The variable is with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
393 or EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACESS
394 set, but the AuthInfo does NOT pass the validation
395 check carried out by the firmware.
396 @retval EFI_UNSUPPORTED Unsupported to process authenticated variable.
401 AuthVariableLibProcessVariable (
402 IN CHAR16
*VariableName
,
403 IN EFI_GUID
*VendorGuid
,
412 // Process PK, KEK, Sigdb, AuditMode, DeployedMode separately.
414 if (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, EFI_PLATFORM_KEY_NAME
) == 0)){
415 Status
= ProcessVarWithPk (VariableName
, VendorGuid
, Data
, DataSize
, Attributes
, TRUE
);
416 } else if (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, EFI_KEY_EXCHANGE_KEY_NAME
) == 0)) {
417 Status
= ProcessVarWithPk (VariableName
, VendorGuid
, Data
, DataSize
, Attributes
, FALSE
);
418 } else if (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
)
419 && (StrCmp (VariableName
, EFI_AUDIT_MODE_NAME
) == 0 || StrCmp (VariableName
, EFI_DEPLOYED_MODE_NAME
) == 0)) {
420 Status
= ProcessSecureBootModeVar(VariableName
, VendorGuid
, Data
, DataSize
, Attributes
);
421 } else if (CompareGuid (VendorGuid
, &gEfiImageSecurityDatabaseGuid
) &&
422 ((StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE
) == 0) ||
423 (StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE1
) == 0) ||
424 (StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE2
) == 0)
426 Status
= ProcessVarWithPk (VariableName
, VendorGuid
, Data
, DataSize
, Attributes
, FALSE
);
427 if (EFI_ERROR (Status
)) {
428 Status
= ProcessVarWithKek (VariableName
, VendorGuid
, Data
, DataSize
, Attributes
);
431 Status
= ProcessVariable (VariableName
, VendorGuid
, Data
, DataSize
, Attributes
);