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