blob: 73d77965fdda2be507657432b61141b2c170b54b [file] [log] [blame]
Stefan Berger56a3c242015-05-26 16:51:06 -04001/*
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
Peter Maydell04308912016-01-26 18:17:30 +000022#include "qemu/osdep.h"
Stefan Berger56a3c242015-05-26 16:51:06 -040023#include "tpm_util.h"
24#include "tpm_int.h"
25
26/*
Amarnath Valluri4a3d8092017-09-29 14:10:19 +030027 * Write an error message in the given output buffer.
28 */
29void tpm_util_write_fatal_error_response(uint8_t *out, uint32_t out_len)
30{
31 if (out_len >= sizeof(struct tpm_resp_hdr)) {
32 struct tpm_resp_hdr *resp = (struct tpm_resp_hdr *)out;
33
34 resp->tag = cpu_to_be16(TPM_TAG_RSP_COMMAND);
35 resp->len = cpu_to_be32(sizeof(struct tpm_resp_hdr));
36 resp->errcode = cpu_to_be32(TPM_FAIL);
37 }
38}
39
40bool tpm_util_is_selftest(const uint8_t *in, uint32_t in_len)
41{
42 struct tpm_req_hdr *hdr = (struct tpm_req_hdr *)in;
43
44 if (in_len >= sizeof(*hdr)) {
45 return (be32_to_cpu(hdr->ordinal) == TPM_ORD_ContinueSelfTest);
46 }
47
48 return false;
49}
50
51/*
Stefan Berger56a3c242015-05-26 16:51:06 -040052 * A basic test of a TPM device. We expect a well formatted response header
53 * (error response is fine) within one second.
54 */
55static int tpm_util_test(int fd,
56 unsigned char *request,
57 size_t requestlen,
58 uint16_t *return_tag)
59{
60 struct tpm_resp_hdr *resp;
61 fd_set readfds;
62 int n;
63 struct timeval tv = {
64 .tv_sec = 1,
65 .tv_usec = 0,
66 };
67 unsigned char buf[1024];
68
69 n = write(fd, request, requestlen);
70 if (n < 0) {
Stefan Berger98979cd2017-10-11 08:52:43 -040071 return -errno;
Stefan Berger56a3c242015-05-26 16:51:06 -040072 }
73 if (n != requestlen) {
Stefan Berger98979cd2017-10-11 08:52:43 -040074 return -EFAULT;
Stefan Berger56a3c242015-05-26 16:51:06 -040075 }
76
77 FD_ZERO(&readfds);
78 FD_SET(fd, &readfds);
79
80 /* wait for a second */
81 n = select(fd + 1, &readfds, NULL, NULL, &tv);
82 if (n != 1) {
Stefan Berger98979cd2017-10-11 08:52:43 -040083 return -errno;
Stefan Berger56a3c242015-05-26 16:51:06 -040084 }
85
86 n = read(fd, &buf, sizeof(buf));
87 if (n < sizeof(struct tpm_resp_hdr)) {
Stefan Berger98979cd2017-10-11 08:52:43 -040088 return -EFAULT;
Stefan Berger56a3c242015-05-26 16:51:06 -040089 }
90
91 resp = (struct tpm_resp_hdr *)buf;
92 /* check the header */
93 if (be32_to_cpu(resp->len) != n) {
Stefan Berger98979cd2017-10-11 08:52:43 -040094 return -EMSGSIZE;
Stefan Berger56a3c242015-05-26 16:51:06 -040095 }
96
97 *return_tag = be16_to_cpu(resp->tag);
98
99 return 0;
100}
101
102/*
103 * Probe for the TPM device in the back
104 * Returns 0 on success with the version of the probed TPM set, 1 on failure.
105 */
106int tpm_util_test_tpmdev(int tpm_fd, TPMVersion *tpm_version)
107{
108 /*
109 * Sending a TPM1.2 command to a TPM2 should return a TPM1.2
110 * header (tag = 0xc4) and error code (TPM_BADTAG = 0x1e)
111 *
112 * Sending a TPM2 command to a TPM 2 will give a TPM 2 tag in the
113 * header.
114 * Sending a TPM2 command to a TPM 1.2 will give a TPM 1.2 tag
115 * in the header and an error code.
116 */
117 const struct tpm_req_hdr test_req = {
118 .tag = cpu_to_be16(TPM_TAG_RQU_COMMAND),
119 .len = cpu_to_be32(sizeof(test_req)),
120 .ordinal = cpu_to_be32(TPM_ORD_GetTicks),
121 };
122
123 const struct tpm_req_hdr test_req_tpm2 = {
124 .tag = cpu_to_be16(TPM2_ST_NO_SESSIONS),
125 .len = cpu_to_be32(sizeof(test_req_tpm2)),
126 .ordinal = cpu_to_be32(TPM2_CC_ReadClock),
127 };
128 uint16_t return_tag;
129 int ret;
130
131 /* Send TPM 2 command */
132 ret = tpm_util_test(tpm_fd, (unsigned char *)&test_req_tpm2,
133 sizeof(test_req_tpm2), &return_tag);
134 /* TPM 2 would respond with a tag of TPM2_ST_NO_SESSIONS */
135 if (!ret && return_tag == TPM2_ST_NO_SESSIONS) {
136 *tpm_version = TPM_VERSION_2_0;
137 return 0;
138 }
139
140 /* Send TPM 1.2 command */
141 ret = tpm_util_test(tpm_fd, (unsigned char *)&test_req,
142 sizeof(test_req), &return_tag);
143 if (!ret && return_tag == TPM_TAG_RSP_COMMAND) {
144 *tpm_version = TPM_VERSION_1_2;
145 /* this is a TPM 1.2 */
146 return 0;
147 }
148
149 *tpm_version = TPM_VERSION_UNSPEC;
150
151 return 1;
152}