2 HII Config Access protocol implementation of SecureBoot configuration module.
4 Copyright (c) 2011 - 2012, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "SecureBootConfigImpl.h"
17 CHAR16 mSecureBootStorageName
[] = L
"SECUREBOOT_CONFIGURATION";
19 SECUREBOOT_CONFIG_PRIVATE_DATA mSecureBootConfigPrivateDateTemplate
= {
20 SECUREBOOT_CONFIG_PRIVATE_DATA_SIGNATURE
,
22 SecureBootExtractConfig
,
23 SecureBootRouteConfig
,
28 HII_VENDOR_DEVICE_PATH mSecureBootHiiVendorDevicePath
= {
34 (UINT8
) (sizeof (VENDOR_DEVICE_PATH
)),
35 (UINT8
) ((sizeof (VENDOR_DEVICE_PATH
)) >> 8)
38 SECUREBOOT_CONFIG_FORM_SET_GUID
42 END_ENTIRE_DEVICE_PATH_SUBTYPE
,
44 (UINT8
) (END_DEVICE_PATH_LENGTH
),
45 (UINT8
) ((END_DEVICE_PATH_LENGTH
) >> 8)
52 // OID ASN.1 Value for Hash Algorithms
54 UINT8 mHashOidValue
[] = {
55 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x02, 0x05, // OBJ_md5
56 0x2B, 0x0E, 0x03, 0x02, 0x1A, // OBJ_sha1
57 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x04, // OBJ_sha224
58 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, // OBJ_sha256
59 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02, // OBJ_sha384
60 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03, // OBJ_sha512
63 HASH_TABLE mHash
[] = {
64 { L
"SHA1", 20, &mHashOidValue
[8], 5, Sha1GetContextSize
, Sha1Init
, Sha1Update
, Sha1Final
},
65 { L
"SHA224", 28, &mHashOidValue
[13], 9, NULL
, NULL
, NULL
, NULL
},
66 { L
"SHA256", 32, &mHashOidValue
[22], 9, Sha256GetContextSize
,Sha256Init
, Sha256Update
, Sha256Final
},
67 { L
"SHA384", 48, &mHashOidValue
[31], 9, NULL
, NULL
, NULL
, NULL
},
68 { L
"SHA512", 64, &mHashOidValue
[40], 9, NULL
, NULL
, NULL
, NULL
}
72 // Variable Definitions
73 UINT32 mPeCoffHeaderOffset
= 0;
74 WIN_CERTIFICATE
*mCertificate
= NULL
;
75 IMAGE_TYPE mImageType
;
76 UINT8
*mImageBase
= NULL
;
78 UINT8 mImageDigest
[MAX_DIGEST_SIZE
];
79 UINTN mImageDigestSize
;
81 EFI_IMAGE_SECURITY_DATA_DIRECTORY
*mSecDataDir
= NULL
;
82 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION mNtHeader
;
86 Set Secure Boot option into variable space.
88 @param[in] VarValue The option of Secure Boot.
90 @retval EFI_SUCCESS The operation is finished successfully.
91 @retval Others Other errors as indicated.
95 SaveSecureBootVariable (
101 Status
= gRT
->SetVariable (
102 EFI_SECURE_BOOT_ENABLE_NAME
,
103 &gEfiSecureBootEnableDisableGuid
,
104 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
112 Create a time based data payload by concatenating the EFI_VARIABLE_AUTHENTICATION_2
113 descriptor with the input data. NO authentication is required in this function.
115 @param[in, out] DataSize On input, the size of Data buffer in bytes.
116 On output, the size of data returned in Data
118 @param[in, out] Data On input, Pointer to data buffer to be wrapped or
119 pointer to NULL to wrap an empty payload.
120 On output, Pointer to the new payload date buffer allocated from pool,
121 it's caller's responsibility to free the memory when finish using it.
123 @retval EFI_SUCCESS Create time based payload successfully.
124 @retval EFI_OUT_OF_RESOURCES There are not enough memory resourses to create time based payload.
125 @retval EFI_INVALID_PARAMETER The parameter is invalid.
126 @retval Others Unexpected error happens.
130 CreateTimeBasedPayload (
131 IN OUT UINTN
*DataSize
,
139 EFI_VARIABLE_AUTHENTICATION_2
*DescriptorData
;
140 UINTN DescriptorSize
;
143 if (Data
== NULL
|| DataSize
== NULL
) {
144 return EFI_INVALID_PARAMETER
;
148 // In Setup mode or Custom mode, the variable does not need to be signed but the
149 // parameters to the SetVariable() call still need to be prepared as authenticated
150 // variable. So we create EFI_VARIABLE_AUTHENTICATED_2 descriptor without certificate
154 PayloadSize
= *DataSize
;
156 DescriptorSize
= OFFSET_OF (EFI_VARIABLE_AUTHENTICATION_2
, AuthInfo
) + OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID
, CertData
);
157 NewData
= (UINT8
*) AllocateZeroPool (DescriptorSize
+ PayloadSize
);
158 if (NewData
== NULL
) {
159 return EFI_OUT_OF_RESOURCES
;
162 if ((Payload
!= NULL
) && (PayloadSize
!= 0)) {
163 CopyMem (NewData
+ DescriptorSize
, Payload
, PayloadSize
);
166 DescriptorData
= (EFI_VARIABLE_AUTHENTICATION_2
*) (NewData
);
168 ZeroMem (&Time
, sizeof (EFI_TIME
));
169 Status
= gRT
->GetTime (&Time
, NULL
);
170 if (EFI_ERROR (Status
)) {
179 CopyMem (&DescriptorData
->TimeStamp
, &Time
, sizeof (EFI_TIME
));
181 DescriptorData
->AuthInfo
.Hdr
.dwLength
= OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID
, CertData
);
182 DescriptorData
->AuthInfo
.Hdr
.wRevision
= 0x0200;
183 DescriptorData
->AuthInfo
.Hdr
.wCertificateType
= WIN_CERT_TYPE_EFI_GUID
;
184 CopyGuid (&DescriptorData
->AuthInfo
.CertType
, &gEfiCertPkcs7Guid
);
186 if (Payload
!= NULL
) {
190 *DataSize
= DescriptorSize
+ PayloadSize
;
196 Internal helper function to delete a Variable given its name and GUID, NO authentication
199 @param[in] VariableName Name of the Variable.
200 @param[in] VendorGuid GUID of the Variable.
202 @retval EFI_SUCCESS Variable deleted successfully.
203 @retval Others The driver failed to start the device.
208 IN CHAR16
*VariableName
,
209 IN EFI_GUID
*VendorGuid
218 GetVariable2 (VariableName
, VendorGuid
, &Variable
, NULL
);
219 if (Variable
== NULL
) {
225 Attr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
226 | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
228 Status
= CreateTimeBasedPayload (&DataSize
, &Data
);
229 if (EFI_ERROR (Status
)) {
230 DEBUG ((EFI_D_ERROR
, "Fail to create time-based data payload: %r", Status
));
234 Status
= gRT
->SetVariable (
248 Generate the PK signature list from the X509 Certificate storing file (.cer)
250 @param[in] X509File FileHandle of X509 Certificate storing file.
251 @param[out] PkCert Point to the data buffer to store the signature list.
253 @return EFI_UNSUPPORTED Unsupported Key Length.
254 @return EFI_OUT_OF_RESOURCES There are not enough memory resourses to form the signature list.
258 CreatePkX509SignatureList (
259 IN EFI_FILE_HANDLE X509File
,
260 OUT EFI_SIGNATURE_LIST
**PkCert
266 EFI_SIGNATURE_DATA
*PkCertData
;
272 Status
= ReadFileContent (X509File
, (VOID
**) &X509Data
, &X509DataSize
, 0);
273 if (EFI_ERROR (Status
)) {
276 ASSERT (X509Data
!= NULL
);
279 // Allocate space for PK certificate list and initialize it.
280 // Create PK database entry with SignatureHeaderSize equals 0.
282 *PkCert
= (EFI_SIGNATURE_LIST
*) AllocateZeroPool (
283 sizeof(EFI_SIGNATURE_LIST
) + sizeof(EFI_SIGNATURE_DATA
) - 1
286 if (*PkCert
== NULL
) {
287 Status
= EFI_OUT_OF_RESOURCES
;
291 (*PkCert
)->SignatureListSize
= (UINT32
) (sizeof(EFI_SIGNATURE_LIST
)
292 + sizeof(EFI_SIGNATURE_DATA
) - 1
294 (*PkCert
)->SignatureSize
= (UINT32
) (sizeof(EFI_SIGNATURE_DATA
) - 1 + X509DataSize
);
295 (*PkCert
)->SignatureHeaderSize
= 0;
296 CopyGuid (&(*PkCert
)->SignatureType
, &gEfiCertX509Guid
);
297 PkCertData
= (EFI_SIGNATURE_DATA
*) ((UINTN
)(*PkCert
)
298 + sizeof(EFI_SIGNATURE_LIST
)
299 + (*PkCert
)->SignatureHeaderSize
);
300 CopyGuid (&PkCertData
->SignatureOwner
, &gEfiGlobalVariableGuid
);
302 // Fill the PK database with PKpub data from X509 certificate file.
304 CopyMem (&(PkCertData
->SignatureData
[0]), X509Data
, X509DataSize
);
308 if (X509Data
!= NULL
) {
312 if (EFI_ERROR(Status
) && *PkCert
!= NULL
) {
321 Enroll new PK into the System without original PK's authentication.
323 The SignatureOwner GUID will be the same with PK's vendorguid.
325 @param[in] PrivateData The module's private data.
327 @retval EFI_SUCCESS New PK enrolled successfully.
328 @retval EFI_INVALID_PARAMETER The parameter is invalid.
329 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
334 IN SECUREBOOT_CONFIG_PRIVATE_DATA
* Private
340 EFI_SIGNATURE_LIST
*PkCert
;
343 if (Private
->FileContext
->FileName
== NULL
) {
344 return EFI_INVALID_PARAMETER
;
350 // Parse the file's postfix. Only support *.cer(X509) files.
352 FilePostFix
= Private
->FileContext
->FileName
+ StrLen (Private
->FileContext
->FileName
) - 4;
353 if (CompareMem (FilePostFix
, L
".cer",4)) {
354 DEBUG ((EFI_D_ERROR
, "Don't support the file, only *.cer is supported."));
355 return EFI_INVALID_PARAMETER
;
357 DEBUG ((EFI_D_INFO
, "FileName= %s\n", Private
->FileContext
->FileName
));
358 DEBUG ((EFI_D_INFO
, "FilePostFix = %s\n", FilePostFix
));
361 // Prase the selected PK file and generature PK certificate list.
363 Status
= CreatePkX509SignatureList (
364 Private
->FileContext
->FHandle
,
367 if (EFI_ERROR (Status
)) {
370 ASSERT (PkCert
!= NULL
);
373 // Set Platform Key variable.
375 Attr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
376 | EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
377 DataSize
= PkCert
->SignatureListSize
;
378 Status
= CreateTimeBasedPayload (&DataSize
, (UINT8
**) &PkCert
);
379 if (EFI_ERROR (Status
)) {
380 DEBUG ((EFI_D_ERROR
, "Fail to create time-based data payload: %r", Status
));
384 Status
= gRT
->SetVariable(
385 EFI_PLATFORM_KEY_NAME
,
386 &gEfiGlobalVariableGuid
,
391 if (EFI_ERROR (Status
)) {
392 if (Status
== EFI_OUT_OF_RESOURCES
) {
393 DEBUG ((EFI_D_ERROR
, "Enroll PK failed with out of resource.\n"));
400 if (PkCert
!= NULL
) {
404 if (Private
->FileContext
->FHandle
!= NULL
) {
405 CloseFile (Private
->FileContext
->FHandle
);
406 Private
->FileContext
->FHandle
= NULL
;
413 Remove the PK variable.
415 @retval EFI_SUCCESS Delete PK successfully.
416 @retval Others Could not allow to delete PK.
426 Status
= DeleteVariable (
427 EFI_PLATFORM_KEY_NAME
,
428 &gEfiGlobalVariableGuid
434 Enroll a new KEK item from public key storing file (*.pbk).
436 @param[in] PrivateData The module's private data.
438 @retval EFI_SUCCESS New KEK enrolled successfully.
439 @retval EFI_INVALID_PARAMETER The parameter is invalid.
440 @retval EFI_UNSUPPORTED Unsupported command.
441 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
446 IN SECUREBOOT_CONFIG_PRIVATE_DATA
*Private
452 EFI_SIGNATURE_LIST
*KekSigList
;
455 CPL_KEY_INFO
*KeyInfo
;
456 EFI_SIGNATURE_DATA
*KEKSigData
;
457 UINTN KekSigListSize
;
472 // Form the KeKpub certificate list into EFI_SIGNATURE_LIST type.
473 // First, We have to parse out public key data from the pbk key file.
475 Status
= ReadFileContent (
476 Private
->FileContext
->FHandle
,
481 if (EFI_ERROR (Status
)) {
484 ASSERT (KeyBlob
!= NULL
);
485 KeyInfo
= (CPL_KEY_INFO
*) KeyBlob
;
486 if (KeyInfo
->KeyLengthInBits
/ 8 != WIN_CERT_UEFI_RSA2048_SIZE
) {
487 DEBUG ((DEBUG_ERROR
, "Unsupported key length, Only RSA2048 is supported.\n"));
488 Status
= EFI_UNSUPPORTED
;
493 // Convert the Public key to fix octet string format represented in RSA PKCS#1.
495 KeyLenInBytes
= KeyInfo
->KeyLengthInBits
/ 8;
496 KeyBuffer
= AllocateZeroPool (KeyLenInBytes
);
497 if (KeyBuffer
== NULL
) {
498 Status
= EFI_OUT_OF_RESOURCES
;
502 (UINTN
*) (KeyBlob
+ sizeof (CPL_KEY_INFO
)),
503 KeyLenInBytes
/ sizeof (UINTN
),
507 CopyMem(KeyBlob
+ sizeof(CPL_KEY_INFO
), KeyBuffer
, KeyLenInBytes
);
510 // Form an new EFI_SIGNATURE_LIST.
512 KekSigListSize
= sizeof(EFI_SIGNATURE_LIST
)
513 + sizeof(EFI_SIGNATURE_DATA
) - 1
514 + WIN_CERT_UEFI_RSA2048_SIZE
;
516 KekSigList
= (EFI_SIGNATURE_LIST
*) AllocateZeroPool (KekSigListSize
);
517 if (KekSigList
== NULL
) {
518 Status
= EFI_OUT_OF_RESOURCES
;
522 KekSigList
->SignatureListSize
= sizeof(EFI_SIGNATURE_LIST
)
523 + sizeof(EFI_SIGNATURE_DATA
) - 1
524 + WIN_CERT_UEFI_RSA2048_SIZE
;
525 KekSigList
->SignatureHeaderSize
= 0;
526 KekSigList
->SignatureSize
= sizeof(EFI_SIGNATURE_DATA
) - 1 + WIN_CERT_UEFI_RSA2048_SIZE
;
527 CopyGuid (&KekSigList
->SignatureType
, &gEfiCertRsa2048Guid
);
529 KEKSigData
= (EFI_SIGNATURE_DATA
*)((UINT8
*)KekSigList
+ sizeof(EFI_SIGNATURE_LIST
));
530 CopyGuid (&KEKSigData
->SignatureOwner
, Private
->SignatureGUID
);
532 KEKSigData
->SignatureData
,
533 KeyBlob
+ sizeof(CPL_KEY_INFO
),
534 WIN_CERT_UEFI_RSA2048_SIZE
538 // Check if KEK entry has been already existed.
539 // If true, use EFI_VARIABLE_APPEND_WRITE attribute to append the
540 // new KEK to original variable.
542 Attr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
543 | EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
544 Status
= CreateTimeBasedPayload (&KekSigListSize
, (UINT8
**) &KekSigList
);
545 if (EFI_ERROR (Status
)) {
546 DEBUG ((EFI_D_ERROR
, "Fail to create time-based data payload: %r", Status
));
550 Status
= gRT
->GetVariable(
551 EFI_KEY_EXCHANGE_KEY_NAME
,
552 &gEfiGlobalVariableGuid
,
557 if (Status
== EFI_BUFFER_TOO_SMALL
) {
558 Attr
|= EFI_VARIABLE_APPEND_WRITE
;
559 } else if (Status
!= EFI_NOT_FOUND
) {
564 // Done. Now we have formed the correct KEKpub database item, just set it into variable storage,
566 Status
= gRT
->SetVariable(
567 EFI_KEY_EXCHANGE_KEY_NAME
,
568 &gEfiGlobalVariableGuid
,
573 if (EFI_ERROR (Status
)) {
579 CloseFile (Private
->FileContext
->FHandle
);
580 Private
->FileContext
->FHandle
= NULL
;
581 Private
->FileContext
->FileName
= NULL
;
583 if (Private
->SignatureGUID
!= NULL
) {
584 FreePool (Private
->SignatureGUID
);
585 Private
->SignatureGUID
= NULL
;
588 if (KeyBlob
!= NULL
) {
591 if (KeyBuffer
!= NULL
) {
592 FreePool (KeyBuffer
);
594 if (KekSigList
!= NULL
) {
595 FreePool (KekSigList
);
602 Enroll a new KEK item from X509 certificate file.
604 @param[in] PrivateData The module's private data.
606 @retval EFI_SUCCESS New X509 is enrolled successfully.
607 @retval EFI_INVALID_PARAMETER The parameter is invalid.
608 @retval EFI_UNSUPPORTED Unsupported command.
609 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
614 IN SECUREBOOT_CONFIG_PRIVATE_DATA
*Private
620 EFI_SIGNATURE_DATA
*KEKSigData
;
621 EFI_SIGNATURE_LIST
*KekSigList
;
623 UINTN KekSigListSize
;
633 Status
= ReadFileContent (
634 Private
->FileContext
->FHandle
,
639 if (EFI_ERROR (Status
)) {
642 ASSERT (X509Data
!= NULL
);
644 KekSigListSize
= sizeof(EFI_SIGNATURE_LIST
) + sizeof(EFI_SIGNATURE_DATA
) - 1 + X509DataSize
;
645 KekSigList
= (EFI_SIGNATURE_LIST
*) AllocateZeroPool (KekSigListSize
);
646 if (KekSigList
== NULL
) {
647 Status
= EFI_OUT_OF_RESOURCES
;
652 // Fill Certificate Database parameters.
654 KekSigList
->SignatureListSize
= (UINT32
) KekSigListSize
;
655 KekSigList
->SignatureHeaderSize
= 0;
656 KekSigList
->SignatureSize
= (UINT32
) (sizeof(EFI_SIGNATURE_DATA
) - 1 + X509DataSize
);
657 CopyGuid (&KekSigList
->SignatureType
, &gEfiCertX509Guid
);
659 KEKSigData
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) KekSigList
+ sizeof (EFI_SIGNATURE_LIST
));
660 CopyGuid (&KEKSigData
->SignatureOwner
, Private
->SignatureGUID
);
661 CopyMem (KEKSigData
->SignatureData
, X509Data
, X509DataSize
);
664 // Check if KEK been already existed.
665 // If true, use EFI_VARIABLE_APPEND_WRITE attribute to append the
666 // new kek to original variable
668 Attr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
669 | EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
670 Status
= CreateTimeBasedPayload (&KekSigListSize
, (UINT8
**) &KekSigList
);
671 if (EFI_ERROR (Status
)) {
672 DEBUG ((EFI_D_ERROR
, "Fail to create time-based data payload: %r", Status
));
676 Status
= gRT
->GetVariable(
677 EFI_KEY_EXCHANGE_KEY_NAME
,
678 &gEfiGlobalVariableGuid
,
683 if (Status
== EFI_BUFFER_TOO_SMALL
) {
684 Attr
|= EFI_VARIABLE_APPEND_WRITE
;
685 } else if (Status
!= EFI_NOT_FOUND
) {
689 Status
= gRT
->SetVariable(
690 EFI_KEY_EXCHANGE_KEY_NAME
,
691 &gEfiGlobalVariableGuid
,
696 if (EFI_ERROR (Status
)) {
702 CloseFile (Private
->FileContext
->FHandle
);
703 Private
->FileContext
->FileName
= NULL
;
704 Private
->FileContext
->FHandle
= NULL
;
706 if (Private
->SignatureGUID
!= NULL
) {
707 FreePool (Private
->SignatureGUID
);
708 Private
->SignatureGUID
= NULL
;
711 if (KekSigList
!= NULL
) {
712 FreePool (KekSigList
);
719 Enroll new KEK into the System without PK's authentication.
720 The SignatureOwner GUID will be Private->SignatureGUID.
722 @param[in] PrivateData The module's private data.
724 @retval EFI_SUCCESS New KEK enrolled successful.
725 @retval EFI_INVALID_PARAMETER The parameter is invalid.
726 @retval others Fail to enroll KEK data.
730 EnrollKeyExchangeKey (
731 IN SECUREBOOT_CONFIG_PRIVATE_DATA
*Private
736 if ((Private
->FileContext
->FileName
== NULL
) || (Private
->SignatureGUID
== NULL
)) {
737 return EFI_INVALID_PARAMETER
;
741 // Parse the file's postfix. Supports .cer and .der file as X509 certificate,
742 // and .pbk as RSA public key file.
744 FilePostFix
= Private
->FileContext
->FileName
+ StrLen (Private
->FileContext
->FileName
) - 4;
745 if ((CompareMem (FilePostFix
, L
".cer",4) == 0) || (CompareMem (FilePostFix
, L
".der",4) == 0)) {
746 return EnrollX509ToKek (Private
);
747 } else if (CompareMem (FilePostFix
, L
".pbk",4) == 0) {
748 return EnrollRsa2048ToKek (Private
);
750 return EFI_INVALID_PARAMETER
;
755 Enroll a new X509 certificate into Signature Database (DB or DBX) without
756 KEK's authentication.
758 @param[in] PrivateData The module's private data.
759 @param[in] VariableName Variable name of signature database, must be
760 EFI_IMAGE_SECURITY_DATABASE or EFI_IMAGE_SECURITY_DATABASE1.
762 @retval EFI_SUCCESS New X509 is enrolled successfully.
763 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
768 IN SECUREBOOT_CONFIG_PRIVATE_DATA
*Private
,
769 IN CHAR16
*VariableName
775 EFI_SIGNATURE_LIST
*SigDBCert
;
776 EFI_SIGNATURE_DATA
*SigDBCertData
;
787 SigDBCertData
= NULL
;
790 Status
= ReadFileContent (
791 Private
->FileContext
->FHandle
,
796 if (EFI_ERROR (Status
)) {
799 ASSERT (X509Data
!= NULL
);
801 SigDBSize
= sizeof(EFI_SIGNATURE_LIST
) + sizeof(EFI_SIGNATURE_DATA
) - 1 + X509DataSize
;
803 Data
= AllocateZeroPool (SigDBSize
);
805 Status
= EFI_OUT_OF_RESOURCES
;
810 // Fill Certificate Database parameters.
812 SigDBCert
= (EFI_SIGNATURE_LIST
*) Data
;
813 SigDBCert
->SignatureListSize
= (UINT32
) SigDBSize
;
814 SigDBCert
->SignatureHeaderSize
= 0;
815 SigDBCert
->SignatureSize
= (UINT32
) (sizeof(EFI_SIGNATURE_DATA
) - 1 + X509DataSize
);
816 CopyGuid (&SigDBCert
->SignatureType
, &gEfiCertX509Guid
);
818 SigDBCertData
= (EFI_SIGNATURE_DATA
*) ((UINT8
* ) SigDBCert
+ sizeof (EFI_SIGNATURE_LIST
));
819 CopyGuid (&SigDBCertData
->SignatureOwner
, Private
->SignatureGUID
);
820 CopyMem ((UINT8
* ) (SigDBCertData
->SignatureData
), X509Data
, X509DataSize
);
823 // Check if signature database entry has been already existed.
824 // If true, use EFI_VARIABLE_APPEND_WRITE attribute to append the
825 // new signature data to original variable
827 Attr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
828 | EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
829 Status
= CreateTimeBasedPayload (&SigDBSize
, (UINT8
**) &Data
);
830 if (EFI_ERROR (Status
)) {
831 DEBUG ((EFI_D_ERROR
, "Fail to create time-based data payload: %r", Status
));
835 Status
= gRT
->GetVariable(
837 &gEfiImageSecurityDatabaseGuid
,
842 if (Status
== EFI_BUFFER_TOO_SMALL
) {
843 Attr
|= EFI_VARIABLE_APPEND_WRITE
;
844 } else if (Status
!= EFI_NOT_FOUND
) {
848 Status
= gRT
->SetVariable(
850 &gEfiImageSecurityDatabaseGuid
,
855 if (EFI_ERROR (Status
)) {
861 CloseFile (Private
->FileContext
->FHandle
);
862 Private
->FileContext
->FileName
= NULL
;
863 Private
->FileContext
->FHandle
= NULL
;
865 if (Private
->SignatureGUID
!= NULL
) {
866 FreePool (Private
->SignatureGUID
);
867 Private
->SignatureGUID
= NULL
;
874 if (X509Data
!= NULL
) {
882 Load PE/COFF image information into internal buffer and check its validity.
884 @retval EFI_SUCCESS Successful
885 @retval EFI_UNSUPPORTED Invalid PE/COFF file
886 @retval EFI_ABORTED Serious error occurs, like file I/O error etc.
894 EFI_IMAGE_DOS_HEADER
*DosHdr
;
895 EFI_IMAGE_NT_HEADERS32
*NtHeader32
;
896 EFI_IMAGE_NT_HEADERS64
*NtHeader64
;
901 // Read the Dos header
903 DosHdr
= (EFI_IMAGE_DOS_HEADER
*)(mImageBase
);
904 if (DosHdr
->e_magic
== EFI_IMAGE_DOS_SIGNATURE
)
907 // DOS image header is present,
908 // So read the PE header after the DOS image header
910 mPeCoffHeaderOffset
= DosHdr
->e_lfanew
;
914 mPeCoffHeaderOffset
= 0;
918 // Read PE header and check the signature validity and machine compatibility
920 NtHeader32
= (EFI_IMAGE_NT_HEADERS32
*) (mImageBase
+ mPeCoffHeaderOffset
);
921 if (NtHeader32
->Signature
!= EFI_IMAGE_NT_SIGNATURE
)
923 return EFI_UNSUPPORTED
;
926 mNtHeader
.Pe32
= NtHeader32
;
929 // Check the architecture field of PE header and get the Certificate Data Directory data
930 // Note the size of FileHeader field is constant for both IA32 and X64 arch
932 if ((NtHeader32
->FileHeader
.Machine
== EFI_IMAGE_MACHINE_IA32
)
933 || (NtHeader32
->FileHeader
.Machine
== EFI_IMAGE_MACHINE_EBC
)) {
935 // IA-32 Architecture
937 mImageType
= ImageType_IA32
;
938 mSecDataDir
= (EFI_IMAGE_SECURITY_DATA_DIRECTORY
*) &(NtHeader32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
]);
940 else if ((NtHeader32
->FileHeader
.Machine
== EFI_IMAGE_MACHINE_IA64
)
941 || (NtHeader32
->FileHeader
.Machine
== EFI_IMAGE_MACHINE_X64
)) {
943 // 64-bits Architecture
945 mImageType
= ImageType_X64
;
946 NtHeader64
= (EFI_IMAGE_NT_HEADERS64
*) (mImageBase
+ mPeCoffHeaderOffset
);
947 mSecDataDir
= (EFI_IMAGE_SECURITY_DATA_DIRECTORY
*) &(NtHeader64
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
]);
949 return EFI_UNSUPPORTED
;
956 Calculate hash of Pe/Coff image based on the authenticode image hashing in
957 PE/COFF Specification 8.0 Appendix A
959 @param[in] HashAlg Hash algorithm type.
961 @retval TRUE Successfully hash image.
962 @retval FALSE Fail in hash image.
972 EFI_IMAGE_SECTION_HEADER
*Section
;
977 UINTN SumOfBytesHashed
;
978 EFI_IMAGE_SECTION_HEADER
*SectionHeader
;
983 SectionHeader
= NULL
;
986 if ((HashAlg
!= HASHALG_SHA1
) && (HashAlg
!= HASHALG_SHA256
)) {
991 // Initialize context of hash.
993 ZeroMem (mImageDigest
, MAX_DIGEST_SIZE
);
995 if (HashAlg
== HASHALG_SHA1
) {
996 mImageDigestSize
= SHA1_DIGEST_SIZE
;
997 mCertType
= gEfiCertSha1Guid
;
998 } else if (HashAlg
== HASHALG_SHA256
) {
999 mImageDigestSize
= SHA256_DIGEST_SIZE
;
1000 mCertType
= gEfiCertSha256Guid
;
1003 CtxSize
= mHash
[HashAlg
].GetContextSize();
1005 HashCtx
= AllocatePool (CtxSize
);
1006 ASSERT (HashCtx
!= NULL
);
1008 // 1. Load the image header into memory.
1010 // 2. Initialize a SHA hash context.
1011 Status
= mHash
[HashAlg
].HashInit(HashCtx
);
1016 // Measuring PE/COFF Image Header;
1017 // But CheckSum field and SECURITY data directory (certificate) are excluded
1019 Magic
= mNtHeader
.Pe32
->OptionalHeader
.Magic
;
1021 // 3. Calculate the distance from the base of the image header to the image checksum address.
1022 // 4. Hash the image header from its base to beginning of the image checksum.
1024 HashBase
= mImageBase
;
1025 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
1029 HashSize
= (UINTN
) ((UINT8
*) (&mNtHeader
.Pe32
->OptionalHeader
.CheckSum
) - HashBase
);
1032 // Use PE32+ offset.
1034 HashSize
= (UINTN
) ((UINT8
*) (&mNtHeader
.Pe32Plus
->OptionalHeader
.CheckSum
) - HashBase
);
1037 Status
= mHash
[HashAlg
].HashUpdate(HashCtx
, HashBase
, HashSize
);
1042 // 5. Skip over the image checksum (it occupies a single ULONG).
1043 // 6. Get the address of the beginning of the Cert Directory.
1044 // 7. Hash everything from the end of the checksum to the start of the Cert Directory.
1046 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
1050 HashBase
= (UINT8
*) &mNtHeader
.Pe32
->OptionalHeader
.CheckSum
+ sizeof (UINT32
);
1051 HashSize
= (UINTN
) ((UINT8
*) (&mNtHeader
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
]) - HashBase
);
1054 // Use PE32+ offset.
1056 HashBase
= (UINT8
*) &mNtHeader
.Pe32Plus
->OptionalHeader
.CheckSum
+ sizeof (UINT32
);
1057 HashSize
= (UINTN
) ((UINT8
*) (&mNtHeader
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
]) - HashBase
);
1060 Status
= mHash
[HashAlg
].HashUpdate(HashCtx
, HashBase
, HashSize
);
1065 // 8. Skip over the Cert Directory. (It is sizeof(IMAGE_DATA_DIRECTORY) bytes.)
1066 // 9. Hash everything from the end of the Cert Directory to the end of image header.
1068 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
1072 HashBase
= (UINT8
*) &mNtHeader
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
+ 1];
1073 HashSize
= mNtHeader
.Pe32
->OptionalHeader
.SizeOfHeaders
- (UINTN
) ((UINT8
*) (&mNtHeader
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
+ 1]) - mImageBase
);
1076 // Use PE32+ offset.
1078 HashBase
= (UINT8
*) &mNtHeader
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
+ 1];
1079 HashSize
= mNtHeader
.Pe32Plus
->OptionalHeader
.SizeOfHeaders
- (UINTN
) ((UINT8
*) (&mNtHeader
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
+ 1]) - mImageBase
);
1082 Status
= mHash
[HashAlg
].HashUpdate(HashCtx
, HashBase
, HashSize
);
1087 // 10. Set the SUM_OF_BYTES_HASHED to the size of the header.
1089 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
1093 SumOfBytesHashed
= mNtHeader
.Pe32
->OptionalHeader
.SizeOfHeaders
;
1098 SumOfBytesHashed
= mNtHeader
.Pe32Plus
->OptionalHeader
.SizeOfHeaders
;
1102 // 11. Build a temporary table of pointers to all the IMAGE_SECTION_HEADER
1103 // structures in the image. The 'NumberOfSections' field of the image
1104 // header indicates how big the table should be. Do not include any
1105 // IMAGE_SECTION_HEADERs in the table whose 'SizeOfRawData' field is zero.
1107 SectionHeader
= (EFI_IMAGE_SECTION_HEADER
*) AllocateZeroPool (sizeof (EFI_IMAGE_SECTION_HEADER
) * mNtHeader
.Pe32
->FileHeader
.NumberOfSections
);
1108 ASSERT (SectionHeader
!= NULL
);
1110 // 12. Using the 'PointerToRawData' in the referenced section headers as
1111 // a key, arrange the elements in the table in ascending order. In other
1112 // words, sort the section headers according to the disk-file offset of
1115 Section
= (EFI_IMAGE_SECTION_HEADER
*) (
1117 mPeCoffHeaderOffset
+
1119 sizeof (EFI_IMAGE_FILE_HEADER
) +
1120 mNtHeader
.Pe32
->FileHeader
.SizeOfOptionalHeader
1122 for (Index
= 0; Index
< mNtHeader
.Pe32
->FileHeader
.NumberOfSections
; Index
++) {
1124 while ((Pos
> 0) && (Section
->PointerToRawData
< SectionHeader
[Pos
- 1].PointerToRawData
)) {
1125 CopyMem (&SectionHeader
[Pos
], &SectionHeader
[Pos
- 1], sizeof (EFI_IMAGE_SECTION_HEADER
));
1128 CopyMem (&SectionHeader
[Pos
], Section
, sizeof (EFI_IMAGE_SECTION_HEADER
));
1133 // 13. Walk through the sorted table, bring the corresponding section
1134 // into memory, and hash the entire section (using the 'SizeOfRawData'
1135 // field in the section header to determine the amount of data to hash).
1136 // 14. Add the section's 'SizeOfRawData' to SUM_OF_BYTES_HASHED .
1137 // 15. Repeat steps 13 and 14 for all the sections in the sorted table.
1139 for (Index
= 0; Index
< mNtHeader
.Pe32
->FileHeader
.NumberOfSections
; Index
++) {
1140 Section
= &SectionHeader
[Index
];
1141 if (Section
->SizeOfRawData
== 0) {
1144 HashBase
= mImageBase
+ Section
->PointerToRawData
;
1145 HashSize
= (UINTN
) Section
->SizeOfRawData
;
1147 Status
= mHash
[HashAlg
].HashUpdate(HashCtx
, HashBase
, HashSize
);
1152 SumOfBytesHashed
+= HashSize
;
1156 // 16. If the file size is greater than SUM_OF_BYTES_HASHED, there is extra
1157 // data in the file that needs to be added to the hash. This data begins
1158 // at file offset SUM_OF_BYTES_HASHED and its length is:
1159 // FileSize - (CertDirectory->Size)
1161 if (mImageSize
> SumOfBytesHashed
) {
1162 HashBase
= mImageBase
+ SumOfBytesHashed
;
1163 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
1169 mNtHeader
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
].Size
-
1173 // Use PE32+ offset.
1177 mNtHeader
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
].Size
-
1181 Status
= mHash
[HashAlg
].HashUpdate(HashCtx
, HashBase
, HashSize
);
1187 Status
= mHash
[HashAlg
].HashFinal(HashCtx
, mImageDigest
);
1190 if (HashCtx
!= NULL
) {
1193 if (SectionHeader
!= NULL
) {
1194 FreePool (SectionHeader
);
1200 Recognize the Hash algorithm in PE/COFF Authenticode and caculate hash of
1201 Pe/Coff image based on the authenticated image hashing in PE/COFF Specification
1204 @retval EFI_UNSUPPORTED Hash algorithm is not supported.
1205 @retval EFI_SUCCESS Hash successfully.
1214 WIN_CERTIFICATE_EFI_PKCS
*PkcsCertData
;
1216 PkcsCertData
= (WIN_CERTIFICATE_EFI_PKCS
*) (mImageBase
+ mSecDataDir
->Offset
);
1218 for (Index
= 0; Index
< HASHALG_MAX
; Index
++) {
1220 // Check the Hash algorithm in PE/COFF Authenticode.
1221 // According to PKCS#7 Definition:
1222 // SignedData ::= SEQUENCE {
1224 // digestAlgorithms DigestAlgorithmIdentifiers,
1225 // contentInfo ContentInfo,
1227 // The DigestAlgorithmIdentifiers can be used to determine the hash algorithm in PE/COFF hashing
1228 // This field has the fixed offset (+32) in final Authenticode ASN.1 data.
1229 // Fixed offset (+32) is calculated based on two bytes of length encoding.
1231 if ((*(PkcsCertData
->CertData
+ 1) & TWO_BYTE_ENCODE
) != TWO_BYTE_ENCODE
) {
1233 // Only support two bytes of Long Form of Length Encoding.
1239 if (CompareMem (PkcsCertData
->CertData
+ 32, mHash
[Index
].OidValue
, mHash
[Index
].OidLength
) == 0) {
1244 if (Index
== HASHALG_MAX
) {
1245 return EFI_UNSUPPORTED
;
1249 // HASH PE Image based on Hash algorithm in PE/COFF Authenticode.
1251 if (!HashPeImage(Index
)) {
1252 return EFI_UNSUPPORTED
;
1259 Enroll a new executable's signature into Signature Database.
1261 @param[in] PrivateData The module's private data.
1262 @param[in] VariableName Variable name of signature database, must be
1263 EFI_IMAGE_SECURITY_DATABASE or EFI_IMAGE_SECURITY_DATABASE1.
1265 @retval EFI_SUCCESS New signature is enrolled successfully.
1266 @retval EFI_INVALID_PARAMETER The parameter is invalid.
1267 @retval EFI_UNSUPPORTED Unsupported command.
1268 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
1272 EnrollImageSignatureToSigDB (
1273 IN SECUREBOOT_CONFIG_PRIVATE_DATA
*Private
,
1274 IN CHAR16
*VariableName
1278 EFI_SIGNATURE_LIST
*SigDBCert
;
1279 EFI_SIGNATURE_DATA
*SigDBCertData
;
1284 WIN_CERTIFICATE_UEFI_GUID
*GuidCertData
;
1287 GuidCertData
= NULL
;
1290 // Form the SigDB certificate list.
1291 // Format the data item into EFI_SIGNATURE_LIST type.
1293 // We need to parse executable's signature data from specified signed executable file.
1294 // In current implementation, we simply trust the pass-in signed executable file.
1295 // In reality, it's OS's responsibility to verify the signed executable file.
1299 // Read the whole file content
1301 Status
= ReadFileContent(
1302 Private
->FileContext
->FHandle
,
1303 (VOID
**) &mImageBase
,
1307 if (EFI_ERROR (Status
)) {
1310 ASSERT (mImageBase
!= NULL
);
1312 Status
= LoadPeImage ();
1313 if (EFI_ERROR (Status
)) {
1317 if (mSecDataDir
->SizeOfCert
== 0) {
1318 if (!HashPeImage (HASHALG_SHA256
)) {
1319 Status
= EFI_SECURITY_VIOLATION
;
1325 // Read the certificate data
1327 mCertificate
= (WIN_CERTIFICATE
*)(mImageBase
+ mSecDataDir
->Offset
);
1329 if (mCertificate
->wCertificateType
== WIN_CERT_TYPE_EFI_GUID
) {
1330 GuidCertData
= (WIN_CERTIFICATE_UEFI_GUID
*) mCertificate
;
1331 if (CompareMem (&GuidCertData
->CertType
, &gEfiCertTypeRsa2048Sha256Guid
, sizeof(EFI_GUID
)) != 0) {
1332 Status
= EFI_ABORTED
;
1336 if (!HashPeImage (HASHALG_SHA256
)) {
1337 Status
= EFI_ABORTED
;
1341 } else if (mCertificate
->wCertificateType
== WIN_CERT_TYPE_PKCS_SIGNED_DATA
) {
1343 Status
= HashPeImageByType ();
1344 if (EFI_ERROR (Status
)) {
1348 Status
= EFI_ABORTED
;
1354 // Create a new SigDB entry.
1356 SigDBSize
= sizeof(EFI_SIGNATURE_LIST
)
1357 + sizeof(EFI_SIGNATURE_DATA
) - 1
1358 + (UINT32
) mImageDigestSize
;
1360 Data
= (UINT8
*) AllocateZeroPool (SigDBSize
);
1362 Status
= EFI_OUT_OF_RESOURCES
;
1367 // Adjust the Certificate Database parameters.
1369 SigDBCert
= (EFI_SIGNATURE_LIST
*) Data
;
1370 SigDBCert
->SignatureListSize
= (UINT32
) SigDBSize
;
1371 SigDBCert
->SignatureHeaderSize
= 0;
1372 SigDBCert
->SignatureSize
= sizeof(EFI_SIGNATURE_DATA
) - 1 + (UINT32
) mImageDigestSize
;
1373 CopyGuid (&SigDBCert
->SignatureType
, &mCertType
);
1375 SigDBCertData
= (EFI_SIGNATURE_DATA
*)((UINT8
*)SigDBCert
+ sizeof(EFI_SIGNATURE_LIST
));
1376 CopyGuid (&SigDBCertData
->SignatureOwner
, Private
->SignatureGUID
);
1377 CopyMem (SigDBCertData
->SignatureData
, mImageDigest
, mImageDigestSize
);
1379 Attr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
1380 | EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
1381 Status
= CreateTimeBasedPayload (&SigDBSize
, (UINT8
**) &Data
);
1382 if (EFI_ERROR (Status
)) {
1383 DEBUG ((EFI_D_ERROR
, "Fail to create time-based data payload: %r", Status
));
1388 // Check if SigDB variable has been already existed.
1389 // If true, use EFI_VARIABLE_APPEND_WRITE attribute to append the
1390 // new signature data to original variable
1393 Status
= gRT
->GetVariable(
1395 &gEfiImageSecurityDatabaseGuid
,
1400 if (Status
== EFI_BUFFER_TOO_SMALL
) {
1401 Attr
|= EFI_VARIABLE_APPEND_WRITE
;
1402 } else if (Status
!= EFI_NOT_FOUND
) {
1407 // Enroll the variable.
1409 Status
= gRT
->SetVariable(
1411 &gEfiImageSecurityDatabaseGuid
,
1416 if (EFI_ERROR (Status
)) {
1422 CloseFile (Private
->FileContext
->FHandle
);
1423 Private
->FileContext
->FHandle
= NULL
;
1424 Private
->FileContext
->FileName
= NULL
;
1426 if (Private
->SignatureGUID
!= NULL
) {
1427 FreePool (Private
->SignatureGUID
);
1428 Private
->SignatureGUID
= NULL
;
1435 if (mImageBase
!= NULL
) {
1436 FreePool (mImageBase
);
1444 Enroll signature into DB/DBX without KEK's authentication.
1445 The SignatureOwner GUID will be Private->SignatureGUID.
1447 @param[in] PrivateData The module's private data.
1448 @param[in] VariableName Variable name of signature database, must be
1449 EFI_IMAGE_SECURITY_DATABASE or EFI_IMAGE_SECURITY_DATABASE1.
1451 @retval EFI_SUCCESS New signature enrolled successfully.
1452 @retval EFI_INVALID_PARAMETER The parameter is invalid.
1453 @retval others Fail to enroll signature data.
1457 EnrollSignatureDatabase (
1458 IN SECUREBOOT_CONFIG_PRIVATE_DATA
*Private
,
1459 IN CHAR16
*VariableName
1462 UINT16
* FilePostFix
;
1464 if ((Private
->FileContext
->FileName
== NULL
) || (Private
->FileContext
->FHandle
== NULL
) || (Private
->SignatureGUID
== NULL
)) {
1465 return EFI_INVALID_PARAMETER
;
1469 // Parse the file's postfix.
1471 FilePostFix
= Private
->FileContext
->FileName
+ StrLen (Private
->FileContext
->FileName
) - 4;
1472 if ((CompareMem (FilePostFix
, L
".cer",4) == 0) || (CompareMem (FilePostFix
, L
".der",4) == 0)) {
1474 // Supports .cer and .der file as X509 certificate.
1476 return EnrollX509toSigDB (Private
, VariableName
);
1479 return EnrollImageSignatureToSigDB (Private
, VariableName
);
1483 List all signatures in specified signature database (e.g. KEK/DB/DBX)
1484 by GUID in the page for user to select and delete as needed.
1486 @param[in] PrivateData Module's private data.
1487 @param[in] VariableName The variable name of the vendor's signature database.
1488 @param[in] VendorGuid A unique identifier for the vendor.
1489 @param[in] LabelNumber Label number to insert opcodes.
1490 @param[in] FormId Form ID of current page.
1491 @param[in] QuestionIdBase Base question id of the signature list.
1493 @retval EFI_SUCCESS Success to update the signature list page
1494 @retval EFI_OUT_OF_RESOURCES Unable to allocate required resources.
1499 IN SECUREBOOT_CONFIG_PRIVATE_DATA
*PrivateData
,
1500 IN CHAR16
*VariableName
,
1501 IN EFI_GUID
*VendorGuid
,
1502 IN UINT16 LabelNumber
,
1503 IN EFI_FORM_ID FormId
,
1504 IN EFI_QUESTION_ID QuestionIdBase
1511 VOID
*StartOpCodeHandle
;
1512 VOID
*EndOpCodeHandle
;
1513 EFI_IFR_GUID_LABEL
*StartLabel
;
1514 EFI_IFR_GUID_LABEL
*EndLabel
;
1517 EFI_SIGNATURE_LIST
*CertList
;
1518 EFI_SIGNATURE_DATA
*Cert
;
1519 UINT32 ItemDataSize
;
1521 EFI_STRING_ID GuidID
;
1528 StartOpCodeHandle
= NULL
;
1529 EndOpCodeHandle
= NULL
;
1532 // Initialize the container for dynamic opcodes.
1534 StartOpCodeHandle
= HiiAllocateOpCodeHandle ();
1535 if (StartOpCodeHandle
== NULL
) {
1536 Status
= EFI_OUT_OF_RESOURCES
;
1540 EndOpCodeHandle
= HiiAllocateOpCodeHandle ();
1541 if (EndOpCodeHandle
== NULL
) {
1542 Status
= EFI_OUT_OF_RESOURCES
;
1547 // Create Hii Extend Label OpCode.
1549 StartLabel
= (EFI_IFR_GUID_LABEL
*) HiiCreateGuidOpCode (
1553 sizeof (EFI_IFR_GUID_LABEL
)
1555 StartLabel
->ExtendOpCode
= EFI_IFR_EXTEND_OP_LABEL
;
1556 StartLabel
->Number
= LabelNumber
;
1558 EndLabel
= (EFI_IFR_GUID_LABEL
*) HiiCreateGuidOpCode (
1562 sizeof (EFI_IFR_GUID_LABEL
)
1564 EndLabel
->ExtendOpCode
= EFI_IFR_EXTEND_OP_LABEL
;
1565 EndLabel
->Number
= LABEL_END
;
1571 Status
= gRT
->GetVariable (VariableName
, VendorGuid
, NULL
, &DataSize
, Data
);
1572 if (EFI_ERROR (Status
) && Status
!= EFI_BUFFER_TOO_SMALL
) {
1576 Data
= (UINT8
*) AllocateZeroPool (DataSize
);
1578 Status
= EFI_OUT_OF_RESOURCES
;
1582 Status
= gRT
->GetVariable (VariableName
, VendorGuid
, NULL
, &DataSize
, Data
);
1583 if (EFI_ERROR (Status
)) {
1587 GuidStr
= AllocateZeroPool (100);
1588 if (GuidStr
== NULL
) {
1589 Status
= EFI_OUT_OF_RESOURCES
;
1594 // Enumerate all KEK pub data.
1596 ItemDataSize
= (UINT32
) DataSize
;
1597 CertList
= (EFI_SIGNATURE_LIST
*) Data
;
1600 while ((ItemDataSize
> 0) && (ItemDataSize
>= CertList
->SignatureListSize
)) {
1602 if (CompareGuid (&CertList
->SignatureType
, &gEfiCertRsa2048Guid
)) {
1603 Help
= STRING_TOKEN (STR_CERT_TYPE_RSA2048_SHA256_GUID
);
1604 } else if (CompareGuid (&CertList
->SignatureType
, &gEfiCertX509Guid
)) {
1605 Help
= STRING_TOKEN (STR_CERT_TYPE_PCKS7_GUID
);
1606 } else if (CompareGuid (&CertList
->SignatureType
, &gEfiCertSha1Guid
)) {
1607 Help
= STRING_TOKEN (STR_CERT_TYPE_SHA1_GUID
);
1608 } else if (CompareGuid (&CertList
->SignatureType
, &gEfiCertSha256Guid
)) {
1609 Help
= STRING_TOKEN (STR_CERT_TYPE_SHA256_GUID
);
1612 // The signature type is not supported in current implementation.
1617 CertCount
= (CertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - CertList
->SignatureHeaderSize
) / CertList
->SignatureSize
;
1618 for (Index
= 0; Index
< CertCount
; Index
++) {
1619 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
1620 + sizeof (EFI_SIGNATURE_LIST
)
1621 + CertList
->SignatureHeaderSize
1622 + Index
* CertList
->SignatureSize
);
1624 // Display GUID and help
1626 GuidToString (&Cert
->SignatureOwner
, GuidStr
, 100);
1627 GuidID
= HiiSetString (PrivateData
->HiiHandle
, 0, GuidStr
, NULL
);
1628 HiiCreateCheckBoxOpCode (
1630 (EFI_QUESTION_ID
) (QuestionIdBase
+ GuidIndex
++),
1635 EFI_IFR_FLAG_CALLBACK
,
1641 ItemDataSize
-= CertList
->SignatureListSize
;
1642 CertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) CertList
+ CertList
->SignatureListSize
);
1647 PrivateData
->HiiHandle
,
1648 &gSecureBootConfigFormSetGuid
,
1654 if (StartOpCodeHandle
!= NULL
) {
1655 HiiFreeOpCodeHandle (StartOpCodeHandle
);
1658 if (EndOpCodeHandle
!= NULL
) {
1659 HiiFreeOpCodeHandle (EndOpCodeHandle
);
1666 if (GuidStr
!= NULL
) {
1674 Delete a KEK entry from KEK database.
1676 @param[in] PrivateData Module's private data.
1677 @param[in] QuestionId Question id of the KEK item to delete.
1679 @retval EFI_SUCCESS Delete kek item successfully.
1680 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
1684 DeleteKeyExchangeKey (
1685 IN SECUREBOOT_CONFIG_PRIVATE_DATA
*PrivateData
,
1686 IN EFI_QUESTION_ID QuestionId
1695 EFI_SIGNATURE_LIST
*CertList
;
1696 EFI_SIGNATURE_LIST
*NewCertList
;
1697 EFI_SIGNATURE_DATA
*Cert
;
1700 BOOLEAN IsKEKItemFound
;
1702 UINTN DeleteKekIndex
;
1710 DeleteKekIndex
= QuestionId
- OPTION_DEL_KEK_QUESTION_ID
;
1713 // Get original KEK variable.
1716 Status
= gRT
->GetVariable (EFI_KEY_EXCHANGE_KEY_NAME
, &gEfiGlobalVariableGuid
, NULL
, &DataSize
, NULL
);
1717 if (EFI_ERROR(Status
) && Status
!= EFI_BUFFER_TOO_SMALL
) {
1721 OldData
= (UINT8
*)AllocateZeroPool(DataSize
);
1722 if (OldData
== NULL
) {
1723 Status
= EFI_OUT_OF_RESOURCES
;
1727 Status
= gRT
->GetVariable (EFI_KEY_EXCHANGE_KEY_NAME
, &gEfiGlobalVariableGuid
, &Attr
, &DataSize
, OldData
);
1728 if (EFI_ERROR(Status
)) {
1733 // Allocate space for new variable.
1735 Data
= (UINT8
*) AllocateZeroPool (DataSize
);
1737 Status
= EFI_OUT_OF_RESOURCES
;
1742 // Enumerate all KEK pub data and erasing the target item.
1744 IsKEKItemFound
= FALSE
;
1745 KekDataSize
= (UINT32
) DataSize
;
1746 CertList
= (EFI_SIGNATURE_LIST
*) OldData
;
1749 while ((KekDataSize
> 0) && (KekDataSize
>= CertList
->SignatureListSize
)) {
1750 if (CompareGuid (&CertList
->SignatureType
, &gEfiCertRsa2048Guid
) ||
1751 CompareGuid (&CertList
->SignatureType
, &gEfiCertX509Guid
)) {
1752 CopyMem (Data
+ Offset
, CertList
, (sizeof(EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
));
1753 NewCertList
= (EFI_SIGNATURE_LIST
*)(Data
+ Offset
);
1754 Offset
+= (sizeof(EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
1755 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
1756 CertCount
= (CertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - CertList
->SignatureHeaderSize
) / CertList
->SignatureSize
;
1757 for (Index
= 0; Index
< CertCount
; Index
++) {
1758 if (GuidIndex
== DeleteKekIndex
) {
1760 // Find it! Skip it!
1762 NewCertList
->SignatureListSize
-= CertList
->SignatureSize
;
1763 IsKEKItemFound
= TRUE
;
1766 // This item doesn't match. Copy it to the Data buffer.
1768 CopyMem (Data
+ Offset
, Cert
, CertList
->SignatureSize
);
1769 Offset
+= CertList
->SignatureSize
;
1772 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) Cert
+ CertList
->SignatureSize
);
1776 // This List doesn't match. Copy it to the Data buffer.
1778 CopyMem (Data
+ Offset
, CertList
, CertList
->SignatureListSize
);
1779 Offset
+= CertList
->SignatureListSize
;
1782 KekDataSize
-= CertList
->SignatureListSize
;
1783 CertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) CertList
+ CertList
->SignatureListSize
);
1786 if (!IsKEKItemFound
) {
1788 // Doesn't find the Kek Item!
1790 Status
= EFI_NOT_FOUND
;
1795 // Delete the Signature header if there is no signature in the list.
1797 KekDataSize
= Offset
;
1798 CertList
= (EFI_SIGNATURE_LIST
*) Data
;
1800 ZeroMem (OldData
, KekDataSize
);
1801 while ((KekDataSize
> 0) && (KekDataSize
>= CertList
->SignatureListSize
)) {
1802 CertCount
= (CertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - CertList
->SignatureHeaderSize
) / CertList
->SignatureSize
;
1803 DEBUG ((DEBUG_ERROR
, " CertCount = %x\n", CertCount
));
1804 if (CertCount
!= 0) {
1805 CopyMem (OldData
+ Offset
, CertList
, CertList
->SignatureListSize
);
1806 Offset
+= CertList
->SignatureListSize
;
1808 KekDataSize
-= CertList
->SignatureListSize
;
1809 CertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) CertList
+ CertList
->SignatureListSize
);
1813 if ((Attr
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) {
1814 Status
= CreateTimeBasedPayload (&DataSize
, &OldData
);
1815 if (EFI_ERROR (Status
)) {
1816 DEBUG ((EFI_D_ERROR
, "Fail to create time-based data payload: %r", Status
));
1821 Status
= gRT
->SetVariable(
1822 EFI_KEY_EXCHANGE_KEY_NAME
,
1823 &gEfiGlobalVariableGuid
,
1828 if (EFI_ERROR (Status
)) {
1829 DEBUG ((DEBUG_ERROR
, "Failed to set variable, Status = %r\n", Status
));
1838 if (OldData
!= NULL
) {
1842 return UpdateDeletePage (
1844 EFI_KEY_EXCHANGE_KEY_NAME
,
1845 &gEfiGlobalVariableGuid
,
1847 FORMID_DELETE_KEK_FORM
,
1848 OPTION_DEL_KEK_QUESTION_ID
1853 Delete a signature entry from siganture database.
1855 @param[in] PrivateData Module's private data.
1856 @param[in] VariableName The variable name of the vendor's signature database.
1857 @param[in] VendorGuid A unique identifier for the vendor.
1858 @param[in] LabelNumber Label number to insert opcodes.
1859 @param[in] FormId Form ID of current page.
1860 @param[in] QuestionIdBase Base question id of the signature list.
1861 @param[in] DeleteIndex Signature index to delete.
1863 @retval EFI_SUCCESS Delete siganture successfully.
1864 @retval EFI_NOT_FOUND Can't find the signature item,
1865 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
1869 IN SECUREBOOT_CONFIG_PRIVATE_DATA
*PrivateData
,
1870 IN CHAR16
*VariableName
,
1871 IN EFI_GUID
*VendorGuid
,
1872 IN UINT16 LabelNumber
,
1873 IN EFI_FORM_ID FormId
,
1874 IN EFI_QUESTION_ID QuestionIdBase
,
1875 IN UINTN DeleteIndex
1884 EFI_SIGNATURE_LIST
*CertList
;
1885 EFI_SIGNATURE_LIST
*NewCertList
;
1886 EFI_SIGNATURE_DATA
*Cert
;
1889 BOOLEAN IsItemFound
;
1890 UINT32 ItemDataSize
;
1900 // Get original signature list data.
1903 Status
= gRT
->GetVariable (VariableName
, VendorGuid
, NULL
, &DataSize
, NULL
);
1904 if (EFI_ERROR (Status
) && Status
!= EFI_BUFFER_TOO_SMALL
) {
1908 OldData
= (UINT8
*) AllocateZeroPool (DataSize
);
1909 if (OldData
== NULL
) {
1910 Status
= EFI_OUT_OF_RESOURCES
;
1914 Status
= gRT
->GetVariable (VariableName
, VendorGuid
, &Attr
, &DataSize
, OldData
);
1915 if (EFI_ERROR(Status
)) {
1920 // Allocate space for new variable.
1922 Data
= (UINT8
*) AllocateZeroPool (DataSize
);
1924 Status
= EFI_OUT_OF_RESOURCES
;
1929 // Enumerate all signature data and erasing the target item.
1931 IsItemFound
= FALSE
;
1932 ItemDataSize
= (UINT32
) DataSize
;
1933 CertList
= (EFI_SIGNATURE_LIST
*) OldData
;
1936 while ((ItemDataSize
> 0) && (ItemDataSize
>= CertList
->SignatureListSize
)) {
1937 if (CompareGuid (&CertList
->SignatureType
, &gEfiCertRsa2048Guid
) ||
1938 CompareGuid (&CertList
->SignatureType
, &gEfiCertX509Guid
) ||
1939 CompareGuid (&CertList
->SignatureType
, &gEfiCertSha1Guid
) ||
1940 CompareGuid (&CertList
->SignatureType
, &gEfiCertSha256Guid
)
1943 // Copy EFI_SIGNATURE_LIST header then calculate the signature count in this list.
1945 CopyMem (Data
+ Offset
, CertList
, (sizeof(EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
));
1946 NewCertList
= (EFI_SIGNATURE_LIST
*) (Data
+ Offset
);
1947 Offset
+= (sizeof(EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
1948 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
1949 CertCount
= (CertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - CertList
->SignatureHeaderSize
) / CertList
->SignatureSize
;
1950 for (Index
= 0; Index
< CertCount
; Index
++) {
1951 if (GuidIndex
== DeleteIndex
) {
1953 // Find it! Skip it!
1955 NewCertList
->SignatureListSize
-= CertList
->SignatureSize
;
1959 // This item doesn't match. Copy it to the Data buffer.
1961 CopyMem (Data
+ Offset
, (UINT8
*)(Cert
), CertList
->SignatureSize
);
1962 Offset
+= CertList
->SignatureSize
;
1965 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) Cert
+ CertList
->SignatureSize
);
1969 // This List doesn't match. Just copy it to the Data buffer.
1971 CopyMem (Data
+ Offset
, (UINT8
*)(CertList
), CertList
->SignatureListSize
);
1972 Offset
+= CertList
->SignatureListSize
;
1975 ItemDataSize
-= CertList
->SignatureListSize
;
1976 CertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) CertList
+ CertList
->SignatureListSize
);
1981 // Doesn't find the signature Item!
1983 Status
= EFI_NOT_FOUND
;
1988 // Delete the EFI_SIGNATURE_LIST header if there is no signature in the list.
1990 ItemDataSize
= Offset
;
1991 CertList
= (EFI_SIGNATURE_LIST
*) Data
;
1993 ZeroMem (OldData
, ItemDataSize
);
1994 while ((ItemDataSize
> 0) && (ItemDataSize
>= CertList
->SignatureListSize
)) {
1995 CertCount
= (CertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - CertList
->SignatureHeaderSize
) / CertList
->SignatureSize
;
1996 DEBUG ((DEBUG_ERROR
, " CertCount = %x\n", CertCount
));
1997 if (CertCount
!= 0) {
1998 CopyMem (OldData
+ Offset
, (UINT8
*)(CertList
), CertList
->SignatureListSize
);
1999 Offset
+= CertList
->SignatureListSize
;
2001 ItemDataSize
-= CertList
->SignatureListSize
;
2002 CertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) CertList
+ CertList
->SignatureListSize
);
2006 if ((Attr
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) {
2007 Status
= CreateTimeBasedPayload (&DataSize
, &OldData
);
2008 if (EFI_ERROR (Status
)) {
2009 DEBUG ((EFI_D_ERROR
, "Fail to create time-based data payload: %r", Status
));
2014 Status
= gRT
->SetVariable(
2021 if (EFI_ERROR (Status
)) {
2022 DEBUG ((DEBUG_ERROR
, "Failed to set variable, Status = %r\n", Status
));
2031 if (OldData
!= NULL
) {
2035 return UpdateDeletePage (
2046 This function extracts configuration from variable.
2048 @param[in, out] ConfigData Point to SecureBoot configuration private data.
2052 SecureBootExtractConfigFromVariable (
2053 IN OUT SECUREBOOT_CONFIGURATION
*ConfigData
2056 UINT8
*SecureBootEnable
;
2058 UINT8
*SecureBootMode
;
2060 SecureBootEnable
= NULL
;
2062 SecureBootMode
= NULL
;
2065 // Get the SecureBootEnable Variable
2067 GetVariable2 (EFI_SECURE_BOOT_ENABLE_NAME
, &gEfiSecureBootEnableDisableGuid
, &SecureBootEnable
, NULL
);
2070 // If the SecureBootEnable Variable doesn't exist, hide the SecureBoot Enable/Disable
2073 if (SecureBootEnable
== NULL
) {
2074 ConfigData
->HideSecureBoot
= TRUE
;
2076 ConfigData
->HideSecureBoot
= FALSE
;
2077 ConfigData
->SecureBootState
= *SecureBootEnable
;
2080 // If it is Physical Presence User, set the PhysicalPresent to true.
2082 if (UserPhysicalPresent()) {
2083 ConfigData
->PhysicalPresent
= TRUE
;
2085 ConfigData
->PhysicalPresent
= FALSE
;
2089 // If there is no PK then the Delete Pk button will be gray.
2091 GetVariable2 (EFI_SETUP_MODE_NAME
, &gEfiGlobalVariableGuid
, &SetupMode
, NULL
);
2092 if (SetupMode
== NULL
|| (*SetupMode
) == 1) {
2093 ConfigData
->HasPk
= FALSE
;
2095 ConfigData
->HasPk
= TRUE
;
2099 // Get the SecureBootMode from CustomMode variable.
2101 GetVariable2 (EFI_CUSTOM_MODE_NAME
, &gEfiCustomModeEnableGuid
, &SecureBootMode
, NULL
);
2102 if (SecureBootMode
== NULL
) {
2103 ConfigData
->SecureBootMode
= STANDARD_SECURE_BOOT_MODE
;
2105 ConfigData
->SecureBootMode
= *(SecureBootMode
);
2111 This function allows a caller to extract the current configuration for one
2112 or more named elements from the target driver.
2114 @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
2115 @param[in] Request A null-terminated Unicode string in
2116 <ConfigRequest> format.
2117 @param[out] Progress On return, points to a character in the Request
2118 string. Points to the string's null terminator if
2119 request was successful. Points to the most recent
2120 '&' before the first failing name/value pair (or
2121 the beginning of the string if the failure is in
2122 the first name/value pair) if the request was not
2124 @param[out] Results A null-terminated Unicode string in
2125 <ConfigAltResp> format which has all values filled
2126 in for the names in the Request string. String to
2127 be allocated by the called function.
2129 @retval EFI_SUCCESS The Results is filled with the requested values.
2130 @retval EFI_OUT_OF_RESOURCES Not enough memory to store the results.
2131 @retval EFI_INVALID_PARAMETER Request is illegal syntax, or unknown name.
2132 @retval EFI_NOT_FOUND Routing data doesn't match any storage in this
2138 SecureBootExtractConfig (
2139 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL
*This
,
2140 IN CONST EFI_STRING Request
,
2141 OUT EFI_STRING
*Progress
,
2142 OUT EFI_STRING
*Results
2148 SECUREBOOT_CONFIGURATION Configuration
;
2149 EFI_STRING ConfigRequest
;
2150 EFI_STRING ConfigRequestHdr
;
2151 SECUREBOOT_CONFIG_PRIVATE_DATA
*PrivateData
;
2152 BOOLEAN AllocatedRequest
;
2154 if (Progress
== NULL
|| Results
== NULL
) {
2155 return EFI_INVALID_PARAMETER
;
2158 AllocatedRequest
= FALSE
;
2159 ConfigRequestHdr
= NULL
;
2160 ConfigRequest
= NULL
;
2163 ZeroMem (&Configuration
, sizeof (Configuration
));
2164 PrivateData
= SECUREBOOT_CONFIG_PRIVATE_FROM_THIS (This
);
2165 *Progress
= Request
;
2167 if ((Request
!= NULL
) && !HiiIsConfigHdrMatch (Request
, &gSecureBootConfigFormSetGuid
, mSecureBootStorageName
)) {
2168 return EFI_NOT_FOUND
;
2172 // Get Configuration from Variable.
2174 SecureBootExtractConfigFromVariable (&Configuration
);
2176 BufferSize
= sizeof (SECUREBOOT_CONFIGURATION
);
2177 ConfigRequest
= Request
;
2178 if ((Request
== NULL
) || (StrStr (Request
, L
"OFFSET") == NULL
)) {
2180 // Request is set to NULL or OFFSET is NULL, construct full request string.
2182 // Allocate and fill a buffer large enough to hold the <ConfigHdr> template
2183 // followed by "&OFFSET=0&WIDTH=WWWWWWWWWWWWWWWW" followed by a Null-terminator
2185 ConfigRequestHdr
= HiiConstructConfigHdr (&gSecureBootConfigFormSetGuid
, mSecureBootStorageName
, PrivateData
->DriverHandle
);
2186 Size
= (StrLen (ConfigRequestHdr
) + 32 + 1) * sizeof (CHAR16
);
2187 ConfigRequest
= AllocateZeroPool (Size
);
2188 ASSERT (ConfigRequest
!= NULL
);
2189 AllocatedRequest
= TRUE
;
2190 UnicodeSPrint (ConfigRequest
, Size
, L
"%s&OFFSET=0&WIDTH=%016LX", ConfigRequestHdr
, (UINT64
)BufferSize
);
2191 FreePool (ConfigRequestHdr
);
2192 ConfigRequestHdr
= NULL
;
2195 Status
= gHiiConfigRouting
->BlockToConfig (
2198 (UINT8
*) &Configuration
,
2205 // Free the allocated config request string.
2207 if (AllocatedRequest
) {
2208 FreePool (ConfigRequest
);
2212 // Set Progress string to the original request string.
2214 if (Request
== NULL
) {
2216 } else if (StrStr (Request
, L
"OFFSET") == NULL
) {
2217 *Progress
= Request
+ StrLen (Request
);
2224 This function processes the results of changes in configuration.
2226 @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
2227 @param[in] Configuration A null-terminated Unicode string in <ConfigResp>
2229 @param[out] Progress A pointer to a string filled in with the offset of
2230 the most recent '&' before the first failing
2231 name/value pair (or the beginning of the string if
2232 the failure is in the first name/value pair) or
2233 the terminating NULL if all was successful.
2235 @retval EFI_SUCCESS The Results is processed successfully.
2236 @retval EFI_INVALID_PARAMETER Configuration is NULL.
2237 @retval EFI_NOT_FOUND Routing data doesn't match any storage in this
2243 SecureBootRouteConfig (
2244 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL
*This
,
2245 IN CONST EFI_STRING Configuration
,
2246 OUT EFI_STRING
*Progress
2249 if (Configuration
== NULL
|| Progress
== NULL
) {
2250 return EFI_INVALID_PARAMETER
;
2253 *Progress
= Configuration
;
2254 if (!HiiIsConfigHdrMatch (Configuration
, &gSecureBootConfigFormSetGuid
, mSecureBootStorageName
)) {
2255 return EFI_NOT_FOUND
;
2258 *Progress
= Configuration
+ StrLen (Configuration
);
2263 This function is called to provide results data to the driver.
2265 @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
2266 @param[in] Action Specifies the type of action taken by the browser.
2267 @param[in] QuestionId A unique value which is sent to the original
2268 exporting driver so that it can identify the type
2270 @param[in] Type The type of value for the question.
2271 @param[in] Value A pointer to the data being sent to the original
2273 @param[out] ActionRequest On return, points to the action requested by the
2276 @retval EFI_SUCCESS The callback successfully handled the action.
2277 @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the
2278 variable and its data.
2279 @retval EFI_DEVICE_ERROR The variable could not be saved.
2280 @retval EFI_UNSUPPORTED The specified Action is not supported by the
2286 SecureBootCallback (
2287 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL
*This
,
2288 IN EFI_BROWSER_ACTION Action
,
2289 IN EFI_QUESTION_ID QuestionId
,
2291 IN EFI_IFR_TYPE_VALUE
*Value
,
2292 OUT EFI_BROWSER_ACTION_REQUEST
*ActionRequest
2297 SECUREBOOT_CONFIG_PRIVATE_DATA
*Private
;
2299 SECUREBOOT_CONFIGURATION
*IfrNvData
;
2301 UINT8
*SecureBootEnable
;
2303 SecureBootEnable
= NULL
;
2305 if ((This
== NULL
) || (Value
== NULL
) || (ActionRequest
== NULL
)) {
2306 return EFI_INVALID_PARAMETER
;
2309 if ((Action
!= EFI_BROWSER_ACTION_CHANGED
) && (Action
!= EFI_BROWSER_ACTION_CHANGING
)) {
2310 return EFI_UNSUPPORTED
;
2313 Private
= SECUREBOOT_CONFIG_PRIVATE_FROM_THIS (This
);
2316 // Retrieve uncommitted data from Browser
2318 BufferSize
= sizeof (SECUREBOOT_CONFIGURATION
);
2319 IfrNvData
= AllocateZeroPool (BufferSize
);
2320 if (IfrNvData
== NULL
) {
2321 return EFI_OUT_OF_RESOURCES
;
2324 Status
= EFI_SUCCESS
;
2326 HiiGetBrowserData (NULL
, NULL
, BufferSize
, (UINT8
*) IfrNvData
);
2328 if (Action
== EFI_BROWSER_ACTION_CHANGING
) {
2330 switch (QuestionId
) {
2331 case KEY_SECURE_BOOT_ENABLE
:
2332 GetVariable2 (EFI_SECURE_BOOT_ENABLE_NAME
, &gEfiSecureBootEnableDisableGuid
, &SecureBootEnable
, NULL
);
2333 if (NULL
!= SecureBootEnable
) {
2334 if (EFI_ERROR (SaveSecureBootVariable (Value
->u8
))) {
2336 EFI_LIGHTGRAY
| EFI_BACKGROUND_BLUE
,
2338 L
"Only Physical Presence User could disable secure boot!",
2341 Status
= EFI_UNSUPPORTED
;
2343 *ActionRequest
= EFI_BROWSER_ACTION_REQUEST_FORM_APPLY
;
2347 case KEY_SECURE_BOOT_OPTION
:
2348 FreeMenu (&DirectoryMenu
);
2349 FreeMenu (&FsOptionMenu
);
2352 case KEY_SECURE_BOOT_KEK_OPTION
:
2353 case KEY_SECURE_BOOT_DB_OPTION
:
2354 case KEY_SECURE_BOOT_DBX_OPTION
:
2356 // Clear Signature GUID.
2358 ZeroMem (IfrNvData
->SignatureGuid
, sizeof (IfrNvData
->SignatureGuid
));
2359 if (Private
->SignatureGUID
== NULL
) {
2360 Private
->SignatureGUID
= (EFI_GUID
*) AllocateZeroPool (sizeof (EFI_GUID
));
2361 if (Private
->SignatureGUID
== NULL
) {
2362 return EFI_OUT_OF_RESOURCES
;
2366 if (QuestionId
== KEY_SECURE_BOOT_DB_OPTION
) {
2367 LabelId
= SECUREBOOT_ENROLL_SIGNATURE_TO_DB
;
2368 } else if (QuestionId
== KEY_SECURE_BOOT_DBX_OPTION
) {
2369 LabelId
= SECUREBOOT_ENROLL_SIGNATURE_TO_DBX
;
2371 LabelId
= FORMID_ENROLL_KEK_FORM
;
2375 // Refresh selected file.
2377 CleanUpPage (LabelId
, Private
);
2380 case SECUREBOOT_ADD_PK_FILE_FORM_ID
:
2381 case FORMID_ENROLL_KEK_FORM
:
2382 case SECUREBOOT_ENROLL_SIGNATURE_TO_DB
:
2383 case SECUREBOOT_ENROLL_SIGNATURE_TO_DBX
:
2384 if (QuestionId
== SECUREBOOT_ADD_PK_FILE_FORM_ID
) {
2385 Private
->FeCurrentState
= FileExplorerStateEnrollPkFile
;
2386 } else if (QuestionId
== FORMID_ENROLL_KEK_FORM
) {
2387 Private
->FeCurrentState
= FileExplorerStateEnrollKekFile
;
2388 } else if (QuestionId
== SECUREBOOT_ENROLL_SIGNATURE_TO_DB
) {
2389 Private
->FeCurrentState
= FileExplorerStateEnrollSignatureFileToDb
;
2391 Private
->FeCurrentState
= FileExplorerStateEnrollSignatureFileToDbx
;
2394 Private
->FeDisplayContext
= FileExplorerDisplayUnknown
;
2395 CleanUpPage (FORM_FILE_EXPLORER_ID
, Private
);
2396 UpdateFileExplorer (Private
, 0);
2399 case KEY_SECURE_BOOT_DELETE_PK
:
2401 Status
= DeletePlatformKey ();
2402 *ActionRequest
= EFI_BROWSER_ACTION_REQUEST_FORM_APPLY
;
2406 case KEY_DELETE_KEK
:
2409 EFI_KEY_EXCHANGE_KEY_NAME
,
2410 &gEfiGlobalVariableGuid
,
2412 FORMID_DELETE_KEK_FORM
,
2413 OPTION_DEL_KEK_QUESTION_ID
2417 case SECUREBOOT_DELETE_SIGNATURE_FROM_DB
:
2420 EFI_IMAGE_SECURITY_DATABASE
,
2421 &gEfiImageSecurityDatabaseGuid
,
2423 SECUREBOOT_DELETE_SIGNATURE_FROM_DB
,
2424 OPTION_DEL_DB_QUESTION_ID
2428 case SECUREBOOT_DELETE_SIGNATURE_FROM_DBX
:
2431 EFI_IMAGE_SECURITY_DATABASE1
,
2432 &gEfiImageSecurityDatabaseGuid
,
2434 SECUREBOOT_DELETE_SIGNATURE_FROM_DBX
,
2435 OPTION_DEL_DBX_QUESTION_ID
2440 case KEY_VALUE_SAVE_AND_EXIT_KEK
:
2441 Status
= EnrollKeyExchangeKey (Private
);
2444 case KEY_VALUE_SAVE_AND_EXIT_DB
:
2445 Status
= EnrollSignatureDatabase (Private
, EFI_IMAGE_SECURITY_DATABASE
);
2448 case KEY_VALUE_SAVE_AND_EXIT_DBX
:
2449 Status
= EnrollSignatureDatabase (Private
, EFI_IMAGE_SECURITY_DATABASE1
);
2453 if (QuestionId
>= FILE_OPTION_OFFSET
) {
2454 UpdateFileExplorer (Private
, QuestionId
);
2455 } else if ((QuestionId
>= OPTION_DEL_KEK_QUESTION_ID
) &&
2456 (QuestionId
< (OPTION_DEL_KEK_QUESTION_ID
+ OPTION_CONFIG_RANGE
))) {
2457 DeleteKeyExchangeKey (Private
, QuestionId
);
2458 } else if ((QuestionId
>= OPTION_DEL_DB_QUESTION_ID
) &&
2459 (QuestionId
< (OPTION_DEL_DB_QUESTION_ID
+ OPTION_CONFIG_RANGE
))) {
2462 EFI_IMAGE_SECURITY_DATABASE
,
2463 &gEfiImageSecurityDatabaseGuid
,
2465 SECUREBOOT_DELETE_SIGNATURE_FROM_DB
,
2466 OPTION_DEL_DB_QUESTION_ID
,
2467 QuestionId
- OPTION_DEL_DB_QUESTION_ID
2469 } else if ((QuestionId
>= OPTION_DEL_DBX_QUESTION_ID
) &&
2470 (QuestionId
< (OPTION_DEL_DBX_QUESTION_ID
+ OPTION_CONFIG_RANGE
))) {
2473 EFI_IMAGE_SECURITY_DATABASE1
,
2474 &gEfiImageSecurityDatabaseGuid
,
2476 SECUREBOOT_DELETE_SIGNATURE_FROM_DBX
,
2477 OPTION_DEL_DBX_QUESTION_ID
,
2478 QuestionId
- OPTION_DEL_DBX_QUESTION_ID
2483 } else if (Action
== EFI_BROWSER_ACTION_CHANGED
) {
2484 switch (QuestionId
) {
2485 case KEY_SECURE_BOOT_ENABLE
:
2486 *ActionRequest
= EFI_BROWSER_ACTION_REQUEST_SUBMIT
;
2488 case KEY_VALUE_SAVE_AND_EXIT_PK
:
2489 Status
= EnrollPlatformKey (Private
);
2490 if (EFI_ERROR (Status
)) {
2492 EFI_LIGHTGRAY
| EFI_BACKGROUND_BLUE
,
2494 L
"ERROR: Unsupported file type, only *.cer is supported!",
2498 *ActionRequest
= EFI_BROWSER_ACTION_REQUEST_EXIT
;
2502 case KEY_VALUE_NO_SAVE_AND_EXIT_PK
:
2503 case KEY_VALUE_NO_SAVE_AND_EXIT_KEK
:
2504 case KEY_VALUE_NO_SAVE_AND_EXIT_DB
:
2505 case KEY_VALUE_NO_SAVE_AND_EXIT_DBX
:
2506 if (Private
->FileContext
->FHandle
!= NULL
) {
2507 CloseFile (Private
->FileContext
->FHandle
);
2508 Private
->FileContext
->FHandle
= NULL
;
2509 Private
->FileContext
->FileName
= NULL
;
2512 if (Private
->SignatureGUID
!= NULL
) {
2513 FreePool (Private
->SignatureGUID
);
2514 Private
->SignatureGUID
= NULL
;
2516 *ActionRequest
= EFI_BROWSER_ACTION_REQUEST_EXIT
;
2519 case KEY_SECURE_BOOT_MODE
:
2520 GetVariable2 (EFI_CUSTOM_MODE_NAME
, &gEfiCustomModeEnableGuid
, &SecureBootEnable
, NULL
);
2521 if (NULL
!= SecureBootEnable
) {
2522 Status
= gRT
->SetVariable (
2523 EFI_CUSTOM_MODE_NAME
,
2524 &gEfiCustomModeEnableGuid
,
2525 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
2529 *ActionRequest
= EFI_BROWSER_ACTION_REQUEST_FORM_APPLY
;
2530 IfrNvData
->SecureBootMode
= Value
->u8
;
2534 case KEY_SECURE_BOOT_KEK_GUID
:
2535 case KEY_SECURE_BOOT_SIGNATURE_GUID_DB
:
2536 case KEY_SECURE_BOOT_SIGNATURE_GUID_DBX
:
2537 ASSERT (Private
->SignatureGUID
!= NULL
);
2538 Status
= StringToGuid (
2539 IfrNvData
->SignatureGuid
,
2540 StrLen (IfrNvData
->SignatureGuid
),
2541 Private
->SignatureGUID
2543 if (EFI_ERROR (Status
)) {
2547 *ActionRequest
= EFI_BROWSER_ACTION_REQUEST_FORM_APPLY
;
2550 case KEY_SECURE_BOOT_DELETE_PK
:
2552 *ActionRequest
= EFI_BROWSER_ACTION_REQUEST_SUBMIT
;
2558 if (!EFI_ERROR (Status
)) {
2559 BufferSize
= sizeof (SECUREBOOT_CONFIGURATION
);
2560 HiiSetBrowserData (NULL
, NULL
, BufferSize
, (UINT8
*) IfrNvData
, NULL
);
2562 FreePool (IfrNvData
);
2568 This function publish the SecureBoot configuration Form.
2570 @param[in, out] PrivateData Points to SecureBoot configuration private data.
2572 @retval EFI_SUCCESS HII Form is installed successfully.
2573 @retval EFI_OUT_OF_RESOURCES Not enough resource for HII Form installation.
2574 @retval Others Other errors as indicated.
2578 InstallSecureBootConfigForm (
2579 IN OUT SECUREBOOT_CONFIG_PRIVATE_DATA
*PrivateData
2583 EFI_HII_HANDLE HiiHandle
;
2584 EFI_HANDLE DriverHandle
;
2585 EFI_HII_CONFIG_ACCESS_PROTOCOL
*ConfigAccess
;
2587 DriverHandle
= NULL
;
2588 ConfigAccess
= &PrivateData
->ConfigAccess
;
2589 Status
= gBS
->InstallMultipleProtocolInterfaces (
2591 &gEfiDevicePathProtocolGuid
,
2592 &mSecureBootHiiVendorDevicePath
,
2593 &gEfiHiiConfigAccessProtocolGuid
,
2597 if (EFI_ERROR (Status
)) {
2601 PrivateData
->DriverHandle
= DriverHandle
;
2604 // Publish the HII package list
2606 HiiHandle
= HiiAddPackages (
2607 &gSecureBootConfigFormSetGuid
,
2609 SecureBootConfigDxeStrings
,
2610 SecureBootConfigBin
,
2613 if (HiiHandle
== NULL
) {
2614 gBS
->UninstallMultipleProtocolInterfaces (
2616 &gEfiDevicePathProtocolGuid
,
2617 &mSecureBootHiiVendorDevicePath
,
2618 &gEfiHiiConfigAccessProtocolGuid
,
2622 return EFI_OUT_OF_RESOURCES
;
2625 PrivateData
->HiiHandle
= HiiHandle
;
2627 PrivateData
->FileContext
= AllocateZeroPool (sizeof (SECUREBOOT_FILE_CONTEXT
));
2628 PrivateData
->MenuEntry
= AllocateZeroPool (sizeof (SECUREBOOT_MENU_ENTRY
));
2630 if (PrivateData
->FileContext
== NULL
|| PrivateData
->MenuEntry
== NULL
) {
2631 UninstallSecureBootConfigForm (PrivateData
);
2632 return EFI_OUT_OF_RESOURCES
;
2635 PrivateData
->FeCurrentState
= FileExplorerStateInActive
;
2636 PrivateData
->FeDisplayContext
= FileExplorerDisplayUnknown
;
2638 InitializeListHead (&FsOptionMenu
.Head
);
2639 InitializeListHead (&DirectoryMenu
.Head
);
2642 // Init OpCode Handle and Allocate space for creation of Buffer
2644 mStartOpCodeHandle
= HiiAllocateOpCodeHandle ();
2645 if (mStartOpCodeHandle
== NULL
) {
2646 UninstallSecureBootConfigForm (PrivateData
);
2647 return EFI_OUT_OF_RESOURCES
;
2650 mEndOpCodeHandle
= HiiAllocateOpCodeHandle ();
2651 if (mEndOpCodeHandle
== NULL
) {
2652 UninstallSecureBootConfigForm (PrivateData
);
2653 return EFI_OUT_OF_RESOURCES
;
2657 // Create Hii Extend Label OpCode as the start opcode
2659 mStartLabel
= (EFI_IFR_GUID_LABEL
*) HiiCreateGuidOpCode (
2663 sizeof (EFI_IFR_GUID_LABEL
)
2665 mStartLabel
->ExtendOpCode
= EFI_IFR_EXTEND_OP_LABEL
;
2668 // Create Hii Extend Label OpCode as the end opcode
2670 mEndLabel
= (EFI_IFR_GUID_LABEL
*) HiiCreateGuidOpCode (
2674 sizeof (EFI_IFR_GUID_LABEL
)
2676 mEndLabel
->ExtendOpCode
= EFI_IFR_EXTEND_OP_LABEL
;
2677 mEndLabel
->Number
= LABEL_END
;
2683 This function removes SecureBoot configuration Form.
2685 @param[in, out] PrivateData Points to SecureBoot configuration private data.
2689 UninstallSecureBootConfigForm (
2690 IN OUT SECUREBOOT_CONFIG_PRIVATE_DATA
*PrivateData
2694 // Uninstall HII package list
2696 if (PrivateData
->HiiHandle
!= NULL
) {
2697 HiiRemovePackages (PrivateData
->HiiHandle
);
2698 PrivateData
->HiiHandle
= NULL
;
2702 // Uninstall HII Config Access Protocol
2704 if (PrivateData
->DriverHandle
!= NULL
) {
2705 gBS
->UninstallMultipleProtocolInterfaces (
2706 PrivateData
->DriverHandle
,
2707 &gEfiDevicePathProtocolGuid
,
2708 &mSecureBootHiiVendorDevicePath
,
2709 &gEfiHiiConfigAccessProtocolGuid
,
2710 &PrivateData
->ConfigAccess
,
2713 PrivateData
->DriverHandle
= NULL
;
2716 if (PrivateData
->SignatureGUID
!= NULL
) {
2717 FreePool (PrivateData
->SignatureGUID
);
2720 if (PrivateData
->MenuEntry
!= NULL
) {
2721 FreePool (PrivateData
->MenuEntry
);
2724 if (PrivateData
->FileContext
!= NULL
) {
2725 FreePool (PrivateData
->FileContext
);
2728 FreePool (PrivateData
);
2730 FreeMenu (&DirectoryMenu
);
2731 FreeMenu (&FsOptionMenu
);
2733 if (mStartOpCodeHandle
!= NULL
) {
2734 HiiFreeOpCodeHandle (mStartOpCodeHandle
);
2737 if (mEndOpCodeHandle
!= NULL
) {
2738 HiiFreeOpCodeHandle (mEndOpCodeHandle
);