xref: /openbmc/qemu/tests/qtest/tpm-tests.c (revision a68694cd)
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-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=dev,chardev=chr "
53         "-device %s,tpmdev=dev",
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         "\x80\x01\x00\x00\x00\x3e\x00\x00\x00\x00\x00\x00\x00\x16\x00\x00"
64         "\x00\x01\x00\x0b\x03\x00\x04\x00\x00\x00\x00\x01\x00\x20\xf6\x85"
65         "\x98\xe5\x86\x8d\xe6\x8b\x97\x29\x99\x60\xf2\x71\x7d\x17\x67\x89"
66         "\xa4\x2f\x9a\xae\xa8\xc7\xb7\xaa\x79\xa8\x62\x56\xc1\xde";
67     tpm_util_pcrread(s, tx, tpm_pcrread_resp,
68                      sizeof(tpm_pcrread_resp));
69 
70     qtest_end();
71     tpm_util_swtpm_kill(swtpm_pid);
72 
73     if (addr) {
74         g_unlink(addr->u.q_unix.path);
75         qapi_free_SocketAddress(addr);
76     }
77 }
78 
79 void tpm_test_swtpm_migration_test(const char *src_tpm_path,
80                                    const char *dst_tpm_path,
81                                    const char *uri, tx_func *tx,
82                                    const char *ifmodel,
83                                    const char *machine_options)
84 {
85     gboolean succ;
86     GPid src_tpm_pid, dst_tpm_pid;
87     SocketAddress *src_tpm_addr = NULL, *dst_tpm_addr = NULL;
88     GError *error = NULL;
89     QTestState *src_qemu, *dst_qemu;
90 
91     if (tpm_test_swtpm_skip()) {
92         return;
93     }
94 
95     succ = tpm_util_swtpm_start(src_tpm_path, &src_tpm_pid,
96                                 &src_tpm_addr, &error);
97     g_assert_true(succ);
98 
99     succ = tpm_util_swtpm_start(dst_tpm_path, &dst_tpm_pid,
100                                 &dst_tpm_addr, &error);
101     g_assert_true(succ);
102 
103     tpm_util_migration_start_qemu(&src_qemu, &dst_qemu,
104                                   src_tpm_addr, dst_tpm_addr, uri,
105                                   ifmodel, machine_options);
106 
107     tpm_util_startup(src_qemu, tx);
108     tpm_util_pcrextend(src_qemu, tx);
109 
110     static const unsigned char tpm_pcrread_resp[] =
111         "\x80\x01\x00\x00\x00\x3e\x00\x00\x00\x00\x00\x00\x00\x16\x00\x00"
112         "\x00\x01\x00\x0b\x03\x00\x04\x00\x00\x00\x00\x01\x00\x20\xf6\x85"
113         "\x98\xe5\x86\x8d\xe6\x8b\x97\x29\x99\x60\xf2\x71\x7d\x17\x67\x89"
114         "\xa4\x2f\x9a\xae\xa8\xc7\xb7\xaa\x79\xa8\x62\x56\xc1\xde";
115     tpm_util_pcrread(src_qemu, tx, tpm_pcrread_resp,
116                      sizeof(tpm_pcrread_resp));
117 
118     tpm_util_migrate(src_qemu, uri);
119     tpm_util_wait_for_migration_complete(src_qemu);
120 
121     tpm_util_pcrread(dst_qemu, tx, tpm_pcrread_resp,
122                      sizeof(tpm_pcrread_resp));
123 
124     qtest_quit(dst_qemu);
125     qtest_quit(src_qemu);
126 
127     tpm_util_swtpm_kill(dst_tpm_pid);
128     if (dst_tpm_addr) {
129         g_unlink(dst_tpm_addr->u.q_unix.path);
130         qapi_free_SocketAddress(dst_tpm_addr);
131     }
132 
133     tpm_util_swtpm_kill(src_tpm_pid);
134     if (src_tpm_addr) {
135         g_unlink(src_tpm_addr->u.q_unix.path);
136         qapi_free_SocketAddress(src_tpm_addr);
137     }
138 }
139