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