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