vaseboot/VasEBoot-core/disk/key_protector.c

74 lines
2.7 KiB
C

/*
* VAS_EBOOT -- GRand Unified Bootloader
* Copyright (C) 2022 Microsoft Corporation
* Copyright (C) 2024 Free Software Foundation, Inc.
*
* VAS_EBOOT 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.
*
* VAS_EBOOT 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 VAS_EBOOT. If not, see <http://www.gnu.org/licenses/>.
*/
#include <VasEBoot/dl.h>
#include <VasEBoot/list.h>
#include <VasEBoot/misc.h>
#include <VasEBoot/mm.h>
#include <VasEBoot/key_protector.h>
VAS_EBOOT_MOD_LICENSE ("GPLv3+");
struct VasEBoot_key_protector *VasEBoot_key_protectors = NULL;
VasEBoot_err_t
VasEBoot_key_protector_register (struct VasEBoot_key_protector *protector)
{
if (protector == NULL || protector->name == NULL || protector->name[0] == '\0')
return VasEBoot_error (VAS_EBOOT_ERR_BAD_ARGUMENT, "Invalid key protector for registration");
if (VasEBoot_key_protectors != NULL &&
VasEBoot_named_list_find (VAS_EBOOT_AS_NAMED_LIST (VasEBoot_key_protectors), protector->name) != NULL)
return VasEBoot_error (VAS_EBOOT_ERR_BAD_ARGUMENT, "Key protector '%s' already registered", protector->name);
VasEBoot_list_push (VAS_EBOOT_AS_LIST_P (&VasEBoot_key_protectors), VAS_EBOOT_AS_LIST (protector));
return VAS_EBOOT_ERR_NONE;
}
VasEBoot_err_t
VasEBoot_key_protector_unregister (struct VasEBoot_key_protector *protector)
{
if (protector == NULL)
return VasEBoot_error (VAS_EBOOT_ERR_BAD_ARGUMENT, "Invalid key protector for unregistration");
VasEBoot_list_remove (VAS_EBOOT_AS_LIST (protector));
return VAS_EBOOT_ERR_NONE;
}
VasEBoot_err_t
VasEBoot_key_protector_recover_key (const char *protector, VasEBoot_uint8_t **key,
VasEBoot_size_t *key_size)
{
struct VasEBoot_key_protector *kp = NULL;
if (VasEBoot_key_protectors == NULL)
return VasEBoot_error (VAS_EBOOT_ERR_OUT_OF_RANGE, "No key protector registered");
if (protector == NULL || protector[0] == '\0')
return VasEBoot_error (VAS_EBOOT_ERR_BAD_ARGUMENT, "Invalid key protector");
kp = VasEBoot_named_list_find (VAS_EBOOT_AS_NAMED_LIST (VasEBoot_key_protectors), protector);
if (kp == NULL)
return VasEBoot_error (VAS_EBOOT_ERR_OUT_OF_RANGE, "Key protector '%s' not found", protector);
return kp->recover_key (key, key_size);
}