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
21 #include "qemu/osdep.h"
22 #include "channel.h"
23 #include "migration.h"
24 #include "tls.h"
25 #include "options.h"
26 #include "crypto/tlscreds.h"
27 #include "qemu/error-report.h"
28 #include "qapi/error.h"
29 #include "trace.h"
30
31 static QCryptoTLSCreds *
migration_tls_get_creds(QCryptoTLSCredsEndpoint endpoint,Error ** errp)32 migration_tls_get_creds(QCryptoTLSCredsEndpoint endpoint, Error **errp)
33 {
34 Object *creds;
35 const char *tls_creds = migrate_tls_creds();
36 QCryptoTLSCreds *ret;
37
38 creds = object_resolve_path_component(object_get_objects_root(), tls_creds);
39 if (!creds) {
40 error_setg(errp, "No TLS credentials with id '%s'", 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 tls_creds);
48 return NULL;
49 }
50 if (!qcrypto_tls_creds_check_endpoint(ret, endpoint, errp)) {
51 return NULL;
52 }
53
54 return ret;
55 }
56
57
migration_tls_incoming_handshake(QIOTask * task,gpointer opaque)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
64 if (qio_task_propagate_error(task, &err)) {
65 trace_migration_tls_incoming_handshake_error(error_get_pretty(err));
66 error_report_err(err);
67 } else {
68 trace_migration_tls_incoming_handshake_complete();
69 migration_channel_process_incoming(ioc);
70 }
71 object_unref(OBJECT(ioc));
72 }
73
migration_tls_channel_process_incoming(MigrationState * s,QIOChannel * ioc,Error ** errp)74 void migration_tls_channel_process_incoming(MigrationState *s,
75 QIOChannel *ioc,
76 Error **errp)
77 {
78 QCryptoTLSCreds *creds;
79 QIOChannelTLS *tioc;
80
81 creds = migration_tls_get_creds(QCRYPTO_TLS_CREDS_ENDPOINT_SERVER, errp);
82 if (!creds) {
83 return;
84 }
85
86 tioc = qio_channel_tls_new_server(ioc, creds, migrate_tls_authz(), errp);
87 if (!tioc) {
88 return;
89 }
90
91 trace_migration_tls_incoming_handshake_start();
92 qio_channel_set_name(QIO_CHANNEL(tioc), "migration-tls-incoming");
93 if (migrate_postcopy_ram() || migrate_return_path()) {
94 qio_channel_set_feature(QIO_CHANNEL(tioc),
95 QIO_CHANNEL_FEATURE_CONCURRENT_IO);
96 }
97 qio_channel_tls_handshake(tioc,
98 migration_tls_incoming_handshake,
99 NULL,
100 NULL,
101 NULL);
102 }
103
104
migration_tls_outgoing_handshake(QIOTask * task,gpointer opaque)105 static void migration_tls_outgoing_handshake(QIOTask *task,
106 gpointer opaque)
107 {
108 MigrationState *s = opaque;
109 QIOChannel *ioc = QIO_CHANNEL(qio_task_get_source(task));
110 Error *err = NULL;
111
112 if (qio_task_propagate_error(task, &err)) {
113 trace_migration_tls_outgoing_handshake_error(error_get_pretty(err));
114 } else {
115 trace_migration_tls_outgoing_handshake_complete();
116 }
117 migration_channel_connect(s, ioc, NULL, err);
118 object_unref(OBJECT(ioc));
119 }
120
migration_tls_client_create(QIOChannel * ioc,const char * hostname,Error ** errp)121 QIOChannelTLS *migration_tls_client_create(QIOChannel *ioc,
122 const char *hostname,
123 Error **errp)
124 {
125 QCryptoTLSCreds *creds;
126
127 creds = migration_tls_get_creds(QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT, errp);
128 if (!creds) {
129 return NULL;
130 }
131
132 const char *tls_hostname = migrate_tls_hostname();
133 if (tls_hostname && *tls_hostname) {
134 hostname = tls_hostname;
135 }
136
137 return qio_channel_tls_new_client(ioc, creds, hostname, errp);
138 }
139
migration_tls_channel_connect(MigrationState * s,QIOChannel * ioc,const char * hostname,Error ** errp)140 void migration_tls_channel_connect(MigrationState *s,
141 QIOChannel *ioc,
142 const char *hostname,
143 Error **errp)
144 {
145 QIOChannelTLS *tioc;
146
147 tioc = migration_tls_client_create(ioc, hostname, errp);
148 if (!tioc) {
149 return;
150 }
151
152 /* Save hostname into MigrationState for handshake */
153 s->hostname = g_strdup(hostname);
154 trace_migration_tls_outgoing_handshake_start(hostname);
155 qio_channel_set_name(QIO_CHANNEL(tioc), "migration-tls-outgoing");
156
157 if (migrate_postcopy_ram() || migrate_return_path()) {
158 qio_channel_set_feature(QIO_CHANNEL(tioc),
159 QIO_CHANNEL_FEATURE_CONCURRENT_IO);
160 }
161 qio_channel_tls_handshake(tioc,
162 migration_tls_outgoing_handshake,
163 s,
164 NULL,
165 NULL);
166 }
167
migration_tls_channel_end(QIOChannel * ioc,Error ** errp)168 void migration_tls_channel_end(QIOChannel *ioc, Error **errp)
169 {
170 qio_channel_tls_bye(QIO_CHANNEL_TLS(ioc), errp);
171 }
172
migrate_channel_requires_tls_upgrade(QIOChannel * ioc)173 bool migrate_channel_requires_tls_upgrade(QIOChannel *ioc)
174 {
175 if (!migrate_tls()) {
176 return false;
177 }
178
179 return !object_dynamic_cast(OBJECT(ioc), TYPE_QIO_CHANNEL_TLS);
180 }
181