2
0

tpm-tests.c 3.9 KB

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