]> git.proxmox.com Git - mirror_edk2.git/blame - SecurityPkg/VariableAuthenticated/RuntimeDxe/AuthService.c
SecurityPkg Variable: Remove mStorageData buffer allocation and use Scratch buffer...
[mirror_edk2.git] / SecurityPkg / VariableAuthenticated / RuntimeDxe / AuthService.c
CommitLineData
0c18794e 1/** @file\r
2 Implement authentication services for the authenticated variable\r
3 service in UEFI2.2.\r
4\r
dc204d5a
JY
5 Caution: This module requires additional review when modified.\r
6 This driver will have external input - variable data. It may be input in SMM mode.\r
7 This external input must be validated carefully to avoid security issue like\r
8 buffer overflow, integer overflow.\r
9 Variable attribute should also be checked to avoid authentication bypass.\r
10\r
11 ProcessVarWithPk(), ProcessVarWithKek() and ProcessVariable() are the function to do\r
12 variable authentication.\r
13\r
14 VerifyTimeBasedPayload() and VerifyCounterBasedPayload() are sub function to do verification.\r
15 They will do basic validation for authentication data structure, then call crypto library\r
16 to verify the signature.\r
17\r
83758cdc 18Copyright (c) 2009 - 2013, Intel Corporation. All rights reserved.<BR>\r
2d3fb919 19This program and the accompanying materials\r
20are licensed and made available under the terms and conditions of the BSD License\r
21which accompanies this distribution. The full text of the license may be found at\r
0c18794e 22http://opensource.org/licenses/bsd-license.php\r
23\r
2d3fb919 24THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
0c18794e 25WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
26\r
27**/\r
28\r
29#include "Variable.h"\r
30#include "AuthService.h"\r
31\r
32///\r
33/// Global database array for scratch\r
2d3fb919 34///\r
0c18794e 35UINT8 mPubKeyStore[MAX_KEYDB_SIZE];\r
36UINT32 mPubKeyNumber;\r
f6e23353 37UINT8 mCertDbStore[MAX_CERTDB_SIZE];\r
0c18794e 38UINT32 mPlatformMode;\r
a555940b
FS
39UINT8 mVendorKeyState;\r
40\r
ae09f979 41EFI_GUID mSignatureSupport[] = {EFI_CERT_SHA1_GUID, EFI_CERT_SHA256_GUID, EFI_CERT_RSA2048_GUID, EFI_CERT_X509_GUID};\r
0c18794e 42//\r
43// Public Exponent of RSA Key.\r
44//\r
45CONST UINT8 mRsaE[] = { 0x01, 0x00, 0x01 };\r
46//\r
47// Hash context pointer\r
48//\r
49VOID *mHashCtx = NULL;\r
50\r
2d3fb919 51//\r
52// The serialization of the values of the VariableName, VendorGuid and Attributes\r
53// parameters of the SetVariable() call and the TimeStamp component of the\r
54// EFI_VARIABLE_AUTHENTICATION_2 descriptor followed by the variable's new value\r
55// i.e. (VariableName, VendorGuid, Attributes, TimeStamp, Data)\r
56//\r
57UINT8 *mSerializationRuntimeBuffer = NULL;\r
58\r
d912bad7 59//\r
60// Requirement for different signature type which have been defined in UEFI spec.\r
61// These data are used to peform SignatureList format check while setting PK/KEK variable.\r
62//\r
63EFI_SIGNATURE_ITEM mSupportSigItem[] = {\r
64//{SigType, SigHeaderSize, SigDataSize }\r
65 {EFI_CERT_SHA256_GUID, 0, 32 },\r
66 {EFI_CERT_RSA2048_GUID, 0, 256 },\r
67 {EFI_CERT_RSA2048_SHA256_GUID, 0, 256 },\r
68 {EFI_CERT_SHA1_GUID, 0, 20 },\r
69 {EFI_CERT_RSA2048_SHA1_GUID, 0, 256 },\r
70 {EFI_CERT_X509_GUID, 0, ((UINT32) ~0)},\r
71 {EFI_CERT_SHA224_GUID, 0, 28 },\r
72 {EFI_CERT_SHA384_GUID, 0, 48 },\r
73 {EFI_CERT_SHA512_GUID, 0, 64 }\r
74};\r
75\r
ecc722ad 76/**\r
77 Determine whether this operation needs a physical present user.\r
78\r
79 @param[in] VariableName Name of the Variable.\r
80 @param[in] VendorGuid GUID of the Variable.\r
81\r
82 @retval TRUE This variable is protected, only a physical present user could set this variable.\r
83 @retval FALSE This variable is not protected.\r
84 \r
85**/\r
86BOOLEAN\r
87NeedPhysicallyPresent(\r
88 IN CHAR16 *VariableName,\r
89 IN EFI_GUID *VendorGuid\r
90 )\r
91{\r
92 if ((CompareGuid (VendorGuid, &gEfiSecureBootEnableDisableGuid) && (StrCmp (VariableName, EFI_SECURE_BOOT_ENABLE_NAME) == 0))\r
93 || (CompareGuid (VendorGuid, &gEfiCustomModeEnableGuid) && (StrCmp (VariableName, EFI_CUSTOM_MODE_NAME) == 0))) {\r
94 return TRUE;\r
95 }\r
96 \r
97 return FALSE;\r
98}\r
99\r
100/**\r
101 Determine whether the platform is operating in Custom Secure Boot mode.\r
102\r
103 @retval TRUE The platform is operating in Custom mode.\r
104 @retval FALSE The platform is operating in Standard mode.\r
105\r
106**/\r
107BOOLEAN\r
108InCustomMode (\r
109 VOID\r
110 )\r
111{\r
112 VARIABLE_POINTER_TRACK Variable;\r
113\r
114 FindVariable (EFI_CUSTOM_MODE_NAME, &gEfiCustomModeEnableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
115 if (Variable.CurrPtr != NULL && *(GetVariableDataPtr (Variable.CurrPtr)) == CUSTOM_SECURE_BOOT_MODE) {\r
116 return TRUE;\r
117 }\r
118 \r
119 return FALSE;\r
120}\r
121\r
122\r
0c18794e 123/**\r
2d3fb919 124 Internal function to delete a Variable given its name and GUID, no authentication\r
125 required.\r
0c18794e 126\r
2d3fb919 127 @param[in] VariableName Name of the Variable.\r
128 @param[in] VendorGuid GUID of the Variable.\r
0c18794e 129\r
2d3fb919 130 @retval EFI_SUCCESS Variable deleted successfully.\r
131 @retval Others The driver failded to start the device.\r
0c18794e 132\r
133**/\r
134EFI_STATUS\r
2d3fb919 135DeleteVariable (\r
136 IN CHAR16 *VariableName,\r
137 IN EFI_GUID *VendorGuid\r
138 )\r
139{\r
140 EFI_STATUS Status;\r
141 VARIABLE_POINTER_TRACK Variable;\r
142\r
ecc722ad 143 Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
2d3fb919 144 if (EFI_ERROR (Status)) {\r
145 return EFI_SUCCESS;\r
146 }\r
147\r
148 ASSERT (Variable.CurrPtr != NULL);\r
149 return UpdateVariable (VariableName, VendorGuid, NULL, 0, 0, 0, 0, &Variable, NULL);\r
150}\r
0c18794e 151\r
152/**\r
153 Initializes for authenticated varibale service.\r
154\r
155 @retval EFI_SUCCESS Function successfully executed.\r
156 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resources.\r
157\r
158**/\r
159EFI_STATUS\r
160AutenticatedVariableServiceInitialize (\r
161 VOID\r
162 )\r
163{\r
164 EFI_STATUS Status;\r
165 VARIABLE_POINTER_TRACK Variable;\r
7aaf2fd6 166 VARIABLE_POINTER_TRACK PkVariable;\r
0c18794e 167 UINT8 VarValue;\r
168 UINT32 VarAttr;\r
169 UINT8 *Data;\r
170 UINTN DataSize;\r
171 UINTN CtxSize;\r
beda2356 172 UINT8 SecureBootMode;\r
173 UINT8 SecureBootEnable;\r
ecc722ad 174 UINT8 CustomMode;\r
ed47ae02 175 UINT32 ListSize;\r
2d3fb919 176\r
0c18794e 177 //\r
178 // Initialize hash context.\r
179 //\r
180 CtxSize = Sha256GetContextSize ();\r
181 mHashCtx = AllocateRuntimePool (CtxSize);\r
182 if (mHashCtx == NULL) {\r
183 return EFI_OUT_OF_RESOURCES;\r
184 }\r
185\r
0c18794e 186 //\r
2d3fb919 187 // Prepare runtime buffer for serialized data of time-based authenticated\r
188 // Variable, i.e. (VariableName, VendorGuid, Attributes, TimeStamp, Data).\r
189 //\r
190 mSerializationRuntimeBuffer = AllocateRuntimePool (PcdGet32 (PcdMaxVariableSize) + sizeof (EFI_GUID) + sizeof (UINT32) + sizeof (EFI_TIME));\r
191 if (mSerializationRuntimeBuffer == NULL) {\r
192 return EFI_OUT_OF_RESOURCES;\r
193 }\r
194\r
195 //\r
196 // Check "AuthVarKeyDatabase" variable's existence.\r
197 // If it doesn't exist, create a new one with initial value of 0 and EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS set.\r
0c18794e 198 //\r
199 Status = FindVariable (\r
2d3fb919 200 AUTHVAR_KEYDB_NAME,\r
201 &gEfiAuthenticatedVariableGuid,\r
202 &Variable,\r
ecc722ad 203 &mVariableModuleGlobal->VariableGlobal,\r
204 FALSE\r
0c18794e 205 );\r
206\r
207 if (Variable.CurrPtr == NULL) {\r
208 VarAttr = EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS;\r
209 VarValue = 0;\r
210 mPubKeyNumber = 0;\r
211 Status = UpdateVariable (\r
212 AUTHVAR_KEYDB_NAME,\r
213 &gEfiAuthenticatedVariableGuid,\r
214 &VarValue,\r
215 sizeof(UINT8),\r
216 VarAttr,\r
217 0,\r
218 0,\r
219 &Variable,\r
220 NULL\r
221 );\r
222 if (EFI_ERROR (Status)) {\r
223 return Status;\r
224 }\r
225 } else {\r
226 //\r
227 // Load database in global variable for cache.\r
228 //\r
229 DataSize = DataSizeOfVariable (Variable.CurrPtr);\r
230 Data = GetVariableDataPtr (Variable.CurrPtr);\r
231 ASSERT ((DataSize != 0) && (Data != NULL));\r
232 CopyMem (mPubKeyStore, (UINT8 *) Data, DataSize);\r
233 mPubKeyNumber = (UINT32) (DataSize / EFI_CERT_TYPE_RSA2048_SIZE);\r
234 }\r
7aaf2fd6 235\r
ecc722ad 236 FindVariable (EFI_PLATFORM_KEY_NAME, &gEfiGlobalVariableGuid, &PkVariable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
7aaf2fd6 237 if (PkVariable.CurrPtr == NULL) {\r
238 DEBUG ((EFI_D_INFO, "Variable %s does not exist.\n", EFI_PLATFORM_KEY_NAME));\r
239 } else {\r
240 DEBUG ((EFI_D_INFO, "Variable %s exists.\n", EFI_PLATFORM_KEY_NAME));\r
241 }\r
242 \r
0c18794e 243 //\r
a555940b 244 // Create "SetupMode" variable with BS+RT attribute set.\r
0c18794e 245 //\r
05a643f9 246 FindVariable (EFI_SETUP_MODE_NAME, &gEfiGlobalVariableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
247 if (PkVariable.CurrPtr == NULL) {\r
248 mPlatformMode = SETUP_MODE;\r
249 } else {\r
250 mPlatformMode = USER_MODE;\r
251 }\r
252 Status = UpdateVariable (\r
2d3fb919 253 EFI_SETUP_MODE_NAME,\r
254 &gEfiGlobalVariableGuid,\r
05a643f9 255 &mPlatformMode,\r
256 sizeof(UINT8),\r
257 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r
258 0,\r
259 0,\r
2d3fb919 260 &Variable,\r
05a643f9 261 NULL\r
0c18794e 262 );\r
05a643f9 263 if (EFI_ERROR (Status)) {\r
264 return Status;\r
0c18794e 265 }\r
05a643f9 266 \r
0c18794e 267 //\r
a555940b 268 // Create "SignatureSupport" variable with BS+RT attribute set.\r
0c18794e 269 //\r
05a643f9 270 FindVariable (EFI_SIGNATURE_SUPPORT_NAME, &gEfiGlobalVariableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
271 Status = UpdateVariable (\r
272 EFI_SIGNATURE_SUPPORT_NAME,\r
273 &gEfiGlobalVariableGuid,\r
274 mSignatureSupport,\r
275 sizeof(mSignatureSupport),\r
276 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r
277 0,\r
278 0,\r
279 &Variable,\r
280 NULL\r
281 );\r
282 if (EFI_ERROR (Status)) {\r
283 return Status;\r
0c18794e 284 }\r
beda2356 285\r
286 //\r
287 // If "SecureBootEnable" variable exists, then update "SecureBoot" variable.\r
2d3fb919 288 // If "SecureBootEnable" variable is SECURE_BOOT_ENABLE and in USER_MODE, Set "SecureBoot" variable to SECURE_BOOT_MODE_ENABLE.\r
beda2356 289 // If "SecureBootEnable" variable is SECURE_BOOT_DISABLE, Set "SecureBoot" variable to SECURE_BOOT_MODE_DISABLE.\r
290 //\r
8f8ca22e 291 SecureBootEnable = SECURE_BOOT_DISABLE;\r
ecc722ad 292 FindVariable (EFI_SECURE_BOOT_ENABLE_NAME, &gEfiSecureBootEnableDisableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
beda2356 293 if (Variable.CurrPtr != NULL) {\r
294 SecureBootEnable = *(GetVariableDataPtr (Variable.CurrPtr));\r
2d3fb919 295 } else if (mPlatformMode == USER_MODE) {\r
296 //\r
297 // "SecureBootEnable" not exist, initialize it in USER_MODE.\r
298 //\r
8f8ca22e 299 SecureBootEnable = SECURE_BOOT_ENABLE;\r
beda2356 300 Status = UpdateVariable (\r
2d3fb919 301 EFI_SECURE_BOOT_ENABLE_NAME,\r
302 &gEfiSecureBootEnableDisableGuid,\r
303 &SecureBootEnable,\r
304 sizeof (UINT8),\r
305 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,\r
306 0,\r
307 0,\r
beda2356 308 &Variable,\r
309 NULL\r
310 );\r
311 if (EFI_ERROR (Status)) {\r
312 return Status;\r
313 }\r
314 }\r
315\r
05a643f9 316 //\r
a555940b 317 // Create "SecureBoot" variable with BS+RT attribute set.\r
05a643f9 318 //\r
2d3fb919 319 if (SecureBootEnable == SECURE_BOOT_ENABLE && mPlatformMode == USER_MODE) {\r
320 SecureBootMode = SECURE_BOOT_MODE_ENABLE;\r
321 } else {\r
322 SecureBootMode = SECURE_BOOT_MODE_DISABLE;\r
323 }\r
ecc722ad 324 FindVariable (EFI_SECURE_BOOT_MODE_NAME, &gEfiGlobalVariableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
2d3fb919 325 Status = UpdateVariable (\r
326 EFI_SECURE_BOOT_MODE_NAME,\r
327 &gEfiGlobalVariableGuid,\r
328 &SecureBootMode,\r
329 sizeof (UINT8),\r
05a643f9 330 EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS,\r
2d3fb919 331 0,\r
332 0,\r
333 &Variable,\r
334 NULL\r
335 );\r
336 if (EFI_ERROR (Status)) {\r
337 return Status;\r
338 }\r
339\r
7aaf2fd6 340 DEBUG ((EFI_D_INFO, "Variable %s is %x\n", EFI_SETUP_MODE_NAME, mPlatformMode));\r
341 DEBUG ((EFI_D_INFO, "Variable %s is %x\n", EFI_SECURE_BOOT_MODE_NAME, SecureBootMode));\r
342 DEBUG ((EFI_D_INFO, "Variable %s is %x\n", EFI_SECURE_BOOT_ENABLE_NAME, SecureBootEnable));\r
343\r
0c18794e 344 //\r
f71ed839 345 // Initialize "CustomMode" in STANDARD_SECURE_BOOT_MODE state.\r
0c18794e 346 //\r
ecc722ad 347 FindVariable (EFI_CUSTOM_MODE_NAME, &gEfiCustomModeEnableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
f71ed839 348 CustomMode = STANDARD_SECURE_BOOT_MODE;\r
349 Status = UpdateVariable (\r
350 EFI_CUSTOM_MODE_NAME,\r
351 &gEfiCustomModeEnableGuid,\r
352 &CustomMode,\r
353 sizeof (UINT8),\r
354 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,\r
355 0,\r
356 0,\r
357 &Variable,\r
358 NULL\r
359 );\r
360 if (EFI_ERROR (Status)) {\r
361 return Status;\r
0c18794e 362 }\r
ecc722ad 363 \r
364 DEBUG ((EFI_D_INFO, "Variable %s is %x\n", EFI_CUSTOM_MODE_NAME, CustomMode));\r
2d3fb919 365\r
ed47ae02 366 //\r
367 // Check "certdb" variable's existence.\r
368 // If it doesn't exist, then create a new one with \r
369 // EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set.\r
370 //\r
371 Status = FindVariable (\r
372 EFI_CERT_DB_NAME,\r
373 &gEfiCertDbGuid,\r
374 &Variable,\r
375 &mVariableModuleGlobal->VariableGlobal,\r
376 FALSE\r
377 );\r
378\r
379 if (Variable.CurrPtr == NULL) {\r
380 VarAttr = EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS;\r
f6e23353 381 ListSize = sizeof (UINT32);\r
ed47ae02 382 Status = UpdateVariable (\r
383 EFI_CERT_DB_NAME,\r
384 &gEfiCertDbGuid,\r
385 &ListSize,\r
386 sizeof (UINT32),\r
387 VarAttr,\r
388 0,\r
389 0,\r
390 &Variable,\r
391 NULL\r
392 );\r
f6e23353 393 if (EFI_ERROR (Status)) {\r
394 return Status;\r
395 }\r
ed47ae02 396 } \r
397\r
a555940b
FS
398 //\r
399 // Check "VendorKeysNv" variable's existence and create "VendorKeys" variable accordingly.\r
400 //\r
401 FindVariable (EFI_VENDOR_KEYS_NV_VARIABLE_NAME, &gEfiVendorKeysNvGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
402 if (Variable.CurrPtr != NULL) {\r
403 mVendorKeyState = *(GetVariableDataPtr (Variable.CurrPtr));\r
404 } else {\r
405 //\r
406 // "VendorKeysNv" not exist, initialize it in VENDOR_KEYS_VALID state.\r
407 //\r
408 mVendorKeyState = VENDOR_KEYS_VALID;\r
409 Status = UpdateVariable (\r
410 EFI_VENDOR_KEYS_NV_VARIABLE_NAME,\r
411 &gEfiVendorKeysNvGuid,\r
412 &mVendorKeyState,\r
413 sizeof (UINT8),\r
414 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS,\r
415 0,\r
416 0,\r
417 &Variable,\r
418 NULL\r
419 );\r
420 if (EFI_ERROR (Status)) {\r
421 return Status;\r
422 }\r
423 }\r
424\r
425 //\r
426 // Create "VendorKeys" variable with BS+RT attribute set.\r
427 //\r
428 FindVariable (EFI_VENDOR_KEYS_VARIABLE_NAME, &gEfiGlobalVariableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
429 Status = UpdateVariable (\r
430 EFI_VENDOR_KEYS_VARIABLE_NAME,\r
431 &gEfiGlobalVariableGuid,\r
432 &mVendorKeyState,\r
433 sizeof (UINT8),\r
434 EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS,\r
435 0,\r
436 0,\r
437 &Variable,\r
438 NULL\r
439 );\r
440 if (EFI_ERROR (Status)) {\r
441 return Status;\r
442 }\r
443\r
444 DEBUG ((EFI_D_INFO, "Variable %s is %x\n", EFI_VENDOR_KEYS_VARIABLE_NAME, mVendorKeyState));\r
445\r
0c18794e 446 return Status;\r
447}\r
448\r
449/**\r
450 Add public key in store and return its index.\r
451\r
452 @param[in] PubKey Input pointer to Public Key data\r
453\r
454 @return Index of new added item\r
455\r
456**/\r
457UINT32\r
458AddPubKeyInStore (\r
459 IN UINT8 *PubKey\r
460 )\r
461{\r
462 EFI_STATUS Status;\r
463 BOOLEAN IsFound;\r
464 UINT32 Index;\r
465 VARIABLE_POINTER_TRACK Variable;\r
466 UINT8 *Ptr;\r
83758cdc 467 UINT8 *Data;\r
468 UINTN DataSize;\r
0c18794e 469\r
470 if (PubKey == NULL) {\r
471 return 0;\r
472 }\r
473\r
474 Status = FindVariable (\r
475 AUTHVAR_KEYDB_NAME,\r
476 &gEfiAuthenticatedVariableGuid,\r
477 &Variable,\r
ecc722ad 478 &mVariableModuleGlobal->VariableGlobal,\r
479 FALSE\r
0c18794e 480 );\r
481 ASSERT_EFI_ERROR (Status);\r
ca5a7d87 482 if (EFI_ERROR (Status)) {\r
483 return 0;\r
484 }\r
485\r
0c18794e 486 //\r
487 // Check whether the public key entry does exist.\r
488 //\r
489 IsFound = FALSE;\r
490 for (Ptr = mPubKeyStore, Index = 1; Index <= mPubKeyNumber; Index++) {\r
491 if (CompareMem (Ptr, PubKey, EFI_CERT_TYPE_RSA2048_SIZE) == 0) {\r
492 IsFound = TRUE;\r
493 break;\r
494 }\r
495 Ptr += EFI_CERT_TYPE_RSA2048_SIZE;\r
496 }\r
497\r
498 if (!IsFound) {\r
499 //\r
500 // Add public key in database.\r
501 //\r
502 if (mPubKeyNumber == MAX_KEY_NUM) {\r
503 //\r
83758cdc 504 // Public key dadatase is full, try to reclaim invalid key.\r
0c18794e 505 //\r
83758cdc 506 if (AtRuntime ()) {\r
507 //\r
508 // NV storage can't reclaim at runtime.\r
509 //\r
510 return 0;\r
511 }\r
512 \r
513 Status = Reclaim (\r
514 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,\r
515 &mVariableModuleGlobal->NonVolatileLastVariableOffset,\r
516 FALSE,\r
517 NULL,\r
7baf3c69
SZ
518 NULL,\r
519 0,\r
83758cdc 520 TRUE\r
521 );\r
522 if (EFI_ERROR (Status)) {\r
523 return 0;\r
524 }\r
525\r
526 Status = FindVariable (\r
527 AUTHVAR_KEYDB_NAME,\r
528 &gEfiAuthenticatedVariableGuid,\r
529 &Variable,\r
530 &mVariableModuleGlobal->VariableGlobal,\r
531 FALSE\r
532 );\r
533 ASSERT_EFI_ERROR (Status);\r
ca5a7d87 534 if (EFI_ERROR (Status)) {\r
535 return 0;\r
536 }\r
83758cdc 537\r
538 DataSize = DataSizeOfVariable (Variable.CurrPtr);\r
539 Data = GetVariableDataPtr (Variable.CurrPtr);\r
540 ASSERT ((DataSize != 0) && (Data != NULL));\r
541 CopyMem (mPubKeyStore, (UINT8 *) Data, DataSize);\r
542 mPubKeyNumber = (UINT32) (DataSize / EFI_CERT_TYPE_RSA2048_SIZE);\r
543\r
544 if (mPubKeyNumber == MAX_KEY_NUM) {\r
545 return 0;\r
546 } \r
0c18794e 547 }\r
548\r
549 CopyMem (mPubKeyStore + mPubKeyNumber * EFI_CERT_TYPE_RSA2048_SIZE, PubKey, EFI_CERT_TYPE_RSA2048_SIZE);\r
550 Index = ++mPubKeyNumber;\r
551 //\r
552 // Update public key database variable.\r
553 //\r
554 Status = UpdateVariable (\r
555 AUTHVAR_KEYDB_NAME,\r
556 &gEfiAuthenticatedVariableGuid,\r
557 mPubKeyStore,\r
558 mPubKeyNumber * EFI_CERT_TYPE_RSA2048_SIZE,\r
559 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS,\r
560 0,\r
561 0,\r
562 &Variable,\r
563 NULL\r
564 );\r
565 ASSERT_EFI_ERROR (Status);\r
566 }\r
567\r
568 return Index;\r
569}\r
570\r
571/**\r
85560919 572 Verify data payload with AuthInfo in EFI_CERT_TYPE_RSA2048_SHA256_GUID type.\r
0c18794e 573 Follow the steps in UEFI2.2.\r
574\r
dc204d5a
JY
575 Caution: This function may receive untrusted input.\r
576 This function may be invoked in SMM mode, and datasize and data are external input.\r
577 This function will do basic validation, before parse the data.\r
578 This function will parse the authentication carefully to avoid security issues, like\r
579 buffer overflow, integer overflow.\r
580\r
0c18794e 581 @param[in] Data Pointer to data with AuthInfo.\r
582 @param[in] DataSize Size of Data.\r
583 @param[in] PubKey Public key used for verification.\r
584\r
2d3fb919 585 @retval EFI_INVALID_PARAMETER Invalid parameter.\r
0c18794e 586 @retval EFI_SECURITY_VIOLATION If authentication failed.\r
2d3fb919 587 @retval EFI_SUCCESS Authentication successful.\r
0c18794e 588\r
589**/\r
590EFI_STATUS\r
591VerifyCounterBasedPayload (\r
592 IN UINT8 *Data,\r
593 IN UINTN DataSize,\r
594 IN UINT8 *PubKey\r
595 )\r
596{\r
597 BOOLEAN Status;\r
598 EFI_VARIABLE_AUTHENTICATION *CertData;\r
599 EFI_CERT_BLOCK_RSA_2048_SHA256 *CertBlock;\r
600 UINT8 Digest[SHA256_DIGEST_SIZE];\r
601 VOID *Rsa;\r
275beb2b 602 UINTN PayloadSize;\r
603 \r
604 PayloadSize = DataSize - AUTHINFO_SIZE;\r
0c18794e 605 Rsa = NULL;\r
606 CertData = NULL;\r
607 CertBlock = NULL;\r
608\r
609 if (Data == NULL || PubKey == NULL) {\r
610 return EFI_INVALID_PARAMETER;\r
611 }\r
612\r
613 CertData = (EFI_VARIABLE_AUTHENTICATION *) Data;\r
614 CertBlock = (EFI_CERT_BLOCK_RSA_2048_SHA256 *) (CertData->AuthInfo.CertData);\r
615\r
616 //\r
617 // wCertificateType should be WIN_CERT_TYPE_EFI_GUID.\r
85560919 618 // Cert type should be EFI_CERT_TYPE_RSA2048_SHA256_GUID.\r
0c18794e 619 //\r
620 if ((CertData->AuthInfo.Hdr.wCertificateType != WIN_CERT_TYPE_EFI_GUID) ||\r
85560919 621 !CompareGuid (&CertData->AuthInfo.CertType, &gEfiCertTypeRsa2048Sha256Guid)\r
0c18794e 622 ) {\r
623 //\r
624 // Invalid AuthInfo type, return EFI_SECURITY_VIOLATION.\r
625 //\r
626 return EFI_SECURITY_VIOLATION;\r
627 }\r
628 //\r
629 // Hash data payload with SHA256.\r
630 //\r
631 ZeroMem (Digest, SHA256_DIGEST_SIZE);\r
632 Status = Sha256Init (mHashCtx);\r
633 if (!Status) {\r
634 goto Done;\r
635 }\r
275beb2b 636 Status = Sha256Update (mHashCtx, Data + AUTHINFO_SIZE, PayloadSize);\r
637 if (!Status) {\r
638 goto Done;\r
639 }\r
640 //\r
641 // Hash Size.\r
642 //\r
643 Status = Sha256Update (mHashCtx, &PayloadSize, sizeof (UINTN));\r
0c18794e 644 if (!Status) {\r
645 goto Done;\r
646 }\r
647 //\r
648 // Hash Monotonic Count.\r
649 //\r
650 Status = Sha256Update (mHashCtx, &CertData->MonotonicCount, sizeof (UINT64));\r
651 if (!Status) {\r
652 goto Done;\r
653 }\r
654 Status = Sha256Final (mHashCtx, Digest);\r
655 if (!Status) {\r
656 goto Done;\r
657 }\r
658 //\r
659 // Generate & Initialize RSA Context.\r
660 //\r
661 Rsa = RsaNew ();\r
662 ASSERT (Rsa != NULL);\r
2d3fb919 663 //\r
0c18794e 664 // Set RSA Key Components.\r
665 // NOTE: Only N and E are needed to be set as RSA public key for signature verification.\r
666 //\r
667 Status = RsaSetKey (Rsa, RsaKeyN, PubKey, EFI_CERT_TYPE_RSA2048_SIZE);\r
668 if (!Status) {\r
669 goto Done;\r
670 }\r
671 Status = RsaSetKey (Rsa, RsaKeyE, mRsaE, sizeof (mRsaE));\r
672 if (!Status) {\r
673 goto Done;\r
674 }\r
675 //\r
676 // Verify the signature.\r
677 //\r
678 Status = RsaPkcs1Verify (\r
2d3fb919 679 Rsa,\r
680 Digest,\r
681 SHA256_DIGEST_SIZE,\r
682 CertBlock->Signature,\r
0c18794e 683 EFI_CERT_TYPE_RSA2048_SHA256_SIZE\r
684 );\r
685\r
686Done:\r
687 if (Rsa != NULL) {\r
688 RsaFree (Rsa);\r
689 }\r
690 if (Status) {\r
691 return EFI_SUCCESS;\r
692 } else {\r
693 return EFI_SECURITY_VIOLATION;\r
694 }\r
695}\r
696\r
0c18794e 697/**\r
698 Update platform mode.\r
699\r
700 @param[in] Mode SETUP_MODE or USER_MODE.\r
701\r
702 @return EFI_INVALID_PARAMETER Invalid parameter.\r
703 @return EFI_SUCCESS Update platform mode successfully.\r
704\r
705**/\r
706EFI_STATUS\r
707UpdatePlatformMode (\r
708 IN UINT32 Mode\r
709 )\r
710{\r
711 EFI_STATUS Status;\r
712 VARIABLE_POINTER_TRACK Variable;\r
0c18794e 713 UINT8 SecureBootMode;\r
beda2356 714 UINT8 SecureBootEnable;\r
715 UINTN VariableDataSize;\r
2d3fb919 716\r
0c18794e 717 Status = FindVariable (\r
2d3fb919 718 EFI_SETUP_MODE_NAME,\r
719 &gEfiGlobalVariableGuid,\r
720 &Variable,\r
ecc722ad 721 &mVariableModuleGlobal->VariableGlobal,\r
722 FALSE\r
0c18794e 723 );\r
724 if (EFI_ERROR (Status)) {\r
725 return Status;\r
726 }\r
727\r
05a643f9 728 //\r
729 // Update the value of SetupMode variable by a simple mem copy, this could avoid possible\r
730 // variable storage reclaim at runtime.\r
731 //\r
732 mPlatformMode = (UINT8) Mode;\r
733 CopyMem (GetVariableDataPtr (Variable.CurrPtr), &mPlatformMode, sizeof(UINT8));\r
0c18794e 734\r
2d3fb919 735 if (AtRuntime ()) {\r
736 //\r
737 // SecureBoot Variable indicates whether the platform firmware is operating\r
738 // in Secure boot mode (1) or not (0), so we should not change SecureBoot\r
739 // Variable in runtime.\r
740 //\r
741 return Status;\r
742 }\r
743\r
0c18794e 744 //\r
745 // Check "SecureBoot" variable's existence.\r
746 // If it doesn't exist, firmware has no capability to perform driver signing verification,\r
747 // then set "SecureBoot" to 0.\r
748 //\r
749 Status = FindVariable (\r
2d3fb919 750 EFI_SECURE_BOOT_MODE_NAME,\r
751 &gEfiGlobalVariableGuid,\r
752 &Variable,\r
ecc722ad 753 &mVariableModuleGlobal->VariableGlobal,\r
754 FALSE\r
0c18794e 755 );\r
756 //\r
757 // If "SecureBoot" variable exists, then check "SetupMode" variable update.\r
758 // If "SetupMode" variable is USER_MODE, "SecureBoot" variable is set to 1.\r
759 // If "SetupMode" variable is SETUP_MODE, "SecureBoot" variable is set to 0.\r
760 //\r
761 if (Variable.CurrPtr == NULL) {\r
762 SecureBootMode = SECURE_BOOT_MODE_DISABLE;\r
763 } else {\r
764 if (mPlatformMode == USER_MODE) {\r
765 SecureBootMode = SECURE_BOOT_MODE_ENABLE;\r
766 } else if (mPlatformMode == SETUP_MODE) {\r
767 SecureBootMode = SECURE_BOOT_MODE_DISABLE;\r
768 } else {\r
769 return EFI_NOT_FOUND;\r
770 }\r
771 }\r
772\r
beda2356 773 Status = UpdateVariable (\r
0c18794e 774 EFI_SECURE_BOOT_MODE_NAME,\r
775 &gEfiGlobalVariableGuid,\r
776 &SecureBootMode,\r
777 sizeof(UINT8),\r
0ba17ade 778 EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS,\r
0c18794e 779 0,\r
780 0,\r
781 &Variable,\r
782 NULL\r
783 );\r
beda2356 784 if (EFI_ERROR (Status)) {\r
785 return Status;\r
786 }\r
787\r
788 //\r
789 // Check "SecureBootEnable" variable's existence. It can enable/disable secure boot feature.\r
790 //\r
791 Status = FindVariable (\r
2d3fb919 792 EFI_SECURE_BOOT_ENABLE_NAME,\r
793 &gEfiSecureBootEnableDisableGuid,\r
794 &Variable,\r
ecc722ad 795 &mVariableModuleGlobal->VariableGlobal,\r
796 FALSE\r
beda2356 797 );\r
2d3fb919 798\r
beda2356 799 if (SecureBootMode == SECURE_BOOT_MODE_ENABLE) {\r
800 //\r
801 // Create the "SecureBootEnable" variable as secure boot is enabled.\r
802 //\r
803 SecureBootEnable = SECURE_BOOT_ENABLE;\r
804 VariableDataSize = sizeof (SecureBootEnable);\r
805 } else {\r
806 //\r
2d3fb919 807 // Delete the "SecureBootEnable" variable if this variable exist as "SecureBoot"\r
beda2356 808 // variable is not in secure boot state.\r
809 //\r
810 if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {\r
811 return EFI_SUCCESS;\r
812 }\r
813 SecureBootEnable = SECURE_BOOT_DISABLE;\r
814 VariableDataSize = 0;\r
815 }\r
2d3fb919 816\r
beda2356 817 Status = UpdateVariable (\r
2d3fb919 818 EFI_SECURE_BOOT_ENABLE_NAME,\r
819 &gEfiSecureBootEnableDisableGuid,\r
820 &SecureBootEnable,\r
821 VariableDataSize,\r
822 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,\r
823 0,\r
824 0,\r
beda2356 825 &Variable,\r
826 NULL\r
827 );\r
828 return Status;\r
0c18794e 829}\r
830\r
d912bad7 831/**\r
e77f9ef6 832 Check input data form to make sure it is a valid EFI_SIGNATURE_LIST for PK/KEK/db/dbx variable.\r
d912bad7 833\r
834 @param[in] VariableName Name of Variable to be check.\r
835 @param[in] VendorGuid Variable vendor GUID.\r
836 @param[in] Data Point to the variable data to be checked.\r
837 @param[in] DataSize Size of Data.\r
838\r
839 @return EFI_INVALID_PARAMETER Invalid signature list format.\r
840 @return EFI_SUCCESS Passed signature list format check successfully.\r
841 \r
842**/\r
843EFI_STATUS\r
844CheckSignatureListFormat(\r
845 IN CHAR16 *VariableName,\r
846 IN EFI_GUID *VendorGuid,\r
847 IN VOID *Data,\r
848 IN UINTN DataSize\r
849 )\r
850{\r
851 EFI_SIGNATURE_LIST *SigList;\r
852 UINTN SigDataSize;\r
853 UINT32 Index;\r
854 UINT32 SigCount;\r
855 BOOLEAN IsPk;\r
e77f9ef6 856 VOID *RsaContext;\r
857 EFI_SIGNATURE_DATA *CertData;\r
858 UINTN CertLen;\r
d912bad7 859\r
860 if (DataSize == 0) {\r
861 return EFI_SUCCESS;\r
862 }\r
863\r
864 ASSERT (VariableName != NULL && VendorGuid != NULL && Data != NULL);\r
865\r
866 if (CompareGuid (VendorGuid, &gEfiGlobalVariableGuid) && (StrCmp (VariableName, EFI_PLATFORM_KEY_NAME) == 0)){\r
867 IsPk = TRUE;\r
e77f9ef6 868 } else if ((CompareGuid (VendorGuid, &gEfiGlobalVariableGuid) && StrCmp (VariableName, EFI_KEY_EXCHANGE_KEY_NAME) == 0) ||\r
869 (CompareGuid (VendorGuid, &gEfiImageSecurityDatabaseGuid) && \r
870 (StrCmp (VariableName, EFI_IMAGE_SECURITY_DATABASE) == 0 || StrCmp (VariableName, EFI_IMAGE_SECURITY_DATABASE1) == 0))){\r
d912bad7 871 IsPk = FALSE;\r
872 } else {\r
873 return EFI_SUCCESS;\r
874 }\r
875\r
876 SigCount = 0;\r
877 SigList = (EFI_SIGNATURE_LIST *) Data;\r
878 SigDataSize = DataSize;\r
e77f9ef6 879 RsaContext = NULL;\r
d912bad7 880\r
881 //\r
882 // Walk throuth the input signature list and check the data format.\r
883 // If any signature is incorrectly formed, the whole check will fail.\r
884 //\r
885 while ((SigDataSize > 0) && (SigDataSize >= SigList->SignatureListSize)) {\r
886 for (Index = 0; Index < (sizeof (mSupportSigItem) / sizeof (EFI_SIGNATURE_ITEM)); Index++ ) {\r
887 if (CompareGuid (&SigList->SignatureType, &mSupportSigItem[Index].SigType)) {\r
888 //\r
889 // The value of SignatureSize should always be 16 (size of SignatureOwner \r
890 // component) add the data length according to signature type.\r
891 //\r
892 if (mSupportSigItem[Index].SigDataSize != ((UINT32) ~0) && \r
893 (SigList->SignatureSize - sizeof (EFI_GUID)) != mSupportSigItem[Index].SigDataSize) {\r
894 return EFI_INVALID_PARAMETER;\r
895 }\r
896 if (mSupportSigItem[Index].SigHeaderSize != ((UINTN) ~0) &&\r
897 SigList->SignatureHeaderSize != mSupportSigItem[Index].SigHeaderSize) {\r
898 return EFI_INVALID_PARAMETER;\r
899 }\r
900 break;\r
901 }\r
902 }\r
903\r
904 if (Index == (sizeof (mSupportSigItem) / sizeof (EFI_SIGNATURE_ITEM))) {\r
905 //\r
906 // Undefined signature type.\r
907 //\r
908 return EFI_INVALID_PARAMETER;\r
909 }\r
910\r
e77f9ef6 911 if (CompareGuid (&SigList->SignatureType, &gEfiCertX509Guid)) {\r
912 //\r
913 // Try to retrieve the RSA public key from the X.509 certificate.\r
914 // If this operation fails, it's not a valid certificate.\r
915 //\r
916 RsaContext = RsaNew ();\r
917 if (RsaContext == NULL) {\r
918 return EFI_INVALID_PARAMETER;\r
919 }\r
920 CertData = (EFI_SIGNATURE_DATA *) ((UINT8 *) SigList + sizeof (EFI_SIGNATURE_LIST) + SigList->SignatureHeaderSize);\r
921 CertLen = SigList->SignatureSize - sizeof (EFI_GUID);\r
922 if (!RsaGetPublicKeyFromX509 (CertData->SignatureData, CertLen, &RsaContext)) {\r
923 RsaFree (RsaContext);\r
924 return EFI_INVALID_PARAMETER;\r
925 }\r
926 RsaFree (RsaContext);\r
927 }\r
928\r
d912bad7 929 if ((SigList->SignatureListSize - sizeof (EFI_SIGNATURE_LIST) - SigList->SignatureHeaderSize) % SigList->SignatureSize != 0) {\r
930 return EFI_INVALID_PARAMETER;\r
931 }\r
932 SigCount += (SigList->SignatureListSize - sizeof (EFI_SIGNATURE_LIST) - SigList->SignatureHeaderSize) / SigList->SignatureSize;\r
933 \r
934 SigDataSize -= SigList->SignatureListSize;\r
935 SigList = (EFI_SIGNATURE_LIST *) ((UINT8 *) SigList + SigList->SignatureListSize);\r
936 }\r
937\r
938 if (((UINTN) SigList - (UINTN) Data) != DataSize) {\r
939 return EFI_INVALID_PARAMETER;\r
940 }\r
941\r
942 if (IsPk && SigCount > 1) {\r
943 return EFI_INVALID_PARAMETER;\r
944 }\r
945\r
946 return EFI_SUCCESS;\r
947}\r
948\r
a555940b
FS
949/**\r
950 Update "VendorKeys" variable to record the out of band secure boot key modification.\r
951\r
952 @return EFI_SUCCESS Variable is updated successfully.\r
953 @return Others Failed to update variable.\r
954 \r
955**/\r
956EFI_STATUS\r
957VendorKeyIsModified (\r
958 VOID\r
959 )\r
960{\r
961 EFI_STATUS Status;\r
962 VARIABLE_POINTER_TRACK Variable;\r
963\r
964 if (mVendorKeyState == VENDOR_KEYS_MODIFIED) {\r
965 return EFI_SUCCESS;\r
966 }\r
967 mVendorKeyState = VENDOR_KEYS_MODIFIED;\r
968 \r
969 FindVariable (EFI_VENDOR_KEYS_NV_VARIABLE_NAME, &gEfiVendorKeysNvGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
970 Status = UpdateVariable (\r
971 EFI_VENDOR_KEYS_NV_VARIABLE_NAME,\r
972 &gEfiVendorKeysNvGuid,\r
973 &mVendorKeyState,\r
974 sizeof (UINT8),\r
975 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS,\r
976 0,\r
977 0,\r
978 &Variable,\r
979 NULL\r
980 );\r
981 if (EFI_ERROR (Status)) {\r
982 return Status;\r
983 }\r
984\r
985 FindVariable (EFI_VENDOR_KEYS_VARIABLE_NAME, &gEfiGlobalVariableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
986 return UpdateVariable (\r
987 EFI_VENDOR_KEYS_VARIABLE_NAME,\r
988 &gEfiGlobalVariableGuid,\r
989 &mVendorKeyState,\r
990 sizeof (UINT8),\r
991 EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS,\r
992 0,\r
993 0,\r
994 &Variable,\r
995 NULL\r
996 );\r
997}\r
998\r
0c18794e 999/**\r
1000 Process variable with platform key for verification.\r
1001\r
dc204d5a
JY
1002 Caution: This function may receive untrusted input.\r
1003 This function may be invoked in SMM mode, and datasize and data are external input.\r
1004 This function will do basic validation, before parse the data.\r
1005 This function will parse the authentication carefully to avoid security issues, like\r
1006 buffer overflow, integer overflow.\r
1007 This function will check attribute carefully to avoid authentication bypass.\r
1008\r
0c18794e 1009 @param[in] VariableName Name of Variable to be found.\r
1010 @param[in] VendorGuid Variable vendor GUID.\r
1011 @param[in] Data Data pointer.\r
1012 @param[in] DataSize Size of Data found. If size is less than the\r
1013 data, this value contains the required size.\r
1014 @param[in] Variable The variable information which is used to keep track of variable usage.\r
1015 @param[in] Attributes Attribute value of the variable\r
1016 @param[in] IsPk Indicate whether it is to process pk.\r
1017\r
1018 @return EFI_INVALID_PARAMETER Invalid parameter.\r
2d3fb919 1019 @return EFI_SECURITY_VIOLATION The variable does NOT pass the validation.\r
1020 check carried out by the firmware.\r
0c18794e 1021 @return EFI_SUCCESS Variable passed validation successfully.\r
1022\r
1023**/\r
1024EFI_STATUS\r
1025ProcessVarWithPk (\r
1026 IN CHAR16 *VariableName,\r
1027 IN EFI_GUID *VendorGuid,\r
1028 IN VOID *Data,\r
1029 IN UINTN DataSize,\r
1030 IN VARIABLE_POINTER_TRACK *Variable,\r
1031 IN UINT32 Attributes OPTIONAL,\r
1032 IN BOOLEAN IsPk\r
1033 )\r
1034{\r
1035 EFI_STATUS Status;\r
0c18794e 1036 BOOLEAN Del;\r
2d3fb919 1037 UINT8 *Payload;\r
1038 UINTN PayloadSize;\r
0c18794e 1039\r
8c1babfd 1040 if ((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0 || \r
1041 (Attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) == 0) {\r
0c18794e 1042 //\r
05a643f9 1043 // PK, KEK and db/dbx should set EFI_VARIABLE_NON_VOLATILE attribute and should be a time-based\r
8c1babfd 1044 // authenticated variable.\r
0c18794e 1045 //\r
1046 return EFI_INVALID_PARAMETER;\r
1047 }\r
1048\r
785d84ea 1049 Del = FALSE;\r
1050 if ((InCustomMode() && UserPhysicalPresent()) || (mPlatformMode == SETUP_MODE && !IsPk)) {\r
8c1babfd 1051 Payload = (UINT8 *) Data + AUTHINFO2_SIZE (Data);\r
1052 PayloadSize = DataSize - AUTHINFO2_SIZE (Data);\r
785d84ea 1053 if (PayloadSize == 0) {\r
1054 Del = TRUE;\r
1055 }\r
2d3fb919 1056\r
d912bad7 1057 Status = CheckSignatureListFormat(VariableName, VendorGuid, Payload, PayloadSize);\r
1058 if (EFI_ERROR (Status)) {\r
1059 return Status;\r
1060 }\r
1061\r
2d3fb919 1062 Status = UpdateVariable (\r
1063 VariableName,\r
1064 VendorGuid,\r
1065 Payload,\r
1066 PayloadSize,\r
1067 Attributes,\r
1068 0,\r
8c1babfd 1069 0,\r
2d3fb919 1070 Variable,\r
8c1babfd 1071 &((EFI_VARIABLE_AUTHENTICATION_2 *) Data)->TimeStamp\r
2d3fb919 1072 );\r
a555940b
FS
1073 if (EFI_ERROR(Status)) {\r
1074 return Status;\r
1075 }\r
1076\r
5ecc20b5 1077 if ((mPlatformMode != SETUP_MODE) || IsPk) {\r
a555940b
FS
1078 Status = VendorKeyIsModified ();\r
1079 }\r
785d84ea 1080 } else if (mPlatformMode == USER_MODE) {\r
1081 //\r
1082 // Verify against X509 Cert in PK database.\r
1083 //\r
1084 Status = VerifyTimeBasedPayload (\r
1085 VariableName,\r
1086 VendorGuid,\r
1087 Data,\r
1088 DataSize,\r
1089 Variable,\r
1090 Attributes,\r
1091 AuthVarTypePk,\r
1092 &Del\r
1093 );\r
1094 } else {\r
1095 //\r
1096 // Verify against the certificate in data payload.\r
1097 //\r
1098 Status = VerifyTimeBasedPayload (\r
1099 VariableName,\r
1100 VendorGuid,\r
1101 Data,\r
1102 DataSize,\r
1103 Variable,\r
1104 Attributes,\r
1105 AuthVarTypePayload,\r
1106 &Del\r
1107 );\r
1108 }\r
ecc722ad 1109\r
785d84ea 1110 if (!EFI_ERROR(Status) && IsPk) {\r
1111 if (mPlatformMode == SETUP_MODE && !Del) {\r
1112 //\r
1113 // If enroll PK in setup mode, need change to user mode.\r
1114 //\r
1115 Status = UpdatePlatformMode (USER_MODE);\r
1116 } else if (mPlatformMode == USER_MODE && Del){\r
1117 //\r
1118 // If delete PK in user mode, need change to setup mode.\r
1119 //\r
1120 Status = UpdatePlatformMode (SETUP_MODE);\r
1121 }\r
0c18794e 1122 }\r
1123\r
1124 return Status;\r
1125}\r
1126\r
1127/**\r
1128 Process variable with key exchange key for verification.\r
1129\r
dc204d5a
JY
1130 Caution: This function may receive untrusted input.\r
1131 This function may be invoked in SMM mode, and datasize and data are external input.\r
1132 This function will do basic validation, before parse the data.\r
1133 This function will parse the authentication carefully to avoid security issues, like\r
1134 buffer overflow, integer overflow.\r
1135 This function will check attribute carefully to avoid authentication bypass.\r
1136\r
0c18794e 1137 @param[in] VariableName Name of Variable to be found.\r
1138 @param[in] VendorGuid Variable vendor GUID.\r
1139 @param[in] Data Data pointer.\r
1140 @param[in] DataSize Size of Data found. If size is less than the\r
1141 data, this value contains the required size.\r
1142 @param[in] Variable The variable information which is used to keep track of variable usage.\r
1143 @param[in] Attributes Attribute value of the variable.\r
1144\r
1145 @return EFI_INVALID_PARAMETER Invalid parameter.\r
2d3fb919 1146 @return EFI_SECURITY_VIOLATION The variable does NOT pass the validation\r
1147 check carried out by the firmware.\r
0c18794e 1148 @return EFI_SUCCESS Variable pass validation successfully.\r
1149\r
1150**/\r
1151EFI_STATUS\r
1152ProcessVarWithKek (\r
1153 IN CHAR16 *VariableName,\r
1154 IN EFI_GUID *VendorGuid,\r
1155 IN VOID *Data,\r
1156 IN UINTN DataSize,\r
1157 IN VARIABLE_POINTER_TRACK *Variable,\r
1158 IN UINT32 Attributes OPTIONAL\r
1159 )\r
1160{\r
1161 EFI_STATUS Status;\r
2d3fb919 1162 UINT8 *Payload;\r
1163 UINTN PayloadSize;\r
ecc722ad 1164\r
8c1babfd 1165 if ((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0 ||\r
1166 (Attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) == 0) {\r
ecc722ad 1167 //\r
8c1babfd 1168 // DB and DBX should set EFI_VARIABLE_NON_VOLATILE attribute and should be a time-based\r
1169 // authenticated variable.\r
ecc722ad 1170 //\r
1171 return EFI_INVALID_PARAMETER;\r
1172 }\r
0c18794e 1173\r
ecc722ad 1174 Status = EFI_SUCCESS;\r
1175 if (mPlatformMode == USER_MODE && !(InCustomMode() && UserPhysicalPresent())) {\r
8c1babfd 1176 //\r
1177 // Time-based, verify against X509 Cert KEK.\r
1178 //\r
1179 return VerifyTimeBasedPayload (\r
1180 VariableName,\r
1181 VendorGuid,\r
1182 Data,\r
1183 DataSize,\r
1184 Variable,\r
1185 Attributes,\r
1186 AuthVarTypeKek,\r
1187 NULL\r
1188 );\r
0c18794e 1189 } else {\r
1190 //\r
ecc722ad 1191 // If in setup mode or custom secure boot mode, no authentication needed.\r
0c18794e 1192 //\r
8c1babfd 1193 Payload = (UINT8 *) Data + AUTHINFO2_SIZE (Data);\r
1194 PayloadSize = DataSize - AUTHINFO2_SIZE (Data);\r
2d3fb919 1195\r
e77f9ef6 1196 Status = CheckSignatureListFormat(VariableName, VendorGuid, Payload, PayloadSize);\r
1197 if (EFI_ERROR (Status)) {\r
1198 return Status;\r
1199 }\r
1200 \r
0c18794e 1201 Status = UpdateVariable (\r
2d3fb919 1202 VariableName,\r
1203 VendorGuid,\r
1204 Payload,\r
1205 PayloadSize,\r
1206 Attributes,\r
1207 0,\r
8c1babfd 1208 0,\r
0c18794e 1209 Variable,\r
8c1babfd 1210 &((EFI_VARIABLE_AUTHENTICATION_2 *) Data)->TimeStamp\r
0c18794e 1211 );\r
a555940b
FS
1212 if (EFI_ERROR (Status)) {\r
1213 return Status;\r
1214 }\r
1215\r
1216 if (mPlatformMode != SETUP_MODE) {\r
1217 Status = VendorKeyIsModified ();\r
1218 }\r
0c18794e 1219 }\r
1220\r
1221 return Status;\r
1222}\r
1223\r
1224/**\r
1225 Process variable with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS/EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set\r
1226\r
dc204d5a
JY
1227 Caution: This function may receive untrusted input.\r
1228 This function may be invoked in SMM mode, and datasize and data are external input.\r
1229 This function will do basic validation, before parse the data.\r
1230 This function will parse the authentication carefully to avoid security issues, like\r
1231 buffer overflow, integer overflow.\r
1232 This function will check attribute carefully to avoid authentication bypass.\r
1233\r
0c18794e 1234 @param[in] VariableName Name of Variable to be found.\r
1235 @param[in] VendorGuid Variable vendor GUID.\r
1236\r
1237 @param[in] Data Data pointer.\r
1238 @param[in] DataSize Size of Data found. If size is less than the\r
1239 data, this value contains the required size.\r
1240 @param[in] Variable The variable information which is used to keep track of variable usage.\r
1241 @param[in] Attributes Attribute value of the variable.\r
1242\r
1243 @return EFI_INVALID_PARAMETER Invalid parameter.\r
1244 @return EFI_WRITE_PROTECTED Variable is write-protected and needs authentication with\r
1245 EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS set.\r
275beb2b 1246 @return EFI_OUT_OF_RESOURCES The Database to save the public key is full.\r
0c18794e 1247 @return EFI_SECURITY_VIOLATION The variable is with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS\r
2d3fb919 1248 set, but the AuthInfo does NOT pass the validation\r
1249 check carried out by the firmware.\r
0c18794e 1250 @return EFI_SUCCESS Variable is not write-protected or pass validation successfully.\r
1251\r
1252**/\r
1253EFI_STATUS\r
1254ProcessVariable (\r
1255 IN CHAR16 *VariableName,\r
1256 IN EFI_GUID *VendorGuid,\r
1257 IN VOID *Data,\r
1258 IN UINTN DataSize,\r
1259 IN VARIABLE_POINTER_TRACK *Variable,\r
1260 IN UINT32 Attributes\r
1261 )\r
1262{\r
1263 EFI_STATUS Status;\r
1264 BOOLEAN IsDeletion;\r
1265 BOOLEAN IsFirstTime;\r
1266 UINT8 *PubKey;\r
1267 EFI_VARIABLE_AUTHENTICATION *CertData;\r
1268 EFI_CERT_BLOCK_RSA_2048_SHA256 *CertBlock;\r
1269 UINT32 KeyIndex;\r
1270 UINT64 MonotonicCount;\r
1271\r
2d3fb919 1272 KeyIndex = 0;\r
0c18794e 1273 CertData = NULL;\r
1274 CertBlock = NULL;\r
1275 PubKey = NULL;\r
1276 IsDeletion = FALSE;\r
1277\r
ecc722ad 1278 if (NeedPhysicallyPresent(VariableName, VendorGuid) && !UserPhysicalPresent()) {\r
1279 //\r
1280 // This variable is protected, only physical present user could modify its value.\r
1281 //\r
1282 return EFI_SECURITY_VIOLATION;\r
1283 }\r
1284 \r
89be2b03 1285 //\r
1286 // A time-based authenticated variable and a count-based authenticated variable\r
1287 // can't be updated by each other.\r
1288 // \r
1289 if (Variable->CurrPtr != NULL) { \r
1290 if (((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) != 0) &&\r
1291 ((Variable->CurrPtr->Attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) != 0)) {\r
1292 return EFI_SECURITY_VIOLATION; \r
1293 }\r
1294 \r
1295 if (((Attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) != 0) && \r
1296 ((Variable->CurrPtr->Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) != 0)) {\r
1297 return EFI_SECURITY_VIOLATION; \r
1298 }\r
1299 }\r
1300 \r
0c18794e 1301 //\r
1302 // Process Time-based Authenticated variable.\r
1303 //\r
1304 if ((Attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) != 0) {\r
ed47ae02 1305 return VerifyTimeBasedPayload (\r
1306 VariableName,\r
1307 VendorGuid,\r
1308 Data,\r
1309 DataSize,\r
1310 Variable,\r
1311 Attributes,\r
1312 AuthVarTypePriv,\r
1313 NULL\r
1314 );\r
0c18794e 1315 }\r
2d3fb919 1316\r
0c18794e 1317 //\r
1318 // Determine if first time SetVariable with the EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS.\r
1319 //\r
1320 if ((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) != 0) {\r
1321 //\r
1322 // Determine current operation type.\r
1323 //\r
1324 if (DataSize == AUTHINFO_SIZE) {\r
1325 IsDeletion = TRUE;\r
1326 }\r
1327 //\r
1328 // Determine whether this is the first time with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS set.\r
1329 //\r
1330 if (Variable->CurrPtr == NULL) {\r
1331 IsFirstTime = TRUE;\r
1332 } else if ((Variable->CurrPtr->Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) == 0) {\r
1333 IsFirstTime = TRUE;\r
1334 } else {\r
1335 KeyIndex = Variable->CurrPtr->PubKeyIndex;\r
1336 IsFirstTime = FALSE;\r
1337 }\r
389c8779 1338 } else if ((Variable->CurrPtr != NULL) && \r
1339 ((Variable->CurrPtr->Attributes & (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)) != 0)\r
1340 ) {\r
0c18794e 1341 //\r
1342 // If the variable is already write-protected, it always needs authentication before update.\r
1343 //\r
1344 return EFI_WRITE_PROTECTED;\r
1345 } else {\r
1346 //\r
1347 // If without EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS, set and attributes collision.\r
1348 // That means it is not authenticated variable, just update variable as usual.\r
1349 //\r
1350 Status = UpdateVariable (VariableName, VendorGuid, Data, DataSize, Attributes, 0, 0, Variable, NULL);\r
1351 return Status;\r
1352 }\r
1353\r
1354 //\r
1355 // Get PubKey and check Monotonic Count value corresponding to the variable.\r
1356 //\r
1357 CertData = (EFI_VARIABLE_AUTHENTICATION *) Data;\r
1358 CertBlock = (EFI_CERT_BLOCK_RSA_2048_SHA256 *) (CertData->AuthInfo.CertData);\r
1359 PubKey = CertBlock->PublicKey;\r
1360\r
1361 //\r
1362 // Update Monotonic Count value.\r
1363 //\r
1364 MonotonicCount = CertData->MonotonicCount;\r
1365\r
1366 if (!IsFirstTime) {\r
1367 //\r
1368 // Check input PubKey.\r
1369 //\r
1370 if (CompareMem (PubKey, mPubKeyStore + (KeyIndex - 1) * EFI_CERT_TYPE_RSA2048_SIZE, EFI_CERT_TYPE_RSA2048_SIZE) != 0) {\r
1371 return EFI_SECURITY_VIOLATION;\r
1372 }\r
1373 //\r
1374 // Compare the current monotonic count and ensure that it is greater than the last SetVariable\r
1375 // operation with the EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS attribute set.\r
1376 //\r
1377 if (CertData->MonotonicCount <= Variable->CurrPtr->MonotonicCount) {\r
1378 //\r
1379 // Monotonic count check fail, suspicious replay attack, return EFI_SECURITY_VIOLATION.\r
1380 //\r
1381 return EFI_SECURITY_VIOLATION;\r
1382 }\r
2d3fb919 1383 }\r
0c18794e 1384 //\r
1385 // Verify the certificate in Data payload.\r
1386 //\r
1387 Status = VerifyCounterBasedPayload (Data, DataSize, PubKey);\r
1388 if (EFI_ERROR (Status)) {\r
1389 return Status;\r
1390 }\r
2d3fb919 1391\r
0c18794e 1392 //\r
1393 // Now, the signature has been verified!\r
1394 //\r
1395 if (IsFirstTime && !IsDeletion) {\r
1396 //\r
1397 // Update public key database variable if need.\r
1398 //\r
1399 KeyIndex = AddPubKeyInStore (PubKey);\r
4e33001c 1400 if (KeyIndex == 0) {\r
275beb2b 1401 return EFI_OUT_OF_RESOURCES;\r
4e33001c 1402 }\r
0c18794e 1403 }\r
1404\r
1405 //\r
1406 // Verification pass.\r
1407 //\r
1408 return UpdateVariable (VariableName, VendorGuid, (UINT8*)Data + AUTHINFO_SIZE, DataSize - AUTHINFO_SIZE, Attributes, KeyIndex, MonotonicCount, Variable, NULL);\r
1409}\r
1410\r
2d3fb919 1411/**\r
1412 Merge two buffers which formatted as EFI_SIGNATURE_LIST. Only the new EFI_SIGNATURE_DATA\r
1413 will be appended to the original EFI_SIGNATURE_LIST, duplicate EFI_SIGNATURE_DATA\r
1414 will be ignored.\r
1415\r
732d199d 1416 @param[in, out] Data Pointer to original EFI_SIGNATURE_LIST.\r
1417 @param[in] DataSize Size of Data buffer.\r
1418 @param[in] FreeBufSize Size of free data buffer \r
1419 @param[in] NewData Pointer to new EFI_SIGNATURE_LIST to be appended.\r
1420 @param[in] NewDataSize Size of NewData buffer.\r
1421 @param[out] MergedBufSize Size of the merged buffer\r
2d3fb919 1422\r
732d199d 1423 @return EFI_BUFFER_TOO_SMALL if input Data buffer overflowed\r
2d3fb919 1424\r
1425**/\r
732d199d 1426EFI_STATUS\r
2d3fb919 1427AppendSignatureList (\r
1428 IN OUT VOID *Data,\r
1429 IN UINTN DataSize,\r
732d199d 1430 IN UINTN FreeBufSize,\r
2d3fb919 1431 IN VOID *NewData,\r
732d199d 1432 IN UINTN NewDataSize,\r
1433 OUT UINTN *MergedBufSize\r
2d3fb919 1434 )\r
1435{\r
1436 EFI_SIGNATURE_LIST *CertList;\r
1437 EFI_SIGNATURE_DATA *Cert;\r
1438 UINTN CertCount;\r
1439 EFI_SIGNATURE_LIST *NewCertList;\r
1440 EFI_SIGNATURE_DATA *NewCert;\r
1441 UINTN NewCertCount;\r
1442 UINTN Index;\r
1443 UINTN Index2;\r
1444 UINTN Size;\r
1445 UINT8 *Tail;\r
1446 UINTN CopiedCount;\r
1447 UINTN SignatureListSize;\r
1448 BOOLEAN IsNewCert;\r
1449\r
1450 Tail = (UINT8 *) Data + DataSize;\r
1451\r
1452 NewCertList = (EFI_SIGNATURE_LIST *) NewData;\r
1453 while ((NewDataSize > 0) && (NewDataSize >= NewCertList->SignatureListSize)) {\r
1454 NewCert = (EFI_SIGNATURE_DATA *) ((UINT8 *) NewCertList + sizeof (EFI_SIGNATURE_LIST) + NewCertList->SignatureHeaderSize);\r
1455 NewCertCount = (NewCertList->SignatureListSize - sizeof (EFI_SIGNATURE_LIST) - NewCertList->SignatureHeaderSize) / NewCertList->SignatureSize;\r
1456\r
1457 CopiedCount = 0;\r
1458 for (Index = 0; Index < NewCertCount; Index++) {\r
1459 IsNewCert = TRUE;\r
1460\r
1461 Size = DataSize;\r
1462 CertList = (EFI_SIGNATURE_LIST *) Data;\r
1463 while ((Size > 0) && (Size >= CertList->SignatureListSize)) {\r
1464 if (CompareGuid (&CertList->SignatureType, &NewCertList->SignatureType) &&\r
1465 (CertList->SignatureSize == NewCertList->SignatureSize)) {\r
1466 Cert = (EFI_SIGNATURE_DATA *) ((UINT8 *) CertList + sizeof (EFI_SIGNATURE_LIST) + CertList->SignatureHeaderSize);\r
1467 CertCount = (CertList->SignatureListSize - sizeof (EFI_SIGNATURE_LIST) - CertList->SignatureHeaderSize) / CertList->SignatureSize;\r
1468 for (Index2 = 0; Index2 < CertCount; Index2++) {\r
1469 //\r
1470 // Iterate each Signature Data in this Signature List.\r
1471 //\r
1472 if (CompareMem (NewCert, Cert, CertList->SignatureSize) == 0) {\r
1473 IsNewCert = FALSE;\r
1474 break;\r
1475 }\r
1476 Cert = (EFI_SIGNATURE_DATA *) ((UINT8 *) Cert + CertList->SignatureSize);\r
1477 }\r
1478 }\r
1479\r
1480 if (!IsNewCert) {\r
1481 break;\r
1482 }\r
1483 Size -= CertList->SignatureListSize;\r
1484 CertList = (EFI_SIGNATURE_LIST *) ((UINT8 *) CertList + CertList->SignatureListSize);\r
1485 }\r
1486\r
1487 if (IsNewCert) {\r
1488 //\r
1489 // New EFI_SIGNATURE_DATA, append it.\r
1490 //\r
1491 if (CopiedCount == 0) {\r
732d199d 1492 if (FreeBufSize < sizeof (EFI_SIGNATURE_LIST) + NewCertList->SignatureHeaderSize) {\r
1493 return EFI_BUFFER_TOO_SMALL;\r
1494 }\r
1495\r
2d3fb919 1496 //\r
1497 // Copy EFI_SIGNATURE_LIST header for only once.\r
1498 //\r
732d199d 1499\r
2d3fb919 1500 CopyMem (Tail, NewCertList, sizeof (EFI_SIGNATURE_LIST) + NewCertList->SignatureHeaderSize);\r
1501 Tail = Tail + sizeof (EFI_SIGNATURE_LIST) + NewCertList->SignatureHeaderSize;\r
732d199d 1502 FreeBufSize -= sizeof (EFI_SIGNATURE_LIST) + NewCertList->SignatureHeaderSize;\r
2d3fb919 1503 }\r
1504\r
732d199d 1505 if (FreeBufSize < NewCertList->SignatureSize) {\r
1506 return EFI_BUFFER_TOO_SMALL;\r
1507 }\r
2d3fb919 1508 CopyMem (Tail, NewCert, NewCertList->SignatureSize);\r
1509 Tail += NewCertList->SignatureSize;\r
732d199d 1510 FreeBufSize -= NewCertList->SignatureSize;\r
2d3fb919 1511 CopiedCount++;\r
1512 }\r
1513\r
1514 NewCert = (EFI_SIGNATURE_DATA *) ((UINT8 *) NewCert + NewCertList->SignatureSize);\r
1515 }\r
1516\r
1517 //\r
1518 // Update SignatureListSize in newly appended EFI_SIGNATURE_LIST.\r
1519 //\r
1520 if (CopiedCount != 0) {\r
1521 SignatureListSize = sizeof (EFI_SIGNATURE_LIST) + NewCertList->SignatureHeaderSize + (CopiedCount * NewCertList->SignatureSize);\r
1522 CertList = (EFI_SIGNATURE_LIST *) (Tail - SignatureListSize);\r
1523 CertList->SignatureListSize = (UINT32) SignatureListSize;\r
1524 }\r
1525\r
1526 NewDataSize -= NewCertList->SignatureListSize;\r
1527 NewCertList = (EFI_SIGNATURE_LIST *) ((UINT8 *) NewCertList + NewCertList->SignatureListSize);\r
1528 }\r
1529\r
732d199d 1530 *MergedBufSize = (Tail - (UINT8 *) Data);\r
1531 return EFI_SUCCESS;\r
2d3fb919 1532}\r
1533\r
0c18794e 1534/**\r
1535 Compare two EFI_TIME data.\r
1536\r
1537\r
1538 @param FirstTime A pointer to the first EFI_TIME data.\r
1539 @param SecondTime A pointer to the second EFI_TIME data.\r
1540\r
1541 @retval TRUE The FirstTime is not later than the SecondTime.\r
1542 @retval FALSE The FirstTime is later than the SecondTime.\r
1543\r
1544**/\r
1545BOOLEAN\r
1546CompareTimeStamp (\r
1547 IN EFI_TIME *FirstTime,\r
1548 IN EFI_TIME *SecondTime\r
1549 )\r
1550{\r
1551 if (FirstTime->Year != SecondTime->Year) {\r
1552 return (BOOLEAN) (FirstTime->Year < SecondTime->Year);\r
1553 } else if (FirstTime->Month != SecondTime->Month) {\r
1554 return (BOOLEAN) (FirstTime->Month < SecondTime->Month);\r
1555 } else if (FirstTime->Day != SecondTime->Day) {\r
1556 return (BOOLEAN) (FirstTime->Day < SecondTime->Day);\r
1557 } else if (FirstTime->Hour != SecondTime->Hour) {\r
1558 return (BOOLEAN) (FirstTime->Hour < SecondTime->Hour);\r
1559 } else if (FirstTime->Minute != SecondTime->Minute) {\r
42ed7604 1560 return (BOOLEAN) (FirstTime->Minute < SecondTime->Minute);\r
2d3fb919 1561 }\r
0c18794e 1562\r
1563 return (BOOLEAN) (FirstTime->Second <= SecondTime->Second);\r
1564}\r
1565\r
ed47ae02 1566/**\r
1567 Find matching signer's certificates for common authenticated variable\r
1568 by corresponding VariableName and VendorGuid from "certdb".\r
1569\r
1570 The data format of "certdb":\r
1571 //\r
1572 // UINT32 CertDbListSize;\r
1573 // /// AUTH_CERT_DB_DATA Certs1[];\r
1574 // /// AUTH_CERT_DB_DATA Certs2[];\r
1575 // /// ...\r
1576 // /// AUTH_CERT_DB_DATA Certsn[];\r
1577 //\r
1578\r
1579 @param[in] VariableName Name of authenticated Variable.\r
1580 @param[in] VendorGuid Vendor GUID of authenticated Variable.\r
1581 @param[in] Data Pointer to variable "certdb".\r
1582 @param[in] DataSize Size of variable "certdb".\r
1583 @param[out] CertOffset Offset of matching CertData, from starting of Data.\r
1584 @param[out] CertDataSize Length of CertData in bytes.\r
1585 @param[out] CertNodeOffset Offset of matching AUTH_CERT_DB_DATA , from\r
1586 starting of Data.\r
1587 @param[out] CertNodeSize Length of AUTH_CERT_DB_DATA in bytes.\r
1588\r
1589 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.\r
1590 @retval EFI_NOT_FOUND Fail to find matching certs.\r
1591 @retval EFI_SUCCESS Find matching certs and output parameters.\r
1592\r
1593**/\r
1594EFI_STATUS\r
1595FindCertsFromDb (\r
1596 IN CHAR16 *VariableName,\r
1597 IN EFI_GUID *VendorGuid,\r
1598 IN UINT8 *Data,\r
1599 IN UINTN DataSize,\r
1600 OUT UINT32 *CertOffset, OPTIONAL\r
1601 OUT UINT32 *CertDataSize, OPTIONAL\r
1602 OUT UINT32 *CertNodeOffset,OPTIONAL\r
1603 OUT UINT32 *CertNodeSize OPTIONAL\r
1604 )\r
1605{\r
1606 UINT32 Offset;\r
1607 AUTH_CERT_DB_DATA *Ptr;\r
1608 UINT32 CertSize;\r
1609 UINT32 NameSize;\r
1610 UINT32 NodeSize;\r
1611 UINT32 CertDbListSize;\r
1612\r
1613 if ((VariableName == NULL) || (VendorGuid == NULL) || (Data == NULL)) {\r
1614 return EFI_INVALID_PARAMETER;\r
1615 }\r
1616\r
1617 //\r
1618 // Check whether DataSize matches recorded CertDbListSize.\r
1619 //\r
1620 if (DataSize < sizeof (UINT32)) {\r
1621 return EFI_INVALID_PARAMETER;\r
1622 }\r
1623\r
1624 CertDbListSize = ReadUnaligned32 ((UINT32 *) Data);\r
1625\r
1626 if (CertDbListSize != (UINT32) DataSize) {\r
1627 return EFI_INVALID_PARAMETER;\r
1628 }\r
1629\r
1630 Offset = sizeof (UINT32);\r
1631\r
1632 //\r
1633 // Get corresponding certificates by VendorGuid and VariableName.\r
1634 //\r
1635 while (Offset < (UINT32) DataSize) {\r
1636 Ptr = (AUTH_CERT_DB_DATA *) (Data + Offset);\r
1637 //\r
1638 // Check whether VendorGuid matches.\r
1639 //\r
1640 if (CompareGuid (&Ptr->VendorGuid, VendorGuid)) {\r
1641 NodeSize = ReadUnaligned32 (&Ptr->CertNodeSize);\r
1642 NameSize = ReadUnaligned32 (&Ptr->NameSize);\r
1643 CertSize = ReadUnaligned32 (&Ptr->CertDataSize);\r
1644\r
1645 if (NodeSize != sizeof (EFI_GUID) + sizeof (UINT32) * 3 + CertSize +\r
1646 sizeof (CHAR16) * NameSize) {\r
1647 return EFI_INVALID_PARAMETER;\r
1648 }\r
1649\r
1650 Offset = Offset + sizeof (EFI_GUID) + sizeof (UINT32) * 3;\r
1651 //\r
1652 // Check whether VariableName matches.\r
1653 //\r
1654 if ((NameSize == StrLen (VariableName)) && \r
1655 (CompareMem (Data + Offset, VariableName, NameSize * sizeof (CHAR16)) == 0)) {\r
1656 Offset = Offset + NameSize * sizeof (CHAR16);\r
1657\r
1658 if (CertOffset != NULL) {\r
1659 *CertOffset = Offset;\r
1660 }\r
1661\r
1662 if (CertDataSize != NULL) {\r
1663 *CertDataSize = CertSize; \r
1664 }\r
1665\r
1666 if (CertNodeOffset != NULL) {\r
1667 *CertNodeOffset = (UINT32) ((UINT8 *) Ptr - Data);\r
1668 }\r
1669\r
1670 if (CertNodeSize != NULL) {\r
1671 *CertNodeSize = NodeSize;\r
1672 }\r
1673\r
1674 return EFI_SUCCESS;\r
1675 } else {\r
1676 Offset = Offset + NameSize * sizeof (CHAR16) + CertSize;\r
1677 }\r
1678 } else {\r
1679 NodeSize = ReadUnaligned32 (&Ptr->CertNodeSize);\r
1680 Offset = Offset + NodeSize;\r
1681 }\r
1682 }\r
1683\r
1684 return EFI_NOT_FOUND; \r
1685}\r
1686\r
1687/**\r
1688 Retrieve signer's certificates for common authenticated variable\r
1689 by corresponding VariableName and VendorGuid from "certdb".\r
1690\r
1691 @param[in] VariableName Name of authenticated Variable.\r
1692 @param[in] VendorGuid Vendor GUID of authenticated Variable.\r
1693 @param[out] CertData Pointer to signer's certificates.\r
1694 @param[out] CertDataSize Length of CertData in bytes.\r
1695\r
1696 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.\r
1697 @retval EFI_NOT_FOUND Fail to find "certdb" or matching certs.\r
1698 @retval EFI_SUCCESS Get signer's certificates successfully.\r
1699\r
1700**/\r
1701EFI_STATUS\r
1702GetCertsFromDb (\r
1703 IN CHAR16 *VariableName,\r
1704 IN EFI_GUID *VendorGuid,\r
1705 OUT UINT8 **CertData,\r
1706 OUT UINT32 *CertDataSize\r
1707 )\r
1708{\r
1709 VARIABLE_POINTER_TRACK CertDbVariable;\r
1710 EFI_STATUS Status;\r
1711 UINT8 *Data;\r
1712 UINTN DataSize;\r
1713 UINT32 CertOffset;\r
1714\r
1715 if ((VariableName == NULL) || (VendorGuid == NULL) || (CertData == NULL) || (CertDataSize == NULL)) {\r
1716 return EFI_INVALID_PARAMETER;\r
1717 }\r
1718 \r
1719 //\r
1720 // Get variable "certdb".\r
1721 //\r
1722 Status = FindVariable (\r
1723 EFI_CERT_DB_NAME,\r
1724 &gEfiCertDbGuid,\r
1725 &CertDbVariable,\r
1726 &mVariableModuleGlobal->VariableGlobal,\r
1727 FALSE\r
1728 ); \r
1729 if (EFI_ERROR (Status)) {\r
1730 return Status;\r
1731 }\r
1732\r
1733 DataSize = DataSizeOfVariable (CertDbVariable.CurrPtr);\r
1734 Data = GetVariableDataPtr (CertDbVariable.CurrPtr);\r
1735 if ((DataSize == 0) || (Data == NULL)) {\r
1736 ASSERT (FALSE);\r
1737 return EFI_NOT_FOUND;\r
1738 }\r
1739\r
1740 Status = FindCertsFromDb (\r
1741 VariableName,\r
1742 VendorGuid,\r
1743 Data,\r
1744 DataSize,\r
1745 &CertOffset,\r
1746 CertDataSize,\r
1747 NULL,\r
1748 NULL\r
1749 );\r
1750\r
1751 if (EFI_ERROR (Status)) {\r
1752 return Status;\r
1753 }\r
1754\r
1755 *CertData = Data + CertOffset;\r
1756 return EFI_SUCCESS;\r
1757}\r
1758\r
1759/**\r
1760 Delete matching signer's certificates when deleting common authenticated\r
1761 variable by corresponding VariableName and VendorGuid from "certdb".\r
1762\r
1763 @param[in] VariableName Name of authenticated Variable.\r
1764 @param[in] VendorGuid Vendor GUID of authenticated Variable.\r
1765\r
1766 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.\r
1767 @retval EFI_NOT_FOUND Fail to find "certdb" or matching certs.\r
1768 @retval EFI_OUT_OF_RESOURCES The operation is failed due to lack of resources.\r
1769 @retval EFI_SUCCESS The operation is completed successfully.\r
1770\r
1771**/\r
1772EFI_STATUS\r
1773DeleteCertsFromDb (\r
1774 IN CHAR16 *VariableName,\r
1775 IN EFI_GUID *VendorGuid\r
1776 )\r
1777{\r
1778 VARIABLE_POINTER_TRACK CertDbVariable;\r
1779 EFI_STATUS Status;\r
1780 UINT8 *Data;\r
1781 UINTN DataSize;\r
1782 UINT32 VarAttr;\r
1783 UINT32 CertNodeOffset;\r
1784 UINT32 CertNodeSize;\r
1785 UINT8 *NewCertDb;\r
1786 UINT32 NewCertDbSize;\r
1787\r
1788 if ((VariableName == NULL) || (VendorGuid == NULL)) {\r
1789 return EFI_INVALID_PARAMETER;\r
1790 }\r
1791 \r
1792 //\r
1793 // Get variable "certdb".\r
1794 //\r
1795 Status = FindVariable (\r
1796 EFI_CERT_DB_NAME,\r
1797 &gEfiCertDbGuid,\r
1798 &CertDbVariable,\r
1799 &mVariableModuleGlobal->VariableGlobal,\r
1800 FALSE\r
1801 ); \r
1802 if (EFI_ERROR (Status)) {\r
1803 return Status;\r
1804 }\r
1805\r
1806 DataSize = DataSizeOfVariable (CertDbVariable.CurrPtr);\r
1807 Data = GetVariableDataPtr (CertDbVariable.CurrPtr);\r
1808 if ((DataSize == 0) || (Data == NULL)) {\r
1809 ASSERT (FALSE);\r
1810 return EFI_NOT_FOUND;\r
1811 }\r
1812\r
1813 if (DataSize == sizeof (UINT32)) {\r
1814 //\r
1815 // There is no certs in certdb.\r
1816 //\r
1817 return EFI_SUCCESS;\r
1818 }\r
1819\r
1820 //\r
1821 // Get corresponding cert node from certdb.\r
1822 //\r
1823 Status = FindCertsFromDb (\r
1824 VariableName,\r
1825 VendorGuid,\r
1826 Data,\r
1827 DataSize,\r
1828 NULL,\r
1829 NULL,\r
1830 &CertNodeOffset,\r
1831 &CertNodeSize\r
1832 );\r
1833\r
1834 if (EFI_ERROR (Status)) {\r
1835 return Status;\r
1836 }\r
1837\r
1838 if (DataSize < (CertNodeOffset + CertNodeSize)) {\r
1839 return EFI_NOT_FOUND;\r
1840 }\r
1841\r
1842 //\r
1843 // Construct new data content of variable "certdb".\r
1844 //\r
1845 NewCertDbSize = (UINT32) DataSize - CertNodeSize;\r
f6e23353 1846 NewCertDb = (UINT8*) mCertDbStore;\r
ed47ae02 1847\r
1848 //\r
1849 // Copy the DB entries before deleting node.\r
1850 //\r
1851 CopyMem (NewCertDb, Data, CertNodeOffset);\r
1852 //\r
1853 // Update CertDbListSize.\r
1854 //\r
1855 CopyMem (NewCertDb, &NewCertDbSize, sizeof (UINT32));\r
1856 //\r
1857 // Copy the DB entries after deleting node.\r
1858 //\r
1859 if (DataSize > (CertNodeOffset + CertNodeSize)) {\r
1860 CopyMem (\r
1861 NewCertDb + CertNodeOffset,\r
1862 Data + CertNodeOffset + CertNodeSize,\r
1863 DataSize - CertNodeOffset - CertNodeSize\r
1864 );\r
1865 }\r
1866\r
1867 //\r
1868 // Set "certdb".\r
1869 // \r
1870 VarAttr = EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS; \r
1871 Status = UpdateVariable (\r
1872 EFI_CERT_DB_NAME,\r
1873 &gEfiCertDbGuid,\r
1874 NewCertDb,\r
1875 NewCertDbSize,\r
1876 VarAttr,\r
1877 0,\r
1878 0,\r
1879 &CertDbVariable,\r
1880 NULL\r
1881 );\r
1882\r
ed47ae02 1883 return Status;\r
1884}\r
1885\r
1886/**\r
1887 Insert signer's certificates for common authenticated variable with VariableName\r
1888 and VendorGuid in AUTH_CERT_DB_DATA to "certdb".\r
1889\r
1890 @param[in] VariableName Name of authenticated Variable.\r
1891 @param[in] VendorGuid Vendor GUID of authenticated Variable.\r
1892 @param[in] CertData Pointer to signer's certificates.\r
1893 @param[in] CertDataSize Length of CertData in bytes.\r
1894\r
1895 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.\r
1896 @retval EFI_ACCESS_DENIED An AUTH_CERT_DB_DATA entry with same VariableName\r
1897 and VendorGuid already exists.\r
1898 @retval EFI_OUT_OF_RESOURCES The operation is failed due to lack of resources.\r
1899 @retval EFI_SUCCESS Insert an AUTH_CERT_DB_DATA entry to "certdb"\r
1900\r
1901**/\r
1902EFI_STATUS\r
1903InsertCertsToDb (\r
1904 IN CHAR16 *VariableName,\r
1905 IN EFI_GUID *VendorGuid,\r
1906 IN UINT8 *CertData,\r
1907 IN UINTN CertDataSize\r
1908 )\r
1909{\r
1910 VARIABLE_POINTER_TRACK CertDbVariable;\r
1911 EFI_STATUS Status;\r
1912 UINT8 *Data;\r
1913 UINTN DataSize;\r
1914 UINT32 VarAttr;\r
1915 UINT8 *NewCertDb;\r
1916 UINT32 NewCertDbSize;\r
1917 UINT32 CertNodeSize;\r
1918 UINT32 NameSize;\r
1919 AUTH_CERT_DB_DATA *Ptr;\r
1920\r
1921 if ((VariableName == NULL) || (VendorGuid == NULL) || (CertData == NULL)) {\r
1922 return EFI_INVALID_PARAMETER;\r
1923 }\r
1924 \r
1925 //\r
1926 // Get variable "certdb".\r
1927 //\r
1928 Status = FindVariable (\r
1929 EFI_CERT_DB_NAME,\r
1930 &gEfiCertDbGuid,\r
1931 &CertDbVariable,\r
1932 &mVariableModuleGlobal->VariableGlobal,\r
1933 FALSE\r
1934 ); \r
1935 if (EFI_ERROR (Status)) {\r
1936 return Status;\r
1937 }\r
1938\r
1939 DataSize = DataSizeOfVariable (CertDbVariable.CurrPtr);\r
1940 Data = GetVariableDataPtr (CertDbVariable.CurrPtr);\r
1941 if ((DataSize == 0) || (Data == NULL)) {\r
1942 ASSERT (FALSE);\r
1943 return EFI_NOT_FOUND;\r
1944 }\r
1945\r
1946 //\r
1947 // Find whether matching cert node already exists in "certdb".\r
1948 // If yes return error.\r
1949 //\r
1950 Status = FindCertsFromDb (\r
1951 VariableName,\r
1952 VendorGuid,\r
1953 Data,\r
1954 DataSize,\r
1955 NULL,\r
1956 NULL,\r
1957 NULL,\r
1958 NULL\r
1959 );\r
1960\r
1961 if (!EFI_ERROR (Status)) {\r
1962 ASSERT (FALSE);\r
1963 return EFI_ACCESS_DENIED;\r
1964 }\r
1965\r
1966 //\r
1967 // Construct new data content of variable "certdb".\r
1968 //\r
1969 NameSize = (UINT32) StrLen (VariableName);\r
1970 CertNodeSize = sizeof (AUTH_CERT_DB_DATA) + (UINT32) CertDataSize + NameSize * sizeof (CHAR16); \r
f6e23353 1971 NewCertDbSize = (UINT32) DataSize + CertNodeSize;\r
1972 if (NewCertDbSize > MAX_CERTDB_SIZE) {\r
ed47ae02 1973 return EFI_OUT_OF_RESOURCES;\r
1974 }\r
f6e23353 1975 NewCertDb = (UINT8*) mCertDbStore;\r
ed47ae02 1976\r
1977 //\r
1978 // Copy the DB entries before deleting node.\r
1979 //\r
1980 CopyMem (NewCertDb, Data, DataSize);\r
1981 //\r
1982 // Update CertDbListSize.\r
1983 //\r
1984 CopyMem (NewCertDb, &NewCertDbSize, sizeof (UINT32));\r
1985 //\r
1986 // Construct new cert node.\r
1987 //\r
1988 Ptr = (AUTH_CERT_DB_DATA *) (NewCertDb + DataSize);\r
1989 CopyGuid (&Ptr->VendorGuid, VendorGuid);\r
1990 CopyMem (&Ptr->CertNodeSize, &CertNodeSize, sizeof (UINT32));\r
1991 CopyMem (&Ptr->NameSize, &NameSize, sizeof (UINT32));\r
1992 CopyMem (&Ptr->CertDataSize, &CertDataSize, sizeof (UINT32));\r
1993 \r
1994 CopyMem (\r
1995 (UINT8 *) Ptr + sizeof (AUTH_CERT_DB_DATA),\r
1996 VariableName,\r
1997 NameSize * sizeof (CHAR16)\r
1998 );\r
1999\r
2000 CopyMem (\r
2001 (UINT8 *) Ptr + sizeof (AUTH_CERT_DB_DATA) + NameSize * sizeof (CHAR16),\r
2002 CertData,\r
2003 CertDataSize\r
2004 );\r
2005 \r
2006 //\r
2007 // Set "certdb".\r
2008 // \r
2009 VarAttr = EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS; \r
2010 Status = UpdateVariable (\r
2011 EFI_CERT_DB_NAME,\r
2012 &gEfiCertDbGuid,\r
2013 NewCertDb,\r
2014 NewCertDbSize,\r
2015 VarAttr,\r
2016 0,\r
2017 0,\r
2018 &CertDbVariable,\r
2019 NULL\r
2020 );\r
2021\r
ed47ae02 2022 return Status;\r
2023}\r
2024\r
0c18794e 2025/**\r
2026 Process variable with EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set\r
2027\r
dc204d5a
JY
2028 Caution: This function may receive untrusted input.\r
2029 This function may be invoked in SMM mode, and datasize and data are external input.\r
2030 This function will do basic validation, before parse the data.\r
2031 This function will parse the authentication carefully to avoid security issues, like\r
2032 buffer overflow, integer overflow.\r
2033\r
0c18794e 2034 @param[in] VariableName Name of Variable to be found.\r
2035 @param[in] VendorGuid Variable vendor GUID.\r
2036 @param[in] Data Data pointer.\r
2037 @param[in] DataSize Size of Data found. If size is less than the\r
2038 data, this value contains the required size.\r
2039 @param[in] Variable The variable information which is used to keep track of variable usage.\r
2040 @param[in] Attributes Attribute value of the variable.\r
785d84ea 2041 @param[in] AuthVarType Verify against PK, KEK database, private database or certificate in data payload.\r
0c18794e 2042 @param[out] VarDel Delete the variable or not.\r
2043\r
2044 @retval EFI_INVALID_PARAMETER Invalid parameter.\r
2d3fb919 2045 @retval EFI_SECURITY_VIOLATION The variable does NOT pass the validation\r
2046 check carried out by the firmware.\r
0c18794e 2047 @retval EFI_OUT_OF_RESOURCES Failed to process variable due to lack\r
2048 of resources.\r
2049 @retval EFI_SUCCESS Variable pass validation successfully.\r
2050\r
2051**/\r
2052EFI_STATUS\r
2053VerifyTimeBasedPayload (\r
2054 IN CHAR16 *VariableName,\r
2055 IN EFI_GUID *VendorGuid,\r
2056 IN VOID *Data,\r
2057 IN UINTN DataSize,\r
2058 IN VARIABLE_POINTER_TRACK *Variable,\r
2059 IN UINT32 Attributes,\r
ed47ae02 2060 IN AUTHVAR_TYPE AuthVarType,\r
0c18794e 2061 OUT BOOLEAN *VarDel\r
2062 )\r
2063{\r
2064 UINT8 *RootCert;\r
2065 UINT8 *SigData;\r
2d3fb919 2066 UINT8 *PayloadPtr;\r
0c18794e 2067 UINTN RootCertSize;\r
2068 UINTN Index;\r
2d3fb919 2069 UINTN CertCount;\r
2070 UINTN PayloadSize;\r
0c18794e 2071 UINT32 Attr;\r
2072 UINT32 SigDataSize;\r
2073 UINT32 KekDataSize;\r
0c18794e 2074 BOOLEAN VerifyStatus;\r
2075 EFI_STATUS Status;\r
2076 EFI_SIGNATURE_LIST *CertList;\r
2077 EFI_SIGNATURE_DATA *Cert;\r
2078 VARIABLE_POINTER_TRACK KekVariable;\r
2079 EFI_VARIABLE_AUTHENTICATION_2 *CertData;\r
2080 UINT8 *NewData;\r
2081 UINTN NewDataSize;\r
2082 VARIABLE_POINTER_TRACK PkVariable;\r
2d3fb919 2083 UINT8 *Buffer;\r
2084 UINTN Length;\r
ed47ae02 2085 UINT8 *SignerCerts;\r
2086 UINT8 *WrapSigData;\r
2087 UINTN CertStackSize;\r
2088 UINT8 *CertsInCertDb;\r
2089 UINT32 CertsSizeinDb;\r
0c18794e 2090\r
0c18794e 2091 VerifyStatus = FALSE;\r
2092 CertData = NULL;\r
2093 NewData = NULL;\r
2094 Attr = Attributes;\r
ed47ae02 2095 WrapSigData = NULL;\r
2096 SignerCerts = NULL;\r
2097 RootCert = NULL;\r
0c18794e 2098\r
2099 //\r
2d3fb919 2100 // When the attribute EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS is\r
0c18794e 2101 // set, then the Data buffer shall begin with an instance of a complete (and serialized)\r
2d3fb919 2102 // EFI_VARIABLE_AUTHENTICATION_2 descriptor. The descriptor shall be followed by the new\r
2103 // variable value and DataSize shall reflect the combined size of the descriptor and the new\r
2104 // variable value. The authentication descriptor is not part of the variable data and is not\r
0c18794e 2105 // returned by subsequent calls to GetVariable().\r
2106 //\r
2107 CertData = (EFI_VARIABLE_AUTHENTICATION_2 *) Data;\r
2d3fb919 2108\r
2109 //\r
2110 // Verify that Pad1, Nanosecond, TimeZone, Daylight and Pad2 components of the\r
2111 // TimeStamp value are set to zero.\r
2112 //\r
2113 if ((CertData->TimeStamp.Pad1 != 0) ||\r
2114 (CertData->TimeStamp.Nanosecond != 0) ||\r
2115 (CertData->TimeStamp.TimeZone != 0) ||\r
2116 (CertData->TimeStamp.Daylight != 0) ||\r
2117 (CertData->TimeStamp.Pad2 != 0)) {\r
2e24814a 2118 return EFI_SECURITY_VIOLATION;\r
2d3fb919 2119 }\r
2120\r
0c18794e 2121 if ((Variable->CurrPtr != NULL) && ((Attributes & EFI_VARIABLE_APPEND_WRITE) == 0)) {\r
2122 if (CompareTimeStamp (&CertData->TimeStamp, &Variable->CurrPtr->TimeStamp)) {\r
2123 //\r
2124 // TimeStamp check fail, suspicious replay attack, return EFI_SECURITY_VIOLATION.\r
2125 //\r
2126 return EFI_SECURITY_VIOLATION;\r
2127 }\r
2128 }\r
2129\r
2130 //\r
2131 // wCertificateType should be WIN_CERT_TYPE_EFI_GUID.\r
2132 // Cert type should be EFI_CERT_TYPE_PKCS7_GUID.\r
2133 //\r
2134 if ((CertData->AuthInfo.Hdr.wCertificateType != WIN_CERT_TYPE_EFI_GUID) ||\r
2d3fb919 2135 !CompareGuid (&CertData->AuthInfo.CertType, &gEfiCertPkcs7Guid)) {\r
0c18794e 2136 //\r
2137 // Invalid AuthInfo type, return EFI_SECURITY_VIOLATION.\r
2138 //\r
2139 return EFI_SECURITY_VIOLATION;\r
2140 }\r
2d3fb919 2141\r
0c18794e 2142 //\r
2143 // Find out Pkcs7 SignedData which follows the EFI_VARIABLE_AUTHENTICATION_2 descriptor.\r
2144 // AuthInfo.Hdr.dwLength is the length of the entire certificate, including the length of the header.\r
2145 //\r
2d3fb919 2146 SigData = CertData->AuthInfo.CertData;\r
2147 SigDataSize = CertData->AuthInfo.Hdr.dwLength - (UINT32) (OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID, CertData));\r
648f98d1 2148\r
0c18794e 2149 //\r
2150 // Find out the new data payload which follows Pkcs7 SignedData directly.\r
2151 //\r
2d3fb919 2152 PayloadPtr = SigData + SigDataSize;\r
2153 PayloadSize = DataSize - OFFSET_OF_AUTHINFO2_CERT_DATA - (UINTN) SigDataSize;\r
0c18794e 2154\r
2155 //\r
2156 // Construct a buffer to fill with (VariableName, VendorGuid, Attributes, TimeStamp, Data).\r
2157 //\r
2d3fb919 2158 NewDataSize = PayloadSize + sizeof (EFI_TIME) + sizeof (UINT32) +\r
2159 sizeof (EFI_GUID) + StrSize (VariableName) - sizeof (CHAR16);\r
2160 NewData = mSerializationRuntimeBuffer;\r
0c18794e 2161\r
2d3fb919 2162 Buffer = NewData;\r
2163 Length = StrLen (VariableName) * sizeof (CHAR16);\r
2164 CopyMem (Buffer, VariableName, Length);\r
2165 Buffer += Length;\r
0c18794e 2166\r
2d3fb919 2167 Length = sizeof (EFI_GUID);\r
2168 CopyMem (Buffer, VendorGuid, Length);\r
2169 Buffer += Length;\r
0c18794e 2170\r
2d3fb919 2171 Length = sizeof (UINT32);\r
2172 CopyMem (Buffer, &Attr, Length);\r
2173 Buffer += Length;\r
0c18794e 2174\r
2d3fb919 2175 Length = sizeof (EFI_TIME);\r
2176 CopyMem (Buffer, &CertData->TimeStamp, Length);\r
2177 Buffer += Length;\r
0c18794e 2178\r
2d3fb919 2179 CopyMem (Buffer, PayloadPtr, PayloadSize);\r
0c18794e 2180\r
ed47ae02 2181 if (AuthVarType == AuthVarTypePk) {\r
0c18794e 2182 //\r
74b5fd7a
FS
2183 // Verify that the signature has been made with the current Platform Key (no chaining for PK).\r
2184 // First, get signer's certificates from SignedData.\r
2185 //\r
2186 VerifyStatus = Pkcs7GetSigners (\r
2187 SigData,\r
2188 SigDataSize,\r
2189 &SignerCerts,\r
2190 &CertStackSize,\r
2191 &RootCert,\r
2192 &RootCertSize\r
2193 );\r
2194 if (!VerifyStatus) {\r
2195 goto Exit;\r
2196 }\r
2197\r
2198 //\r
2199 // Second, get the current platform key from variable. Check whether it's identical with signer's certificates\r
2200 // in SignedData. If not, return error immediately.\r
0c18794e 2201 //\r
2202 Status = FindVariable (\r
2d3fb919 2203 EFI_PLATFORM_KEY_NAME,\r
2204 &gEfiGlobalVariableGuid,\r
2205 &PkVariable,\r
ecc722ad 2206 &mVariableModuleGlobal->VariableGlobal,\r
2207 FALSE\r
0c18794e 2208 );\r
2209 if (EFI_ERROR (Status)) {\r
74b5fd7a
FS
2210 VerifyStatus = FALSE;\r
2211 goto Exit;\r
0c18794e 2212 }\r
0c18794e 2213 CertList = (EFI_SIGNATURE_LIST *) GetVariableDataPtr (PkVariable.CurrPtr);\r
2214 Cert = (EFI_SIGNATURE_DATA *) ((UINT8 *) CertList + sizeof (EFI_SIGNATURE_LIST) + CertList->SignatureHeaderSize);\r
74b5fd7a
FS
2215 if ((RootCertSize != (CertList->SignatureSize - (sizeof (EFI_SIGNATURE_DATA) - 1))) ||\r
2216 (CompareMem (Cert->SignatureData, RootCert, RootCertSize) != 0)) {\r
2217 VerifyStatus = FALSE;\r
2218 goto Exit;\r
2219 }\r
0c18794e 2220\r
2221 //\r
2222 // Verify Pkcs7 SignedData via Pkcs7Verify library.\r
2223 //\r
2224 VerifyStatus = Pkcs7Verify (\r
2225 SigData,\r
2226 SigDataSize,\r
2227 RootCert,\r
2228 RootCertSize,\r
2229 NewData,\r
2230 NewDataSize\r
2231 );\r
2232\r
ed47ae02 2233 } else if (AuthVarType == AuthVarTypeKek) {\r
2d3fb919 2234\r
0c18794e 2235 //\r
2236 // Get KEK database from variable.\r
2237 //\r
2238 Status = FindVariable (\r
2d3fb919 2239 EFI_KEY_EXCHANGE_KEY_NAME,\r
2240 &gEfiGlobalVariableGuid,\r
2241 &KekVariable,\r
ecc722ad 2242 &mVariableModuleGlobal->VariableGlobal,\r
2243 FALSE\r
0c18794e 2244 );\r
2245 if (EFI_ERROR (Status)) {\r
2246 return Status;\r
2247 }\r
2248\r
2249 //\r
2250 // Ready to verify Pkcs7 SignedData. Go through KEK Signature Database to find out X.509 CertList.\r
2d3fb919 2251 //\r
0c18794e 2252 KekDataSize = KekVariable.CurrPtr->DataSize;\r
2253 CertList = (EFI_SIGNATURE_LIST *) GetVariableDataPtr (KekVariable.CurrPtr);\r
2254 while ((KekDataSize > 0) && (KekDataSize >= CertList->SignatureListSize)) {\r
2255 if (CompareGuid (&CertList->SignatureType, &gEfiCertX509Guid)) {\r
2256 Cert = (EFI_SIGNATURE_DATA *) ((UINT8 *) CertList + sizeof (EFI_SIGNATURE_LIST) + CertList->SignatureHeaderSize);\r
2257 CertCount = (CertList->SignatureListSize - sizeof (EFI_SIGNATURE_LIST) - CertList->SignatureHeaderSize) / CertList->SignatureSize;\r
2258 for (Index = 0; Index < CertCount; Index++) {\r
2259 //\r
2260 // Iterate each Signature Data Node within this CertList for a verify\r
2261 //\r
2262 RootCert = Cert->SignatureData;\r
4e33001c 2263 RootCertSize = CertList->SignatureSize - (sizeof (EFI_SIGNATURE_DATA) - 1);\r
2d3fb919 2264\r
0c18794e 2265 //\r
2266 // Verify Pkcs7 SignedData via Pkcs7Verify library.\r
2267 //\r
2268 VerifyStatus = Pkcs7Verify (\r
2269 SigData,\r
2270 SigDataSize,\r
2271 RootCert,\r
2272 RootCertSize,\r
2273 NewData,\r
2274 NewDataSize\r
2275 );\r
2276 if (VerifyStatus) {\r
2277 goto Exit;\r
2278 }\r
2279 Cert = (EFI_SIGNATURE_DATA *) ((UINT8 *) Cert + CertList->SignatureSize);\r
2280 }\r
2281 }\r
2282 KekDataSize -= CertList->SignatureListSize;\r
2283 CertList = (EFI_SIGNATURE_LIST *) ((UINT8 *) CertList + CertList->SignatureListSize);\r
2284 }\r
ed47ae02 2285 } else if (AuthVarType == AuthVarTypePriv) {\r
2286\r
2287 //\r
2288 // Process common authenticated variable except PK/KEK/DB/DBX.\r
2289 // Get signer's certificates from SignedData.\r
2290 //\r
2291 VerifyStatus = Pkcs7GetSigners (\r
2292 SigData,\r
2293 SigDataSize,\r
2294 &SignerCerts,\r
2295 &CertStackSize,\r
2296 &RootCert,\r
2297 &RootCertSize\r
2298 );\r
2299 if (!VerifyStatus) {\r
2300 goto Exit;\r
2301 }\r
2302\r
2303 //\r
2304 // Get previously stored signer's certificates from certdb for existing\r
2305 // variable. Check whether they are identical with signer's certificates\r
2306 // in SignedData. If not, return error immediately.\r
2307 //\r
2308 if ((Variable->CurrPtr != NULL)) {\r
2309 VerifyStatus = FALSE;\r
2310\r
2311 Status = GetCertsFromDb (VariableName, VendorGuid, &CertsInCertDb, &CertsSizeinDb);\r
2312 if (EFI_ERROR (Status)) {\r
2313 goto Exit;\r
2314 }\r
2315 \r
2316 if ((CertStackSize != CertsSizeinDb) ||\r
2317 (CompareMem (SignerCerts, CertsInCertDb, CertsSizeinDb) != 0)) {\r
2318 goto Exit;\r
2319 }\r
2320 }\r
2321\r
2322 VerifyStatus = Pkcs7Verify (\r
2323 SigData,\r
2324 SigDataSize,\r
2325 RootCert,\r
2326 RootCertSize,\r
2327 NewData,\r
2328 NewDataSize\r
2329 );\r
2330 if (!VerifyStatus) {\r
2331 goto Exit;\r
2332 }\r
2333\r
2334 //\r
2335 // Delete signer's certificates when delete the common authenticated variable.\r
2336 //\r
275beb2b 2337 if ((PayloadSize == 0) && (Variable->CurrPtr != NULL) && ((Attributes & EFI_VARIABLE_APPEND_WRITE) == 0)) {\r
ed47ae02 2338 Status = DeleteCertsFromDb (VariableName, VendorGuid);\r
2339 if (EFI_ERROR (Status)) {\r
2340 VerifyStatus = FALSE;\r
2341 goto Exit;\r
2342 }\r
275beb2b 2343 } else if (Variable->CurrPtr == NULL && PayloadSize != 0) {\r
ed47ae02 2344 //\r
2345 // Insert signer's certificates when adding a new common authenticated variable.\r
2346 //\r
2347 Status = InsertCertsToDb (VariableName, VendorGuid, SignerCerts, CertStackSize);\r
2348 if (EFI_ERROR (Status)) {\r
2349 VerifyStatus = FALSE;\r
2350 goto Exit;\r
2351 }\r
2352 }\r
785d84ea 2353 } else if (AuthVarType == AuthVarTypePayload) {\r
2354 CertList = (EFI_SIGNATURE_LIST *) PayloadPtr;\r
2355 Cert = (EFI_SIGNATURE_DATA *) ((UINT8 *) CertList + sizeof (EFI_SIGNATURE_LIST) + CertList->SignatureHeaderSize);\r
2356 RootCert = Cert->SignatureData;\r
2357 RootCertSize = CertList->SignatureSize - (sizeof (EFI_SIGNATURE_DATA) - 1);\r
2358 \r
2359 // Verify Pkcs7 SignedData via Pkcs7Verify library.\r
2360 //\r
2361 VerifyStatus = Pkcs7Verify (\r
2362 SigData,\r
2363 SigDataSize,\r
2364 RootCert,\r
2365 RootCertSize,\r
2366 NewData,\r
2367 NewDataSize\r
2368 );\r
ed47ae02 2369 } else {\r
2370 return EFI_SECURITY_VIOLATION;\r
0c18794e 2371 }\r
2372\r
2373Exit:\r
2374\r
74b5fd7a 2375 if (AuthVarType == AuthVarTypePk || AuthVarType == AuthVarTypePriv) {\r
ed47ae02 2376 Pkcs7FreeSigners (RootCert);\r
2377 Pkcs7FreeSigners (SignerCerts);\r
2378 }\r
2379\r
0c18794e 2380 if (!VerifyStatus) {\r
2381 return EFI_SECURITY_VIOLATION;\r
2382 }\r
2383\r
d912bad7 2384 Status = CheckSignatureListFormat(VariableName, VendorGuid, PayloadPtr, PayloadSize);\r
2385 if (EFI_ERROR (Status)) {\r
2386 return Status;\r
2387 }\r
2388\r
2d3fb919 2389 if ((PayloadSize == 0) && (VarDel != NULL)) {\r
0c18794e 2390 *VarDel = TRUE;\r
2391 }\r
2d3fb919 2392\r
0c18794e 2393 //\r
2394 // Final step: Update/Append Variable if it pass Pkcs7Verify\r
2395 //\r
ed47ae02 2396 return UpdateVariable (\r
2397 VariableName,\r
2398 VendorGuid,\r
2399 PayloadPtr,\r
2400 PayloadSize,\r
2401 Attributes,\r
2402 0,\r
2403 0,\r
2404 Variable,\r
2405 &CertData->TimeStamp\r
2406 );\r
0c18794e 2407}\r
ed47ae02 2408\r