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