From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-lf1-f48.google.com (mail-lf1-f48.google.com [209.85.167.48]) by mx.groups.io with SMTP id smtpd.web12.6059.1626265831324289269 for ; Wed, 14 Jul 2021 05:30:31 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@semihalf-com.20150623.gappssmtp.com header.s=20150623 header.b=uoe6121V; spf=none, err=SPF record not found (domain: semihalf.com, ip: 209.85.167.48, mailfrom: gjb@semihalf.com) Received: by mail-lf1-f48.google.com with SMTP id q16so3300782lfa.5 for ; Wed, 14 Jul 2021 05:30:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=semihalf-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=x5lyzjg3JPvUBWQ24P2XnRjNFJojeiYx4EXvJTHBL78=; b=uoe6121VAlqk/kzMgfHeP9bslBvrN0g0rI0ufv9MEULkDOcPGq4dNHuEKNH9Vq2AGN 9lYLIUaNge3eIRmL/lGidRk2HEiim+lGdvQLEO42bzP6BFHVKbL8AIOjFOLm6S6/IgOP az/Y4qSD2wLBAqH9fmuBZIar5yb6gbQcoLuulCiCSRJ8PwOUJq6Bd8w9/rLn6HpvYAv3 TIDwVyZMjQ9xus9gHgdHFBs8XbGN6rU+LVjLXO9WDwTilzaGKh+lLVYLcswA4MBQZ2Wy //a0JFxQSdtWzLAs0lE1YKPtD8tbLFMt/AU7rJNkWTzIPiS3MDU7RNUEhkYZzkQBSBqT 32Ww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=x5lyzjg3JPvUBWQ24P2XnRjNFJojeiYx4EXvJTHBL78=; b=DLMmVSku7T/0pydpfSEntOCwfnVT7heac9M/y3zPWx77+TOek3+HzO/+WxlrrxeSj3 u+8A5TaBMhukYnPbR7XgOsMU3JsL18R3cOEHzwa5oWCY2yJAjQep0g28Xtm9lS7DSlZi fbcsWRJvmD8EWrJPmdVDT3GCQs0llzKFzhkFXJG9n9ZaqSxv/o80h/E5oK5Bm2kdGHxV qvXDuM5liOx8Z+xrxWPkZKZfeLA9Js1j+7vjNrQCvvg2UiSVeIFmjPXk5KU8TtUGHMVZ OpeKItykcXE2AyBajiTZfSb5OMdp9f/WoMG7CUIejPACCpxXbfbrBx7eS8x4omqX46Ep 9NOQ== X-Gm-Message-State: AOAM530o4i5Zs4Jg2+88VB/66+SAkalQE6Pg0EYfdP5+hR2pjaVrgQHV FkPC+8gmUxjMnb8VpQSOmytaQUrGUUVCKDEv X-Google-Smtp-Source: ABdhPJzfDn2Z21DNLjDDVnxDuDk+8iz6SWXKM6qJr/Nmtkx55Cbrr8w1gFph9riwNa1+8iEKw5EMsw== X-Received: by 2002:a05:6512:1326:: with SMTP id x38mr7667726lfu.62.1626265829407; Wed, 14 Jul 2021 05:30:29 -0700 (PDT) Return-Path: Received: from gilgamesh.lab.semihalf.net ([83.142.187.85]) by smtp.gmail.com with ESMTPSA id j2sm224846ljc.49.2021.07.14.05.30.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Jul 2021 05:30:29 -0700 (PDT) From: "Grzegorz Bernacki" To: devel@edk2.groups.io Cc: leif@nuviainc.com, ardb+tianocore@kernel.org, Samer.El-Haj-Mahmoud@arm.com, sunny.Wang@arm.com, mw@semihalf.com, upstream@semihalf.com, jiewen.yao@intel.com, jian.j.wang@intel.com, min.m.xu@intel.com, lersek@redhat.com, sami.mujawar@arm.com, afish@apple.com, ray.ni@intel.com, jordan.l.justen@intel.com, rebecca@bsdio.com, grehan@freebsd.org, thomas.abraham@arm.com, chasel.chiu@intel.com, nathaniel.l.desimone@intel.com, gaoliming@byosoft.com.cn, eric.dong@intel.com, michael.d.kinney@intel.com, zailiang.sun@intel.com, yi.qian@intel.com, graeme@nuviainc.com, rad@semihalf.com, pete@akeo.ie, Grzegorz Bernacki , Jiewen Yao , Sunny Wang Subject: [PATCH v6 09/11] SecurityPkg: Add EnrollFromDefaultKeys application. Date: Wed, 14 Jul 2021 14:29:50 +0200 Message-Id: <20210714122952.1340890-10-gjb@semihalf.com> X-Mailer: git-send-email 2.29.0 In-Reply-To: <20210714122952.1340890-1-gjb@semihalf.com> References: <20210714122952.1340890-1-gjb@semihalf.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit This application allows user to force key enrollment from Secure Boot default variables. Signed-off-by: Grzegorz Bernacki Reviewed-by: Jiewen Yao Reviewed-by: Sunny Wang --- SecurityPkg/EnrollFromDefaultKeysApp/EnrollFromDefaultKeysApp.inf | 48 +++++++++ SecurityPkg/EnrollFromDefaultKeysApp/EnrollFromDefaultKeysApp.c | 110 ++++++++++++++++++++ 2 files changed, 158 insertions(+) create mode 100644 SecurityPkg/EnrollFromDefaultKeysApp/EnrollFromDefaultKeysApp.inf create mode 100644 SecurityPkg/EnrollFromDefaultKeysApp/EnrollFromDefaultKeysApp.c diff --git a/SecurityPkg/EnrollFromDefaultKeysApp/EnrollFromDefaultKeysApp.inf b/SecurityPkg/EnrollFromDefaultKeysApp/EnrollFromDefaultKeysApp.inf new file mode 100644 index 0000000000..8675b30291 --- /dev/null +++ b/SecurityPkg/EnrollFromDefaultKeysApp/EnrollFromDefaultKeysApp.inf @@ -0,0 +1,48 @@ +## @file +# Enroll PK, KEK, db, dbx from Default variables +# +# Copyright (c) 2021, ARM Ltd. All rights reserved.
+# Copyright (c) 2021, Semihalf All rights reserved.
+# SPDX-License-Identifier: BSD-2-Clause-Patent +## + +[Defines] + INF_VERSION = 1.28 + BASE_NAME = EnrollFromDefaultKeysApp + FILE_GUID = 6F18CB2F-1293-4BC1-ABB8-35F84C71812E + MODULE_TYPE = UEFI_APPLICATION + VERSION_STRING = 0.1 + ENTRY_POINT = UefiMain + +[Sources] + EnrollFromDefaultKeysApp.c + +[Packages] + MdeModulePkg/MdeModulePkg.dec + MdePkg/MdePkg.dec + SecurityPkg/SecurityPkg.dec + +[Guids] + gEfiCertPkcs7Guid + gEfiCertSha256Guid + gEfiCertX509Guid + gEfiCustomModeEnableGuid + gEfiGlobalVariableGuid + gEfiImageSecurityDatabaseGuid + gEfiSecureBootEnableDisableGuid + +[Protocols] + gEfiSmbiosProtocolGuid ## CONSUMES + +[LibraryClasses] + BaseLib + BaseMemoryLib + DebugLib + MemoryAllocationLib + PrintLib + UefiApplicationEntryPoint + UefiBootServicesTableLib + UefiLib + UefiRuntimeServicesTableLib + SecureBootVariableLib + SecureBootVariableProvisionLib diff --git a/SecurityPkg/EnrollFromDefaultKeysApp/EnrollFromDefaultKeysApp.c b/SecurityPkg/EnrollFromDefaultKeysApp/EnrollFromDefaultKeysApp.c new file mode 100644 index 0000000000..13b7c61732 --- /dev/null +++ b/SecurityPkg/EnrollFromDefaultKeysApp/EnrollFromDefaultKeysApp.c @@ -0,0 +1,110 @@ +/** @file + Enroll default PK, KEK, db, dbx. + +Copyright (c) 2021, ARM Ltd. All rights reserved.
+Copyright (c) 2021, Semihalf All rights reserved.
+ +SPDX-License-Identifier: BSD-2-Clause-Patent +**/ + +#include // gEfiCustomModeEnableGuid +#include // EFI_SETUP_MODE_NAME +#include // EFI_IMAGE_SECURITY_DATABASE +#include // GUID_STRING_LENGTH +#include // CopyGuid() +#include // ASSERT() +#include // FreePool() +#include // AsciiSPrint() +#include // gBS +#include // AsciiPrint() +#include // gRT +#include +#include +#include + +/** + Entry point function of this shell application. +**/ +EFI_STATUS +EFIAPI +UefiMain ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + EFI_STATUS Status; + UINT8 SetupMode; + + Status = GetSetupMode (&SetupMode); + if (EFI_ERROR (Status)) { + AsciiPrint ("EnrollFromDefaultKeysApp: Cannot get SetupMode variable: %r\n", Status); + return 1; + } + + if (SetupMode == USER_MODE) { + AsciiPrint ("EnrollFromDefaultKeysApp: Skipped - USER_MODE\n"); + return 1; + } + + Status = SetSecureBootMode (CUSTOM_SECURE_BOOT_MODE); + if (EFI_ERROR (Status)) { + AsciiPrint ("EnrollFromDefaultKeysApp: Cannot set CUSTOM_SECURE_BOOT_MODE: %r\n", Status); + return 1; + } + + Status = EnrollDbFromDefault (); + if (EFI_ERROR (Status)) { + AsciiPrint ("EnrollFromDefaultKeysApp: Cannot enroll db: %r\n", Status); + goto error; + } + + Status = EnrollDbxFromDefault (); + if (EFI_ERROR (Status)) { + AsciiPrint ("EnrollFromDefaultKeysApp: Cannot enroll dbt: %r\n", Status); + } + + Status = EnrollDbtFromDefault (); + if (EFI_ERROR (Status)) { + AsciiPrint ("EnrollFromDefaultKeysApp: Cannot enroll dbx: %r\n", Status); + } + + Status = EnrollKEKFromDefault (); + if (EFI_ERROR (Status)) { + AsciiPrint ("EnrollFromDefaultKeysApp: Cannot enroll KEK: %r\n", Status); + goto cleardbs; + } + + Status = EnrollPKFromDefault (); + if (EFI_ERROR (Status)) { + AsciiPrint ("EnrollFromDefaultKeysApp: Cannot enroll PK: %r\n", Status); + goto clearKEK; + } + + Status = SetSecureBootMode (STANDARD_SECURE_BOOT_MODE); + if (EFI_ERROR (Status)) { + AsciiPrint ( + "EnrollFromDefaultKeysApp: Cannot set CustomMode to STANDARD_SECURE_BOOT_MODE\n" + "Please do it manually, otherwise system can be easily compromised\n" + ); + } + return 0; + +clearKEK: + DeleteKEK (); + +cleardbs: + DeleteDbt (); + DeleteDbx (); + DeleteDb (); + +error: + Status = SetSecureBootMode (STANDARD_SECURE_BOOT_MODE); + if (EFI_ERROR (Status)) { + AsciiPrint ( + "EnrollFromDefaultKeysApp: Cannot set CustomMode to STANDARD_SECURE_BOOT_MODE\n" + "Please do it manually, otherwise system can be easily compromised\n" + ); + } + + return 1; +} -- 2.25.1