Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1petZJ-0003Ev-HM for mharc-grub-devel@gnu.org; Wed, 22 Mar 2023 04:11:37 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1petZI-0003Dp-Bm for grub-devel@gnu.org; Wed, 22 Mar 2023 04:11:36 -0400 Received: from mail-am6eur05on2053.outbound.protection.outlook.com ([40.107.22.53] helo=EUR05-AM6-obe.outbound.protection.outlook.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1petZF-00032h-Vu for grub-devel@gnu.org; Wed, 22 Mar 2023 04:11:36 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=DqJb4RxWEXuGiShqZzIHVWykUeraLiiZTK5YCy9CrIBlBZJqupE6b+0b605bubsX2PXwsUAguAluaKpPbIxCoYl/325UI/DijITHxDZWWw8pKDyg3POSWx24vbhn1YkQ2jFmDJC5C1QLKe8D1+kkLlZ0pGRR/3CUt1W3Rj5++k5hIN4TA2gXY/obs7uk0VGIeWW2ThEfsutXynN4qUG2Ic4+I3YQ5rJ1XeJlJTYqJcJjUIWz9g87y4HxLsasGNnCEvol2x4PBAVuJnSn20KKCAoUM9P+sPHvoRotXApS23OK4694Mk21c/K0QY2aMvGzji2MvtcXb6j2S4ymHUkQxg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=Bc2n2k05AZKZm0M5ck4GkNBTeR1G5J6NF8j/zq/UR8Q=; b=VMSc6TrFy/F0VC5E5GKM1asYCPkdR9gy+Y23jpyYVbANcP81TuRgaG9uT1BSTEtoctqCE3pNXuzMKYrQxS3ry2SdXkoeOGztJ0GXTjuQVLuZ2k6o1tPlmq3XLQL3ksbofhiu7IQQU1+Hecth65sNUEqNo8agozwjimnaBcjtrV3qVrwK9vT6JQ2N3Mnp7qloFE0MVf2Clh2mhQrDOq8FqiuIlTIYBTMYhAkC5Fb90JhqVhxCvmtN5jtUy76AJKaXKRjZkikt+qFEHou5pblFTsr6nuLFK9d37KZiUPJb0ssvCwWYF3WvUKC+jS0YPTDtdWjmWfKc1hdUyU2U4wtiFA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=suse.com; dmarc=pass action=none header.from=suse.com; dkim=pass header.d=suse.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=Bc2n2k05AZKZm0M5ck4GkNBTeR1G5J6NF8j/zq/UR8Q=; b=4C2LUy0oWQ9UDLyUNPLF30awgsbVuYy20IVfRJyifqqjSUFsknBxpfKK7YFKmIlcVzLjLOvQ2FPDmBK0SGMjjovvhWbJNqluy3yigxvAlv4RrrAy6AKk4yhcdMrEQjdzWe2uD3GdmwZ6nEhFjTsRytTAE2wp8JOm1dXTAOcGtBdykiOFc7Bv+MyeRqOmT5R9WNtwAD0yH2gFt80iZLF4vZGPbdw08LL6PLVkvWMbC9Qqah9cBsf6Y6xVtBL9Eer1Emo7O178IS3VDlF7miHk4sxx6tW2/MyD7hoi+h4j1JAuSwm7IIMg2yS3Rs/QkHzdRUdEQDt98PgDQxxyuu4i4g== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=suse.com; Received: from DBBPR04MB6140.eurprd04.prod.outlook.com (2603:10a6:10:c7::18) by DU0PR04MB9585.eurprd04.prod.outlook.com (2603:10a6:10:316::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6178.37; Wed, 22 Mar 2023 08:11:31 +0000 Received: from DBBPR04MB6140.eurprd04.prod.outlook.com ([fe80::688f:bc10:59e2:772a]) by DBBPR04MB6140.eurprd04.prod.outlook.com ([fe80::688f:bc10:59e2:772a%6]) with mapi id 15.20.6178.037; Wed, 22 Mar 2023 08:11:31 +0000 From: Gary Lin To: The development of GNU GRUB Cc: Hernan Gatta , Daniel Axtens , 'Daniel Kiper ' , shkhisti@microsoft.com, jaskaran.khurana@microsoft.com, christopher.co@microsoft.com, daniel.mihai@microsoft.com, rharwood@redhat.com, jaredz@redhat.com, development@efficientek.com, jejb@linux.ibm.com, mchang@suse.com Subject: [PATCH v2 07/11] protectors: Add key protectors framework Date: Wed, 22 Mar 2023 16:10:12 +0800 Message-Id: <20230322081016.12075-8-glin@suse.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20230322081016.12075-1-glin@suse.com> References: <20230322081016.12075-1-glin@suse.com> Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: TYAPR01CA0231.jpnprd01.prod.outlook.com (2603:1096:404:11e::27) To DBBPR04MB6140.eurprd04.prod.outlook.com (2603:10a6:10:c7::18) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DBBPR04MB6140:EE_|DU0PR04MB9585:EE_ X-MS-Office365-Filtering-Correlation-Id: 8c3ec531-1cef-4b6c-9489-08db2aad0b4e X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: F7An7B4UkykKtoXJXRyzxFMuYSBNnaiUR8lSm1QZI4QRiRg5M6sJJyCAbSel4MEyDapytUlxb3tIsP4gU/axOJMGp+U4Se89jaLQZcc394ADPbGRcb6fZp1z/gHzfL3pznB1fHrF5vpax7gW1r0fj3HPqrbbK+YRqSykrgotwNhQUJzuyNZpBVKVWqkMsjR5F4/3te047IAoFW65zJfUaqZpLdRcQJKpclLp9VCBpJTMK0Jos0JRghxbNIUmtATSJWxUKL2h8CV3xzbVYX1MJzmOB8QPj/mrfQ0t/jEYh+Gkmv2dpz1J0QNohTy57UdUrBD2jwrKYSkvwyX/AcXCrsOYVHUwvgB4oSBBX8B5hzHi9nbTGIcH2FINxqGwgxCEybWRsIYFJ5qo9SiZmPHhO0kFvaRo/8azeGxNoMqhyh1mxqxoEGEapu0olXD0ygU9qg5tHglxCcXIA5SQgf2eGo4774gYxP5wxbFC7CVeDoE+pf4fQXdVfH1je1ntLQcc53nU478eWDsNrZpCk5Uj6HFqQtXss0H5yTpe612xLr/2SXyEAr8c5fodich+2GLQGNGbSpzsD+o+wDbLh2XUGNIb6M3m510329wEEC9GSus31bgG7HOguo5YoVbEBk7K6I4/hR5HX6oyWWueEYozXyzIc6af7prP2dPRS/EItAVFZz7C7PB8BaW0+Ab3UXN2 X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:DBBPR04MB6140.eurprd04.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230025)(346002)(396003)(39860400002)(366004)(136003)(376002)(451199018)(478600001)(2616005)(6512007)(54906003)(83380400001)(86362001)(38100700002)(66556008)(66476007)(41300700001)(36756003)(8676002)(4326008)(66946007)(2906002)(5660300002)(6916009)(6666004)(7416002)(1076003)(8936002)(316002)(6486002)(45080400002)(6506007)(26005)(186003)(107886003)(66899018)(2004002); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?j4/XAmX4ZBaVTVI0igCsD+jQSmuJMrpgFCQntz8TOjnGF/fSFz5WMzHzALRK?= =?us-ascii?Q?iNkKfYzAugKveyPbsT5T+yM/T59sCpURMNVPmkujYmdZ4o0/f7PeNqHU6hdm?= =?us-ascii?Q?N4nWrZpN/vfWVqezNBHa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ii?Q?SjSeQ0bdj4Cd+2HDdSWwdlhXY4rHnaTQJvFJkdxXuNKR5F2anc4IYGwSWnnT?= =?us-ascii?Q?0f7Belf6M6eT76R5MRWOB3Rm9IMmou8M8iwSc4RYtaVevnULg+E8fp0O0yJs?= =?us-ascii?Q?S5yXuyvXSTg7y2Tes5bJlg5vtJPZj9jIOkFW/Z9R4HoFCEx+0dHKJDC1/bXm?= =?us-ascii?Q?crjo5bIPVxKqqdnX8G/yKQcfw2d2gZWIc0kjUltbh0O/pqCPKQdj+nh+JaM7?= =?us-ascii?Q?MKx4wx6ryjd4yxzoX2hVlxaZK2hThpgpeVKyvmCg9/aYA76NdnY5z18WbMCC?= =?us-ascii?Q?AmrInb0bTJVJ7fdT0JwJ6f88dwjAkdry6ZgLbXYUBotCCveiUmrtqdF6F7G+?= =?us-ascii?Q?RrtFtpFkT5tvVwC3kQFFYu5p8i+gQ+eobgt3yEqjs4XfspEyxdkddqHp/Ud1?= =?us-ascii?Q?zLHvMZ09211f6V52TeKkMbk0L/w+7IUHiWT3g/ELJtKaMfuDjfV8qulmTXHX?= =?us-ascii?Q?oW2FrFZnAkTxc6Gtu+UiIPKjOkBbXOrUpA/Hywnkq3ujzgxoZCYWWzeC4+TS?= =?us-ascii?Q?K/STaaeDKo9sD//ys3NBqMf0XG/E8d38nF0PIX9V?= X-OriginatorOrg: suse.com X-MS-Exchange-CrossTenant-Network-Message-Id: 8c3ec531-1cef-4b6c-9489-08db2aad0b4e X-MS-Exchange-CrossTenant-AuthSource: DBBPR04MB6140.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Mar 2023 08:11:31.4996 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: f7a17af6-1c5c-4a36-aa8b-f5be247aa4ba X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: kd528vbDTEhLqFRIZDKro7zVfXCMEeJDaVlgXJl1WEGUhJpiukvxmn+4Zw37Byy4 X-MS-Exchange-Transport-CrossTenantHeadersStamped: DU0PR04MB9585 Received-SPF: pass client-ip=40.107.22.53; envelope-from=GLin@suse.com; helo=EUR05-AM6-obe.outbound.protection.outlook.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 22 Mar 2023 08:11:36 -0000 From: Hernan Gatta A key protector encapsulates functionality to retrieve an unlocking key for a fully-encrypted disk from a specific source. A key protector module registers itself with the key protectors framework when it is loaded and unregisters when unloaded. Additionally, a key protector may accept parameters that describe how it should operate. The key protectors framework, besides offering registration and unregistration functions, also offers a one-stop routine for finding and invoking a key protector by name. If a key protector with the specified name exists and if an unlocking key is successfully retrieved by it, the function returns to the caller the retrieved key and its length. Signed-off-by: Hernan Gatta Signed-off-by: Gary Lin --- grub-core/Makefile.am | 1 + grub-core/Makefile.core.def | 1 + grub-core/kern/protectors.c | 75 +++++++++++++++++++++++++++++++++++++ include/grub/protector.h | 48 ++++++++++++++++++++++++ 4 files changed, 125 insertions(+) create mode 100644 grub-core/kern/protectors.c create mode 100644 include/grub/protector.h diff --git a/grub-core/Makefile.am b/grub-core/Makefile.am index 80e7a83ed..79d17a3d2 100644 --- a/grub-core/Makefile.am +++ b/grub-core/Makefile.am @@ -90,6 +90,7 @@ endif KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/mm.h KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/parser.h KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/partition.h +KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/protector.h KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/stack_protector.h KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/term.h KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/time.h diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def index 9b4423d47..53b680a77 100644 --- a/grub-core/Makefile.core.def +++ b/grub-core/Makefile.core.def @@ -144,6 +144,7 @@ kernel = { common = kern/misc.c; common = kern/parser.c; common = kern/partition.c; + common = kern/protectors.c; common = kern/rescue_parser.c; common = kern/rescue_reader.c; common = kern/term.c; diff --git a/grub-core/kern/protectors.c b/grub-core/kern/protectors.c new file mode 100644 index 000000000..934f831cd --- /dev/null +++ b/grub-core/kern/protectors.c @@ -0,0 +1,75 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2022 Microsoft Corporation + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#include +#include +#include +#include + +struct grub_key_protector *grub_key_protectors = NULL; + +grub_err_t +grub_key_protector_register (struct grub_key_protector *protector) +{ + if (!protector || !protector->name || !grub_strlen(protector->name)) + return GRUB_ERR_BAD_ARGUMENT; + + if (grub_key_protectors && + grub_named_list_find (GRUB_AS_NAMED_LIST (grub_key_protectors), + protector->name)) + return GRUB_ERR_BAD_ARGUMENT; + + grub_list_push (GRUB_AS_LIST_P (&grub_key_protectors), + GRUB_AS_LIST (protector)); + + return GRUB_ERR_NONE; +} + +grub_err_t +grub_key_protector_unregister (struct grub_key_protector *protector) +{ + if (!protector) + return GRUB_ERR_BAD_ARGUMENT; + + grub_list_remove (GRUB_AS_LIST (protector)); + + return GRUB_ERR_NONE; +} + +grub_err_t +grub_key_protector_recover_key (const char *protector, grub_uint8_t **key, + grub_size_t *key_size) +{ + struct grub_key_protector *kp = NULL; + + if (!grub_key_protectors) + return GRUB_ERR_OUT_OF_RANGE; + + if (!protector || !grub_strlen (protector)) + return GRUB_ERR_BAD_ARGUMENT; + + kp = grub_named_list_find (GRUB_AS_NAMED_LIST (grub_key_protectors), + protector); + if (!kp) + return grub_error (GRUB_ERR_OUT_OF_RANGE, + N_("A key protector with name '%s' could not be found. " + "Is the name spelled correctly and is the " + "corresponding module loaded?"), protector); + + return kp->recover_key (key, key_size); +} diff --git a/include/grub/protector.h b/include/grub/protector.h new file mode 100644 index 000000000..3d9f69bce --- /dev/null +++ b/include/grub/protector.h @@ -0,0 +1,48 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2022 Microsoft Corporation + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#ifndef GRUB_PROTECTOR_HEADER +#define GRUB_PROTECTOR_HEADER 1 + +#include +#include + +struct grub_key_protector +{ + struct grub_key_protector *next; + struct grub_key_protector **prev; + + const char *name; + + grub_err_t (*recover_key) (grub_uint8_t **key, grub_size_t *key_size); +}; + +extern struct grub_key_protector *EXPORT_VAR (grub_key_protectors); + +grub_err_t +EXPORT_FUNC (grub_key_protector_register) (struct grub_key_protector *protector); + +grub_err_t +EXPORT_FUNC (grub_key_protector_unregister) (struct grub_key_protector *protector); + +grub_err_t +EXPORT_FUNC (grub_key_protector_recover_key) (const char *protector, + grub_uint8_t **key, + grub_size_t *key_size); + +#endif /* ! GRUB_PROTECTOR_HEADER */ -- 2.35.3