tpm.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2018 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * Core TPM support code.
  19. */
  20. #include <grub/err.h>
  21. #include <grub/i18n.h>
  22. #include <grub/misc.h>
  23. #include <grub/mm.h>
  24. #include <grub/tpm.h>
  25. #include <grub/term.h>
  26. #include <grub/verify.h>
  27. #include <grub/dl.h>
  28. GRUB_MOD_LICENSE ("GPLv3+");
  29. static grub_err_t
  30. grub_tpm_verify_init (grub_file_t io,
  31. enum grub_file_type type __attribute__ ((unused)),
  32. void **context, enum grub_verify_flags *flags)
  33. {
  34. *context = io->name;
  35. *flags |= GRUB_VERIFY_FLAGS_SINGLE_CHUNK;
  36. return GRUB_ERR_NONE;
  37. }
  38. static grub_err_t
  39. grub_tpm_verify_write (void *context, void *buf, grub_size_t size)
  40. {
  41. return grub_tpm_measure (buf, size, GRUB_BINARY_PCR, context);
  42. }
  43. static grub_err_t
  44. grub_tpm_verify_string (char *str, enum grub_verify_string_type type)
  45. {
  46. const char *prefix = NULL;
  47. char *description;
  48. grub_err_t status;
  49. switch (type)
  50. {
  51. case GRUB_VERIFY_KERNEL_CMDLINE:
  52. prefix = "kernel_cmdline: ";
  53. break;
  54. case GRUB_VERIFY_MODULE_CMDLINE:
  55. prefix = "module_cmdline: ";
  56. break;
  57. case GRUB_VERIFY_COMMAND:
  58. prefix = "grub_cmd: ";
  59. break;
  60. }
  61. description = grub_malloc (grub_strlen (str) + grub_strlen (prefix) + 1);
  62. if (!description)
  63. return grub_errno;
  64. grub_memcpy (description, prefix, grub_strlen (prefix));
  65. grub_memcpy (description + grub_strlen (prefix), str,
  66. grub_strlen (str) + 1);
  67. status =
  68. grub_tpm_measure ((unsigned char *) str, grub_strlen (str),
  69. GRUB_STRING_PCR, description);
  70. grub_free (description);
  71. return status;
  72. }
  73. struct grub_file_verifier grub_tpm_verifier = {
  74. .name = "tpm",
  75. .init = grub_tpm_verify_init,
  76. .write = grub_tpm_verify_write,
  77. .verify_string = grub_tpm_verify_string,
  78. };
  79. GRUB_MOD_INIT (tpm)
  80. {
  81. grub_verifier_register (&grub_tpm_verifier);
  82. }
  83. GRUB_MOD_FINI (tpm)
  84. {
  85. grub_verifier_unregister (&grub_tpm_verifier);
  86. }