2 HII Config Access protocol implementation of SecureBoot configuration module.
4 Copyright (c) 2011 - 2017, Intel Corporation. All rights reserved.<BR>
5 (C) Copyright 2018 Hewlett Packard Enterprise Development LP<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include "SecureBootConfigImpl.h"
17 #include <Library/BaseCryptLib.h>
19 CHAR16 mSecureBootStorageName
[] = L
"SECUREBOOT_CONFIGURATION";
21 SECUREBOOT_CONFIG_PRIVATE_DATA mSecureBootConfigPrivateDateTemplate
= {
22 SECUREBOOT_CONFIG_PRIVATE_DATA_SIGNATURE
,
24 SecureBootExtractConfig
,
25 SecureBootRouteConfig
,
30 HII_VENDOR_DEVICE_PATH mSecureBootHiiVendorDevicePath
= {
36 (UINT8
) (sizeof (VENDOR_DEVICE_PATH
)),
37 (UINT8
) ((sizeof (VENDOR_DEVICE_PATH
)) >> 8)
40 SECUREBOOT_CONFIG_FORM_SET_GUID
44 END_ENTIRE_DEVICE_PATH_SUBTYPE
,
46 (UINT8
) (END_DEVICE_PATH_LENGTH
),
47 (UINT8
) ((END_DEVICE_PATH_LENGTH
) >> 8)
53 BOOLEAN mIsEnterSecureBootForm
= FALSE
;
56 // OID ASN.1 Value for Hash Algorithms
58 UINT8 mHashOidValue
[] = {
59 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x02, 0x05, // OBJ_md5
60 0x2B, 0x0E, 0x03, 0x02, 0x1A, // OBJ_sha1
61 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x04, // OBJ_sha224
62 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, // OBJ_sha256
63 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02, // OBJ_sha384
64 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03, // OBJ_sha512
67 HASH_TABLE mHash
[] = {
68 { L
"SHA224", 28, &mHashOidValue
[13], 9, NULL
, NULL
, NULL
, NULL
},
69 { L
"SHA256", 32, &mHashOidValue
[22], 9, Sha256GetContextSize
, Sha256Init
, Sha256Update
, Sha256Final
},
70 { L
"SHA384", 48, &mHashOidValue
[31], 9, Sha384GetContextSize
, Sha384Init
, Sha384Update
, Sha384Final
},
71 { L
"SHA512", 64, &mHashOidValue
[40], 9, Sha512GetContextSize
, Sha512Init
, Sha512Update
, Sha512Final
}
75 // Variable Definitions
77 UINT32 mPeCoffHeaderOffset
= 0;
78 WIN_CERTIFICATE
*mCertificate
= NULL
;
79 IMAGE_TYPE mImageType
;
80 UINT8
*mImageBase
= NULL
;
82 UINT8 mImageDigest
[MAX_DIGEST_SIZE
];
83 UINTN mImageDigestSize
;
85 EFI_IMAGE_SECURITY_DATA_DIRECTORY
*mSecDataDir
= NULL
;
86 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION mNtHeader
;
89 // Possible DER-encoded certificate file suffixes, end with NULL pointer.
91 CHAR16
* mDerEncodedSuffix
[] = {
97 CHAR16
* mSupportX509Suffix
= L
"*.cer/der/crt";
99 SECUREBOOT_CONFIG_PRIVATE_DATA
*gSecureBootPrivateData
= NULL
;
102 This code cleans up enrolled file by closing file & free related resources attached to
105 @param[in] FileContext FileContext cached in SecureBootConfig driver
110 IN SECUREBOOT_FILE_CONTEXT
*FileContext
113 if (FileContext
->FHandle
!= NULL
) {
114 CloseFile (FileContext
->FHandle
);
115 FileContext
->FHandle
= NULL
;
118 if (FileContext
->FileName
!= NULL
){
119 FreePool(FileContext
->FileName
);
120 FileContext
->FileName
= NULL
;
122 FileContext
->FileType
= UNKNOWN_FILE_TYPE
;
127 This code checks if the FileSuffix is one of the possible DER-encoded certificate suffix.
129 @param[in] FileSuffix The suffix of the input certificate file
131 @retval TRUE It's a DER-encoded certificate.
132 @retval FALSE It's NOT a DER-encoded certificate.
136 IsDerEncodeCertificate (
137 IN CONST CHAR16
*FileSuffix
141 for (Index
= 0; mDerEncodedSuffix
[Index
] != NULL
; Index
++) {
142 if (StrCmp (FileSuffix
, mDerEncodedSuffix
[Index
]) == 0) {
150 This code checks if the file content complies with EFI_VARIABLE_AUTHENTICATION_2 format
151 The function reads file content but won't open/close given FileHandle.
153 @param[in] FileHandle The FileHandle to be checked
155 @retval TRUE The content is EFI_VARIABLE_AUTHENTICATION_2 format.
156 @retval FALSE The content is NOT a EFI_VARIABLE_AUTHENTICATION_2 format.
160 IsAuthentication2Format (
161 IN EFI_FILE_HANDLE FileHandle
165 EFI_VARIABLE_AUTHENTICATION_2
*Auth2
;
166 BOOLEAN IsAuth2Format
;
168 IsAuth2Format
= FALSE
;
171 // Read the whole file content
173 Status
= ReadFileContent(
175 (VOID
**) &mImageBase
,
179 if (EFI_ERROR (Status
)) {
183 Auth2
= (EFI_VARIABLE_AUTHENTICATION_2
*)mImageBase
;
184 if (Auth2
->AuthInfo
.Hdr
.wCertificateType
!= WIN_CERT_TYPE_EFI_GUID
) {
188 if (CompareGuid(&gEfiCertPkcs7Guid
, &Auth2
->AuthInfo
.CertType
)) {
189 IsAuth2Format
= TRUE
;
194 // Do not close File. simply check file content
196 if (mImageBase
!= NULL
) {
197 FreePool (mImageBase
);
201 return IsAuth2Format
;
205 Set Secure Boot option into variable space.
207 @param[in] VarValue The option of Secure Boot.
209 @retval EFI_SUCCESS The operation is finished successfully.
210 @retval Others Other errors as indicated.
214 SaveSecureBootVariable (
220 Status
= gRT
->SetVariable (
221 EFI_SECURE_BOOT_ENABLE_NAME
,
222 &gEfiSecureBootEnableDisableGuid
,
223 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
231 Create a time based data payload by concatenating the EFI_VARIABLE_AUTHENTICATION_2
232 descriptor with the input data. NO authentication is required in this function.
234 @param[in, out] DataSize On input, the size of Data buffer in bytes.
235 On output, the size of data returned in Data
237 @param[in, out] Data On input, Pointer to data buffer to be wrapped or
238 pointer to NULL to wrap an empty payload.
239 On output, Pointer to the new payload date buffer allocated from pool,
240 it's caller's responsibility to free the memory when finish using it.
242 @retval EFI_SUCCESS Create time based payload successfully.
243 @retval EFI_OUT_OF_RESOURCES There are not enough memory resourses to create time based payload.
244 @retval EFI_INVALID_PARAMETER The parameter is invalid.
245 @retval Others Unexpected error happens.
249 CreateTimeBasedPayload (
250 IN OUT UINTN
*DataSize
,
258 EFI_VARIABLE_AUTHENTICATION_2
*DescriptorData
;
259 UINTN DescriptorSize
;
262 if (Data
== NULL
|| DataSize
== NULL
) {
263 return EFI_INVALID_PARAMETER
;
267 // In Setup mode or Custom mode, the variable does not need to be signed but the
268 // parameters to the SetVariable() call still need to be prepared as authenticated
269 // variable. So we create EFI_VARIABLE_AUTHENTICATED_2 descriptor without certificate
273 PayloadSize
= *DataSize
;
275 DescriptorSize
= OFFSET_OF (EFI_VARIABLE_AUTHENTICATION_2
, AuthInfo
) + OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID
, CertData
);
276 NewData
= (UINT8
*) AllocateZeroPool (DescriptorSize
+ PayloadSize
);
277 if (NewData
== NULL
) {
278 return EFI_OUT_OF_RESOURCES
;
281 if ((Payload
!= NULL
) && (PayloadSize
!= 0)) {
282 CopyMem (NewData
+ DescriptorSize
, Payload
, PayloadSize
);
285 DescriptorData
= (EFI_VARIABLE_AUTHENTICATION_2
*) (NewData
);
287 ZeroMem (&Time
, sizeof (EFI_TIME
));
288 Status
= gRT
->GetTime (&Time
, NULL
);
289 if (EFI_ERROR (Status
)) {
298 CopyMem (&DescriptorData
->TimeStamp
, &Time
, sizeof (EFI_TIME
));
300 DescriptorData
->AuthInfo
.Hdr
.dwLength
= OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID
, CertData
);
301 DescriptorData
->AuthInfo
.Hdr
.wRevision
= 0x0200;
302 DescriptorData
->AuthInfo
.Hdr
.wCertificateType
= WIN_CERT_TYPE_EFI_GUID
;
303 CopyGuid (&DescriptorData
->AuthInfo
.CertType
, &gEfiCertPkcs7Guid
);
305 if (Payload
!= NULL
) {
309 *DataSize
= DescriptorSize
+ PayloadSize
;
315 Internal helper function to delete a Variable given its name and GUID, NO authentication
318 @param[in] VariableName Name of the Variable.
319 @param[in] VendorGuid GUID of the Variable.
321 @retval EFI_SUCCESS Variable deleted successfully.
322 @retval Others The driver failed to start the device.
327 IN CHAR16
*VariableName
,
328 IN EFI_GUID
*VendorGuid
337 GetVariable2 (VariableName
, VendorGuid
, &Variable
, NULL
);
338 if (Variable
== NULL
) {
345 Attr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
346 | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
348 Status
= CreateTimeBasedPayload (&DataSize
, &Data
);
349 if (EFI_ERROR (Status
)) {
350 DEBUG ((EFI_D_ERROR
, "Fail to create time-based data payload: %r", Status
));
354 Status
= gRT
->SetVariable (
369 Set the platform secure boot mode into "Custom" or "Standard" mode.
371 @param[in] SecureBootMode New secure boot mode: STANDARD_SECURE_BOOT_MODE or
372 CUSTOM_SECURE_BOOT_MODE.
374 @return EFI_SUCCESS The platform has switched to the special mode successfully.
375 @return other Fail to operate the secure boot mode.
380 IN UINT8 SecureBootMode
383 return gRT
->SetVariable (
384 EFI_CUSTOM_MODE_NAME
,
385 &gEfiCustomModeEnableGuid
,
386 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
393 Generate the PK signature list from the X509 Certificate storing file (.cer)
395 @param[in] X509File FileHandle of X509 Certificate storing file.
396 @param[out] PkCert Point to the data buffer to store the signature list.
398 @return EFI_UNSUPPORTED Unsupported Key Length.
399 @return EFI_OUT_OF_RESOURCES There are not enough memory resourses to form the signature list.
403 CreatePkX509SignatureList (
404 IN EFI_FILE_HANDLE X509File
,
405 OUT EFI_SIGNATURE_LIST
**PkCert
411 EFI_SIGNATURE_DATA
*PkCertData
;
417 Status
= ReadFileContent (X509File
, (VOID
**) &X509Data
, &X509DataSize
, 0);
418 if (EFI_ERROR (Status
)) {
421 ASSERT (X509Data
!= NULL
);
424 // Allocate space for PK certificate list and initialize it.
425 // Create PK database entry with SignatureHeaderSize equals 0.
427 *PkCert
= (EFI_SIGNATURE_LIST
*) AllocateZeroPool (
428 sizeof(EFI_SIGNATURE_LIST
) + sizeof(EFI_SIGNATURE_DATA
) - 1
431 if (*PkCert
== NULL
) {
432 Status
= EFI_OUT_OF_RESOURCES
;
436 (*PkCert
)->SignatureListSize
= (UINT32
) (sizeof(EFI_SIGNATURE_LIST
)
437 + sizeof(EFI_SIGNATURE_DATA
) - 1
439 (*PkCert
)->SignatureSize
= (UINT32
) (sizeof(EFI_SIGNATURE_DATA
) - 1 + X509DataSize
);
440 (*PkCert
)->SignatureHeaderSize
= 0;
441 CopyGuid (&(*PkCert
)->SignatureType
, &gEfiCertX509Guid
);
442 PkCertData
= (EFI_SIGNATURE_DATA
*) ((UINTN
)(*PkCert
)
443 + sizeof(EFI_SIGNATURE_LIST
)
444 + (*PkCert
)->SignatureHeaderSize
);
445 CopyGuid (&PkCertData
->SignatureOwner
, &gEfiGlobalVariableGuid
);
447 // Fill the PK database with PKpub data from X509 certificate file.
449 CopyMem (&(PkCertData
->SignatureData
[0]), X509Data
, X509DataSize
);
453 if (X509Data
!= NULL
) {
457 if (EFI_ERROR(Status
) && *PkCert
!= NULL
) {
466 Enroll new PK into the System without original PK's authentication.
468 The SignatureOwner GUID will be the same with PK's vendorguid.
470 @param[in] PrivateData The module's private data.
472 @retval EFI_SUCCESS New PK enrolled successfully.
473 @retval EFI_INVALID_PARAMETER The parameter is invalid.
474 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
479 IN SECUREBOOT_CONFIG_PRIVATE_DATA
* Private
485 EFI_SIGNATURE_LIST
*PkCert
;
489 if (Private
->FileContext
->FileName
== NULL
) {
490 return EFI_INVALID_PARAMETER
;
495 Status
= SetSecureBootMode(CUSTOM_SECURE_BOOT_MODE
);
496 if (EFI_ERROR (Status
)) {
501 // Parse the file's postfix. Only support DER encoded X.509 certificate files.
503 NameLength
= StrLen (Private
->FileContext
->FileName
);
504 if (NameLength
<= 4) {
505 return EFI_INVALID_PARAMETER
;
507 FilePostFix
= Private
->FileContext
->FileName
+ NameLength
- 4;
508 if (!IsDerEncodeCertificate(FilePostFix
)) {
509 DEBUG ((EFI_D_ERROR
, "Unsupported file type, only DER encoded certificate (%s) is supported.", mSupportX509Suffix
));
510 return EFI_INVALID_PARAMETER
;
512 DEBUG ((EFI_D_INFO
, "FileName= %s\n", Private
->FileContext
->FileName
));
513 DEBUG ((EFI_D_INFO
, "FilePostFix = %s\n", FilePostFix
));
516 // Prase the selected PK file and generature PK certificate list.
518 Status
= CreatePkX509SignatureList (
519 Private
->FileContext
->FHandle
,
522 if (EFI_ERROR (Status
)) {
525 ASSERT (PkCert
!= NULL
);
528 // Set Platform Key variable.
530 Attr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
531 | EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
532 DataSize
= PkCert
->SignatureListSize
;
533 Status
= CreateTimeBasedPayload (&DataSize
, (UINT8
**) &PkCert
);
534 if (EFI_ERROR (Status
)) {
535 DEBUG ((EFI_D_ERROR
, "Fail to create time-based data payload: %r", Status
));
539 Status
= gRT
->SetVariable(
540 EFI_PLATFORM_KEY_NAME
,
541 &gEfiGlobalVariableGuid
,
546 if (EFI_ERROR (Status
)) {
547 if (Status
== EFI_OUT_OF_RESOURCES
) {
548 DEBUG ((EFI_D_ERROR
, "Enroll PK failed with out of resource.\n"));
555 if (PkCert
!= NULL
) {
559 CloseEnrolledFile(Private
->FileContext
);
565 Remove the PK variable.
567 @retval EFI_SUCCESS Delete PK successfully.
568 @retval Others Could not allow to delete PK.
578 Status
= SetSecureBootMode(CUSTOM_SECURE_BOOT_MODE
);
579 if (EFI_ERROR (Status
)) {
583 Status
= DeleteVariable (
584 EFI_PLATFORM_KEY_NAME
,
585 &gEfiGlobalVariableGuid
591 Enroll a new KEK item from public key storing file (*.pbk).
593 @param[in] PrivateData The module's private data.
595 @retval EFI_SUCCESS New KEK enrolled successfully.
596 @retval EFI_INVALID_PARAMETER The parameter is invalid.
597 @retval EFI_UNSUPPORTED Unsupported command.
598 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
603 IN SECUREBOOT_CONFIG_PRIVATE_DATA
*Private
609 EFI_SIGNATURE_LIST
*KekSigList
;
612 CPL_KEY_INFO
*KeyInfo
;
613 EFI_SIGNATURE_DATA
*KEKSigData
;
614 UINTN KekSigListSize
;
629 // Form the KeKpub certificate list into EFI_SIGNATURE_LIST type.
630 // First, We have to parse out public key data from the pbk key file.
632 Status
= ReadFileContent (
633 Private
->FileContext
->FHandle
,
638 if (EFI_ERROR (Status
)) {
641 ASSERT (KeyBlob
!= NULL
);
642 KeyInfo
= (CPL_KEY_INFO
*) KeyBlob
;
643 if (KeyInfo
->KeyLengthInBits
/ 8 != WIN_CERT_UEFI_RSA2048_SIZE
) {
644 DEBUG ((DEBUG_ERROR
, "Unsupported key length, Only RSA2048 is supported.\n"));
645 Status
= EFI_UNSUPPORTED
;
650 // Convert the Public key to fix octet string format represented in RSA PKCS#1.
652 KeyLenInBytes
= KeyInfo
->KeyLengthInBits
/ 8;
653 KeyBuffer
= AllocateZeroPool (KeyLenInBytes
);
654 if (KeyBuffer
== NULL
) {
655 Status
= EFI_OUT_OF_RESOURCES
;
659 (UINTN
*) (KeyBlob
+ sizeof (CPL_KEY_INFO
)),
660 KeyLenInBytes
/ sizeof (UINTN
),
664 CopyMem(KeyBlob
+ sizeof(CPL_KEY_INFO
), KeyBuffer
, KeyLenInBytes
);
667 // Form an new EFI_SIGNATURE_LIST.
669 KekSigListSize
= sizeof(EFI_SIGNATURE_LIST
)
670 + sizeof(EFI_SIGNATURE_DATA
) - 1
671 + WIN_CERT_UEFI_RSA2048_SIZE
;
673 KekSigList
= (EFI_SIGNATURE_LIST
*) AllocateZeroPool (KekSigListSize
);
674 if (KekSigList
== NULL
) {
675 Status
= EFI_OUT_OF_RESOURCES
;
679 KekSigList
->SignatureListSize
= sizeof(EFI_SIGNATURE_LIST
)
680 + sizeof(EFI_SIGNATURE_DATA
) - 1
681 + WIN_CERT_UEFI_RSA2048_SIZE
;
682 KekSigList
->SignatureHeaderSize
= 0;
683 KekSigList
->SignatureSize
= sizeof(EFI_SIGNATURE_DATA
) - 1 + WIN_CERT_UEFI_RSA2048_SIZE
;
684 CopyGuid (&KekSigList
->SignatureType
, &gEfiCertRsa2048Guid
);
686 KEKSigData
= (EFI_SIGNATURE_DATA
*)((UINT8
*)KekSigList
+ sizeof(EFI_SIGNATURE_LIST
));
687 CopyGuid (&KEKSigData
->SignatureOwner
, Private
->SignatureGUID
);
689 KEKSigData
->SignatureData
,
690 KeyBlob
+ sizeof(CPL_KEY_INFO
),
691 WIN_CERT_UEFI_RSA2048_SIZE
695 // Check if KEK entry has been already existed.
696 // If true, use EFI_VARIABLE_APPEND_WRITE attribute to append the
697 // new KEK to original variable.
699 Attr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
700 | EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
701 Status
= CreateTimeBasedPayload (&KekSigListSize
, (UINT8
**) &KekSigList
);
702 if (EFI_ERROR (Status
)) {
703 DEBUG ((EFI_D_ERROR
, "Fail to create time-based data payload: %r", Status
));
707 Status
= gRT
->GetVariable(
708 EFI_KEY_EXCHANGE_KEY_NAME
,
709 &gEfiGlobalVariableGuid
,
714 if (Status
== EFI_BUFFER_TOO_SMALL
) {
715 Attr
|= EFI_VARIABLE_APPEND_WRITE
;
716 } else if (Status
!= EFI_NOT_FOUND
) {
721 // Done. Now we have formed the correct KEKpub database item, just set it into variable storage,
723 Status
= gRT
->SetVariable(
724 EFI_KEY_EXCHANGE_KEY_NAME
,
725 &gEfiGlobalVariableGuid
,
730 if (EFI_ERROR (Status
)) {
736 CloseEnrolledFile(Private
->FileContext
);
738 if (Private
->SignatureGUID
!= NULL
) {
739 FreePool (Private
->SignatureGUID
);
740 Private
->SignatureGUID
= NULL
;
743 if (KeyBlob
!= NULL
) {
746 if (KeyBuffer
!= NULL
) {
747 FreePool (KeyBuffer
);
749 if (KekSigList
!= NULL
) {
750 FreePool (KekSigList
);
757 Enroll a new KEK item from X509 certificate file.
759 @param[in] PrivateData The module's private data.
761 @retval EFI_SUCCESS New X509 is enrolled successfully.
762 @retval EFI_INVALID_PARAMETER The parameter is invalid.
763 @retval EFI_UNSUPPORTED Unsupported command.
764 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
769 IN SECUREBOOT_CONFIG_PRIVATE_DATA
*Private
775 EFI_SIGNATURE_DATA
*KEKSigData
;
776 EFI_SIGNATURE_LIST
*KekSigList
;
778 UINTN KekSigListSize
;
788 Status
= ReadFileContent (
789 Private
->FileContext
->FHandle
,
794 if (EFI_ERROR (Status
)) {
797 ASSERT (X509Data
!= NULL
);
799 KekSigListSize
= sizeof(EFI_SIGNATURE_LIST
) + sizeof(EFI_SIGNATURE_DATA
) - 1 + X509DataSize
;
800 KekSigList
= (EFI_SIGNATURE_LIST
*) AllocateZeroPool (KekSigListSize
);
801 if (KekSigList
== NULL
) {
802 Status
= EFI_OUT_OF_RESOURCES
;
807 // Fill Certificate Database parameters.
809 KekSigList
->SignatureListSize
= (UINT32
) KekSigListSize
;
810 KekSigList
->SignatureHeaderSize
= 0;
811 KekSigList
->SignatureSize
= (UINT32
) (sizeof(EFI_SIGNATURE_DATA
) - 1 + X509DataSize
);
812 CopyGuid (&KekSigList
->SignatureType
, &gEfiCertX509Guid
);
814 KEKSigData
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) KekSigList
+ sizeof (EFI_SIGNATURE_LIST
));
815 CopyGuid (&KEKSigData
->SignatureOwner
, Private
->SignatureGUID
);
816 CopyMem (KEKSigData
->SignatureData
, X509Data
, X509DataSize
);
819 // Check if KEK been already existed.
820 // If true, use EFI_VARIABLE_APPEND_WRITE attribute to append the
821 // new kek to original variable
823 Attr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
824 | EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
825 Status
= CreateTimeBasedPayload (&KekSigListSize
, (UINT8
**) &KekSigList
);
826 if (EFI_ERROR (Status
)) {
827 DEBUG ((EFI_D_ERROR
, "Fail to create time-based data payload: %r", Status
));
831 Status
= gRT
->GetVariable(
832 EFI_KEY_EXCHANGE_KEY_NAME
,
833 &gEfiGlobalVariableGuid
,
838 if (Status
== EFI_BUFFER_TOO_SMALL
) {
839 Attr
|= EFI_VARIABLE_APPEND_WRITE
;
840 } else if (Status
!= EFI_NOT_FOUND
) {
844 Status
= gRT
->SetVariable(
845 EFI_KEY_EXCHANGE_KEY_NAME
,
846 &gEfiGlobalVariableGuid
,
851 if (EFI_ERROR (Status
)) {
857 CloseEnrolledFile(Private
->FileContext
);
859 if (Private
->SignatureGUID
!= NULL
) {
860 FreePool (Private
->SignatureGUID
);
861 Private
->SignatureGUID
= NULL
;
864 if (KekSigList
!= NULL
) {
865 FreePool (KekSigList
);
872 Enroll new KEK into the System without PK's authentication.
873 The SignatureOwner GUID will be Private->SignatureGUID.
875 @param[in] PrivateData The module's private data.
877 @retval EFI_SUCCESS New KEK enrolled successful.
878 @retval EFI_INVALID_PARAMETER The parameter is invalid.
879 @retval others Fail to enroll KEK data.
883 EnrollKeyExchangeKey (
884 IN SECUREBOOT_CONFIG_PRIVATE_DATA
*Private
891 if ((Private
->FileContext
->FHandle
== NULL
) || (Private
->FileContext
->FileName
== NULL
) || (Private
->SignatureGUID
== NULL
)) {
892 return EFI_INVALID_PARAMETER
;
895 Status
= SetSecureBootMode(CUSTOM_SECURE_BOOT_MODE
);
896 if (EFI_ERROR (Status
)) {
901 // Parse the file's postfix. Supports DER-encoded X509 certificate,
902 // and .pbk as RSA public key file.
904 NameLength
= StrLen (Private
->FileContext
->FileName
);
905 if (NameLength
<= 4) {
906 return EFI_INVALID_PARAMETER
;
908 FilePostFix
= Private
->FileContext
->FileName
+ NameLength
- 4;
909 if (IsDerEncodeCertificate(FilePostFix
)) {
910 return EnrollX509ToKek (Private
);
911 } else if (CompareMem (FilePostFix
, L
".pbk",4) == 0) {
912 return EnrollRsa2048ToKek (Private
);
915 // File type is wrong, simply close it
917 CloseEnrolledFile(Private
->FileContext
);
919 return EFI_INVALID_PARAMETER
;
924 Enroll a new X509 certificate into Signature Database (DB or DBX or DBT) without
925 KEK's authentication.
927 @param[in] PrivateData The module's private data.
928 @param[in] VariableName Variable name of signature database, must be
929 EFI_IMAGE_SECURITY_DATABASE or EFI_IMAGE_SECURITY_DATABASE1.
931 @retval EFI_SUCCESS New X509 is enrolled successfully.
932 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
937 IN SECUREBOOT_CONFIG_PRIVATE_DATA
*Private
,
938 IN CHAR16
*VariableName
944 EFI_SIGNATURE_LIST
*SigDBCert
;
945 EFI_SIGNATURE_DATA
*SigDBCertData
;
956 SigDBCertData
= NULL
;
959 Status
= ReadFileContent (
960 Private
->FileContext
->FHandle
,
965 if (EFI_ERROR (Status
)) {
968 ASSERT (X509Data
!= NULL
);
970 SigDBSize
= sizeof(EFI_SIGNATURE_LIST
) + sizeof(EFI_SIGNATURE_DATA
) - 1 + X509DataSize
;
972 Data
= AllocateZeroPool (SigDBSize
);
974 Status
= EFI_OUT_OF_RESOURCES
;
979 // Fill Certificate Database parameters.
981 SigDBCert
= (EFI_SIGNATURE_LIST
*) Data
;
982 SigDBCert
->SignatureListSize
= (UINT32
) SigDBSize
;
983 SigDBCert
->SignatureHeaderSize
= 0;
984 SigDBCert
->SignatureSize
= (UINT32
) (sizeof(EFI_SIGNATURE_DATA
) - 1 + X509DataSize
);
985 CopyGuid (&SigDBCert
->SignatureType
, &gEfiCertX509Guid
);
987 SigDBCertData
= (EFI_SIGNATURE_DATA
*) ((UINT8
* ) SigDBCert
+ sizeof (EFI_SIGNATURE_LIST
));
988 CopyGuid (&SigDBCertData
->SignatureOwner
, Private
->SignatureGUID
);
989 CopyMem ((UINT8
* ) (SigDBCertData
->SignatureData
), X509Data
, X509DataSize
);
992 // Check if signature database entry has been already existed.
993 // If true, use EFI_VARIABLE_APPEND_WRITE attribute to append the
994 // new signature data to original variable
996 Attr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
997 | EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
998 Status
= CreateTimeBasedPayload (&SigDBSize
, (UINT8
**) &Data
);
999 if (EFI_ERROR (Status
)) {
1000 DEBUG ((EFI_D_ERROR
, "Fail to create time-based data payload: %r", Status
));
1004 Status
= gRT
->GetVariable(
1006 &gEfiImageSecurityDatabaseGuid
,
1011 if (Status
== EFI_BUFFER_TOO_SMALL
) {
1012 Attr
|= EFI_VARIABLE_APPEND_WRITE
;
1013 } else if (Status
!= EFI_NOT_FOUND
) {
1017 Status
= gRT
->SetVariable(
1019 &gEfiImageSecurityDatabaseGuid
,
1024 if (EFI_ERROR (Status
)) {
1030 CloseEnrolledFile(Private
->FileContext
);
1032 if (Private
->SignatureGUID
!= NULL
) {
1033 FreePool (Private
->SignatureGUID
);
1034 Private
->SignatureGUID
= NULL
;
1041 if (X509Data
!= NULL
) {
1042 FreePool (X509Data
);
1049 Check whether signature is in specified database.
1051 @param[in] VariableName Name of database variable that is searched in.
1052 @param[in] Signature Pointer to signature that is searched for.
1053 @param[in] SignatureSize Size of Signature.
1055 @return TRUE Found the signature in the variable database.
1056 @return FALSE Not found the signature in the variable database.
1060 IsSignatureFoundInDatabase (
1061 IN CHAR16
*VariableName
,
1062 IN UINT8
*Signature
,
1063 IN UINTN SignatureSize
1067 EFI_SIGNATURE_LIST
*CertList
;
1068 EFI_SIGNATURE_DATA
*Cert
;
1076 // Read signature database variable.
1081 Status
= gRT
->GetVariable (VariableName
, &gEfiImageSecurityDatabaseGuid
, NULL
, &DataSize
, NULL
);
1082 if (Status
!= EFI_BUFFER_TOO_SMALL
) {
1086 Data
= (UINT8
*) AllocateZeroPool (DataSize
);
1091 Status
= gRT
->GetVariable (VariableName
, &gEfiImageSecurityDatabaseGuid
, NULL
, &DataSize
, Data
);
1092 if (EFI_ERROR (Status
)) {
1097 // Enumerate all signature data in SigDB to check if executable's signature exists.
1099 CertList
= (EFI_SIGNATURE_LIST
*) Data
;
1100 while ((DataSize
> 0) && (DataSize
>= CertList
->SignatureListSize
)) {
1101 CertCount
= (CertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - CertList
->SignatureHeaderSize
) / CertList
->SignatureSize
;
1102 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
1103 if ((CertList
->SignatureSize
== sizeof(EFI_SIGNATURE_DATA
) - 1 + SignatureSize
) && (CompareGuid(&CertList
->SignatureType
, &gEfiCertX509Guid
))) {
1104 for (Index
= 0; Index
< CertCount
; Index
++) {
1105 if (CompareMem (Cert
->SignatureData
, Signature
, SignatureSize
) == 0) {
1107 // Find the signature in database.
1112 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) Cert
+ CertList
->SignatureSize
);
1120 DataSize
-= CertList
->SignatureListSize
;
1121 CertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) CertList
+ CertList
->SignatureListSize
);
1133 Calculate the hash of a certificate data with the specified hash algorithm.
1135 @param[in] CertData The certificate data to be hashed.
1136 @param[in] CertSize The certificate size in bytes.
1137 @param[in] HashAlg The specified hash algorithm.
1138 @param[out] CertHash The output digest of the certificate
1140 @retval TRUE Successfully got the hash of the CertData.
1141 @retval FALSE Failed to get the hash of CertData.
1161 if (HashAlg
>= HASHALG_MAX
) {
1166 // Retrieve the TBSCertificate for Hash Calculation.
1168 if (!X509GetTBSCert (CertData
, CertSize
, &TBSCert
, &TBSCertSize
)) {
1173 // 1. Initialize context of hash.
1175 CtxSize
= mHash
[HashAlg
].GetContextSize ();
1176 HashCtx
= AllocatePool (CtxSize
);
1177 ASSERT (HashCtx
!= NULL
);
1180 // 2. Initialize a hash context.
1182 Status
= mHash
[HashAlg
].HashInit (HashCtx
);
1188 // 3. Calculate the hash.
1190 Status
= mHash
[HashAlg
].HashUpdate (HashCtx
, TBSCert
, TBSCertSize
);
1196 // 4. Get the hash result.
1198 ZeroMem (CertHash
, mHash
[HashAlg
].DigestLength
);
1199 Status
= mHash
[HashAlg
].HashFinal (HashCtx
, CertHash
);
1202 if (HashCtx
!= NULL
) {
1210 Check whether the hash of an X.509 certificate is in forbidden database (DBX).
1212 @param[in] Certificate Pointer to X.509 Certificate that is searched for.
1213 @param[in] CertSize Size of X.509 Certificate.
1215 @return TRUE Found the certificate hash in the forbidden database.
1216 @return FALSE Certificate hash is Not found in the forbidden database.
1220 IsCertHashFoundInDbx (
1221 IN UINT8
*Certificate
,
1227 EFI_SIGNATURE_LIST
*DbxList
;
1228 EFI_SIGNATURE_DATA
*CertHash
;
1229 UINTN CertHashCount
;
1232 UINT8 CertDigest
[MAX_DIGEST_SIZE
];
1234 UINTN SiglistHeaderSize
;
1239 HashAlg
= HASHALG_MAX
;
1243 // Read signature database variable.
1246 Status
= gRT
->GetVariable (EFI_IMAGE_SECURITY_DATABASE1
, &gEfiImageSecurityDatabaseGuid
, NULL
, &DataSize
, NULL
);
1247 if (Status
!= EFI_BUFFER_TOO_SMALL
) {
1251 Data
= (UINT8
*) AllocateZeroPool (DataSize
);
1256 Status
= gRT
->GetVariable (EFI_IMAGE_SECURITY_DATABASE1
, &gEfiImageSecurityDatabaseGuid
, NULL
, &DataSize
, Data
);
1257 if (EFI_ERROR (Status
)) {
1262 // Check whether the certificate hash exists in the forbidden database.
1264 DbxList
= (EFI_SIGNATURE_LIST
*) Data
;
1265 while ((DataSize
> 0) && (DataSize
>= DbxList
->SignatureListSize
)) {
1267 // Determine Hash Algorithm of Certificate in the forbidden database.
1269 if (CompareGuid (&DbxList
->SignatureType
, &gEfiCertX509Sha256Guid
)) {
1270 HashAlg
= HASHALG_SHA256
;
1271 } else if (CompareGuid (&DbxList
->SignatureType
, &gEfiCertX509Sha384Guid
)) {
1272 HashAlg
= HASHALG_SHA384
;
1273 } else if (CompareGuid (&DbxList
->SignatureType
, &gEfiCertX509Sha512Guid
)) {
1274 HashAlg
= HASHALG_SHA512
;
1276 DataSize
-= DbxList
->SignatureListSize
;
1277 DbxList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) DbxList
+ DbxList
->SignatureListSize
);
1282 // Calculate the hash value of current db certificate for comparision.
1284 if (!CalculateCertHash (Certificate
, CertSize
, HashAlg
, CertDigest
)) {
1288 SiglistHeaderSize
= sizeof (EFI_SIGNATURE_LIST
) + DbxList
->SignatureHeaderSize
;
1289 CertHash
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) DbxList
+ SiglistHeaderSize
);
1290 CertHashCount
= (DbxList
->SignatureListSize
- SiglistHeaderSize
) / DbxList
->SignatureSize
;
1291 for (Index
= 0; Index
< CertHashCount
; Index
++) {
1293 // Iterate each Signature Data Node within this CertList for verify.
1295 DbxCertHash
= CertHash
->SignatureData
;
1296 if (CompareMem (DbxCertHash
, CertDigest
, mHash
[HashAlg
].DigestLength
) == 0) {
1298 // Hash of Certificate is found in forbidden database.
1303 CertHash
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertHash
+ DbxList
->SignatureSize
);
1306 DataSize
-= DbxList
->SignatureListSize
;
1307 DbxList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) DbxList
+ DbxList
->SignatureListSize
);
1319 Check whether the signature list exists in given variable data.
1321 It searches the signature list for the ceritificate hash by CertType.
1322 If the signature list is found, get the offset of Database for the
1323 next hash of a certificate.
1325 @param[in] Database Variable data to save signature list.
1326 @param[in] DatabaseSize Variable size.
1327 @param[in] SignatureType The type of the signature.
1328 @param[out] Offset The offset to save a new hash of certificate.
1330 @return TRUE The signature list is found in the forbidden database.
1331 @return FALSE The signature list is not found in the forbidden database.
1334 GetSignaturelistOffset (
1335 IN EFI_SIGNATURE_LIST
*Database
,
1336 IN UINTN DatabaseSize
,
1337 IN EFI_GUID
*SignatureType
,
1341 EFI_SIGNATURE_LIST
*SigList
;
1344 if ((Database
== NULL
) || (DatabaseSize
== 0)) {
1350 SiglistSize
= DatabaseSize
;
1351 while ((SiglistSize
> 0) && (SiglistSize
>= SigList
->SignatureListSize
)) {
1352 if (CompareGuid (&SigList
->SignatureType
, SignatureType
)) {
1353 *Offset
= DatabaseSize
- SiglistSize
;
1356 SiglistSize
-= SigList
->SignatureListSize
;
1357 SigList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) SigList
+ SigList
->SignatureListSize
);
1364 Enroll a new X509 certificate hash into Signature Database (dbx) without
1365 KEK's authentication.
1367 @param[in] PrivateData The module's private data.
1368 @param[in] HashAlg The hash algorithm to enroll the certificate.
1369 @param[in] RevocationDate The revocation date of the certificate.
1370 @param[in] RevocationTime The revocation time of the certificate.
1371 @param[in] AlwaysRevocation Indicate whether the certificate is always revoked.
1373 @retval EFI_SUCCESS New X509 is enrolled successfully.
1374 @retval EFI_INVALID_PARAMETER The parameter is invalid.
1375 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
1379 EnrollX509HashtoSigDB (
1380 IN SECUREBOOT_CONFIG_PRIVATE_DATA
*Private
,
1382 IN EFI_HII_DATE
*RevocationDate
,
1383 IN EFI_HII_TIME
*RevocationTime
,
1384 IN BOOLEAN AlwaysRevocation
1390 EFI_SIGNATURE_LIST
*SignatureList
;
1391 UINTN SignatureListSize
;
1397 EFI_SIGNATURE_DATA
*SignatureData
;
1398 UINTN SignatureSize
;
1399 EFI_GUID SignatureType
;
1401 UINT8 CertHash
[MAX_DIGEST_SIZE
];
1402 UINT16
* FilePostFix
;
1409 SignatureData
= NULL
;
1410 SignatureList
= NULL
;
1414 if ((Private
->FileContext
->FileName
== NULL
) || (Private
->FileContext
->FHandle
== NULL
) || (Private
->SignatureGUID
== NULL
)) {
1415 return EFI_INVALID_PARAMETER
;
1418 Status
= SetSecureBootMode (CUSTOM_SECURE_BOOT_MODE
);
1419 if (EFI_ERROR (Status
)) {
1424 // Parse the file's postfix.
1426 NameLength
= StrLen (Private
->FileContext
->FileName
);
1427 if (NameLength
<= 4) {
1428 return EFI_INVALID_PARAMETER
;
1430 FilePostFix
= Private
->FileContext
->FileName
+ NameLength
- 4;
1431 if (!IsDerEncodeCertificate(FilePostFix
)) {
1433 // Only supports DER-encoded X509 certificate.
1435 return EFI_INVALID_PARAMETER
;
1439 // Get the certificate from file and calculate its hash.
1441 Status
= ReadFileContent (
1442 Private
->FileContext
->FHandle
,
1447 if (EFI_ERROR (Status
)) {
1450 ASSERT (X509Data
!= NULL
);
1452 if (!CalculateCertHash (X509Data
, X509DataSize
, HashAlg
, CertHash
)) {
1457 // Get the variable for enrollment.
1460 Status
= gRT
->GetVariable (EFI_IMAGE_SECURITY_DATABASE1
, &gEfiImageSecurityDatabaseGuid
, NULL
, &DataSize
, NULL
);
1461 if (Status
== EFI_BUFFER_TOO_SMALL
) {
1462 Data
= (UINT8
*) AllocateZeroPool (DataSize
);
1464 return EFI_OUT_OF_RESOURCES
;
1467 Status
= gRT
->GetVariable (EFI_IMAGE_SECURITY_DATABASE1
, &gEfiImageSecurityDatabaseGuid
, NULL
, &DataSize
, Data
);
1468 if (EFI_ERROR (Status
)) {
1474 // Allocate memory for Signature and fill the Signature
1476 SignatureSize
= sizeof(EFI_SIGNATURE_DATA
) - 1 + sizeof (EFI_TIME
) + mHash
[HashAlg
].DigestLength
;
1477 SignatureData
= (EFI_SIGNATURE_DATA
*) AllocateZeroPool (SignatureSize
);
1478 if (SignatureData
== NULL
) {
1479 return EFI_OUT_OF_RESOURCES
;
1481 CopyGuid (&SignatureData
->SignatureOwner
, Private
->SignatureGUID
);
1482 CopyMem (SignatureData
->SignatureData
, CertHash
, mHash
[HashAlg
].DigestLength
);
1487 if (!AlwaysRevocation
) {
1488 Time
= (EFI_TIME
*)(&SignatureData
->SignatureData
+ mHash
[HashAlg
].DigestLength
);
1489 Time
->Year
= RevocationDate
->Year
;
1490 Time
->Month
= RevocationDate
->Month
;
1491 Time
->Day
= RevocationDate
->Day
;
1492 Time
->Hour
= RevocationTime
->Hour
;
1493 Time
->Minute
= RevocationTime
->Minute
;
1494 Time
->Second
= RevocationTime
->Second
;
1498 // Determine the GUID for certificate hash.
1501 case HASHALG_SHA256
:
1502 SignatureType
= gEfiCertX509Sha256Guid
;
1504 case HASHALG_SHA384
:
1505 SignatureType
= gEfiCertX509Sha384Guid
;
1507 case HASHALG_SHA512
:
1508 SignatureType
= gEfiCertX509Sha512Guid
;
1515 // Add signature into the new variable data buffer
1517 if (GetSignaturelistOffset((EFI_SIGNATURE_LIST
*)Data
, DataSize
, &SignatureType
, &Offset
)) {
1519 // Add the signature to the found signaturelist.
1521 DbSize
= DataSize
+ SignatureSize
;
1522 NewData
= AllocateZeroPool (DbSize
);
1523 if (NewData
== NULL
) {
1524 Status
= EFI_OUT_OF_RESOURCES
;
1528 SignatureList
= (EFI_SIGNATURE_LIST
*)(Data
+ Offset
);
1529 SignatureListSize
= (UINTN
) ReadUnaligned32 ((UINT32
*)&SignatureList
->SignatureListSize
);
1530 CopyMem (NewData
, Data
, Offset
+ SignatureListSize
);
1532 SignatureList
= (EFI_SIGNATURE_LIST
*)(NewData
+ Offset
);
1533 WriteUnaligned32 ((UINT32
*) &SignatureList
->SignatureListSize
, (UINT32
)(SignatureListSize
+ SignatureSize
));
1535 Offset
+= SignatureListSize
;
1536 CopyMem (NewData
+ Offset
, SignatureData
, SignatureSize
);
1537 CopyMem (NewData
+ Offset
+ SignatureSize
, Data
+ Offset
, DataSize
- Offset
);
1544 // Create a new signaturelist, and add the signature into the signaturelist.
1546 DbSize
= DataSize
+ sizeof(EFI_SIGNATURE_LIST
) + SignatureSize
;
1547 NewData
= AllocateZeroPool (DbSize
);
1548 if (NewData
== NULL
) {
1549 Status
= EFI_OUT_OF_RESOURCES
;
1553 // Fill Certificate Database parameters.
1555 SignatureList
= (EFI_SIGNATURE_LIST
*) (NewData
+ DataSize
);
1556 SignatureListSize
= sizeof(EFI_SIGNATURE_LIST
) + SignatureSize
;
1557 WriteUnaligned32 ((UINT32
*) &SignatureList
->SignatureListSize
, (UINT32
) SignatureListSize
);
1558 WriteUnaligned32 ((UINT32
*) &SignatureList
->SignatureSize
, (UINT32
) SignatureSize
);
1559 CopyGuid (&SignatureList
->SignatureType
, &SignatureType
);
1560 CopyMem ((UINT8
* ) SignatureList
+ sizeof (EFI_SIGNATURE_LIST
), SignatureData
, SignatureSize
);
1561 if ((DataSize
!= 0) && (Data
!= NULL
)) {
1562 CopyMem (NewData
, Data
, DataSize
);
1569 Status
= CreateTimeBasedPayload (&DataSize
, (UINT8
**) &Data
);
1570 if (EFI_ERROR (Status
)) {
1574 Attr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
1575 | EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
1576 Status
= gRT
->SetVariable(
1577 EFI_IMAGE_SECURITY_DATABASE1
,
1578 &gEfiImageSecurityDatabaseGuid
,
1583 if (EFI_ERROR (Status
)) {
1589 CloseEnrolledFile(Private
->FileContext
);
1591 if (Private
->SignatureGUID
!= NULL
) {
1592 FreePool (Private
->SignatureGUID
);
1593 Private
->SignatureGUID
= NULL
;
1600 if (SignatureData
!= NULL
) {
1601 FreePool (SignatureData
);
1604 if (X509Data
!= NULL
) {
1605 FreePool (X509Data
);
1612 Check whether a certificate from a file exists in dbx.
1614 @param[in] PrivateData The module's private data.
1615 @param[in] VariableName Variable name of signature database, must be
1616 EFI_IMAGE_SECURITY_DATABASE1.
1618 @retval TRUE The X509 certificate is found in dbx successfully.
1619 @retval FALSE The X509 certificate is not found in dbx.
1623 IN SECUREBOOT_CONFIG_PRIVATE_DATA
*Private
,
1624 IN CHAR16
*VariableName
1633 // Read the certificate from file
1637 Status
= ReadFileContent (
1638 Private
->FileContext
->FHandle
,
1643 if (EFI_ERROR (Status
)) {
1648 // Check the raw certificate.
1651 if (IsSignatureFoundInDatabase (EFI_IMAGE_SECURITY_DATABASE1
, X509Data
, X509DataSize
)) {
1657 // Check the hash of certificate.
1659 if (IsCertHashFoundInDbx (X509Data
, X509DataSize
)) {
1665 if (X509Data
!= NULL
) {
1666 FreePool (X509Data
);
1673 Reads contents of a PE/COFF image in memory buffer.
1675 Caution: This function may receive untrusted input.
1676 PE/COFF image is external input, so this function will make sure the PE/COFF image content
1677 read is within the image buffer.
1679 @param FileHandle Pointer to the file handle to read the PE/COFF image.
1680 @param FileOffset Offset into the PE/COFF image to begin the read operation.
1681 @param ReadSize On input, the size in bytes of the requested read operation.
1682 On output, the number of bytes actually read.
1683 @param Buffer Output buffer that contains the data read from the PE/COFF image.
1685 @retval EFI_SUCCESS The specified portion of the PE/COFF image was read and the size
1689 SecureBootConfigImageRead (
1690 IN VOID
*FileHandle
,
1691 IN UINTN FileOffset
,
1692 IN OUT UINTN
*ReadSize
,
1698 if (FileHandle
== NULL
|| ReadSize
== NULL
|| Buffer
== NULL
) {
1699 return EFI_INVALID_PARAMETER
;
1702 if (MAX_ADDRESS
- FileOffset
< *ReadSize
) {
1703 return EFI_INVALID_PARAMETER
;
1706 EndPosition
= FileOffset
+ *ReadSize
;
1707 if (EndPosition
> mImageSize
) {
1708 *ReadSize
= (UINT32
)(mImageSize
- FileOffset
);
1711 if (FileOffset
>= mImageSize
) {
1715 CopyMem (Buffer
, (UINT8
*)((UINTN
) FileHandle
+ FileOffset
), *ReadSize
);
1721 Load PE/COFF image information into internal buffer and check its validity.
1723 @retval EFI_SUCCESS Successful
1724 @retval EFI_UNSUPPORTED Invalid PE/COFF file
1725 @retval EFI_ABORTED Serious error occurs, like file I/O error etc.
1733 EFI_IMAGE_DOS_HEADER
*DosHdr
;
1734 EFI_IMAGE_NT_HEADERS32
*NtHeader32
;
1735 EFI_IMAGE_NT_HEADERS64
*NtHeader64
;
1736 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext
;
1742 ZeroMem (&ImageContext
, sizeof (ImageContext
));
1743 ImageContext
.Handle
= (VOID
*) mImageBase
;
1744 ImageContext
.ImageRead
= (PE_COFF_LOADER_READ_FILE
) SecureBootConfigImageRead
;
1747 // Get information about the image being loaded
1749 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
1750 if (EFI_ERROR (Status
)) {
1752 // The information can't be got from the invalid PeImage
1754 DEBUG ((DEBUG_INFO
, "SecureBootConfigDxe: PeImage invalid. \n"));
1759 // Read the Dos header
1761 DosHdr
= (EFI_IMAGE_DOS_HEADER
*)(mImageBase
);
1762 if (DosHdr
->e_magic
== EFI_IMAGE_DOS_SIGNATURE
)
1765 // DOS image header is present,
1766 // So read the PE header after the DOS image header
1768 mPeCoffHeaderOffset
= DosHdr
->e_lfanew
;
1772 mPeCoffHeaderOffset
= 0;
1776 // Read PE header and check the signature validity and machine compatibility
1778 NtHeader32
= (EFI_IMAGE_NT_HEADERS32
*) (mImageBase
+ mPeCoffHeaderOffset
);
1779 if (NtHeader32
->Signature
!= EFI_IMAGE_NT_SIGNATURE
)
1781 return EFI_UNSUPPORTED
;
1784 mNtHeader
.Pe32
= NtHeader32
;
1787 // Check the architecture field of PE header and get the Certificate Data Directory data
1788 // Note the size of FileHeader field is constant for both IA32 and X64 arch
1790 if ((NtHeader32
->FileHeader
.Machine
== EFI_IMAGE_MACHINE_IA32
)
1791 || (NtHeader32
->FileHeader
.Machine
== EFI_IMAGE_MACHINE_EBC
)
1792 || (NtHeader32
->FileHeader
.Machine
== EFI_IMAGE_MACHINE_ARMTHUMB_MIXED
)) {
1794 // 32-bits Architecture
1796 mImageType
= ImageType_IA32
;
1797 mSecDataDir
= (EFI_IMAGE_SECURITY_DATA_DIRECTORY
*) &(NtHeader32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
]);
1799 else if ((NtHeader32
->FileHeader
.Machine
== EFI_IMAGE_MACHINE_IA64
)
1800 || (NtHeader32
->FileHeader
.Machine
== EFI_IMAGE_MACHINE_X64
)
1801 || (NtHeader32
->FileHeader
.Machine
== EFI_IMAGE_MACHINE_AARCH64
)) {
1803 // 64-bits Architecture
1805 mImageType
= ImageType_X64
;
1806 NtHeader64
= (EFI_IMAGE_NT_HEADERS64
*) (mImageBase
+ mPeCoffHeaderOffset
);
1807 mSecDataDir
= (EFI_IMAGE_SECURITY_DATA_DIRECTORY
*) &(NtHeader64
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
]);
1809 return EFI_UNSUPPORTED
;
1816 Calculate hash of Pe/Coff image based on the authenticode image hashing in
1817 PE/COFF Specification 8.0 Appendix A
1819 Notes: PE/COFF image has been checked by BasePeCoffLib PeCoffLoaderGetImageInfo() in
1820 the function LoadPeImage ().
1822 @param[in] HashAlg Hash algorithm type.
1824 @retval TRUE Successfully hash image.
1825 @retval FALSE Fail in hash image.
1835 EFI_IMAGE_SECTION_HEADER
*Section
;
1840 UINTN SumOfBytesHashed
;
1841 EFI_IMAGE_SECTION_HEADER
*SectionHeader
;
1846 SectionHeader
= NULL
;
1849 if (HashAlg
!= HASHALG_SHA256
) {
1854 // Initialize context of hash.
1856 ZeroMem (mImageDigest
, MAX_DIGEST_SIZE
);
1858 mImageDigestSize
= SHA256_DIGEST_SIZE
;
1859 mCertType
= gEfiCertSha256Guid
;
1861 CtxSize
= mHash
[HashAlg
].GetContextSize();
1863 HashCtx
= AllocatePool (CtxSize
);
1864 ASSERT (HashCtx
!= NULL
);
1866 // 1. Load the image header into memory.
1868 // 2. Initialize a SHA hash context.
1869 Status
= mHash
[HashAlg
].HashInit(HashCtx
);
1874 // Measuring PE/COFF Image Header;
1875 // But CheckSum field and SECURITY data directory (certificate) are excluded
1877 if (mNtHeader
.Pe32
->FileHeader
.Machine
== IMAGE_FILE_MACHINE_IA64
&& mNtHeader
.Pe32
->OptionalHeader
.Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
1879 // NOTE: Some versions of Linux ELILO for Itanium have an incorrect magic value
1880 // in the PE/COFF Header. If the MachineType is Itanium(IA64) and the
1881 // Magic value in the OptionalHeader is EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
1882 // then override the magic value to EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
1884 Magic
= EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
;
1887 // Get the magic value from the PE/COFF Optional Header
1889 Magic
= mNtHeader
.Pe32
->OptionalHeader
.Magic
;
1893 // 3. Calculate the distance from the base of the image header to the image checksum address.
1894 // 4. Hash the image header from its base to beginning of the image checksum.
1896 HashBase
= mImageBase
;
1897 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
1901 HashSize
= (UINTN
) (&mNtHeader
.Pe32
->OptionalHeader
.CheckSum
) - (UINTN
) HashBase
;
1904 // Use PE32+ offset.
1906 HashSize
= (UINTN
) (&mNtHeader
.Pe32Plus
->OptionalHeader
.CheckSum
) - (UINTN
) HashBase
;
1909 Status
= mHash
[HashAlg
].HashUpdate(HashCtx
, HashBase
, HashSize
);
1914 // 5. Skip over the image checksum (it occupies a single ULONG).
1915 // 6. Get the address of the beginning of the Cert Directory.
1916 // 7. Hash everything from the end of the checksum to the start of the Cert Directory.
1918 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
1922 HashBase
= (UINT8
*) &mNtHeader
.Pe32
->OptionalHeader
.CheckSum
+ sizeof (UINT32
);
1923 HashSize
= (UINTN
) (&mNtHeader
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
]) - (UINTN
) HashBase
;
1926 // Use PE32+ offset.
1928 HashBase
= (UINT8
*) &mNtHeader
.Pe32Plus
->OptionalHeader
.CheckSum
+ sizeof (UINT32
);
1929 HashSize
= (UINTN
) (&mNtHeader
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
]) - (UINTN
) HashBase
;
1932 Status
= mHash
[HashAlg
].HashUpdate(HashCtx
, HashBase
, HashSize
);
1937 // 8. Skip over the Cert Directory. (It is sizeof(IMAGE_DATA_DIRECTORY) bytes.)
1938 // 9. Hash everything from the end of the Cert Directory to the end of image header.
1940 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
1944 HashBase
= (UINT8
*) &mNtHeader
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
+ 1];
1945 HashSize
= mNtHeader
.Pe32
->OptionalHeader
.SizeOfHeaders
- ((UINTN
) (&mNtHeader
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
+ 1]) - (UINTN
) mImageBase
);
1948 // Use PE32+ offset.
1950 HashBase
= (UINT8
*) &mNtHeader
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
+ 1];
1951 HashSize
= mNtHeader
.Pe32Plus
->OptionalHeader
.SizeOfHeaders
- ((UINTN
) (&mNtHeader
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
+ 1]) - (UINTN
) mImageBase
);
1954 Status
= mHash
[HashAlg
].HashUpdate(HashCtx
, HashBase
, HashSize
);
1959 // 10. Set the SUM_OF_BYTES_HASHED to the size of the header.
1961 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
1965 SumOfBytesHashed
= mNtHeader
.Pe32
->OptionalHeader
.SizeOfHeaders
;
1970 SumOfBytesHashed
= mNtHeader
.Pe32Plus
->OptionalHeader
.SizeOfHeaders
;
1974 // 11. Build a temporary table of pointers to all the IMAGE_SECTION_HEADER
1975 // structures in the image. The 'NumberOfSections' field of the image
1976 // header indicates how big the table should be. Do not include any
1977 // IMAGE_SECTION_HEADERs in the table whose 'SizeOfRawData' field is zero.
1979 SectionHeader
= (EFI_IMAGE_SECTION_HEADER
*) AllocateZeroPool (sizeof (EFI_IMAGE_SECTION_HEADER
) * mNtHeader
.Pe32
->FileHeader
.NumberOfSections
);
1980 ASSERT (SectionHeader
!= NULL
);
1982 // 12. Using the 'PointerToRawData' in the referenced section headers as
1983 // a key, arrange the elements in the table in ascending order. In other
1984 // words, sort the section headers according to the disk-file offset of
1987 Section
= (EFI_IMAGE_SECTION_HEADER
*) (
1989 mPeCoffHeaderOffset
+
1991 sizeof (EFI_IMAGE_FILE_HEADER
) +
1992 mNtHeader
.Pe32
->FileHeader
.SizeOfOptionalHeader
1994 for (Index
= 0; Index
< mNtHeader
.Pe32
->FileHeader
.NumberOfSections
; Index
++) {
1996 while ((Pos
> 0) && (Section
->PointerToRawData
< SectionHeader
[Pos
- 1].PointerToRawData
)) {
1997 CopyMem (&SectionHeader
[Pos
], &SectionHeader
[Pos
- 1], sizeof (EFI_IMAGE_SECTION_HEADER
));
2000 CopyMem (&SectionHeader
[Pos
], Section
, sizeof (EFI_IMAGE_SECTION_HEADER
));
2005 // 13. Walk through the sorted table, bring the corresponding section
2006 // into memory, and hash the entire section (using the 'SizeOfRawData'
2007 // field in the section header to determine the amount of data to hash).
2008 // 14. Add the section's 'SizeOfRawData' to SUM_OF_BYTES_HASHED .
2009 // 15. Repeat steps 13 and 14 for all the sections in the sorted table.
2011 for (Index
= 0; Index
< mNtHeader
.Pe32
->FileHeader
.NumberOfSections
; Index
++) {
2012 Section
= &SectionHeader
[Index
];
2013 if (Section
->SizeOfRawData
== 0) {
2016 HashBase
= mImageBase
+ Section
->PointerToRawData
;
2017 HashSize
= (UINTN
) Section
->SizeOfRawData
;
2019 Status
= mHash
[HashAlg
].HashUpdate(HashCtx
, HashBase
, HashSize
);
2024 SumOfBytesHashed
+= HashSize
;
2028 // 16. If the file size is greater than SUM_OF_BYTES_HASHED, there is extra
2029 // data in the file that needs to be added to the hash. This data begins
2030 // at file offset SUM_OF_BYTES_HASHED and its length is:
2031 // FileSize - (CertDirectory->Size)
2033 if (mImageSize
> SumOfBytesHashed
) {
2034 HashBase
= mImageBase
+ SumOfBytesHashed
;
2035 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
2041 mNtHeader
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
].Size
-
2045 // Use PE32+ offset.
2049 mNtHeader
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
].Size
-
2053 Status
= mHash
[HashAlg
].HashUpdate(HashCtx
, HashBase
, HashSize
);
2059 Status
= mHash
[HashAlg
].HashFinal(HashCtx
, mImageDigest
);
2062 if (HashCtx
!= NULL
) {
2065 if (SectionHeader
!= NULL
) {
2066 FreePool (SectionHeader
);
2072 Recognize the Hash algorithm in PE/COFF Authenticode and calculate hash of
2073 Pe/Coff image based on the authenticated image hashing in PE/COFF Specification
2076 @retval EFI_UNSUPPORTED Hash algorithm is not supported.
2077 @retval EFI_SUCCESS Hash successfully.
2086 WIN_CERTIFICATE_EFI_PKCS
*PkcsCertData
;
2088 PkcsCertData
= (WIN_CERTIFICATE_EFI_PKCS
*) (mImageBase
+ mSecDataDir
->Offset
);
2090 for (Index
= 0; Index
< HASHALG_MAX
; Index
++) {
2092 // Check the Hash algorithm in PE/COFF Authenticode.
2093 // According to PKCS#7 Definition:
2094 // SignedData ::= SEQUENCE {
2096 // digestAlgorithms DigestAlgorithmIdentifiers,
2097 // contentInfo ContentInfo,
2099 // The DigestAlgorithmIdentifiers can be used to determine the hash algorithm in PE/COFF hashing
2100 // This field has the fixed offset (+32) in final Authenticode ASN.1 data.
2101 // Fixed offset (+32) is calculated based on two bytes of length encoding.
2103 if ((*(PkcsCertData
->CertData
+ 1) & TWO_BYTE_ENCODE
) != TWO_BYTE_ENCODE
) {
2105 // Only support two bytes of Long Form of Length Encoding.
2111 if (CompareMem (PkcsCertData
->CertData
+ 32, mHash
[Index
].OidValue
, mHash
[Index
].OidLength
) == 0) {
2116 if (Index
== HASHALG_MAX
) {
2117 return EFI_UNSUPPORTED
;
2121 // HASH PE Image based on Hash algorithm in PE/COFF Authenticode.
2123 if (!HashPeImage(Index
)) {
2124 return EFI_UNSUPPORTED
;
2131 Enroll a new executable's signature into Signature Database.
2133 @param[in] PrivateData The module's private data.
2134 @param[in] VariableName Variable name of signature database, must be
2135 EFI_IMAGE_SECURITY_DATABASE, EFI_IMAGE_SECURITY_DATABASE1
2136 or EFI_IMAGE_SECURITY_DATABASE2.
2138 @retval EFI_SUCCESS New signature is enrolled successfully.
2139 @retval EFI_INVALID_PARAMETER The parameter is invalid.
2140 @retval EFI_UNSUPPORTED Unsupported command.
2141 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
2145 EnrollAuthentication2Descriptor (
2146 IN SECUREBOOT_CONFIG_PRIVATE_DATA
*Private
,
2147 IN CHAR16
*VariableName
2158 // DBT only support DER-X509 Cert Enrollment
2160 if (StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE2
) == 0) {
2161 return EFI_UNSUPPORTED
;
2165 // Read the whole file content
2167 Status
= ReadFileContent(
2168 Private
->FileContext
->FHandle
,
2169 (VOID
**) &mImageBase
,
2173 if (EFI_ERROR (Status
)) {
2176 ASSERT (mImageBase
!= NULL
);
2178 Attr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
2179 | EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
2182 // Check if SigDB variable has been already existed.
2183 // If true, use EFI_VARIABLE_APPEND_WRITE attribute to append the
2184 // new signature data to original variable
2187 Status
= gRT
->GetVariable(
2189 &gEfiImageSecurityDatabaseGuid
,
2194 if (Status
== EFI_BUFFER_TOO_SMALL
) {
2195 Attr
|= EFI_VARIABLE_APPEND_WRITE
;
2196 } else if (Status
!= EFI_NOT_FOUND
) {
2201 // Diretly set AUTHENTICATION_2 data to SetVariable
2203 Status
= gRT
->SetVariable(
2205 &gEfiImageSecurityDatabaseGuid
,
2211 DEBUG((DEBUG_INFO
, "Enroll AUTH_2 data to Var:%s Status: %x\n", VariableName
, Status
));
2215 CloseEnrolledFile(Private
->FileContext
);
2221 if (mImageBase
!= NULL
) {
2222 FreePool (mImageBase
);
2232 Enroll a new executable's signature into Signature Database.
2234 @param[in] PrivateData The module's private data.
2235 @param[in] VariableName Variable name of signature database, must be
2236 EFI_IMAGE_SECURITY_DATABASE, EFI_IMAGE_SECURITY_DATABASE1
2237 or EFI_IMAGE_SECURITY_DATABASE2.
2239 @retval EFI_SUCCESS New signature is enrolled successfully.
2240 @retval EFI_INVALID_PARAMETER The parameter is invalid.
2241 @retval EFI_UNSUPPORTED Unsupported command.
2242 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
2246 EnrollImageSignatureToSigDB (
2247 IN SECUREBOOT_CONFIG_PRIVATE_DATA
*Private
,
2248 IN CHAR16
*VariableName
2252 EFI_SIGNATURE_LIST
*SigDBCert
;
2253 EFI_SIGNATURE_DATA
*SigDBCertData
;
2258 WIN_CERTIFICATE_UEFI_GUID
*GuidCertData
;
2261 GuidCertData
= NULL
;
2263 if (StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE2
) == 0) {
2264 return EFI_UNSUPPORTED
;
2268 // Form the SigDB certificate list.
2269 // Format the data item into EFI_SIGNATURE_LIST type.
2271 // We need to parse executable's signature data from specified signed executable file.
2272 // In current implementation, we simply trust the pass-in signed executable file.
2273 // In reality, it's OS's responsibility to verify the signed executable file.
2277 // Read the whole file content
2279 Status
= ReadFileContent(
2280 Private
->FileContext
->FHandle
,
2281 (VOID
**) &mImageBase
,
2285 if (EFI_ERROR (Status
)) {
2288 ASSERT (mImageBase
!= NULL
);
2290 Status
= LoadPeImage ();
2291 if (EFI_ERROR (Status
)) {
2295 if (mSecDataDir
->SizeOfCert
== 0) {
2296 if (!HashPeImage (HASHALG_SHA256
)) {
2297 Status
= EFI_SECURITY_VIOLATION
;
2303 // Read the certificate data
2305 mCertificate
= (WIN_CERTIFICATE
*)(mImageBase
+ mSecDataDir
->Offset
);
2307 if (mCertificate
->wCertificateType
== WIN_CERT_TYPE_EFI_GUID
) {
2308 GuidCertData
= (WIN_CERTIFICATE_UEFI_GUID
*) mCertificate
;
2309 if (CompareMem (&GuidCertData
->CertType
, &gEfiCertTypeRsa2048Sha256Guid
, sizeof(EFI_GUID
)) != 0) {
2310 Status
= EFI_ABORTED
;
2314 if (!HashPeImage (HASHALG_SHA256
)) {
2315 Status
= EFI_ABORTED
;
2319 } else if (mCertificate
->wCertificateType
== WIN_CERT_TYPE_PKCS_SIGNED_DATA
) {
2321 Status
= HashPeImageByType ();
2322 if (EFI_ERROR (Status
)) {
2326 Status
= EFI_ABORTED
;
2332 // Create a new SigDB entry.
2334 SigDBSize
= sizeof(EFI_SIGNATURE_LIST
)
2335 + sizeof(EFI_SIGNATURE_DATA
) - 1
2336 + (UINT32
) mImageDigestSize
;
2338 Data
= (UINT8
*) AllocateZeroPool (SigDBSize
);
2340 Status
= EFI_OUT_OF_RESOURCES
;
2345 // Adjust the Certificate Database parameters.
2347 SigDBCert
= (EFI_SIGNATURE_LIST
*) Data
;
2348 SigDBCert
->SignatureListSize
= (UINT32
) SigDBSize
;
2349 SigDBCert
->SignatureHeaderSize
= 0;
2350 SigDBCert
->SignatureSize
= sizeof(EFI_SIGNATURE_DATA
) - 1 + (UINT32
) mImageDigestSize
;
2351 CopyGuid (&SigDBCert
->SignatureType
, &mCertType
);
2353 SigDBCertData
= (EFI_SIGNATURE_DATA
*)((UINT8
*)SigDBCert
+ sizeof(EFI_SIGNATURE_LIST
));
2354 CopyGuid (&SigDBCertData
->SignatureOwner
, Private
->SignatureGUID
);
2355 CopyMem (SigDBCertData
->SignatureData
, mImageDigest
, mImageDigestSize
);
2357 Attr
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
2358 | EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
2359 Status
= CreateTimeBasedPayload (&SigDBSize
, (UINT8
**) &Data
);
2360 if (EFI_ERROR (Status
)) {
2361 DEBUG ((EFI_D_ERROR
, "Fail to create time-based data payload: %r", Status
));
2366 // Check if SigDB variable has been already existed.
2367 // If true, use EFI_VARIABLE_APPEND_WRITE attribute to append the
2368 // new signature data to original variable
2371 Status
= gRT
->GetVariable(
2373 &gEfiImageSecurityDatabaseGuid
,
2378 if (Status
== EFI_BUFFER_TOO_SMALL
) {
2379 Attr
|= EFI_VARIABLE_APPEND_WRITE
;
2380 } else if (Status
!= EFI_NOT_FOUND
) {
2385 // Enroll the variable.
2387 Status
= gRT
->SetVariable(
2389 &gEfiImageSecurityDatabaseGuid
,
2394 if (EFI_ERROR (Status
)) {
2400 CloseEnrolledFile(Private
->FileContext
);
2402 if (Private
->SignatureGUID
!= NULL
) {
2403 FreePool (Private
->SignatureGUID
);
2404 Private
->SignatureGUID
= NULL
;
2411 if (mImageBase
!= NULL
) {
2412 FreePool (mImageBase
);
2420 Enroll signature into DB/DBX/DBT without KEK's authentication.
2421 The SignatureOwner GUID will be Private->SignatureGUID.
2423 @param[in] PrivateData The module's private data.
2424 @param[in] VariableName Variable name of signature database, must be
2425 EFI_IMAGE_SECURITY_DATABASE or EFI_IMAGE_SECURITY_DATABASE1.
2427 @retval EFI_SUCCESS New signature enrolled successfully.
2428 @retval EFI_INVALID_PARAMETER The parameter is invalid.
2429 @retval others Fail to enroll signature data.
2433 EnrollSignatureDatabase (
2434 IN SECUREBOOT_CONFIG_PRIVATE_DATA
*Private
,
2435 IN CHAR16
*VariableName
2438 UINT16
* FilePostFix
;
2442 if ((Private
->FileContext
->FileName
== NULL
) || (Private
->FileContext
->FHandle
== NULL
) || (Private
->SignatureGUID
== NULL
)) {
2443 return EFI_INVALID_PARAMETER
;
2446 Status
= SetSecureBootMode (CUSTOM_SECURE_BOOT_MODE
);
2447 if (EFI_ERROR (Status
)) {
2452 // Parse the file's postfix.
2454 NameLength
= StrLen (Private
->FileContext
->FileName
);
2455 if (NameLength
<= 4) {
2456 return EFI_INVALID_PARAMETER
;
2458 FilePostFix
= Private
->FileContext
->FileName
+ NameLength
- 4;
2459 if (IsDerEncodeCertificate (FilePostFix
)) {
2461 // Supports DER-encoded X509 certificate.
2463 return EnrollX509toSigDB (Private
, VariableName
);
2464 } else if (IsAuthentication2Format(Private
->FileContext
->FHandle
)){
2465 return EnrollAuthentication2Descriptor(Private
, VariableName
);
2467 return EnrollImageSignatureToSigDB (Private
, VariableName
);
2472 List all signatures in specified signature database (e.g. KEK/DB/DBX/DBT)
2473 by GUID in the page for user to select and delete as needed.
2475 @param[in] PrivateData Module's private data.
2476 @param[in] VariableName The variable name of the vendor's signature database.
2477 @param[in] VendorGuid A unique identifier for the vendor.
2478 @param[in] LabelNumber Label number to insert opcodes.
2479 @param[in] FormId Form ID of current page.
2480 @param[in] QuestionIdBase Base question id of the signature list.
2482 @retval EFI_SUCCESS Success to update the signature list page
2483 @retval EFI_OUT_OF_RESOURCES Unable to allocate required resources.
2488 IN SECUREBOOT_CONFIG_PRIVATE_DATA
*PrivateData
,
2489 IN CHAR16
*VariableName
,
2490 IN EFI_GUID
*VendorGuid
,
2491 IN UINT16 LabelNumber
,
2492 IN EFI_FORM_ID FormId
,
2493 IN EFI_QUESTION_ID QuestionIdBase
2500 VOID
*StartOpCodeHandle
;
2501 VOID
*EndOpCodeHandle
;
2502 EFI_IFR_GUID_LABEL
*StartLabel
;
2503 EFI_IFR_GUID_LABEL
*EndLabel
;
2506 EFI_SIGNATURE_LIST
*CertList
;
2507 EFI_SIGNATURE_DATA
*Cert
;
2508 UINT32 ItemDataSize
;
2510 EFI_STRING_ID GuidID
;
2517 StartOpCodeHandle
= NULL
;
2518 EndOpCodeHandle
= NULL
;
2521 // Initialize the container for dynamic opcodes.
2523 StartOpCodeHandle
= HiiAllocateOpCodeHandle ();
2524 if (StartOpCodeHandle
== NULL
) {
2525 Status
= EFI_OUT_OF_RESOURCES
;
2529 EndOpCodeHandle
= HiiAllocateOpCodeHandle ();
2530 if (EndOpCodeHandle
== NULL
) {
2531 Status
= EFI_OUT_OF_RESOURCES
;
2536 // Create Hii Extend Label OpCode.
2538 StartLabel
= (EFI_IFR_GUID_LABEL
*) HiiCreateGuidOpCode (
2542 sizeof (EFI_IFR_GUID_LABEL
)
2544 StartLabel
->ExtendOpCode
= EFI_IFR_EXTEND_OP_LABEL
;
2545 StartLabel
->Number
= LabelNumber
;
2547 EndLabel
= (EFI_IFR_GUID_LABEL
*) HiiCreateGuidOpCode (
2551 sizeof (EFI_IFR_GUID_LABEL
)
2553 EndLabel
->ExtendOpCode
= EFI_IFR_EXTEND_OP_LABEL
;
2554 EndLabel
->Number
= LABEL_END
;
2560 Status
= gRT
->GetVariable (VariableName
, VendorGuid
, NULL
, &DataSize
, Data
);
2561 if (EFI_ERROR (Status
) && Status
!= EFI_BUFFER_TOO_SMALL
) {
2565 Data
= (UINT8
*) AllocateZeroPool (DataSize
);
2567 Status
= EFI_OUT_OF_RESOURCES
;
2571 Status
= gRT
->GetVariable (VariableName
, VendorGuid
, NULL
, &DataSize
, Data
);
2572 if (EFI_ERROR (Status
)) {
2576 GuidStr
= AllocateZeroPool (100);
2577 if (GuidStr
== NULL
) {
2578 Status
= EFI_OUT_OF_RESOURCES
;
2583 // Enumerate all KEK pub data.
2585 ItemDataSize
= (UINT32
) DataSize
;
2586 CertList
= (EFI_SIGNATURE_LIST
*) Data
;
2589 while ((ItemDataSize
> 0) && (ItemDataSize
>= CertList
->SignatureListSize
)) {
2591 if (CompareGuid (&CertList
->SignatureType
, &gEfiCertRsa2048Guid
)) {
2592 Help
= STRING_TOKEN (STR_CERT_TYPE_RSA2048_SHA256_GUID
);
2593 } else if (CompareGuid (&CertList
->SignatureType
, &gEfiCertX509Guid
)) {
2594 Help
= STRING_TOKEN (STR_CERT_TYPE_PCKS7_GUID
);
2595 } else if (CompareGuid (&CertList
->SignatureType
, &gEfiCertSha1Guid
)) {
2596 Help
= STRING_TOKEN (STR_CERT_TYPE_SHA1_GUID
);
2597 } else if (CompareGuid (&CertList
->SignatureType
, &gEfiCertSha256Guid
)) {
2598 Help
= STRING_TOKEN (STR_CERT_TYPE_SHA256_GUID
);
2599 } else if (CompareGuid (&CertList
->SignatureType
, &gEfiCertX509Sha256Guid
)) {
2600 Help
= STRING_TOKEN (STR_CERT_TYPE_X509_SHA256_GUID
);
2601 } else if (CompareGuid (&CertList
->SignatureType
, &gEfiCertX509Sha384Guid
)) {
2602 Help
= STRING_TOKEN (STR_CERT_TYPE_X509_SHA384_GUID
);
2603 } else if (CompareGuid (&CertList
->SignatureType
, &gEfiCertX509Sha512Guid
)) {
2604 Help
= STRING_TOKEN (STR_CERT_TYPE_X509_SHA512_GUID
);
2607 // The signature type is not supported in current implementation.
2609 ItemDataSize
-= CertList
->SignatureListSize
;
2610 CertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) CertList
+ CertList
->SignatureListSize
);
2614 CertCount
= (CertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - CertList
->SignatureHeaderSize
) / CertList
->SignatureSize
;
2615 for (Index
= 0; Index
< CertCount
; Index
++) {
2616 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
2617 + sizeof (EFI_SIGNATURE_LIST
)
2618 + CertList
->SignatureHeaderSize
2619 + Index
* CertList
->SignatureSize
);
2621 // Display GUID and help
2623 GuidToString (&Cert
->SignatureOwner
, GuidStr
, 100);
2624 GuidID
= HiiSetString (PrivateData
->HiiHandle
, 0, GuidStr
, NULL
);
2625 HiiCreateCheckBoxOpCode (
2627 (EFI_QUESTION_ID
) (QuestionIdBase
+ GuidIndex
++),
2632 EFI_IFR_FLAG_CALLBACK
,
2638 ItemDataSize
-= CertList
->SignatureListSize
;
2639 CertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) CertList
+ CertList
->SignatureListSize
);
2644 PrivateData
->HiiHandle
,
2645 &gSecureBootConfigFormSetGuid
,
2651 if (StartOpCodeHandle
!= NULL
) {
2652 HiiFreeOpCodeHandle (StartOpCodeHandle
);
2655 if (EndOpCodeHandle
!= NULL
) {
2656 HiiFreeOpCodeHandle (EndOpCodeHandle
);
2663 if (GuidStr
!= NULL
) {
2671 Delete a KEK entry from KEK database.
2673 @param[in] PrivateData Module's private data.
2674 @param[in] QuestionId Question id of the KEK item to delete.
2676 @retval EFI_SUCCESS Delete kek item successfully.
2677 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
2681 DeleteKeyExchangeKey (
2682 IN SECUREBOOT_CONFIG_PRIVATE_DATA
*PrivateData
,
2683 IN EFI_QUESTION_ID QuestionId
2692 EFI_SIGNATURE_LIST
*CertList
;
2693 EFI_SIGNATURE_LIST
*NewCertList
;
2694 EFI_SIGNATURE_DATA
*Cert
;
2697 BOOLEAN IsKEKItemFound
;
2699 UINTN DeleteKekIndex
;
2707 DeleteKekIndex
= QuestionId
- OPTION_DEL_KEK_QUESTION_ID
;
2709 Status
= SetSecureBootMode(CUSTOM_SECURE_BOOT_MODE
);
2710 if (EFI_ERROR (Status
)) {
2715 // Get original KEK variable.
2718 Status
= gRT
->GetVariable (EFI_KEY_EXCHANGE_KEY_NAME
, &gEfiGlobalVariableGuid
, NULL
, &DataSize
, NULL
);
2719 if (EFI_ERROR(Status
) && Status
!= EFI_BUFFER_TOO_SMALL
) {
2723 OldData
= (UINT8
*)AllocateZeroPool(DataSize
);
2724 if (OldData
== NULL
) {
2725 Status
= EFI_OUT_OF_RESOURCES
;
2729 Status
= gRT
->GetVariable (EFI_KEY_EXCHANGE_KEY_NAME
, &gEfiGlobalVariableGuid
, &Attr
, &DataSize
, OldData
);
2730 if (EFI_ERROR(Status
)) {
2735 // Allocate space for new variable.
2737 Data
= (UINT8
*) AllocateZeroPool (DataSize
);
2739 Status
= EFI_OUT_OF_RESOURCES
;
2744 // Enumerate all KEK pub data and erasing the target item.
2746 IsKEKItemFound
= FALSE
;
2747 KekDataSize
= (UINT32
) DataSize
;
2748 CertList
= (EFI_SIGNATURE_LIST
*) OldData
;
2751 while ((KekDataSize
> 0) && (KekDataSize
>= CertList
->SignatureListSize
)) {
2752 if (CompareGuid (&CertList
->SignatureType
, &gEfiCertRsa2048Guid
) ||
2753 CompareGuid (&CertList
->SignatureType
, &gEfiCertX509Guid
)) {
2754 CopyMem (Data
+ Offset
, CertList
, (sizeof(EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
));
2755 NewCertList
= (EFI_SIGNATURE_LIST
*)(Data
+ Offset
);
2756 Offset
+= (sizeof(EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
2757 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
2758 CertCount
= (CertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - CertList
->SignatureHeaderSize
) / CertList
->SignatureSize
;
2759 for (Index
= 0; Index
< CertCount
; Index
++) {
2760 if (GuidIndex
== DeleteKekIndex
) {
2762 // Find it! Skip it!
2764 NewCertList
->SignatureListSize
-= CertList
->SignatureSize
;
2765 IsKEKItemFound
= TRUE
;
2768 // This item doesn't match. Copy it to the Data buffer.
2770 CopyMem (Data
+ Offset
, Cert
, CertList
->SignatureSize
);
2771 Offset
+= CertList
->SignatureSize
;
2774 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) Cert
+ CertList
->SignatureSize
);
2778 // This List doesn't match. Copy it to the Data buffer.
2780 CopyMem (Data
+ Offset
, CertList
, CertList
->SignatureListSize
);
2781 Offset
+= CertList
->SignatureListSize
;
2784 KekDataSize
-= CertList
->SignatureListSize
;
2785 CertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) CertList
+ CertList
->SignatureListSize
);
2788 if (!IsKEKItemFound
) {
2790 // Doesn't find the Kek Item!
2792 Status
= EFI_NOT_FOUND
;
2797 // Delete the Signature header if there is no signature in the list.
2799 KekDataSize
= Offset
;
2800 CertList
= (EFI_SIGNATURE_LIST
*) Data
;
2802 ZeroMem (OldData
, KekDataSize
);
2803 while ((KekDataSize
> 0) && (KekDataSize
>= CertList
->SignatureListSize
)) {
2804 CertCount
= (CertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - CertList
->SignatureHeaderSize
) / CertList
->SignatureSize
;
2805 DEBUG ((DEBUG_INFO
, " CertCount = %x\n", CertCount
));
2806 if (CertCount
!= 0) {
2807 CopyMem (OldData
+ Offset
, CertList
, CertList
->SignatureListSize
);
2808 Offset
+= CertList
->SignatureListSize
;
2810 KekDataSize
-= CertList
->SignatureListSize
;
2811 CertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) CertList
+ CertList
->SignatureListSize
);
2815 if ((Attr
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) {
2816 Status
= CreateTimeBasedPayload (&DataSize
, &OldData
);
2817 if (EFI_ERROR (Status
)) {
2818 DEBUG ((EFI_D_ERROR
, "Fail to create time-based data payload: %r", Status
));
2823 Status
= gRT
->SetVariable(
2824 EFI_KEY_EXCHANGE_KEY_NAME
,
2825 &gEfiGlobalVariableGuid
,
2830 if (EFI_ERROR (Status
)) {
2831 DEBUG ((DEBUG_ERROR
, "Failed to set variable, Status = %r\n", Status
));
2840 if (OldData
!= NULL
) {
2844 return UpdateDeletePage (
2846 EFI_KEY_EXCHANGE_KEY_NAME
,
2847 &gEfiGlobalVariableGuid
,
2849 FORMID_DELETE_KEK_FORM
,
2850 OPTION_DEL_KEK_QUESTION_ID
2855 Delete a signature entry from signature database.
2857 @param[in] PrivateData Module's private data.
2858 @param[in] VariableName The variable name of the vendor's signature database.
2859 @param[in] VendorGuid A unique identifier for the vendor.
2860 @param[in] LabelNumber Label number to insert opcodes.
2861 @param[in] FormId Form ID of current page.
2862 @param[in] QuestionIdBase Base question id of the signature list.
2863 @param[in] DeleteIndex Signature index to delete.
2865 @retval EFI_SUCCESS Delete signature successfully.
2866 @retval EFI_NOT_FOUND Can't find the signature item,
2867 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
2871 IN SECUREBOOT_CONFIG_PRIVATE_DATA
*PrivateData
,
2872 IN CHAR16
*VariableName
,
2873 IN EFI_GUID
*VendorGuid
,
2874 IN UINT16 LabelNumber
,
2875 IN EFI_FORM_ID FormId
,
2876 IN EFI_QUESTION_ID QuestionIdBase
,
2877 IN UINTN DeleteIndex
2886 EFI_SIGNATURE_LIST
*CertList
;
2887 EFI_SIGNATURE_LIST
*NewCertList
;
2888 EFI_SIGNATURE_DATA
*Cert
;
2891 BOOLEAN IsItemFound
;
2892 UINT32 ItemDataSize
;
2901 Status
= SetSecureBootMode(CUSTOM_SECURE_BOOT_MODE
);
2902 if (EFI_ERROR (Status
)) {
2907 // Get original signature list data.
2910 Status
= gRT
->GetVariable (VariableName
, VendorGuid
, NULL
, &DataSize
, NULL
);
2911 if (EFI_ERROR (Status
) && Status
!= EFI_BUFFER_TOO_SMALL
) {
2915 OldData
= (UINT8
*) AllocateZeroPool (DataSize
);
2916 if (OldData
== NULL
) {
2917 Status
= EFI_OUT_OF_RESOURCES
;
2921 Status
= gRT
->GetVariable (VariableName
, VendorGuid
, &Attr
, &DataSize
, OldData
);
2922 if (EFI_ERROR(Status
)) {
2927 // Allocate space for new variable.
2929 Data
= (UINT8
*) AllocateZeroPool (DataSize
);
2931 Status
= EFI_OUT_OF_RESOURCES
;
2936 // Enumerate all signature data and erasing the target it