iotests: Add filter commit test cases
[qemu.git] / migration / tls.c
blob7a02ec8656d949156b329e3a2c44fa464203e39e
1 /*
2 * QEMU migration TLS support
4 * Copyright (c) 2015 Red Hat, Inc.
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.
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.
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/>.
21 #include "qemu/osdep.h"
22 #include "channel.h"
23 #include "migration.h"
24 #include "tls.h"
25 #include "io/channel-tls.h"
26 #include "crypto/tlscreds.h"
27 #include "qemu/error-report.h"
28 #include "qapi/error.h"
29 #include "trace.h"
31 static QCryptoTLSCreds *
32 migration_tls_get_creds(MigrationState *s,
33 QCryptoTLSCredsEndpoint endpoint,
34 Error **errp)
36 Object *creds;
37 QCryptoTLSCreds *ret;
39 creds = object_resolve_path_component(
40 object_get_objects_root(), s->parameters.tls_creds);
41 if (!creds) {
42 error_setg(errp, "No TLS credentials with id '%s'",
43 s->parameters.tls_creds);
44 return NULL;
46 ret = (QCryptoTLSCreds *)object_dynamic_cast(
47 creds, TYPE_QCRYPTO_TLS_CREDS);
48 if (!ret) {
49 error_setg(errp, "Object with id '%s' is not TLS credentials",
50 s->parameters.tls_creds);
51 return NULL;
53 if (ret->endpoint != endpoint) {
54 error_setg(errp,
55 "Expected TLS credentials for a %s endpoint",
56 endpoint == QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT ?
57 "client" : "server");
58 return NULL;
61 return ret;
65 static void migration_tls_incoming_handshake(QIOTask *task,
66 gpointer opaque)
68 QIOChannel *ioc = QIO_CHANNEL(qio_task_get_source(task));
69 Error *err = NULL;
71 if (qio_task_propagate_error(task, &err)) {
72 trace_migration_tls_incoming_handshake_error(error_get_pretty(err));
73 error_report_err(err);
74 } else {
75 trace_migration_tls_incoming_handshake_complete();
76 migration_channel_process_incoming(ioc);
78 object_unref(OBJECT(ioc));
81 void migration_tls_channel_process_incoming(MigrationState *s,
82 QIOChannel *ioc,
83 Error **errp)
85 QCryptoTLSCreds *creds;
86 QIOChannelTLS *tioc;
88 creds = migration_tls_get_creds(
89 s, QCRYPTO_TLS_CREDS_ENDPOINT_SERVER, errp);
90 if (!creds) {
91 return;
94 tioc = qio_channel_tls_new_server(
95 ioc, creds,
96 s->parameters.tls_authz,
97 errp);
98 if (!tioc) {
99 return;
102 trace_migration_tls_incoming_handshake_start();
103 qio_channel_set_name(QIO_CHANNEL(tioc), "migration-tls-incoming");
104 qio_channel_tls_handshake(tioc,
105 migration_tls_incoming_handshake,
106 NULL,
107 NULL,
108 NULL);
112 static void migration_tls_outgoing_handshake(QIOTask *task,
113 gpointer opaque)
115 MigrationState *s = opaque;
116 QIOChannel *ioc = QIO_CHANNEL(qio_task_get_source(task));
117 Error *err = NULL;
119 if (qio_task_propagate_error(task, &err)) {
120 trace_migration_tls_outgoing_handshake_error(error_get_pretty(err));
121 } else {
122 trace_migration_tls_outgoing_handshake_complete();
124 migration_channel_connect(s, ioc, NULL, err);
125 object_unref(OBJECT(ioc));
129 void migration_tls_channel_connect(MigrationState *s,
130 QIOChannel *ioc,
131 const char *hostname,
132 Error **errp)
134 QCryptoTLSCreds *creds;
135 QIOChannelTLS *tioc;
137 creds = migration_tls_get_creds(
138 s, QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT, errp);
139 if (!creds) {
140 return;
143 if (s->parameters.tls_hostname && *s->parameters.tls_hostname) {
144 hostname = s->parameters.tls_hostname;
146 if (!hostname) {
147 error_setg(errp, "No hostname available for TLS");
148 return;
151 tioc = qio_channel_tls_new_client(
152 ioc, creds, hostname, errp);
153 if (!tioc) {
154 return;
157 trace_migration_tls_outgoing_handshake_start(hostname);
158 qio_channel_set_name(QIO_CHANNEL(tioc), "migration-tls-outgoing");
159 qio_channel_tls_handshake(tioc,
160 migration_tls_outgoing_handshake,
162 NULL,
163 NULL);