tpm.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. * EFI TPM support code.
  19. */
  20. #include <grub/err.h>
  21. #include <grub/i18n.h>
  22. #include <grub/efi/api.h>
  23. #include <grub/efi/efi.h>
  24. #include <grub/efi/cc.h>
  25. #include <grub/efi/tpm.h>
  26. #include <grub/mm.h>
  27. #include <grub/tpm.h>
  28. #include <grub/term.h>
  29. typedef TCG_PCR_EVENT grub_tpm_event_t;
  30. static grub_guid_t tpm_guid = EFI_TPM_GUID;
  31. static grub_guid_t tpm2_guid = EFI_TPM2_GUID;
  32. static grub_guid_t cc_measurement_guid = GRUB_EFI_CC_MEASUREMENT_PROTOCOL_GUID;
  33. static grub_efi_handle_t *grub_tpm_handle;
  34. static grub_uint8_t grub_tpm_version;
  35. static grub_int8_t tpm1_present = -1;
  36. static grub_int8_t tpm2_present = -1;
  37. static grub_efi_boolean_t
  38. grub_tpm1_present (grub_efi_tpm_protocol_t *tpm)
  39. {
  40. grub_efi_status_t status;
  41. TCG_EFI_BOOT_SERVICE_CAPABILITY caps;
  42. grub_uint32_t flags;
  43. grub_efi_physical_address_t eventlog, lastevent;
  44. if (tpm1_present != -1)
  45. return (grub_efi_boolean_t) tpm1_present;
  46. caps.Size = (grub_uint8_t) sizeof (caps);
  47. status = tpm->status_check (tpm, &caps, &flags, &eventlog, &lastevent);
  48. if (status != GRUB_EFI_SUCCESS || caps.TPMDeactivatedFlag
  49. || !caps.TPMPresentFlag)
  50. tpm1_present = 0;
  51. else
  52. tpm1_present = 1;
  53. grub_dprintf ("tpm", "tpm1%s present\n", tpm1_present ? "" : " NOT");
  54. return (grub_efi_boolean_t) tpm1_present;
  55. }
  56. static grub_efi_boolean_t
  57. grub_tpm2_present (grub_efi_tpm2_protocol_t *tpm)
  58. {
  59. grub_efi_status_t status;
  60. EFI_TCG2_BOOT_SERVICE_CAPABILITY caps;
  61. caps.Size = (grub_uint8_t) sizeof (caps);
  62. if (tpm2_present != -1)
  63. return (grub_efi_boolean_t) tpm2_present;
  64. status = tpm->get_capability (tpm, &caps);
  65. if (status != GRUB_EFI_SUCCESS || !caps.TPMPresentFlag)
  66. tpm2_present = 0;
  67. else
  68. tpm2_present = 1;
  69. grub_dprintf ("tpm", "tpm2%s present\n", tpm2_present ? "" : " NOT");
  70. return (grub_efi_boolean_t) tpm2_present;
  71. }
  72. static grub_efi_boolean_t
  73. grub_tpm_handle_find (grub_efi_handle_t *tpm_handle,
  74. grub_efi_uint8_t *protocol_version)
  75. {
  76. grub_efi_handle_t *handles;
  77. grub_efi_uintn_t num_handles;
  78. if (grub_tpm_handle != NULL)
  79. {
  80. *tpm_handle = grub_tpm_handle;
  81. *protocol_version = grub_tpm_version;
  82. return 1;
  83. }
  84. handles = grub_efi_locate_handle (GRUB_EFI_BY_PROTOCOL, &tpm_guid, NULL,
  85. &num_handles);
  86. if (handles && num_handles > 0)
  87. {
  88. grub_tpm_handle = handles[0];
  89. *tpm_handle = handles[0];
  90. grub_tpm_version = 1;
  91. *protocol_version = 1;
  92. grub_dprintf ("tpm", "TPM handle Found, version: 1\n");
  93. return 1;
  94. }
  95. handles = grub_efi_locate_handle (GRUB_EFI_BY_PROTOCOL, &tpm2_guid, NULL,
  96. &num_handles);
  97. if (handles && num_handles > 0)
  98. {
  99. grub_tpm_handle = handles[0];
  100. *tpm_handle = handles[0];
  101. grub_tpm_version = 2;
  102. *protocol_version = 2;
  103. grub_dprintf ("tpm", "TPM handle Found, version: 2\n");
  104. return 1;
  105. }
  106. return 0;
  107. }
  108. static grub_err_t
  109. grub_efi_log_event_status (grub_efi_status_t status)
  110. {
  111. switch (status)
  112. {
  113. case GRUB_EFI_SUCCESS:
  114. return GRUB_ERR_NONE;
  115. case GRUB_EFI_DEVICE_ERROR:
  116. return grub_error (GRUB_ERR_IO, N_("command failed"));
  117. case GRUB_EFI_INVALID_PARAMETER:
  118. return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("invalid parameter"));
  119. case GRUB_EFI_BUFFER_TOO_SMALL:
  120. return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("output buffer too small"));
  121. case GRUB_EFI_NOT_FOUND:
  122. return grub_error (GRUB_ERR_UNKNOWN_DEVICE, N_("TPM unavailable"));
  123. default:
  124. return grub_error (grub_is_tpm_fail_fatal () ? GRUB_ERR_UNKNOWN_DEVICE : GRUB_ERR_NONE, N_("unknown TPM error"));
  125. }
  126. }
  127. static grub_err_t
  128. grub_tpm1_log_event (grub_efi_handle_t tpm_handle, unsigned char *buf,
  129. grub_size_t size, grub_uint8_t pcr,
  130. const char *description)
  131. {
  132. grub_tpm_event_t *event;
  133. grub_efi_status_t status;
  134. grub_efi_tpm_protocol_t *tpm;
  135. grub_efi_physical_address_t lastevent;
  136. grub_uint32_t algorithm;
  137. grub_uint32_t eventnum = 0;
  138. tpm = grub_efi_open_protocol (tpm_handle, &tpm_guid,
  139. GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL);
  140. if (!grub_tpm1_present (tpm))
  141. return 0;
  142. event = grub_zalloc (sizeof (*event) + grub_strlen (description) + 1);
  143. if (!event)
  144. return grub_error (GRUB_ERR_OUT_OF_MEMORY,
  145. N_("cannot allocate TPM event buffer"));
  146. event->PCRIndex = pcr;
  147. event->EventType = EV_IPL;
  148. event->EventSize = grub_strlen (description) + 1;
  149. grub_strcpy ((char *) event->Event, description);
  150. algorithm = TCG_ALG_SHA;
  151. status = tpm->log_extend_event (tpm, (grub_addr_t) buf, (grub_uint64_t) size,
  152. algorithm, event, &eventnum, &lastevent);
  153. grub_free (event);
  154. return grub_efi_log_event_status (status);
  155. }
  156. static grub_err_t
  157. grub_tpm2_log_event (grub_efi_handle_t tpm_handle, unsigned char *buf,
  158. grub_size_t size, grub_uint8_t pcr,
  159. const char *description)
  160. {
  161. EFI_TCG2_EVENT *event;
  162. grub_efi_status_t status;
  163. grub_efi_tpm2_protocol_t *tpm;
  164. tpm = grub_efi_open_protocol (tpm_handle, &tpm2_guid,
  165. GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL);
  166. if (!grub_tpm2_present (tpm))
  167. return 0;
  168. event =
  169. grub_zalloc (sizeof (EFI_TCG2_EVENT) + grub_strlen (description) + 1);
  170. if (!event)
  171. return grub_error (GRUB_ERR_OUT_OF_MEMORY,
  172. N_("cannot allocate TPM event buffer"));
  173. event->Header.HeaderSize = sizeof (EFI_TCG2_EVENT_HEADER);
  174. event->Header.HeaderVersion = 1;
  175. event->Header.PCRIndex = pcr;
  176. event->Header.EventType = EV_IPL;
  177. event->Size =
  178. sizeof (*event) - sizeof (event->Event) + grub_strlen (description) + 1;
  179. grub_strcpy ((char *) event->Event, description);
  180. status = tpm->hash_log_extend_event (tpm, 0, (grub_addr_t) buf,
  181. (grub_uint64_t) size, event);
  182. grub_free (event);
  183. return grub_efi_log_event_status (status);
  184. }
  185. static void
  186. grub_cc_log_event (unsigned char *buf, grub_size_t size, grub_uint8_t pcr,
  187. const char *description)
  188. {
  189. grub_efi_cc_event_t *event;
  190. grub_efi_status_t status;
  191. grub_efi_cc_protocol_t *cc;
  192. grub_efi_cc_mr_index_t mr;
  193. cc = grub_efi_locate_protocol (&cc_measurement_guid, NULL);
  194. if (cc == NULL)
  195. return;
  196. status = cc->map_pcr_to_mr_index (cc, pcr, &mr);
  197. if (status != GRUB_EFI_SUCCESS)
  198. {
  199. grub_efi_log_event_status (status);
  200. return;
  201. }
  202. event = grub_zalloc (sizeof (grub_efi_cc_event_t) +
  203. grub_strlen (description) + 1);
  204. if (event == NULL)
  205. {
  206. grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("cannot allocate CC event buffer"));
  207. return;
  208. }
  209. event->Header.HeaderSize = sizeof (grub_efi_cc_event_header_t);
  210. event->Header.HeaderVersion = GRUB_EFI_CC_EVENT_HEADER_VERSION;
  211. event->Header.MrIndex = mr;
  212. event->Header.EventType = EV_IPL;
  213. event->Size = sizeof (*event) + grub_strlen (description) + 1;
  214. grub_strcpy ((char *) event->Event, description);
  215. status = cc->hash_log_extend_event (cc, 0,
  216. (grub_efi_physical_address_t)(grub_addr_t) buf,
  217. (grub_efi_uint64_t) size, event);
  218. grub_free (event);
  219. if (status != GRUB_EFI_SUCCESS)
  220. grub_efi_log_event_status (status);
  221. }
  222. grub_err_t
  223. grub_tpm_measure (unsigned char *buf, grub_size_t size, grub_uint8_t pcr,
  224. const char *description)
  225. {
  226. grub_efi_handle_t tpm_handle;
  227. grub_efi_uint8_t protocol_version;
  228. grub_cc_log_event(buf, size, pcr, description);
  229. if (!grub_tpm_handle_find (&tpm_handle, &protocol_version))
  230. return 0;
  231. grub_dprintf ("tpm", "log_event, pcr = %d, size = 0x%" PRIxGRUB_SIZE ", %s\n",
  232. pcr, size, description);
  233. if (protocol_version == 1)
  234. return grub_tpm1_log_event (tpm_handle, buf, size, pcr, description);
  235. else
  236. return grub_tpm2_log_event (tpm_handle, buf, size, pcr, description);
  237. }
  238. int
  239. grub_tpm_present (void)
  240. {
  241. grub_efi_handle_t tpm_handle;
  242. grub_efi_uint8_t protocol_version;
  243. grub_efi_cc_protocol_t *cc;
  244. /*
  245. * When confidential computing measurement protocol is enabled
  246. * we assume the TPM is present.
  247. */
  248. cc = grub_efi_locate_protocol (&cc_measurement_guid, NULL);
  249. if (cc != NULL)
  250. return 1;
  251. if (!grub_tpm_handle_find (&tpm_handle, &protocol_version))
  252. return 0;
  253. if (protocol_version == 1)
  254. {
  255. grub_efi_tpm_protocol_t *tpm;
  256. tpm = grub_efi_open_protocol (tpm_handle, &tpm_guid,
  257. GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL);
  258. if (!tpm)
  259. {
  260. grub_dprintf ("tpm", "Cannot open TPM protocol\n");
  261. return 0;
  262. }
  263. return grub_tpm1_present (tpm);
  264. }
  265. else
  266. {
  267. grub_efi_tpm2_protocol_t *tpm;
  268. tpm = grub_efi_open_protocol (tpm_handle, &tpm2_guid,
  269. GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL);
  270. if (!tpm)
  271. {
  272. grub_dprintf ("tpm", "Cannot open TPM protocol\n");
  273. return 0;
  274. }
  275. return grub_tpm2_present (tpm);
  276. }
  277. }