xref: /openbmc/qemu/migration/tls.c (revision 39920a04)
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 *
32 migration_tls_get_creds(MigrationState *s,
33                         QCryptoTLSCredsEndpoint endpoint,
34                         Error **errp)
35 {
36     Object *creds;
37     const char *tls_creds = migrate_tls_creds();
38     QCryptoTLSCreds *ret;
39 
40     creds = object_resolve_path_component(object_get_objects_root(), tls_creds);
41     if (!creds) {
42         error_setg(errp, "No TLS credentials with id '%s'", tls_creds);
43         return NULL;
44     }
45     ret = (QCryptoTLSCreds *)object_dynamic_cast(
46         creds, TYPE_QCRYPTO_TLS_CREDS);
47     if (!ret) {
48         error_setg(errp, "Object with id '%s' is not TLS credentials",
49                    tls_creds);
50         return NULL;
51     }
52     if (!qcrypto_tls_creds_check_endpoint(ret, endpoint, errp)) {
53         return NULL;
54     }
55 
56     return ret;
57 }
58 
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 
66     if (qio_task_propagate_error(task, &err)) {
67         trace_migration_tls_incoming_handshake_error(error_get_pretty(err));
68         error_report_err(err);
69     } else {
70         trace_migration_tls_incoming_handshake_complete();
71         migration_channel_process_incoming(ioc);
72     }
73     object_unref(OBJECT(ioc));
74 }
75 
76 void migration_tls_channel_process_incoming(MigrationState *s,
77                                             QIOChannel *ioc,
78                                             Error **errp)
79 {
80     QCryptoTLSCreds *creds;
81     QIOChannelTLS *tioc;
82 
83     creds = migration_tls_get_creds(
84         s, QCRYPTO_TLS_CREDS_ENDPOINT_SERVER, errp);
85     if (!creds) {
86         return;
87     }
88 
89     tioc = qio_channel_tls_new_server(ioc, creds, migrate_tls_authz(), errp);
90     if (!tioc) {
91         return;
92     }
93 
94     trace_migration_tls_incoming_handshake_start();
95     qio_channel_set_name(QIO_CHANNEL(tioc), "migration-tls-incoming");
96     qio_channel_tls_handshake(tioc,
97                               migration_tls_incoming_handshake,
98                               NULL,
99                               NULL,
100                               NULL);
101 }
102 
103 
104 static void migration_tls_outgoing_handshake(QIOTask *task,
105                                              gpointer opaque)
106 {
107     MigrationState *s = opaque;
108     QIOChannel *ioc = QIO_CHANNEL(qio_task_get_source(task));
109     Error *err = NULL;
110 
111     if (qio_task_propagate_error(task, &err)) {
112         trace_migration_tls_outgoing_handshake_error(error_get_pretty(err));
113     } else {
114         trace_migration_tls_outgoing_handshake_complete();
115     }
116     migration_channel_connect(s, ioc, NULL, err);
117     object_unref(OBJECT(ioc));
118 }
119 
120 QIOChannelTLS *migration_tls_client_create(MigrationState *s,
121                                            QIOChannel *ioc,
122                                            const char *hostname,
123                                            Error **errp)
124 {
125     QCryptoTLSCreds *creds;
126 
127     creds = migration_tls_get_creds(
128         s, QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT, errp);
129     if (!creds) {
130         return NULL;
131     }
132 
133     const char *tls_hostname = migrate_tls_hostname();
134     if (tls_hostname && *tls_hostname) {
135         hostname = tls_hostname;
136     }
137 
138     return qio_channel_tls_new_client(ioc, creds, hostname, errp);
139 }
140 
141 void migration_tls_channel_connect(MigrationState *s,
142                                    QIOChannel *ioc,
143                                    const char *hostname,
144                                    Error **errp)
145 {
146     QIOChannelTLS *tioc;
147 
148     tioc = migration_tls_client_create(s, ioc, hostname, errp);
149     if (!tioc) {
150         return;
151     }
152 
153     /* Save hostname into MigrationState for handshake */
154     s->hostname = g_strdup(hostname);
155     trace_migration_tls_outgoing_handshake_start(hostname);
156     qio_channel_set_name(QIO_CHANNEL(tioc), "migration-tls-outgoing");
157     qio_channel_tls_handshake(tioc,
158                               migration_tls_outgoing_handshake,
159                               s,
160                               NULL,
161                               NULL);
162 }
163 
164 bool migrate_channel_requires_tls_upgrade(QIOChannel *ioc)
165 {
166     if (!migrate_tls()) {
167         return false;
168     }
169 
170     return !object_dynamic_cast(OBJECT(ioc), TYPE_QIO_CHANNEL_TLS);
171 }
172