]>
Commit | Line | Data |
---|---|---|
b21373d0 SB |
1 | /* |
2 | * QTest TPM utilities | |
3 | * | |
4 | * Copyright (c) 2018 IBM Corporation | |
5 | * Copyright (c) 2018 Red Hat, Inc. | |
6 | * | |
7 | * Authors: | |
8 | * Stefan Berger <stefanb@linux.vnet.ibm.com> | |
9 | * Marc-André Lureau <marcandre.lureau@redhat.com> | |
10 | * | |
11 | * This work is licensed under the terms of the GNU GPL, version 2 or later. | |
12 | * See the COPYING file in the top-level directory. | |
13 | */ | |
14 | ||
15 | #include "qemu/osdep.h" | |
16 | ||
17 | #include "hw/acpi/tpm.h" | |
18 | #include "libqtest.h" | |
19 | #include "tpm-util.h" | |
20 | ||
21 | void tpm_util_crb_transfer(QTestState *s, | |
22 | const unsigned char *req, size_t req_size, | |
23 | unsigned char *rsp, size_t rsp_size) | |
24 | { | |
25 | uint64_t caddr = qtest_readq(s, TPM_CRB_ADDR_BASE + A_CRB_CTRL_CMD_LADDR); | |
26 | uint64_t raddr = qtest_readq(s, TPM_CRB_ADDR_BASE + A_CRB_CTRL_RSP_ADDR); | |
27 | ||
28 | qtest_writeb(s, TPM_CRB_ADDR_BASE + A_CRB_LOC_CTRL, 1); | |
29 | ||
30 | qtest_memwrite(s, caddr, req, req_size); | |
31 | ||
32 | uint32_t sts, start = 1; | |
33 | uint64_t end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND; | |
34 | qtest_writel(s, TPM_CRB_ADDR_BASE + A_CRB_CTRL_START, start); | |
35 | while (true) { | |
36 | start = qtest_readl(s, TPM_CRB_ADDR_BASE + A_CRB_CTRL_START); | |
37 | if ((start & 1) == 0) { | |
38 | break; | |
39 | } | |
40 | if (g_get_monotonic_time() >= end_time) { | |
41 | break; | |
42 | } | |
43 | }; | |
44 | start = qtest_readl(s, TPM_CRB_ADDR_BASE + A_CRB_CTRL_START); | |
45 | g_assert_cmpint(start & 1, ==, 0); | |
46 | sts = qtest_readl(s, TPM_CRB_ADDR_BASE + A_CRB_CTRL_STS); | |
47 | g_assert_cmpint(sts & 1, ==, 0); | |
48 | ||
49 | qtest_memread(s, raddr, rsp, rsp_size); | |
50 | } | |
51 | ||
52 | void tpm_util_startup(QTestState *s, tx_func *tx) | |
53 | { | |
54 | unsigned char buffer[1024]; | |
55 | unsigned char tpm_startup[] = | |
56 | "\x80\x01\x00\x00\x00\x0c\x00\x00\x01\x44\x00\x00"; | |
57 | unsigned char tpm_startup_resp[] = | |
58 | "\x80\x01\x00\x00\x00\x0a\x00\x00\x00\x00"; | |
59 | ||
60 | tx(s, tpm_startup, sizeof(tpm_startup), buffer, sizeof(buffer)); | |
61 | ||
62 | g_assert_cmpmem(buffer, sizeof(tpm_startup_resp), | |
63 | tpm_startup_resp, sizeof(tpm_startup_resp)); | |
64 | } | |
65 | ||
66 | void tpm_util_pcrextend(QTestState *s, tx_func *tx) | |
67 | { | |
68 | unsigned char buffer[1024]; | |
69 | unsigned char tpm_pcrextend[] = | |
70 | "\x80\x02\x00\x00\x00\x41\x00\x00\x01\x82\x00\x00\x00\x0a\x00\x00" | |
71 | "\x00\x09\x40\x00\x00\x09\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00" | |
72 | "\x0b\x74\x65\x73\x74\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" | |
73 | "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" | |
74 | "\x00"; | |
75 | ||
76 | unsigned char tpm_pcrextend_resp[] = | |
77 | "\x80\x02\x00\x00\x00\x13\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" | |
78 | "\x01\x00\x00"; | |
79 | ||
80 | tx(s, tpm_pcrextend, sizeof(tpm_pcrextend), buffer, sizeof(buffer)); | |
81 | ||
82 | g_assert_cmpmem(buffer, sizeof(tpm_pcrextend_resp), | |
83 | tpm_pcrextend_resp, sizeof(tpm_pcrextend_resp)); | |
84 | } | |
85 | ||
86 | void tpm_util_pcrread(QTestState *s, tx_func *tx, | |
87 | const unsigned char *exp_resp, size_t exp_resp_size) | |
88 | { | |
89 | unsigned char buffer[1024]; | |
90 | unsigned char tpm_pcrread[] = | |
91 | "\x80\x01\x00\x00\x00\x14\x00\x00\x01\x7e\x00\x00\x00\x01\x00\x0b" | |
92 | "\x03\x00\x04\x00"; | |
93 | ||
94 | tx(s, tpm_pcrread, sizeof(tpm_pcrread), buffer, sizeof(buffer)); | |
95 | ||
96 | g_assert_cmpmem(buffer, exp_resp_size, exp_resp, exp_resp_size); | |
97 | } | |
98 | ||
99 | static gboolean tpm_util_swtpm_has_tpm2(void) | |
100 | { | |
101 | gint mystdout; | |
102 | gboolean succ; | |
103 | unsigned i; | |
104 | char buffer[10240]; | |
105 | ssize_t n; | |
106 | gchar *swtpm_argv[] = { | |
107 | g_strdup("swtpm"), g_strdup("socket"), g_strdup("--help"), NULL | |
108 | }; | |
109 | ||
110 | succ = g_spawn_async_with_pipes(NULL, swtpm_argv, NULL, | |
111 | G_SPAWN_SEARCH_PATH, NULL, NULL, NULL, | |
112 | NULL, &mystdout, NULL, NULL); | |
113 | if (!succ) { | |
114 | goto cleanup; | |
115 | } | |
116 | ||
117 | n = read(mystdout, buffer, sizeof(buffer) - 1); | |
118 | if (n < 0) { | |
119 | goto cleanup; | |
120 | } | |
121 | buffer[n] = 0; | |
122 | if (!strstr(buffer, "--tpm2")) { | |
123 | succ = false; | |
124 | } | |
125 | ||
126 | cleanup: | |
127 | for (i = 0; swtpm_argv[i]; i++) { | |
128 | g_free(swtpm_argv[i]); | |
129 | } | |
130 | ||
131 | return succ; | |
132 | } | |
133 | ||
134 | gboolean tpm_util_swtpm_start(const char *path, GPid *pid, | |
135 | SocketAddress **addr, GError **error) | |
136 | { | |
137 | char *swtpm_argv_tpmstate = g_strdup_printf("dir=%s", path); | |
138 | char *swtpm_argv_ctrl = g_strdup_printf("type=unixio,path=%s/sock", | |
139 | path); | |
140 | gchar *swtpm_argv[] = { | |
141 | g_strdup("swtpm"), g_strdup("socket"), | |
142 | g_strdup("--tpmstate"), swtpm_argv_tpmstate, | |
143 | g_strdup("--ctrl"), swtpm_argv_ctrl, | |
144 | g_strdup("--tpm2"), | |
145 | NULL | |
146 | }; | |
147 | gboolean succ; | |
148 | unsigned i; | |
149 | ||
150 | succ = tpm_util_swtpm_has_tpm2(); | |
151 | if (!succ) { | |
152 | goto cleanup; | |
153 | } | |
154 | ||
155 | *addr = g_new0(SocketAddress, 1); | |
156 | (*addr)->type = SOCKET_ADDRESS_TYPE_UNIX; | |
157 | (*addr)->u.q_unix.path = g_build_filename(path, "sock", NULL); | |
158 | ||
159 | succ = g_spawn_async(NULL, swtpm_argv, NULL, G_SPAWN_SEARCH_PATH, | |
160 | NULL, NULL, pid, error); | |
161 | ||
162 | cleanup: | |
163 | for (i = 0; swtpm_argv[i]; i++) { | |
164 | g_free(swtpm_argv[i]); | |
165 | } | |
166 | ||
167 | return succ; | |
168 | } | |
169 | ||
170 | void tpm_util_swtpm_kill(GPid pid) | |
171 | { | |
172 | int n; | |
173 | ||
174 | if (!pid) { | |
175 | return; | |
176 | } | |
177 | ||
178 | g_spawn_close_pid(pid); | |
179 | ||
180 | n = kill(pid, 0); | |
181 | if (n < 0) { | |
182 | return; | |
183 | } | |
184 | ||
185 | kill(pid, SIGKILL); | |
186 | } |