xref: /openbmc/qemu/tests/qtest/tpm-tests.c (revision 96420a30)
11e8a1faeSThomas Huth /*
2*96420a30SMichael Tokarev  * QTest TPM common test code
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"
161e8a1faeSThomas Huth #include <glib/gstdio.h>
171e8a1faeSThomas Huth 
181e8a1faeSThomas Huth #include "libqtest-single.h"
191e8a1faeSThomas Huth #include "tpm-tests.h"
201e8a1faeSThomas Huth 
211e8a1faeSThomas Huth static bool
tpm_test_swtpm_skip(void)221e8a1faeSThomas Huth tpm_test_swtpm_skip(void)
231e8a1faeSThomas Huth {
241e8a1faeSThomas Huth     if (!tpm_util_swtpm_has_tpm2()) {
251e8a1faeSThomas Huth         g_test_skip("swtpm not in PATH or missing --tpm2 support");
261e8a1faeSThomas Huth         return true;
271e8a1faeSThomas Huth     }
281e8a1faeSThomas Huth 
291e8a1faeSThomas Huth     return false;
301e8a1faeSThomas Huth }
311e8a1faeSThomas Huth 
tpm_test_swtpm_test(const char * src_tpm_path,tx_func * tx,const char * ifmodel,const char * machine_options)321e8a1faeSThomas Huth void tpm_test_swtpm_test(const char *src_tpm_path, tx_func *tx,
33551cabdfSEric Auger                          const char *ifmodel, const char *machine_options)
341e8a1faeSThomas Huth {
351e8a1faeSThomas Huth     char *args = NULL;
361e8a1faeSThomas Huth     QTestState *s;
371e8a1faeSThomas Huth     SocketAddress *addr = NULL;
381e8a1faeSThomas Huth     gboolean succ;
391e8a1faeSThomas Huth     GPid swtpm_pid;
401e8a1faeSThomas Huth     GError *error = NULL;
411e8a1faeSThomas Huth 
421e8a1faeSThomas Huth     if (tpm_test_swtpm_skip()) {
431e8a1faeSThomas Huth         return;
441e8a1faeSThomas Huth     }
451e8a1faeSThomas Huth 
461e8a1faeSThomas Huth     succ = tpm_util_swtpm_start(src_tpm_path, &swtpm_pid, &addr, &error);
471e8a1faeSThomas Huth     g_assert_true(succ);
481e8a1faeSThomas Huth 
491e8a1faeSThomas Huth     args = g_strdup_printf(
50551cabdfSEric Auger         "%s "
511e8a1faeSThomas Huth         "-chardev socket,id=chr,path=%s "
521e8a1faeSThomas Huth         "-tpmdev emulator,id=dev,chardev=chr "
531e8a1faeSThomas Huth         "-device %s,tpmdev=dev",
54551cabdfSEric Auger         machine_options ? : "", addr->u.q_unix.path, ifmodel);
551e8a1faeSThomas Huth 
561e8a1faeSThomas Huth     s = qtest_start(args);
571e8a1faeSThomas Huth     g_free(args);
581e8a1faeSThomas Huth 
591e8a1faeSThomas Huth     tpm_util_startup(s, tx);
601e8a1faeSThomas Huth     tpm_util_pcrextend(s, tx);
611e8a1faeSThomas Huth 
62ed943cc9SPhilippe Mathieu-Daudé     static const unsigned char tpm_pcrread_resp[] =
631e8a1faeSThomas Huth         "\x80\x01\x00\x00\x00\x3e\x00\x00\x00\x00\x00\x00\x00\x16\x00\x00"
641e8a1faeSThomas Huth         "\x00\x01\x00\x0b\x03\x00\x04\x00\x00\x00\x00\x01\x00\x20\xf6\x85"
651e8a1faeSThomas Huth         "\x98\xe5\x86\x8d\xe6\x8b\x97\x29\x99\x60\xf2\x71\x7d\x17\x67\x89"
661e8a1faeSThomas Huth         "\xa4\x2f\x9a\xae\xa8\xc7\xb7\xaa\x79\xa8\x62\x56\xc1\xde";
671e8a1faeSThomas Huth     tpm_util_pcrread(s, tx, tpm_pcrread_resp,
681e8a1faeSThomas Huth                      sizeof(tpm_pcrread_resp));
691e8a1faeSThomas Huth 
701e8a1faeSThomas Huth     qtest_end();
711e8a1faeSThomas Huth     tpm_util_swtpm_kill(swtpm_pid);
721e8a1faeSThomas Huth 
731e8a1faeSThomas Huth     g_unlink(addr->u.q_unix.path);
741e8a1faeSThomas Huth     qapi_free_SocketAddress(addr);
751e8a1faeSThomas Huth }
761e8a1faeSThomas Huth 
tpm_test_swtpm_migration_test(const char * src_tpm_path,const char * dst_tpm_path,const char * uri,tx_func * tx,const char * ifmodel,const char * machine_options)771e8a1faeSThomas Huth void tpm_test_swtpm_migration_test(const char *src_tpm_path,
781e8a1faeSThomas Huth                                    const char *dst_tpm_path,
791e8a1faeSThomas Huth                                    const char *uri, tx_func *tx,
80551cabdfSEric Auger                                    const char *ifmodel,
81551cabdfSEric Auger                                    const char *machine_options)
821e8a1faeSThomas Huth {
831e8a1faeSThomas Huth     gboolean succ;
841e8a1faeSThomas Huth     GPid src_tpm_pid, dst_tpm_pid;
851e8a1faeSThomas Huth     SocketAddress *src_tpm_addr = NULL, *dst_tpm_addr = NULL;
861e8a1faeSThomas Huth     GError *error = NULL;
871e8a1faeSThomas Huth     QTestState *src_qemu, *dst_qemu;
881e8a1faeSThomas Huth 
891e8a1faeSThomas Huth     if (tpm_test_swtpm_skip()) {
901e8a1faeSThomas Huth         return;
911e8a1faeSThomas Huth     }
921e8a1faeSThomas Huth 
931e8a1faeSThomas Huth     succ = tpm_util_swtpm_start(src_tpm_path, &src_tpm_pid,
941e8a1faeSThomas Huth                                 &src_tpm_addr, &error);
951e8a1faeSThomas Huth     g_assert_true(succ);
961e8a1faeSThomas Huth 
971e8a1faeSThomas Huth     succ = tpm_util_swtpm_start(dst_tpm_path, &dst_tpm_pid,
981e8a1faeSThomas Huth                                 &dst_tpm_addr, &error);
991e8a1faeSThomas Huth     g_assert_true(succ);
1001e8a1faeSThomas Huth 
1011e8a1faeSThomas Huth     tpm_util_migration_start_qemu(&src_qemu, &dst_qemu,
1021e8a1faeSThomas Huth                                   src_tpm_addr, dst_tpm_addr, uri,
103551cabdfSEric Auger                                   ifmodel, machine_options);
1041e8a1faeSThomas Huth 
1051e8a1faeSThomas Huth     tpm_util_startup(src_qemu, tx);
1061e8a1faeSThomas Huth     tpm_util_pcrextend(src_qemu, tx);
1071e8a1faeSThomas Huth 
108ed943cc9SPhilippe Mathieu-Daudé     static const unsigned char tpm_pcrread_resp[] =
1091e8a1faeSThomas Huth         "\x80\x01\x00\x00\x00\x3e\x00\x00\x00\x00\x00\x00\x00\x16\x00\x00"
1101e8a1faeSThomas Huth         "\x00\x01\x00\x0b\x03\x00\x04\x00\x00\x00\x00\x01\x00\x20\xf6\x85"
1111e8a1faeSThomas Huth         "\x98\xe5\x86\x8d\xe6\x8b\x97\x29\x99\x60\xf2\x71\x7d\x17\x67\x89"
1121e8a1faeSThomas Huth         "\xa4\x2f\x9a\xae\xa8\xc7\xb7\xaa\x79\xa8\x62\x56\xc1\xde";
1131e8a1faeSThomas Huth     tpm_util_pcrread(src_qemu, tx, tpm_pcrread_resp,
1141e8a1faeSThomas Huth                      sizeof(tpm_pcrread_resp));
1151e8a1faeSThomas Huth 
1161e8a1faeSThomas Huth     tpm_util_migrate(src_qemu, uri);
1171e8a1faeSThomas Huth     tpm_util_wait_for_migration_complete(src_qemu);
1181e8a1faeSThomas Huth 
1191e8a1faeSThomas Huth     tpm_util_pcrread(dst_qemu, tx, tpm_pcrread_resp,
1201e8a1faeSThomas Huth                      sizeof(tpm_pcrread_resp));
1211e8a1faeSThomas Huth 
1221e8a1faeSThomas Huth     qtest_quit(dst_qemu);
1231e8a1faeSThomas Huth     qtest_quit(src_qemu);
1241e8a1faeSThomas Huth 
1251e8a1faeSThomas Huth     tpm_util_swtpm_kill(dst_tpm_pid);
1261e8a1faeSThomas Huth     g_unlink(dst_tpm_addr->u.q_unix.path);
1271e8a1faeSThomas Huth     qapi_free_SocketAddress(dst_tpm_addr);
1281e8a1faeSThomas Huth 
1291e8a1faeSThomas Huth     tpm_util_swtpm_kill(src_tpm_pid);
1301e8a1faeSThomas Huth     g_unlink(src_tpm_addr->u.q_unix.path);
1311e8a1faeSThomas Huth     qapi_free_SocketAddress(src_tpm_addr);
1321e8a1faeSThomas Huth }
133