2018-05-30 19:30:26 +03:00
|
|
|
/*
|
2023-07-14 14:33:27 +03:00
|
|
|
* QTest TPM common test code
|
2018-05-30 19:30:26 +03:00
|
|
|
*
|
|
|
|
* Copyright (c) 2018 IBM Corporation
|
|
|
|
* Copyright (c) 2018 Red Hat, Inc.
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Stefan Berger <stefanb@linux.vnet.ibm.com>
|
|
|
|
* Marc-André Lureau <marcandre.lureau@redhat.com>
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU GPL, version 2 or later.
|
|
|
|
* See the COPYING file in the top-level directory.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "qemu/osdep.h"
|
|
|
|
#include <glib/gstdio.h>
|
|
|
|
|
2019-09-03 08:50:26 +03:00
|
|
|
#include "libqtest-single.h"
|
2018-05-30 19:30:26 +03:00
|
|
|
#include "tpm-tests.h"
|
|
|
|
|
2018-10-03 16:21:29 +03:00
|
|
|
static bool
|
|
|
|
tpm_test_swtpm_skip(void)
|
|
|
|
{
|
|
|
|
if (!tpm_util_swtpm_has_tpm2()) {
|
2019-04-24 12:43:13 +03:00
|
|
|
g_test_skip("swtpm not in PATH or missing --tpm2 support");
|
2018-10-03 16:21:29 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-05-30 21:31:12 +03:00
|
|
|
void tpm_test_swtpm_test(const char *src_tpm_path, tx_func *tx,
|
2020-03-05 19:51:47 +03:00
|
|
|
const char *ifmodel, const char *machine_options)
|
2018-05-30 19:30:26 +03:00
|
|
|
{
|
|
|
|
char *args = NULL;
|
|
|
|
QTestState *s;
|
|
|
|
SocketAddress *addr = NULL;
|
|
|
|
gboolean succ;
|
|
|
|
GPid swtpm_pid;
|
|
|
|
GError *error = NULL;
|
|
|
|
|
2018-10-03 16:21:29 +03:00
|
|
|
if (tpm_test_swtpm_skip()) {
|
2018-05-30 19:30:26 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-03 16:21:29 +03:00
|
|
|
succ = tpm_util_swtpm_start(src_tpm_path, &swtpm_pid, &addr, &error);
|
|
|
|
g_assert_true(succ);
|
|
|
|
|
2018-05-30 19:30:26 +03:00
|
|
|
args = g_strdup_printf(
|
2020-03-05 19:51:47 +03:00
|
|
|
"%s "
|
2018-05-30 19:30:26 +03:00
|
|
|
"-chardev socket,id=chr,path=%s "
|
|
|
|
"-tpmdev emulator,id=dev,chardev=chr "
|
2018-05-30 21:31:12 +03:00
|
|
|
"-device %s,tpmdev=dev",
|
2020-03-05 19:51:47 +03:00
|
|
|
machine_options ? : "", addr->u.q_unix.path, ifmodel);
|
2018-05-30 19:30:26 +03:00
|
|
|
|
|
|
|
s = qtest_start(args);
|
|
|
|
g_free(args);
|
|
|
|
|
|
|
|
tpm_util_startup(s, tx);
|
|
|
|
tpm_util_pcrextend(s, tx);
|
|
|
|
|
2020-09-02 11:09:09 +03:00
|
|
|
static const unsigned char tpm_pcrread_resp[] =
|
2018-05-30 19:30:26 +03:00
|
|
|
"\x80\x01\x00\x00\x00\x3e\x00\x00\x00\x00\x00\x00\x00\x16\x00\x00"
|
|
|
|
"\x00\x01\x00\x0b\x03\x00\x04\x00\x00\x00\x00\x01\x00\x20\xf6\x85"
|
|
|
|
"\x98\xe5\x86\x8d\xe6\x8b\x97\x29\x99\x60\xf2\x71\x7d\x17\x67\x89"
|
|
|
|
"\xa4\x2f\x9a\xae\xa8\xc7\xb7\xaa\x79\xa8\x62\x56\xc1\xde";
|
|
|
|
tpm_util_pcrread(s, tx, tpm_pcrread_resp,
|
|
|
|
sizeof(tpm_pcrread_resp));
|
|
|
|
|
|
|
|
qtest_end();
|
|
|
|
tpm_util_swtpm_kill(swtpm_pid);
|
|
|
|
|
2020-11-05 18:03:36 +03:00
|
|
|
g_unlink(addr->u.q_unix.path);
|
|
|
|
qapi_free_SocketAddress(addr);
|
2018-05-30 19:30:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void tpm_test_swtpm_migration_test(const char *src_tpm_path,
|
|
|
|
const char *dst_tpm_path,
|
2018-05-30 21:31:12 +03:00
|
|
|
const char *uri, tx_func *tx,
|
2020-03-05 19:51:47 +03:00
|
|
|
const char *ifmodel,
|
|
|
|
const char *machine_options)
|
2018-05-30 19:30:26 +03:00
|
|
|
{
|
|
|
|
gboolean succ;
|
|
|
|
GPid src_tpm_pid, dst_tpm_pid;
|
|
|
|
SocketAddress *src_tpm_addr = NULL, *dst_tpm_addr = NULL;
|
|
|
|
GError *error = NULL;
|
|
|
|
QTestState *src_qemu, *dst_qemu;
|
|
|
|
|
2018-10-03 16:21:29 +03:00
|
|
|
if (tpm_test_swtpm_skip()) {
|
2018-05-30 19:30:26 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-03 16:21:29 +03:00
|
|
|
succ = tpm_util_swtpm_start(src_tpm_path, &src_tpm_pid,
|
|
|
|
&src_tpm_addr, &error);
|
|
|
|
g_assert_true(succ);
|
|
|
|
|
2018-05-30 19:30:26 +03:00
|
|
|
succ = tpm_util_swtpm_start(dst_tpm_path, &dst_tpm_pid,
|
|
|
|
&dst_tpm_addr, &error);
|
2018-10-03 16:21:29 +03:00
|
|
|
g_assert_true(succ);
|
2018-05-30 19:30:26 +03:00
|
|
|
|
|
|
|
tpm_util_migration_start_qemu(&src_qemu, &dst_qemu,
|
2018-05-30 21:31:12 +03:00
|
|
|
src_tpm_addr, dst_tpm_addr, uri,
|
2020-03-05 19:51:47 +03:00
|
|
|
ifmodel, machine_options);
|
2018-05-30 19:30:26 +03:00
|
|
|
|
|
|
|
tpm_util_startup(src_qemu, tx);
|
|
|
|
tpm_util_pcrextend(src_qemu, tx);
|
|
|
|
|
2020-09-02 11:09:09 +03:00
|
|
|
static const unsigned char tpm_pcrread_resp[] =
|
2018-05-30 19:30:26 +03:00
|
|
|
"\x80\x01\x00\x00\x00\x3e\x00\x00\x00\x00\x00\x00\x00\x16\x00\x00"
|
|
|
|
"\x00\x01\x00\x0b\x03\x00\x04\x00\x00\x00\x00\x01\x00\x20\xf6\x85"
|
|
|
|
"\x98\xe5\x86\x8d\xe6\x8b\x97\x29\x99\x60\xf2\x71\x7d\x17\x67\x89"
|
|
|
|
"\xa4\x2f\x9a\xae\xa8\xc7\xb7\xaa\x79\xa8\x62\x56\xc1\xde";
|
|
|
|
tpm_util_pcrread(src_qemu, tx, tpm_pcrread_resp,
|
|
|
|
sizeof(tpm_pcrread_resp));
|
|
|
|
|
|
|
|
tpm_util_migrate(src_qemu, uri);
|
|
|
|
tpm_util_wait_for_migration_complete(src_qemu);
|
|
|
|
|
|
|
|
tpm_util_pcrread(dst_qemu, tx, tpm_pcrread_resp,
|
|
|
|
sizeof(tpm_pcrread_resp));
|
|
|
|
|
|
|
|
qtest_quit(dst_qemu);
|
|
|
|
qtest_quit(src_qemu);
|
|
|
|
|
|
|
|
tpm_util_swtpm_kill(dst_tpm_pid);
|
2021-05-25 16:44:57 +03:00
|
|
|
g_unlink(dst_tpm_addr->u.q_unix.path);
|
|
|
|
qapi_free_SocketAddress(dst_tpm_addr);
|
2018-05-30 19:30:26 +03:00
|
|
|
|
|
|
|
tpm_util_swtpm_kill(src_tpm_pid);
|
2021-05-25 16:44:57 +03:00
|
|
|
g_unlink(src_tpm_addr->u.q_unix.path);
|
|
|
|
qapi_free_SocketAddress(src_tpm_addr);
|
2018-05-30 19:30:26 +03:00
|
|
|
}
|