pcr.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #include <inttypes.h>
  3. #include <stdbool.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include "log.h"
  8. #include "pcr.h"
  9. #include "tpm2.h"
  10. #include "tpm2_systemdeps.h"
  11. #include "tpm2_tool.h"
  12. #include "tpm2_alg_util.h"
  13. #include "tpm2_util.h"
  14. #define MAX(a,b) ((a>b)?a:b)
  15. static inline void set_pcr_select_size(TPMS_PCR_SELECTION *pcr_selection,
  16. UINT8 size) {
  17. pcr_selection->sizeofSelect = size;
  18. }
  19. bool pcr_get_id(const char *arg, UINT32 *pcr_id) {
  20. if (arg == NULL || pcr_id == NULL) {
  21. LOG_ERR("arg or pcr_id is NULL");
  22. return false;
  23. }
  24. return tpm2_util_handle_from_optarg(arg, pcr_id, TPM2_HANDLE_FLAGS_PCR);
  25. }
  26. static bool pcr_parse_list(const char *str, size_t len,
  27. TPMS_PCR_SELECTION *pcr_select) {
  28. char buf[4];
  29. const char *current_string;
  30. int current_length;
  31. UINT32 pcr;
  32. if (str == NULL || len == 0 || strlen(str) == 0) {
  33. return false;
  34. }
  35. pcr_select->sizeofSelect = 3;
  36. pcr_select->pcrSelect[0] = 0;
  37. pcr_select->pcrSelect[1] = 0;
  38. pcr_select->pcrSelect[2] = 0;
  39. if (!strncmp(str, "all", 3)) {
  40. pcr_select->pcrSelect[0] = 0xff;
  41. pcr_select->pcrSelect[1] = 0xff;
  42. pcr_select->pcrSelect[2] = 0xff;
  43. return true;
  44. }
  45. if (!strncmp(str, "none", 4)) {
  46. pcr_select->pcrSelect[0] = 0x00;
  47. pcr_select->pcrSelect[1] = 0x00;
  48. pcr_select->pcrSelect[2] = 0x00;
  49. return true;
  50. }
  51. do {
  52. current_string = str;
  53. str = memchr(current_string, ',', len);
  54. if (str) {
  55. current_length = str - current_string;
  56. str++;
  57. len -= current_length + 1;
  58. } else {
  59. current_length = len;
  60. len = 0;
  61. }
  62. if ((size_t) current_length > sizeof(buf) - 1) {
  63. return false;
  64. }
  65. snprintf(buf, current_length + 1, "%s", current_string);
  66. if (!pcr_get_id(buf, &pcr)) {
  67. return false;
  68. }
  69. pcr_select->pcrSelect[pcr / 8] |= (1 << (pcr % 8));
  70. } while (str);
  71. return true;
  72. }
  73. static bool pcr_parse_selection(const char *str, size_t len,
  74. TPMS_PCR_SELECTION *pcr_select) {
  75. const char *left_string;
  76. char buf[9];
  77. if (str == NULL || len == 0 || strlen(str) == 0)
  78. return false;
  79. left_string = memchr(str, ':', len);
  80. if (left_string == NULL) {
  81. return false;
  82. }
  83. if ((size_t) (left_string - str) > sizeof(buf) - 1) {
  84. return false;
  85. }
  86. snprintf(buf, left_string - str + 1, "%s", str);
  87. pcr_select->hash = tpm2_alg_util_from_optarg(buf, tpm2_alg_util_flags_hash);
  88. if (pcr_select->hash == TPM2_ALG_ERROR) {
  89. return false;
  90. }
  91. left_string++;
  92. if ((size_t) (left_string - str) >= len) {
  93. return false;
  94. }
  95. if (!pcr_parse_list(left_string, str + len - left_string, pcr_select)) {
  96. return false;
  97. }
  98. return true;
  99. }
  100. static void shrink_pcr_selection(TPML_PCR_SELECTION *s) {
  101. UINT32 i, j;
  102. //seek for the first empty item
  103. for (i = 0; i < s->count; i++)
  104. if (!s->pcrSelections[i].hash)
  105. break;
  106. j = i + 1;
  107. for (; i < s->count; i++) {
  108. if (!s->pcrSelections[i].hash) {
  109. for (; j < s->count; j++)
  110. if (s->pcrSelections[j].hash)
  111. break;
  112. if (j >= s->count)
  113. break;
  114. memcpy(&s->pcrSelections[i], &s->pcrSelections[j],
  115. sizeof(s->pcrSelections[i]));
  116. s->pcrSelections[j].hash = 0;
  117. j++;
  118. }
  119. }
  120. s->count = i;
  121. }
  122. static void pcr_update_pcr_selections(TPML_PCR_SELECTION *s1,
  123. TPML_PCR_SELECTION *s2) {
  124. UINT32 i1, i2, j;
  125. for (i2 = 0; i2 < s2->count; i2++) {
  126. for (i1 = 0; i1 < s1->count; i1++) {
  127. if (s2->pcrSelections[i2].hash != s1->pcrSelections[i1].hash)
  128. continue;
  129. for (j = 0; j < s1->pcrSelections[i1].sizeofSelect; j++)
  130. s1->pcrSelections[i1].pcrSelect[j] &=
  131. ~s2->pcrSelections[i2].pcrSelect[j];
  132. }
  133. }
  134. }
  135. static bool pcr_unset_pcr_sections(TPML_PCR_SELECTION *s) {
  136. UINT32 i, j;
  137. for (i = 0; i < s->count; i++) {
  138. for (j = 0; j < s->pcrSelections[i].sizeofSelect; j++) {
  139. if (s->pcrSelections[i].pcrSelect[j]) {
  140. return false;
  141. }
  142. }
  143. }
  144. return true;
  145. }
  146. bool pcr_print_pcr_struct_le(TPML_PCR_SELECTION *pcr_select, tpm2_pcrs *pcrs) {
  147. UINT32 vi = 0, di = 0, i;
  148. bool result = true;
  149. tpm2_tool_output("pcrs:\n");
  150. /* Loop through all PCR/hash banks */
  151. for (i = 0; i < le32toh(pcr_select->count); i++) {
  152. const char *alg_name = tpm2_alg_util_algtostr(
  153. le16toh(pcr_select->pcrSelections[i].hash), tpm2_alg_util_flags_hash);
  154. tpm2_tool_output(" %s:\n", alg_name);
  155. /* Loop through all PCRs in this bank */
  156. unsigned int pcr_id;
  157. for (pcr_id = 0; pcr_id < pcr_select->pcrSelections[i].sizeofSelect * 8u;
  158. pcr_id++) {
  159. if (!tpm2_util_is_pcr_select_bit_set(&pcr_select->pcrSelections[i],
  160. pcr_id)) {
  161. continue; // skip non-selected banks
  162. }
  163. if (vi >= le64toh(pcrs->count) || di >= le32toh(pcrs->pcr_values[vi].count)) {
  164. LOG_ERR("Something wrong, trying to print but nothing more");
  165. return false;
  166. }
  167. /* Print out PCR ID */
  168. tpm2_tool_output(" %-2d: 0x", pcr_id);
  169. /* Print out current PCR digest value */
  170. TPM2B_DIGEST *b = &pcrs->pcr_values[vi].digests[di];
  171. int k;
  172. for (k = 0; k < le16toh(b->size); k++) {
  173. tpm2_tool_output("%02X", b->buffer[k]);
  174. }
  175. tpm2_tool_output("\n");
  176. if (++di < le32toh(pcrs->pcr_values[vi].count)) {
  177. continue;
  178. }
  179. di = 0;
  180. if (++vi < le64toh(pcrs->count)) {
  181. continue;
  182. }
  183. }
  184. }
  185. return result;
  186. }
  187. bool pcr_print_pcr_struct(TPML_PCR_SELECTION *pcr_select, tpm2_pcrs *pcrs) {
  188. UINT32 vi = 0, di = 0, i;
  189. bool result = true;
  190. tpm2_tool_output("pcrs:\n");
  191. // Loop through all PCR/hash banks
  192. for (i = 0; i < pcr_select->count; i++) {
  193. const char *alg_name = tpm2_alg_util_algtostr(
  194. pcr_select->pcrSelections[i].hash, tpm2_alg_util_flags_hash);
  195. tpm2_tool_output(" %s:\n", alg_name);
  196. // Loop through all PCRs in this bank
  197. unsigned int pcr_id;
  198. for (pcr_id = 0; pcr_id < pcr_select->pcrSelections[i].sizeofSelect * 8u;
  199. pcr_id++) {
  200. if (!tpm2_util_is_pcr_select_bit_set(&pcr_select->pcrSelections[i],
  201. pcr_id)) {
  202. // skip non-selected banks
  203. continue;
  204. }
  205. if (vi >= pcrs->count || di >= pcrs->pcr_values[vi].count) {
  206. LOG_ERR("Something wrong, trying to print but nothing more");
  207. return false;
  208. }
  209. // Print out PCR ID
  210. tpm2_tool_output(" %-2d: 0x", pcr_id);
  211. // Print out current PCR digest value
  212. TPM2B_DIGEST *b = &pcrs->pcr_values[vi].digests[di];
  213. int k;
  214. for (k = 0; k < b->size; k++) {
  215. tpm2_tool_output("%02X", b->buffer[k]);
  216. }
  217. tpm2_tool_output("\n");
  218. if (++di < pcrs->pcr_values[vi].count) {
  219. continue;
  220. }
  221. di = 0;
  222. if (++vi < pcrs->count) {
  223. continue;
  224. }
  225. }
  226. }
  227. return result;
  228. }
  229. bool pcr_print_pcr_selections(TPML_PCR_SELECTION *pcr_selections) {
  230. tpm2_tool_output("selected-pcrs:\n");
  231. /* Iterate throught the pcr banks */
  232. UINT32 i;
  233. for (i = 0; i < pcr_selections->count; i++) {
  234. /* Print hash alg of the current bank */
  235. const char *halgstr = tpm2_alg_util_algtostr(
  236. pcr_selections->pcrSelections[i].hash,
  237. tpm2_alg_util_flags_hash);
  238. if (halgstr != NULL) {
  239. tpm2_tool_output(" - %s: [", halgstr);
  240. } else {
  241. LOG_ERR("Unsupported hash algorithm 0x%08x",
  242. pcr_selections->pcrSelections[i].hash);
  243. return false;
  244. }
  245. /* Iterate through the PCRs of the bank */
  246. bool first = true;
  247. unsigned j;
  248. for (j = 0; j < pcr_selections->pcrSelections[i].sizeofSelect * 8;
  249. j++) {
  250. if ((pcr_selections->pcrSelections[i].pcrSelect[j / 8]
  251. & 1 << (j % 8)) != 0) {
  252. if (first) {
  253. tpm2_tool_output(" %i", j);
  254. first = false;
  255. } else {
  256. tpm2_tool_output(", %i", j);
  257. }
  258. }
  259. }
  260. tpm2_tool_output(" ]\n");
  261. }
  262. return true;
  263. }
  264. bool pcr_parse_selections(const char *arg, TPML_PCR_SELECTION *pcr_select) {
  265. const char *left_string = arg;
  266. const char *current_string = arg;
  267. int current_length = 0;
  268. if (arg == NULL || pcr_select == NULL) {
  269. return false;
  270. }
  271. pcr_select->count = 0;
  272. do {
  273. current_string = left_string;
  274. left_string = strchr(current_string, '+');
  275. if (left_string) {
  276. current_length = left_string - current_string;
  277. left_string++;
  278. } else
  279. current_length = strlen(current_string);
  280. if (!pcr_parse_selection(current_string, current_length,
  281. &pcr_select->pcrSelections[pcr_select->count]))
  282. return false;
  283. pcr_select->count++;
  284. } while (left_string);
  285. if (pcr_select->count == 0) {
  286. return false;
  287. }
  288. return true;
  289. }
  290. tool_rc pcr_get_banks(ESYS_CONTEXT *esys_context,
  291. TPMS_CAPABILITY_DATA *capability_data, tpm2_algorithm *algs) {
  292. TPMI_YES_NO more_data;
  293. TPMS_CAPABILITY_DATA *capdata_ret;
  294. tool_rc rc = tpm2_get_capability(esys_context, ESYS_TR_NONE, ESYS_TR_NONE,
  295. ESYS_TR_NONE, TPM2_CAP_PCRS, no_argument, required_argument,
  296. &more_data, &capdata_ret);
  297. if (rc != tool_rc_success) {
  298. return rc;
  299. }
  300. *capability_data = *capdata_ret;
  301. unsigned i;
  302. // If the TPM support more bank algorithm that we currently
  303. // able to manage, throw an error
  304. if (capability_data->data.assignedPCR.count > ARRAY_LEN(algs->alg)) {
  305. LOG_ERR("Current implementation does not support more than %zu banks, "
  306. "got %" PRIu32 " banks supported by TPM",
  307. sizeof(algs->alg), capability_data->data.assignedPCR.count);
  308. free(capdata_ret);
  309. return tool_rc_general_error;
  310. }
  311. for (i = 0; i < capability_data->data.assignedPCR.count; i++) {
  312. algs->alg[i] = capability_data->data.assignedPCR.pcrSelections[i].hash;
  313. }
  314. algs->count = capability_data->data.assignedPCR.count;
  315. free(capdata_ret);
  316. return tool_rc_success;
  317. }
  318. bool pcr_init_pcr_selection(TPMS_CAPABILITY_DATA *cap_data,
  319. TPML_PCR_SELECTION *pcr_sel, TPMI_ALG_HASH alg_id) {
  320. UINT32 i, j;
  321. pcr_sel->count = 0;
  322. for (i = 0; i < cap_data->data.assignedPCR.count; i++) {
  323. if (alg_id
  324. && (cap_data->data.assignedPCR.pcrSelections[i].hash != alg_id))
  325. continue;
  326. pcr_sel->pcrSelections[pcr_sel->count].hash =
  327. cap_data->data.assignedPCR.pcrSelections[i].hash;
  328. set_pcr_select_size(&pcr_sel->pcrSelections[pcr_sel->count],
  329. cap_data->data.assignedPCR.pcrSelections[i].sizeofSelect);
  330. for (j = 0; j < pcr_sel->pcrSelections[pcr_sel->count].sizeofSelect;
  331. j++)
  332. pcr_sel->pcrSelections[pcr_sel->count].pcrSelect[j] =
  333. cap_data->data.assignedPCR.pcrSelections[i].pcrSelect[j];
  334. pcr_sel->count++;
  335. }
  336. if (pcr_sel->count == 0)
  337. return false;
  338. return true;
  339. }
  340. bool pcr_check_pcr_selection(TPMS_CAPABILITY_DATA *cap_data,
  341. TPML_PCR_SELECTION *pcr_sel) {
  342. UINT32 i, j, k;
  343. for (i = 0; i < pcr_sel->count; i++) {
  344. for (j = 0; j < cap_data->data.assignedPCR.count; j++) {
  345. if (pcr_sel->pcrSelections[i].hash
  346. == cap_data->data.assignedPCR.pcrSelections[j].hash) {
  347. for (k = 0; k < pcr_sel->pcrSelections[i].sizeofSelect; k++)
  348. pcr_sel->pcrSelections[i].pcrSelect[k] &=
  349. cap_data->data.assignedPCR.pcrSelections[j].pcrSelect[k];
  350. break;
  351. }
  352. }
  353. if (j >= cap_data->data.assignedPCR.count) {
  354. const char *alg_name = tpm2_alg_util_algtostr(
  355. pcr_sel->pcrSelections[i].hash, tpm2_alg_util_flags_hash);
  356. LOG_WARN("Ignore unsupported bank/algorithm: %s(0x%04x)", alg_name,
  357. pcr_sel->pcrSelections[i].hash);
  358. pcr_sel->pcrSelections[i].hash = 0; //mark it as to be removed
  359. }
  360. }
  361. shrink_pcr_selection(pcr_sel);
  362. if (pcr_sel->count == 0)
  363. return false;
  364. return true;
  365. }
  366. tool_rc pcr_read_pcr_values(ESYS_CONTEXT *esys_context,
  367. TPML_PCR_SELECTION *pcr_select, tpm2_pcrs *pcrs) {
  368. TPML_PCR_SELECTION pcr_selection_tmp;
  369. TPML_PCR_SELECTION *pcr_selection_out;
  370. UINT32 pcr_update_counter;
  371. //1. prepare pcrSelectionIn with g_pcrSelections
  372. memcpy(&pcr_selection_tmp, pcr_select, sizeof(pcr_selection_tmp));
  373. //2. call pcr_read
  374. pcrs->count = 0;
  375. do {
  376. TPML_DIGEST *v;
  377. tool_rc rc = tpm2_pcr_read(esys_context, ESYS_TR_NONE, ESYS_TR_NONE,
  378. ESYS_TR_NONE, &pcr_selection_tmp, &pcr_update_counter,
  379. &pcr_selection_out, &v);
  380. if (rc != tool_rc_success) {
  381. return rc;
  382. }
  383. pcrs->pcr_values[pcrs->count] = *v;
  384. free(v);
  385. //3. unmask pcrSelectionOut bits from pcrSelectionIn
  386. pcr_update_pcr_selections(&pcr_selection_tmp, pcr_selection_out);
  387. free(pcr_selection_out);
  388. //4. goto step 2 if pcrSelctionIn still has bits set
  389. } while (++pcrs->count < ARRAY_LEN(pcrs->pcr_values)
  390. && !pcr_unset_pcr_sections(&pcr_selection_tmp));
  391. if (pcrs->count >= ARRAY_LEN(pcrs->pcr_values)
  392. && !pcr_unset_pcr_sections(&pcr_selection_tmp)) {
  393. LOG_ERR("too much pcrs to get! try to split into multiple calls...");
  394. return tool_rc_general_error;
  395. }
  396. return tool_rc_success;
  397. }