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