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