xref: /qemu/tests/qtest/tpm-util.c (revision ea5e73b6)
11e8a1faeSThomas Huth /*
21e8a1faeSThomas Huth  * QTest TPM utilities
31e8a1faeSThomas Huth  *
41e8a1faeSThomas Huth  * Copyright (c) 2018 IBM Corporation
51e8a1faeSThomas Huth  * Copyright (c) 2018 Red Hat, Inc.
61e8a1faeSThomas Huth  *
71e8a1faeSThomas Huth  * Authors:
81e8a1faeSThomas Huth  *   Stefan Berger <stefanb@linux.vnet.ibm.com>
91e8a1faeSThomas Huth  *   Marc-André Lureau <marcandre.lureau@redhat.com>
101e8a1faeSThomas Huth  *
111e8a1faeSThomas Huth  * This work is licensed under the terms of the GNU GPL, version 2 or later.
121e8a1faeSThomas Huth  * See the COPYING file in the top-level directory.
131e8a1faeSThomas Huth  */
141e8a1faeSThomas Huth 
151e8a1faeSThomas Huth #include "qemu/osdep.h"
16*daa8bb57SThomas Huth #include <glib/gstdio.h>
171e8a1faeSThomas Huth 
181e8a1faeSThomas Huth #include "hw/acpi/tpm.h"
19907b5105SMarc-André Lureau #include "libqtest.h"
201e8a1faeSThomas Huth #include "tpm-util.h"
211e8a1faeSThomas Huth #include "qapi/qmp/qdict.h"
221e8a1faeSThomas Huth 
tpm_util_crb_transfer(QTestState * s,const unsigned char * req,size_t req_size,unsigned char * rsp,size_t rsp_size)231e8a1faeSThomas Huth void tpm_util_crb_transfer(QTestState *s,
241e8a1faeSThomas Huth                            const unsigned char *req, size_t req_size,
251e8a1faeSThomas Huth                            unsigned char *rsp, size_t rsp_size)
261e8a1faeSThomas Huth {
271e8a1faeSThomas Huth     uint64_t caddr = qtest_readq(s, TPM_CRB_ADDR_BASE + A_CRB_CTRL_CMD_LADDR);
281e8a1faeSThomas Huth     uint64_t raddr = qtest_readq(s, TPM_CRB_ADDR_BASE + A_CRB_CTRL_RSP_ADDR);
291e8a1faeSThomas Huth 
301e8a1faeSThomas Huth     qtest_writeb(s, TPM_CRB_ADDR_BASE + A_CRB_LOC_CTRL, 1);
311e8a1faeSThomas Huth 
321e8a1faeSThomas Huth     qtest_memwrite(s, caddr, req, req_size);
331e8a1faeSThomas Huth 
341e8a1faeSThomas Huth     uint32_t sts, start = 1;
351e8a1faeSThomas Huth     uint64_t end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND;
361e8a1faeSThomas Huth     qtest_writel(s, TPM_CRB_ADDR_BASE + A_CRB_CTRL_START, start);
371e8a1faeSThomas Huth     while (true) {
381e8a1faeSThomas Huth         start = qtest_readl(s, TPM_CRB_ADDR_BASE + A_CRB_CTRL_START);
391e8a1faeSThomas Huth         if ((start & 1) == 0) {
401e8a1faeSThomas Huth             break;
411e8a1faeSThomas Huth         }
421e8a1faeSThomas Huth         if (g_get_monotonic_time() >= end_time) {
431e8a1faeSThomas Huth             break;
441e8a1faeSThomas Huth         }
451e8a1faeSThomas Huth     };
461e8a1faeSThomas Huth     start = qtest_readl(s, TPM_CRB_ADDR_BASE + A_CRB_CTRL_START);
471e8a1faeSThomas Huth     g_assert_cmpint(start & 1, ==, 0);
481e8a1faeSThomas Huth     sts = qtest_readl(s, TPM_CRB_ADDR_BASE + A_CRB_CTRL_STS);
491e8a1faeSThomas Huth     g_assert_cmpint(sts & 1, ==, 0);
501e8a1faeSThomas Huth 
511e8a1faeSThomas Huth     qtest_memread(s, raddr, rsp, rsp_size);
521e8a1faeSThomas Huth }
531e8a1faeSThomas Huth 
tpm_util_startup(QTestState * s,tx_func * tx)541e8a1faeSThomas Huth void tpm_util_startup(QTestState *s, tx_func *tx)
551e8a1faeSThomas Huth {
561e8a1faeSThomas Huth     unsigned char buffer[1024];
57ed943cc9SPhilippe Mathieu-Daudé     static const unsigned char tpm_startup[] =
581e8a1faeSThomas Huth         "\x80\x01\x00\x00\x00\x0c\x00\x00\x01\x44\x00\x00";
59ed943cc9SPhilippe Mathieu-Daudé     static const unsigned char tpm_startup_resp[] =
601e8a1faeSThomas Huth         "\x80\x01\x00\x00\x00\x0a\x00\x00\x00\x00";
611e8a1faeSThomas Huth 
621e8a1faeSThomas Huth     tx(s, tpm_startup, sizeof(tpm_startup), buffer, sizeof(buffer));
631e8a1faeSThomas Huth 
641e8a1faeSThomas Huth     g_assert_cmpmem(buffer, sizeof(tpm_startup_resp),
651e8a1faeSThomas Huth                     tpm_startup_resp, sizeof(tpm_startup_resp));
661e8a1faeSThomas Huth }
671e8a1faeSThomas Huth 
tpm_util_pcrextend(QTestState * s,tx_func * tx)681e8a1faeSThomas Huth void tpm_util_pcrextend(QTestState *s, tx_func *tx)
691e8a1faeSThomas Huth {
701e8a1faeSThomas Huth     unsigned char buffer[1024];
71ed943cc9SPhilippe Mathieu-Daudé     static const unsigned char tpm_pcrextend[] =
721e8a1faeSThomas Huth         "\x80\x02\x00\x00\x00\x41\x00\x00\x01\x82\x00\x00\x00\x0a\x00\x00"
731e8a1faeSThomas Huth         "\x00\x09\x40\x00\x00\x09\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00"
741e8a1faeSThomas Huth         "\x0b\x74\x65\x73\x74\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
751e8a1faeSThomas Huth         "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
761e8a1faeSThomas Huth         "\x00";
771e8a1faeSThomas Huth 
78ed943cc9SPhilippe Mathieu-Daudé     static const unsigned char tpm_pcrextend_resp[] =
791e8a1faeSThomas Huth         "\x80\x02\x00\x00\x00\x13\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
801e8a1faeSThomas Huth         "\x01\x00\x00";
811e8a1faeSThomas Huth 
821e8a1faeSThomas Huth     tx(s, tpm_pcrextend, sizeof(tpm_pcrextend), buffer, sizeof(buffer));
831e8a1faeSThomas Huth 
841e8a1faeSThomas Huth     g_assert_cmpmem(buffer, sizeof(tpm_pcrextend_resp),
851e8a1faeSThomas Huth                     tpm_pcrextend_resp, sizeof(tpm_pcrextend_resp));
861e8a1faeSThomas Huth }
871e8a1faeSThomas Huth 
tpm_util_pcrread(QTestState * s,tx_func * tx,const unsigned char * exp_resp,size_t exp_resp_size)881e8a1faeSThomas Huth void tpm_util_pcrread(QTestState *s, tx_func *tx,
891e8a1faeSThomas Huth                       const unsigned char *exp_resp, size_t exp_resp_size)
901e8a1faeSThomas Huth {
911e8a1faeSThomas Huth     unsigned char buffer[1024];
92ed943cc9SPhilippe Mathieu-Daudé     static const unsigned char tpm_pcrread[] =
931e8a1faeSThomas Huth         "\x80\x01\x00\x00\x00\x14\x00\x00\x01\x7e\x00\x00\x00\x01\x00\x0b"
941e8a1faeSThomas Huth         "\x03\x00\x04\x00";
951e8a1faeSThomas Huth 
961e8a1faeSThomas Huth     tx(s, tpm_pcrread, sizeof(tpm_pcrread), buffer, sizeof(buffer));
971e8a1faeSThomas Huth 
98df8a7568SStefan Berger     /* skip pcrUpdateCounter (14th byte) in comparison */
99df8a7568SStefan Berger     g_assert(exp_resp_size >= 15);
100df8a7568SStefan Berger     g_assert_cmpmem(buffer, 13, exp_resp, 13);
101df8a7568SStefan Berger     g_assert_cmpmem(&buffer[14], exp_resp_size - 14,
102df8a7568SStefan Berger                     &exp_resp[14], exp_resp_size - 14);
1031e8a1faeSThomas Huth }
1041e8a1faeSThomas Huth 
tpm_util_swtpm_has_tpm2(void)1051e8a1faeSThomas Huth bool tpm_util_swtpm_has_tpm2(void)
1061e8a1faeSThomas Huth {
1071e8a1faeSThomas Huth     bool has_tpm2 = false;
1081e8a1faeSThomas Huth     char *out = NULL;
1091e8a1faeSThomas Huth     static const char *argv[] = {
1101e8a1faeSThomas Huth         "swtpm", "socket", "--help", NULL
1111e8a1faeSThomas Huth     };
1121e8a1faeSThomas Huth 
1131e8a1faeSThomas Huth     if (!g_spawn_sync(NULL /* working_dir */,
1141e8a1faeSThomas Huth                       (char **)argv,
1151e8a1faeSThomas Huth                       NULL /* envp */,
1161e8a1faeSThomas Huth                       G_SPAWN_SEARCH_PATH,
1171e8a1faeSThomas Huth                       NULL /* child_setup */,
1181e8a1faeSThomas Huth                       NULL /* user_data */,
1191e8a1faeSThomas Huth                       &out,
1201e8a1faeSThomas Huth                       NULL /* err */,
1211e8a1faeSThomas Huth                       NULL /* exit_status */,
1221e8a1faeSThomas Huth                       NULL)) {
1231e8a1faeSThomas Huth         return false;
1241e8a1faeSThomas Huth     }
1251e8a1faeSThomas Huth 
1261e8a1faeSThomas Huth     if (strstr(out, "--tpm2")) {
1271e8a1faeSThomas Huth         has_tpm2 = true;
1281e8a1faeSThomas Huth     }
1291e8a1faeSThomas Huth 
1301e8a1faeSThomas Huth     g_free(out);
1311e8a1faeSThomas Huth     return has_tpm2;
1321e8a1faeSThomas Huth }
1331e8a1faeSThomas Huth 
tpm_util_swtpm_start(const char * path,GPid * pid,SocketAddress ** addr,GError ** error)1341e8a1faeSThomas Huth gboolean tpm_util_swtpm_start(const char *path, GPid *pid,
1351e8a1faeSThomas Huth                               SocketAddress **addr, GError **error)
1361e8a1faeSThomas Huth {
1371e8a1faeSThomas Huth     char *swtpm_argv_tpmstate = g_strdup_printf("dir=%s", path);
1381e8a1faeSThomas Huth     char *swtpm_argv_ctrl = g_strdup_printf("type=unixio,path=%s/sock",
1391e8a1faeSThomas Huth                                             path);
1401e8a1faeSThomas Huth     gchar *swtpm_argv[] = {
1411e8a1faeSThomas Huth         g_strdup("swtpm"), g_strdup("socket"),
1421e8a1faeSThomas Huth         g_strdup("--tpmstate"), swtpm_argv_tpmstate,
1431e8a1faeSThomas Huth         g_strdup("--ctrl"), swtpm_argv_ctrl,
1441e8a1faeSThomas Huth         g_strdup("--tpm2"),
1451e8a1faeSThomas Huth         NULL
1461e8a1faeSThomas Huth     };
1471e8a1faeSThomas Huth     gboolean succ;
1481e8a1faeSThomas Huth     unsigned i;
1491e8a1faeSThomas Huth 
1501e8a1faeSThomas Huth     *addr = g_new0(SocketAddress, 1);
1511e8a1faeSThomas Huth     (*addr)->type = SOCKET_ADDRESS_TYPE_UNIX;
1521e8a1faeSThomas Huth     (*addr)->u.q_unix.path = g_build_filename(path, "sock", NULL);
1531e8a1faeSThomas Huth 
1541e8a1faeSThomas Huth     succ = g_spawn_async(NULL, swtpm_argv, NULL, G_SPAWN_SEARCH_PATH,
1551e8a1faeSThomas Huth                          NULL, NULL, pid, error);
1561e8a1faeSThomas Huth 
1571e8a1faeSThomas Huth     for (i = 0; swtpm_argv[i]; i++) {
1581e8a1faeSThomas Huth         g_free(swtpm_argv[i]);
1591e8a1faeSThomas Huth     }
1601e8a1faeSThomas Huth 
1611e8a1faeSThomas Huth     return succ;
1621e8a1faeSThomas Huth }
1631e8a1faeSThomas Huth 
tpm_util_swtpm_kill(GPid pid)1641e8a1faeSThomas Huth void tpm_util_swtpm_kill(GPid pid)
1651e8a1faeSThomas Huth {
1661e8a1faeSThomas Huth     int n;
1671e8a1faeSThomas Huth 
1681e8a1faeSThomas Huth     if (!pid) {
1691e8a1faeSThomas Huth         return;
1701e8a1faeSThomas Huth     }
1711e8a1faeSThomas Huth 
1721e8a1faeSThomas Huth     g_spawn_close_pid(pid);
1731e8a1faeSThomas Huth 
1741e8a1faeSThomas Huth     n = kill(pid, 0);
1751e8a1faeSThomas Huth     if (n < 0) {
1761e8a1faeSThomas Huth         return;
1771e8a1faeSThomas Huth     }
1781e8a1faeSThomas Huth 
1791e8a1faeSThomas Huth     kill(pid, SIGKILL);
1801e8a1faeSThomas Huth }
1811e8a1faeSThomas Huth 
tpm_util_migrate(QTestState * who,const char * uri)1821e8a1faeSThomas Huth void tpm_util_migrate(QTestState *who, const char *uri)
1831e8a1faeSThomas Huth {
1841e8a1faeSThomas Huth     QDict *rsp;
1851e8a1faeSThomas Huth 
1861e8a1faeSThomas Huth     rsp = qtest_qmp(who,
1871e8a1faeSThomas Huth                     "{ 'execute': 'migrate', 'arguments': { 'uri': %s } }",
1881e8a1faeSThomas Huth                     uri);
1891e8a1faeSThomas Huth     g_assert(qdict_haskey(rsp, "return"));
1901e8a1faeSThomas Huth     qobject_unref(rsp);
1911e8a1faeSThomas Huth }
1921e8a1faeSThomas Huth 
tpm_util_wait_for_migration_complete(QTestState * who)1931e8a1faeSThomas Huth void tpm_util_wait_for_migration_complete(QTestState *who)
1941e8a1faeSThomas Huth {
1951e8a1faeSThomas Huth     while (true) {
196bb1a5b97SMaxim Levitsky         QDict *rsp;
1971e8a1faeSThomas Huth         QDict *rsp_return;
1981e8a1faeSThomas Huth         bool completed;
1991e8a1faeSThomas Huth         const char *status;
2001e8a1faeSThomas Huth 
201bb1a5b97SMaxim Levitsky         rsp = qtest_qmp(who, "{ 'execute': 'query-migrate' }");
202bb1a5b97SMaxim Levitsky         g_assert(qdict_haskey(rsp, "return"));
203bb1a5b97SMaxim Levitsky         rsp_return = qdict_get_qdict(rsp, "return");
204bb1a5b97SMaxim Levitsky 
205bb1a5b97SMaxim Levitsky         g_assert(!qdict_haskey(rsp_return, "error"));
2061e8a1faeSThomas Huth         status = qdict_get_str(rsp_return, "status");
2071e8a1faeSThomas Huth         completed = strcmp(status, "completed") == 0;
2081e8a1faeSThomas Huth         g_assert_cmpstr(status, !=,  "failed");
209f8e3df09SStefan Berger         qobject_unref(rsp);
2101e8a1faeSThomas Huth         if (completed) {
2111e8a1faeSThomas Huth             return;
2121e8a1faeSThomas Huth         }
2131e8a1faeSThomas Huth         usleep(1000);
2141e8a1faeSThomas Huth     }
2151e8a1faeSThomas Huth }
2161e8a1faeSThomas Huth 
tpm_util_migration_start_qemu(QTestState ** src_qemu,QTestState ** dst_qemu,SocketAddress * src_tpm_addr,SocketAddress * dst_tpm_addr,const char * miguri,const char * ifmodel,const char * machine_options)2171e8a1faeSThomas Huth void tpm_util_migration_start_qemu(QTestState **src_qemu,
2181e8a1faeSThomas Huth                                    QTestState **dst_qemu,
2191e8a1faeSThomas Huth                                    SocketAddress *src_tpm_addr,
2201e8a1faeSThomas Huth                                    SocketAddress *dst_tpm_addr,
2211e8a1faeSThomas Huth                                    const char *miguri,
222551cabdfSEric Auger                                    const char *ifmodel,
223551cabdfSEric Auger                                    const char *machine_options)
2241e8a1faeSThomas Huth {
2251e8a1faeSThomas Huth     char *src_qemu_args, *dst_qemu_args;
2261e8a1faeSThomas Huth 
2271e8a1faeSThomas Huth     src_qemu_args = g_strdup_printf(
228551cabdfSEric Auger         "%s "
2291e8a1faeSThomas Huth         "-chardev socket,id=chr,path=%s "
2301e8a1faeSThomas Huth         "-tpmdev emulator,id=dev,chardev=chr "
2311e8a1faeSThomas Huth         "-device %s,tpmdev=dev ",
232551cabdfSEric Auger         machine_options ? : "", src_tpm_addr->u.q_unix.path, ifmodel);
2331e8a1faeSThomas Huth 
2341e8a1faeSThomas Huth     *src_qemu = qtest_init(src_qemu_args);
2351e8a1faeSThomas Huth 
2361e8a1faeSThomas Huth     dst_qemu_args = g_strdup_printf(
237551cabdfSEric Auger         "%s "
2381e8a1faeSThomas Huth         "-chardev socket,id=chr,path=%s "
2391e8a1faeSThomas Huth         "-tpmdev emulator,id=dev,chardev=chr "
2401e8a1faeSThomas Huth         "-device %s,tpmdev=dev "
2411e8a1faeSThomas Huth         "-incoming %s",
242551cabdfSEric Auger         machine_options ? : "",
2431e8a1faeSThomas Huth         dst_tpm_addr->u.q_unix.path,
2441e8a1faeSThomas Huth         ifmodel, miguri);
2451e8a1faeSThomas Huth 
2461e8a1faeSThomas Huth     *dst_qemu = qtest_init(dst_qemu_args);
2471e8a1faeSThomas Huth 
248e7b13acdSPeter Maydell     g_free(src_qemu_args);
249e7b13acdSPeter Maydell     g_free(dst_qemu_args);
2501e8a1faeSThomas Huth }
251*daa8bb57SThomas Huth 
252*daa8bb57SThomas Huth /* Remove directory with remainders of swtpm */
tpm_util_rmdir(const char * path)253*daa8bb57SThomas Huth void tpm_util_rmdir(const char *path)
254*daa8bb57SThomas Huth {
255*daa8bb57SThomas Huth     char *filename;
256*daa8bb57SThomas Huth     int ret;
257*daa8bb57SThomas Huth 
258*daa8bb57SThomas Huth     filename = g_strdup_printf("%s/tpm2-00.permall", path);
259*daa8bb57SThomas Huth     g_unlink(filename);
260*daa8bb57SThomas Huth     g_free(filename);
261*daa8bb57SThomas Huth 
262*daa8bb57SThomas Huth     filename = g_strdup_printf("%s/.lock", path);
263*daa8bb57SThomas Huth     g_unlink(filename);
264*daa8bb57SThomas Huth     g_free(filename);
265*daa8bb57SThomas Huth 
266*daa8bb57SThomas Huth     ret = g_rmdir(path);
267*daa8bb57SThomas Huth     g_assert(!ret);
268*daa8bb57SThomas Huth }
269