From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-lf1-f41.google.com (mail-lf1-f41.google.com [209.85.167.41]) by mx.groups.io with SMTP id smtpd.web11.5129.1622022162640359969 for ; Wed, 26 May 2021 02:42:43 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@semihalf-com.20150623.gappssmtp.com header.s=20150623 header.b=u3E+AHtc; spf=none, err=SPF record not found (domain: semihalf.com, ip: 209.85.167.41, mailfrom: gjb@semihalf.com) Received: by mail-lf1-f41.google.com with SMTP id i22so1548555lfl.10 for ; Wed, 26 May 2021 02:42:42 -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=NxAZq9RtTgBXunO/2snhxyMM4DndYKPCYxOQ+qlKbd4=; b=u3E+AHtcqELGEOTlvmnb4VZvtcks6VwVGtfg/fTMHmc/jUCxa4UjNAhLOmWLHwz21I 56V9GpYwMs8vDh8JqdQWtn76Hn7dyN7tqiGUQFzohw+tN99mm8mRHzZR8a80Ei0F9+0E RYpfTxpnklc5BaCDCNzMr4R7I/7mQbiUr98pmX1voHpYu31LBQKkJzrc5VtuuzRC1vOl A5+JPnj4d0+s7kYkMP50WUNqnNmfiUztyngFtnDG44j85BYXGgpSGYYWXgXHdIE6iJUZ X1HTPo4tpZO/oDISjWgrEdU6tNKG0biFn94ejpyhN+gTkoFmkSwy8G3BOc6R1j13kKxy 0KmA== 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=NxAZq9RtTgBXunO/2snhxyMM4DndYKPCYxOQ+qlKbd4=; b=Q1w1l6xMbEU3NAEyQTqEJJiBAXHEQ+8nQ1BnXVwxz33RVSIMHl+ggh1eYS1vYQ2eVP YgdTAL794BHVGzBMN1mAWpXwr9Gp1wwXMkRq+dM+m7wgcolR8Hoe+gce5O63zkOF83MI tI2fZHfBLyN9Z8HegvUcFBQDWyTtFgxt1+UDmRrSYAJxWJKnw572/3/4dPrj7J+NPaiF NU59O7xwPoAFEiJYt9BiMRTsYLmw62cxZKVBS4tGPnohyAAoH0xsr4IBBM4jzbW1Osuu QxTqyPmwM5+snLSedCA/M2+DkCIxboXZO9lgnNLUmeWeukyNKXXABMNkMikRxrJD+Tc0 FJIg== X-Gm-Message-State: AOAM530FYRvdHp6Gw6F0kIecSn5rnchIfB6PIMMoK/LPvlVqszbQwG+r t5NieeqySiR1qcM6c3rcS2PIQKRcLYY50KVg9XI= X-Google-Smtp-Source: ABdhPJxKzG2MXd+dasscMAqd0tzFNLX1dE9gbDJJTPBTNMb97GKcVHIsbuuBVXv++P7ia/YCueJb5g== X-Received: by 2002:a05:6512:2313:: with SMTP id o19mr1614172lfu.237.1622022160570; Wed, 26 May 2021 02:42:40 -0700 (PDT) Return-Path: Received: from gilgamesh.lab.semihalf.net ([83.142.187.85]) by smtp.gmail.com with ESMTPSA id y19sm2380268ljy.32.2021.05.26.02.42.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 26 May 2021 02:42:40 -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, gjb@semihalf.com, upstream@semihalf.com, jiewen.yao@intel.com, jian.j.wang@intel.com, min.m.xu@intel.com, lersek@redhat.com Subject: [PATCH 4/6] SecurityPkg: Add SecEnrollDefaultKeys application. Date: Wed, 26 May 2021 11:42:02 +0200 Message-Id: <20210526094204.73600-6-gjb@semihalf.com> X-Mailer: git-send-email 2.29.0 In-Reply-To: <20210526094204.73600-1-gjb@semihalf.com> References: <20210526094204.73600-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 --- SecurityPkg/SecEnrollDefaultKeysApp/SecEnrollDefaultKeysApp.inf | 48 +++++++++ SecurityPkg/SecEnrollDefaultKeysApp/SecEnrollDefaultKeysApp.c | 108 ++++++++++++++++++++ 2 files changed, 156 insertions(+) create mode 100644 SecurityPkg/SecEnrollDefaultKeysApp/SecEnrollDefaultKeysApp.inf create mode 100644 SecurityPkg/SecEnrollDefaultKeysApp/SecEnrollDefaultKeysApp.c diff --git a/SecurityPkg/SecEnrollDefaultKeysApp/SecEnrollDefaultKeysApp.inf b/SecurityPkg/SecEnrollDefaultKeysApp/SecEnrollDefaultKeysApp.inf new file mode 100644 index 0000000000..9d575ae0ac --- /dev/null +++ b/SecurityPkg/SecEnrollDefaultKeysApp/SecEnrollDefaultKeysApp.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 = SecEnrollDefaultKeysApp + FILE_GUID = 6F18CB2F-1293-4BC1-ABB8-35F84C71812E + MODULE_TYPE = UEFI_APPLICATION + VERSION_STRING = 0.1 + ENTRY_POINT = ShellCEntryLib + +[Sources] + SecEnrollDefaultKeysApp.c + +[Packages] + MdeModulePkg/MdeModulePkg.dec + MdePkg/MdePkg.dec + SecurityPkg/SecurityPkg.dec + ShellPkg/ShellPkg.dec + +[Guids] + gEfiCertPkcs7Guid + gEfiCertSha256Guid + gEfiCertX509Guid + gEfiCustomModeEnableGuid + gEfiGlobalVariableGuid + gEfiImageSecurityDatabaseGuid + gEfiSecureBootEnableDisableGuid + +[Protocols] + gEfiSmbiosProtocolGuid ## CONSUMES + +[LibraryClasses] + BaseLib + BaseMemoryLib + DebugLib + MemoryAllocationLib + PrintLib + ShellCEntryLib + UefiBootServicesTableLib + UefiLib + UefiRuntimeServicesTableLib + SecBootVariableLib diff --git a/SecurityPkg/SecEnrollDefaultKeysApp/SecEnrollDefaultKeysApp.c b/SecurityPkg/SecEnrollDefaultKeysApp/SecEnrollDefaultKeysApp.c new file mode 100644 index 0000000000..b66dd93a7a --- /dev/null +++ b/SecurityPkg/SecEnrollDefaultKeysApp/SecEnrollDefaultKeysApp.c @@ -0,0 +1,108 @@ +/** @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 // ShellAppMain() +#include // gBS +#include // AsciiPrint() +#include // gRT +#include +#include + +#define FAIL(fmt...) AsciiPrint("SecEnrollDefaultKeysApp: " fmt) + +/** + Entry point function of this shell application. +**/ +INTN +EFIAPI +ShellAppMain ( + IN UINTN Argc, + IN CHAR16 **Argv + ) +{ + EFI_STATUS Status; + UINT8 SetupMode; + + Status = CheckSetupMode (&SetupMode); + if (EFI_ERROR (Status)) { + FAIL ("Cannot get SetupMode variable: %r\n", Status); + return 1; + } + + if (SetupMode == USER_MODE) { + FAIL ("Skipped - USER_MODE\n"); + return 1; + } + + Status = SetSecureBootMode (CUSTOM_SECURE_BOOT_MODE); + if (EFI_ERROR (Status)) { + FAIL ("Cannot set CUSTOM_SECURE_BOOT_MODE: %r\n", Status); + return 1; + } + + Status = EnrollDbFromDefault (); + if (EFI_ERROR (Status)) { + FAIL ("Cannot enroll db: %r\n", Status); + goto error; + } + + Status = EnrollDbxFromDefault (); + if (EFI_ERROR (Status)) { + FAIL ("Cannot enroll dbt: %r\n", Status); + } + + Status = EnrollDbtFromDefault (); + if (EFI_ERROR (Status)) { + FAIL ("Cannot enroll dbx: %r\n", Status); + } + + Status = EnrollKEKFromDefault (); + if (EFI_ERROR (Status)) { + FAIL ("Cannot enroll KEK: %r\n", Status); + goto cleardbs; + } + + Status = EnrollPKFromDefault (); + if (EFI_ERROR (Status)) { + FAIL ("Cannot enroll PK: %r\n", Status); + goto clearKEK; + } + + Status = SetSecureBootMode (STANDARD_SECURE_BOOT_MODE); + if (EFI_ERROR (Status)) { + FAIL ("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 ("SecEnrollDefaultKeysApp: 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