| 1 | /* |
| 2 | * QTest TPM common test code |
| 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 | #include <glib/gstdio.h> |
| 17 | |
| 18 | #include "libqtest-single.h" |
| 19 | #include "tpm-tests.h" |
| 20 | |
| 21 | static bool |
| 22 | tpm_test_swtpm_skip(void) |
| 23 | { |
| 24 | if (!tpm_util_swtpm_has_tpm2()) { |
| 25 | g_test_skip("swtpm not in PATH or missing --tpm2 support"); |
| 26 | return true; |
| 27 | } |
| 28 | |
| 29 | return false; |
| 30 | } |
| 31 | |
| 32 | void tpm_test_swtpm_test(const char *src_tpm_path, tx_func *tx, |
| 33 | const char *ifmodel, const char *machine_options) |
| 34 | { |
| 35 | char *args = NULL; |
| 36 | QTestState *s; |
| 37 | SocketAddress *addr = NULL; |
| 38 | gboolean succ; |
| 39 | GPid swtpm_pid; |
| 40 | GError *error = NULL; |
| 41 | |
| 42 | if (tpm_test_swtpm_skip()) { |
| 43 | return; |
| 44 | } |
| 45 | |
| 46 | succ = tpm_util_swtpm_start(src_tpm_path, &swtpm_pid, &addr, &error); |
| 47 | g_assert_true(succ); |
| 48 | |
| 49 | args = g_strdup_printf( |
| 50 | "%s " |
| 51 | "-chardev socket,id=chr,path=%s " |
| 52 | "-tpmdev emulator,id=tpm0,chardev=chr " |
| 53 | "-device %s,tpmdev=tpm0", |
| 54 | machine_options ? : "", addr->u.q_unix.path, ifmodel); |
| 55 | |
| 56 | s = qtest_start(args); |
| 57 | g_free(args); |
| 58 | |
| 59 | tpm_util_startup(s, tx); |
| 60 | tpm_util_pcrextend(s, tx); |
| 61 | |
| 62 | static const unsigned char tpm_pcrread_resp[] = { |
| 63 | 0x80, 0x01, 0x00, 0x00, 0x00, 0x3e, 0x00, 0x00, |
| 64 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x16, 0x00, 0x00, |
| 65 | 0x00, 0x01, 0x00, 0x0b, 0x03, 0x00, 0x04, 0x00, |
| 66 | 0x00, 0x00, 0x00, 0x01, 0x00, 0x20, 0xf6, 0x85, |
| 67 | 0x98, 0xe5, 0x86, 0x8d, 0xe6, 0x8b, 0x97, 0x29, |
| 68 | 0x99, 0x60, 0xf2, 0x71, 0x7d, 0x17, 0x67, 0x89, |
| 69 | 0xa4, 0x2f, 0x9a, 0xae, 0xa8, 0xc7, 0xb7, 0xaa, |
| 70 | 0x79, 0xa8, 0x62, 0x56, 0xc1, 0xde |
| 71 | }; |
| 72 | tpm_util_pcrread(s, tx, tpm_pcrread_resp, |
| 73 | sizeof(tpm_pcrread_resp)); |
| 74 | |
| 75 | qtest_end(); |
| 76 | tpm_util_swtpm_kill(swtpm_pid); |
| 77 | |
| 78 | g_unlink(addr->u.q_unix.path); |
| 79 | qapi_free_SocketAddress(addr); |
| 80 | } |
| 81 | |
| 82 | void tpm_test_swtpm_migration_test(const char *src_tpm_path, |
| 83 | const char *dst_tpm_path, |
| 84 | const char *uri, tx_func *tx, |
| 85 | const char *ifmodel, |
| 86 | const char *machine_options) |
| 87 | { |
| 88 | gboolean succ; |
| 89 | GPid src_tpm_pid, dst_tpm_pid; |
| 90 | SocketAddress *src_tpm_addr = NULL, *dst_tpm_addr = NULL; |
| 91 | GError *error = NULL; |
| 92 | QTestState *src_qemu, *dst_qemu; |
| 93 | |
| 94 | if (tpm_test_swtpm_skip()) { |
| 95 | return; |
| 96 | } |
| 97 | |
| 98 | succ = tpm_util_swtpm_start(src_tpm_path, &src_tpm_pid, |
| 99 | &src_tpm_addr, &error); |
| 100 | g_assert_true(succ); |
| 101 | |
| 102 | succ = tpm_util_swtpm_start(dst_tpm_path, &dst_tpm_pid, |
| 103 | &dst_tpm_addr, &error); |
| 104 | g_assert_true(succ); |
| 105 | |
| 106 | tpm_util_migration_start_qemu(&src_qemu, &dst_qemu, |
| 107 | src_tpm_addr, dst_tpm_addr, uri, |
| 108 | ifmodel, machine_options); |
| 109 | |
| 110 | tpm_util_startup(src_qemu, tx); |
| 111 | tpm_util_pcrextend(src_qemu, tx); |
| 112 | |
| 113 | static const unsigned char tpm_pcrread_resp[] = { |
| 114 | 0x80, 0x01, 0x00, 0x00, 0x00, 0x3e, 0x00, 0x00, |
| 115 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x16, 0x00, 0x00, |
| 116 | 0x00, 0x01, 0x00, 0x0b, 0x03, 0x00, 0x04, 0x00, |
| 117 | 0x00, 0x00, 0x00, 0x01, 0x00, 0x20, 0xf6, 0x85, |
| 118 | 0x98, 0xe5, 0x86, 0x8d, 0xe6, 0x8b, 0x97, 0x29, |
| 119 | 0x99, 0x60, 0xf2, 0x71, 0x7d, 0x17, 0x67, 0x89, |
| 120 | 0xa4, 0x2f, 0x9a, 0xae, 0xa8, 0xc7, 0xb7, 0xaa, |
| 121 | 0x79, 0xa8, 0x62, 0x56, 0xc1, 0xde, |
| 122 | }; |
| 123 | tpm_util_pcrread(src_qemu, tx, tpm_pcrread_resp, |
| 124 | sizeof(tpm_pcrread_resp)); |
| 125 | |
| 126 | tpm_util_migrate(src_qemu, uri); |
| 127 | tpm_util_wait_for_migration_complete(dst_qemu); |
| 128 | |
| 129 | tpm_util_pcrread(dst_qemu, tx, tpm_pcrread_resp, |
| 130 | sizeof(tpm_pcrread_resp)); |
| 131 | |
| 132 | qtest_quit(dst_qemu); |
| 133 | qtest_quit(src_qemu); |
| 134 | |
| 135 | tpm_util_swtpm_kill(dst_tpm_pid); |
| 136 | g_unlink(dst_tpm_addr->u.q_unix.path); |
| 137 | qapi_free_SocketAddress(dst_tpm_addr); |
| 138 | |
| 139 | tpm_util_swtpm_kill(src_tpm_pid); |
| 140 | g_unlink(src_tpm_addr->u.q_unix.path); |
| 141 | qapi_free_SocketAddress(src_tpm_addr); |
| 142 | } |