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