]> git.proxmox.com Git - mirror_edk2.git/blame - SecurityPkg/Library/AuthVariableLib/AuthVariableLib.c
SecurityPkg: Apply uncrustify changes
[mirror_edk2.git] / SecurityPkg / Library / AuthVariableLib / AuthVariableLib.c
CommitLineData
a6811666
SZ
1/** @file\r
2 Implement authentication services for the authenticated variables.\r
3\r
4 Caution: This module requires additional review when modified.\r
5 This driver will have external input - variable data. It may be input in SMM mode.\r
6 This external input must be validated carefully to avoid security issue like\r
7 buffer overflow, integer overflow.\r
8 Variable attribute should also be checked to avoid authentication bypass.\r
9 The whole SMM authentication variable design relies on the integrity of flash part and SMM.\r
10 which is assumed to be protected by platform. All variable code and metadata in flash/SMM Memory\r
11 may not be modified without authorization. If platform fails to protect these resources,\r
12 the authentication service provided in this driver will be broken, and the behavior is undefined.\r
13\r
98c2d961 14Copyright (c) 2015 - 2016, Intel Corporation. All rights reserved.<BR>\r
289b714b 15SPDX-License-Identifier: BSD-2-Clause-Patent\r
a6811666
SZ
16\r
17**/\r
18\r
19#include "AuthServiceInternal.h"\r
20\r
21///\r
22/// Global database array for scratch\r
23///\r
c411b485
MK
24UINT8 *mCertDbStore;\r
25UINT32 mMaxCertDbSize;\r
26UINT32 mPlatformMode;\r
27UINT8 mVendorKeyState;\r
a6811666 28\r
c411b485 29EFI_GUID mSignatureSupport[] = { EFI_CERT_SHA1_GUID, EFI_CERT_SHA256_GUID, EFI_CERT_RSA2048_GUID, EFI_CERT_X509_GUID };\r
a6811666
SZ
30\r
31//\r
32// Hash context pointer\r
33//\r
34VOID *mHashCtx = NULL;\r
35\r
c411b485 36VARIABLE_ENTRY_PROPERTY mAuthVarEntry[] = {\r
a6811666
SZ
37 {\r
38 &gEfiSecureBootEnableDisableGuid,\r
39 EFI_SECURE_BOOT_ENABLE_NAME,\r
40 {\r
41 VAR_CHECK_VARIABLE_PROPERTY_REVISION,\r
42 0,\r
43 VARIABLE_ATTRIBUTE_NV_BS,\r
44 sizeof (UINT8),\r
45 sizeof (UINT8)\r
46 }\r
47 },\r
48 {\r
49 &gEfiCustomModeEnableGuid,\r
50 EFI_CUSTOM_MODE_NAME,\r
51 {\r
52 VAR_CHECK_VARIABLE_PROPERTY_REVISION,\r
53 0,\r
54 VARIABLE_ATTRIBUTE_NV_BS,\r
55 sizeof (UINT8),\r
56 sizeof (UINT8)\r
57 }\r
58 },\r
59 {\r
60 &gEfiVendorKeysNvGuid,\r
61 EFI_VENDOR_KEYS_NV_VARIABLE_NAME,\r
62 {\r
63 VAR_CHECK_VARIABLE_PROPERTY_REVISION,\r
64 VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY,\r
65 VARIABLE_ATTRIBUTE_NV_BS_RT_AT,\r
66 sizeof (UINT8),\r
67 sizeof (UINT8)\r
68 }\r
69 },\r
a6811666
SZ
70 {\r
71 &gEfiCertDbGuid,\r
72 EFI_CERT_DB_NAME,\r
73 {\r
74 VAR_CHECK_VARIABLE_PROPERTY_REVISION,\r
75 VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY,\r
76 VARIABLE_ATTRIBUTE_NV_BS_RT_AT,\r
77 sizeof (UINT32),\r
78 MAX_UINTN\r
79 }\r
80 },\r
98c2d961
CZ
81 {\r
82 &gEfiCertDbGuid,\r
83 EFI_CERT_DB_VOLATILE_NAME,\r
84 {\r
85 VAR_CHECK_VARIABLE_PROPERTY_REVISION,\r
86 VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY,\r
87 VARIABLE_ATTRIBUTE_BS_RT_AT,\r
88 sizeof (UINT32),\r
89 MAX_UINTN\r
90 }\r
91 },\r
a6811666
SZ
92};\r
93\r
c411b485 94VOID **mAuthVarAddressPointer[9];\r
a6811666 95\r
c411b485 96AUTH_VAR_LIB_CONTEXT_IN *mAuthVarLibContextIn = NULL;\r
a6811666
SZ
97\r
98/**\r
d6b926e7 99 Initialization for authenticated variable services.\r
a6811666
SZ
100 If this initialization returns error status, other APIs will not work\r
101 and expect to be not called then.\r
102\r
103 @param[in] AuthVarLibContextIn Pointer to input auth variable lib context.\r
104 @param[out] AuthVarLibContextOut Pointer to output auth variable lib context.\r
105\r
106 @retval EFI_SUCCESS Function successfully executed.\r
107 @retval EFI_INVALID_PARAMETER If AuthVarLibContextIn == NULL or AuthVarLibContextOut == NULL.\r
108 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough resource.\r
109 @retval EFI_UNSUPPORTED Unsupported to process authenticated variable.\r
110\r
111**/\r
112EFI_STATUS\r
113EFIAPI\r
114AuthVariableLibInitialize (\r
115 IN AUTH_VAR_LIB_CONTEXT_IN *AuthVarLibContextIn,\r
116 OUT AUTH_VAR_LIB_CONTEXT_OUT *AuthVarLibContextOut\r
117 )\r
118{\r
c411b485
MK
119 EFI_STATUS Status;\r
120 UINT32 VarAttr;\r
121 UINT8 *Data;\r
122 UINTN DataSize;\r
123 UINTN CtxSize;\r
124 UINT8 SecureBootMode;\r
125 UINT8 SecureBootEnable;\r
126 UINT8 CustomMode;\r
127 UINT32 ListSize;\r
a6811666
SZ
128\r
129 if ((AuthVarLibContextIn == NULL) || (AuthVarLibContextOut == NULL)) {\r
130 return EFI_INVALID_PARAMETER;\r
131 }\r
132\r
133 mAuthVarLibContextIn = AuthVarLibContextIn;\r
134\r
135 //\r
136 // Initialize hash context.\r
137 //\r
c411b485
MK
138 CtxSize = Sha256GetContextSize ();\r
139 mHashCtx = AllocateRuntimePool (CtxSize);\r
a6811666
SZ
140 if (mHashCtx == NULL) {\r
141 return EFI_OUT_OF_RESOURCES;\r
142 }\r
143\r
a6811666
SZ
144 //\r
145 // Reserve runtime buffer for certificate database. The size excludes variable header and name size.\r
98c2d961 146 // Use EFI_CERT_DB_VOLATILE_NAME size since it is longer.\r
a6811666 147 //\r
c411b485 148 mMaxCertDbSize = (UINT32)(mAuthVarLibContextIn->MaxAuthVariableSize - sizeof (EFI_CERT_DB_VOLATILE_NAME));\r
a6811666
SZ
149 mCertDbStore = AllocateRuntimePool (mMaxCertDbSize);\r
150 if (mCertDbStore == NULL) {\r
151 return EFI_OUT_OF_RESOURCES;\r
152 }\r
153\r
c411b485 154 Status = AuthServiceInternalFindVariable (EFI_PLATFORM_KEY_NAME, &gEfiGlobalVariableGuid, (VOID **)&Data, &DataSize);\r
560ac77e 155 if (EFI_ERROR (Status)) {\r
e905fbb0 156 DEBUG ((DEBUG_INFO, "Variable %s does not exist.\n", EFI_PLATFORM_KEY_NAME));\r
560ac77e 157 } else {\r
e905fbb0 158 DEBUG ((DEBUG_INFO, "Variable %s exists.\n", EFI_PLATFORM_KEY_NAME));\r
560ac77e
ZC
159 }\r
160\r
a6811666 161 //\r
560ac77e 162 // Create "SetupMode" variable with BS+RT attribute set.\r
a6811666 163 //\r
560ac77e
ZC
164 if (EFI_ERROR (Status)) {\r
165 mPlatformMode = SETUP_MODE;\r
166 } else {\r
167 mPlatformMode = USER_MODE;\r
168 }\r
c411b485 169\r
560ac77e
ZC
170 Status = AuthServiceInternalUpdateVariable (\r
171 EFI_SETUP_MODE_NAME,\r
172 &gEfiGlobalVariableGuid,\r
173 &mPlatformMode,\r
c411b485 174 sizeof (UINT8),\r
560ac77e
ZC
175 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS\r
176 );\r
177 if (EFI_ERROR (Status)) {\r
178 return Status;\r
179 }\r
a6811666
SZ
180\r
181 //\r
182 // Create "SignatureSupport" variable with BS+RT attribute set.\r
183 //\r
c411b485
MK
184 Status = AuthServiceInternalUpdateVariable (\r
185 EFI_SIGNATURE_SUPPORT_NAME,\r
186 &gEfiGlobalVariableGuid,\r
187 mSignatureSupport,\r
188 sizeof (mSignatureSupport),\r
189 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS\r
190 );\r
a6811666
SZ
191 if (EFI_ERROR (Status)) {\r
192 return Status;\r
193 }\r
194\r
560ac77e
ZC
195 //\r
196 // If "SecureBootEnable" variable exists, then update "SecureBoot" variable.\r
197 // If "SecureBootEnable" variable is SECURE_BOOT_ENABLE and in USER_MODE, Set "SecureBoot" variable to SECURE_BOOT_MODE_ENABLE.\r
198 // If "SecureBootEnable" variable is SECURE_BOOT_DISABLE, Set "SecureBoot" variable to SECURE_BOOT_MODE_DISABLE.\r
199 //\r
200 SecureBootEnable = SECURE_BOOT_DISABLE;\r
c411b485 201 Status = AuthServiceInternalFindVariable (EFI_SECURE_BOOT_ENABLE_NAME, &gEfiSecureBootEnableDisableGuid, (VOID **)&Data, &DataSize);\r
560ac77e 202 if (!EFI_ERROR (Status)) {\r
c411b485
MK
203 if (mPlatformMode == USER_MODE) {\r
204 SecureBootEnable = *(UINT8 *)Data;\r
560ac77e
ZC
205 }\r
206 } else if (mPlatformMode == USER_MODE) {\r
207 //\r
208 // "SecureBootEnable" not exist, initialize it in USER_MODE.\r
209 //\r
210 SecureBootEnable = SECURE_BOOT_ENABLE;\r
c411b485
MK
211 Status = AuthServiceInternalUpdateVariable (\r
212 EFI_SECURE_BOOT_ENABLE_NAME,\r
213 &gEfiSecureBootEnableDisableGuid,\r
214 &SecureBootEnable,\r
215 sizeof (UINT8),\r
216 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS\r
217 );\r
560ac77e
ZC
218 if (EFI_ERROR (Status)) {\r
219 return Status;\r
220 }\r
221 }\r
222\r
223 //\r
224 // Create "SecureBoot" variable with BS+RT attribute set.\r
225 //\r
c411b485 226 if ((SecureBootEnable == SECURE_BOOT_ENABLE) && (mPlatformMode == USER_MODE)) {\r
560ac77e
ZC
227 SecureBootMode = SECURE_BOOT_MODE_ENABLE;\r
228 } else {\r
229 SecureBootMode = SECURE_BOOT_MODE_DISABLE;\r
230 }\r
c411b485 231\r
560ac77e
ZC
232 Status = AuthServiceInternalUpdateVariable (\r
233 EFI_SECURE_BOOT_MODE_NAME,\r
234 &gEfiGlobalVariableGuid,\r
235 &SecureBootMode,\r
236 sizeof (UINT8),\r
237 EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS\r
238 );\r
239 if (EFI_ERROR (Status)) {\r
240 return Status;\r
241 }\r
242\r
e905fbb0
MK
243 DEBUG ((DEBUG_INFO, "Variable %s is %x\n", EFI_SETUP_MODE_NAME, mPlatformMode));\r
244 DEBUG ((DEBUG_INFO, "Variable %s is %x\n", EFI_SECURE_BOOT_MODE_NAME, SecureBootMode));\r
245 DEBUG ((DEBUG_INFO, "Variable %s is %x\n", EFI_SECURE_BOOT_ENABLE_NAME, SecureBootEnable));\r
560ac77e 246\r
a6811666
SZ
247 //\r
248 // Initialize "CustomMode" in STANDARD_SECURE_BOOT_MODE state.\r
249 //\r
250 CustomMode = STANDARD_SECURE_BOOT_MODE;\r
c411b485
MK
251 Status = AuthServiceInternalUpdateVariable (\r
252 EFI_CUSTOM_MODE_NAME,\r
253 &gEfiCustomModeEnableGuid,\r
254 &CustomMode,\r
255 sizeof (UINT8),\r
256 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS\r
257 );\r
a6811666
SZ
258 if (EFI_ERROR (Status)) {\r
259 return Status;\r
260 }\r
261\r
e905fbb0 262 DEBUG ((DEBUG_INFO, "Variable %s is %x\n", EFI_CUSTOM_MODE_NAME, CustomMode));\r
a6811666
SZ
263\r
264 //\r
265 // Check "certdb" variable's existence.\r
266 // If it doesn't exist, then create a new one with\r
267 // EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set.\r
268 //\r
269 Status = AuthServiceInternalFindVariable (\r
270 EFI_CERT_DB_NAME,\r
271 &gEfiCertDbGuid,\r
c411b485 272 (VOID **)&Data,\r
a6811666
SZ
273 &DataSize\r
274 );\r
275 if (EFI_ERROR (Status)) {\r
276 VarAttr = EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS;\r
277 ListSize = sizeof (UINT32);\r
278 Status = AuthServiceInternalUpdateVariable (\r
279 EFI_CERT_DB_NAME,\r
280 &gEfiCertDbGuid,\r
281 &ListSize,\r
282 sizeof (UINT32),\r
283 VarAttr\r
284 );\r
285 if (EFI_ERROR (Status)) {\r
286 return Status;\r
287 }\r
64b6a3ff
CZ
288 } else {\r
289 //\r
290 // Clean up Certs to make certDB & Time based auth variable consistent\r
291 //\r
c411b485 292 Status = CleanCertsFromDb ();\r
64b6a3ff 293 if (EFI_ERROR (Status)) {\r
e905fbb0 294 DEBUG ((DEBUG_ERROR, "Clean up CertDB fail! Status %x\n", Status));\r
64b6a3ff
CZ
295 return Status;\r
296 }\r
a6811666
SZ
297 }\r
298\r
98c2d961
CZ
299 //\r
300 // Create "certdbv" variable with RT+BS+AT set.\r
301 //\r
302 VarAttr = EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS;\r
303 ListSize = sizeof (UINT32);\r
304 Status = AuthServiceInternalUpdateVariable (\r
305 EFI_CERT_DB_VOLATILE_NAME,\r
306 &gEfiCertDbGuid,\r
307 &ListSize,\r
308 sizeof (UINT32),\r
309 VarAttr\r
310 );\r
311 if (EFI_ERROR (Status)) {\r
312 return Status;\r
313 }\r
314\r
a6811666
SZ
315 //\r
316 // Check "VendorKeysNv" variable's existence and create "VendorKeys" variable accordingly.\r
317 //\r
c411b485 318 Status = AuthServiceInternalFindVariable (EFI_VENDOR_KEYS_NV_VARIABLE_NAME, &gEfiVendorKeysNvGuid, (VOID **)&Data, &DataSize);\r
a6811666
SZ
319 if (!EFI_ERROR (Status)) {\r
320 mVendorKeyState = *(UINT8 *)Data;\r
321 } else {\r
322 //\r
323 // "VendorKeysNv" not exist, initialize it in VENDOR_KEYS_VALID state.\r
324 //\r
325 mVendorKeyState = VENDOR_KEYS_VALID;\r
c411b485
MK
326 Status = AuthServiceInternalUpdateVariable (\r
327 EFI_VENDOR_KEYS_NV_VARIABLE_NAME,\r
328 &gEfiVendorKeysNvGuid,\r
329 &mVendorKeyState,\r
330 sizeof (UINT8),\r
331 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS\r
332 );\r
a6811666
SZ
333 if (EFI_ERROR (Status)) {\r
334 return Status;\r
335 }\r
336 }\r
337\r
338 //\r
339 // Create "VendorKeys" variable with BS+RT attribute set.\r
340 //\r
341 Status = AuthServiceInternalUpdateVariable (\r
342 EFI_VENDOR_KEYS_VARIABLE_NAME,\r
343 &gEfiGlobalVariableGuid,\r
344 &mVendorKeyState,\r
345 sizeof (UINT8),\r
346 EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS\r
347 );\r
348 if (EFI_ERROR (Status)) {\r
349 return Status;\r
350 }\r
351\r
e905fbb0 352 DEBUG ((DEBUG_INFO, "Variable %s is %x\n", EFI_VENDOR_KEYS_VARIABLE_NAME, mVendorKeyState));\r
a6811666 353\r
c411b485
MK
354 AuthVarLibContextOut->StructVersion = AUTH_VAR_LIB_CONTEXT_OUT_STRUCT_VERSION;\r
355 AuthVarLibContextOut->StructSize = sizeof (AUTH_VAR_LIB_CONTEXT_OUT);\r
356 AuthVarLibContextOut->AuthVarEntry = mAuthVarEntry;\r
357 AuthVarLibContextOut->AuthVarEntryCount = ARRAY_SIZE (mAuthVarEntry);\r
358 mAuthVarAddressPointer[0] = (VOID **)&mCertDbStore;\r
359 mAuthVarAddressPointer[1] = (VOID **)&mHashCtx;\r
360 mAuthVarAddressPointer[2] = (VOID **)&mAuthVarLibContextIn;\r
361 mAuthVarAddressPointer[3] = (VOID **)&(mAuthVarLibContextIn->FindVariable),\r
362 mAuthVarAddressPointer[4] = (VOID **)&(mAuthVarLibContextIn->FindNextVariable),\r
363 mAuthVarAddressPointer[5] = (VOID **)&(mAuthVarLibContextIn->UpdateVariable),\r
364 mAuthVarAddressPointer[6] = (VOID **)&(mAuthVarLibContextIn->GetScratchBuffer),\r
365 mAuthVarAddressPointer[7] = (VOID **)&(mAuthVarLibContextIn->CheckRemainingSpaceForConsistency),\r
366 mAuthVarAddressPointer[8] = (VOID **)&(mAuthVarLibContextIn->AtRuntime),\r
367 AuthVarLibContextOut->AddressPointer = mAuthVarAddressPointer;\r
3318f893 368 AuthVarLibContextOut->AddressPointerCount = ARRAY_SIZE (mAuthVarAddressPointer);\r
a6811666
SZ
369\r
370 return Status;\r
371}\r
372\r
373/**\r
0130fdde 374 Process variable with EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set.\r
a6811666
SZ
375\r
376 @param[in] VariableName Name of the variable.\r
377 @param[in] VendorGuid Variable vendor GUID.\r
378 @param[in] Data Data pointer.\r
379 @param[in] DataSize Size of Data.\r
380 @param[in] Attributes Attribute value of the variable.\r
381\r
382 @retval EFI_SUCCESS The firmware has successfully stored the variable and its data as\r
383 defined by the Attributes.\r
384 @retval EFI_INVALID_PARAMETER Invalid parameter.\r
385 @retval EFI_WRITE_PROTECTED Variable is write-protected.\r
386 @retval EFI_OUT_OF_RESOURCES There is not enough resource.\r
d6b926e7 387 @retval EFI_SECURITY_VIOLATION The variable is with EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS\r
a6811666
SZ
388 set, but the AuthInfo does NOT pass the validation\r
389 check carried out by the firmware.\r
390 @retval EFI_UNSUPPORTED Unsupported to process authenticated variable.\r
391\r
392**/\r
393EFI_STATUS\r
394EFIAPI\r
395AuthVariableLibProcessVariable (\r
c411b485
MK
396 IN CHAR16 *VariableName,\r
397 IN EFI_GUID *VendorGuid,\r
398 IN VOID *Data,\r
399 IN UINTN DataSize,\r
400 IN UINT32 Attributes\r
a6811666
SZ
401 )\r
402{\r
c411b485 403 EFI_STATUS Status;\r
a6811666 404\r
c411b485 405 if (CompareGuid (VendorGuid, &gEfiGlobalVariableGuid) && (StrCmp (VariableName, EFI_PLATFORM_KEY_NAME) == 0)) {\r
a6811666
SZ
406 Status = ProcessVarWithPk (VariableName, VendorGuid, Data, DataSize, Attributes, TRUE);\r
407 } else if (CompareGuid (VendorGuid, &gEfiGlobalVariableGuid) && (StrCmp (VariableName, EFI_KEY_EXCHANGE_KEY_NAME) == 0)) {\r
408 Status = ProcessVarWithPk (VariableName, VendorGuid, Data, DataSize, Attributes, FALSE);\r
409 } else if (CompareGuid (VendorGuid, &gEfiImageSecurityDatabaseGuid) &&\r
410 ((StrCmp (VariableName, EFI_IMAGE_SECURITY_DATABASE) == 0) ||\r
411 (StrCmp (VariableName, EFI_IMAGE_SECURITY_DATABASE1) == 0) ||\r
412 (StrCmp (VariableName, EFI_IMAGE_SECURITY_DATABASE2) == 0)\r
c411b485
MK
413 ))\r
414 {\r
a6811666
SZ
415 Status = ProcessVarWithPk (VariableName, VendorGuid, Data, DataSize, Attributes, FALSE);\r
416 if (EFI_ERROR (Status)) {\r
417 Status = ProcessVarWithKek (VariableName, VendorGuid, Data, DataSize, Attributes);\r
418 }\r
419 } else {\r
420 Status = ProcessVariable (VariableName, VendorGuid, Data, DataSize, Attributes);\r
421 }\r
422\r
423 return Status;\r
424}\r