tls.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 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 (ret->endpoint != endpoint) {
  50. error_setg(errp,
  51. "Expected TLS credentials for a %s endpoint",
  52. endpoint == QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT ?
  53. "client" : "server");
  54. return NULL;
  55. }
  56. return ret;
  57. }
  58. static void migration_tls_incoming_handshake(QIOTask *task,
  59. gpointer opaque)
  60. {
  61. QIOChannel *ioc = QIO_CHANNEL(qio_task_get_source(task));
  62. Error *err = NULL;
  63. if (qio_task_propagate_error(task, &err)) {
  64. trace_migration_tls_incoming_handshake_error(error_get_pretty(err));
  65. error_report_err(err);
  66. } else {
  67. trace_migration_tls_incoming_handshake_complete();
  68. migration_channel_process_incoming(ioc);
  69. }
  70. object_unref(OBJECT(ioc));
  71. }
  72. void migration_tls_channel_process_incoming(MigrationState *s,
  73. QIOChannel *ioc,
  74. Error **errp)
  75. {
  76. QCryptoTLSCreds *creds;
  77. QIOChannelTLS *tioc;
  78. creds = migration_tls_get_creds(
  79. s, QCRYPTO_TLS_CREDS_ENDPOINT_SERVER, errp);
  80. if (!creds) {
  81. return;
  82. }
  83. tioc = qio_channel_tls_new_server(
  84. ioc, creds,
  85. s->parameters.tls_authz,
  86. errp);
  87. if (!tioc) {
  88. return;
  89. }
  90. trace_migration_tls_incoming_handshake_start();
  91. qio_channel_set_name(QIO_CHANNEL(tioc), "migration-tls-incoming");
  92. qio_channel_tls_handshake(tioc,
  93. migration_tls_incoming_handshake,
  94. NULL,
  95. NULL,
  96. NULL);
  97. }
  98. static void migration_tls_outgoing_handshake(QIOTask *task,
  99. gpointer opaque)
  100. {
  101. MigrationState *s = opaque;
  102. QIOChannel *ioc = QIO_CHANNEL(qio_task_get_source(task));
  103. Error *err = NULL;
  104. if (qio_task_propagate_error(task, &err)) {
  105. trace_migration_tls_outgoing_handshake_error(error_get_pretty(err));
  106. } else {
  107. trace_migration_tls_outgoing_handshake_complete();
  108. }
  109. migration_channel_connect(s, ioc, NULL, err);
  110. object_unref(OBJECT(ioc));
  111. }
  112. QIOChannelTLS *migration_tls_client_create(MigrationState *s,
  113. QIOChannel *ioc,
  114. const char *hostname,
  115. Error **errp)
  116. {
  117. QCryptoTLSCreds *creds;
  118. QIOChannelTLS *tioc;
  119. creds = migration_tls_get_creds(
  120. s, QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT, errp);
  121. if (!creds) {
  122. return NULL;
  123. }
  124. if (s->parameters.tls_hostname && *s->parameters.tls_hostname) {
  125. hostname = s->parameters.tls_hostname;
  126. }
  127. if (!hostname) {
  128. error_setg(errp, "No hostname available for TLS");
  129. return NULL;
  130. }
  131. tioc = qio_channel_tls_new_client(
  132. ioc, creds, hostname, errp);
  133. return tioc;
  134. }
  135. void migration_tls_channel_connect(MigrationState *s,
  136. QIOChannel *ioc,
  137. const char *hostname,
  138. Error **errp)
  139. {
  140. QIOChannelTLS *tioc;
  141. tioc = migration_tls_client_create(s, ioc, hostname, errp);
  142. if (!tioc) {
  143. return;
  144. }
  145. /* Save hostname into MigrationState for handshake */
  146. s->hostname = g_strdup(hostname);
  147. trace_migration_tls_outgoing_handshake_start(hostname);
  148. qio_channel_set_name(QIO_CHANNEL(tioc), "migration-tls-outgoing");
  149. qio_channel_tls_handshake(tioc,
  150. migration_tls_outgoing_handshake,
  151. s,
  152. NULL,
  153. NULL);
  154. }