tls.c 4.9 KB

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