s390-ccw: force diag 308 subcode to unsigned long
[qemu.git] / tests / test-io-channel-tls.c
blobbb88ee870f0e26a9f8f3052631c3261dc0507191
1 /*
2 * QEMU I/O channel TLS test
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.1 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
18 * <http://www.gnu.org/licenses/>.
20 * Author: Daniel P. Berrange <berrange@redhat.com>
24 #include "qemu/osdep.h"
26 #include "crypto-tls-x509-helpers.h"
27 #include "io/channel-tls.h"
28 #include "io/channel-socket.h"
29 #include "io-channel-helpers.h"
30 #include "crypto/init.h"
31 #include "crypto/tlscredsx509.h"
32 #include "qemu/acl.h"
33 #include "qom/object_interfaces.h"
35 #ifdef QCRYPTO_HAVE_TLS_TEST_SUPPORT
37 #define WORKDIR "tests/test-io-channel-tls-work/"
38 #define KEYFILE WORKDIR "key-ctx.pem"
40 struct QIOChannelTLSTestData {
41 const char *servercacrt;
42 const char *clientcacrt;
43 const char *servercrt;
44 const char *clientcrt;
45 bool expectServerFail;
46 bool expectClientFail;
47 const char *hostname;
48 const char *const *wildcards;
51 struct QIOChannelTLSHandshakeData {
52 bool finished;
53 bool failed;
56 static void test_tls_handshake_done(QIOTask *task,
57 gpointer opaque)
59 struct QIOChannelTLSHandshakeData *data = opaque;
61 data->finished = true;
62 data->failed = qio_task_propagate_error(task, NULL);
66 static QCryptoTLSCreds *test_tls_creds_create(QCryptoTLSCredsEndpoint endpoint,
67 const char *certdir,
68 Error **errp)
70 Object *parent = object_get_objects_root();
71 Object *creds = object_new_with_props(
72 TYPE_QCRYPTO_TLS_CREDS_X509,
73 parent,
74 (endpoint == QCRYPTO_TLS_CREDS_ENDPOINT_SERVER ?
75 "testtlscredsserver" : "testtlscredsclient"),
76 errp,
77 "endpoint", (endpoint == QCRYPTO_TLS_CREDS_ENDPOINT_SERVER ?
78 "server" : "client"),
79 "dir", certdir,
80 "verify-peer", "yes",
81 "priority", "NORMAL",
82 /* We skip initial sanity checks here because we
83 * want to make sure that problems are being
84 * detected at the TLS session validation stage,
85 * and the test-crypto-tlscreds test already
86 * validate the sanity check code.
88 "sanity-check", "no",
89 NULL
92 if (*errp) {
93 return NULL;
95 return QCRYPTO_TLS_CREDS(creds);
100 * This tests validation checking of peer certificates
102 * This is replicating the checks that are done for an
103 * active TLS session after handshake completes. To
104 * simulate that we create our TLS contexts, skipping
105 * sanity checks. When then get a socketpair, and
106 * initiate a TLS session across them. Finally do
107 * do actual cert validation tests
109 static void test_io_channel_tls(const void *opaque)
111 struct QIOChannelTLSTestData *data =
112 (struct QIOChannelTLSTestData *)opaque;
113 QCryptoTLSCreds *clientCreds;
114 QCryptoTLSCreds *serverCreds;
115 QIOChannelTLS *clientChanTLS;
116 QIOChannelTLS *serverChanTLS;
117 QIOChannelSocket *clientChanSock;
118 QIOChannelSocket *serverChanSock;
119 qemu_acl *acl;
120 const char * const *wildcards;
121 int channel[2];
122 struct QIOChannelTLSHandshakeData clientHandshake = { false, false };
123 struct QIOChannelTLSHandshakeData serverHandshake = { false, false };
124 Error *err = NULL;
125 QIOChannelTest *test;
126 GMainContext *mainloop;
128 /* We'll use this for our fake client-server connection */
129 g_assert(socketpair(AF_UNIX, SOCK_STREAM, 0, channel) == 0);
131 #define CLIENT_CERT_DIR "tests/test-io-channel-tls-client/"
132 #define SERVER_CERT_DIR "tests/test-io-channel-tls-server/"
133 mkdir(CLIENT_CERT_DIR, 0700);
134 mkdir(SERVER_CERT_DIR, 0700);
136 unlink(SERVER_CERT_DIR QCRYPTO_TLS_CREDS_X509_CA_CERT);
137 unlink(SERVER_CERT_DIR QCRYPTO_TLS_CREDS_X509_SERVER_CERT);
138 unlink(SERVER_CERT_DIR QCRYPTO_TLS_CREDS_X509_SERVER_KEY);
140 unlink(CLIENT_CERT_DIR QCRYPTO_TLS_CREDS_X509_CA_CERT);
141 unlink(CLIENT_CERT_DIR QCRYPTO_TLS_CREDS_X509_CLIENT_CERT);
142 unlink(CLIENT_CERT_DIR QCRYPTO_TLS_CREDS_X509_CLIENT_KEY);
144 g_assert(link(data->servercacrt,
145 SERVER_CERT_DIR QCRYPTO_TLS_CREDS_X509_CA_CERT) == 0);
146 g_assert(link(data->servercrt,
147 SERVER_CERT_DIR QCRYPTO_TLS_CREDS_X509_SERVER_CERT) == 0);
148 g_assert(link(KEYFILE,
149 SERVER_CERT_DIR QCRYPTO_TLS_CREDS_X509_SERVER_KEY) == 0);
151 g_assert(link(data->clientcacrt,
152 CLIENT_CERT_DIR QCRYPTO_TLS_CREDS_X509_CA_CERT) == 0);
153 g_assert(link(data->clientcrt,
154 CLIENT_CERT_DIR QCRYPTO_TLS_CREDS_X509_CLIENT_CERT) == 0);
155 g_assert(link(KEYFILE,
156 CLIENT_CERT_DIR QCRYPTO_TLS_CREDS_X509_CLIENT_KEY) == 0);
158 clientCreds = test_tls_creds_create(
159 QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT,
160 CLIENT_CERT_DIR,
161 &err);
162 g_assert(clientCreds != NULL);
164 serverCreds = test_tls_creds_create(
165 QCRYPTO_TLS_CREDS_ENDPOINT_SERVER,
166 SERVER_CERT_DIR,
167 &err);
168 g_assert(serverCreds != NULL);
170 acl = qemu_acl_init("channeltlsacl");
171 qemu_acl_reset(acl);
172 wildcards = data->wildcards;
173 while (wildcards && *wildcards) {
174 qemu_acl_append(acl, 0, *wildcards);
175 wildcards++;
178 clientChanSock = qio_channel_socket_new_fd(
179 channel[0], &err);
180 g_assert(clientChanSock != NULL);
181 serverChanSock = qio_channel_socket_new_fd(
182 channel[1], &err);
183 g_assert(serverChanSock != NULL);
186 * We have an evil loop to do the handshake in a single
187 * thread, so we need these non-blocking to avoid deadlock
188 * of ourselves
190 qio_channel_set_blocking(QIO_CHANNEL(clientChanSock), false, NULL);
191 qio_channel_set_blocking(QIO_CHANNEL(serverChanSock), false, NULL);
193 /* Now the real part of the test, setup the sessions */
194 clientChanTLS = qio_channel_tls_new_client(
195 QIO_CHANNEL(clientChanSock), clientCreds,
196 data->hostname, &err);
197 g_assert(clientChanTLS != NULL);
199 serverChanTLS = qio_channel_tls_new_server(
200 QIO_CHANNEL(serverChanSock), serverCreds,
201 "channeltlsacl", &err);
202 g_assert(serverChanTLS != NULL);
204 qio_channel_tls_handshake(clientChanTLS,
205 test_tls_handshake_done,
206 &clientHandshake,
207 NULL,
208 NULL);
209 qio_channel_tls_handshake(serverChanTLS,
210 test_tls_handshake_done,
211 &serverHandshake,
212 NULL,
213 NULL);
216 * Finally we loop around & around doing handshake on each
217 * session until we get an error, or the handshake completes.
218 * This relies on the socketpair being nonblocking to avoid
219 * deadlocking ourselves upon handshake
221 mainloop = g_main_context_default();
222 do {
223 g_main_context_iteration(mainloop, TRUE);
224 } while (!clientHandshake.finished ||
225 !serverHandshake.finished);
227 g_assert(clientHandshake.failed == data->expectClientFail);
228 g_assert(serverHandshake.failed == data->expectServerFail);
230 test = qio_channel_test_new();
231 qio_channel_test_run_threads(test, false,
232 QIO_CHANNEL(clientChanTLS),
233 QIO_CHANNEL(serverChanTLS));
234 qio_channel_test_validate(test);
236 test = qio_channel_test_new();
237 qio_channel_test_run_threads(test, true,
238 QIO_CHANNEL(clientChanTLS),
239 QIO_CHANNEL(serverChanTLS));
240 qio_channel_test_validate(test);
242 unlink(SERVER_CERT_DIR QCRYPTO_TLS_CREDS_X509_CA_CERT);
243 unlink(SERVER_CERT_DIR QCRYPTO_TLS_CREDS_X509_SERVER_CERT);
244 unlink(SERVER_CERT_DIR QCRYPTO_TLS_CREDS_X509_SERVER_KEY);
246 unlink(CLIENT_CERT_DIR QCRYPTO_TLS_CREDS_X509_CA_CERT);
247 unlink(CLIENT_CERT_DIR QCRYPTO_TLS_CREDS_X509_CLIENT_CERT);
248 unlink(CLIENT_CERT_DIR QCRYPTO_TLS_CREDS_X509_CLIENT_KEY);
250 rmdir(CLIENT_CERT_DIR);
251 rmdir(SERVER_CERT_DIR);
253 object_unparent(OBJECT(serverCreds));
254 object_unparent(OBJECT(clientCreds));
256 object_unref(OBJECT(serverChanTLS));
257 object_unref(OBJECT(clientChanTLS));
259 object_unref(OBJECT(serverChanSock));
260 object_unref(OBJECT(clientChanSock));
262 close(channel[0]);
263 close(channel[1]);
267 int main(int argc, char **argv)
269 int ret;
271 g_assert(qcrypto_init(NULL) == 0);
273 module_call_init(MODULE_INIT_QOM);
274 g_test_init(&argc, &argv, NULL);
275 setenv("GNUTLS_FORCE_FIPS_MODE", "2", 1);
277 mkdir(WORKDIR, 0700);
279 test_tls_init(KEYFILE);
281 # define TEST_CHANNEL(name, caCrt, \
282 serverCrt, clientCrt, \
283 expectServerFail, expectClientFail, \
284 hostname, wildcards) \
285 struct QIOChannelTLSTestData name = { \
286 caCrt, caCrt, serverCrt, clientCrt, \
287 expectServerFail, expectClientFail, \
288 hostname, wildcards \
289 }; \
290 g_test_add_data_func("/qio/channel/tls/" # name, \
291 &name, test_io_channel_tls);
293 /* A perfect CA, perfect client & perfect server */
295 /* Basic:CA:critical */
296 TLS_ROOT_REQ(cacertreq,
297 "UK", "qemu CA", NULL, NULL, NULL, NULL,
298 true, true, true,
299 true, true, GNUTLS_KEY_KEY_CERT_SIGN,
300 false, false, NULL, NULL,
301 0, 0);
302 TLS_CERT_REQ(servercertreq, cacertreq,
303 "UK", "qemu.org", NULL, NULL, NULL, NULL,
304 true, true, false,
305 true, true,
306 GNUTLS_KEY_DIGITAL_SIGNATURE | GNUTLS_KEY_KEY_ENCIPHERMENT,
307 true, true, GNUTLS_KP_TLS_WWW_SERVER, NULL,
308 0, 0);
309 TLS_CERT_REQ(clientcertreq, cacertreq,
310 "UK", "qemu", NULL, NULL, NULL, NULL,
311 true, true, false,
312 true, true,
313 GNUTLS_KEY_DIGITAL_SIGNATURE | GNUTLS_KEY_KEY_ENCIPHERMENT,
314 true, true, GNUTLS_KP_TLS_WWW_CLIENT, NULL,
315 0, 0);
317 const char *const wildcards[] = {
318 "C=UK,CN=qemu*",
319 NULL,
321 TEST_CHANNEL(basic, cacertreq.filename, servercertreq.filename,
322 clientcertreq.filename, false, false,
323 "qemu.org", wildcards);
325 ret = g_test_run();
327 test_tls_discard_cert(&clientcertreq);
328 test_tls_discard_cert(&servercertreq);
329 test_tls_discard_cert(&cacertreq);
331 test_tls_cleanup(KEYFILE);
332 rmdir(WORKDIR);
334 return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
337 #else /* ! QCRYPTO_HAVE_TLS_TEST_SUPPORT */
340 main(void)
342 return EXIT_SUCCESS;
345 #endif /* ! QCRYPTO_HAVE_TLS_TEST_SUPPORT */