2
0

tpm_util.c 11 KB

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