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