Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1pmTmn-0006As-ID for mharc-grub-devel@gnu.org; Wed, 12 Apr 2023 02:16:53 -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 1pmTmb-00066L-9T for grub-devel@gnu.org; Wed, 12 Apr 2023 02:16:41 -0400 Received: from mail-am6eur05on2086.outbound.protection.outlook.com ([40.107.22.86] 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 1pmTmZ-0006SV-8Q for grub-devel@gnu.org; Wed, 12 Apr 2023 02:16:41 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=G7ewnHm4sEz0OTSEFcC45SZ76EeUvkwkKXT1+ld0rUYAefB2M9EKRje4PwWPthes2AL+aheNTL7zT6etnvn9F9FlvkwuhCHT6vJMU7naYLH74uF4BUrjjRHPvMUIkerykBAz125GC1nXaIo7K7Eyp6/H/zwgytJxqIVu9+YGpmxMkOb5+PinZ4bKp35fWkMnOult3UuyP39oXTEAPNzL9P2cmYqrWAEBJp7LJhCS6YieiLbE12LOaq9bZCI4DyW+oiNAGX/M+oir4ujOE6U6ynxNcxo8TmUtSpvcoex2Qa9pAXpbz+CW+lD4xZA98WQkipKU0zEvKbV7kcL4Y6YN1g== 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=Biw3h2t7GRgg+w95RqiXo7OOGTOoR9L0hb+AbAnud54=; b=lTT3Fnc2ZsC7llwlex6XtXzRjPzg9ts9yyK1ys4Kr0wOQHmyWRmPSroqGsMJ6TG+Ynld6eGynmndtICaGLpMmP2vDZKkYnmE+pYEPA4YIkjwJbpvmAcYzJF+RCLO5POpDHntfJdGZrDCZQUvBckDMeZIm9t0xyhEQJpjbN9nFTz6A7FBjdICZO1/3MdZkxduYhOX/Lz48tmEvdMgAnazeKK2O+zHyaFPBQNSyGLtWf7aFV4W0UTLeMeKujZSEEYUr0wpGVdZjAZ9zIkPbwOKYHXmenmlr71B9X0bLk0PR4FmwysvvjwYGq/flL8f1KT0KcMy+Kx4mEWeQnCls8UFFQ== 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=Biw3h2t7GRgg+w95RqiXo7OOGTOoR9L0hb+AbAnud54=; b=lKQpdTvb3zT1p1ia9SHHTRy/s1bnwnvyBFO/2sZZSHzJ4lPdwHADAb7jRppCDOAlUrcWI/pi3ly/G6jGNNp0kenaOq3jyb51ST+d7AamW7mPC1BQfE1gVhB0GmOSEp6lIslWIUDbX5Hu714oT4uXBnwDm9rM3BJNTze7s4ik1mTQggQwUqkgk/mp1/tLAbGAiEEs0586h7eXamSyPqafeQ2LK5OnbcKHFdH8RHLYE0ilsId9NEn5PCeGyn0WaVk4eIyFxvySzzU82i1SFiCX0DLBM9e2xFjOsnq9Zyfw++IjKMtt83PP0B1k3ud6DwvtBinJUB0LXPJUcpD8Fp7n7A== 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 DU2PR04MB8663.eurprd04.prod.outlook.com (2603:10a6:10:2de::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6298.30; Wed, 12 Apr 2023 06:16:30 +0000 Received: from DBBPR04MB6140.eurprd04.prod.outlook.com ([fe80::8e45:fffd:c1eb:3ebd]) by DBBPR04MB6140.eurprd04.prod.outlook.com ([fe80::8e45:fffd:c1eb:3ebd%4]) with mapi id 15.20.6277.038; Wed, 12 Apr 2023 06:16:30 +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 v3 08/12] protectors: Add key protectors framework Date: Wed, 12 Apr 2023 14:15:49 +0800 Message-Id: <20230412061553.7597-9-glin@suse.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20230412061553.7597-1-glin@suse.com> References: <20230412061553.7597-1-glin@suse.com> Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: FR3P281CA0022.DEUP281.PROD.OUTLOOK.COM (2603:10a6:d10:1c::10) To DBBPR04MB6140.eurprd04.prod.outlook.com (2603:10a6:10:c7::18) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DBBPR04MB6140:EE_|DU2PR04MB8663:EE_ X-MS-Office365-Filtering-Correlation-Id: 464b25f2-ed6d-43ea-7dc7-08db3b1d74e8 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: mFtE2krs/IfYHbHMRr+QP4TCP2ftL19lZzROQxrku7nHR9qSH1QI08BhXXqfkbabCtm28vM61xYh8bN4gld/LeWyxjSVauU3tOnNBqXeq7UPjGHHsjo8bdEOjNZj7y3THHzrJXhUjVa8uwv+ipMbpPOB/Yi/2Hz0wDl7tDfNzQITpQUAkoVeOoasCitxHMGzsRhuNdCMwqkqk6LaSr/7gb6MSGfJjOA5g6d+LhBBKGaH+O229mObnyoIKXmnf4WpW6j0Wlbivxgzdj8WLZCW2rMG0/NK9C80eOalHp6ENBgr8QohKEtYaC+BXxrSJcj6oCixesaF8B3fixTSzGk6VRUFAalC6VGoJeDX4G/3KLvkYkrbNXn8JNXzis7PLE7+ZZIn4Chyr/bct3haOXS+dEUMI2D3v20zHH9DjvbTzVuuYmTWatabYeWthIa9BjAxA9JLPdEcCcT9gH7puMaE+bHwBkDdsx4stbaJtsUev4jxfJiuku3AMRSZwPK05TnJGsKcbn5kUZWRcoi6hjKNh0ZSVaCPZcHhp0pvgSIfXnnuv2uTLQlqPN4BYorrixXp/OLRGw77U3ylcTq/LYd43G/L4ikJBEpIloVfvlcdo6E= 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)(39860400002)(366004)(346002)(396003)(136003)(376002)(451199021)(83380400001)(107886003)(2616005)(45080400002)(478600001)(6486002)(6666004)(186003)(54906003)(26005)(1076003)(6506007)(6512007)(2906002)(7416002)(36756003)(5660300002)(38100700002)(66556008)(6916009)(4326008)(41300700001)(66476007)(66946007)(316002)(8936002)(86362001)(8676002)(66899021)(2004002); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?onNi9PYRu9Rg8IpqQ1kawNylUy2r4XFGmKgCmgJyTnhFrJckpLhZy6ckwR09?= =?us-ascii?Q?PCFz23K55L0XPBcqK+RbsksdQoeFRw7Ue6R4vuJ9xBEXItn4/OUOLQjds6Qk?= =?us-ascii?Q?W8fBiS227rf8DEXqIKoNo18pO5cTuunGQse0TMgKYs8yOx4zcDqIX1V5834R?= =?us-ascii?Q?7tm6OWWr4fL4vFTQm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?= =?us-ascii?Q?iIOUeJ0rXx6s+PQnS6X61P+2/9yxCZnaG1JDNfJWhhlfVaO6xG+8O6dfwni9?= =?us-ascii?Q?Z1b2aKy6/CaBqItempQeXjDzUUHV8vU02+yL096+WXD7UtPZa2Ps13L6eM2K?= =?us-ascii?Q?qLS8R/fCNNR2LW23dfohFeHSjaE/ux/TQJQdTUFxmQ55bO/qtEMzncIRdAaG?= =?us-ascii?Q?jivV2oV50Qo14krQaro9CCSFgu0rBXyN0EtxE0usqqhAwd1eGsUdraPcmUlj?= =?us-ascii?Q?bF8mGeNjsFD1e+KWbDHX1FUUTHaRwzh1pm5tx/oHrVxsqVpObs9QhMjPenIh?= =?us-ascii?Q?2xAP8XFJ7OvUF67bbJcSTEgjtagx7hdNeh8lQwMeSDBP0weNvQwEoqKv7o1D?= =?us-ascii?Q?MqkD/nxBxqgq2oo/EZ+/AicJo+xifOjnnUeqSPhIgeSvr1TTRXS5zLV7IPfk?= =?us-ascii?Q?d9iYruQeMkt+wGLFx63qzAHi9HWK5ZkyYFPXQijimXTSn9P0dt+DVjX/4m4s?= =?us-ascii?Q?gPQkeOc61zlcVBSy0os=3D?= X-OriginatorOrg: suse.com X-MS-Exchange-CrossTenant-Network-Message-Id: 464b25f2-ed6d-43ea-7dc7-08db3b1d74e8 X-MS-Exchange-CrossTenant-AuthSource: DBBPR04MB6140.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Apr 2023 06:16:30.7680 (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: 0BPY8jHg4Pe8qcUCI2oBnSVZq9oszTZ4Q7WLJ/nu93WqhRWFfzxhDM0G4Mb8Ud1w X-MS-Exchange-Transport-CrossTenantHeadersStamped: DU2PR04MB8663 Received-SPF: pass client-ip=40.107.22.86; 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, 12 Apr 2023 06:16:41 -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..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