key_protector.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2022 Microsoft Corporation
  4. * Copyright (C) 2024 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <grub/dl.h>
  20. #include <grub/list.h>
  21. #include <grub/misc.h>
  22. #include <grub/mm.h>
  23. #include <grub/key_protector.h>
  24. GRUB_MOD_LICENSE ("GPLv3+");
  25. struct grub_key_protector *grub_key_protectors = NULL;
  26. grub_err_t
  27. grub_key_protector_register (struct grub_key_protector *protector)
  28. {
  29. if (protector == NULL || protector->name == NULL || protector->name[0] == '\0')
  30. return grub_error (GRUB_ERR_BAD_ARGUMENT, "Invalid key protector for registration");
  31. if (grub_key_protectors != NULL &&
  32. grub_named_list_find (GRUB_AS_NAMED_LIST (grub_key_protectors), protector->name) != NULL)
  33. return grub_error (GRUB_ERR_BAD_ARGUMENT, "Key protector '%s' already registered", protector->name);
  34. grub_list_push (GRUB_AS_LIST_P (&grub_key_protectors), GRUB_AS_LIST (protector));
  35. return GRUB_ERR_NONE;
  36. }
  37. grub_err_t
  38. grub_key_protector_unregister (struct grub_key_protector *protector)
  39. {
  40. if (protector == NULL)
  41. return grub_error (GRUB_ERR_BAD_ARGUMENT, "Invalid key protector for unregistration");
  42. grub_list_remove (GRUB_AS_LIST (protector));
  43. return GRUB_ERR_NONE;
  44. }
  45. grub_err_t
  46. grub_key_protector_recover_key (const char *protector, grub_uint8_t **key,
  47. grub_size_t *key_size)
  48. {
  49. struct grub_key_protector *kp = NULL;
  50. if (grub_key_protectors == NULL)
  51. return grub_error (GRUB_ERR_OUT_OF_RANGE, "No key protector registered");
  52. if (protector == NULL || protector[0] == '\0')
  53. return grub_error (GRUB_ERR_BAD_ARGUMENT, "Invalid key protector");
  54. kp = grub_named_list_find (GRUB_AS_NAMED_LIST (grub_key_protectors), protector);
  55. if (kp == NULL)
  56. return grub_error (GRUB_ERR_OUT_OF_RANGE, "Key protector '%s' not found", protector);
  57. return kp->recover_key (key, key_size);
  58. }