tls.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 "crypto/tlscreds.h"
  25. #include "qemu/error-report.h"
  26. #include "qapi/error.h"
  27. #include "trace.h"
  28. static QCryptoTLSCreds *
  29. migration_tls_get_creds(MigrationState *s,
  30. QCryptoTLSCredsEndpoint endpoint,
  31. Error **errp)
  32. {
  33. Object *creds;
  34. QCryptoTLSCreds *ret;
  35. creds = object_resolve_path_component(
  36. object_get_objects_root(), s->parameters.tls_creds);
  37. if (!creds) {
  38. error_setg(errp, "No TLS credentials with id '%s'",
  39. s->parameters.tls_creds);
  40. return NULL;
  41. }
  42. ret = (QCryptoTLSCreds *)object_dynamic_cast(
  43. creds, TYPE_QCRYPTO_TLS_CREDS);
  44. if (!ret) {
  45. error_setg(errp, "Object with id '%s' is not TLS credentials",
  46. s->parameters.tls_creds);
  47. return NULL;
  48. }
  49. if (!qcrypto_tls_creds_check_endpoint(ret, endpoint, errp)) {
  50. return NULL;
  51. }
  52. return ret;
  53. }
  54. static void migration_tls_incoming_handshake(QIOTask *task,
  55. gpointer opaque)
  56. {
  57. QIOChannel *ioc = QIO_CHANNEL(qio_task_get_source(task));
  58. Error *err = NULL;
  59. if (qio_task_propagate_error(task, &err)) {
  60. trace_migration_tls_incoming_handshake_error(error_get_pretty(err));
  61. error_report_err(err);
  62. } else {
  63. trace_migration_tls_incoming_handshake_complete();
  64. migration_channel_process_incoming(ioc);
  65. }
  66. object_unref(OBJECT(ioc));
  67. }
  68. void migration_tls_channel_process_incoming(MigrationState *s,
  69. QIOChannel *ioc,
  70. Error **errp)
  71. {
  72. QCryptoTLSCreds *creds;
  73. QIOChannelTLS *tioc;
  74. creds = migration_tls_get_creds(
  75. s, QCRYPTO_TLS_CREDS_ENDPOINT_SERVER, errp);
  76. if (!creds) {
  77. return;
  78. }
  79. tioc = qio_channel_tls_new_server(
  80. ioc, creds,
  81. s->parameters.tls_authz,
  82. errp);
  83. if (!tioc) {
  84. return;
  85. }
  86. trace_migration_tls_incoming_handshake_start();
  87. qio_channel_set_name(QIO_CHANNEL(tioc), "migration-tls-incoming");
  88. qio_channel_tls_handshake(tioc,
  89. migration_tls_incoming_handshake,
  90. NULL,
  91. NULL,
  92. NULL);
  93. }
  94. static void migration_tls_outgoing_handshake(QIOTask *task,
  95. gpointer opaque)
  96. {
  97. MigrationState *s = opaque;
  98. QIOChannel *ioc = QIO_CHANNEL(qio_task_get_source(task));
  99. Error *err = NULL;
  100. if (qio_task_propagate_error(task, &err)) {
  101. trace_migration_tls_outgoing_handshake_error(error_get_pretty(err));
  102. } else {
  103. trace_migration_tls_outgoing_handshake_complete();
  104. }
  105. migration_channel_connect(s, ioc, NULL, err);
  106. object_unref(OBJECT(ioc));
  107. }
  108. QIOChannelTLS *migration_tls_client_create(MigrationState *s,
  109. QIOChannel *ioc,
  110. const char *hostname,
  111. Error **errp)
  112. {
  113. QCryptoTLSCreds *creds;
  114. creds = migration_tls_get_creds(
  115. s, QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT, errp);
  116. if (!creds) {
  117. return NULL;
  118. }
  119. if (s->parameters.tls_hostname && *s->parameters.tls_hostname) {
  120. hostname = s->parameters.tls_hostname;
  121. }
  122. return qio_channel_tls_new_client(ioc, creds, hostname, errp);
  123. }
  124. void migration_tls_channel_connect(MigrationState *s,
  125. QIOChannel *ioc,
  126. const char *hostname,
  127. Error **errp)
  128. {
  129. QIOChannelTLS *tioc;
  130. tioc = migration_tls_client_create(s, ioc, hostname, errp);
  131. if (!tioc) {
  132. return;
  133. }
  134. /* Save hostname into MigrationState for handshake */
  135. s->hostname = g_strdup(hostname);
  136. trace_migration_tls_outgoing_handshake_start(hostname);
  137. qio_channel_set_name(QIO_CHANNEL(tioc), "migration-tls-outgoing");
  138. qio_channel_tls_handshake(tioc,
  139. migration_tls_outgoing_handshake,
  140. s,
  141. NULL,
  142. NULL);
  143. }
  144. bool migrate_channel_requires_tls_upgrade(QIOChannel *ioc)
  145. {
  146. if (!migrate_use_tls()) {
  147. return false;
  148. }
  149. return !object_dynamic_cast(OBJECT(ioc), TYPE_QIO_CHANNEL_TLS);
  150. }