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