2 * Copyright (C) 2015 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see
16 * <http://www.gnu.org/licenses/>.
18 * Author: Daniel P. Berrange <berrange@redhat.com>
21 #include "qemu/osdep.h"
23 #include "crypto-tls-x509-helpers.h"
24 #include "crypto/tlscredsx509.h"
25 #include "crypto/tlssession.h"
26 #include "qom/object_interfaces.h"
27 #include "qapi/error.h"
28 #include "qemu/sockets.h"
31 #ifdef QCRYPTO_HAVE_TLS_TEST_SUPPORT
33 #define WORKDIR "tests/test-crypto-tlssession-work/"
34 #define KEYFILE WORKDIR "key-ctx.pem"
36 struct QCryptoTLSSessionTestData
{
37 const char *servercacrt
;
38 const char *clientcacrt
;
39 const char *servercrt
;
40 const char *clientcrt
;
41 bool expectServerFail
;
42 bool expectClientFail
;
44 const char *const *wildcards
;
48 static ssize_t
testWrite(const char *buf
, size_t len
, void *opaque
)
52 return write(*fd
, buf
, len
);
55 static ssize_t
testRead(char *buf
, size_t len
, void *opaque
)
59 return read(*fd
, buf
, len
);
62 static QCryptoTLSCreds
*test_tls_creds_create(QCryptoTLSCredsEndpoint endpoint
,
67 Object
*parent
= object_get_objects_root();
68 Object
*creds
= object_new_with_props(
69 TYPE_QCRYPTO_TLS_CREDS_X509
,
71 (endpoint
== QCRYPTO_TLS_CREDS_ENDPOINT_SERVER
?
72 "testtlscredsserver" : "testtlscredsclient"),
74 "endpoint", (endpoint
== QCRYPTO_TLS_CREDS_ENDPOINT_SERVER
?
78 /* We skip initial sanity checks here because we
79 * want to make sure that problems are being
80 * detected at the TLS session validation stage,
81 * and the test-crypto-tlscreds test already
82 * validate the sanity check code.
89 error_propagate(errp
, err
);
92 return QCRYPTO_TLS_CREDS(creds
);
97 * This tests validation checking of peer certificates
99 * This is replicating the checks that are done for an
100 * active TLS session after handshake completes. To
101 * simulate that we create our TLS contexts, skipping
102 * sanity checks. We then get a socketpair, and
103 * initiate a TLS session across them. Finally do
104 * do actual cert validation tests
106 static void test_crypto_tls_session(const void *opaque
)
108 struct QCryptoTLSSessionTestData
*data
=
109 (struct QCryptoTLSSessionTestData
*)opaque
;
110 QCryptoTLSCreds
*clientCreds
;
111 QCryptoTLSCreds
*serverCreds
;
112 QCryptoTLSSession
*clientSess
= NULL
;
113 QCryptoTLSSession
*serverSess
= NULL
;
115 const char * const *wildcards
;
117 bool clientShake
= false;
118 bool serverShake
= false;
122 /* We'll use this for our fake client-server connection */
123 ret
= socketpair(AF_UNIX
, SOCK_STREAM
, 0, channel
);
127 * We have an evil loop to do the handshake in a single
128 * thread, so we need these non-blocking to avoid deadlock
131 qemu_set_nonblock(channel
[0]);
132 qemu_set_nonblock(channel
[1]);
134 #define CLIENT_CERT_DIR "tests/test-crypto-tlssession-client/"
135 #define SERVER_CERT_DIR "tests/test-crypto-tlssession-server/"
136 mkdir(CLIENT_CERT_DIR
, 0700);
137 mkdir(SERVER_CERT_DIR
, 0700);
139 unlink(SERVER_CERT_DIR QCRYPTO_TLS_CREDS_X509_CA_CERT
);
140 unlink(SERVER_CERT_DIR QCRYPTO_TLS_CREDS_X509_SERVER_CERT
);
141 unlink(SERVER_CERT_DIR QCRYPTO_TLS_CREDS_X509_SERVER_KEY
);
143 unlink(CLIENT_CERT_DIR QCRYPTO_TLS_CREDS_X509_CA_CERT
);
144 unlink(CLIENT_CERT_DIR QCRYPTO_TLS_CREDS_X509_CLIENT_CERT
);
145 unlink(CLIENT_CERT_DIR QCRYPTO_TLS_CREDS_X509_CLIENT_KEY
);
147 g_assert(link(data
->servercacrt
,
148 SERVER_CERT_DIR QCRYPTO_TLS_CREDS_X509_CA_CERT
) == 0);
149 g_assert(link(data
->servercrt
,
150 SERVER_CERT_DIR QCRYPTO_TLS_CREDS_X509_SERVER_CERT
) == 0);
151 g_assert(link(KEYFILE
,
152 SERVER_CERT_DIR QCRYPTO_TLS_CREDS_X509_SERVER_KEY
) == 0);
154 g_assert(link(data
->clientcacrt
,
155 CLIENT_CERT_DIR QCRYPTO_TLS_CREDS_X509_CA_CERT
) == 0);
156 g_assert(link(data
->clientcrt
,
157 CLIENT_CERT_DIR QCRYPTO_TLS_CREDS_X509_CLIENT_CERT
) == 0);
158 g_assert(link(KEYFILE
,
159 CLIENT_CERT_DIR QCRYPTO_TLS_CREDS_X509_CLIENT_KEY
) == 0);
161 clientCreds
= test_tls_creds_create(
162 QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT
,
165 g_assert(clientCreds
!= NULL
);
167 serverCreds
= test_tls_creds_create(
168 QCRYPTO_TLS_CREDS_ENDPOINT_SERVER
,
171 g_assert(serverCreds
!= NULL
);
173 acl
= qemu_acl_init("tlssessionacl");
175 wildcards
= data
->wildcards
;
176 while (wildcards
&& *wildcards
) {
177 qemu_acl_append(acl
, 0, *wildcards
);
181 /* Now the real part of the test, setup the sessions */
182 clientSess
= qcrypto_tls_session_new(
183 clientCreds
, data
->hostname
, NULL
,
184 QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT
, &err
);
185 serverSess
= qcrypto_tls_session_new(
187 data
->wildcards
? "tlssessionacl" : NULL
,
188 QCRYPTO_TLS_CREDS_ENDPOINT_SERVER
, &err
);
190 g_assert(clientSess
!= NULL
);
191 g_assert(serverSess
!= NULL
);
193 /* For handshake to work, we need to set the I/O callbacks
194 * to read/write over the socketpair
196 qcrypto_tls_session_set_callbacks(serverSess
,
199 qcrypto_tls_session_set_callbacks(clientSess
,
204 * Finally we loop around & around doing handshake on each
205 * session until we get an error, or the handshake completes.
206 * This relies on the socketpair being nonblocking to avoid
207 * deadlocking ourselves upon handshake
212 rv
= qcrypto_tls_session_handshake(serverSess
,
215 if (qcrypto_tls_session_get_handshake_status(serverSess
) ==
216 QCRYPTO_TLS_HANDSHAKE_COMPLETE
) {
221 rv
= qcrypto_tls_session_handshake(clientSess
,
224 if (qcrypto_tls_session_get_handshake_status(clientSess
) ==
225 QCRYPTO_TLS_HANDSHAKE_COMPLETE
) {
229 } while (!clientShake
&& !serverShake
);
232 /* Finally make sure the server validation does what
235 if (qcrypto_tls_session_check_credentials(serverSess
, &err
) < 0) {
236 g_assert(data
->expectServerFail
);
240 g_assert(!data
->expectServerFail
);
244 * And the same for the client validation check
246 if (qcrypto_tls_session_check_credentials(clientSess
, &err
) < 0) {
247 g_assert(data
->expectClientFail
);
251 g_assert(!data
->expectClientFail
);
254 unlink(SERVER_CERT_DIR QCRYPTO_TLS_CREDS_X509_CA_CERT
);
255 unlink(SERVER_CERT_DIR QCRYPTO_TLS_CREDS_X509_SERVER_CERT
);
256 unlink(SERVER_CERT_DIR QCRYPTO_TLS_CREDS_X509_SERVER_KEY
);
258 unlink(CLIENT_CERT_DIR QCRYPTO_TLS_CREDS_X509_CA_CERT
);
259 unlink(CLIENT_CERT_DIR QCRYPTO_TLS_CREDS_X509_CLIENT_CERT
);
260 unlink(CLIENT_CERT_DIR QCRYPTO_TLS_CREDS_X509_CLIENT_KEY
);
262 rmdir(CLIENT_CERT_DIR
);
263 rmdir(SERVER_CERT_DIR
);
265 object_unparent(OBJECT(serverCreds
));
266 object_unparent(OBJECT(clientCreds
));
268 qcrypto_tls_session_free(serverSess
);
269 qcrypto_tls_session_free(clientSess
);
276 int main(int argc
, char **argv
)
280 module_call_init(MODULE_INIT_QOM
);
281 g_test_init(&argc
, &argv
, NULL
);
282 setenv("GNUTLS_FORCE_FIPS_MODE", "2", 1);
284 mkdir(WORKDIR
, 0700);
286 test_tls_init(KEYFILE
);
288 # define TEST_SESS_REG(name, caCrt, \
289 serverCrt, clientCrt, \
290 expectServerFail, expectClientFail, \
291 hostname, wildcards) \
292 struct QCryptoTLSSessionTestData name = { \
293 caCrt, caCrt, serverCrt, clientCrt, \
294 expectServerFail, expectClientFail, \
295 hostname, wildcards \
297 g_test_add_data_func("/qcrypto/tlssession/" # name, \
298 &name, test_crypto_tls_session); \
301 # define TEST_SESS_REG_EXT(name, serverCaCrt, clientCaCrt, \
302 serverCrt, clientCrt, \
303 expectServerFail, expectClientFail, \
304 hostname, wildcards) \
305 struct QCryptoTLSSessionTestData name = { \
306 serverCaCrt, clientCaCrt, serverCrt, clientCrt, \
307 expectServerFail, expectClientFail, \
308 hostname, wildcards \
310 g_test_add_data_func("/qcrypto/tlssession/" # name, \
311 &name, test_crypto_tls_session); \
313 /* A perfect CA, perfect client & perfect server */
315 /* Basic:CA:critical */
316 TLS_ROOT_REQ(cacertreq
,
317 "UK", "qemu CA", NULL
, NULL
, NULL
, NULL
,
319 true, true, GNUTLS_KEY_KEY_CERT_SIGN
,
320 false, false, NULL
, NULL
,
323 TLS_ROOT_REQ(altcacertreq
,
324 "UK", "qemu CA 1", NULL
, NULL
, NULL
, NULL
,
327 false, false, NULL
, NULL
,
330 TLS_CERT_REQ(servercertreq
, cacertreq
,
331 "UK", "qemu.org", NULL
, NULL
, NULL
, NULL
,
334 GNUTLS_KEY_DIGITAL_SIGNATURE
| GNUTLS_KEY_KEY_ENCIPHERMENT
,
335 true, true, GNUTLS_KP_TLS_WWW_SERVER
, NULL
,
337 TLS_CERT_REQ(clientcertreq
, cacertreq
,
338 "UK", "qemu", NULL
, NULL
, NULL
, NULL
,
341 GNUTLS_KEY_DIGITAL_SIGNATURE
| GNUTLS_KEY_KEY_ENCIPHERMENT
,
342 true, true, GNUTLS_KP_TLS_WWW_CLIENT
, NULL
,
345 TLS_CERT_REQ(clientcertaltreq
, altcacertreq
,
346 "UK", "qemu", NULL
, NULL
, NULL
, NULL
,
349 GNUTLS_KEY_DIGITAL_SIGNATURE
| GNUTLS_KEY_KEY_ENCIPHERMENT
,
350 true, true, GNUTLS_KP_TLS_WWW_CLIENT
, NULL
,
353 TEST_SESS_REG(basicca
, cacertreq
.filename
,
354 servercertreq
.filename
, clientcertreq
.filename
,
355 false, false, "qemu.org", NULL
);
356 TEST_SESS_REG_EXT(differentca
, cacertreq
.filename
,
357 altcacertreq
.filename
, servercertreq
.filename
,
358 clientcertaltreq
.filename
, true, true, "qemu.org", NULL
);
361 /* When an altname is set, the CN is ignored, so it must be duplicated
362 * as an altname for it to match */
363 TLS_CERT_REQ(servercertalt1req
, cacertreq
,
364 "UK", "qemu.org", "www.qemu.org", "qemu.org",
365 "192.168.122.1", "fec0::dead:beaf",
368 GNUTLS_KEY_DIGITAL_SIGNATURE
| GNUTLS_KEY_KEY_ENCIPHERMENT
,
369 true, true, GNUTLS_KP_TLS_WWW_SERVER
, NULL
,
371 /* This intentionally doesn't replicate */
372 TLS_CERT_REQ(servercertalt2req
, cacertreq
,
373 "UK", "qemu.org", "www.qemu.org", "wiki.qemu.org",
374 "192.168.122.1", "fec0::dead:beaf",
377 GNUTLS_KEY_DIGITAL_SIGNATURE
| GNUTLS_KEY_KEY_ENCIPHERMENT
,
378 true, true, GNUTLS_KP_TLS_WWW_SERVER
, NULL
,
381 TEST_SESS_REG(altname1
, cacertreq
.filename
,
382 servercertalt1req
.filename
, clientcertreq
.filename
,
383 false, false, "qemu.org", NULL
);
384 TEST_SESS_REG(altname2
, cacertreq
.filename
,
385 servercertalt1req
.filename
, clientcertreq
.filename
,
386 false, false, "www.qemu.org", NULL
);
387 TEST_SESS_REG(altname3
, cacertreq
.filename
,
388 servercertalt1req
.filename
, clientcertreq
.filename
,
389 false, true, "wiki.qemu.org", NULL
);
391 TEST_SESS_REG(altname4
, cacertreq
.filename
,
392 servercertalt2req
.filename
, clientcertreq
.filename
,
393 false, true, "qemu.org", NULL
);
394 TEST_SESS_REG(altname5
, cacertreq
.filename
,
395 servercertalt2req
.filename
, clientcertreq
.filename
,
396 false, false, "www.qemu.org", NULL
);
397 TEST_SESS_REG(altname6
, cacertreq
.filename
,
398 servercertalt2req
.filename
, clientcertreq
.filename
,
399 false, false, "wiki.qemu.org", NULL
);
401 const char *const wildcards1
[] = {
405 const char *const wildcards2
[] = {
409 const char *const wildcards3
[] = {
414 const char *const wildcards4
[] = {
418 const char *const wildcards5
[] = {
422 const char *const wildcards6
[] = {
427 TEST_SESS_REG(wildcard1
, cacertreq
.filename
,
428 servercertreq
.filename
, clientcertreq
.filename
,
429 true, false, "qemu.org", wildcards1
);
430 TEST_SESS_REG(wildcard2
, cacertreq
.filename
,
431 servercertreq
.filename
, clientcertreq
.filename
,
432 false, false, "qemu.org", wildcards2
);
433 TEST_SESS_REG(wildcard3
, cacertreq
.filename
,
434 servercertreq
.filename
, clientcertreq
.filename
,
435 false, false, "qemu.org", wildcards3
);
436 TEST_SESS_REG(wildcard4
, cacertreq
.filename
,
437 servercertreq
.filename
, clientcertreq
.filename
,
438 true, false, "qemu.org", wildcards4
);
439 TEST_SESS_REG(wildcard5
, cacertreq
.filename
,
440 servercertreq
.filename
, clientcertreq
.filename
,
441 false, false, "qemu.org", wildcards5
);
442 TEST_SESS_REG(wildcard6
, cacertreq
.filename
,
443 servercertreq
.filename
, clientcertreq
.filename
,
444 false, false, "qemu.org", wildcards6
);
446 TLS_ROOT_REQ(cacertrootreq
,
447 "UK", "qemu root", NULL
, NULL
, NULL
, NULL
,
449 true, true, GNUTLS_KEY_KEY_CERT_SIGN
,
450 false, false, NULL
, NULL
,
452 TLS_CERT_REQ(cacertlevel1areq
, cacertrootreq
,
453 "UK", "qemu level 1a", NULL
, NULL
, NULL
, NULL
,
455 true, true, GNUTLS_KEY_KEY_CERT_SIGN
,
456 false, false, NULL
, NULL
,
458 TLS_CERT_REQ(cacertlevel1breq
, cacertrootreq
,
459 "UK", "qemu level 1b", NULL
, NULL
, NULL
, NULL
,
461 true, true, GNUTLS_KEY_KEY_CERT_SIGN
,
462 false, false, NULL
, NULL
,
464 TLS_CERT_REQ(cacertlevel2areq
, cacertlevel1areq
,
465 "UK", "qemu level 2a", NULL
, NULL
, NULL
, NULL
,
467 true, true, GNUTLS_KEY_KEY_CERT_SIGN
,
468 false, false, NULL
, NULL
,
470 TLS_CERT_REQ(servercertlevel3areq
, cacertlevel2areq
,
471 "UK", "qemu.org", NULL
, NULL
, NULL
, NULL
,
474 GNUTLS_KEY_DIGITAL_SIGNATURE
| GNUTLS_KEY_KEY_ENCIPHERMENT
,
475 true, true, GNUTLS_KP_TLS_WWW_SERVER
, NULL
,
477 TLS_CERT_REQ(clientcertlevel2breq
, cacertlevel1breq
,
478 "UK", "qemu client level 2b", NULL
, NULL
, NULL
, NULL
,
481 GNUTLS_KEY_DIGITAL_SIGNATURE
| GNUTLS_KEY_KEY_ENCIPHERMENT
,
482 true, true, GNUTLS_KP_TLS_WWW_CLIENT
, NULL
,
485 gnutls_x509_crt_t certchain
[] = {
487 cacertlevel1areq
.crt
,
488 cacertlevel1breq
.crt
,
489 cacertlevel2areq
.crt
,
492 test_tls_write_cert_chain(WORKDIR
"cacertchain-sess.pem",
494 G_N_ELEMENTS(certchain
));
496 TEST_SESS_REG(cachain
, WORKDIR
"cacertchain-sess.pem",
497 servercertlevel3areq
.filename
, clientcertlevel2breq
.filename
,
498 false, false, "qemu.org", NULL
);
502 test_tls_discard_cert(&clientcertreq
);
503 test_tls_discard_cert(&clientcertaltreq
);
505 test_tls_discard_cert(&servercertreq
);
506 test_tls_discard_cert(&servercertalt1req
);
507 test_tls_discard_cert(&servercertalt2req
);
509 test_tls_discard_cert(&cacertreq
);
510 test_tls_discard_cert(&altcacertreq
);
512 test_tls_discard_cert(&cacertrootreq
);
513 test_tls_discard_cert(&cacertlevel1areq
);
514 test_tls_discard_cert(&cacertlevel1breq
);
515 test_tls_discard_cert(&cacertlevel2areq
);
516 test_tls_discard_cert(&servercertlevel3areq
);
517 test_tls_discard_cert(&clientcertlevel2breq
);
518 unlink(WORKDIR
"cacertchain-sess.pem");
520 test_tls_cleanup(KEYFILE
);
523 return ret
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;
526 #else /* ! QCRYPTO_HAVE_TLS_TEST_SUPPORT */
534 #endif /* ! QCRYPTO_HAVE_TLS_TEST_SUPPORT */