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-tls-psk-helpers.h"
25 #include "crypto/tlscredsx509.h"
26 #include "crypto/tlscredspsk.h"
27 #include "crypto/tlssession.h"
28 #include "qom/object_interfaces.h"
29 #include "qapi/error.h"
30 #include "qemu/sockets.h"
33 #ifdef QCRYPTO_HAVE_TLS_TEST_SUPPORT
35 #define WORKDIR "tests/test-crypto-tlssession-work/"
36 #define PSKFILE WORKDIR "keys.psk"
37 #define KEYFILE WORKDIR "key-ctx.pem"
39 static ssize_t
testWrite(const char *buf
, size_t len
, void *opaque
)
43 return write(*fd
, buf
, len
);
46 static ssize_t
testRead(char *buf
, size_t len
, void *opaque
)
50 return read(*fd
, buf
, len
);
53 static QCryptoTLSCreds
*test_tls_creds_psk_create(
54 QCryptoTLSCredsEndpoint endpoint
,
59 Object
*parent
= object_get_objects_root();
60 Object
*creds
= object_new_with_props(
61 TYPE_QCRYPTO_TLS_CREDS_PSK
,
63 (endpoint
== QCRYPTO_TLS_CREDS_ENDPOINT_SERVER
?
64 "testtlscredsserver" : "testtlscredsclient"),
66 "endpoint", (endpoint
== QCRYPTO_TLS_CREDS_ENDPOINT_SERVER
?
74 error_propagate(errp
, err
);
77 return QCRYPTO_TLS_CREDS(creds
);
81 static void test_crypto_tls_session_psk(void)
83 QCryptoTLSCreds
*clientCreds
;
84 QCryptoTLSCreds
*serverCreds
;
85 QCryptoTLSSession
*clientSess
= NULL
;
86 QCryptoTLSSession
*serverSess
= NULL
;
88 bool clientShake
= false;
89 bool serverShake
= false;
93 /* We'll use this for our fake client-server connection */
94 ret
= socketpair(AF_UNIX
, SOCK_STREAM
, 0, channel
);
98 * We have an evil loop to do the handshake in a single
99 * thread, so we need these non-blocking to avoid deadlock
102 qemu_set_nonblock(channel
[0]);
103 qemu_set_nonblock(channel
[1]);
105 clientCreds
= test_tls_creds_psk_create(
106 QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT
,
109 g_assert(clientCreds
!= NULL
);
111 serverCreds
= test_tls_creds_psk_create(
112 QCRYPTO_TLS_CREDS_ENDPOINT_SERVER
,
115 g_assert(serverCreds
!= NULL
);
117 /* Now the real part of the test, setup the sessions */
118 clientSess
= qcrypto_tls_session_new(
119 clientCreds
, NULL
, NULL
,
120 QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT
, &err
);
121 serverSess
= qcrypto_tls_session_new(
122 serverCreds
, NULL
, NULL
,
123 QCRYPTO_TLS_CREDS_ENDPOINT_SERVER
, &err
);
125 g_assert(clientSess
!= NULL
);
126 g_assert(serverSess
!= NULL
);
128 /* For handshake to work, we need to set the I/O callbacks
129 * to read/write over the socketpair
131 qcrypto_tls_session_set_callbacks(serverSess
,
134 qcrypto_tls_session_set_callbacks(clientSess
,
139 * Finally we loop around & around doing handshake on each
140 * session until we get an error, or the handshake completes.
141 * This relies on the socketpair being nonblocking to avoid
142 * deadlocking ourselves upon handshake
147 rv
= qcrypto_tls_session_handshake(serverSess
,
150 if (qcrypto_tls_session_get_handshake_status(serverSess
) ==
151 QCRYPTO_TLS_HANDSHAKE_COMPLETE
) {
156 rv
= qcrypto_tls_session_handshake(clientSess
,
159 if (qcrypto_tls_session_get_handshake_status(clientSess
) ==
160 QCRYPTO_TLS_HANDSHAKE_COMPLETE
) {
164 } while (!clientShake
&& !serverShake
);
167 /* Finally make sure the server & client validation is successful. */
168 g_assert(qcrypto_tls_session_check_credentials(serverSess
, &err
) == 0);
169 g_assert(qcrypto_tls_session_check_credentials(clientSess
, &err
) == 0);
171 object_unparent(OBJECT(serverCreds
));
172 object_unparent(OBJECT(clientCreds
));
174 qcrypto_tls_session_free(serverSess
);
175 qcrypto_tls_session_free(clientSess
);
182 struct QCryptoTLSSessionTestData
{
183 const char *servercacrt
;
184 const char *clientcacrt
;
185 const char *servercrt
;
186 const char *clientcrt
;
187 bool expectServerFail
;
188 bool expectClientFail
;
189 const char *hostname
;
190 const char *const *wildcards
;
193 static QCryptoTLSCreds
*test_tls_creds_x509_create(
194 QCryptoTLSCredsEndpoint endpoint
,
199 Object
*parent
= object_get_objects_root();
200 Object
*creds
= object_new_with_props(
201 TYPE_QCRYPTO_TLS_CREDS_X509
,
203 (endpoint
== QCRYPTO_TLS_CREDS_ENDPOINT_SERVER
?
204 "testtlscredsserver" : "testtlscredsclient"),
206 "endpoint", (endpoint
== QCRYPTO_TLS_CREDS_ENDPOINT_SERVER
?
207 "server" : "client"),
209 "verify-peer", "yes",
210 "priority", "NORMAL",
211 /* We skip initial sanity checks here because we
212 * want to make sure that problems are being
213 * detected at the TLS session validation stage,
214 * and the test-crypto-tlscreds test already
215 * validate the sanity check code.
217 "sanity-check", "no",
222 error_propagate(errp
, err
);
225 return QCRYPTO_TLS_CREDS(creds
);
230 * This tests validation checking of peer certificates
232 * This is replicating the checks that are done for an
233 * active TLS session after handshake completes. To
234 * simulate that we create our TLS contexts, skipping
235 * sanity checks. We then get a socketpair, and
236 * initiate a TLS session across them. Finally do
237 * do actual cert validation tests
239 static void test_crypto_tls_session_x509(const void *opaque
)
241 struct QCryptoTLSSessionTestData
*data
=
242 (struct QCryptoTLSSessionTestData
*)opaque
;
243 QCryptoTLSCreds
*clientCreds
;
244 QCryptoTLSCreds
*serverCreds
;
245 QCryptoTLSSession
*clientSess
= NULL
;
246 QCryptoTLSSession
*serverSess
= NULL
;
248 const char * const *wildcards
;
250 bool clientShake
= false;
251 bool serverShake
= false;
255 /* We'll use this for our fake client-server connection */
256 ret
= socketpair(AF_UNIX
, SOCK_STREAM
, 0, channel
);
260 * We have an evil loop to do the handshake in a single
261 * thread, so we need these non-blocking to avoid deadlock
264 qemu_set_nonblock(channel
[0]);
265 qemu_set_nonblock(channel
[1]);
267 #define CLIENT_CERT_DIR "tests/test-crypto-tlssession-client/"
268 #define SERVER_CERT_DIR "tests/test-crypto-tlssession-server/"
269 mkdir(CLIENT_CERT_DIR
, 0700);
270 mkdir(SERVER_CERT_DIR
, 0700);
272 unlink(SERVER_CERT_DIR QCRYPTO_TLS_CREDS_X509_CA_CERT
);
273 unlink(SERVER_CERT_DIR QCRYPTO_TLS_CREDS_X509_SERVER_CERT
);
274 unlink(SERVER_CERT_DIR QCRYPTO_TLS_CREDS_X509_SERVER_KEY
);
276 unlink(CLIENT_CERT_DIR QCRYPTO_TLS_CREDS_X509_CA_CERT
);
277 unlink(CLIENT_CERT_DIR QCRYPTO_TLS_CREDS_X509_CLIENT_CERT
);
278 unlink(CLIENT_CERT_DIR QCRYPTO_TLS_CREDS_X509_CLIENT_KEY
);
280 g_assert(link(data
->servercacrt
,
281 SERVER_CERT_DIR QCRYPTO_TLS_CREDS_X509_CA_CERT
) == 0);
282 g_assert(link(data
->servercrt
,
283 SERVER_CERT_DIR QCRYPTO_TLS_CREDS_X509_SERVER_CERT
) == 0);
284 g_assert(link(KEYFILE
,
285 SERVER_CERT_DIR QCRYPTO_TLS_CREDS_X509_SERVER_KEY
) == 0);
287 g_assert(link(data
->clientcacrt
,
288 CLIENT_CERT_DIR QCRYPTO_TLS_CREDS_X509_CA_CERT
) == 0);
289 g_assert(link(data
->clientcrt
,
290 CLIENT_CERT_DIR QCRYPTO_TLS_CREDS_X509_CLIENT_CERT
) == 0);
291 g_assert(link(KEYFILE
,
292 CLIENT_CERT_DIR QCRYPTO_TLS_CREDS_X509_CLIENT_KEY
) == 0);
294 clientCreds
= test_tls_creds_x509_create(
295 QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT
,
298 g_assert(clientCreds
!= NULL
);
300 serverCreds
= test_tls_creds_x509_create(
301 QCRYPTO_TLS_CREDS_ENDPOINT_SERVER
,
304 g_assert(serverCreds
!= NULL
);
306 acl
= qemu_acl_init("tlssessionacl");
308 wildcards
= data
->wildcards
;
309 while (wildcards
&& *wildcards
) {
310 qemu_acl_append(acl
, 0, *wildcards
);
314 /* Now the real part of the test, setup the sessions */
315 clientSess
= qcrypto_tls_session_new(
316 clientCreds
, data
->hostname
, NULL
,
317 QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT
, &err
);
318 serverSess
= qcrypto_tls_session_new(
320 data
->wildcards
? "tlssessionacl" : NULL
,
321 QCRYPTO_TLS_CREDS_ENDPOINT_SERVER
, &err
);
323 g_assert(clientSess
!= NULL
);
324 g_assert(serverSess
!= NULL
);
326 /* For handshake to work, we need to set the I/O callbacks
327 * to read/write over the socketpair
329 qcrypto_tls_session_set_callbacks(serverSess
,
332 qcrypto_tls_session_set_callbacks(clientSess
,
337 * Finally we loop around & around doing handshake on each
338 * session until we get an error, or the handshake completes.
339 * This relies on the socketpair being nonblocking to avoid
340 * deadlocking ourselves upon handshake
345 rv
= qcrypto_tls_session_handshake(serverSess
,
348 if (qcrypto_tls_session_get_handshake_status(serverSess
) ==
349 QCRYPTO_TLS_HANDSHAKE_COMPLETE
) {
354 rv
= qcrypto_tls_session_handshake(clientSess
,
357 if (qcrypto_tls_session_get_handshake_status(clientSess
) ==
358 QCRYPTO_TLS_HANDSHAKE_COMPLETE
) {
362 } while (!clientShake
&& !serverShake
);
365 /* Finally make sure the server validation does what
368 if (qcrypto_tls_session_check_credentials(serverSess
, &err
) < 0) {
369 g_assert(data
->expectServerFail
);
373 g_assert(!data
->expectServerFail
);
377 * And the same for the client validation check
379 if (qcrypto_tls_session_check_credentials(clientSess
, &err
) < 0) {
380 g_assert(data
->expectClientFail
);
384 g_assert(!data
->expectClientFail
);
387 unlink(SERVER_CERT_DIR QCRYPTO_TLS_CREDS_X509_CA_CERT
);
388 unlink(SERVER_CERT_DIR QCRYPTO_TLS_CREDS_X509_SERVER_CERT
);
389 unlink(SERVER_CERT_DIR QCRYPTO_TLS_CREDS_X509_SERVER_KEY
);
391 unlink(CLIENT_CERT_DIR QCRYPTO_TLS_CREDS_X509_CA_CERT
);
392 unlink(CLIENT_CERT_DIR QCRYPTO_TLS_CREDS_X509_CLIENT_CERT
);
393 unlink(CLIENT_CERT_DIR QCRYPTO_TLS_CREDS_X509_CLIENT_KEY
);
395 rmdir(CLIENT_CERT_DIR
);
396 rmdir(SERVER_CERT_DIR
);
398 object_unparent(OBJECT(serverCreds
));
399 object_unparent(OBJECT(clientCreds
));
401 qcrypto_tls_session_free(serverSess
);
402 qcrypto_tls_session_free(clientSess
);
409 int main(int argc
, char **argv
)
413 module_call_init(MODULE_INIT_QOM
);
414 g_test_init(&argc
, &argv
, NULL
);
415 setenv("GNUTLS_FORCE_FIPS_MODE", "2", 1);
417 mkdir(WORKDIR
, 0700);
419 test_tls_init(KEYFILE
);
420 test_tls_psk_init(PSKFILE
);
422 /* Simple initial test using Pre-Shared Keys. */
423 g_test_add_func("/qcrypto/tlssession/psk",
424 test_crypto_tls_session_psk
);
426 /* More complex tests using X.509 certificates. */
427 # define TEST_SESS_REG(name, caCrt, \
428 serverCrt, clientCrt, \
429 expectServerFail, expectClientFail, \
430 hostname, wildcards) \
431 struct QCryptoTLSSessionTestData name = { \
432 caCrt, caCrt, serverCrt, clientCrt, \
433 expectServerFail, expectClientFail, \
434 hostname, wildcards \
436 g_test_add_data_func("/qcrypto/tlssession/" # name, \
437 &name, test_crypto_tls_session_x509); \
440 # define TEST_SESS_REG_EXT(name, serverCaCrt, clientCaCrt, \
441 serverCrt, clientCrt, \
442 expectServerFail, expectClientFail, \
443 hostname, wildcards) \
444 struct QCryptoTLSSessionTestData name = { \
445 serverCaCrt, clientCaCrt, serverCrt, clientCrt, \
446 expectServerFail, expectClientFail, \
447 hostname, wildcards \
449 g_test_add_data_func("/qcrypto/tlssession/" # name, \
450 &name, test_crypto_tls_session_x509); \
452 /* A perfect CA, perfect client & perfect server */
454 /* Basic:CA:critical */
455 TLS_ROOT_REQ(cacertreq
,
456 "UK", "qemu CA", NULL
, NULL
, NULL
, NULL
,
458 true, true, GNUTLS_KEY_KEY_CERT_SIGN
,
459 false, false, NULL
, NULL
,
462 TLS_ROOT_REQ(altcacertreq
,
463 "UK", "qemu CA 1", NULL
, NULL
, NULL
, NULL
,
466 false, false, NULL
, NULL
,
469 TLS_CERT_REQ(servercertreq
, cacertreq
,
470 "UK", "qemu.org", NULL
, NULL
, NULL
, NULL
,
473 GNUTLS_KEY_DIGITAL_SIGNATURE
| GNUTLS_KEY_KEY_ENCIPHERMENT
,
474 true, true, GNUTLS_KP_TLS_WWW_SERVER
, NULL
,
476 TLS_CERT_REQ(clientcertreq
, cacertreq
,
477 "UK", "qemu", NULL
, NULL
, NULL
, NULL
,
480 GNUTLS_KEY_DIGITAL_SIGNATURE
| GNUTLS_KEY_KEY_ENCIPHERMENT
,
481 true, true, GNUTLS_KP_TLS_WWW_CLIENT
, NULL
,
484 TLS_CERT_REQ(clientcertaltreq
, altcacertreq
,
485 "UK", "qemu", NULL
, NULL
, NULL
, NULL
,
488 GNUTLS_KEY_DIGITAL_SIGNATURE
| GNUTLS_KEY_KEY_ENCIPHERMENT
,
489 true, true, GNUTLS_KP_TLS_WWW_CLIENT
, NULL
,
492 TEST_SESS_REG(basicca
, cacertreq
.filename
,
493 servercertreq
.filename
, clientcertreq
.filename
,
494 false, false, "qemu.org", NULL
);
495 TEST_SESS_REG_EXT(differentca
, cacertreq
.filename
,
496 altcacertreq
.filename
, servercertreq
.filename
,
497 clientcertaltreq
.filename
, true, true, "qemu.org", NULL
);
500 /* When an altname is set, the CN is ignored, so it must be duplicated
501 * as an altname for it to match */
502 TLS_CERT_REQ(servercertalt1req
, cacertreq
,
503 "UK", "qemu.org", "www.qemu.org", "qemu.org",
504 "192.168.122.1", "fec0::dead:beaf",
507 GNUTLS_KEY_DIGITAL_SIGNATURE
| GNUTLS_KEY_KEY_ENCIPHERMENT
,
508 true, true, GNUTLS_KP_TLS_WWW_SERVER
, NULL
,
510 /* This intentionally doesn't replicate */
511 TLS_CERT_REQ(servercertalt2req
, cacertreq
,
512 "UK", "qemu.org", "www.qemu.org", "wiki.qemu.org",
513 "192.168.122.1", "fec0::dead:beaf",
516 GNUTLS_KEY_DIGITAL_SIGNATURE
| GNUTLS_KEY_KEY_ENCIPHERMENT
,
517 true, true, GNUTLS_KP_TLS_WWW_SERVER
, NULL
,
520 TEST_SESS_REG(altname1
, cacertreq
.filename
,
521 servercertalt1req
.filename
, clientcertreq
.filename
,
522 false, false, "qemu.org", NULL
);
523 TEST_SESS_REG(altname2
, cacertreq
.filename
,
524 servercertalt1req
.filename
, clientcertreq
.filename
,
525 false, false, "www.qemu.org", NULL
);
526 TEST_SESS_REG(altname3
, cacertreq
.filename
,
527 servercertalt1req
.filename
, clientcertreq
.filename
,
528 false, true, "wiki.qemu.org", NULL
);
530 TEST_SESS_REG(altname4
, cacertreq
.filename
,
531 servercertalt2req
.filename
, clientcertreq
.filename
,
532 false, true, "qemu.org", NULL
);
533 TEST_SESS_REG(altname5
, cacertreq
.filename
,
534 servercertalt2req
.filename
, clientcertreq
.filename
,
535 false, false, "www.qemu.org", NULL
);
536 TEST_SESS_REG(altname6
, cacertreq
.filename
,
537 servercertalt2req
.filename
, clientcertreq
.filename
,
538 false, false, "wiki.qemu.org", NULL
);
540 const char *const wildcards1
[] = {
544 const char *const wildcards2
[] = {
548 const char *const wildcards3
[] = {
553 const char *const wildcards4
[] = {
557 const char *const wildcards5
[] = {
561 const char *const wildcards6
[] = {
566 TEST_SESS_REG(wildcard1
, cacertreq
.filename
,
567 servercertreq
.filename
, clientcertreq
.filename
,
568 true, false, "qemu.org", wildcards1
);
569 TEST_SESS_REG(wildcard2
, cacertreq
.filename
,
570 servercertreq
.filename
, clientcertreq
.filename
,
571 false, false, "qemu.org", wildcards2
);
572 TEST_SESS_REG(wildcard3
, cacertreq
.filename
,
573 servercertreq
.filename
, clientcertreq
.filename
,
574 false, false, "qemu.org", wildcards3
);
575 TEST_SESS_REG(wildcard4
, cacertreq
.filename
,
576 servercertreq
.filename
, clientcertreq
.filename
,
577 true, false, "qemu.org", wildcards4
);
578 TEST_SESS_REG(wildcard5
, cacertreq
.filename
,
579 servercertreq
.filename
, clientcertreq
.filename
,
580 false, false, "qemu.org", wildcards5
);
581 TEST_SESS_REG(wildcard6
, cacertreq
.filename
,
582 servercertreq
.filename
, clientcertreq
.filename
,
583 false, false, "qemu.org", wildcards6
);
585 TLS_ROOT_REQ(cacertrootreq
,
586 "UK", "qemu root", NULL
, NULL
, NULL
, NULL
,
588 true, true, GNUTLS_KEY_KEY_CERT_SIGN
,
589 false, false, NULL
, NULL
,
591 TLS_CERT_REQ(cacertlevel1areq
, cacertrootreq
,
592 "UK", "qemu level 1a", NULL
, NULL
, NULL
, NULL
,
594 true, true, GNUTLS_KEY_KEY_CERT_SIGN
,
595 false, false, NULL
, NULL
,
597 TLS_CERT_REQ(cacertlevel1breq
, cacertrootreq
,
598 "UK", "qemu level 1b", NULL
, NULL
, NULL
, NULL
,
600 true, true, GNUTLS_KEY_KEY_CERT_SIGN
,
601 false, false, NULL
, NULL
,
603 TLS_CERT_REQ(cacertlevel2areq
, cacertlevel1areq
,
604 "UK", "qemu level 2a", NULL
, NULL
, NULL
, NULL
,
606 true, true, GNUTLS_KEY_KEY_CERT_SIGN
,
607 false, false, NULL
, NULL
,
609 TLS_CERT_REQ(servercertlevel3areq
, cacertlevel2areq
,
610 "UK", "qemu.org", NULL
, NULL
, NULL
, NULL
,
613 GNUTLS_KEY_DIGITAL_SIGNATURE
| GNUTLS_KEY_KEY_ENCIPHERMENT
,
614 true, true, GNUTLS_KP_TLS_WWW_SERVER
, NULL
,
616 TLS_CERT_REQ(clientcertlevel2breq
, cacertlevel1breq
,
617 "UK", "qemu client level 2b", NULL
, NULL
, NULL
, NULL
,
620 GNUTLS_KEY_DIGITAL_SIGNATURE
| GNUTLS_KEY_KEY_ENCIPHERMENT
,
621 true, true, GNUTLS_KP_TLS_WWW_CLIENT
, NULL
,
624 gnutls_x509_crt_t certchain
[] = {
626 cacertlevel1areq
.crt
,
627 cacertlevel1breq
.crt
,
628 cacertlevel2areq
.crt
,
631 test_tls_write_cert_chain(WORKDIR
"cacertchain-sess.pem",
633 G_N_ELEMENTS(certchain
));
635 TEST_SESS_REG(cachain
, WORKDIR
"cacertchain-sess.pem",
636 servercertlevel3areq
.filename
, clientcertlevel2breq
.filename
,
637 false, false, "qemu.org", NULL
);
641 test_tls_discard_cert(&clientcertreq
);
642 test_tls_discard_cert(&clientcertaltreq
);
644 test_tls_discard_cert(&servercertreq
);
645 test_tls_discard_cert(&servercertalt1req
);
646 test_tls_discard_cert(&servercertalt2req
);
648 test_tls_discard_cert(&cacertreq
);
649 test_tls_discard_cert(&altcacertreq
);
651 test_tls_discard_cert(&cacertrootreq
);
652 test_tls_discard_cert(&cacertlevel1areq
);
653 test_tls_discard_cert(&cacertlevel1breq
);
654 test_tls_discard_cert(&cacertlevel2areq
);
655 test_tls_discard_cert(&servercertlevel3areq
);
656 test_tls_discard_cert(&clientcertlevel2breq
);
657 unlink(WORKDIR
"cacertchain-sess.pem");
659 test_tls_psk_cleanup(PSKFILE
);
660 test_tls_cleanup(KEYFILE
);
663 return ret
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;
666 #else /* ! QCRYPTO_HAVE_TLS_TEST_SUPPORT */
674 #endif /* ! QCRYPTO_HAVE_TLS_TEST_SUPPORT */