tpm_util.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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 "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. 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. fd_set readfds;
  102. int n;
  103. struct timeval tv = {
  104. .tv_sec = 1,
  105. .tv_usec = 0,
  106. };
  107. n = write(fd, request, requestlen);
  108. if (n < 0) {
  109. return -errno;
  110. }
  111. if (n != requestlen) {
  112. return -EFAULT;
  113. }
  114. FD_ZERO(&readfds);
  115. FD_SET(fd, &readfds);
  116. /* wait for a second */
  117. n = select(fd + 1, &readfds, NULL, NULL, &tv);
  118. if (n != 1) {
  119. return -errno;
  120. }
  121. n = read(fd, response, responselen);
  122. if (n < sizeof(struct tpm_resp_hdr)) {
  123. return -EFAULT;
  124. }
  125. /* check the header */
  126. if (tpm_cmd_get_size(response) != n) {
  127. return -EMSGSIZE;
  128. }
  129. return 0;
  130. }
  131. /*
  132. * A basic test of a TPM device. We expect a well formatted response header
  133. * (error response is fine).
  134. */
  135. static int tpm_util_test(int fd,
  136. const void *request,
  137. size_t requestlen,
  138. uint16_t *return_tag)
  139. {
  140. char buf[1024];
  141. ssize_t ret;
  142. ret = tpm_util_request(fd, request, requestlen,
  143. buf, sizeof(buf));
  144. if (ret < 0) {
  145. return ret;
  146. }
  147. *return_tag = tpm_cmd_get_tag(buf);
  148. return 0;
  149. }
  150. /*
  151. * Probe for the TPM device in the back
  152. * Returns 0 on success with the version of the probed TPM set, 1 on failure.
  153. */
  154. int tpm_util_test_tpmdev(int tpm_fd, TPMVersion *tpm_version)
  155. {
  156. /*
  157. * Sending a TPM1.2 command to a TPM2 should return a TPM1.2
  158. * header (tag = 0xc4) and error code (TPM_BADTAG = 0x1e)
  159. *
  160. * Sending a TPM2 command to a TPM 2 will give a TPM 2 tag in the
  161. * header.
  162. * Sending a TPM2 command to a TPM 1.2 will give a TPM 1.2 tag
  163. * in the header and an error code.
  164. */
  165. const struct tpm_req_hdr test_req = {
  166. .tag = cpu_to_be16(TPM_TAG_RQU_COMMAND),
  167. .len = cpu_to_be32(sizeof(test_req)),
  168. .ordinal = cpu_to_be32(TPM_ORD_GetTicks),
  169. };
  170. const struct tpm_req_hdr test_req_tpm2 = {
  171. .tag = cpu_to_be16(TPM2_ST_NO_SESSIONS),
  172. .len = cpu_to_be32(sizeof(test_req_tpm2)),
  173. .ordinal = cpu_to_be32(TPM2_CC_ReadClock),
  174. };
  175. uint16_t return_tag;
  176. int ret;
  177. /* Send TPM 2 command */
  178. ret = tpm_util_test(tpm_fd, &test_req_tpm2,
  179. sizeof(test_req_tpm2), &return_tag);
  180. /* TPM 2 would respond with a tag of TPM2_ST_NO_SESSIONS */
  181. if (!ret && return_tag == TPM2_ST_NO_SESSIONS) {
  182. *tpm_version = TPM_VERSION_2_0;
  183. return 0;
  184. }
  185. /* Send TPM 1.2 command */
  186. ret = tpm_util_test(tpm_fd, &test_req,
  187. sizeof(test_req), &return_tag);
  188. if (!ret && return_tag == TPM_TAG_RSP_COMMAND) {
  189. *tpm_version = TPM_VERSION_1_2;
  190. /* this is a TPM 1.2 */
  191. return 0;
  192. }
  193. *tpm_version = TPM_VERSION_UNSPEC;
  194. return 1;
  195. }
  196. int tpm_util_get_buffer_size(int tpm_fd, TPMVersion tpm_version,
  197. size_t *buffersize)
  198. {
  199. int ret;
  200. switch (tpm_version) {
  201. case TPM_VERSION_1_2: {
  202. const struct tpm_req_get_buffer_size {
  203. struct tpm_req_hdr hdr;
  204. uint32_t capability;
  205. uint32_t len;
  206. uint32_t subcap;
  207. } QEMU_PACKED tpm_get_buffer_size = {
  208. .hdr = {
  209. .tag = cpu_to_be16(TPM_TAG_RQU_COMMAND),
  210. .len = cpu_to_be32(sizeof(tpm_get_buffer_size)),
  211. .ordinal = cpu_to_be32(TPM_ORD_GetCapability),
  212. },
  213. .capability = cpu_to_be32(TPM_CAP_PROPERTY),
  214. .len = cpu_to_be32(sizeof(uint32_t)),
  215. .subcap = cpu_to_be32(TPM_CAP_PROP_INPUT_BUFFER),
  216. };
  217. struct tpm_resp_get_buffer_size {
  218. struct tpm_resp_hdr hdr;
  219. uint32_t len;
  220. uint32_t buffersize;
  221. } QEMU_PACKED tpm_resp;
  222. ret = tpm_util_request(tpm_fd, &tpm_get_buffer_size,
  223. sizeof(tpm_get_buffer_size),
  224. &tpm_resp, sizeof(tpm_resp));
  225. if (ret < 0) {
  226. return ret;
  227. }
  228. if (be32_to_cpu(tpm_resp.hdr.len) != sizeof(tpm_resp) ||
  229. be32_to_cpu(tpm_resp.len) != sizeof(uint32_t)) {
  230. trace_tpm_util_get_buffer_size_hdr_len(
  231. be32_to_cpu(tpm_resp.hdr.len),
  232. sizeof(tpm_resp));
  233. trace_tpm_util_get_buffer_size_len(be32_to_cpu(tpm_resp.len),
  234. sizeof(uint32_t));
  235. error_report("tpm_util: Got unexpected response to "
  236. "TPM_GetCapability; errcode: 0x%x",
  237. be32_to_cpu(tpm_resp.hdr.errcode));
  238. return -EFAULT;
  239. }
  240. *buffersize = be32_to_cpu(tpm_resp.buffersize);
  241. break;
  242. }
  243. case TPM_VERSION_2_0: {
  244. const struct tpm2_req_get_buffer_size {
  245. struct tpm_req_hdr hdr;
  246. uint32_t capability;
  247. uint32_t property;
  248. uint32_t count;
  249. } QEMU_PACKED tpm2_get_buffer_size = {
  250. .hdr = {
  251. .tag = cpu_to_be16(TPM2_ST_NO_SESSIONS),
  252. .len = cpu_to_be32(sizeof(tpm2_get_buffer_size)),
  253. .ordinal = cpu_to_be32(TPM2_CC_GetCapability),
  254. },
  255. .capability = cpu_to_be32(TPM2_CAP_TPM_PROPERTIES),
  256. .property = cpu_to_be32(TPM2_PT_MAX_COMMAND_SIZE),
  257. .count = cpu_to_be32(2), /* also get TPM2_PT_MAX_RESPONSE_SIZE */
  258. };
  259. struct tpm2_resp_get_buffer_size {
  260. struct tpm_resp_hdr hdr;
  261. uint8_t more;
  262. uint32_t capability;
  263. uint32_t count;
  264. uint32_t property1;
  265. uint32_t value1;
  266. uint32_t property2;
  267. uint32_t value2;
  268. } QEMU_PACKED tpm2_resp;
  269. ret = tpm_util_request(tpm_fd, &tpm2_get_buffer_size,
  270. sizeof(tpm2_get_buffer_size),
  271. &tpm2_resp, sizeof(tpm2_resp));
  272. if (ret < 0) {
  273. return ret;
  274. }
  275. if (be32_to_cpu(tpm2_resp.hdr.len) != sizeof(tpm2_resp) ||
  276. be32_to_cpu(tpm2_resp.count) != 2) {
  277. trace_tpm_util_get_buffer_size_hdr_len2(
  278. be32_to_cpu(tpm2_resp.hdr.len),
  279. sizeof(tpm2_resp));
  280. trace_tpm_util_get_buffer_size_len2(
  281. be32_to_cpu(tpm2_resp.count), 2);
  282. error_report("tpm_util: Got unexpected response to "
  283. "TPM2_GetCapability; errcode: 0x%x",
  284. be32_to_cpu(tpm2_resp.hdr.errcode));
  285. return -EFAULT;
  286. }
  287. *buffersize = MAX(be32_to_cpu(tpm2_resp.value1),
  288. be32_to_cpu(tpm2_resp.value2));
  289. break;
  290. }
  291. case TPM_VERSION_UNSPEC:
  292. return -EFAULT;
  293. }
  294. trace_tpm_util_get_buffer_size(*buffersize);
  295. return 0;
  296. }
  297. void tpm_sized_buffer_reset(TPMSizedBuffer *tsb)
  298. {
  299. g_free(tsb->buffer);
  300. tsb->buffer = NULL;
  301. tsb->size = 0;
  302. }
  303. void tpm_util_show_buffer(const unsigned char *buffer,
  304. size_t buffer_size, const char *string)
  305. {
  306. size_t len, i;
  307. char *line_buffer, *p;
  308. if (!trace_event_get_state_backends(TRACE_TPM_UTIL_SHOW_BUFFER)) {
  309. return;
  310. }
  311. len = MIN(tpm_cmd_get_size(buffer), buffer_size);
  312. /*
  313. * allocate enough room for 3 chars per buffer entry plus a
  314. * newline after every 16 chars and a final null terminator.
  315. */
  316. line_buffer = g_malloc(len * 3 + (len / 16) + 1);
  317. for (i = 0, p = line_buffer; i < len; i++) {
  318. if (i && !(i % 16)) {
  319. p += sprintf(p, "\n");
  320. }
  321. p += sprintf(p, "%.2X ", buffer[i]);
  322. }
  323. trace_tpm_util_show_buffer(string, len, line_buffer);
  324. g_free(line_buffer);
  325. }