2 * TPM utility functions
4 * Copyright (c) 2010 - 2015 IBM Corporation
6 * Stefan Berger <stefanb@us.ibm.com>
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.
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.
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/>
22 #include "qemu/osdep.h"
23 #include "qemu/error-report.h"
24 #include "qapi/error.h"
25 #include "qapi/visitor.h"
28 #include "exec/memory.h"
29 #include "sysemu/tpm_backend.h"
34 #define DPRINTF(fmt, ...) do { \
36 fprintf(stderr, "tpm-util:"fmt"\n", ## __VA_ARGS__); \
40 /* tpm backend property */
42 static void get_tpm(Object
*obj
, Visitor
*v
, const char *name
, void *opaque
,
45 DeviceState
*dev
= DEVICE(obj
);
46 TPMBackend
**be
= qdev_get_prop_ptr(dev
, opaque
);
49 p
= g_strdup(*be
? (*be
)->id
: "");
50 visit_type_str(v
, name
, &p
, errp
);
54 static void set_tpm(Object
*obj
, Visitor
*v
, const char *name
, void *opaque
,
57 DeviceState
*dev
= DEVICE(obj
);
58 Error
*local_err
= NULL
;
59 Property
*prop
= opaque
;
60 TPMBackend
*s
, **be
= qdev_get_prop_ptr(dev
, prop
);
64 qdev_prop_set_after_realize(dev
, name
, errp
);
68 visit_type_str(v
, name
, &str
, &local_err
);
70 error_propagate(errp
, local_err
);
74 s
= qemu_find_tpm_be(str
);
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 */
84 static void release_tpm(Object
*obj
, const char *name
, void *opaque
)
86 DeviceState
*dev
= DEVICE(obj
);
87 Property
*prop
= opaque
;
88 TPMBackend
**be
= qdev_get_prop_ptr(dev
, prop
);
91 tpm_backend_reset(*be
);
95 const PropertyInfo qdev_prop_tpm
= {
97 .description
= "ID of a tpm to use as a backend",
100 .release
= release_tpm
,
104 * Write an error message in the given output buffer.
106 void tpm_util_write_fatal_error_response(uint8_t *out
, uint32_t out_len
)
108 if (out_len
>= sizeof(struct tpm_resp_hdr
)) {
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
);
115 bool tpm_util_is_selftest(const uint8_t *in
, uint32_t in_len
)
117 if (in_len
>= sizeof(struct tpm_req_hdr
)) {
118 return tpm_cmd_get_ordinal(in
) == TPM_ORD_ContinueSelfTest
;
125 * Send request to a TPM device. We expect a response within one second.
127 static int tpm_util_request(int fd
,
135 struct timeval tv
= {
140 n
= write(fd
, request
, requestlen
);
144 if (n
!= requestlen
) {
149 FD_SET(fd
, &readfds
);
151 /* wait for a second */
152 n
= select(fd
+ 1, &readfds
, NULL
, NULL
, &tv
);
157 n
= read(fd
, response
, responselen
);
158 if (n
< sizeof(struct tpm_resp_hdr
)) {
162 /* check the header */
163 if (tpm_cmd_get_size(response
) != n
) {
171 * A basic test of a TPM device. We expect a well formatted response header
172 * (error response is fine).
174 static int tpm_util_test(int fd
,
177 uint16_t *return_tag
)
182 ret
= tpm_util_request(fd
, request
, requestlen
,
188 *return_tag
= tpm_cmd_get_tag(buf
);
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.
197 int tpm_util_test_tpmdev(int tpm_fd
, TPMVersion
*tpm_version
)
200 * Sending a TPM1.2 command to a TPM2 should return a TPM1.2
201 * header (tag = 0xc4) and error code (TPM_BADTAG = 0x1e)
203 * Sending a TPM2 command to a TPM 2 will give a TPM 2 tag in the
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.
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
),
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
),
222 /* Send TPM 2 command */
223 ret
= tpm_util_test(tpm_fd
, &test_req_tpm2
,
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
;
231 /* Send TPM 1.2 command */
232 ret
= tpm_util_test(tpm_fd
, &test_req
,
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 */
240 *tpm_version
= TPM_VERSION_UNSPEC
;
245 int tpm_util_get_buffer_size(int tpm_fd
, TPMVersion tpm_version
,
250 switch (tpm_version
) {
251 case TPM_VERSION_1_2
: {
252 const struct tpm_req_get_buffer_size
{
253 struct tpm_req_hdr hdr
;
257 } QEMU_PACKED tpm_get_buffer_size
= {
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
),
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
),
267 struct tpm_resp_get_buffer_size
{
268 struct tpm_resp_hdr hdr
;
271 } QEMU_PACKED tpm_resp
;
273 ret
= tpm_util_request(tpm_fd
, &tpm_get_buffer_size
,
274 sizeof(tpm_get_buffer_size
),
275 &tpm_resp
, sizeof(tpm_resp
));
280 if (be32_to_cpu(tpm_resp
.hdr
.len
) != sizeof(tpm_resp
) ||
281 be32_to_cpu(tpm_resp
.len
) != sizeof(uint32_t)) {
282 DPRINTF("tpm_resp->hdr.len = %u, expected = %zu\n",
283 be32_to_cpu(tpm_resp
.hdr
.len
), sizeof(tpm_resp
));
284 DPRINTF("tpm_resp->len = %u, expected = %zu\n",
285 be32_to_cpu(tpm_resp
.len
), sizeof(uint32_t));
286 error_report("tpm_util: Got unexpected response to "
287 "TPM_GetCapability; errcode: 0x%x",
288 be32_to_cpu(tpm_resp
.hdr
.errcode
));
291 *buffersize
= be32_to_cpu(tpm_resp
.buffersize
);
294 case TPM_VERSION_2_0
: {
295 const struct tpm2_req_get_buffer_size
{
296 struct tpm_req_hdr hdr
;
300 } QEMU_PACKED tpm2_get_buffer_size
= {
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
),
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 */
310 struct tpm2_resp_get_buffer_size
{
311 struct tpm_resp_hdr hdr
;
319 } QEMU_PACKED tpm2_resp
;
321 ret
= tpm_util_request(tpm_fd
, &tpm2_get_buffer_size
,
322 sizeof(tpm2_get_buffer_size
),
323 &tpm2_resp
, sizeof(tpm2_resp
));
328 if (be32_to_cpu(tpm2_resp
.hdr
.len
) != sizeof(tpm2_resp
) ||
329 be32_to_cpu(tpm2_resp
.count
) != 2) {
330 DPRINTF("tpm2_resp->hdr.len = %u, expected = %zu\n",
331 be32_to_cpu(tpm2_resp
.hdr
.len
), sizeof(tpm2_resp
));
332 DPRINTF("tpm2_resp->len = %u, expected = %u\n",
333 be32_to_cpu(tpm2_resp
.count
), 2);
334 error_report("tpm_util: Got unexpected response to "
335 "TPM2_GetCapability; errcode: 0x%x",
336 be32_to_cpu(tpm2_resp
.hdr
.errcode
));
339 *buffersize
= MAX(be32_to_cpu(tpm2_resp
.value1
),
340 be32_to_cpu(tpm2_resp
.value2
));
343 case TPM_VERSION_UNSPEC
:
347 DPRINTF("buffersize of device: %zu\n", *buffersize
);
352 void tpm_sized_buffer_reset(TPMSizedBuffer
*tsb
)