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 DER encoded X.509 certificate files (*.cer or *.der).
352 FilePostFix
= Private
->FileContext
->FileName
+ StrLen (Private
->FileContext
->FileName
) - 4;
353 if ((CompareMem (FilePostFix
, L
".cer",4) != 0) && (CompareMem (FilePostFix
, L
".der",4) != 0)) {
354 DEBUG ((EFI_D_ERROR
, "Unsupported file type, only DER encoded certificate file (*.cer or *.der) 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 if (mNtHeader
.Pe32
->FileHeader
.Machine
== IMAGE_FILE_MACHINE_IA64
&& mNtHeader
.Pe32
->OptionalHeader
.Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
1021 // NOTE: Some versions of Linux ELILO for Itanium have an incorrect magic value
1022 // in the PE/COFF Header. If the MachineType is Itanium(IA64) and the
1023 // Magic value in the OptionalHeader is EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
1024 // then override the magic value to EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
1026 Magic
= EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
;
1029 // Get the magic value from the PE/COFF Optional Header
1031 Magic
= mNtHeader
.Pe32
->OptionalHeader
.Magic
;
1035 // 3. Calculate the distance from the base of the image header to the image checksum address.
1036 // 4. Hash the image header from its base to beginning of the image checksum.
1038 HashBase
= mImageBase
;
1039 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
1043 HashSize
= (UINTN
) ((UINT8
*) (&mNtHeader
.Pe32
->OptionalHeader
.CheckSum
) - HashBase
);
1046 // Use PE32+ offset.
1048 HashSize
= (UINTN
) ((UINT8
*) (&mNtHeader
.Pe32Plus
->OptionalHeader
.CheckSum
) - HashBase
);
1051 Status
= mHash
[HashAlg
].HashUpdate(HashCtx
, HashBase
, HashSize
);
1056 // 5. Skip over the image checksum (it occupies a single ULONG).
1057 // 6. Get the address of the beginning of the Cert Directory.
1058 // 7. Hash everything from the end of the checksum to the start of the Cert Directory.
1060 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
1064 HashBase
= (UINT8
*) &mNtHeader
.Pe32
->OptionalHeader
.CheckSum
+ sizeof (UINT32
);
1065 HashSize
= (UINTN
) ((UINT8
*) (&mNtHeader
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
]) - HashBase
);
1068 // Use PE32+ offset.
1070 HashBase
= (UINT8
*) &mNtHeader
.Pe32Plus
->OptionalHeader
.CheckSum
+ sizeof (UINT32
);
1071 HashSize
= (UINTN
) ((UINT8
*) (&mNtHeader
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
]) - HashBase
);
1074 Status
= mHash
[HashAlg
].HashUpdate(HashCtx
, HashBase
, HashSize
);
1079 // 8. Skip over the Cert Directory. (It is sizeof(IMAGE_DATA_DIRECTORY) bytes.)
1080 // 9. Hash everything from the end of the Cert Directory to the end of image header.
1082 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
1086 HashBase
= (UINT8
*) &mNtHeader
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
+ 1];
1087 HashSize
= mNtHeader
.Pe32
->OptionalHeader
.SizeOfHeaders
- (UINTN
) ((UINT8
*) (&mNtHeader
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
+ 1]) - mImageBase
);
1090 // Use PE32+ offset.
1092 HashBase
= (UINT8
*) &mNtHeader
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
+ 1];
1093 HashSize
= mNtHeader
.Pe32Plus
->OptionalHeader
.SizeOfHeaders
- (UINTN
) ((UINT8
*) (&mNtHeader
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
+ 1]) - mImageBase
);
1096 Status
= mHash
[HashAlg
].HashUpdate(HashCtx
, HashBase
, HashSize
);
1101 // 10. Set the SUM_OF_BYTES_HASHED to the size of the header.
1103 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
1107 SumOfBytesHashed
= mNtHeader
.Pe32
->OptionalHeader
.SizeOfHeaders
;
1112 SumOfBytesHashed
= mNtHeader
.Pe32Plus
->OptionalHeader
.SizeOfHeaders
;
1116 // 11. Build a temporary table of pointers to all the IMAGE_SECTION_HEADER
1117 // structures in the image. The 'NumberOfSections' field of the image
1118 // header indicates how big the table should be. Do not include any
1119 // IMAGE_SECTION_HEADERs in the table whose 'SizeOfRawData' field is zero.
1121 SectionHeader
= (EFI_IMAGE_SECTION_HEADER
*) AllocateZeroPool (sizeof (EFI_IMAGE_SECTION_HEADER
) * mNtHeader
.Pe32
->FileHeader
.NumberOfSections
);
1122 ASSERT (SectionHeader
!= NULL
);
1124 // 12. Using the 'PointerToRawData' in the referenced section headers as
1125 // a key, arrange the elements in the table in ascending order. In other
1126 // words, sort the section headers according to the disk-file offset of
1129 Section
= (EFI_IMAGE_SECTION_HEADER
*) (
1131 mPeCoffHeaderOffset
+
1133 sizeof (EFI_IMAGE_FILE_HEADER
) +
1134 mNtHeader
.Pe32
->FileHeader
.SizeOfOptionalHeader
1136 for (Index
= 0; Index
< mNtHeader
.Pe32
->FileHeader
.NumberOfSections
; Index
++) {
1138 while ((Pos
> 0) && (Section
->PointerToRawData
< SectionHeader
[Pos
- 1].PointerToRawData
)) {
1139 CopyMem (&SectionHeader
[Pos
], &SectionHeader
[Pos
- 1], sizeof (EFI_IMAGE_SECTION_HEADER
));
1142 CopyMem (&SectionHeader
[Pos
], Section
, sizeof (EFI_IMAGE_SECTION_HEADER
));
1147 // 13. Walk through the sorted table, bring the corresponding section
1148 // into memory, and hash the entire section (using the 'SizeOfRawData'
1149 // field in the section header to determine the amount of data to hash).
1150 // 14. Add the section's 'SizeOfRawData' to SUM_OF_BYTES_HASHED .
1151 // 15. Repeat steps 13 and 14 for all the sections in the sorted table.
1153 for (Index
= 0; Index
< mNtHeader
.Pe32
->FileHeader
.NumberOfSections
; Index
++) {
1154 Section
= &SectionHeader
[Index
];
1155 if (Section
->SizeOfRawData
== 0) {
1158 HashBase
= mImageBase
+ Section
->PointerToRawData
;
1159 HashSize
= (UINTN
) Section
->SizeOfRawData
;
1161 Status
= mHash
[HashAlg
].HashUpdate(HashCtx
, HashBase
, HashSize
);
1166 SumOfBytesHashed
+= HashSize
;
1170 // 16. If the file size is greater than SUM_OF_BYTES_HASHED, there is extra
1171 // data in the file that needs to be added to the hash. This data begins
1172 // at file offset SUM_OF_BYTES_HASHED and its length is:
1173 // FileSize - (CertDirectory->Size)
1175 if (mImageSize
> SumOfBytesHashed
) {
1176 HashBase
= mImageBase
+ SumOfBytesHashed
;
1177 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
1183 mNtHeader
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
].Size
-
1187 // Use PE32+ offset.
1191 mNtHeader
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
].Size
-
1195 Status
= mHash
[HashAlg
].HashUpdate(HashCtx
, HashBase
, HashSize
);
1201 Status
= mHash
[HashAlg
].HashFinal(HashCtx
, mImageDigest
);
1204 if (HashCtx
!= NULL
) {
1207 if (SectionHeader
!= NULL
) {
1208 FreePool (SectionHeader
);
1214 Recognize the Hash algorithm in PE/COFF Authenticode and caculate hash of
1215 Pe/Coff image based on the authenticated image hashing in PE/COFF Specification
1218 @retval EFI_UNSUPPORTED Hash algorithm is not supported.
1219 @retval EFI_SUCCESS Hash successfully.
1228 WIN_CERTIFICATE_EFI_PKCS
*PkcsCertData
;
1230 PkcsCertData
= (WIN_CERTIFICATE_EFI_PKCS
*) (mImageBase
+ mSecDataDir
->Offset
);
1232 for (Index
= 0; Index
< HASHALG_MAX
; Index
++) {
1234 // Check the Hash algorithm in PE/COFF Authenticode.
1235 // According to PKCS#7 Definition:
1236 // SignedData ::= SEQUENCE {
1238 // digestAlgorithms DigestAlgorithmIdentifiers,
1239 // contentInfo ContentInfo,
1241 // The DigestAlgorithmIdentifiers can be used to determine the hash algorithm in PE/COFF hashing
1242 // This field has the fixed offset (+32) in final Authenticode ASN.1 data.
1243 // Fixed offset (+32) is calculated based on two bytes of length encoding.
1245 if ((*(PkcsCertData
->CertData
+ 1) & TWO_BYTE_ENCODE
) != TWO_BYTE_ENCODE
) {
1247 // Only support two bytes of Long Form of Length Encoding.
1253 if (CompareMem (PkcsCertData
->CertData
+ 32, mHash
[Index
].OidValue
, mHash
[Index
].OidLength
) == 0) {
1258 if (Index
== HASHALG_MAX
) {
1259 return EFI_UNSUPPORTED
;
1263 // HASH PE Image based on Hash algorithm in PE/COFF Authenticode.
1265 if (!HashPeImage(Index
)) {
1266 return EFI_UNSUPPORTED
;
1273 Enroll a new executable's signature into Signature Database.
1275 @param[in] PrivateData The module's private data.
1276 @param[in] VariableName Variable name of signature database, must be
1277 EFI_IMAGE_SECURITY_DATABASE or EFI_IMAGE_SECURITY_DATABASE1.
1279 @retval EFI_SUCCESS New signature is enrolled successfully.
1280 @retval EFI_INVALID_PARAMETER The parameter is invalid.
1281 @retval EFI_UNSUPPORTED Unsupported command.
1282 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
1286 EnrollImageSignatureToSigDB (
1287 IN SECUREBOOT_CONFIG_PRIVATE_DATA
*Private
,
1288 IN CHAR16
*VariableName
1292 EFI_SIGNATURE_LIST
*SigDBCert
;
1293 EFI_SIGNATURE_DATA
*SigDBCertData
;
1298 WIN_CERTIFICATE_UEFI_GUID
*GuidCertData
;
1301 GuidCertData
= NULL
;
1304 // Form the SigDB certificate list.
1305 // Format the data item into EFI_SIGNATURE_LIST type.
1307 // We need to parse executable's signature data from specified signed executable file.
1308 // In current implementation, we simply trust the pass-in signed executable file.
1309 // In reality, it's OS's responsibility to verify the signed executable file.
1313 // Read the whole file content
1315 Status
= ReadFileContent(
1316 Private
->FileContext
->FHandle
,
1317 (VOID
**) &mImageBase
,
1321 if (EFI_ERROR (Status
)) {
1324 ASSERT (mImageBase
!= NULL
);
1326 Status
= LoadPeImage ();
1327 if (EFI_ERROR (Status
)) {
1331 if (mSecDataDir
->SizeOfCert
== 0) {
1332 if (!HashPeImage (HASHALG_SHA256
)) {
1333 Status
= EFI_SECURITY_VIOLATION
;
1339 // Read the certificate data
1341 mCertificate
= (WIN_CERTIFICATE
*)(mImageBase
+ mSecDataDir
->Offset
);
1343 if (mCertificate
->wCertificateType
== WIN_CERT_TYPE_EFI_GUID
) {
1344 GuidCertData
= (WIN_CERTIFICATE_UEFI_GUID
*) mCertificate
;
1345 if (CompareMem (&GuidCertData
->CertType
, &gEfiCertTypeRsa2048Sha256Guid
, sizeof(EFI_GUID
)) != 0) {
1346 Status
= EFI_ABORTED
;
1350 if (!HashPeImage (HASHALG_SHA256
)) {
1351 Status
= EFI_ABORTED
;
1355 } else if (mCertificate
->wCertificateType
== WIN_CERT_TYPE_PKCS_SIGNED_DATA
) {
1357 Status
= HashPeImageByType ();
1358 if (EFI_ERROR (Status
)) {
1362 Status
= EFI_ABORTED
;
1368 // Create a new SigDB entry.
1370 SigDBSize
= sizeof(EFI_SIGNATURE_LIST
)
1371 + sizeof(EFI_SIGNATURE_DATA
) - 1
1372 + (UINT32
) mImageDigestSize
;
1374 Data
= (UINT8
*) AllocateZeroPool (SigDBSize
);
1376 Status
= EFI_OUT_OF_RESOURCES
;
1381 // Adjust the Certificate Database parameters.
1383 SigDBCert
= (EFI_SIGNATURE_LIST
*) Data
;
1384 SigDBCert
->SignatureListSize
= (UINT32
) SigDBSize
;
1385 SigDBCert
->SignatureHeaderSize
= 0;
1386 SigDBCert
->SignatureSize
= sizeof(EFI_SIGNATURE_DATA
) - 1 + (UINT32
) mImageDigestSize
;
1387 CopyGuid (&SigDBCert
->SignatureType
, &mCertType
);
1389 SigDBCertData
= (EFI_SIGNATURE_DATA
*)((UINT8
*)SigDBCert
+ sizeof(EFI_SIGNATURE_LIST
));
1390 CopyGuid (&SigDBCertData
->SignatureOwner
, Private
->SignatureGUID
);
1391 CopyMem (SigDBCertData
->SignatureData
, mImageDigest
, mImageDigestSize
);
1393 Attr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
1394 | EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
1395 Status
= CreateTimeBasedPayload (&SigDBSize
, (UINT8
**) &Data
);
1396 if (EFI_ERROR (Status
)) {
1397 DEBUG ((EFI_D_ERROR
, "Fail to create time-based data payload: %r", Status
));
1402 // Check if SigDB variable has been already existed.
1403 // If true, use EFI_VARIABLE_APPEND_WRITE attribute to append the
1404 // new signature data to original variable
1407 Status
= gRT
->GetVariable(
1409 &gEfiImageSecurityDatabaseGuid
,
1414 if (Status
== EFI_BUFFER_TOO_SMALL
) {
1415 Attr
|= EFI_VARIABLE_APPEND_WRITE
;
1416 } else if (Status
!= EFI_NOT_FOUND
) {
1421 // Enroll the variable.
1423 Status
= gRT
->SetVariable(
1425 &gEfiImageSecurityDatabaseGuid
,
1430 if (EFI_ERROR (Status
)) {
1436 CloseFile (Private
->FileContext
->FHandle
);
1437 Private
->FileContext
->FHandle
= NULL
;
1438 Private
->FileContext
->FileName
= NULL
;
1440 if (Private
->SignatureGUID
!= NULL
) {
1441 FreePool (Private
->SignatureGUID
);
1442 Private
->SignatureGUID
= NULL
;
1449 if (mImageBase
!= NULL
) {
1450 FreePool (mImageBase
);
1458 Enroll signature into DB/DBX without KEK's authentication.
1459 The SignatureOwner GUID will be Private->SignatureGUID.
1461 @param[in] PrivateData The module's private data.
1462 @param[in] VariableName Variable name of signature database, must be
1463 EFI_IMAGE_SECURITY_DATABASE or EFI_IMAGE_SECURITY_DATABASE1.
1465 @retval EFI_SUCCESS New signature enrolled successfully.
1466 @retval EFI_INVALID_PARAMETER The parameter is invalid.
1467 @retval others Fail to enroll signature data.
1471 EnrollSignatureDatabase (
1472 IN SECUREBOOT_CONFIG_PRIVATE_DATA
*Private
,
1473 IN CHAR16
*VariableName
1476 UINT16
* FilePostFix
;
1478 if ((Private
->FileContext
->FileName
== NULL
) || (Private
->FileContext
->FHandle
== NULL
) || (Private
->SignatureGUID
== NULL
)) {
1479 return EFI_INVALID_PARAMETER
;
1483 // Parse the file's postfix.
1485 FilePostFix
= Private
->FileContext
->FileName
+ StrLen (Private
->FileContext
->FileName
) - 4;
1486 if ((CompareMem (FilePostFix
, L
".cer",4) == 0) || (CompareMem (FilePostFix
, L
".der",4) == 0)) {
1488 // Supports .cer and .der file as X509 certificate.
1490 return EnrollX509toSigDB (Private
, VariableName
);
1493 return EnrollImageSignatureToSigDB (Private
, VariableName
);
1497 List all signatures in specified signature database (e.g. KEK/DB/DBX)
1498 by GUID in the page for user to select and delete as needed.
1500 @param[in] PrivateData Module's private data.
1501 @param[in] VariableName The variable name of the vendor's signature database.
1502 @param[in] VendorGuid A unique identifier for the vendor.
1503 @param[in] LabelNumber Label number to insert opcodes.
1504 @param[in] FormId Form ID of current page.
1505 @param[in] QuestionIdBase Base question id of the signature list.
1507 @retval EFI_SUCCESS Success to update the signature list page
1508 @retval EFI_OUT_OF_RESOURCES Unable to allocate required resources.
1513 IN SECUREBOOT_CONFIG_PRIVATE_DATA
*PrivateData
,
1514 IN CHAR16
*VariableName
,
1515 IN EFI_GUID
*VendorGuid
,
1516 IN UINT16 LabelNumber
,
1517 IN EFI_FORM_ID FormId
,
1518 IN EFI_QUESTION_ID QuestionIdBase
1525 VOID
*StartOpCodeHandle
;
1526 VOID
*EndOpCodeHandle
;
1527 EFI_IFR_GUID_LABEL
*StartLabel
;
1528 EFI_IFR_GUID_LABEL
*EndLabel
;
1531 EFI_SIGNATURE_LIST
*CertList
;
1532 EFI_SIGNATURE_DATA
*Cert
;
1533 UINT32 ItemDataSize
;
1535 EFI_STRING_ID GuidID
;
1542 StartOpCodeHandle
= NULL
;
1543 EndOpCodeHandle
= NULL
;
1546 // Initialize the container for dynamic opcodes.
1548 StartOpCodeHandle
= HiiAllocateOpCodeHandle ();
1549 if (StartOpCodeHandle
== NULL
) {
1550 Status
= EFI_OUT_OF_RESOURCES
;
1554 EndOpCodeHandle
= HiiAllocateOpCodeHandle ();
1555 if (EndOpCodeHandle
== NULL
) {
1556 Status
= EFI_OUT_OF_RESOURCES
;
1561 // Create Hii Extend Label OpCode.
1563 StartLabel
= (EFI_IFR_GUID_LABEL
*) HiiCreateGuidOpCode (
1567 sizeof (EFI_IFR_GUID_LABEL
)
1569 StartLabel
->ExtendOpCode
= EFI_IFR_EXTEND_OP_LABEL
;
1570 StartLabel
->Number
= LabelNumber
;
1572 EndLabel
= (EFI_IFR_GUID_LABEL
*) HiiCreateGuidOpCode (
1576 sizeof (EFI_IFR_GUID_LABEL
)
1578 EndLabel
->ExtendOpCode
= EFI_IFR_EXTEND_OP_LABEL
;
1579 EndLabel
->Number
= LABEL_END
;
1585 Status
= gRT
->GetVariable (VariableName
, VendorGuid
, NULL
, &DataSize
, Data
);
1586 if (EFI_ERROR (Status
) && Status
!= EFI_BUFFER_TOO_SMALL
) {
1590 Data
= (UINT8
*) AllocateZeroPool (DataSize
);
1592 Status
= EFI_OUT_OF_RESOURCES
;
1596 Status
= gRT
->GetVariable (VariableName
, VendorGuid
, NULL
, &DataSize
, Data
);
1597 if (EFI_ERROR (Status
)) {
1601 GuidStr
= AllocateZeroPool (100);
1602 if (GuidStr
== NULL
) {
1603 Status
= EFI_OUT_OF_RESOURCES
;
1608 // Enumerate all KEK pub data.
1610 ItemDataSize
= (UINT32
) DataSize
;
1611 CertList
= (EFI_SIGNATURE_LIST
*) Data
;
1614 while ((ItemDataSize
> 0) && (ItemDataSize
>= CertList
->SignatureListSize
)) {
1616 if (CompareGuid (&CertList
->SignatureType
, &gEfiCertRsa2048Guid
)) {
1617 Help
= STRING_TOKEN (STR_CERT_TYPE_RSA2048_SHA256_GUID
);
1618 } else if (CompareGuid (&CertList
->SignatureType
, &gEfiCertX509Guid
)) {
1619 Help
= STRING_TOKEN (STR_CERT_TYPE_PCKS7_GUID
);
1620 } else if (CompareGuid (&CertList
->SignatureType
, &gEfiCertSha1Guid
)) {
1621 Help
= STRING_TOKEN (STR_CERT_TYPE_SHA1_GUID
);
1622 } else if (CompareGuid (&CertList
->SignatureType
, &gEfiCertSha256Guid
)) {
1623 Help
= STRING_TOKEN (STR_CERT_TYPE_SHA256_GUID
);
1626 // The signature type is not supported in current implementation.
1631 CertCount
= (CertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - CertList
->SignatureHeaderSize
) / CertList
->SignatureSize
;
1632 for (Index
= 0; Index
< CertCount
; Index
++) {
1633 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
1634 + sizeof (EFI_SIGNATURE_LIST
)
1635 + CertList
->SignatureHeaderSize
1636 + Index
* CertList
->SignatureSize
);
1638 // Display GUID and help
1640 GuidToString (&Cert
->SignatureOwner
, GuidStr
, 100);
1641 GuidID
= HiiSetString (PrivateData
->HiiHandle
, 0, GuidStr
, NULL
);
1642 HiiCreateCheckBoxOpCode (
1644 (EFI_QUESTION_ID
) (QuestionIdBase
+ GuidIndex
++),
1649 EFI_IFR_FLAG_CALLBACK
,
1655 ItemDataSize
-= CertList
->SignatureListSize
;
1656 CertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) CertList
+ CertList
->SignatureListSize
);
1661 PrivateData
->HiiHandle
,
1662 &gSecureBootConfigFormSetGuid
,
1668 if (StartOpCodeHandle
!= NULL
) {
1669 HiiFreeOpCodeHandle (StartOpCodeHandle
);
1672 if (EndOpCodeHandle
!= NULL
) {
1673 HiiFreeOpCodeHandle (EndOpCodeHandle
);
1680 if (GuidStr
!= NULL
) {
1688 Delete a KEK entry from KEK database.
1690 @param[in] PrivateData Module's private data.
1691 @param[in] QuestionId Question id of the KEK item to delete.
1693 @retval EFI_SUCCESS Delete kek item successfully.
1694 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
1698 DeleteKeyExchangeKey (
1699 IN SECUREBOOT_CONFIG_PRIVATE_DATA
*PrivateData
,
1700 IN EFI_QUESTION_ID QuestionId
1709 EFI_SIGNATURE_LIST
*CertList
;
1710 EFI_SIGNATURE_LIST
*NewCertList
;
1711 EFI_SIGNATURE_DATA
*Cert
;
1714 BOOLEAN IsKEKItemFound
;
1716 UINTN DeleteKekIndex
;
1724 DeleteKekIndex
= QuestionId
- OPTION_DEL_KEK_QUESTION_ID
;
1727 // Get original KEK variable.
1730 Status
= gRT
->GetVariable (EFI_KEY_EXCHANGE_KEY_NAME
, &gEfiGlobalVariableGuid
, NULL
, &DataSize
, NULL
);
1731 if (EFI_ERROR(Status
) && Status
!= EFI_BUFFER_TOO_SMALL
) {
1735 OldData
= (UINT8
*)AllocateZeroPool(DataSize
);
1736 if (OldData
== NULL
) {
1737 Status
= EFI_OUT_OF_RESOURCES
;
1741 Status
= gRT
->GetVariable (EFI_KEY_EXCHANGE_KEY_NAME
, &gEfiGlobalVariableGuid
, &Attr
, &DataSize
, OldData
);
1742 if (EFI_ERROR(Status
)) {
1747 // Allocate space for new variable.
1749 Data
= (UINT8
*) AllocateZeroPool (DataSize
);
1751 Status
= EFI_OUT_OF_RESOURCES
;
1756 // Enumerate all KEK pub data and erasing the target item.
1758 IsKEKItemFound
= FALSE
;
1759 KekDataSize
= (UINT32
) DataSize
;
1760 CertList
= (EFI_SIGNATURE_LIST
*) OldData
;
1763 while ((KekDataSize
> 0) && (KekDataSize
>= CertList
->SignatureListSize
)) {
1764 if (CompareGuid (&CertList
->SignatureType
, &gEfiCertRsa2048Guid
) ||
1765 CompareGuid (&CertList
->SignatureType
, &gEfiCertX509Guid
)) {
1766 CopyMem (Data
+ Offset
, CertList
, (sizeof(EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
));
1767 NewCertList
= (EFI_SIGNATURE_LIST
*)(Data
+ Offset
);
1768 Offset
+= (sizeof(EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
1769 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
1770 CertCount
= (CertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - CertList
->SignatureHeaderSize
) / CertList
->SignatureSize
;
1771 for (Index
= 0; Index
< CertCount
; Index
++) {
1772 if (GuidIndex
== DeleteKekIndex
) {
1774 // Find it! Skip it!
1776 NewCertList
->SignatureListSize
-= CertList
->SignatureSize
;
1777 IsKEKItemFound
= TRUE
;
1780 // This item doesn't match. Copy it to the Data buffer.
1782 CopyMem (Data
+ Offset
, Cert
, CertList
->SignatureSize
);
1783 Offset
+= CertList
->SignatureSize
;
1786 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) Cert
+ CertList
->SignatureSize
);
1790 // This List doesn't match. Copy it to the Data buffer.
1792 CopyMem (Data
+ Offset
, CertList
, CertList
->SignatureListSize
);
1793 Offset
+= CertList
->SignatureListSize
;
1796 KekDataSize
-= CertList
->SignatureListSize
;
1797 CertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) CertList
+ CertList
->SignatureListSize
);
1800 if (!IsKEKItemFound
) {
1802 // Doesn't find the Kek Item!
1804 Status
= EFI_NOT_FOUND
;
1809 // Delete the Signature header if there is no signature in the list.
1811 KekDataSize
= Offset
;
1812 CertList
= (EFI_SIGNATURE_LIST
*) Data
;
1814 ZeroMem (OldData
, KekDataSize
);
1815 while ((KekDataSize
> 0) && (KekDataSize
>= CertList
->SignatureListSize
)) {
1816 CertCount
= (CertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - CertList
->SignatureHeaderSize
) / CertList
->SignatureSize
;
1817 DEBUG ((DEBUG_ERROR
, " CertCount = %x\n", CertCount
));
1818 if (CertCount
!= 0) {
1819 CopyMem (OldData
+ Offset
, CertList
, CertList
->SignatureListSize
);
1820 Offset
+= CertList
->SignatureListSize
;
1822 KekDataSize
-= CertList
->SignatureListSize
;
1823 CertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) CertList
+ CertList
->SignatureListSize
);
1827 if ((Attr
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) {
1828 Status
= CreateTimeBasedPayload (&DataSize
, &OldData
);
1829 if (EFI_ERROR (Status
)) {
1830 DEBUG ((EFI_D_ERROR
, "Fail to create time-based data payload: %r", Status
));
1835 Status
= gRT
->SetVariable(
1836 EFI_KEY_EXCHANGE_KEY_NAME
,
1837 &gEfiGlobalVariableGuid
,
1842 if (EFI_ERROR (Status
)) {
1843 DEBUG ((DEBUG_ERROR
, "Failed to set variable, Status = %r\n", Status
));
1852 if (OldData
!= NULL
) {
1856 return UpdateDeletePage (
1858 EFI_KEY_EXCHANGE_KEY_NAME
,
1859 &gEfiGlobalVariableGuid
,
1861 FORMID_DELETE_KEK_FORM
,
1862 OPTION_DEL_KEK_QUESTION_ID
1867 Delete a signature entry from siganture database.
1869 @param[in] PrivateData Module's private data.
1870 @param[in] VariableName The variable name of the vendor's signature database.
1871 @param[in] VendorGuid A unique identifier for the vendor.
1872 @param[in] LabelNumber Label number to insert opcodes.
1873 @param[in] FormId Form ID of current page.
1874 @param[in] QuestionIdBase Base question id of the signature list.
1875 @param[in] DeleteIndex Signature index to delete.
1877 @retval EFI_SUCCESS Delete siganture successfully.
1878 @retval EFI_NOT_FOUND Can't find the signature item,
1879 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
1883 IN SECUREBOOT_CONFIG_PRIVATE_DATA
*PrivateData
,
1884 IN CHAR16
*VariableName
,
1885 IN EFI_GUID
*VendorGuid
,
1886 IN UINT16 LabelNumber
,
1887 IN EFI_FORM_ID FormId
,
1888 IN EFI_QUESTION_ID QuestionIdBase
,
1889 IN UINTN DeleteIndex
1898 EFI_SIGNATURE_LIST
*CertList
;
1899 EFI_SIGNATURE_LIST
*NewCertList
;
1900 EFI_SIGNATURE_DATA
*Cert
;
1903 BOOLEAN IsItemFound
;
1904 UINT32 ItemDataSize
;
1914 // Get original signature list data.
1917 Status
= gRT
->GetVariable (VariableName
, VendorGuid
, NULL
, &DataSize
, NULL
);
1918 if (EFI_ERROR (Status
) && Status
!= EFI_BUFFER_TOO_SMALL
) {
1922 OldData
= (UINT8
*) AllocateZeroPool (DataSize
);
1923 if (OldData
== NULL
) {
1924 Status
= EFI_OUT_OF_RESOURCES
;
1928 Status
= gRT
->GetVariable (VariableName
, VendorGuid
, &Attr
, &DataSize
, OldData
);
1929 if (EFI_ERROR(Status
)) {
1934 // Allocate space for new variable.
1936 Data
= (UINT8
*) AllocateZeroPool (DataSize
);
1938 Status
= EFI_OUT_OF_RESOURCES
;
1943 // Enumerate all signature data and erasing the target item.
1945 IsItemFound
= FALSE
;
1946 ItemDataSize
= (UINT32
) DataSize
;
1947 CertList
= (EFI_SIGNATURE_LIST
*) OldData
;
1950 while ((ItemDataSize
> 0) && (ItemDataSize
>= CertList
->SignatureListSize
)) {
1951 if (CompareGuid (&CertList
->SignatureType
, &gEfiCertRsa2048Guid
) ||
1952 CompareGuid (&CertList
->SignatureType
, &gEfiCertX509Guid
) ||
1953 CompareGuid (&CertList
->SignatureType
, &gEfiCertSha1Guid
) ||
1954 CompareGuid (&CertList
->SignatureType
, &gEfiCertSha256Guid
)
1957 // Copy EFI_SIGNATURE_LIST header then calculate the signature count in this list.
1959 CopyMem (Data
+ Offset
, CertList
, (sizeof(EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
));
1960 NewCertList
= (EFI_SIGNATURE_LIST
*) (Data
+ Offset
);
1961 Offset
+= (sizeof(EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
1962 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
1963 CertCount
= (CertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - CertList
->SignatureHeaderSize
) / CertList
->SignatureSize
;
1964 for (Index
= 0; Index
< CertCount
; Index
++) {
1965 if (GuidIndex
== DeleteIndex
) {
1967 // Find it! Skip it!
1969 NewCertList
->SignatureListSize
-= CertList
->SignatureSize
;
1973 // This item doesn't match. Copy it to the Data buffer.
1975 CopyMem (Data
+ Offset
, (UINT8
*)(Cert
), CertList
->SignatureSize
);
1976 Offset
+= CertList
->SignatureSize
;
1979 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) Cert
+ CertList
->SignatureSize
);
1983 // This List doesn't match. Just copy it to the Data buffer.
1985 CopyMem (Data
+ Offset
, (UINT8
*)(CertList
), CertList
->SignatureListSize
);
1986 Offset
+= CertList
->SignatureListSize
;
1989 ItemDataSize
-= CertList
->SignatureListSize
;
1990 CertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) CertList
+ CertList
->SignatureListSize
);
1995 // Doesn't find the signature Item!
1997 Status
= EFI_NOT_FOUND
;
2002 // Delete the EFI_SIGNATURE_LIST header if there is no signature in the list.
2004 ItemDataSize
= Offset
;
2005 CertList
= (EFI_SIGNATURE_LIST
*) Data
;
2007 ZeroMem (OldData
, ItemDataSize
);
2008 while ((ItemDataSize
> 0) && (ItemDataSize
>= CertList
->SignatureListSize
)) {
2009 CertCount
= (CertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - CertList
->SignatureHeaderSize
) / CertList
->SignatureSize
;
2010 DEBUG ((DEBUG_ERROR
, " CertCount = %x\n", CertCount
));
2011 if (CertCount
!= 0) {
2012 CopyMem (OldData
+ Offset
, (UINT8
*)(CertList
), CertList
->SignatureListSize
);
2013 Offset
+= CertList
->SignatureListSize
;
2015 ItemDataSize
-= CertList
->SignatureListSize
;
2016 CertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) CertList
+ CertList
->SignatureListSize
);
2020 if ((Attr
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) {
2021 Status
= CreateTimeBasedPayload (&DataSize
, &OldData
);
2022 if (EFI_ERROR (Status
)) {
2023 DEBUG ((EFI_D_ERROR
, "Fail to create time-based data payload: %r", Status
));
2028 Status
= gRT
->SetVariable(
2035 if (EFI_ERROR (Status
)) {
2036 DEBUG ((DEBUG_ERROR
, "Failed to set variable, Status = %r\n", Status
));
2045 if (OldData
!= NULL
) {
2049 return UpdateDeletePage (
2060 This function extracts configuration from variable.
2062 @param[in, out] ConfigData Point to SecureBoot configuration private data.
2066 SecureBootExtractConfigFromVariable (
2067 IN OUT SECUREBOOT_CONFIGURATION
*ConfigData
2070 UINT8
*SecureBootEnable
;
2073 UINT8
*SecureBootMode
;
2075 SecureBootEnable
= NULL
;
2078 SecureBootMode
= NULL
;
2081 // If the SecureBootEnable Variable doesn't exist, hide the SecureBoot Enable/Disable
2084 GetVariable2 (EFI_SECURE_BOOT_ENABLE_NAME
, &gEfiSecureBootEnableDisableGuid
, (VOID
**)&SecureBootEnable
, NULL
);
2085 if (SecureBootEnable
== NULL
) {
2086 ConfigData
->HideSecureBoot
= TRUE
;
2088 ConfigData
->HideSecureBoot
= FALSE
;
2092 // If it is Physical Presence User, set the PhysicalPresent to true.
2094 if (UserPhysicalPresent()) {
2095 ConfigData
->PhysicalPresent
= TRUE
;
2097 ConfigData
->PhysicalPresent
= FALSE
;
2101 // If there is no PK then the Delete Pk button will be gray.
2103 GetVariable2 (EFI_SETUP_MODE_NAME
, &gEfiGlobalVariableGuid
, (VOID
**)&SetupMode
, NULL
);
2104 if (SetupMode
== NULL
|| (*SetupMode
) == SETUP_MODE
) {
2105 ConfigData
->HasPk
= FALSE
;
2107 ConfigData
->HasPk
= TRUE
;
2111 // If the value of SecureBoot variable is 1, the platform is operating in secure boot mode.
2113 GetVariable2 (EFI_SECURE_BOOT_MODE_NAME
, &gEfiGlobalVariableGuid
, (VOID
**)&SecureBoot
, NULL
);
2114 if (SecureBoot
!= NULL
&& *SecureBoot
== SECURE_BOOT_MODE_ENABLE
) {
2115 ConfigData
->SecureBootState
= TRUE
;
2117 ConfigData
->SecureBootState
= FALSE
;
2121 // Get the SecureBootMode from CustomMode variable.
2123 GetVariable2 (EFI_CUSTOM_MODE_NAME
, &gEfiCustomModeEnableGuid
, (VOID
**)&SecureBootMode
, NULL
);
2124 if (SecureBootMode
== NULL
) {
2125 ConfigData
->SecureBootMode
= STANDARD_SECURE_BOOT_MODE
;
2127 ConfigData
->SecureBootMode
= *(SecureBootMode
);
2133 This function allows a caller to extract the current configuration for one
2134 or more named elements from the target driver.
2136 @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
2137 @param[in] Request A null-terminated Unicode string in
2138 <ConfigRequest> format.
2139 @param[out] Progress On return, points to a character in the Request
2140 string. Points to the string's null terminator if
2141 request was successful. Points to the most recent
2142 '&' before the first failing name/value pair (or
2143 the beginning of the string if the failure is in
2144 the first name/value pair) if the request was not
2146 @param[out] Results A null-terminated Unicode string in
2147 <ConfigAltResp> format which has all values filled
2148 in for the names in the Request string. String to
2149 be allocated by the called function.
2151 @retval EFI_SUCCESS The Results is filled with the requested values.
2152 @retval EFI_OUT_OF_RESOURCES Not enough memory to store the results.
2153 @retval EFI_INVALID_PARAMETER Request is illegal syntax, or unknown name.
2154 @retval EFI_NOT_FOUND Routing data doesn't match any storage in this
2160 SecureBootExtractConfig (
2161 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL
*This
,
2162 IN CONST EFI_STRING Request
,
2163 OUT EFI_STRING
*Progress
,
2164 OUT EFI_STRING
*Results
2170 SECUREBOOT_CONFIGURATION Configuration
;
2171 EFI_STRING ConfigRequest
;
2172 EFI_STRING ConfigRequestHdr
;
2173 SECUREBOOT_CONFIG_PRIVATE_DATA
*PrivateData
;
2174 BOOLEAN AllocatedRequest
;
2176 if (Progress
== NULL
|| Results
== NULL
) {
2177 return EFI_INVALID_PARAMETER
;
2180 AllocatedRequest
= FALSE
;
2181 ConfigRequestHdr
= NULL
;
2182 ConfigRequest
= NULL
;
2185 ZeroMem (&Configuration
, sizeof (Configuration
));
2186 PrivateData
= SECUREBOOT_CONFIG_PRIVATE_FROM_THIS (This
);
2187 *Progress
= Request
;
2189 if ((Request
!= NULL
) && !HiiIsConfigHdrMatch (Request
, &gSecureBootConfigFormSetGuid
, mSecureBootStorageName
)) {
2190 return EFI_NOT_FOUND
;
2194 // Get Configuration from Variable.
2196 SecureBootExtractConfigFromVariable (&Configuration
);
2198 BufferSize
= sizeof (SECUREBOOT_CONFIGURATION
);
2199 ConfigRequest
= Request
;
2200 if ((Request
== NULL
) || (StrStr (Request
, L
"OFFSET") == NULL
)) {
2202 // Request is set to NULL or OFFSET is NULL, construct full request string.
2204 // Allocate and fill a buffer large enough to hold the <ConfigHdr> template
2205 // followed by "&OFFSET=0&WIDTH=WWWWWWWWWWWWWWWW" followed by a Null-terminator
2207 ConfigRequestHdr
= HiiConstructConfigHdr (&gSecureBootConfigFormSetGuid
, mSecureBootStorageName
, PrivateData
->DriverHandle
);
2208 Size
= (StrLen (ConfigRequestHdr
) + 32 + 1) * sizeof (CHAR16
);
2209 ConfigRequest
= AllocateZeroPool (Size
);
2210 ASSERT (ConfigRequest
!= NULL
);
2211 AllocatedRequest
= TRUE
;
2212 UnicodeSPrint (ConfigRequest
, Size
, L
"%s&OFFSET=0&WIDTH=%016LX", ConfigRequestHdr
, (UINT64
)BufferSize
);
2213 FreePool (ConfigRequestHdr
);
2214 ConfigRequestHdr
= NULL
;
2217 Status
= gHiiConfigRouting
->BlockToConfig (
2220 (UINT8
*) &Configuration
,
2227 // Free the allocated config request string.
2229 if (AllocatedRequest
) {
2230 FreePool (ConfigRequest
);
2234 // Set Progress string to the original request string.
2236 if (Request
== NULL
) {
2238 } else if (StrStr (Request
, L
"OFFSET") == NULL
) {
2239 *Progress
= Request
+ StrLen (Request
);
2246 This function processes the results of changes in configuration.
2248 @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
2249 @param[in] Configuration A null-terminated Unicode string in <ConfigResp>
2251 @param[out] Progress A pointer to a string filled in with the offset of
2252 the most recent '&' before the first failing
2253 name/value pair (or the beginning of the string if
2254 the failure is in the first name/value pair) or
2255 the terminating NULL if all was successful.
2257 @retval EFI_SUCCESS The Results is processed successfully.
2258 @retval EFI_INVALID_PARAMETER Configuration is NULL.
2259 @retval EFI_NOT_FOUND Routing data doesn't match any storage in this
2265 SecureBootRouteConfig (
2266 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL
*This
,
2267 IN CONST EFI_STRING Configuration
,
2268 OUT EFI_STRING
*Progress
2271 if (Configuration
== NULL
|| Progress
== NULL
) {
2272 return EFI_INVALID_PARAMETER
;
2275 *Progress
= Configuration
;
2276 if (!HiiIsConfigHdrMatch (Configuration
, &gSecureBootConfigFormSetGuid
, mSecureBootStorageName
)) {
2277 return EFI_NOT_FOUND
;
2280 *Progress
= Configuration
+ StrLen (Configuration
);
2285 This function is called to provide results data to the driver.
2287 @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
2288 @param[in] Action Specifies the type of action taken by the browser.
2289 @param[in] QuestionId A unique value which is sent to the original
2290 exporting driver so that it can identify the type
2292 @param[in] Type The type of value for the question.
2293 @param[in] Value A pointer to the data being sent to the original
2295 @param[out] ActionRequest On return, points to the action requested by the
2298 @retval EFI_SUCCESS The callback successfully handled the action.
2299 @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the
2300 variable and its data.
2301 @retval EFI_DEVICE_ERROR The variable could not be saved.
2302 @retval EFI_UNSUPPORTED The specified Action is not supported by the
2308 SecureBootCallback (
2309 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL
*This
,
2310 IN EFI_BROWSER_ACTION Action
,
2311 IN EFI_QUESTION_ID QuestionId
,
2313 IN EFI_IFR_TYPE_VALUE
*Value
,
2314 OUT EFI_BROWSER_ACTION_REQUEST
*ActionRequest
2319 SECUREBOOT_CONFIG_PRIVATE_DATA
*Private
;
2321 SECUREBOOT_CONFIGURATION
*IfrNvData
;
2323 UINT8
*SecureBootEnable
;
2325 SecureBootEnable
= NULL
;
2327 if ((This
== NULL
) || (Value
== NULL
) || (ActionRequest
== NULL
)) {
2328 return EFI_INVALID_PARAMETER
;
2331 if ((Action
!= EFI_BROWSER_ACTION_CHANGED
) && (Action
!= EFI_BROWSER_ACTION_CHANGING
)) {
2332 return EFI_UNSUPPORTED
;
2335 Private
= SECUREBOOT_CONFIG_PRIVATE_FROM_THIS (This
);
2338 // Retrieve uncommitted data from Browser
2340 BufferSize
= sizeof (SECUREBOOT_CONFIGURATION
);
2341 IfrNvData
= AllocateZeroPool (BufferSize
);
2342 if (IfrNvData
== NULL
) {
2343 return EFI_OUT_OF_RESOURCES
;
2346 Status
= EFI_SUCCESS
;
2348 HiiGetBrowserData (NULL
, NULL
, BufferSize
, (UINT8
*) IfrNvData
);
2350 if (Action
== EFI_BROWSER_ACTION_CHANGING
) {
2352 switch (QuestionId
) {
2353 case KEY_SECURE_BOOT_ENABLE
:
2354 GetVariable2 (EFI_SECURE_BOOT_ENABLE_NAME
, &gEfiSecureBootEnableDisableGuid
, (VOID
**)&SecureBootEnable
, NULL
);
2355 if (NULL
!= SecureBootEnable
) {
2356 if (EFI_ERROR (SaveSecureBootVariable (Value
->u8
))) {
2358 EFI_LIGHTGRAY
| EFI_BACKGROUND_BLUE
,
2360 L
"Only Physical Presence User could disable secure boot!",
2363 Status
= EFI_UNSUPPORTED
;
2365 *ActionRequest
= EFI_BROWSER_ACTION_REQUEST_FORM_APPLY
;
2369 case KEY_SECURE_BOOT_OPTION
:
2370 FreeMenu (&DirectoryMenu
);
2371 FreeMenu (&FsOptionMenu
);
2374 case KEY_SECURE_BOOT_KEK_OPTION
:
2375 case KEY_SECURE_BOOT_DB_OPTION
:
2376 case KEY_SECURE_BOOT_DBX_OPTION
:
2378 // Clear Signature GUID.
2380 ZeroMem (IfrNvData
->SignatureGuid
, sizeof (IfrNvData
->SignatureGuid
));
2381 if (Private
->SignatureGUID
== NULL
) {
2382 Private
->SignatureGUID
= (EFI_GUID
*) AllocateZeroPool (sizeof (EFI_GUID
));
2383 if (Private
->SignatureGUID
== NULL
) {
2384 return EFI_OUT_OF_RESOURCES
;
2388 if (QuestionId
== KEY_SECURE_BOOT_DB_OPTION
) {
2389 LabelId
= SECUREBOOT_ENROLL_SIGNATURE_TO_DB
;
2390 } else if (QuestionId
== KEY_SECURE_BOOT_DBX_OPTION
) {
2391 LabelId
= SECUREBOOT_ENROLL_SIGNATURE_TO_DBX
;
2393 LabelId
= FORMID_ENROLL_KEK_FORM
;
2397 // Refresh selected file.
2399 CleanUpPage (LabelId
, Private
);
2402 case SECUREBOOT_ADD_PK_FILE_FORM_ID
:
2403 case FORMID_ENROLL_KEK_FORM
:
2404 case SECUREBOOT_ENROLL_SIGNATURE_TO_DB
:
2405 case SECUREBOOT_ENROLL_SIGNATURE_TO_DBX
:
2406 if (QuestionId
== SECUREBOOT_ADD_PK_FILE_FORM_ID
) {
2407 Private
->FeCurrentState
= FileExplorerStateEnrollPkFile
;
2408 } else if (QuestionId
== FORMID_ENROLL_KEK_FORM
) {
2409 Private
->FeCurrentState
= FileExplorerStateEnrollKekFile
;
2410 } else if (QuestionId
== SECUREBOOT_ENROLL_SIGNATURE_TO_DB
) {
2411 Private
->FeCurrentState
= FileExplorerStateEnrollSignatureFileToDb
;
2413 Private
->FeCurrentState
= FileExplorerStateEnrollSignatureFileToDbx
;
2416 Private
->FeDisplayContext
= FileExplorerDisplayUnknown
;
2417 CleanUpPage (FORM_FILE_EXPLORER_ID
, Private
);
2418 UpdateFileExplorer (Private
, 0);
2421 case KEY_SECURE_BOOT_DELETE_PK
:
2423 Status
= DeletePlatformKey ();
2424 *ActionRequest
= EFI_BROWSER_ACTION_REQUEST_FORM_APPLY
;
2428 case KEY_DELETE_KEK
:
2431 EFI_KEY_EXCHANGE_KEY_NAME
,
2432 &gEfiGlobalVariableGuid
,
2434 FORMID_DELETE_KEK_FORM
,
2435 OPTION_DEL_KEK_QUESTION_ID
2439 case SECUREBOOT_DELETE_SIGNATURE_FROM_DB
:
2442 EFI_IMAGE_SECURITY_DATABASE
,
2443 &gEfiImageSecurityDatabaseGuid
,
2445 SECUREBOOT_DELETE_SIGNATURE_FROM_DB
,
2446 OPTION_DEL_DB_QUESTION_ID
2450 case SECUREBOOT_DELETE_SIGNATURE_FROM_DBX
:
2453 EFI_IMAGE_SECURITY_DATABASE1
,
2454 &gEfiImageSecurityDatabaseGuid
,
2456 SECUREBOOT_DELETE_SIGNATURE_FROM_DBX
,
2457 OPTION_DEL_DBX_QUESTION_ID
2462 case KEY_VALUE_SAVE_AND_EXIT_KEK
:
2463 Status
= EnrollKeyExchangeKey (Private
);
2466 case KEY_VALUE_SAVE_AND_EXIT_DB
:
2467 Status
= EnrollSignatureDatabase (Private
, EFI_IMAGE_SECURITY_DATABASE
);
2470 case KEY_VALUE_SAVE_AND_EXIT_DBX
:
2471 Status
= EnrollSignatureDatabase (Private
, EFI_IMAGE_SECURITY_DATABASE1
);
2475 if (QuestionId
>= FILE_OPTION_OFFSET
) {
2476 UpdateFileExplorer (Private
, QuestionId
);
2477 } else if ((QuestionId
>= OPTION_DEL_KEK_QUESTION_ID
) &&
2478 (QuestionId
< (OPTION_DEL_KEK_QUESTION_ID
+ OPTION_CONFIG_RANGE
))) {
2479 DeleteKeyExchangeKey (Private
, QuestionId
);
2480 } else if ((QuestionId
>= OPTION_DEL_DB_QUESTION_ID
) &&
2481 (QuestionId
< (OPTION_DEL_DB_QUESTION_ID
+ OPTION_CONFIG_RANGE
))) {
2484 EFI_IMAGE_SECURITY_DATABASE
,
2485 &gEfiImageSecurityDatabaseGuid
,
2487 SECUREBOOT_DELETE_SIGNATURE_FROM_DB
,
2488 OPTION_DEL_DB_QUESTION_ID
,
2489 QuestionId
- OPTION_DEL_DB_QUESTION_ID
2491 } else if ((QuestionId
>= OPTION_DEL_DBX_QUESTION_ID
) &&
2492 (QuestionId
< (OPTION_DEL_DBX_QUESTION_ID
+ OPTION_CONFIG_RANGE
))) {
2495 EFI_IMAGE_SECURITY_DATABASE1
,
2496 &gEfiImageSecurityDatabaseGuid
,
2498 SECUREBOOT_DELETE_SIGNATURE_FROM_DBX
,
2499 OPTION_DEL_DBX_QUESTION_ID
,
2500 QuestionId
- OPTION_DEL_DBX_QUESTION_ID
2505 } else if (Action
== EFI_BROWSER_ACTION_CHANGED
) {
2506 switch (QuestionId
) {
2507 case KEY_SECURE_BOOT_ENABLE
:
2508 *ActionRequest
= EFI_BROWSER_ACTION_REQUEST_SUBMIT
;
2510 case KEY_VALUE_SAVE_AND_EXIT_PK
:
2511 Status
= EnrollPlatformKey (Private
);
2512 if (EFI_ERROR (Status
)) {
2514 EFI_LIGHTGRAY
| EFI_BACKGROUND_BLUE
,
2516 L
"ERROR: Unsupported file type, only DER encoded certificate file (*.cer or *.der) is supported!",
2520 *ActionRequest
= EFI_BROWSER_ACTION_REQUEST_RESET
;
2524 case KEY_VALUE_NO_SAVE_AND_EXIT_PK
:
2525 case KEY_VALUE_NO_SAVE_AND_EXIT_KEK
:
2526 case KEY_VALUE_NO_SAVE_AND_EXIT_DB
:
2527 case KEY_VALUE_NO_SAVE_AND_EXIT_DBX
:
2528 if (Private
->FileContext
->FHandle
!= NULL
) {
2529 CloseFile (Private
->FileContext
->FHandle
);
2530 Private
->FileContext
->FHandle
= NULL
;
2531 Private
->FileContext
->FileName
= NULL
;
2534 if (Private
->SignatureGUID
!= NULL
) {
2535 FreePool (Private
->SignatureGUID
);
2536 Private
->SignatureGUID
= NULL
;
2538 *ActionRequest
= EFI_BROWSER_ACTION_REQUEST_EXIT
;
2541 case KEY_SECURE_BOOT_MODE
:
2542 GetVariable2 (EFI_CUSTOM_MODE_NAME
, &gEfiCustomModeEnableGuid
, (VOID
**)&SecureBootEnable
, NULL
);
2543 if (NULL
!= SecureBootEnable
) {
2544 Status
= gRT
->SetVariable (
2545 EFI_CUSTOM_MODE_NAME
,
2546 &gEfiCustomModeEnableGuid
,
2547 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
2551 *ActionRequest
= EFI_BROWSER_ACTION_REQUEST_FORM_APPLY
;
2552 IfrNvData
->SecureBootMode
= Value
->u8
;
2556 case KEY_SECURE_BOOT_KEK_GUID
:
2557 case KEY_SECURE_BOOT_SIGNATURE_GUID_DB
:
2558 case KEY_SECURE_BOOT_SIGNATURE_GUID_DBX
:
2559 ASSERT (Private
->SignatureGUID
!= NULL
);
2560 Status
= StringToGuid (
2561 IfrNvData
->SignatureGuid
,
2562 StrLen (IfrNvData
->SignatureGuid
),
2563 Private
->SignatureGUID
2565 if (EFI_ERROR (Status
)) {
2569 *ActionRequest
= EFI_BROWSER_ACTION_REQUEST_FORM_APPLY
;
2572 case KEY_SECURE_BOOT_DELETE_PK
:
2574 *ActionRequest
= EFI_BROWSER_ACTION_REQUEST_SUBMIT
;
2580 if (!EFI_ERROR (Status
)) {
2581 BufferSize
= sizeof (SECUREBOOT_CONFIGURATION
);
2582 HiiSetBrowserData (NULL
, NULL
, BufferSize
, (UINT8
*) IfrNvData
, NULL
);
2584 FreePool (IfrNvData
);
2590 This function publish the SecureBoot configuration Form.
2592 @param[in, out] PrivateData Points to SecureBoot configuration private data.
2594 @retval EFI_SUCCESS HII Form is installed successfully.
2595 @retval EFI_OUT_OF_RESOURCES Not enough resource for HII Form installation.
2596 @retval Others Other errors as indicated.
2600 InstallSecureBootConfigForm (
2601 IN OUT SECUREBOOT_CONFIG_PRIVATE_DATA
*PrivateData
2605 EFI_HII_HANDLE HiiHandle
;
2606 EFI_HANDLE DriverHandle
;
2607 EFI_HII_CONFIG_ACCESS_PROTOCOL
*ConfigAccess
;
2609 DriverHandle
= NULL
;
2610 ConfigAccess
= &PrivateData
->ConfigAccess
;
2611 Status
= gBS
->InstallMultipleProtocolInterfaces (
2613 &gEfiDevicePathProtocolGuid
,
2614 &mSecureBootHiiVendorDevicePath
,
2615 &gEfiHiiConfigAccessProtocolGuid
,
2619 if (EFI_ERROR (Status
)) {
2623 PrivateData
->DriverHandle
= DriverHandle
;
2626 // Publish the HII package list
2628 HiiHandle
= HiiAddPackages (
2629 &gSecureBootConfigFormSetGuid
,
2631 SecureBootConfigDxeStrings
,
2632 SecureBootConfigBin
,
2635 if (HiiHandle
== NULL
) {
2636 gBS
->UninstallMultipleProtocolInterfaces (
2638 &gEfiDevicePathProtocolGuid
,
2639 &mSecureBootHiiVendorDevicePath
,
2640 &gEfiHiiConfigAccessProtocolGuid
,
2644 return EFI_OUT_OF_RESOURCES
;
2647 PrivateData
->HiiHandle
= HiiHandle
;
2649 PrivateData
->FileContext
= AllocateZeroPool (sizeof (SECUREBOOT_FILE_CONTEXT
));
2650 PrivateData
->MenuEntry
= AllocateZeroPool (sizeof (SECUREBOOT_MENU_ENTRY
));
2652 if (PrivateData
->FileContext
== NULL
|| PrivateData
->MenuEntry
== NULL
) {
2653 UninstallSecureBootConfigForm (PrivateData
);
2654 return EFI_OUT_OF_RESOURCES
;
2657 PrivateData
->FeCurrentState
= FileExplorerStateInActive
;
2658 PrivateData
->FeDisplayContext
= FileExplorerDisplayUnknown
;
2660 InitializeListHead (&FsOptionMenu
.Head
);
2661 InitializeListHead (&DirectoryMenu
.Head
);
2664 // Init OpCode Handle and Allocate space for creation of Buffer
2666 mStartOpCodeHandle
= HiiAllocateOpCodeHandle ();
2667 if (mStartOpCodeHandle
== NULL
) {
2668 UninstallSecureBootConfigForm (PrivateData
);
2669 return EFI_OUT_OF_RESOURCES
;
2672 mEndOpCodeHandle
= HiiAllocateOpCodeHandle ();
2673 if (mEndOpCodeHandle
== NULL
) {
2674 UninstallSecureBootConfigForm (PrivateData
);
2675 return EFI_OUT_OF_RESOURCES
;
2679 // Create Hii Extend Label OpCode as the start opcode
2681 mStartLabel
= (EFI_IFR_GUID_LABEL
*) HiiCreateGuidOpCode (
2685 sizeof (EFI_IFR_GUID_LABEL
)
2687 mStartLabel
->ExtendOpCode
= EFI_IFR_EXTEND_OP_LABEL
;
2690 // Create Hii Extend Label OpCode as the end opcode
2692 mEndLabel
= (EFI_IFR_GUID_LABEL
*) HiiCreateGuidOpCode (
2696 sizeof (EFI_IFR_GUID_LABEL
)
2698 mEndLabel
->ExtendOpCode
= EFI_IFR_EXTEND_OP_LABEL
;
2699 mEndLabel
->Number
= LABEL_END
;
2705 This function removes SecureBoot configuration Form.
2707 @param[in, out] PrivateData Points to SecureBoot configuration private data.
2711 UninstallSecureBootConfigForm (
2712 IN OUT SECUREBOOT_CONFIG_PRIVATE_DATA
*PrivateData
2716 // Uninstall HII package list
2718 if (PrivateData
->HiiHandle
!= NULL
) {
2719 HiiRemovePackages (PrivateData
->HiiHandle
);
2720 PrivateData
->HiiHandle
= NULL
;
2724 // Uninstall HII Config Access Protocol
2726 if (PrivateData
->DriverHandle
!= NULL
) {
2727 gBS
->UninstallMultipleProtocolInterfaces (
2728 PrivateData
->DriverHandle
,
2729 &gEfiDevicePathProtocolGuid
,
2730 &mSecureBootHiiVendorDevicePath
,
2731 &gEfiHiiConfigAccessProtocolGuid
,
2732 &PrivateData
->ConfigAccess
,
2735 PrivateData
->DriverHandle
= NULL
;
2738 if (PrivateData
->SignatureGUID
!= NULL
) {
2739 FreePool (PrivateData
->SignatureGUID
);
2742 if (PrivateData
->MenuEntry
!= NULL
) {
2743 FreePool (PrivateData
->MenuEntry
);
2746 if (PrivateData
->FileContext
!= NULL
) {
2747 FreePool (PrivateData
->FileContext
);
2750 FreePool (PrivateData
);
2752 FreeMenu (&DirectoryMenu
);
2753 FreeMenu (&FsOptionMenu
);
2755 if (mStartOpCodeHandle
!= NULL
) {
2756 HiiFreeOpCodeHandle (mStartOpCodeHandle
);
2759 if (mEndOpCodeHandle
!= NULL
) {
2760 HiiFreeOpCodeHandle (mEndOpCodeHandle
);