Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1ppOXw-0002zs-5U for mharc-grub-devel@gnu.org; Thu, 20 Apr 2023 03:17:36 -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 1ppOXq-0002xn-MO for grub-devel@gnu.org; Thu, 20 Apr 2023 03:17:33 -0400 Received: from mail-vi1eur05on2060.outbound.protection.outlook.com ([40.107.21.60] helo=EUR05-VI1-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 1ppOXo-0002As-Mz for grub-devel@gnu.org; Thu, 20 Apr 2023 03:17:30 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ia+T99FrXNXRa11rFHqsMCyHr/UohQIkR59ZGTfJrXXKoaLq8kjvkqPEHOOQFpJT+VWe4xfaX1xxBBvEBjLR+C2bYDBy2c0OZDBrOWEOidE0/6wZgKxBC6ZlkYo4nu4xvg09zUGM/pZBqDKcLGws79YSsdr2ImADNgCHwA9I11TmrBFojonTGUK9vVMw69GG4rf3R3hBC/fmHAQF7UIQ4PbdUMaROo0QhSr3Vmkwmpy2OS8LM7fA74nf0uDm3AQqCRo0TQMf23ZmLRNyWwdWowrK53PuAzLBV/c+qyuSfdGp+kZvyKO32sxAxlk3kbQ31iqk0j2oV5aKasH82kWxuA== 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=A0QJnHRhNy5GlIthFGhHYsEg7odUcjli721/KIJ8pPw=; b=ll8qWEYPbJEPdkJacepeFUHKQMLH0dRWLXVBH1IZsD5SXX1xNBNkGVzsieOSQto4UA1JLU9QACrvATzEEn+5PmuMhkGGF8DNPMy9jSbsHWx9mV+E3mpj9o0UWRItMblCOAUj9HFQOUJMce8v6+TxrB0UYv5rrGzKUWiSz4TgBcsbtUTTqdikKifUSq+XQudqXe+eWEybtw31O7pXUqVk+QpGYfz3s2aQqXOEaLzELgAQ8H02QK1PZSO5ev6kRC4zrCYaxX2ISBTbAbC8Xxy+uvjZAsw2LRO8m+UquaGNA9SYzwqsKdQLa0sW/KmYmuN8unu10YRMfe5RHFNNmekFtA== 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=A0QJnHRhNy5GlIthFGhHYsEg7odUcjli721/KIJ8pPw=; b=0NKFN4AGe3DeX5UzBH7t0L732C6PIYuU94ivIdLUviBfliuE0dV69Bt7Yx5nsVwu1XR0RfTPDZqmc2xU+YJnlPwDkKcMJYxtXeBWFiMmxDXtW+Il7mjZnxU+WgVAo37Wb+9W4EP7p3RWSw4KDKQ3oWNHnRyR03LLXaSx80i+toRrFiFF6sl2CYPTxY6E7aaziSjRvCGdeWyJwA3h/pjcv7ujtKiKdSnF1GBFdy6V89hP6OeHLhoENAcZPaCvwm2NTWUbosivt+hYcT69Is90/t53hvDnkDKmhwGRwl+x66kGkcUVp+6eRa/ipoHTb632Dku4/FrYp7ljHZplJ4yrbA== 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 DUZPR04MB9745.eurprd04.prod.outlook.com (2603:10a6:10:4e1::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6319.22; Thu, 20 Apr 2023 07:17:21 +0000 Received: from DBBPR04MB6140.eurprd04.prod.outlook.com ([fe80::dc0f:b8e1:84c9:2906]) by DBBPR04MB6140.eurprd04.prod.outlook.com ([fe80::dc0f:b8e1:84c9:2906%6]) with mapi id 15.20.6298.045; Thu, 20 Apr 2023 07:17:21 +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 v4 08/12] protectors: Add key protectors framework Date: Thu, 20 Apr 2023 15:15:44 +0800 Message-Id: <20230420071548.28286-9-glin@suse.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20230420071548.28286-1-glin@suse.com> References: <20230420071548.28286-1-glin@suse.com> Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: TYCP286CA0033.JPNP286.PROD.OUTLOOK.COM (2603:1096:400:29d::7) To DBBPR04MB6140.eurprd04.prod.outlook.com (2603:10a6:10:c7::18) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DBBPR04MB6140:EE_|DUZPR04MB9745:EE_ X-MS-Office365-Filtering-Correlation-Id: 3a44fc77-9e97-492b-215d-08db416f47f1 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: WpGofO9KG8MbbVxXaQ9gnGODrU5o6It7kvSrl/ntC9FFpHod/zQH9DlotJ8JOeGsF/S0fmQIi947xIkYYvbFquBUCeUs+xQeMuZ4SfDtNg6YOVyhPWMhn/APpPhxTsK+pDx9HXsAGC8elF58SH6/j7ce6h830EEbMo9QPwMbStq4fXmDSBoxVG6pDXjX7g8ey6r3b+jZQoRbr4Nw/p/rwqY6AAN0TpRqf05bhaIDiSCP1/8P+6RIhj0PvphQyWJbe1hgBxa8bzrmDcgwFiziYuJOkDeI5eu+flSUjD0yu+lM2KqI/qGyoUabLyw2zV2P7SCscVdCeHjGXze5/A7pasQFudbbHQvL5k8Jj+OfP7c/c0FPIl0OWpT/dzh0cdVDoO3ZdoX3HWItpsoNx7c8UXfqycLMOePg/uVv6pJRuOYjttNdNdrb20nGYjsTIgSyGZ0yNf00XW+aZsl0wq3mikz1qzgs8arXKWqP8b8KEJv/40ierd8Sv2cyermrEnCKyh6R9hRBEjJPrlU15isWopejr6A9IEX/5o7WxO24k0Bb74ZG5UWCyaJv90TXo4Ghh5xJwp0BmH5PZbAoVGiiCHjt3ycQtUrKahVE2gF5HbU= 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:(13230028)(136003)(366004)(376002)(346002)(396003)(39860400002)(451199021)(36756003)(6916009)(4326008)(54906003)(316002)(66946007)(66556008)(66476007)(186003)(45080400002)(478600001)(6486002)(6666004)(8936002)(5660300002)(41300700001)(8676002)(2906002)(7416002)(86362001)(38100700002)(2616005)(6512007)(1076003)(26005)(107886003)(6506007)(83380400001)(66899021)(2004002); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?Zo1uHmjnx8vsFgiYgk2o+qHDgCBu9rgNkDofBhVHyvdndQrpqK2gHlOlbVOB?= =?us-ascii?Q?ObsoUdsCCrjR+2lZNdtSQ0E9GSG+e684+cpxsTdHmHXfrug2g5jUIA/Spt/O?= =?us-ascii?Q?7bnlPnmr9tdrGt4dThr8hu1azOqdcLt3PUdDSM0Wf9BadvTvsVVKkAIj0u84?= =?us-ascii?Q?sbLKiUUd8zV8M7QNw8l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Z0xCD0kZEQupSnr3/eg0S?= =?us-ascii?Q?r4FNl7zFbuZmBvZt4cWF/lGpe+Ja84vPvM/gKMs8P+bk+C/qHiasm8UDaFMa?= =?us-ascii?Q?beGZVpES3YypgWd8pJ/ZPPgGdDjnw6StfNXryZF3GvgN0YOIcbhxk9G786BV?= =?us-ascii?Q?fjMOIP6DNY3kx++AmeTDhNbNyYk+D/3VLdE6rvSOsbX8IuecU7aqpoBRbF/R?= =?us-ascii?Q?q89EXLavkUJeqmYk0P9Yth93zbEcJchrlZiPzaiwpUCDe8aX+5sblKqG8osF?= =?us-ascii?Q?pRnRsSxHBcaVqX4lerI35Lj5Z1Ckbh1LboYuU/w8+oVU0Wr6X8jtBI34/2Yd?= =?us-ascii?Q?lkHcHNgtpabhr+M7C7gjb6Zmtn+zjDDxeZxnss4/3lP7NJIDgwIS6nuvj2tp?= =?us-ascii?Q?2t+rpP9+u5OYMYZ8tsFVnRKpt4zm5o5fLSwdrW6Q4WjQGPipO+4GgSWPiuRW?= =?us-ascii?Q?0ReykPBjkh/Iuc/yeByby3zX/JIFKAg4vJc0mma02ZK35WnU7iRb7yU1Nzrk?= =?us-ascii?Q?rZqG9hvFYavEx48haW+uQBHKUz4Qny1GgUr33HNn?= X-OriginatorOrg: suse.com X-MS-Exchange-CrossTenant-Network-Message-Id: 3a44fc77-9e97-492b-215d-08db416f47f1 X-MS-Exchange-CrossTenant-AuthSource: DBBPR04MB6140.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Apr 2023 07:17:21.0297 (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: vpRMHTx0rWlAG/XA1t1dQXEfuP7Mk/Bf4uhL6MLBuOY7FT5f4uYeTeb+VOle8QLp X-MS-Exchange-Transport-CrossTenantHeadersStamped: DUZPR04MB9745 Received-SPF: pass client-ip=40.107.21.60; envelope-from=GLin@suse.com; helo=EUR05-VI1-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, T_SCC_BODY_TEXT_LINE=-0.01 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: Thu, 20 Apr 2023 07:17:34 -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 d83c9f7b6..0335d9add 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..5ee059565 --- /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 == NULL || protector->name == NULL || grub_strlen(protector->name) == 0) + 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 == NULL) + 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 == NULL) + return GRUB_ERR_OUT_OF_RANGE; + + if (protector == NULL || grub_strlen (protector) == 0) + return GRUB_ERR_BAD_ARGUMENT; + + kp = grub_named_list_find (GRUB_AS_NAMED_LIST (grub_key_protectors), + protector); + if (kp == NULL) + 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