11e8a1faeSThomas Huth /* 21e8a1faeSThomas Huth * QTest TPM commont 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 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 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 62*ed943cc9SPhilippe 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 if (addr) { 741e8a1faeSThomas Huth g_unlink(addr->u.q_unix.path); 751e8a1faeSThomas Huth qapi_free_SocketAddress(addr); 761e8a1faeSThomas Huth } 771e8a1faeSThomas Huth } 781e8a1faeSThomas Huth 791e8a1faeSThomas Huth void tpm_test_swtpm_migration_test(const char *src_tpm_path, 801e8a1faeSThomas Huth const char *dst_tpm_path, 811e8a1faeSThomas Huth const char *uri, tx_func *tx, 82551cabdfSEric Auger const char *ifmodel, 83551cabdfSEric Auger const char *machine_options) 841e8a1faeSThomas Huth { 851e8a1faeSThomas Huth gboolean succ; 861e8a1faeSThomas Huth GPid src_tpm_pid, dst_tpm_pid; 871e8a1faeSThomas Huth SocketAddress *src_tpm_addr = NULL, *dst_tpm_addr = NULL; 881e8a1faeSThomas Huth GError *error = NULL; 891e8a1faeSThomas Huth QTestState *src_qemu, *dst_qemu; 901e8a1faeSThomas Huth 911e8a1faeSThomas Huth if (tpm_test_swtpm_skip()) { 921e8a1faeSThomas Huth return; 931e8a1faeSThomas Huth } 941e8a1faeSThomas Huth 951e8a1faeSThomas Huth succ = tpm_util_swtpm_start(src_tpm_path, &src_tpm_pid, 961e8a1faeSThomas Huth &src_tpm_addr, &error); 971e8a1faeSThomas Huth g_assert_true(succ); 981e8a1faeSThomas Huth 991e8a1faeSThomas Huth succ = tpm_util_swtpm_start(dst_tpm_path, &dst_tpm_pid, 1001e8a1faeSThomas Huth &dst_tpm_addr, &error); 1011e8a1faeSThomas Huth g_assert_true(succ); 1021e8a1faeSThomas Huth 1031e8a1faeSThomas Huth tpm_util_migration_start_qemu(&src_qemu, &dst_qemu, 1041e8a1faeSThomas Huth src_tpm_addr, dst_tpm_addr, uri, 105551cabdfSEric Auger ifmodel, machine_options); 1061e8a1faeSThomas Huth 1071e8a1faeSThomas Huth tpm_util_startup(src_qemu, tx); 1081e8a1faeSThomas Huth tpm_util_pcrextend(src_qemu, tx); 1091e8a1faeSThomas Huth 110*ed943cc9SPhilippe Mathieu-Daudé static const unsigned char tpm_pcrread_resp[] = 1111e8a1faeSThomas Huth "\x80\x01\x00\x00\x00\x3e\x00\x00\x00\x00\x00\x00\x00\x16\x00\x00" 1121e8a1faeSThomas Huth "\x00\x01\x00\x0b\x03\x00\x04\x00\x00\x00\x00\x01\x00\x20\xf6\x85" 1131e8a1faeSThomas Huth "\x98\xe5\x86\x8d\xe6\x8b\x97\x29\x99\x60\xf2\x71\x7d\x17\x67\x89" 1141e8a1faeSThomas Huth "\xa4\x2f\x9a\xae\xa8\xc7\xb7\xaa\x79\xa8\x62\x56\xc1\xde"; 1151e8a1faeSThomas Huth tpm_util_pcrread(src_qemu, tx, tpm_pcrread_resp, 1161e8a1faeSThomas Huth sizeof(tpm_pcrread_resp)); 1171e8a1faeSThomas Huth 1181e8a1faeSThomas Huth tpm_util_migrate(src_qemu, uri); 1191e8a1faeSThomas Huth tpm_util_wait_for_migration_complete(src_qemu); 1201e8a1faeSThomas Huth 1211e8a1faeSThomas Huth tpm_util_pcrread(dst_qemu, tx, tpm_pcrread_resp, 1221e8a1faeSThomas Huth sizeof(tpm_pcrread_resp)); 1231e8a1faeSThomas Huth 1241e8a1faeSThomas Huth qtest_quit(dst_qemu); 1251e8a1faeSThomas Huth qtest_quit(src_qemu); 1261e8a1faeSThomas Huth 1271e8a1faeSThomas Huth tpm_util_swtpm_kill(dst_tpm_pid); 1281e8a1faeSThomas Huth if (dst_tpm_addr) { 1291e8a1faeSThomas Huth g_unlink(dst_tpm_addr->u.q_unix.path); 1301e8a1faeSThomas Huth qapi_free_SocketAddress(dst_tpm_addr); 1311e8a1faeSThomas Huth } 1321e8a1faeSThomas Huth 1331e8a1faeSThomas Huth tpm_util_swtpm_kill(src_tpm_pid); 1341e8a1faeSThomas Huth if (src_tpm_addr) { 1351e8a1faeSThomas Huth g_unlink(src_tpm_addr->u.q_unix.path); 1361e8a1faeSThomas Huth qapi_free_SocketAddress(src_tpm_addr); 1371e8a1faeSThomas Huth } 1381e8a1faeSThomas Huth } 139