2
0

tls.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * QEMU migration TLS support
  3. *
  4. * Copyright (c) 2015 Red Hat, Inc.
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. #include "qemu/osdep.h"
  21. #include "channel.h"
  22. #include "migration.h"
  23. #include "tls.h"
  24. #include "options.h"
  25. #include "crypto/tlscreds.h"
  26. #include "qemu/error-report.h"
  27. #include "qapi/error.h"
  28. #include "trace.h"
  29. static QCryptoTLSCreds *
  30. migration_tls_get_creds(QCryptoTLSCredsEndpoint endpoint, Error **errp)
  31. {
  32. Object *creds;
  33. const char *tls_creds = migrate_tls_creds();
  34. QCryptoTLSCreds *ret;
  35. creds = object_resolve_path_component(object_get_objects_root(), tls_creds);
  36. if (!creds) {
  37. error_setg(errp, "No TLS credentials with id '%s'", tls_creds);
  38. return NULL;
  39. }
  40. ret = (QCryptoTLSCreds *)object_dynamic_cast(
  41. creds, TYPE_QCRYPTO_TLS_CREDS);
  42. if (!ret) {
  43. error_setg(errp, "Object with id '%s' is not TLS credentials",
  44. tls_creds);
  45. return NULL;
  46. }
  47. if (!qcrypto_tls_creds_check_endpoint(ret, endpoint, errp)) {
  48. return NULL;
  49. }
  50. return ret;
  51. }
  52. static void migration_tls_incoming_handshake(QIOTask *task,
  53. gpointer opaque)
  54. {
  55. QIOChannel *ioc = QIO_CHANNEL(qio_task_get_source(task));
  56. Error *err = NULL;
  57. if (qio_task_propagate_error(task, &err)) {
  58. trace_migration_tls_incoming_handshake_error(error_get_pretty(err));
  59. error_report_err(err);
  60. } else {
  61. trace_migration_tls_incoming_handshake_complete();
  62. migration_channel_process_incoming(ioc);
  63. }
  64. object_unref(OBJECT(ioc));
  65. }
  66. void migration_tls_channel_process_incoming(MigrationState *s,
  67. QIOChannel *ioc,
  68. Error **errp)
  69. {
  70. QCryptoTLSCreds *creds;
  71. QIOChannelTLS *tioc;
  72. creds = migration_tls_get_creds(QCRYPTO_TLS_CREDS_ENDPOINT_SERVER, errp);
  73. if (!creds) {
  74. return;
  75. }
  76. tioc = qio_channel_tls_new_server(ioc, creds, migrate_tls_authz(), errp);
  77. if (!tioc) {
  78. return;
  79. }
  80. trace_migration_tls_incoming_handshake_start();
  81. qio_channel_set_name(QIO_CHANNEL(tioc), "migration-tls-incoming");
  82. qio_channel_tls_handshake(tioc,
  83. migration_tls_incoming_handshake,
  84. NULL,
  85. NULL,
  86. NULL);
  87. }
  88. static void migration_tls_outgoing_handshake(QIOTask *task,
  89. gpointer opaque)
  90. {
  91. MigrationState *s = opaque;
  92. QIOChannel *ioc = QIO_CHANNEL(qio_task_get_source(task));
  93. Error *err = NULL;
  94. if (qio_task_propagate_error(task, &err)) {
  95. trace_migration_tls_outgoing_handshake_error(error_get_pretty(err));
  96. } else {
  97. trace_migration_tls_outgoing_handshake_complete();
  98. }
  99. migration_channel_connect(s, ioc, NULL, err);
  100. object_unref(OBJECT(ioc));
  101. }
  102. QIOChannelTLS *migration_tls_client_create(QIOChannel *ioc,
  103. const char *hostname,
  104. Error **errp)
  105. {
  106. QCryptoTLSCreds *creds;
  107. creds = migration_tls_get_creds(QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT, errp);
  108. if (!creds) {
  109. return NULL;
  110. }
  111. const char *tls_hostname = migrate_tls_hostname();
  112. if (tls_hostname && *tls_hostname) {
  113. hostname = tls_hostname;
  114. }
  115. return qio_channel_tls_new_client(ioc, creds, hostname, errp);
  116. }
  117. void migration_tls_channel_connect(MigrationState *s,
  118. QIOChannel *ioc,
  119. const char *hostname,
  120. Error **errp)
  121. {
  122. QIOChannelTLS *tioc;
  123. tioc = migration_tls_client_create(ioc, hostname, errp);
  124. if (!tioc) {
  125. return;
  126. }
  127. /* Save hostname into MigrationState for handshake */
  128. s->hostname = g_strdup(hostname);
  129. trace_migration_tls_outgoing_handshake_start(hostname);
  130. qio_channel_set_name(QIO_CHANNEL(tioc), "migration-tls-outgoing");
  131. qio_channel_tls_handshake(tioc,
  132. migration_tls_outgoing_handshake,
  133. s,
  134. NULL,
  135. NULL);
  136. }
  137. void migration_tls_channel_end(QIOChannel *ioc, Error **errp)
  138. {
  139. qio_channel_tls_bye(QIO_CHANNEL_TLS(ioc), errp);
  140. }
  141. bool migrate_channel_requires_tls_upgrade(QIOChannel *ioc)
  142. {
  143. if (!migrate_tls()) {
  144. return false;
  145. }
  146. return !object_dynamic_cast(OBJECT(ioc), TYPE_QIO_CHANNEL_TLS);
  147. }