tpm_util.c 11 KB

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