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
74 UINT32 mPeCoffHeaderOffset
= 0;
75 WIN_CERTIFICATE
*mCertificate
= NULL
;
76 IMAGE_TYPE mImageType
;
77 UINT8
*mImageBase
= NULL
;
79 UINT8 mImageDigest
[MAX_DIGEST_SIZE
];
80 UINTN mImageDigestSize
;
82 EFI_IMAGE_SECURITY_DATA_DIRECTORY
*mSecDataDir
= NULL
;
83 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION mNtHeader
;
86 // Possible DER-encoded certificate file suffixes, end with NULL pointer.
88 CHAR16
* mDerEncodedSuffix
[] = {
94 CHAR16
* mSupportX509Suffix
= L
"*.cer/der/crt";
97 This code checks if the FileSuffix is one of the possible DER-encoded certificate suffix.
99 @param[in] FileSuffix The suffix of the input certificate file
101 @retval TRUE It's a DER-encoded certificate.
102 @retval FALSE It's NOT a DER-encoded certificate.
106 IsDerEncodeCertificate (
107 IN CONST CHAR16
*FileSuffix
111 for (Index
= 0; mDerEncodedSuffix
[Index
] != NULL
; Index
++) {
112 if (StrCmp (FileSuffix
, mDerEncodedSuffix
[Index
]) == 0) {
120 Set Secure Boot option into variable space.
122 @param[in] VarValue The option of Secure Boot.
124 @retval EFI_SUCCESS The operation is finished successfully.
125 @retval Others Other errors as indicated.
129 SaveSecureBootVariable (
135 Status
= gRT
->SetVariable (
136 EFI_SECURE_BOOT_ENABLE_NAME
,
137 &gEfiSecureBootEnableDisableGuid
,
138 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
146 Create a time based data payload by concatenating the EFI_VARIABLE_AUTHENTICATION_2
147 descriptor with the input data. NO authentication is required in this function.
149 @param[in, out] DataSize On input, the size of Data buffer in bytes.
150 On output, the size of data returned in Data
152 @param[in, out] Data On input, Pointer to data buffer to be wrapped or
153 pointer to NULL to wrap an empty payload.
154 On output, Pointer to the new payload date buffer allocated from pool,
155 it's caller's responsibility to free the memory when finish using it.
157 @retval EFI_SUCCESS Create time based payload successfully.
158 @retval EFI_OUT_OF_RESOURCES There are not enough memory resourses to create time based payload.
159 @retval EFI_INVALID_PARAMETER The parameter is invalid.
160 @retval Others Unexpected error happens.
164 CreateTimeBasedPayload (
165 IN OUT UINTN
*DataSize
,
173 EFI_VARIABLE_AUTHENTICATION_2
*DescriptorData
;
174 UINTN DescriptorSize
;
177 if (Data
== NULL
|| DataSize
== NULL
) {
178 return EFI_INVALID_PARAMETER
;
182 // In Setup mode or Custom mode, the variable does not need to be signed but the
183 // parameters to the SetVariable() call still need to be prepared as authenticated
184 // variable. So we create EFI_VARIABLE_AUTHENTICATED_2 descriptor without certificate
188 PayloadSize
= *DataSize
;
190 DescriptorSize
= OFFSET_OF (EFI_VARIABLE_AUTHENTICATION_2
, AuthInfo
) + OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID
, CertData
);
191 NewData
= (UINT8
*) AllocateZeroPool (DescriptorSize
+ PayloadSize
);
192 if (NewData
== NULL
) {
193 return EFI_OUT_OF_RESOURCES
;
196 if ((Payload
!= NULL
) && (PayloadSize
!= 0)) {
197 CopyMem (NewData
+ DescriptorSize
, Payload
, PayloadSize
);
200 DescriptorData
= (EFI_VARIABLE_AUTHENTICATION_2
*) (NewData
);
202 ZeroMem (&Time
, sizeof (EFI_TIME
));
203 Status
= gRT
->GetTime (&Time
, NULL
);
204 if (EFI_ERROR (Status
)) {
213 CopyMem (&DescriptorData
->TimeStamp
, &Time
, sizeof (EFI_TIME
));
215 DescriptorData
->AuthInfo
.Hdr
.dwLength
= OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID
, CertData
);
216 DescriptorData
->AuthInfo
.Hdr
.wRevision
= 0x0200;
217 DescriptorData
->AuthInfo
.Hdr
.wCertificateType
= WIN_CERT_TYPE_EFI_GUID
;
218 CopyGuid (&DescriptorData
->AuthInfo
.CertType
, &gEfiCertPkcs7Guid
);
220 if (Payload
!= NULL
) {
224 *DataSize
= DescriptorSize
+ PayloadSize
;
230 Internal helper function to delete a Variable given its name and GUID, NO authentication
233 @param[in] VariableName Name of the Variable.
234 @param[in] VendorGuid GUID of the Variable.
236 @retval EFI_SUCCESS Variable deleted successfully.
237 @retval Others The driver failed to start the device.
242 IN CHAR16
*VariableName
,
243 IN EFI_GUID
*VendorGuid
252 GetVariable2 (VariableName
, VendorGuid
, &Variable
, NULL
);
253 if (Variable
== NULL
) {
259 Attr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
260 | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
262 Status
= CreateTimeBasedPayload (&DataSize
, &Data
);
263 if (EFI_ERROR (Status
)) {
264 DEBUG ((EFI_D_ERROR
, "Fail to create time-based data payload: %r", Status
));
268 Status
= gRT
->SetVariable (
282 Generate the PK signature list from the X509 Certificate storing file (.cer)
284 @param[in] X509File FileHandle of X509 Certificate storing file.
285 @param[out] PkCert Point to the data buffer to store the signature list.
287 @return EFI_UNSUPPORTED Unsupported Key Length.
288 @return EFI_OUT_OF_RESOURCES There are not enough memory resourses to form the signature list.
292 CreatePkX509SignatureList (
293 IN EFI_FILE_HANDLE X509File
,
294 OUT EFI_SIGNATURE_LIST
**PkCert
300 EFI_SIGNATURE_DATA
*PkCertData
;
306 Status
= ReadFileContent (X509File
, (VOID
**) &X509Data
, &X509DataSize
, 0);
307 if (EFI_ERROR (Status
)) {
310 ASSERT (X509Data
!= NULL
);
313 // Allocate space for PK certificate list and initialize it.
314 // Create PK database entry with SignatureHeaderSize equals 0.
316 *PkCert
= (EFI_SIGNATURE_LIST
*) AllocateZeroPool (
317 sizeof(EFI_SIGNATURE_LIST
) + sizeof(EFI_SIGNATURE_DATA
) - 1
320 if (*PkCert
== NULL
) {
321 Status
= EFI_OUT_OF_RESOURCES
;
325 (*PkCert
)->SignatureListSize
= (UINT32
) (sizeof(EFI_SIGNATURE_LIST
)
326 + sizeof(EFI_SIGNATURE_DATA
) - 1
328 (*PkCert
)->SignatureSize
= (UINT32
) (sizeof(EFI_SIGNATURE_DATA
) - 1 + X509DataSize
);
329 (*PkCert
)->SignatureHeaderSize
= 0;
330 CopyGuid (&(*PkCert
)->SignatureType
, &gEfiCertX509Guid
);
331 PkCertData
= (EFI_SIGNATURE_DATA
*) ((UINTN
)(*PkCert
)
332 + sizeof(EFI_SIGNATURE_LIST
)
333 + (*PkCert
)->SignatureHeaderSize
);
334 CopyGuid (&PkCertData
->SignatureOwner
, &gEfiGlobalVariableGuid
);
336 // Fill the PK database with PKpub data from X509 certificate file.
338 CopyMem (&(PkCertData
->SignatureData
[0]), X509Data
, X509DataSize
);
342 if (X509Data
!= NULL
) {
346 if (EFI_ERROR(Status
) && *PkCert
!= NULL
) {
355 Enroll new PK into the System without original PK's authentication.
357 The SignatureOwner GUID will be the same with PK's vendorguid.
359 @param[in] PrivateData The module's private data.
361 @retval EFI_SUCCESS New PK enrolled successfully.
362 @retval EFI_INVALID_PARAMETER The parameter is invalid.
363 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
368 IN SECUREBOOT_CONFIG_PRIVATE_DATA
* Private
374 EFI_SIGNATURE_LIST
*PkCert
;
378 if (Private
->FileContext
->FileName
== NULL
) {
379 return EFI_INVALID_PARAMETER
;
385 // Parse the file's postfix. Only support DER encoded X.509 certificate files.
387 NameLength
= StrLen (Private
->FileContext
->FileName
);
388 if (NameLength
<= 4) {
389 return EFI_INVALID_PARAMETER
;
391 FilePostFix
= Private
->FileContext
->FileName
+ NameLength
- 4;
392 if (!IsDerEncodeCertificate(FilePostFix
)) {
393 DEBUG ((EFI_D_ERROR
, "Unsupported file type, only DER encoded certificate (%s) is supported.", mSupportX509Suffix
));
394 return EFI_INVALID_PARAMETER
;
396 DEBUG ((EFI_D_INFO
, "FileName= %s\n", Private
->FileContext
->FileName
));
397 DEBUG ((EFI_D_INFO
, "FilePostFix = %s\n", FilePostFix
));
400 // Prase the selected PK file and generature PK certificate list.
402 Status
= CreatePkX509SignatureList (
403 Private
->FileContext
->FHandle
,
406 if (EFI_ERROR (Status
)) {
409 ASSERT (PkCert
!= NULL
);
412 // Set Platform Key variable.
414 Attr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
415 | EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
416 DataSize
= PkCert
->SignatureListSize
;
417 Status
= CreateTimeBasedPayload (&DataSize
, (UINT8
**) &PkCert
);
418 if (EFI_ERROR (Status
)) {
419 DEBUG ((EFI_D_ERROR
, "Fail to create time-based data payload: %r", Status
));
423 Status
= gRT
->SetVariable(
424 EFI_PLATFORM_KEY_NAME
,
425 &gEfiGlobalVariableGuid
,
430 if (EFI_ERROR (Status
)) {
431 if (Status
== EFI_OUT_OF_RESOURCES
) {
432 DEBUG ((EFI_D_ERROR
, "Enroll PK failed with out of resource.\n"));
439 if (PkCert
!= NULL
) {
443 if (Private
->FileContext
->FHandle
!= NULL
) {
444 CloseFile (Private
->FileContext
->FHandle
);
445 Private
->FileContext
->FHandle
= NULL
;
452 Remove the PK variable.
454 @retval EFI_SUCCESS Delete PK successfully.
455 @retval Others Could not allow to delete PK.
465 Status
= DeleteVariable (
466 EFI_PLATFORM_KEY_NAME
,
467 &gEfiGlobalVariableGuid
473 Enroll a new KEK item from public key storing file (*.pbk).
475 @param[in] PrivateData The module's private data.
477 @retval EFI_SUCCESS New KEK enrolled successfully.
478 @retval EFI_INVALID_PARAMETER The parameter is invalid.
479 @retval EFI_UNSUPPORTED Unsupported command.
480 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
485 IN SECUREBOOT_CONFIG_PRIVATE_DATA
*Private
491 EFI_SIGNATURE_LIST
*KekSigList
;
494 CPL_KEY_INFO
*KeyInfo
;
495 EFI_SIGNATURE_DATA
*KEKSigData
;
496 UINTN KekSigListSize
;
511 // Form the KeKpub certificate list into EFI_SIGNATURE_LIST type.
512 // First, We have to parse out public key data from the pbk key file.
514 Status
= ReadFileContent (
515 Private
->FileContext
->FHandle
,
520 if (EFI_ERROR (Status
)) {
523 ASSERT (KeyBlob
!= NULL
);
524 KeyInfo
= (CPL_KEY_INFO
*) KeyBlob
;
525 if (KeyInfo
->KeyLengthInBits
/ 8 != WIN_CERT_UEFI_RSA2048_SIZE
) {
526 DEBUG ((DEBUG_ERROR
, "Unsupported key length, Only RSA2048 is supported.\n"));
527 Status
= EFI_UNSUPPORTED
;
532 // Convert the Public key to fix octet string format represented in RSA PKCS#1.
534 KeyLenInBytes
= KeyInfo
->KeyLengthInBits
/ 8;
535 KeyBuffer
= AllocateZeroPool (KeyLenInBytes
);
536 if (KeyBuffer
== NULL
) {
537 Status
= EFI_OUT_OF_RESOURCES
;
541 (UINTN
*) (KeyBlob
+ sizeof (CPL_KEY_INFO
)),
542 KeyLenInBytes
/ sizeof (UINTN
),
546 CopyMem(KeyBlob
+ sizeof(CPL_KEY_INFO
), KeyBuffer
, KeyLenInBytes
);
549 // Form an new EFI_SIGNATURE_LIST.
551 KekSigListSize
= sizeof(EFI_SIGNATURE_LIST
)
552 + sizeof(EFI_SIGNATURE_DATA
) - 1
553 + WIN_CERT_UEFI_RSA2048_SIZE
;
555 KekSigList
= (EFI_SIGNATURE_LIST
*) AllocateZeroPool (KekSigListSize
);
556 if (KekSigList
== NULL
) {
557 Status
= EFI_OUT_OF_RESOURCES
;
561 KekSigList
->SignatureListSize
= sizeof(EFI_SIGNATURE_LIST
)
562 + sizeof(EFI_SIGNATURE_DATA
) - 1
563 + WIN_CERT_UEFI_RSA2048_SIZE
;
564 KekSigList
->SignatureHeaderSize
= 0;
565 KekSigList
->SignatureSize
= sizeof(EFI_SIGNATURE_DATA
) - 1 + WIN_CERT_UEFI_RSA2048_SIZE
;
566 CopyGuid (&KekSigList
->SignatureType
, &gEfiCertRsa2048Guid
);
568 KEKSigData
= (EFI_SIGNATURE_DATA
*)((UINT8
*)KekSigList
+ sizeof(EFI_SIGNATURE_LIST
));
569 CopyGuid (&KEKSigData
->SignatureOwner
, Private
->SignatureGUID
);
571 KEKSigData
->SignatureData
,
572 KeyBlob
+ sizeof(CPL_KEY_INFO
),
573 WIN_CERT_UEFI_RSA2048_SIZE
577 // Check if KEK entry has been already existed.
578 // If true, use EFI_VARIABLE_APPEND_WRITE attribute to append the
579 // new KEK to original variable.
581 Attr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
582 | EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
583 Status
= CreateTimeBasedPayload (&KekSigListSize
, (UINT8
**) &KekSigList
);
584 if (EFI_ERROR (Status
)) {
585 DEBUG ((EFI_D_ERROR
, "Fail to create time-based data payload: %r", Status
));
589 Status
= gRT
->GetVariable(
590 EFI_KEY_EXCHANGE_KEY_NAME
,
591 &gEfiGlobalVariableGuid
,
596 if (Status
== EFI_BUFFER_TOO_SMALL
) {
597 Attr
|= EFI_VARIABLE_APPEND_WRITE
;
598 } else if (Status
!= EFI_NOT_FOUND
) {
603 // Done. Now we have formed the correct KEKpub database item, just set it into variable storage,
605 Status
= gRT
->SetVariable(
606 EFI_KEY_EXCHANGE_KEY_NAME
,
607 &gEfiGlobalVariableGuid
,
612 if (EFI_ERROR (Status
)) {
618 CloseFile (Private
->FileContext
->FHandle
);
619 Private
->FileContext
->FHandle
= NULL
;
620 Private
->FileContext
->FileName
= NULL
;
622 if (Private
->SignatureGUID
!= NULL
) {
623 FreePool (Private
->SignatureGUID
);
624 Private
->SignatureGUID
= NULL
;
627 if (KeyBlob
!= NULL
) {
630 if (KeyBuffer
!= NULL
) {
631 FreePool (KeyBuffer
);
633 if (KekSigList
!= NULL
) {
634 FreePool (KekSigList
);
641 Enroll a new KEK item from X509 certificate file.
643 @param[in] PrivateData The module's private data.
645 @retval EFI_SUCCESS New X509 is enrolled successfully.
646 @retval EFI_INVALID_PARAMETER The parameter is invalid.
647 @retval EFI_UNSUPPORTED Unsupported command.
648 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
653 IN SECUREBOOT_CONFIG_PRIVATE_DATA
*Private
659 EFI_SIGNATURE_DATA
*KEKSigData
;
660 EFI_SIGNATURE_LIST
*KekSigList
;
662 UINTN KekSigListSize
;
672 Status
= ReadFileContent (
673 Private
->FileContext
->FHandle
,
678 if (EFI_ERROR (Status
)) {
681 ASSERT (X509Data
!= NULL
);
683 KekSigListSize
= sizeof(EFI_SIGNATURE_LIST
) + sizeof(EFI_SIGNATURE_DATA
) - 1 + X509DataSize
;
684 KekSigList
= (EFI_SIGNATURE_LIST
*) AllocateZeroPool (KekSigListSize
);
685 if (KekSigList
== NULL
) {
686 Status
= EFI_OUT_OF_RESOURCES
;
691 // Fill Certificate Database parameters.
693 KekSigList
->SignatureListSize
= (UINT32
) KekSigListSize
;
694 KekSigList
->SignatureHeaderSize
= 0;
695 KekSigList
->SignatureSize
= (UINT32
) (sizeof(EFI_SIGNATURE_DATA
) - 1 + X509DataSize
);
696 CopyGuid (&KekSigList
->SignatureType
, &gEfiCertX509Guid
);
698 KEKSigData
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) KekSigList
+ sizeof (EFI_SIGNATURE_LIST
));
699 CopyGuid (&KEKSigData
->SignatureOwner
, Private
->SignatureGUID
);
700 CopyMem (KEKSigData
->SignatureData
, X509Data
, X509DataSize
);
703 // Check if KEK been already existed.
704 // If true, use EFI_VARIABLE_APPEND_WRITE attribute to append the
705 // new kek to original variable
707 Attr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
708 | EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
709 Status
= CreateTimeBasedPayload (&KekSigListSize
, (UINT8
**) &KekSigList
);
710 if (EFI_ERROR (Status
)) {
711 DEBUG ((EFI_D_ERROR
, "Fail to create time-based data payload: %r", Status
));
715 Status
= gRT
->GetVariable(
716 EFI_KEY_EXCHANGE_KEY_NAME
,
717 &gEfiGlobalVariableGuid
,
722 if (Status
== EFI_BUFFER_TOO_SMALL
) {
723 Attr
|= EFI_VARIABLE_APPEND_WRITE
;
724 } else if (Status
!= EFI_NOT_FOUND
) {
728 Status
= gRT
->SetVariable(
729 EFI_KEY_EXCHANGE_KEY_NAME
,
730 &gEfiGlobalVariableGuid
,
735 if (EFI_ERROR (Status
)) {
741 CloseFile (Private
->FileContext
->FHandle
);
742 Private
->FileContext
->FileName
= NULL
;
743 Private
->FileContext
->FHandle
= NULL
;
745 if (Private
->SignatureGUID
!= NULL
) {
746 FreePool (Private
->SignatureGUID
);
747 Private
->SignatureGUID
= NULL
;
750 if (KekSigList
!= NULL
) {
751 FreePool (KekSigList
);
758 Enroll new KEK into the System without PK's authentication.
759 The SignatureOwner GUID will be Private->SignatureGUID.
761 @param[in] PrivateData The module's private data.
763 @retval EFI_SUCCESS New KEK enrolled successful.
764 @retval EFI_INVALID_PARAMETER The parameter is invalid.
765 @retval others Fail to enroll KEK data.
769 EnrollKeyExchangeKey (
770 IN SECUREBOOT_CONFIG_PRIVATE_DATA
*Private
776 if ((Private
->FileContext
->FileName
== NULL
) || (Private
->SignatureGUID
== NULL
)) {
777 return EFI_INVALID_PARAMETER
;
781 // Parse the file's postfix. Supports DER-encoded X509 certificate,
782 // and .pbk as RSA public key file.
784 NameLength
= StrLen (Private
->FileContext
->FileName
);
785 if (NameLength
<= 4) {
786 return EFI_INVALID_PARAMETER
;
788 FilePostFix
= Private
->FileContext
->FileName
+ NameLength
- 4;
789 if (IsDerEncodeCertificate(FilePostFix
)) {
790 return EnrollX509ToKek (Private
);
791 } else if (CompareMem (FilePostFix
, L
".pbk",4) == 0) {
792 return EnrollRsa2048ToKek (Private
);
794 return EFI_INVALID_PARAMETER
;
799 Enroll a new X509 certificate into Signature Database (DB or DBX) without
800 KEK's authentication.
802 @param[in] PrivateData The module's private data.
803 @param[in] VariableName Variable name of signature database, must be
804 EFI_IMAGE_SECURITY_DATABASE or EFI_IMAGE_SECURITY_DATABASE1.
806 @retval EFI_SUCCESS New X509 is enrolled successfully.
807 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
812 IN SECUREBOOT_CONFIG_PRIVATE_DATA
*Private
,
813 IN CHAR16
*VariableName
819 EFI_SIGNATURE_LIST
*SigDBCert
;
820 EFI_SIGNATURE_DATA
*SigDBCertData
;
831 SigDBCertData
= NULL
;
834 Status
= ReadFileContent (
835 Private
->FileContext
->FHandle
,
840 if (EFI_ERROR (Status
)) {
843 ASSERT (X509Data
!= NULL
);
845 SigDBSize
= sizeof(EFI_SIGNATURE_LIST
) + sizeof(EFI_SIGNATURE_DATA
) - 1 + X509DataSize
;
847 Data
= AllocateZeroPool (SigDBSize
);
849 Status
= EFI_OUT_OF_RESOURCES
;
854 // Fill Certificate Database parameters.
856 SigDBCert
= (EFI_SIGNATURE_LIST
*) Data
;
857 SigDBCert
->SignatureListSize
= (UINT32
) SigDBSize
;
858 SigDBCert
->SignatureHeaderSize
= 0;
859 SigDBCert
->SignatureSize
= (UINT32
) (sizeof(EFI_SIGNATURE_DATA
) - 1 + X509DataSize
);
860 CopyGuid (&SigDBCert
->SignatureType
, &gEfiCertX509Guid
);
862 SigDBCertData
= (EFI_SIGNATURE_DATA
*) ((UINT8
* ) SigDBCert
+ sizeof (EFI_SIGNATURE_LIST
));
863 CopyGuid (&SigDBCertData
->SignatureOwner
, Private
->SignatureGUID
);
864 CopyMem ((UINT8
* ) (SigDBCertData
->SignatureData
), X509Data
, X509DataSize
);
867 // Check if signature database entry has been already existed.
868 // If true, use EFI_VARIABLE_APPEND_WRITE attribute to append the
869 // new signature data to original variable
871 Attr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
872 | EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
873 Status
= CreateTimeBasedPayload (&SigDBSize
, (UINT8
**) &Data
);
874 if (EFI_ERROR (Status
)) {
875 DEBUG ((EFI_D_ERROR
, "Fail to create time-based data payload: %r", Status
));
879 Status
= gRT
->GetVariable(
881 &gEfiImageSecurityDatabaseGuid
,
886 if (Status
== EFI_BUFFER_TOO_SMALL
) {
887 Attr
|= EFI_VARIABLE_APPEND_WRITE
;
888 } else if (Status
!= EFI_NOT_FOUND
) {
892 Status
= gRT
->SetVariable(
894 &gEfiImageSecurityDatabaseGuid
,
899 if (EFI_ERROR (Status
)) {
905 CloseFile (Private
->FileContext
->FHandle
);
906 Private
->FileContext
->FileName
= NULL
;
907 Private
->FileContext
->FHandle
= NULL
;
909 if (Private
->SignatureGUID
!= NULL
) {
910 FreePool (Private
->SignatureGUID
);
911 Private
->SignatureGUID
= NULL
;
918 if (X509Data
!= NULL
) {
926 Load PE/COFF image information into internal buffer and check its validity.
928 @retval EFI_SUCCESS Successful
929 @retval EFI_UNSUPPORTED Invalid PE/COFF file
930 @retval EFI_ABORTED Serious error occurs, like file I/O error etc.
938 EFI_IMAGE_DOS_HEADER
*DosHdr
;
939 EFI_IMAGE_NT_HEADERS32
*NtHeader32
;
940 EFI_IMAGE_NT_HEADERS64
*NtHeader64
;
945 // Read the Dos header
947 DosHdr
= (EFI_IMAGE_DOS_HEADER
*)(mImageBase
);
948 if (DosHdr
->e_magic
== EFI_IMAGE_DOS_SIGNATURE
)
951 // DOS image header is present,
952 // So read the PE header after the DOS image header
954 mPeCoffHeaderOffset
= DosHdr
->e_lfanew
;
958 mPeCoffHeaderOffset
= 0;
962 // Read PE header and check the signature validity and machine compatibility
964 NtHeader32
= (EFI_IMAGE_NT_HEADERS32
*) (mImageBase
+ mPeCoffHeaderOffset
);
965 if (NtHeader32
->Signature
!= EFI_IMAGE_NT_SIGNATURE
)
967 return EFI_UNSUPPORTED
;
970 mNtHeader
.Pe32
= NtHeader32
;
973 // Check the architecture field of PE header and get the Certificate Data Directory data
974 // Note the size of FileHeader field is constant for both IA32 and X64 arch
976 if ((NtHeader32
->FileHeader
.Machine
== EFI_IMAGE_MACHINE_IA32
)
977 || (NtHeader32
->FileHeader
.Machine
== EFI_IMAGE_MACHINE_EBC
)) {
979 // IA-32 Architecture
981 mImageType
= ImageType_IA32
;
982 mSecDataDir
= (EFI_IMAGE_SECURITY_DATA_DIRECTORY
*) &(NtHeader32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
]);
984 else if ((NtHeader32
->FileHeader
.Machine
== EFI_IMAGE_MACHINE_IA64
)
985 || (NtHeader32
->FileHeader
.Machine
== EFI_IMAGE_MACHINE_X64
)) {
987 // 64-bits Architecture
989 mImageType
= ImageType_X64
;
990 NtHeader64
= (EFI_IMAGE_NT_HEADERS64
*) (mImageBase
+ mPeCoffHeaderOffset
);
991 mSecDataDir
= (EFI_IMAGE_SECURITY_DATA_DIRECTORY
*) &(NtHeader64
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
]);
993 return EFI_UNSUPPORTED
;
1000 Calculate hash of Pe/Coff image based on the authenticode image hashing in
1001 PE/COFF Specification 8.0 Appendix A
1003 @param[in] HashAlg Hash algorithm type.
1005 @retval TRUE Successfully hash image.
1006 @retval FALSE Fail in hash image.
1016 EFI_IMAGE_SECTION_HEADER
*Section
;
1021 UINTN SumOfBytesHashed
;
1022 EFI_IMAGE_SECTION_HEADER
*SectionHeader
;
1027 SectionHeader
= NULL
;
1030 if ((HashAlg
!= HASHALG_SHA1
) && (HashAlg
!= HASHALG_SHA256
)) {
1035 // Initialize context of hash.
1037 ZeroMem (mImageDigest
, MAX_DIGEST_SIZE
);
1039 if (HashAlg
== HASHALG_SHA1
) {
1040 mImageDigestSize
= SHA1_DIGEST_SIZE
;
1041 mCertType
= gEfiCertSha1Guid
;
1042 } else if (HashAlg
== HASHALG_SHA256
) {
1043 mImageDigestSize
= SHA256_DIGEST_SIZE
;
1044 mCertType
= gEfiCertSha256Guid
;
1047 CtxSize
= mHash
[HashAlg
].GetContextSize();
1049 HashCtx
= AllocatePool (CtxSize
);
1050 ASSERT (HashCtx
!= NULL
);
1052 // 1. Load the image header into memory.
1054 // 2. Initialize a SHA hash context.
1055 Status
= mHash
[HashAlg
].HashInit(HashCtx
);
1060 // Measuring PE/COFF Image Header;
1061 // But CheckSum field and SECURITY data directory (certificate) are excluded
1063 if (mNtHeader
.Pe32
->FileHeader
.Machine
== IMAGE_FILE_MACHINE_IA64
&& mNtHeader
.Pe32
->OptionalHeader
.Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
1065 // NOTE: Some versions of Linux ELILO for Itanium have an incorrect magic value
1066 // in the PE/COFF Header. If the MachineType is Itanium(IA64) and the
1067 // Magic value in the OptionalHeader is EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
1068 // then override the magic value to EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
1070 Magic
= EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
;
1073 // Get the magic value from the PE/COFF Optional Header
1075 Magic
= mNtHeader
.Pe32
->OptionalHeader
.Magic
;
1079 // 3. Calculate the distance from the base of the image header to the image checksum address.
1080 // 4. Hash the image header from its base to beginning of the image checksum.
1082 HashBase
= mImageBase
;
1083 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
1087 HashSize
= (UINTN
) ((UINT8
*) (&mNtHeader
.Pe32
->OptionalHeader
.CheckSum
) - HashBase
);
1090 // Use PE32+ offset.
1092 HashSize
= (UINTN
) ((UINT8
*) (&mNtHeader
.Pe32Plus
->OptionalHeader
.CheckSum
) - HashBase
);
1095 Status
= mHash
[HashAlg
].HashUpdate(HashCtx
, HashBase
, HashSize
);
1100 // 5. Skip over the image checksum (it occupies a single ULONG).
1101 // 6. Get the address of the beginning of the Cert Directory.
1102 // 7. Hash everything from the end of the checksum to the start of the Cert Directory.
1104 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
1108 HashBase
= (UINT8
*) &mNtHeader
.Pe32
->OptionalHeader
.CheckSum
+ sizeof (UINT32
);
1109 HashSize
= (UINTN
) ((UINT8
*) (&mNtHeader
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
]) - HashBase
);
1112 // Use PE32+ offset.
1114 HashBase
= (UINT8
*) &mNtHeader
.Pe32Plus
->OptionalHeader
.CheckSum
+ sizeof (UINT32
);
1115 HashSize
= (UINTN
) ((UINT8
*) (&mNtHeader
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
]) - HashBase
);
1118 Status
= mHash
[HashAlg
].HashUpdate(HashCtx
, HashBase
, HashSize
);
1123 // 8. Skip over the Cert Directory. (It is sizeof(IMAGE_DATA_DIRECTORY) bytes.)
1124 // 9. Hash everything from the end of the Cert Directory to the end of image header.
1126 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
1130 HashBase
= (UINT8
*) &mNtHeader
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
+ 1];
1131 HashSize
= mNtHeader
.Pe32
->OptionalHeader
.SizeOfHeaders
- (UINTN
) ((UINT8
*) (&mNtHeader
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
+ 1]) - mImageBase
);
1134 // Use PE32+ offset.
1136 HashBase
= (UINT8
*) &mNtHeader
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
+ 1];
1137 HashSize
= mNtHeader
.Pe32Plus
->OptionalHeader
.SizeOfHeaders
- (UINTN
) ((UINT8
*) (&mNtHeader
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
+ 1]) - mImageBase
);
1140 Status
= mHash
[HashAlg
].HashUpdate(HashCtx
, HashBase
, HashSize
);
1145 // 10. Set the SUM_OF_BYTES_HASHED to the size of the header.
1147 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
1151 SumOfBytesHashed
= mNtHeader
.Pe32
->OptionalHeader
.SizeOfHeaders
;
1156 SumOfBytesHashed
= mNtHeader
.Pe32Plus
->OptionalHeader
.SizeOfHeaders
;
1160 // 11. Build a temporary table of pointers to all the IMAGE_SECTION_HEADER
1161 // structures in the image. The 'NumberOfSections' field of the image
1162 // header indicates how big the table should be. Do not include any
1163 // IMAGE_SECTION_HEADERs in the table whose 'SizeOfRawData' field is zero.
1165 SectionHeader
= (EFI_IMAGE_SECTION_HEADER
*) AllocateZeroPool (sizeof (EFI_IMAGE_SECTION_HEADER
) * mNtHeader
.Pe32
->FileHeader
.NumberOfSections
);
1166 ASSERT (SectionHeader
!= NULL
);
1168 // 12. Using the 'PointerToRawData' in the referenced section headers as
1169 // a key, arrange the elements in the table in ascending order. In other
1170 // words, sort the section headers according to the disk-file offset of
1173 Section
= (EFI_IMAGE_SECTION_HEADER
*) (
1175 mPeCoffHeaderOffset
+
1177 sizeof (EFI_IMAGE_FILE_HEADER
) +
1178 mNtHeader
.Pe32
->FileHeader
.SizeOfOptionalHeader
1180 for (Index
= 0; Index
< mNtHeader
.Pe32
->FileHeader
.NumberOfSections
; Index
++) {
1182 while ((Pos
> 0) && (Section
->PointerToRawData
< SectionHeader
[Pos
- 1].PointerToRawData
)) {
1183 CopyMem (&SectionHeader
[Pos
], &SectionHeader
[Pos
- 1], sizeof (EFI_IMAGE_SECTION_HEADER
));
1186 CopyMem (&SectionHeader
[Pos
], Section
, sizeof (EFI_IMAGE_SECTION_HEADER
));
1191 // 13. Walk through the sorted table, bring the corresponding section
1192 // into memory, and hash the entire section (using the 'SizeOfRawData'
1193 // field in the section header to determine the amount of data to hash).
1194 // 14. Add the section's 'SizeOfRawData' to SUM_OF_BYTES_HASHED .
1195 // 15. Repeat steps 13 and 14 for all the sections in the sorted table.
1197 for (Index
= 0; Index
< mNtHeader
.Pe32
->FileHeader
.NumberOfSections
; Index
++) {
1198 Section
= &SectionHeader
[Index
];
1199 if (Section
->SizeOfRawData
== 0) {
1202 HashBase
= mImageBase
+ Section
->PointerToRawData
;
1203 HashSize
= (UINTN
) Section
->SizeOfRawData
;
1205 Status
= mHash
[HashAlg
].HashUpdate(HashCtx
, HashBase
, HashSize
);
1210 SumOfBytesHashed
+= HashSize
;
1214 // 16. If the file size is greater than SUM_OF_BYTES_HASHED, there is extra
1215 // data in the file that needs to be added to the hash. This data begins
1216 // at file offset SUM_OF_BYTES_HASHED and its length is:
1217 // FileSize - (CertDirectory->Size)
1219 if (mImageSize
> SumOfBytesHashed
) {
1220 HashBase
= mImageBase
+ SumOfBytesHashed
;
1221 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
1227 mNtHeader
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
].Size
-
1231 // Use PE32+ offset.
1235 mNtHeader
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
].Size
-
1239 Status
= mHash
[HashAlg
].HashUpdate(HashCtx
, HashBase
, HashSize
);
1245 Status
= mHash
[HashAlg
].HashFinal(HashCtx
, mImageDigest
);
1248 if (HashCtx
!= NULL
) {
1251 if (SectionHeader
!= NULL
) {
1252 FreePool (SectionHeader
);
1258 Recognize the Hash algorithm in PE/COFF Authenticode and caculate hash of
1259 Pe/Coff image based on the authenticated image hashing in PE/COFF Specification
1262 @retval EFI_UNSUPPORTED Hash algorithm is not supported.
1263 @retval EFI_SUCCESS Hash successfully.
1272 WIN_CERTIFICATE_EFI_PKCS
*PkcsCertData
;
1274 PkcsCertData
= (WIN_CERTIFICATE_EFI_PKCS
*) (mImageBase
+ mSecDataDir
->Offset
);
1276 for (Index
= 0; Index
< HASHALG_MAX
; Index
++) {
1278 // Check the Hash algorithm in PE/COFF Authenticode.
1279 // According to PKCS#7 Definition:
1280 // SignedData ::= SEQUENCE {
1282 // digestAlgorithms DigestAlgorithmIdentifiers,
1283 // contentInfo ContentInfo,
1285 // The DigestAlgorithmIdentifiers can be used to determine the hash algorithm in PE/COFF hashing
1286 // This field has the fixed offset (+32) in final Authenticode ASN.1 data.
1287 // Fixed offset (+32) is calculated based on two bytes of length encoding.
1289 if ((*(PkcsCertData
->CertData
+ 1) & TWO_BYTE_ENCODE
) != TWO_BYTE_ENCODE
) {
1291 // Only support two bytes of Long Form of Length Encoding.
1297 if (CompareMem (PkcsCertData
->CertData
+ 32, mHash
[Index
].OidValue
, mHash
[Index
].OidLength
) == 0) {
1302 if (Index
== HASHALG_MAX
) {
1303 return EFI_UNSUPPORTED
;
1307 // HASH PE Image based on Hash algorithm in PE/COFF Authenticode.
1309 if (!HashPeImage(Index
)) {
1310 return EFI_UNSUPPORTED
;
1317 Enroll a new executable's signature into Signature Database.
1319 @param[in] PrivateData The module's private data.
1320 @param[in] VariableName Variable name of signature database, must be
1321 EFI_IMAGE_SECURITY_DATABASE or EFI_IMAGE_SECURITY_DATABASE1.
1323 @retval EFI_SUCCESS New signature is enrolled successfully.
1324 @retval EFI_INVALID_PARAMETER The parameter is invalid.
1325 @retval EFI_UNSUPPORTED Unsupported command.
1326 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
1330 EnrollImageSignatureToSigDB (
1331 IN SECUREBOOT_CONFIG_PRIVATE_DATA
*Private
,
1332 IN CHAR16
*VariableName
1336 EFI_SIGNATURE_LIST
*SigDBCert
;
1337 EFI_SIGNATURE_DATA
*SigDBCertData
;
1342 WIN_CERTIFICATE_UEFI_GUID
*GuidCertData
;
1345 GuidCertData
= NULL
;
1348 // Form the SigDB certificate list.
1349 // Format the data item into EFI_SIGNATURE_LIST type.
1351 // We need to parse executable's signature data from specified signed executable file.
1352 // In current implementation, we simply trust the pass-in signed executable file.
1353 // In reality, it's OS's responsibility to verify the signed executable file.
1357 // Read the whole file content
1359 Status
= ReadFileContent(
1360 Private
->FileContext
->FHandle
,
1361 (VOID
**) &mImageBase
,
1365 if (EFI_ERROR (Status
)) {
1368 ASSERT (mImageBase
!= NULL
);
1370 Status
= LoadPeImage ();
1371 if (EFI_ERROR (Status
)) {
1375 if (mSecDataDir
->SizeOfCert
== 0) {
1376 if (!HashPeImage (HASHALG_SHA256
)) {
1377 Status
= EFI_SECURITY_VIOLATION
;
1383 // Read the certificate data
1385 mCertificate
= (WIN_CERTIFICATE
*)(mImageBase
+ mSecDataDir
->Offset
);
1387 if (mCertificate
->wCertificateType
== WIN_CERT_TYPE_EFI_GUID
) {
1388 GuidCertData
= (WIN_CERTIFICATE_UEFI_GUID
*) mCertificate
;
1389 if (CompareMem (&GuidCertData
->CertType
, &gEfiCertTypeRsa2048Sha256Guid
, sizeof(EFI_GUID
)) != 0) {
1390 Status
= EFI_ABORTED
;
1394 if (!HashPeImage (HASHALG_SHA256
)) {
1395 Status
= EFI_ABORTED
;
1399 } else if (mCertificate
->wCertificateType
== WIN_CERT_TYPE_PKCS_SIGNED_DATA
) {
1401 Status
= HashPeImageByType ();
1402 if (EFI_ERROR (Status
)) {
1406 Status
= EFI_ABORTED
;
1412 // Create a new SigDB entry.
1414 SigDBSize
= sizeof(EFI_SIGNATURE_LIST
)
1415 + sizeof(EFI_SIGNATURE_DATA
) - 1
1416 + (UINT32
) mImageDigestSize
;
1418 Data
= (UINT8
*) AllocateZeroPool (SigDBSize
);
1420 Status
= EFI_OUT_OF_RESOURCES
;
1425 // Adjust the Certificate Database parameters.
1427 SigDBCert
= (EFI_SIGNATURE_LIST
*) Data
;
1428 SigDBCert
->SignatureListSize
= (UINT32
) SigDBSize
;
1429 SigDBCert
->SignatureHeaderSize
= 0;
1430 SigDBCert
->SignatureSize
= sizeof(EFI_SIGNATURE_DATA
) - 1 + (UINT32
) mImageDigestSize
;
1431 CopyGuid (&SigDBCert
->SignatureType
, &mCertType
);
1433 SigDBCertData
= (EFI_SIGNATURE_DATA
*)((UINT8
*)SigDBCert
+ sizeof(EFI_SIGNATURE_LIST
));
1434 CopyGuid (&SigDBCertData
->SignatureOwner
, Private
->SignatureGUID
);
1435 CopyMem (SigDBCertData
->SignatureData
, mImageDigest
, mImageDigestSize
);
1437 Attr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
1438 | EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
1439 Status
= CreateTimeBasedPayload (&SigDBSize
, (UINT8
**) &Data
);
1440 if (EFI_ERROR (Status
)) {
1441 DEBUG ((EFI_D_ERROR
, "Fail to create time-based data payload: %r", Status
));
1446 // Check if SigDB variable has been already existed.
1447 // If true, use EFI_VARIABLE_APPEND_WRITE attribute to append the
1448 // new signature data to original variable
1451 Status
= gRT
->GetVariable(
1453 &gEfiImageSecurityDatabaseGuid
,
1458 if (Status
== EFI_BUFFER_TOO_SMALL
) {
1459 Attr
|= EFI_VARIABLE_APPEND_WRITE
;
1460 } else if (Status
!= EFI_NOT_FOUND
) {
1465 // Enroll the variable.
1467 Status
= gRT
->SetVariable(
1469 &gEfiImageSecurityDatabaseGuid
,
1474 if (EFI_ERROR (Status
)) {
1480 CloseFile (Private
->FileContext
->FHandle
);
1481 Private
->FileContext
->FHandle
= NULL
;
1482 Private
->FileContext
->FileName
= NULL
;
1484 if (Private
->SignatureGUID
!= NULL
) {
1485 FreePool (Private
->SignatureGUID
);
1486 Private
->SignatureGUID
= NULL
;
1493 if (mImageBase
!= NULL
) {
1494 FreePool (mImageBase
);
1502 Enroll signature into DB/DBX without KEK's authentication.
1503 The SignatureOwner GUID will be Private->SignatureGUID.
1505 @param[in] PrivateData The module's private data.
1506 @param[in] VariableName Variable name of signature database, must be
1507 EFI_IMAGE_SECURITY_DATABASE or EFI_IMAGE_SECURITY_DATABASE1.
1509 @retval EFI_SUCCESS New signature enrolled successfully.
1510 @retval EFI_INVALID_PARAMETER The parameter is invalid.
1511 @retval others Fail to enroll signature data.
1515 EnrollSignatureDatabase (
1516 IN SECUREBOOT_CONFIG_PRIVATE_DATA
*Private
,
1517 IN CHAR16
*VariableName
1520 UINT16
* FilePostFix
;
1523 if ((Private
->FileContext
->FileName
== NULL
) || (Private
->FileContext
->FHandle
== NULL
) || (Private
->SignatureGUID
== NULL
)) {
1524 return EFI_INVALID_PARAMETER
;
1528 // Parse the file's postfix.
1530 NameLength
= StrLen (Private
->FileContext
->FileName
);
1531 if (NameLength
<= 4) {
1532 return EFI_INVALID_PARAMETER
;
1534 FilePostFix
= Private
->FileContext
->FileName
+ NameLength
- 4;
1535 if (IsDerEncodeCertificate(FilePostFix
)) {
1537 // Supports DER-encoded X509 certificate.
1539 return EnrollX509toSigDB (Private
, VariableName
);
1542 return EnrollImageSignatureToSigDB (Private
, VariableName
);
1546 List all signatures in specified signature database (e.g. KEK/DB/DBX)
1547 by GUID in the page for user to select and delete as needed.
1549 @param[in] PrivateData Module's private data.
1550 @param[in] VariableName The variable name of the vendor's signature database.
1551 @param[in] VendorGuid A unique identifier for the vendor.
1552 @param[in] LabelNumber Label number to insert opcodes.
1553 @param[in] FormId Form ID of current page.
1554 @param[in] QuestionIdBase Base question id of the signature list.
1556 @retval EFI_SUCCESS Success to update the signature list page
1557 @retval EFI_OUT_OF_RESOURCES Unable to allocate required resources.
1562 IN SECUREBOOT_CONFIG_PRIVATE_DATA
*PrivateData
,
1563 IN CHAR16
*VariableName
,
1564 IN EFI_GUID
*VendorGuid
,
1565 IN UINT16 LabelNumber
,
1566 IN EFI_FORM_ID FormId
,
1567 IN EFI_QUESTION_ID QuestionIdBase
1574 VOID
*StartOpCodeHandle
;
1575 VOID
*EndOpCodeHandle
;
1576 EFI_IFR_GUID_LABEL
*StartLabel
;
1577 EFI_IFR_GUID_LABEL
*EndLabel
;
1580 EFI_SIGNATURE_LIST
*CertList
;
1581 EFI_SIGNATURE_DATA
*Cert
;
1582 UINT32 ItemDataSize
;
1584 EFI_STRING_ID GuidID
;
1591 StartOpCodeHandle
= NULL
;
1592 EndOpCodeHandle
= NULL
;
1595 // Initialize the container for dynamic opcodes.
1597 StartOpCodeHandle
= HiiAllocateOpCodeHandle ();
1598 if (StartOpCodeHandle
== NULL
) {
1599 Status
= EFI_OUT_OF_RESOURCES
;
1603 EndOpCodeHandle
= HiiAllocateOpCodeHandle ();
1604 if (EndOpCodeHandle
== NULL
) {
1605 Status
= EFI_OUT_OF_RESOURCES
;
1610 // Create Hii Extend Label OpCode.
1612 StartLabel
= (EFI_IFR_GUID_LABEL
*) HiiCreateGuidOpCode (
1616 sizeof (EFI_IFR_GUID_LABEL
)
1618 StartLabel
->ExtendOpCode
= EFI_IFR_EXTEND_OP_LABEL
;
1619 StartLabel
->Number
= LabelNumber
;
1621 EndLabel
= (EFI_IFR_GUID_LABEL
*) HiiCreateGuidOpCode (
1625 sizeof (EFI_IFR_GUID_LABEL
)
1627 EndLabel
->ExtendOpCode
= EFI_IFR_EXTEND_OP_LABEL
;
1628 EndLabel
->Number
= LABEL_END
;
1634 Status
= gRT
->GetVariable (VariableName
, VendorGuid
, NULL
, &DataSize
, Data
);
1635 if (EFI_ERROR (Status
) && Status
!= EFI_BUFFER_TOO_SMALL
) {
1639 Data
= (UINT8
*) AllocateZeroPool (DataSize
);
1641 Status
= EFI_OUT_OF_RESOURCES
;
1645 Status
= gRT
->GetVariable (VariableName
, VendorGuid
, NULL
, &DataSize
, Data
);
1646 if (EFI_ERROR (Status
)) {
1650 GuidStr
= AllocateZeroPool (100);
1651 if (GuidStr
== NULL
) {
1652 Status
= EFI_OUT_OF_RESOURCES
;
1657 // Enumerate all KEK pub data.
1659 ItemDataSize
= (UINT32
) DataSize
;
1660 CertList
= (EFI_SIGNATURE_LIST
*) Data
;
1663 while ((ItemDataSize
> 0) && (ItemDataSize
>= CertList
->SignatureListSize
)) {
1665 if (CompareGuid (&CertList
->SignatureType
, &gEfiCertRsa2048Guid
)) {
1666 Help
= STRING_TOKEN (STR_CERT_TYPE_RSA2048_SHA256_GUID
);
1667 } else if (CompareGuid (&CertList
->SignatureType
, &gEfiCertX509Guid
)) {
1668 Help
= STRING_TOKEN (STR_CERT_TYPE_PCKS7_GUID
);
1669 } else if (CompareGuid (&CertList
->SignatureType
, &gEfiCertSha1Guid
)) {
1670 Help
= STRING_TOKEN (STR_CERT_TYPE_SHA1_GUID
);
1671 } else if (CompareGuid (&CertList
->SignatureType
, &gEfiCertSha256Guid
)) {
1672 Help
= STRING_TOKEN (STR_CERT_TYPE_SHA256_GUID
);
1675 // The signature type is not supported in current implementation.
1680 CertCount
= (CertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - CertList
->SignatureHeaderSize
) / CertList
->SignatureSize
;
1681 for (Index
= 0; Index
< CertCount
; Index
++) {
1682 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
1683 + sizeof (EFI_SIGNATURE_LIST
)
1684 + CertList
->SignatureHeaderSize
1685 + Index
* CertList
->SignatureSize
);
1687 // Display GUID and help
1689 GuidToString (&Cert
->SignatureOwner
, GuidStr
, 100);
1690 GuidID
= HiiSetString (PrivateData
->HiiHandle
, 0, GuidStr
, NULL
);
1691 HiiCreateCheckBoxOpCode (
1693 (EFI_QUESTION_ID
) (QuestionIdBase
+ GuidIndex
++),
1698 EFI_IFR_FLAG_CALLBACK
,
1704 ItemDataSize
-= CertList
->SignatureListSize
;
1705 CertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) CertList
+ CertList
->SignatureListSize
);
1710 PrivateData
->HiiHandle
,
1711 &gSecureBootConfigFormSetGuid
,
1717 if (StartOpCodeHandle
!= NULL
) {
1718 HiiFreeOpCodeHandle (StartOpCodeHandle
);
1721 if (EndOpCodeHandle
!= NULL
) {
1722 HiiFreeOpCodeHandle (EndOpCodeHandle
);
1729 if (GuidStr
!= NULL
) {
1737 Delete a KEK entry from KEK database.
1739 @param[in] PrivateData Module's private data.
1740 @param[in] QuestionId Question id of the KEK item to delete.
1742 @retval EFI_SUCCESS Delete kek item successfully.
1743 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
1747 DeleteKeyExchangeKey (
1748 IN SECUREBOOT_CONFIG_PRIVATE_DATA
*PrivateData
,
1749 IN EFI_QUESTION_ID QuestionId
1758 EFI_SIGNATURE_LIST
*CertList
;
1759 EFI_SIGNATURE_LIST
*NewCertList
;
1760 EFI_SIGNATURE_DATA
*Cert
;
1763 BOOLEAN IsKEKItemFound
;
1765 UINTN DeleteKekIndex
;
1773 DeleteKekIndex
= QuestionId
- OPTION_DEL_KEK_QUESTION_ID
;
1776 // Get original KEK variable.
1779 Status
= gRT
->GetVariable (EFI_KEY_EXCHANGE_KEY_NAME
, &gEfiGlobalVariableGuid
, NULL
, &DataSize
, NULL
);
1780 if (EFI_ERROR(Status
) && Status
!= EFI_BUFFER_TOO_SMALL
) {
1784 OldData
= (UINT8
*)AllocateZeroPool(DataSize
);
1785 if (OldData
== NULL
) {
1786 Status
= EFI_OUT_OF_RESOURCES
;
1790 Status
= gRT
->GetVariable (EFI_KEY_EXCHANGE_KEY_NAME
, &gEfiGlobalVariableGuid
, &Attr
, &DataSize
, OldData
);
1791 if (EFI_ERROR(Status
)) {
1796 // Allocate space for new variable.
1798 Data
= (UINT8
*) AllocateZeroPool (DataSize
);
1800 Status
= EFI_OUT_OF_RESOURCES
;
1805 // Enumerate all KEK pub data and erasing the target item.
1807 IsKEKItemFound
= FALSE
;
1808 KekDataSize
= (UINT32
) DataSize
;
1809 CertList
= (EFI_SIGNATURE_LIST
*) OldData
;
1812 while ((KekDataSize
> 0) && (KekDataSize
>= CertList
->SignatureListSize
)) {
1813 if (CompareGuid (&CertList
->SignatureType
, &gEfiCertRsa2048Guid
) ||
1814 CompareGuid (&CertList
->SignatureType
, &gEfiCertX509Guid
)) {
1815 CopyMem (Data
+ Offset
, CertList
, (sizeof(EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
));
1816 NewCertList
= (EFI_SIGNATURE_LIST
*)(Data
+ Offset
);
1817 Offset
+= (sizeof(EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
1818 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
1819 CertCount
= (CertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - CertList
->SignatureHeaderSize
) / CertList
->SignatureSize
;
1820 for (Index
= 0; Index
< CertCount
; Index
++) {
1821 if (GuidIndex
== DeleteKekIndex
) {
1823 // Find it! Skip it!
1825 NewCertList
->SignatureListSize
-= CertList
->SignatureSize
;
1826 IsKEKItemFound
= TRUE
;
1829 // This item doesn't match. Copy it to the Data buffer.
1831 CopyMem (Data
+ Offset
, Cert
, CertList
->SignatureSize
);
1832 Offset
+= CertList
->SignatureSize
;
1835 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) Cert
+ CertList
->SignatureSize
);
1839 // This List doesn't match. Copy it to the Data buffer.
1841 CopyMem (Data
+ Offset
, CertList
, CertList
->SignatureListSize
);
1842 Offset
+= CertList
->SignatureListSize
;
1845 KekDataSize
-= CertList
->SignatureListSize
;
1846 CertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) CertList
+ CertList
->SignatureListSize
);
1849 if (!IsKEKItemFound
) {
1851 // Doesn't find the Kek Item!
1853 Status
= EFI_NOT_FOUND
;
1858 // Delete the Signature header if there is no signature in the list.
1860 KekDataSize
= Offset
;
1861 CertList
= (EFI_SIGNATURE_LIST
*) Data
;
1863 ZeroMem (OldData
, KekDataSize
);
1864 while ((KekDataSize
> 0) && (KekDataSize
>= CertList
->SignatureListSize
)) {
1865 CertCount
= (CertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - CertList
->SignatureHeaderSize
) / CertList
->SignatureSize
;
1866 DEBUG ((DEBUG_ERROR
, " CertCount = %x\n", CertCount
));
1867 if (CertCount
!= 0) {
1868 CopyMem (OldData
+ Offset
, CertList
, CertList
->SignatureListSize
);
1869 Offset
+= CertList
->SignatureListSize
;
1871 KekDataSize
-= CertList
->SignatureListSize
;
1872 CertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) CertList
+ CertList
->SignatureListSize
);
1876 if ((Attr
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) {
1877 Status
= CreateTimeBasedPayload (&DataSize
, &OldData
);
1878 if (EFI_ERROR (Status
)) {
1879 DEBUG ((EFI_D_ERROR
, "Fail to create time-based data payload: %r", Status
));
1884 Status
= gRT
->SetVariable(
1885 EFI_KEY_EXCHANGE_KEY_NAME
,
1886 &gEfiGlobalVariableGuid
,
1891 if (EFI_ERROR (Status
)) {
1892 DEBUG ((DEBUG_ERROR
, "Failed to set variable, Status = %r\n", Status
));
1901 if (OldData
!= NULL
) {
1905 return UpdateDeletePage (
1907 EFI_KEY_EXCHANGE_KEY_NAME
,
1908 &gEfiGlobalVariableGuid
,
1910 FORMID_DELETE_KEK_FORM
,
1911 OPTION_DEL_KEK_QUESTION_ID
1916 Delete a signature entry from siganture database.
1918 @param[in] PrivateData Module's private data.
1919 @param[in] VariableName The variable name of the vendor's signature database.
1920 @param[in] VendorGuid A unique identifier for the vendor.
1921 @param[in] LabelNumber Label number to insert opcodes.
1922 @param[in] FormId Form ID of current page.
1923 @param[in] QuestionIdBase Base question id of the signature list.
1924 @param[in] DeleteIndex Signature index to delete.
1926 @retval EFI_SUCCESS Delete siganture successfully.
1927 @retval EFI_NOT_FOUND Can't find the signature item,
1928 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
1932 IN SECUREBOOT_CONFIG_PRIVATE_DATA
*PrivateData
,
1933 IN CHAR16
*VariableName
,
1934 IN EFI_GUID
*VendorGuid
,
1935 IN UINT16 LabelNumber
,
1936 IN EFI_FORM_ID FormId
,
1937 IN EFI_QUESTION_ID QuestionIdBase
,
1938 IN UINTN DeleteIndex
1947 EFI_SIGNATURE_LIST
*CertList
;
1948 EFI_SIGNATURE_LIST
*NewCertList
;
1949 EFI_SIGNATURE_DATA
*Cert
;
1952 BOOLEAN IsItemFound
;
1953 UINT32 ItemDataSize
;
1963 // Get original signature list data.
1966 Status
= gRT
->GetVariable (VariableName
, VendorGuid
, NULL
, &DataSize
, NULL
);
1967 if (EFI_ERROR (Status
) && Status
!= EFI_BUFFER_TOO_SMALL
) {
1971 OldData
= (UINT8
*) AllocateZeroPool (DataSize
);
1972 if (OldData
== NULL
) {
1973 Status
= EFI_OUT_OF_RESOURCES
;
1977 Status
= gRT
->GetVariable (VariableName
, VendorGuid
, &Attr
, &DataSize
, OldData
);
1978 if (EFI_ERROR(Status
)) {
1983 // Allocate space for new variable.
1985 Data
= (UINT8
*) AllocateZeroPool (DataSize
);
1987 Status
= EFI_OUT_OF_RESOURCES
;
1992 // Enumerate all signature data and erasing the target item.
1994 IsItemFound
= FALSE
;
1995 ItemDataSize
= (UINT32
) DataSize
;
1996 CertList
= (EFI_SIGNATURE_LIST
*) OldData
;
1999 while ((ItemDataSize
> 0) && (ItemDataSize
>= CertList
->SignatureListSize
)) {
2000 if (CompareGuid (&CertList
->SignatureType
, &gEfiCertRsa2048Guid
) ||
2001 CompareGuid (&CertList
->SignatureType
, &gEfiCertX509Guid
) ||
2002 CompareGuid (&CertList
->SignatureType
, &gEfiCertSha1Guid
) ||
2003 CompareGuid (&CertList
->SignatureType
, &gEfiCertSha256Guid
)
2006 // Copy EFI_SIGNATURE_LIST header then calculate the signature count in this list.
2008 CopyMem (Data
+ Offset
, CertList
, (sizeof(EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
));
2009 NewCertList
= (EFI_SIGNATURE_LIST
*) (Data
+ Offset
);
2010 Offset
+= (sizeof(EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
2011 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
2012 CertCount
= (CertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - CertList
->SignatureHeaderSize
) / CertList
->SignatureSize
;
2013 for (Index
= 0; Index
< CertCount
; Index
++) {
2014 if (GuidIndex
== DeleteIndex
) {
2016 // Find it! Skip it!
2018 NewCertList
->SignatureListSize
-= CertList
->SignatureSize
;
2022 // This item doesn't match. Copy it to the Data buffer.
2024 CopyMem (Data
+ Offset
, (UINT8
*)(Cert
), CertList
->SignatureSize
);
2025 Offset
+= CertList
->SignatureSize
;
2028 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) Cert
+ CertList
->SignatureSize
);
2032 // This List doesn't match. Just copy it to the Data buffer.
2034 CopyMem (Data
+ Offset
, (UINT8
*)(CertList
), CertList
->SignatureListSize
);
2035 Offset
+= CertList
->SignatureListSize
;
2038 ItemDataSize
-= CertList
->SignatureListSize
;
2039 CertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) CertList
+ CertList
->SignatureListSize
);
2044 // Doesn't find the signature Item!
2046 Status
= EFI_NOT_FOUND
;
2051 // Delete the EFI_SIGNATURE_LIST header if there is no signature in the list.
2053 ItemDataSize
= Offset
;
2054 CertList
= (EFI_SIGNATURE_LIST
*) Data
;
2056 ZeroMem (OldData
, ItemDataSize
);
2057 while ((ItemDataSize
> 0) && (ItemDataSize
>= CertList
->SignatureListSize
)) {
2058 CertCount
= (CertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - CertList
->SignatureHeaderSize
) / CertList
->SignatureSize
;
2059 DEBUG ((DEBUG_ERROR
, " CertCount = %x\n", CertCount
));
2060 if (CertCount
!= 0) {
2061 CopyMem (OldData
+ Offset
, (UINT8
*)(CertList
), CertList
->SignatureListSize
);
2062 Offset
+= CertList
->SignatureListSize
;
2064 ItemDataSize
-= CertList
->SignatureListSize
;
2065 CertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) CertList
+ CertList
->SignatureListSize
);
2069 if ((Attr
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) {
2070 Status
= CreateTimeBasedPayload (&DataSize
, &OldData
);
2071 if (EFI_ERROR (Status
)) {
2072 DEBUG ((EFI_D_ERROR
, "Fail to create time-based data payload: %r", Status
));
2077 Status
= gRT
->SetVariable(
2084 if (EFI_ERROR (Status
)) {
2085 DEBUG ((DEBUG_ERROR
, "Failed to set variable, Status = %r\n", Status
));
2094 if (OldData
!= NULL
) {
2098 return UpdateDeletePage (
2109 This function extracts configuration from variable.
2111 @param[in, out] ConfigData Point to SecureBoot configuration private data.
2115 SecureBootExtractConfigFromVariable (
2116 IN OUT SECUREBOOT_CONFIGURATION
*ConfigData
2119 UINT8
*SecureBootEnable
;
2122 UINT8
*SecureBootMode
;
2124 SecureBootEnable
= NULL
;
2127 SecureBootMode
= NULL
;
2130 // If the SecureBootEnable Variable doesn't exist, hide the SecureBoot Enable/Disable
2133 GetVariable2 (EFI_SECURE_BOOT_ENABLE_NAME
, &gEfiSecureBootEnableDisableGuid
, (VOID
**)&SecureBootEnable
, NULL
);
2134 if (SecureBootEnable
== NULL
) {
2135 ConfigData
->HideSecureBoot
= TRUE
;
2137 ConfigData
->HideSecureBoot
= FALSE
;
2141 // If it is Physical Presence User, set the PhysicalPresent to true.
2143 if (UserPhysicalPresent()) {
2144 ConfigData
->PhysicalPresent
= TRUE
;
2146 ConfigData
->PhysicalPresent
= FALSE
;
2150 // If there is no PK then the Delete Pk button will be gray.
2152 GetVariable2 (EFI_SETUP_MODE_NAME
, &gEfiGlobalVariableGuid
, (VOID
**)&SetupMode
, NULL
);
2153 if (SetupMode
== NULL
|| (*SetupMode
) == SETUP_MODE
) {
2154 ConfigData
->HasPk
= FALSE
;
2156 ConfigData
->HasPk
= TRUE
;
2160 // If the value of SecureBoot variable is 1, the platform is operating in secure boot mode.
2162 GetVariable2 (EFI_SECURE_BOOT_MODE_NAME
, &gEfiGlobalVariableGuid
, (VOID
**)&SecureBoot
, NULL
);
2163 if (SecureBoot
!= NULL
&& *SecureBoot
== SECURE_BOOT_MODE_ENABLE
) {
2164 ConfigData
->SecureBootState
= TRUE
;
2166 ConfigData
->SecureBootState
= FALSE
;
2170 // Get the SecureBootMode from CustomMode variable.
2172 GetVariable2 (EFI_CUSTOM_MODE_NAME
, &gEfiCustomModeEnableGuid
, (VOID
**)&SecureBootMode
, NULL
);
2173 if (SecureBootMode
== NULL
) {
2174 ConfigData
->SecureBootMode
= STANDARD_SECURE_BOOT_MODE
;
2176 ConfigData
->SecureBootMode
= *(SecureBootMode
);
2182 This function allows a caller to extract the current configuration for one
2183 or more named elements from the target driver.
2185 @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
2186 @param[in] Request A null-terminated Unicode string in
2187 <ConfigRequest> format.
2188 @param[out] Progress On return, points to a character in the Request
2189 string. Points to the string's null terminator if
2190 request was successful. Points to the most recent
2191 '&' before the first failing name/value pair (or
2192 the beginning of the string if the failure is in
2193 the first name/value pair) if the request was not
2195 @param[out] Results A null-terminated Unicode string in
2196 <ConfigAltResp> format which has all values filled
2197 in for the names in the Request string. String to
2198 be allocated by the called function.
2200 @retval EFI_SUCCESS The Results is filled with the requested values.
2201 @retval EFI_OUT_OF_RESOURCES Not enough memory to store the results.
2202 @retval EFI_INVALID_PARAMETER Request is illegal syntax, or unknown name.
2203 @retval EFI_NOT_FOUND Routing data doesn't match any storage in this
2209 SecureBootExtractConfig (
2210 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL
*This
,
2211 IN CONST EFI_STRING Request
,
2212 OUT EFI_STRING
*Progress
,
2213 OUT EFI_STRING
*Results
2219 SECUREBOOT_CONFIGURATION Configuration
;
2220 EFI_STRING ConfigRequest
;
2221 EFI_STRING ConfigRequestHdr
;
2222 SECUREBOOT_CONFIG_PRIVATE_DATA
*PrivateData
;
2223 BOOLEAN AllocatedRequest
;
2225 if (Progress
== NULL
|| Results
== NULL
) {
2226 return EFI_INVALID_PARAMETER
;
2229 AllocatedRequest
= FALSE
;
2230 ConfigRequestHdr
= NULL
;
2231 ConfigRequest
= NULL
;
2234 ZeroMem (&Configuration
, sizeof (Configuration
));
2235 PrivateData
= SECUREBOOT_CONFIG_PRIVATE_FROM_THIS (This
);
2236 *Progress
= Request
;
2238 if ((Request
!= NULL
) && !HiiIsConfigHdrMatch (Request
, &gSecureBootConfigFormSetGuid
, mSecureBootStorageName
)) {
2239 return EFI_NOT_FOUND
;
2243 // Get Configuration from Variable.
2245 SecureBootExtractConfigFromVariable (&Configuration
);
2247 BufferSize
= sizeof (SECUREBOOT_CONFIGURATION
);
2248 ConfigRequest
= Request
;
2249 if ((Request
== NULL
) || (StrStr (Request
, L
"OFFSET") == NULL
)) {
2251 // Request is set to NULL or OFFSET is NULL, construct full request string.
2253 // Allocate and fill a buffer large enough to hold the <ConfigHdr> template
2254 // followed by "&OFFSET=0&WIDTH=WWWWWWWWWWWWWWWW" followed by a Null-terminator
2256 ConfigRequestHdr
= HiiConstructConfigHdr (&gSecureBootConfigFormSetGuid
, mSecureBootStorageName
, PrivateData
->DriverHandle
);
2257 Size
= (StrLen (ConfigRequestHdr
) + 32 + 1) * sizeof (CHAR16
);
2258 ConfigRequest
= AllocateZeroPool (Size
);
2259 ASSERT (ConfigRequest
!= NULL
);
2260 AllocatedRequest
= TRUE
;
2261 UnicodeSPrint (ConfigRequest
, Size
, L
"%s&OFFSET=0&WIDTH=%016LX", ConfigRequestHdr
, (UINT64
)BufferSize
);
2262 FreePool (ConfigRequestHdr
);
2263 ConfigRequestHdr
= NULL
;
2266 Status
= gHiiConfigRouting
->BlockToConfig (
2269 (UINT8
*) &Configuration
,
2276 // Free the allocated config request string.
2278 if (AllocatedRequest
) {
2279 FreePool (ConfigRequest
);
2283 // Set Progress string to the original request string.
2285 if (Request
== NULL
) {
2287 } else if (StrStr (Request
, L
"OFFSET") == NULL
) {
2288 *Progress
= Request
+ StrLen (Request
);
2295 This function processes the results of changes in configuration.
2297 @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
2298 @param[in] Configuration A null-terminated Unicode string in <ConfigResp>
2300 @param[out] Progress A pointer to a string filled in with the offset of
2301 the most recent '&' before the first failing
2302 name/value pair (or the beginning of the string if
2303 the failure is in the first name/value pair) or
2304 the terminating NULL if all was successful.
2306 @retval EFI_SUCCESS The Results is processed successfully.
2307 @retval EFI_INVALID_PARAMETER Configuration is NULL.
2308 @retval EFI_NOT_FOUND Routing data doesn't match any storage in this
2314 SecureBootRouteConfig (
2315 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL
*This
,
2316 IN CONST EFI_STRING Configuration
,
2317 OUT EFI_STRING
*Progress
2320 if (Configuration
== NULL
|| Progress
== NULL
) {
2321 return EFI_INVALID_PARAMETER
;
2324 *Progress
= Configuration
;
2325 if (!HiiIsConfigHdrMatch (Configuration
, &gSecureBootConfigFormSetGuid
, mSecureBootStorageName
)) {
2326 return EFI_NOT_FOUND
;
2329 *Progress
= Configuration
+ StrLen (Configuration
);
2334 This function is called to provide results data to the driver.
2336 @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
2337 @param[in] Action Specifies the type of action taken by the browser.
2338 @param[in] QuestionId A unique value which is sent to the original
2339 exporting driver so that it can identify the type
2341 @param[in] Type The type of value for the question.
2342 @param[in] Value A pointer to the data being sent to the original
2344 @param[out] ActionRequest On return, points to the action requested by the
2347 @retval EFI_SUCCESS The callback successfully handled the action.
2348 @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the
2349 variable and its data.
2350 @retval EFI_DEVICE_ERROR The variable could not be saved.
2351 @retval EFI_UNSUPPORTED The specified Action is not supported by the
2357 SecureBootCallback (
2358 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL
*This
,
2359 IN EFI_BROWSER_ACTION Action
,
2360 IN EFI_QUESTION_ID QuestionId
,
2362 IN EFI_IFR_TYPE_VALUE
*Value
,
2363 OUT EFI_BROWSER_ACTION_REQUEST
*ActionRequest
2368 SECUREBOOT_CONFIG_PRIVATE_DATA
*Private
;
2370 SECUREBOOT_CONFIGURATION
*IfrNvData
;
2372 UINT8
*SecureBootEnable
;
2373 CHAR16 PromptString
[100];
2375 SecureBootEnable
= NULL
;
2377 if ((This
== NULL
) || (Value
== NULL
) || (ActionRequest
== NULL
)) {
2378 return EFI_INVALID_PARAMETER
;
2381 if ((Action
!= EFI_BROWSER_ACTION_CHANGED
) && (Action
!= EFI_BROWSER_ACTION_CHANGING
)) {
2382 return EFI_UNSUPPORTED
;
2385 Private
= SECUREBOOT_CONFIG_PRIVATE_FROM_THIS (This
);
2388 // Retrieve uncommitted data from Browser
2390 BufferSize
= sizeof (SECUREBOOT_CONFIGURATION
);
2391 IfrNvData
= AllocateZeroPool (BufferSize
);
2392 if (IfrNvData
== NULL
) {
2393 return EFI_OUT_OF_RESOURCES
;
2396 Status
= EFI_SUCCESS
;
2398 HiiGetBrowserData (NULL
, NULL
, BufferSize
, (UINT8
*) IfrNvData
);
2400 if (Action
== EFI_BROWSER_ACTION_CHANGING
) {
2402 switch (QuestionId
) {
2403 case KEY_SECURE_BOOT_ENABLE
:
2404 GetVariable2 (EFI_SECURE_BOOT_ENABLE_NAME
, &gEfiSecureBootEnableDisableGuid
, (VOID
**)&SecureBootEnable
, NULL
);
2405 if (NULL
!= SecureBootEnable
) {
2406 if (EFI_ERROR (SaveSecureBootVariable (Value
->u8
))) {
2408 EFI_LIGHTGRAY
| EFI_BACKGROUND_BLUE
,
2410 L
"Only Physical Presence User could disable secure boot!",
2413 Status
= EFI_UNSUPPORTED
;
2415 *ActionRequest
= EFI_BROWSER_ACTION_REQUEST_FORM_APPLY
;
2419 case KEY_SECURE_BOOT_OPTION
:
2420 FreeMenu (&DirectoryMenu
);
2421 FreeMenu (&FsOptionMenu
);
2424 case KEY_SECURE_BOOT_KEK_OPTION
:
2425 case KEY_SECURE_BOOT_DB_OPTION
:
2426 case KEY_SECURE_BOOT_DBX_OPTION
:
2428 // Clear Signature GUID.
2430 ZeroMem (IfrNvData
->SignatureGuid
, sizeof (IfrNvData
->SignatureGuid
));
2431 if (Private
->SignatureGUID
== NULL
) {
2432 Private
->SignatureGUID
= (EFI_GUID
*) AllocateZeroPool (sizeof (EFI_GUID
));
2433 if (Private
->SignatureGUID
== NULL
) {
2434 return EFI_OUT_OF_RESOURCES
;
2438 if (QuestionId
== KEY_SECURE_BOOT_DB_OPTION
) {
2439 LabelId
= SECUREBOOT_ENROLL_SIGNATURE_TO_DB
;
2440 } else if (QuestionId
== KEY_SECURE_BOOT_DBX_OPTION
) {
2441 LabelId
= SECUREBOOT_ENROLL_SIGNATURE_TO_DBX
;
2443 LabelId
= FORMID_ENROLL_KEK_FORM
;
2447 // Refresh selected file.
2449 CleanUpPage (LabelId
, Private
);
2452 case SECUREBOOT_ADD_PK_FILE_FORM_ID
:
2453 case FORMID_ENROLL_KEK_FORM
:
2454 case SECUREBOOT_ENROLL_SIGNATURE_TO_DB
:
2455 case SECUREBOOT_ENROLL_SIGNATURE_TO_DBX
:
2456 if (QuestionId
== SECUREBOOT_ADD_PK_FILE_FORM_ID
) {
2457 Private
->FeCurrentState
= FileExplorerStateEnrollPkFile
;
2458 } else if (QuestionId
== FORMID_ENROLL_KEK_FORM
) {
2459 Private
->FeCurrentState
= FileExplorerStateEnrollKekFile
;
2460 } else if (QuestionId
== SECUREBOOT_ENROLL_SIGNATURE_TO_DB
) {
2461 Private
->FeCurrentState
= FileExplorerStateEnrollSignatureFileToDb
;
2463 Private
->FeCurrentState
= FileExplorerStateEnrollSignatureFileToDbx
;
2466 Private
->FeDisplayContext
= FileExplorerDisplayUnknown
;
2467 CleanUpPage (FORM_FILE_EXPLORER_ID
, Private
);
2468 UpdateFileExplorer (Private
, 0);
2471 case KEY_SECURE_BOOT_DELETE_PK
:
2473 Status
= DeletePlatformKey ();
2474 *ActionRequest
= EFI_BROWSER_ACTION_REQUEST_FORM_APPLY
;
2478 case KEY_DELETE_KEK
:
2481 EFI_KEY_EXCHANGE_KEY_NAME
,
2482 &gEfiGlobalVariableGuid
,
2484 FORMID_DELETE_KEK_FORM
,
2485 OPTION_DEL_KEK_QUESTION_ID
2489 case SECUREBOOT_DELETE_SIGNATURE_FROM_DB
:
2492 EFI_IMAGE_SECURITY_DATABASE
,
2493 &gEfiImageSecurityDatabaseGuid
,
2495 SECUREBOOT_DELETE_SIGNATURE_FROM_DB
,
2496 OPTION_DEL_DB_QUESTION_ID
2500 case SECUREBOOT_DELETE_SIGNATURE_FROM_DBX
:
2503 EFI_IMAGE_SECURITY_DATABASE1
,
2504 &gEfiImageSecurityDatabaseGuid
,
2506 SECUREBOOT_DELETE_SIGNATURE_FROM_DBX
,
2507 OPTION_DEL_DBX_QUESTION_ID
2512 case KEY_VALUE_SAVE_AND_EXIT_KEK
:
2513 Status
= EnrollKeyExchangeKey (Private
);
2516 case KEY_VALUE_SAVE_AND_EXIT_DB
:
2517 Status
= EnrollSignatureDatabase (Private
, EFI_IMAGE_SECURITY_DATABASE
);
2520 case KEY_VALUE_SAVE_AND_EXIT_DBX
:
2521 Status
= EnrollSignatureDatabase (Private
, EFI_IMAGE_SECURITY_DATABASE1
);
2525 if (QuestionId
>= FILE_OPTION_OFFSET
) {
2526 UpdateFileExplorer (Private
, QuestionId
);
2527 } else if ((QuestionId
>= OPTION_DEL_KEK_QUESTION_ID
) &&
2528 (QuestionId
< (OPTION_DEL_KEK_QUESTION_ID
+ OPTION_CONFIG_RANGE
))) {
2529 DeleteKeyExchangeKey (Private
, QuestionId
);
2530 } else if ((QuestionId
>= OPTION_DEL_DB_QUESTION_ID
) &&
2531 (QuestionId
< (OPTION_DEL_DB_QUESTION_ID
+ OPTION_CONFIG_RANGE
))) {
2534 EFI_IMAGE_SECURITY_DATABASE
,
2535 &gEfiImageSecurityDatabaseGuid
,
2537 SECUREBOOT_DELETE_SIGNATURE_FROM_DB
,
2538 OPTION_DEL_DB_QUESTION_ID
,
2539 QuestionId
- OPTION_DEL_DB_QUESTION_ID
2541 } else if ((QuestionId
>= OPTION_DEL_DBX_QUESTION_ID
) &&
2542 (QuestionId
< (OPTION_DEL_DBX_QUESTION_ID
+ OPTION_CONFIG_RANGE
))) {
2545 EFI_IMAGE_SECURITY_DATABASE1
,
2546 &gEfiImageSecurityDatabaseGuid
,
2548 SECUREBOOT_DELETE_SIGNATURE_FROM_DBX
,
2549 OPTION_DEL_DBX_QUESTION_ID
,
2550 QuestionId
- OPTION_DEL_DBX_QUESTION_ID
2555 } else if (Action
== EFI_BROWSER_ACTION_CHANGED
) {
2556 switch (QuestionId
) {
2557 case KEY_SECURE_BOOT_ENABLE
:
2558 *ActionRequest
= EFI_BROWSER_ACTION_REQUEST_SUBMIT
;
2560 case KEY_VALUE_SAVE_AND_EXIT_PK
:
2561 Status
= EnrollPlatformKey (Private
);
2564 sizeof (PromptString
),
2565 L
"Only DER encoded certificate file (%s) is supported.",
2568 if (EFI_ERROR (Status
)) {
2570 EFI_LIGHTGRAY
| EFI_BACKGROUND_BLUE
,
2572 L
"ERROR: Unsupported file type!",
2577 *ActionRequest
= EFI_BROWSER_ACTION_REQUEST_SUBMIT
;
2581 case KEY_VALUE_NO_SAVE_AND_EXIT_PK
:
2582 case KEY_VALUE_NO_SAVE_AND_EXIT_KEK
:
2583 case KEY_VALUE_NO_SAVE_AND_EXIT_DB
:
2584 case KEY_VALUE_NO_SAVE_AND_EXIT_DBX
:
2585 if (Private
->FileContext
->FHandle
!= NULL
) {
2586 CloseFile (Private
->FileContext
->FHandle
);
2587 Private
->FileContext
->FHandle
= NULL
;
2588 Private
->FileContext
->FileName
= NULL
;
2591 if (Private
->SignatureGUID
!= NULL
) {
2592 FreePool (Private
->SignatureGUID
);
2593 Private
->SignatureGUID
= NULL
;
2595 *ActionRequest
= EFI_BROWSER_ACTION_REQUEST_EXIT
;
2598 case KEY_SECURE_BOOT_MODE
:
2599 GetVariable2 (EFI_CUSTOM_MODE_NAME
, &gEfiCustomModeEnableGuid
, (VOID
**)&SecureBootEnable
, NULL
);
2600 if (NULL
!= SecureBootEnable
) {
2601 Status
= gRT
->SetVariable (
2602 EFI_CUSTOM_MODE_NAME
,
2603 &gEfiCustomModeEnableGuid
,
2604 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
2608 *ActionRequest
= EFI_BROWSER_ACTION_REQUEST_FORM_APPLY
;
2609 IfrNvData
->SecureBootMode
= Value
->u8
;
2613 case KEY_SECURE_BOOT_KEK_GUID
:
2614 case KEY_SECURE_BOOT_SIGNATURE_GUID_DB
:
2615 case KEY_SECURE_BOOT_SIGNATURE_GUID_DBX
:
2616 ASSERT (Private
->SignatureGUID
!= NULL
);
2617 Status
= StringToGuid (
2618 IfrNvData
->SignatureGuid
,
2619 StrLen (IfrNvData
->SignatureGuid
),
2620 Private
->SignatureGUID
2622 if (EFI_ERROR (Status
)) {
2626 *ActionRequest
= EFI_BROWSER_ACTION_REQUEST_FORM_APPLY
;
2629 case KEY_SECURE_BOOT_DELETE_PK
:
2631 *ActionRequest
= EFI_BROWSER_ACTION_REQUEST_SUBMIT
;
2637 if (!EFI_ERROR (Status
)) {
2638 BufferSize
= sizeof (SECUREBOOT_CONFIGURATION
);
2639 HiiSetBrowserData (NULL
, NULL
, BufferSize
, (UINT8
*) IfrNvData
, NULL
);
2641 FreePool (IfrNvData
);
2647 This function publish the SecureBoot configuration Form.
2649 @param[in, out] PrivateData Points to SecureBoot configuration private data.
2651 @retval EFI_SUCCESS HII Form is installed successfully.
2652 @retval EFI_OUT_OF_RESOURCES Not enough resource for HII Form installation.
2653 @retval Others Other errors as indicated.
2657 InstallSecureBootConfigForm (
2658 IN OUT SECUREBOOT_CONFIG_PRIVATE_DATA
*PrivateData
2662 EFI_HII_HANDLE HiiHandle
;
2663 EFI_HANDLE DriverHandle
;
2664 EFI_HII_CONFIG_ACCESS_PROTOCOL
*ConfigAccess
;
2666 DriverHandle
= NULL
;
2667 ConfigAccess
= &PrivateData
->ConfigAccess
;
2668 Status
= gBS
->InstallMultipleProtocolInterfaces (
2670 &gEfiDevicePathProtocolGuid
,
2671 &mSecureBootHiiVendorDevicePath
,
2672 &gEfiHiiConfigAccessProtocolGuid
,
2676 if (EFI_ERROR (Status
)) {
2680 PrivateData
->DriverHandle
= DriverHandle
;
2683 // Publish the HII package list
2685 HiiHandle
= HiiAddPackages (
2686 &gSecureBootConfigFormSetGuid
,
2688 SecureBootConfigDxeStrings
,
2689 SecureBootConfigBin
,
2692 if (HiiHandle
== NULL
) {
2693 gBS
->UninstallMultipleProtocolInterfaces (
2695 &gEfiDevicePathProtocolGuid
,
2696 &mSecureBootHiiVendorDevicePath
,
2697 &gEfiHiiConfigAccessProtocolGuid
,
2701 return EFI_OUT_OF_RESOURCES
;
2704 PrivateData
->HiiHandle
= HiiHandle
;
2706 PrivateData
->FileContext
= AllocateZeroPool (sizeof (SECUREBOOT_FILE_CONTEXT
));
2707 PrivateData
->MenuEntry
= AllocateZeroPool (sizeof (SECUREBOOT_MENU_ENTRY
));
2709 if (PrivateData
->FileContext
== NULL
|| PrivateData
->MenuEntry
== NULL
) {
2710 UninstallSecureBootConfigForm (PrivateData
);
2711 return EFI_OUT_OF_RESOURCES
;
2714 PrivateData
->FeCurrentState
= FileExplorerStateInActive
;
2715 PrivateData
->FeDisplayContext
= FileExplorerDisplayUnknown
;
2717 InitializeListHead (&FsOptionMenu
.Head
);
2718 InitializeListHead (&DirectoryMenu
.Head
);
2721 // Init OpCode Handle and Allocate space for creation of Buffer
2723 mStartOpCodeHandle
= HiiAllocateOpCodeHandle ();
2724 if (mStartOpCodeHandle
== NULL
) {
2725 UninstallSecureBootConfigForm (PrivateData
);
2726 return EFI_OUT_OF_RESOURCES
;
2729 mEndOpCodeHandle
= HiiAllocateOpCodeHandle ();
2730 if (mEndOpCodeHandle
== NULL
) {
2731 UninstallSecureBootConfigForm (PrivateData
);
2732 return EFI_OUT_OF_RESOURCES
;
2736 // Create Hii Extend Label OpCode as the start opcode
2738 mStartLabel
= (EFI_IFR_GUID_LABEL
*) HiiCreateGuidOpCode (
2742 sizeof (EFI_IFR_GUID_LABEL
)
2744 mStartLabel
->ExtendOpCode
= EFI_IFR_EXTEND_OP_LABEL
;
2747 // Create Hii Extend Label OpCode as the end opcode
2749 mEndLabel
= (EFI_IFR_GUID_LABEL
*) HiiCreateGuidOpCode (
2753 sizeof (EFI_IFR_GUID_LABEL
)
2755 mEndLabel
->ExtendOpCode
= EFI_IFR_EXTEND_OP_LABEL
;
2756 mEndLabel
->Number
= LABEL_END
;
2762 This function removes SecureBoot configuration Form.
2764 @param[in, out] PrivateData Points to SecureBoot configuration private data.
2768 UninstallSecureBootConfigForm (
2769 IN OUT SECUREBOOT_CONFIG_PRIVATE_DATA
*PrivateData
2773 // Uninstall HII package list
2775 if (PrivateData
->HiiHandle
!= NULL
) {
2776 HiiRemovePackages (PrivateData
->HiiHandle
);
2777 PrivateData
->HiiHandle
= NULL
;
2781 // Uninstall HII Config Access Protocol
2783 if (PrivateData
->DriverHandle
!= NULL
) {
2784 gBS
->UninstallMultipleProtocolInterfaces (
2785 PrivateData
->DriverHandle
,
2786 &gEfiDevicePathProtocolGuid
,
2787 &mSecureBootHiiVendorDevicePath
,
2788 &gEfiHiiConfigAccessProtocolGuid
,
2789 &PrivateData
->ConfigAccess
,
2792 PrivateData
->DriverHandle
= NULL
;
2795 if (PrivateData
->SignatureGUID
!= NULL
) {
2796 FreePool (PrivateData
->SignatureGUID
);
2799 if (PrivateData
->MenuEntry
!= NULL
) {
2800 FreePool (PrivateData
->MenuEntry
);
2803 if (PrivateData
->FileContext
!= NULL
) {
2804 FreePool (PrivateData
->FileContext
);
2807 FreePool (PrivateData
);
2809 FreeMenu (&DirectoryMenu
);
2810 FreeMenu (&FsOptionMenu
);
2812 if (mStartOpCodeHandle
!= NULL
) {
2813 HiiFreeOpCodeHandle (mStartOpCodeHandle
);
2816 if (mEndOpCodeHandle
!= NULL
) {
2817 HiiFreeOpCodeHandle (mEndOpCodeHandle
);