tpm_util.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*
  2. * TPM utility functions
  3. *
  4. * Copyright (c) 2010 - 2015 IBM Corporation
  5. * Authors:
  6. * Stefan Berger <stefanb@us.ibm.com>
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, see <http://www.gnu.org/licenses/>
  20. */
  21. #include "qemu/osdep.h"
  22. #include "qemu/error-report.h"
  23. #include "qemu/cutils.h"
  24. #include "qapi/error.h"
  25. #include "qapi/visitor.h"
  26. #include "tpm_int.h"
  27. #include "exec/memory.h"
  28. #include "hw/qdev-properties.h"
  29. #include "system/tpm_backend.h"
  30. #include "system/tpm_util.h"
  31. #include "trace.h"
  32. /* tpm backend property */
  33. static void get_tpm(Object *obj, Visitor *v, const char *name, void *opaque,
  34. Error **errp)
  35. {
  36. TPMBackend **be = object_field_prop_ptr(obj, opaque);
  37. char *p;
  38. p = g_strdup(*be ? (*be)->id : "");
  39. visit_type_str(v, name, &p, errp);
  40. g_free(p);
  41. }
  42. static void set_tpm(Object *obj, Visitor *v, const char *name, void *opaque,
  43. Error **errp)
  44. {
  45. const Property *prop = opaque;
  46. TPMBackend *s, **be = object_field_prop_ptr(obj, prop);
  47. char *str;
  48. if (!visit_type_str(v, name, &str, errp)) {
  49. return;
  50. }
  51. s = qemu_find_tpm_be(str);
  52. if (s == NULL) {
  53. error_setg(errp, "Property '%s.%s' can't find value '%s'",
  54. object_get_typename(obj), name, str);
  55. } else if (tpm_backend_init(s, TPM_IF(obj), errp) == 0) {
  56. *be = s; /* weak reference, avoid cyclic ref */
  57. }
  58. g_free(str);
  59. }
  60. static void release_tpm(Object *obj, const char *name, void *opaque)
  61. {
  62. const Property *prop = opaque;
  63. TPMBackend **be = object_field_prop_ptr(obj, prop);
  64. if (*be) {
  65. tpm_backend_reset(*be);
  66. }
  67. }
  68. const PropertyInfo qdev_prop_tpm = {
  69. .type = "str",
  70. .description = "ID of a tpm to use as a backend",
  71. .get = get_tpm,
  72. .set = set_tpm,
  73. .release = release_tpm,
  74. };
  75. /*
  76. * Write an error message in the given output buffer.
  77. */
  78. void tpm_util_write_fatal_error_response(uint8_t *out, uint32_t out_len)
  79. {
  80. if (out_len >= sizeof(struct tpm_resp_hdr)) {
  81. tpm_cmd_set_tag(out, TPM_TAG_RSP_COMMAND);
  82. tpm_cmd_set_size(out, sizeof(struct tpm_resp_hdr));
  83. tpm_cmd_set_error(out, TPM_FAIL);
  84. }
  85. }
  86. bool tpm_util_is_selftest(const uint8_t *in, uint32_t in_len)
  87. {
  88. if (in_len >= sizeof(struct tpm_req_hdr)) {
  89. return tpm_cmd_get_ordinal(in) == TPM_ORD_ContinueSelfTest;
  90. }
  91. return false;
  92. }
  93. /*
  94. * Send request to a TPM device. We expect a response within one second.
  95. */
  96. static int tpm_util_request(int fd,
  97. const void *request,
  98. size_t requestlen,
  99. void *response,
  100. size_t responselen)
  101. {
  102. GPollFD fds[1] = { {.fd = fd, .events = G_IO_IN } };
  103. int n;
  104. n = write(fd, request, requestlen);
  105. if (n < 0) {
  106. return -errno;
  107. }
  108. if (n != requestlen) {
  109. return -EFAULT;
  110. }
  111. /* wait for a second */
  112. n = RETRY_ON_EINTR(g_poll(fds, 1, 1000));
  113. if (n != 1) {
  114. return -errno;
  115. }
  116. n = read(fd, response, responselen);
  117. if (n < sizeof(struct tpm_resp_hdr)) {
  118. return -EFAULT;
  119. }
  120. /* check the header */
  121. if (tpm_cmd_get_size(response) != n) {
  122. return -EMSGSIZE;
  123. }
  124. return 0;
  125. }
  126. /*
  127. * A basic test of a TPM device. We expect a well formatted response header
  128. * (error response is fine).
  129. */
  130. static int tpm_util_test(int fd,
  131. const void *request,
  132. size_t requestlen,
  133. uint16_t *return_tag)
  134. {
  135. char buf[1024];
  136. ssize_t ret;
  137. ret = tpm_util_request(fd, request, requestlen,
  138. buf, sizeof(buf));
  139. if (ret < 0) {
  140. return ret;
  141. }
  142. *return_tag = tpm_cmd_get_tag(buf);
  143. return 0;
  144. }
  145. /*
  146. * Probe for the TPM device in the back
  147. * Returns 0 on success with the version of the probed TPM set, 1 on failure.
  148. */
  149. int tpm_util_test_tpmdev(int tpm_fd, TPMVersion *tpm_version)
  150. {
  151. /*
  152. * Sending a TPM1.2 command to a TPM2 should return a TPM1.2
  153. * header (tag = 0xc4) and error code (TPM_BADTAG = 0x1e)
  154. *
  155. * Sending a TPM2 command to a TPM 2 will give a TPM 2 tag in the
  156. * header.
  157. * Sending a TPM2 command to a TPM 1.2 will give a TPM 1.2 tag
  158. * in the header and an error code.
  159. */
  160. const struct tpm_req_hdr test_req = {
  161. .tag = cpu_to_be16(TPM_TAG_RQU_COMMAND),
  162. .len = cpu_to_be32(sizeof(test_req)),
  163. .ordinal = cpu_to_be32(TPM_ORD_GetTicks),
  164. };
  165. const struct tpm_req_hdr test_req_tpm2 = {
  166. .tag = cpu_to_be16(TPM2_ST_NO_SESSIONS),
  167. .len = cpu_to_be32(sizeof(test_req_tpm2)),
  168. .ordinal = cpu_to_be32(TPM2_CC_ReadClock),
  169. };
  170. uint16_t return_tag;
  171. int ret;
  172. /* Send TPM 2 command */
  173. ret = tpm_util_test(tpm_fd, &test_req_tpm2,
  174. sizeof(test_req_tpm2), &return_tag);
  175. /* TPM 2 would respond with a tag of TPM2_ST_NO_SESSIONS */
  176. if (!ret && return_tag == TPM2_ST_NO_SESSIONS) {
  177. *tpm_version = TPM_VERSION_2_0;
  178. return 0;
  179. }
  180. /* Send TPM 1.2 command */
  181. ret = tpm_util_test(tpm_fd, &test_req,
  182. sizeof(test_req), &return_tag);
  183. if (!ret && return_tag == TPM_TAG_RSP_COMMAND) {
  184. *tpm_version = TPM_VERSION_1_2;
  185. /* this is a TPM 1.2 */
  186. return 0;
  187. }
  188. *tpm_version = TPM_VERSION_UNSPEC;
  189. return 1;
  190. }
  191. int tpm_util_get_buffer_size(int tpm_fd, TPMVersion tpm_version,
  192. size_t *buffersize)
  193. {
  194. int ret;
  195. switch (tpm_version) {
  196. case TPM_VERSION_1_2: {
  197. const struct tpm_req_get_buffer_size {
  198. struct tpm_req_hdr hdr;
  199. uint32_t capability;
  200. uint32_t len;
  201. uint32_t subcap;
  202. } QEMU_PACKED tpm_get_buffer_size = {
  203. .hdr = {
  204. .tag = cpu_to_be16(TPM_TAG_RQU_COMMAND),
  205. .len = cpu_to_be32(sizeof(tpm_get_buffer_size)),
  206. .ordinal = cpu_to_be32(TPM_ORD_GetCapability),
  207. },
  208. .capability = cpu_to_be32(TPM_CAP_PROPERTY),
  209. .len = cpu_to_be32(sizeof(uint32_t)),
  210. .subcap = cpu_to_be32(TPM_CAP_PROP_INPUT_BUFFER),
  211. };
  212. struct tpm_resp_get_buffer_size {
  213. struct tpm_resp_hdr hdr;
  214. uint32_t len;
  215. uint32_t buffersize;
  216. } QEMU_PACKED tpm_resp;
  217. ret = tpm_util_request(tpm_fd, &tpm_get_buffer_size,
  218. sizeof(tpm_get_buffer_size),
  219. &tpm_resp, sizeof(tpm_resp));
  220. if (ret < 0) {
  221. return ret;
  222. }
  223. if (be32_to_cpu(tpm_resp.hdr.len) != sizeof(tpm_resp) ||
  224. be32_to_cpu(tpm_resp.len) != sizeof(uint32_t)) {
  225. trace_tpm_util_get_buffer_size_hdr_len(
  226. be32_to_cpu(tpm_resp.hdr.len),
  227. sizeof(tpm_resp));
  228. trace_tpm_util_get_buffer_size_len(be32_to_cpu(tpm_resp.len),
  229. sizeof(uint32_t));
  230. error_report("tpm_util: Got unexpected response to "
  231. "TPM_GetCapability; errcode: 0x%x",
  232. be32_to_cpu(tpm_resp.hdr.errcode));
  233. return -EFAULT;
  234. }
  235. *buffersize = be32_to_cpu(tpm_resp.buffersize);
  236. break;
  237. }
  238. case TPM_VERSION_2_0: {
  239. const struct tpm2_req_get_buffer_size {
  240. struct tpm_req_hdr hdr;
  241. uint32_t capability;
  242. uint32_t property;
  243. uint32_t count;
  244. } QEMU_PACKED tpm2_get_buffer_size = {
  245. .hdr = {
  246. .tag = cpu_to_be16(TPM2_ST_NO_SESSIONS),
  247. .len = cpu_to_be32(sizeof(tpm2_get_buffer_size)),
  248. .ordinal = cpu_to_be32(TPM2_CC_GetCapability),
  249. },
  250. .capability = cpu_to_be32(TPM2_CAP_TPM_PROPERTIES),
  251. .property = cpu_to_be32(TPM2_PT_MAX_COMMAND_SIZE),
  252. .count = cpu_to_be32(2), /* also get TPM2_PT_MAX_RESPONSE_SIZE */
  253. };
  254. struct tpm2_resp_get_buffer_size {
  255. struct tpm_resp_hdr hdr;
  256. uint8_t more;
  257. uint32_t capability;
  258. uint32_t count;
  259. uint32_t property1;
  260. uint32_t value1;
  261. uint32_t property2;
  262. uint32_t value2;
  263. } QEMU_PACKED tpm2_resp;
  264. ret = tpm_util_request(tpm_fd, &tpm2_get_buffer_size,
  265. sizeof(tpm2_get_buffer_size),
  266. &tpm2_resp, sizeof(tpm2_resp));
  267. if (ret < 0) {
  268. return ret;
  269. }
  270. if (be32_to_cpu(tpm2_resp.hdr.len) != sizeof(tpm2_resp) ||
  271. be32_to_cpu(tpm2_resp.count) != 2) {
  272. trace_tpm_util_get_buffer_size_hdr_len2(
  273. be32_to_cpu(tpm2_resp.hdr.len),
  274. sizeof(tpm2_resp));
  275. trace_tpm_util_get_buffer_size_len2(
  276. be32_to_cpu(tpm2_resp.count), 2);
  277. error_report("tpm_util: Got unexpected response to "
  278. "TPM2_GetCapability; errcode: 0x%x",
  279. be32_to_cpu(tpm2_resp.hdr.errcode));
  280. return -EFAULT;
  281. }
  282. *buffersize = MAX(be32_to_cpu(tpm2_resp.value1),
  283. be32_to_cpu(tpm2_resp.value2));
  284. break;
  285. }
  286. case TPM_VERSION_UNSPEC:
  287. return -EFAULT;
  288. }
  289. trace_tpm_util_get_buffer_size(*buffersize);
  290. return 0;
  291. }
  292. void tpm_sized_buffer_reset(TPMSizedBuffer *tsb)
  293. {
  294. g_free(tsb->buffer);
  295. tsb->buffer = NULL;
  296. tsb->size = 0;
  297. }
  298. void tpm_util_show_buffer(const unsigned char *buffer,
  299. size_t buffer_size, const char *string)
  300. {
  301. g_autoptr(GString) str = NULL;
  302. size_t len, i, l;
  303. if (!trace_event_get_state_backends(TRACE_TPM_UTIL_SHOW_BUFFER_CONTENT)) {
  304. return;
  305. }
  306. len = MIN(tpm_cmd_get_size(buffer), buffer_size);
  307. trace_tpm_util_show_buffer_header(string, len);
  308. for (i = 0; i < len; i += l) {
  309. if (str) {
  310. g_string_append_c(str, '\n');
  311. }
  312. l = MIN(len, 16);
  313. str = qemu_hexdump_line(str, buffer, l, 1, 0);
  314. }
  315. g_string_ascii_up(str);
  316. trace_tpm_util_show_buffer_content(str->str);
  317. }