|
|
43fe83 |
From 0ef1dcbe3a13878f4a2c22ea38d113e92424be70 Mon Sep 17 00:00:00 2001
|
|
|
43fe83 |
Message-Id: <0ef1dcbe3a13878f4a2c22ea38d113e92424be70.1377873637.git.jdenemar@redhat.com>
|
|
|
43fe83 |
From: "Daniel P. Berrange" <berrange@redhat.com>
|
|
|
43fe83 |
Date: Tue, 13 Aug 2013 11:32:45 +0100
|
|
|
43fe83 |
Subject: [PATCH] Split TLS test into two separate tests
|
|
|
43fe83 |
|
|
|
43fe83 |
For https://bugzilla.redhat.com/show_bug.cgi?id=994158
|
|
|
43fe83 |
|
|
|
43fe83 |
The virnettlscontexttest.c tests both virNetTLSContext
|
|
|
43fe83 |
and virNetTLSSession functionality. Split into two
|
|
|
43fe83 |
separate tests, to make the code size more manageable
|
|
|
43fe83 |
|
|
|
43fe83 |
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
|
|
|
43fe83 |
(cherry picked from commit 236da82dae14a36cf373e3e7af622d9d6f40ecb8)
|
|
|
43fe83 |
---
|
|
|
43fe83 |
cfg.mk | 2 +-
|
|
|
43fe83 |
tests/Makefile.am | 17 +-
|
|
|
43fe83 |
tests/virnettlscontexttest.c | 787 +------------------------------------------
|
|
|
43fe83 |
tests/virnettlshelpers.c | 420 +++++++++++++++++++++++
|
|
|
43fe83 |
tests/virnettlshelpers.h | 79 +++++
|
|
|
43fe83 |
tests/virnettlssessiontest.c | 414 +++++++++++++++++++++++
|
|
|
43fe83 |
6 files changed, 945 insertions(+), 774 deletions(-)
|
|
|
43fe83 |
create mode 100644 tests/virnettlshelpers.c
|
|
|
43fe83 |
create mode 100644 tests/virnettlshelpers.h
|
|
|
43fe83 |
create mode 100644 tests/virnettlssessiontest.c
|
|
|
43fe83 |
|
|
|
43fe83 |
diff --git a/cfg.mk b/cfg.mk
|
|
|
43fe83 |
index 13de268..c59d432 100644
|
|
|
43fe83 |
--- a/cfg.mk
|
|
|
43fe83 |
+++ b/cfg.mk
|
|
|
43fe83 |
@@ -899,7 +899,7 @@ $(srcdir)/src/remote/remote_client_bodies.h: $(srcdir)/src/remote/remote_protoco
|
|
|
43fe83 |
exclude_file_name_regexp--sc_avoid_strcase = ^tools/virsh\.h$$
|
|
|
43fe83 |
|
|
|
43fe83 |
_src1=libvirt|fdstream|qemu/qemu_monitor|util/(vircommand|virfile)|xen/xend_internal|rpc/virnetsocket|lxc/lxc_controller|locking/lock_daemon
|
|
|
43fe83 |
-_test1=shunloadtest|virnettlscontexttest|vircgroupmock
|
|
|
43fe83 |
+_test1=shunloadtest|virnettlscontexttest|virnettlssessiontest|vircgroupmock
|
|
|
43fe83 |
exclude_file_name_regexp--sc_avoid_write = \
|
|
|
43fe83 |
^(src/($(_src1))|daemon/libvirtd|tools/console|tests/($(_test1)))\.c$$
|
|
|
43fe83 |
|
|
|
43fe83 |
diff --git a/tests/Makefile.am b/tests/Makefile.am
|
|
|
43fe83 |
index 5ea806e..efd28e3 100644
|
|
|
43fe83 |
--- a/tests/Makefile.am
|
|
|
43fe83 |
+++ b/tests/Makefile.am
|
|
|
43fe83 |
@@ -134,7 +134,7 @@ test_programs += virdbustest \
|
|
|
43fe83 |
endif
|
|
|
43fe83 |
|
|
|
43fe83 |
if WITH_GNUTLS
|
|
|
43fe83 |
-test_programs += virnettlscontexttest
|
|
|
43fe83 |
+test_programs += virnettlscontexttest virnettlssessiontest
|
|
|
43fe83 |
endif
|
|
|
43fe83 |
|
|
|
43fe83 |
if WITH_SECDRIVER_SELINUX
|
|
|
43fe83 |
@@ -592,17 +592,28 @@ virnetsockettest_LDADD = $(LDADDS)
|
|
|
43fe83 |
|
|
|
43fe83 |
if WITH_GNUTLS
|
|
|
43fe83 |
virnettlscontexttest_SOURCES = \
|
|
|
43fe83 |
- virnettlscontexttest.c testutils.h testutils.c
|
|
|
43fe83 |
+ virnettlscontexttest.c \
|
|
|
43fe83 |
+ virnettlshelpers.h virnettlshelpers.c \
|
|
|
43fe83 |
+ testutils.h testutils.c
|
|
|
43fe83 |
virnettlscontexttest_LDADD = $(LDADDS)
|
|
|
43fe83 |
+virnettlssessiontest_SOURCES = \
|
|
|
43fe83 |
+ virnettlssessiontest.c \
|
|
|
43fe83 |
+ virnettlshelpers.h virnettlshelpers.c \
|
|
|
43fe83 |
+ testutils.h testutils.c
|
|
|
43fe83 |
+virnettlssessiontest_LDADD = $(LDADDS)
|
|
|
43fe83 |
if HAVE_LIBTASN1
|
|
|
43fe83 |
virnettlscontexttest_SOURCES += pkix_asn1_tab.c
|
|
|
43fe83 |
virnettlscontexttest_LDADD += -ltasn1
|
|
|
43fe83 |
+virnettlssessiontest_SOURCES += pkix_asn1_tab.c
|
|
|
43fe83 |
+virnettlssessiontest_LDADD += -ltasn1
|
|
|
43fe83 |
else
|
|
|
43fe83 |
EXTRA_DIST += pkix_asn1_tab.c
|
|
|
43fe83 |
endif
|
|
|
43fe83 |
else
|
|
|
43fe83 |
EXTRA_DIST += \
|
|
|
43fe83 |
- virnettlscontexttest.c testutils.h testutils.c pkix_asn1_tab.c
|
|
|
43fe83 |
+ virnettlscontexttest.c virnettlssessiontest.c \
|
|
|
43fe83 |
+ virnettlshelpers.h virnettlshelpers.c \
|
|
|
43fe83 |
+ testutils.h testutils.c pkix_asn1_tab.c
|
|
|
43fe83 |
endif
|
|
|
43fe83 |
|
|
|
43fe83 |
virtimetest_SOURCES = \
|
|
|
43fe83 |
diff --git a/tests/virnettlscontexttest.c b/tests/virnettlscontexttest.c
|
|
|
43fe83 |
index 908432b..0a0d31a 100644
|
|
|
43fe83 |
--- a/tests/virnettlscontexttest.c
|
|
|
43fe83 |
+++ b/tests/virnettlscontexttest.c
|
|
|
43fe83 |
@@ -23,10 +23,9 @@
|
|
|
43fe83 |
#include <stdlib.h>
|
|
|
43fe83 |
#include <fcntl.h>
|
|
|
43fe83 |
#include <sys/socket.h>
|
|
|
43fe83 |
-#include <gnutls/gnutls.h>
|
|
|
43fe83 |
-#include <gnutls/x509.h>
|
|
|
43fe83 |
|
|
|
43fe83 |
#include "testutils.h"
|
|
|
43fe83 |
+#include "virnettlshelpers.h"
|
|
|
43fe83 |
#include "virutil.h"
|
|
|
43fe83 |
#include "virerror.h"
|
|
|
43fe83 |
#include "viralloc.h"
|
|
|
43fe83 |
@@ -34,409 +33,13 @@
|
|
|
43fe83 |
#include "virfile.h"
|
|
|
43fe83 |
#include "vircommand.h"
|
|
|
43fe83 |
#include "virsocketaddr.h"
|
|
|
43fe83 |
-#include "gnutls_1_0_compat.h"
|
|
|
43fe83 |
|
|
|
43fe83 |
#if !defined WIN32 && HAVE_LIBTASN1_H && LIBGNUTLS_VERSION_NUMBER >= 0x020600
|
|
|
43fe83 |
-# include <libtasn1.h>
|
|
|
43fe83 |
|
|
|
43fe83 |
# include "rpc/virnettlscontext.h"
|
|
|
43fe83 |
|
|
|
43fe83 |
# define VIR_FROM_THIS VIR_FROM_RPC
|
|
|
43fe83 |
|
|
|
43fe83 |
-const char *keyfile = abs_builddir "/virnettlscontexttest-key.pem";
|
|
|
43fe83 |
-
|
|
|
43fe83 |
-/*
|
|
|
43fe83 |
- * These store some static data that is needed when
|
|
|
43fe83 |
- * encoding extensions in the x509 certs
|
|
|
43fe83 |
- */
|
|
|
43fe83 |
-ASN1_TYPE pkix_asn1;
|
|
|
43fe83 |
-extern const ASN1_ARRAY_TYPE pkix_asn1_tab[];
|
|
|
43fe83 |
-
|
|
|
43fe83 |
-/*
|
|
|
43fe83 |
- * To avoid consuming random entropy to generate keys,
|
|
|
43fe83 |
- * here's one we prepared earlier :-)
|
|
|
43fe83 |
- */
|
|
|
43fe83 |
-gnutls_x509_privkey_t privkey;
|
|
|
43fe83 |
-# define PRIVATE_KEY \
|
|
|
43fe83 |
- "-----BEGIN PRIVATE KEY-----\n" \
|
|
|
43fe83 |
- "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBALVcr\n" \
|
|
|
43fe83 |
- "BL40Tm6yq88FBhJNw1aaoCjmtg0l4dWQZ/e9Fimx4ARxFpT+ji4FE\n" \
|
|
|
43fe83 |
- "Cgl9s/SGqC+1nvlkm9ViSo0j7MKDbnDB+VRHDvMAzQhA2X7e8M0n9\n" \
|
|
|
43fe83 |
- "rPolUY2lIVC83q0BBaOBkCj2RSmT2xTEbbC2xLukSrg2WP/ihVOxc\n" \
|
|
|
43fe83 |
- "kXRuyFtzAgMBAAECgYB7slBexDwXrtItAMIH6m/U+LUpNe0Xx48OL\n" \
|
|
|
43fe83 |
- "IOn4a4whNgO/o84uIwygUK27ZGFZT0kAGAk8CdF9hA6ArcbQ62s1H\n" \
|
|
|
43fe83 |
- "myxrUbF9/mrLsQw1NEqpuUk9Ay2Tx5U/wPx35S3W/X2AvR/ZpTnCn\n" \
|
|
|
43fe83 |
- "2q/7ym9fyiSoj86drD7BTvmKXlOnOwQJBAPOFMp4mMa9NGpGuEssO\n" \
|
|
|
43fe83 |
- "m3Uwbp6lhcP0cA9MK+iOmeANpoKWfBdk5O34VbmeXnGYWEkrnX+9J\n" \
|
|
|
43fe83 |
- "bM4wVhnnBWtgBMCQQC+qAEmvwcfhauERKYznMVUVksyeuhxhCe7EK\n" \
|
|
|
43fe83 |
- "mPh+U2+g0WwdKvGDgO0PPt1gq0ILEjspMDeMHVdTwkaVBo/uMhAkA\n" \
|
|
|
43fe83 |
- "Z5SsZyCP2aTOPFDypXRdI4eqRcjaEPOUBq27r3uYb/jeboVb2weLa\n" \
|
|
|
43fe83 |
- "L1MmVuHiIHoa5clswPdWVI2y0em2IGoDAkBPSp/v9VKJEZabk9Frd\n" \
|
|
|
43fe83 |
- "a+7u4fanrM9QrEjY3KhduslSilXZZSxrWjjAJPyPiqFb3M8XXA26W\n" \
|
|
|
43fe83 |
- "nz1KYGnqYKhLcBAkB7dt57n9xfrhDpuyVEv+Uv1D3VVAhZlsaZ5Pp\n" \
|
|
|
43fe83 |
- "dcrhrkJn2sa/+O8OKvdrPSeeu/N5WwYhJf61+CPoenMp7IFci\n" \
|
|
|
43fe83 |
- "-----END PRIVATE KEY-----\n"
|
|
|
43fe83 |
-
|
|
|
43fe83 |
-
|
|
|
43fe83 |
-/*
|
|
|
43fe83 |
- * This contains parameter about how to generate
|
|
|
43fe83 |
- * certificates.
|
|
|
43fe83 |
- */
|
|
|
43fe83 |
-struct testTLSCertReq {
|
|
|
43fe83 |
- gnutls_x509_crt_t crt;
|
|
|
43fe83 |
- gnutls_x509_crt_t cacrt; /* If not set, then the cert will be self-signed */
|
|
|
43fe83 |
-
|
|
|
43fe83 |
- const char *filename;
|
|
|
43fe83 |
-
|
|
|
43fe83 |
- /* Identifying information */
|
|
|
43fe83 |
- const char *country;
|
|
|
43fe83 |
- const char *cn;
|
|
|
43fe83 |
- const char *altname1;
|
|
|
43fe83 |
- const char *altname2;
|
|
|
43fe83 |
- const char *ipaddr1;
|
|
|
43fe83 |
- const char *ipaddr2;
|
|
|
43fe83 |
-
|
|
|
43fe83 |
- /* Basic constraints */
|
|
|
43fe83 |
- bool basicConstraintsEnable;
|
|
|
43fe83 |
- bool basicConstraintsCritical;
|
|
|
43fe83 |
- bool basicConstraintsIsCA;
|
|
|
43fe83 |
-
|
|
|
43fe83 |
- /* Key usage */
|
|
|
43fe83 |
- bool keyUsageEnable;
|
|
|
43fe83 |
- bool keyUsageCritical;
|
|
|
43fe83 |
- int keyUsageValue;
|
|
|
43fe83 |
-
|
|
|
43fe83 |
- /* Key purpose (aka Extended key usage) */
|
|
|
43fe83 |
- bool keyPurposeEnable;
|
|
|
43fe83 |
- bool keyPurposeCritical;
|
|
|
43fe83 |
- const char *keyPurposeOID1;
|
|
|
43fe83 |
- const char *keyPurposeOID2;
|
|
|
43fe83 |
-
|
|
|
43fe83 |
- /* zero for current time, or non-zero for hours from now */
|
|
|
43fe83 |
- int start_offset;
|
|
|
43fe83 |
- /* zero for 24 hours from now, or non-zero for hours from now */
|
|
|
43fe83 |
- int expire_offset;
|
|
|
43fe83 |
-};
|
|
|
43fe83 |
-
|
|
|
43fe83 |
-
|
|
|
43fe83 |
-/*
|
|
|
43fe83 |
- * Turns an ASN1 object into a DER encoded byte array
|
|
|
43fe83 |
- */
|
|
|
43fe83 |
-static void testTLSDerEncode(ASN1_TYPE src,
|
|
|
43fe83 |
- const char *src_name,
|
|
|
43fe83 |
- gnutls_datum_t * res)
|
|
|
43fe83 |
-{
|
|
|
43fe83 |
- int size;
|
|
|
43fe83 |
- char *data = NULL;
|
|
|
43fe83 |
-
|
|
|
43fe83 |
- size = 0;
|
|
|
43fe83 |
- asn1_der_coding(src, src_name, NULL, &size, NULL);
|
|
|
43fe83 |
-
|
|
|
43fe83 |
- if (VIR_ALLOC_N(data, size) < 0)
|
|
|
43fe83 |
- abort();
|
|
|
43fe83 |
-
|
|
|
43fe83 |
- asn1_der_coding(src, src_name, data, &size, NULL);
|
|
|
43fe83 |
-
|
|
|
43fe83 |
- res->data = (unsigned char *)data;
|
|
|
43fe83 |
- res->size = size;
|
|
|
43fe83 |
-}
|
|
|
43fe83 |
-
|
|
|
43fe83 |
-
|
|
|
43fe83 |
-/*
|
|
|
43fe83 |
- * This is a fairly lame x509 certificate generator.
|
|
|
43fe83 |
- *
|
|
|
43fe83 |
- * Do not copy/use this code for generating real certificates
|
|
|
43fe83 |
- * since it leaves out many things that you would want in
|
|
|
43fe83 |
- * certificates for real world usage.
|
|
|
43fe83 |
- *
|
|
|
43fe83 |
- * This is good enough only for doing tests of the libvirt
|
|
|
43fe83 |
- * TLS certificate code
|
|
|
43fe83 |
- */
|
|
|
43fe83 |
-static void
|
|
|
43fe83 |
-testTLSGenerateCert(struct testTLSCertReq *req)
|
|
|
43fe83 |
-{
|
|
|
43fe83 |
- gnutls_x509_crt_t crt;
|
|
|
43fe83 |
- int err;
|
|
|
43fe83 |
- static char buffer[1024*1024];
|
|
|
43fe83 |
- size_t size = sizeof(buffer);
|
|
|
43fe83 |
- char serial[5] = { 1, 2, 3, 4, 0 };
|
|
|
43fe83 |
- gnutls_datum_t der;
|
|
|
43fe83 |
- time_t start = time(NULL) + (60*60*req->start_offset);
|
|
|
43fe83 |
- time_t expire = time(NULL) + (60*60*(req->expire_offset
|
|
|
43fe83 |
- ? req->expire_offset : 24));
|
|
|
43fe83 |
-
|
|
|
43fe83 |
- /*
|
|
|
43fe83 |
- * Prepare our new certificate object
|
|
|
43fe83 |
- */
|
|
|
43fe83 |
- if ((err = gnutls_x509_crt_init(&crt)) < 0) {
|
|
|
43fe83 |
- VIR_WARN("Failed to initialize certificate %s", gnutls_strerror(err));
|
|
|
43fe83 |
- abort();
|
|
|
43fe83 |
- }
|
|
|
43fe83 |
- if ((err = gnutls_x509_crt_set_key(crt, privkey)) < 0) {
|
|
|
43fe83 |
- VIR_WARN("Failed to set certificate key %s", gnutls_strerror(err));
|
|
|
43fe83 |
- abort();
|
|
|
43fe83 |
- }
|
|
|
43fe83 |
-
|
|
|
43fe83 |
- /*
|
|
|
43fe83 |
- * A v3 certificate is required in order to be able
|
|
|
43fe83 |
- * set any of the basic constraints, key purpose and
|
|
|
43fe83 |
- * key usage data
|
|
|
43fe83 |
- */
|
|
|
43fe83 |
- gnutls_x509_crt_set_version(crt, 3);
|
|
|
43fe83 |
-
|
|
|
43fe83 |
- if (req->country) {
|
|
|
43fe83 |
- if ((err = gnutls_x509_crt_set_dn_by_oid(crt, GNUTLS_OID_X520_COUNTRY_NAME, 0,
|
|
|
43fe83 |
- req->country, strlen(req->country))) < 0) {
|
|
|
43fe83 |
- VIR_WARN("Failed to set certificate country name %s", gnutls_strerror(err));
|
|
|
43fe83 |
- abort();
|
|
|
43fe83 |
- }
|
|
|
43fe83 |
- }
|
|
|
43fe83 |
- if (req->cn) {
|
|
|
43fe83 |
- if ((err = gnutls_x509_crt_set_dn_by_oid(crt, GNUTLS_OID_X520_COMMON_NAME, 0,
|
|
|
43fe83 |
- req->cn, strlen(req->cn))) < 0) {
|
|
|
43fe83 |
- VIR_WARN("Failed to set certificate common name %s", gnutls_strerror(err));
|
|
|
43fe83 |
- abort();
|
|
|
43fe83 |
- }
|
|
|
43fe83 |
- }
|
|
|
43fe83 |
-
|
|
|
43fe83 |
- /*
|
|
|
43fe83 |
- * Setup the subject altnames, which are used
|
|
|
43fe83 |
- * for hostname checks in live sessions
|
|
|
43fe83 |
- */
|
|
|
43fe83 |
- if (req->altname1) {
|
|
|
43fe83 |
- if ((err = gnutls_x509_crt_set_subject_alt_name(crt, GNUTLS_SAN_DNSNAME,
|
|
|
43fe83 |
- req->altname1,
|
|
|
43fe83 |
- strlen(req->altname1),
|
|
|
43fe83 |
- GNUTLS_FSAN_APPEND))) {
|
|
|
43fe83 |
- VIR_WARN("Failed to set certificate alt name %s", gnutls_strerror(err));
|
|
|
43fe83 |
- abort();
|
|
|
43fe83 |
- }
|
|
|
43fe83 |
- }
|
|
|
43fe83 |
- if (req->altname2) {
|
|
|
43fe83 |
- if ((err = gnutls_x509_crt_set_subject_alt_name(crt, GNUTLS_SAN_DNSNAME,
|
|
|
43fe83 |
- req->altname2,
|
|
|
43fe83 |
- strlen(req->altname2),
|
|
|
43fe83 |
- GNUTLS_FSAN_APPEND))) {
|
|
|
43fe83 |
- VIR_WARN("Failed to set certificate %s alt name", gnutls_strerror(err));
|
|
|
43fe83 |
- abort();
|
|
|
43fe83 |
- }
|
|
|
43fe83 |
- }
|
|
|
43fe83 |
-
|
|
|
43fe83 |
- /*
|
|
|
43fe83 |
- * IP address need to be put into the cert in their
|
|
|
43fe83 |
- * raw byte form, not strings, hence this is a little
|
|
|
43fe83 |
- * more complicated
|
|
|
43fe83 |
- */
|
|
|
43fe83 |
- if (req->ipaddr1) {
|
|
|
43fe83 |
- virSocketAddr addr;
|
|
|
43fe83 |
- char *data;
|
|
|
43fe83 |
- int len;
|
|
|
43fe83 |
- if (virSocketAddrParse(&addr, req->ipaddr1, 0) < 0) {
|
|
|
43fe83 |
- VIR_WARN("Cannot parse %s", req->ipaddr1);
|
|
|
43fe83 |
- abort();
|
|
|
43fe83 |
- }
|
|
|
43fe83 |
-
|
|
|
43fe83 |
- if (addr.data.sa.sa_family == AF_INET) {
|
|
|
43fe83 |
- data = (char*)&addr.data.inet4.sin_addr;
|
|
|
43fe83 |
- len = 4;
|
|
|
43fe83 |
- } else {
|
|
|
43fe83 |
- data = (char*)&addr.data.inet6.sin6_addr;
|
|
|
43fe83 |
- len = 16;
|
|
|
43fe83 |
- }
|
|
|
43fe83 |
-
|
|
|
43fe83 |
- if ((err = gnutls_x509_crt_set_subject_alt_name(crt, GNUTLS_SAN_IPADDRESS,
|
|
|
43fe83 |
- data, len, GNUTLS_FSAN_APPEND))) {
|
|
|
43fe83 |
- VIR_WARN("Failed to set certificate alt name %s", gnutls_strerror(err));
|
|
|
43fe83 |
- abort();
|
|
|
43fe83 |
- }
|
|
|
43fe83 |
- }
|
|
|
43fe83 |
- if (req->ipaddr2) {
|
|
|
43fe83 |
- virSocketAddr addr;
|
|
|
43fe83 |
- char *data;
|
|
|
43fe83 |
- int len;
|
|
|
43fe83 |
- if (virSocketAddrParse(&addr, req->ipaddr2, 0) < 0) {
|
|
|
43fe83 |
- VIR_WARN("Cannot parse %s", req->ipaddr2);
|
|
|
43fe83 |
- abort();
|
|
|
43fe83 |
- }
|
|
|
43fe83 |
-
|
|
|
43fe83 |
- if (addr.data.sa.sa_family == AF_INET) {
|
|
|
43fe83 |
- data = (char*)&addr.data.inet4.sin_addr;
|
|
|
43fe83 |
- len = 4;
|
|
|
43fe83 |
- } else {
|
|
|
43fe83 |
- data = (char*)&addr.data.inet6.sin6_addr;
|
|
|
43fe83 |
- len = 16;
|
|
|
43fe83 |
- }
|
|
|
43fe83 |
-
|
|
|
43fe83 |
- if ((err = gnutls_x509_crt_set_subject_alt_name(crt, GNUTLS_SAN_IPADDRESS,
|
|
|
43fe83 |
- data, len, GNUTLS_FSAN_APPEND))) {
|
|
|
43fe83 |
- VIR_WARN("Failed to set certificate alt name %s", gnutls_strerror(err));
|
|
|
43fe83 |
- abort();
|
|
|
43fe83 |
- }
|
|
|
43fe83 |
- }
|
|
|
43fe83 |
-
|
|
|
43fe83 |
-
|
|
|
43fe83 |
- /*
|
|
|
43fe83 |
- * Basic constraints are used to decide if the cert
|
|
|
43fe83 |
- * is for a CA or not. We can't use the convenient
|
|
|
43fe83 |
- * gnutls API for setting this, since it hardcodes
|
|
|
43fe83 |
- * the 'critical' field which we want control over
|
|
|
43fe83 |
- */
|
|
|
43fe83 |
- if (req->basicConstraintsEnable) {
|
|
|
43fe83 |
- ASN1_TYPE ext = ASN1_TYPE_EMPTY;
|
|
|
43fe83 |
-
|
|
|
43fe83 |
- asn1_create_element(pkix_asn1, "PKIX1.BasicConstraints", &ext;;
|
|
|
43fe83 |
- asn1_write_value(ext, "cA", req->basicConstraintsIsCA ? "TRUE" : "FALSE", 1);
|
|
|
43fe83 |
- asn1_write_value(ext, "pathLenConstraint", NULL, 0);
|
|
|
43fe83 |
- testTLSDerEncode(ext, "", &der;;
|
|
|
43fe83 |
- if ((err = gnutls_x509_crt_set_extension_by_oid(crt,
|
|
|
43fe83 |
- "2.5.29.19",
|
|
|
43fe83 |
- der.data,
|
|
|
43fe83 |
- der.size,
|
|
|
43fe83 |
- req->basicConstraintsCritical)) < 0) {
|
|
|
43fe83 |
- VIR_WARN("Failed to set certificate basic constraints %s", gnutls_strerror(err));
|
|
|
43fe83 |
- VIR_FREE(der.data);
|
|
|
43fe83 |
- abort();
|
|
|
43fe83 |
- }
|
|
|
43fe83 |
- asn1_delete_structure(&ext;;
|
|
|
43fe83 |
- VIR_FREE(der.data);
|
|
|
43fe83 |
- }
|
|
|
43fe83 |
-
|
|
|
43fe83 |
- /*
|
|
|
43fe83 |
- * Next up the key usage extension. Again we can't
|
|
|
43fe83 |
- * use the gnutls API since it hardcodes the extension
|
|
|
43fe83 |
- * to be 'critical'
|
|
|
43fe83 |
- */
|
|
|
43fe83 |
- if (req->keyUsageEnable) {
|
|
|
43fe83 |
- ASN1_TYPE ext = ASN1_TYPE_EMPTY;
|
|
|
43fe83 |
- char str[2];
|
|
|
43fe83 |
-
|
|
|
43fe83 |
- str[0] = req->keyUsageValue & 0xff;
|
|
|
43fe83 |
- str[1] = (req->keyUsageValue >> 8) & 0xff;
|
|
|
43fe83 |
-
|
|
|
43fe83 |
- asn1_create_element(pkix_asn1, "PKIX1.KeyUsage", &ext;;
|
|
|
43fe83 |
- asn1_write_value(ext, "", str, 9);
|
|
|
43fe83 |
- testTLSDerEncode(ext, "", &der;;
|
|
|
43fe83 |
- if ((err = gnutls_x509_crt_set_extension_by_oid(crt,
|
|
|
43fe83 |
- "2.5.29.15",
|
|
|
43fe83 |
- der.data,
|
|
|
43fe83 |
- der.size,
|
|
|
43fe83 |
- req->keyUsageCritical)) < 0) {
|
|
|
43fe83 |
- VIR_WARN("Failed to set certificate key usage %s", gnutls_strerror(err));
|
|
|
43fe83 |
- VIR_FREE(der.data);
|
|
|
43fe83 |
- abort();
|
|
|
43fe83 |
- }
|
|
|
43fe83 |
- asn1_delete_structure(&ext;;
|
|
|
43fe83 |
- VIR_FREE(der.data);
|
|
|
43fe83 |
- }
|
|
|
43fe83 |
-
|
|
|
43fe83 |
- /*
|
|
|
43fe83 |
- * Finally the key purpose extension. This time
|
|
|
43fe83 |
- * gnutls has the opposite problem, always hardcoding
|
|
|
43fe83 |
- * it to be non-critical. So once again we have to
|
|
|
43fe83 |
- * set this the hard way building up ASN1 data ourselves
|
|
|
43fe83 |
- */
|
|
|
43fe83 |
- if (req->keyPurposeEnable) {
|
|
|
43fe83 |
- ASN1_TYPE ext = ASN1_TYPE_EMPTY;
|
|
|
43fe83 |
-
|
|
|
43fe83 |
- asn1_create_element(pkix_asn1, "PKIX1.ExtKeyUsageSyntax", &ext;;
|
|
|
43fe83 |
- if (req->keyPurposeOID1) {
|
|
|
43fe83 |
- asn1_write_value(ext, "", "NEW", 1);
|
|
|
43fe83 |
- asn1_write_value(ext, "?LAST", req->keyPurposeOID1, 1);
|
|
|
43fe83 |
- }
|
|
|
43fe83 |
- if (req->keyPurposeOID2) {
|
|
|
43fe83 |
- asn1_write_value(ext, "", "NEW", 1);
|
|
|
43fe83 |
- asn1_write_value(ext, "?LAST", req->keyPurposeOID2, 1);
|
|
|
43fe83 |
- }
|
|
|
43fe83 |
- testTLSDerEncode(ext, "", &der;;
|
|
|
43fe83 |
- if ((err = gnutls_x509_crt_set_extension_by_oid(crt,
|
|
|
43fe83 |
- "2.5.29.37",
|
|
|
43fe83 |
- der.data,
|
|
|
43fe83 |
- der.size,
|
|
|
43fe83 |
- req->keyPurposeCritical)) < 0) {
|
|
|
43fe83 |
- VIR_WARN("Failed to set certificate key purpose %s", gnutls_strerror(err));
|
|
|
43fe83 |
- VIR_FREE(der.data);
|
|
|
43fe83 |
- abort();
|
|
|
43fe83 |
- }
|
|
|
43fe83 |
- asn1_delete_structure(&ext;;
|
|
|
43fe83 |
- VIR_FREE(der.data);
|
|
|
43fe83 |
- }
|
|
|
43fe83 |
-
|
|
|
43fe83 |
- /*
|
|
|
43fe83 |
- * Any old serial number will do, so lets pick 5
|
|
|
43fe83 |
- */
|
|
|
43fe83 |
- if ((err = gnutls_x509_crt_set_serial(crt, serial, 5)) < 0) {
|
|
|
43fe83 |
- VIR_WARN("Failed to set certificate serial %s", gnutls_strerror(err));
|
|
|
43fe83 |
- abort();
|
|
|
43fe83 |
- }
|
|
|
43fe83 |
-
|
|
|
43fe83 |
- if ((err = gnutls_x509_crt_set_activation_time(crt, start)) < 0) {
|
|
|
43fe83 |
- VIR_WARN("Failed to set certificate activation %s", gnutls_strerror(err));
|
|
|
43fe83 |
- abort();
|
|
|
43fe83 |
- }
|
|
|
43fe83 |
- if ((err = gnutls_x509_crt_set_expiration_time(crt, expire)) < 0) {
|
|
|
43fe83 |
- VIR_WARN("Failed to set certificate expiration %s", gnutls_strerror(err));
|
|
|
43fe83 |
- abort();
|
|
|
43fe83 |
- }
|
|
|
43fe83 |
-
|
|
|
43fe83 |
-
|
|
|
43fe83 |
- /*
|
|
|
43fe83 |
- * If no 'cart' is set then we are self signing
|
|
|
43fe83 |
- * the cert. This is done for CA certs
|
|
|
43fe83 |
- */
|
|
|
43fe83 |
- if ((err = gnutls_x509_crt_sign(crt, req->cacrt ? req->cacrt : crt, privkey) < 0)) {
|
|
|
43fe83 |
- VIR_WARN("Failed to sign certificate %s", gnutls_strerror(err));
|
|
|
43fe83 |
- abort();
|
|
|
43fe83 |
- }
|
|
|
43fe83 |
-
|
|
|
43fe83 |
- /*
|
|
|
43fe83 |
- * Finally write the new cert out to disk
|
|
|
43fe83 |
- */
|
|
|
43fe83 |
- if ((err = gnutls_x509_crt_export(crt, GNUTLS_X509_FMT_PEM, buffer, &size) < 0)) {
|
|
|
43fe83 |
- VIR_WARN("Failed to export certificate %s", gnutls_strerror(err));
|
|
|
43fe83 |
- abort();
|
|
|
43fe83 |
- }
|
|
|
43fe83 |
-
|
|
|
43fe83 |
- if (virFileWriteStr(req->filename, buffer, 0600) < 0) {
|
|
|
43fe83 |
- VIR_WARN("Failed to write certificate %s %s", req->filename, gnutls_strerror(err));
|
|
|
43fe83 |
- abort();
|
|
|
43fe83 |
- }
|
|
|
43fe83 |
-
|
|
|
43fe83 |
- req->crt = crt;
|
|
|
43fe83 |
- return;
|
|
|
43fe83 |
-}
|
|
|
43fe83 |
-
|
|
|
43fe83 |
-
|
|
|
43fe83 |
-/*
|
|
|
43fe83 |
- * This loads the private key we defined earlier
|
|
|
43fe83 |
- */
|
|
|
43fe83 |
-static gnutls_x509_privkey_t testTLSLoadKey(void)
|
|
|
43fe83 |
-{
|
|
|
43fe83 |
- gnutls_x509_privkey_t key;
|
|
|
43fe83 |
- const gnutls_datum_t data = { (unsigned char *)PRIVATE_KEY, strlen(PRIVATE_KEY) };
|
|
|
43fe83 |
- int err;
|
|
|
43fe83 |
-
|
|
|
43fe83 |
- if ((err = gnutls_x509_privkey_init(&key)) < 0) {
|
|
|
43fe83 |
- VIR_WARN("Failed to init key %s", gnutls_strerror(err));
|
|
|
43fe83 |
- abort();
|
|
|
43fe83 |
- }
|
|
|
43fe83 |
-
|
|
|
43fe83 |
- if ((err = gnutls_x509_privkey_import(key, &data,
|
|
|
43fe83 |
- GNUTLS_X509_FMT_PEM)) < 0) {
|
|
|
43fe83 |
- if (err != GNUTLS_E_BASE64_UNEXPECTED_HEADER_ERROR) {
|
|
|
43fe83 |
- VIR_WARN("Failed to import key %s", gnutls_strerror(err));
|
|
|
43fe83 |
- abort();
|
|
|
43fe83 |
- }
|
|
|
43fe83 |
-
|
|
|
43fe83 |
- if ((err = gnutls_x509_privkey_import_pkcs8(key, &data, GNUTLS_X509_FMT_PEM, NULL, 0)) < 0) {
|
|
|
43fe83 |
- VIR_WARN("Failed to import PKCS8 key %s", gnutls_strerror(err));
|
|
|
43fe83 |
- abort();
|
|
|
43fe83 |
- }
|
|
|
43fe83 |
- }
|
|
|
43fe83 |
-
|
|
|
43fe83 |
- return key;
|
|
|
43fe83 |
-}
|
|
|
43fe83 |
-
|
|
|
43fe83 |
-
|
|
|
43fe83 |
struct testTLSContextData {
|
|
|
43fe83 |
bool isServer;
|
|
|
43fe83 |
struct testTLSCertReq careq;
|
|
|
43fe83 |
@@ -500,257 +103,19 @@ static int testTLSContextInit(const void *opaque)
|
|
|
43fe83 |
|
|
|
43fe83 |
cleanup:
|
|
|
43fe83 |
virObjectUnref(ctxt);
|
|
|
43fe83 |
- gnutls_x509_crt_deinit(data->careq.crt);
|
|
|
43fe83 |
- gnutls_x509_crt_deinit(data->certreq.crt);
|
|
|
43fe83 |
- data->careq.crt = data->certreq.crt = NULL;
|
|
|
43fe83 |
- /* When troubleshooting this tests, we often want to leave the certs on disk */
|
|
|
43fe83 |
- if (getenv("VIRT_TEST_DEBUG_CERTS") == NULL) {
|
|
|
43fe83 |
- unlink(data->careq.filename);
|
|
|
43fe83 |
- unlink(data->certreq.filename);
|
|
|
43fe83 |
- }
|
|
|
43fe83 |
+ testTLSDiscardCert(&data->careq);
|
|
|
43fe83 |
+ testTLSDiscardCert(&data->certreq);
|
|
|
43fe83 |
return ret;
|
|
|
43fe83 |
}
|
|
|
43fe83 |
|
|
|
43fe83 |
|
|
|
43fe83 |
|
|
|
43fe83 |
-struct testTLSSessionData {
|
|
|
43fe83 |
- struct testTLSCertReq careq;
|
|
|
43fe83 |
- struct testTLSCertReq othercareq;
|
|
|
43fe83 |
- struct testTLSCertReq serverreq;
|
|
|
43fe83 |
- struct testTLSCertReq clientreq;
|
|
|
43fe83 |
- bool expectServerFail;
|
|
|
43fe83 |
- bool expectClientFail;
|
|
|
43fe83 |
- const char *hostname;
|
|
|
43fe83 |
- const char *const* wildcards;
|
|
|
43fe83 |
-};
|
|
|
43fe83 |
-
|
|
|
43fe83 |
-
|
|
|
43fe83 |
-static ssize_t testWrite(const char *buf, size_t len, void *opaque)
|
|
|
43fe83 |
-{
|
|
|
43fe83 |
- int *fd = opaque;
|
|
|
43fe83 |
-
|
|
|
43fe83 |
- return write(*fd, buf, len);
|
|
|
43fe83 |
-}
|
|
|
43fe83 |
-
|
|
|
43fe83 |
-static ssize_t testRead(char *buf, size_t len, void *opaque)
|
|
|
43fe83 |
-{
|
|
|
43fe83 |
- int *fd = opaque;
|
|
|
43fe83 |
-
|
|
|
43fe83 |
- return read(*fd, buf, len);
|
|
|
43fe83 |
-}
|
|
|
43fe83 |
-
|
|
|
43fe83 |
-/*
|
|
|
43fe83 |
- * This tests validation checking of peer certificates
|
|
|
43fe83 |
- *
|
|
|
43fe83 |
- * This is replicating the checks that are done for an
|
|
|
43fe83 |
- * active TLS session after handshake completes. To
|
|
|
43fe83 |
- * simulate that we create our TLS contexts, skipping
|
|
|
43fe83 |
- * sanity checks. When then get a socketpair, and
|
|
|
43fe83 |
- * initiate a TLS session across them. Finally do
|
|
|
43fe83 |
- * do actual cert validation tests
|
|
|
43fe83 |
- */
|
|
|
43fe83 |
-static int testTLSSessionInit(const void *opaque)
|
|
|
43fe83 |
-{
|
|
|
43fe83 |
- struct testTLSSessionData *data = (struct testTLSSessionData *)opaque;
|
|
|
43fe83 |
- virNetTLSContextPtr clientCtxt = NULL;
|
|
|
43fe83 |
- virNetTLSContextPtr serverCtxt = NULL;
|
|
|
43fe83 |
- virNetTLSSessionPtr clientSess = NULL;
|
|
|
43fe83 |
- virNetTLSSessionPtr serverSess = NULL;
|
|
|
43fe83 |
- int ret = -1;
|
|
|
43fe83 |
- int channel[2];
|
|
|
43fe83 |
- bool clientShake = false;
|
|
|
43fe83 |
- bool serverShake = false;
|
|
|
43fe83 |
-
|
|
|
43fe83 |
-
|
|
|
43fe83 |
- /* We'll use this for our fake client-server connection */
|
|
|
43fe83 |
- if (socketpair(AF_UNIX, SOCK_STREAM, 0, channel) < 0)
|
|
|
43fe83 |
- abort();
|
|
|
43fe83 |
-
|
|
|
43fe83 |
- /*
|
|
|
43fe83 |
- * We have an evil loop to do the handshake in a single
|
|
|
43fe83 |
- * thread, so we need these non-blocking to avoid deadlock
|
|
|
43fe83 |
- * of ourselves
|
|
|
43fe83 |
- */
|
|
|
43fe83 |
- ignore_value(virSetNonBlock(channel[0]));
|
|
|
43fe83 |
- ignore_value(virSetNonBlock(channel[1]));
|
|
|
43fe83 |
-
|
|
|
43fe83 |
-
|
|
|
43fe83 |
- /* Generate all the certs we need for this test */
|
|
|
43fe83 |
- testTLSGenerateCert(&data->careq);
|
|
|
43fe83 |
- data->serverreq.cacrt = data->careq.crt;
|
|
|
43fe83 |
- testTLSGenerateCert(&data->serverreq);
|
|
|
43fe83 |
-
|
|
|
43fe83 |
- if (data->othercareq.filename) {
|
|
|
43fe83 |
- testTLSGenerateCert(&data->othercareq);
|
|
|
43fe83 |
- data->clientreq.cacrt = data->othercareq.crt;
|
|
|
43fe83 |
- } else {
|
|
|
43fe83 |
- data->clientreq.cacrt = data->careq.crt;
|
|
|
43fe83 |
- }
|
|
|
43fe83 |
- testTLSGenerateCert(&data->clientreq);
|
|
|
43fe83 |
-
|
|
|
43fe83 |
-
|
|
|
43fe83 |
- /* We skip initial sanity checks here because we
|
|
|
43fe83 |
- * want to make sure that problems are being
|
|
|
43fe83 |
- * detected at the TLS session validation stage
|
|
|
43fe83 |
- */
|
|
|
43fe83 |
- serverCtxt = virNetTLSContextNewServer(data->careq.filename,
|
|
|
43fe83 |
- NULL,
|
|
|
43fe83 |
- data->serverreq.filename,
|
|
|
43fe83 |
- keyfile,
|
|
|
43fe83 |
- data->wildcards,
|
|
|
43fe83 |
- false,
|
|
|
43fe83 |
- true);
|
|
|
43fe83 |
-
|
|
|
43fe83 |
- clientCtxt = virNetTLSContextNewClient(data->othercareq.filename ?
|
|
|
43fe83 |
- data->othercareq.filename :
|
|
|
43fe83 |
- data->careq.filename,
|
|
|
43fe83 |
- NULL,
|
|
|
43fe83 |
- data->clientreq.filename,
|
|
|
43fe83 |
- keyfile,
|
|
|
43fe83 |
- false,
|
|
|
43fe83 |
- true);
|
|
|
43fe83 |
-
|
|
|
43fe83 |
- if (!serverCtxt) {
|
|
|
43fe83 |
- VIR_WARN("Unexpected failure loading %s against %s",
|
|
|
43fe83 |
- data->careq.filename, data->serverreq.filename);
|
|
|
43fe83 |
- goto cleanup;
|
|
|
43fe83 |
- }
|
|
|
43fe83 |
- if (!clientCtxt) {
|
|
|
43fe83 |
- VIR_WARN("Unexpected failure loading %s against %s",
|
|
|
43fe83 |
- data->othercareq.filename ? data->othercareq.filename :
|
|
|
43fe83 |
- data->careq.filename, data->clientreq.filename);
|
|
|
43fe83 |
- goto cleanup;
|
|
|
43fe83 |
- }
|
|
|
43fe83 |
-
|
|
|
43fe83 |
-
|
|
|
43fe83 |
- /* Now the real part of the test, setup the sessions */
|
|
|
43fe83 |
- serverSess = virNetTLSSessionNew(serverCtxt, NULL);
|
|
|
43fe83 |
- clientSess = virNetTLSSessionNew(clientCtxt, data->hostname);
|
|
|
43fe83 |
-
|
|
|
43fe83 |
- if (!serverSess) {
|
|
|
43fe83 |
- VIR_WARN("Unexpected failure using %s against %s",
|
|
|
43fe83 |
- data->careq.filename, data->serverreq.filename);
|
|
|
43fe83 |
- goto cleanup;
|
|
|
43fe83 |
- }
|
|
|
43fe83 |
- if (!clientSess) {
|
|
|
43fe83 |
- VIR_WARN("Unexpected failure using %s against %s",
|
|
|
43fe83 |
- data->othercareq.filename ? data->othercareq.filename :
|
|
|
43fe83 |
- data->careq.filename, data->clientreq.filename);
|
|
|
43fe83 |
- goto cleanup;
|
|
|
43fe83 |
- }
|
|
|
43fe83 |
-
|
|
|
43fe83 |
- /* For handshake to work, we need to set the I/O callbacks
|
|
|
43fe83 |
- * to read/write over the socketpair
|
|
|
43fe83 |
- */
|
|
|
43fe83 |
- virNetTLSSessionSetIOCallbacks(serverSess, testWrite, testRead, &channel[0]);
|
|
|
43fe83 |
- virNetTLSSessionSetIOCallbacks(clientSess, testWrite, testRead, &channel[1]);
|
|
|
43fe83 |
-
|
|
|
43fe83 |
- /*
|
|
|
43fe83 |
- * Finally we loop around & around doing handshake on each
|
|
|
43fe83 |
- * session until we get an error, or the handshake completes.
|
|
|
43fe83 |
- * This relies on the socketpair being nonblocking to avoid
|
|
|
43fe83 |
- * deadlocking ourselves upon handshake
|
|
|
43fe83 |
- */
|
|
|
43fe83 |
- do {
|
|
|
43fe83 |
- int rv;
|
|
|
43fe83 |
- if (!serverShake) {
|
|
|
43fe83 |
- rv = virNetTLSSessionHandshake(serverSess);
|
|
|
43fe83 |
- if (rv < 0)
|
|
|
43fe83 |
- goto cleanup;
|
|
|
43fe83 |
- if (rv == VIR_NET_TLS_HANDSHAKE_COMPLETE)
|
|
|
43fe83 |
- serverShake = true;
|
|
|
43fe83 |
- }
|
|
|
43fe83 |
- if (!clientShake) {
|
|
|
43fe83 |
- rv = virNetTLSSessionHandshake(clientSess);
|
|
|
43fe83 |
- if (rv < 0)
|
|
|
43fe83 |
- goto cleanup;
|
|
|
43fe83 |
- if (rv == VIR_NET_TLS_HANDSHAKE_COMPLETE)
|
|
|
43fe83 |
- clientShake = true;
|
|
|
43fe83 |
- }
|
|
|
43fe83 |
- } while (!clientShake && !serverShake);
|
|
|
43fe83 |
-
|
|
|
43fe83 |
-
|
|
|
43fe83 |
- /* Finally make sure the server validation does what
|
|
|
43fe83 |
- * we were expecting
|
|
|
43fe83 |
- */
|
|
|
43fe83 |
- if (virNetTLSContextCheckCertificate(serverCtxt,
|
|
|
43fe83 |
- serverSess) < 0) {
|
|
|
43fe83 |
- if (!data->expectServerFail) {
|
|
|
43fe83 |
- VIR_WARN("Unexpected server cert check fail");
|
|
|
43fe83 |
- goto cleanup;
|
|
|
43fe83 |
- } else {
|
|
|
43fe83 |
- VIR_DEBUG("Got expected server cert fail");
|
|
|
43fe83 |
- }
|
|
|
43fe83 |
- } else {
|
|
|
43fe83 |
- if (data->expectServerFail) {
|
|
|
43fe83 |
- VIR_WARN("Expected server cert check fail");
|
|
|
43fe83 |
- goto cleanup;
|
|
|
43fe83 |
- } else {
|
|
|
43fe83 |
- VIR_DEBUG("Not unexpected server cert fail");
|
|
|
43fe83 |
- }
|
|
|
43fe83 |
- }
|
|
|
43fe83 |
-
|
|
|
43fe83 |
- /*
|
|
|
43fe83 |
- * And the same for the client validation check
|
|
|
43fe83 |
- */
|
|
|
43fe83 |
- if (virNetTLSContextCheckCertificate(clientCtxt,
|
|
|
43fe83 |
- clientSess) < 0) {
|
|
|
43fe83 |
- if (!data->expectClientFail) {
|
|
|
43fe83 |
- VIR_WARN("Unexpected client cert check fail");
|
|
|
43fe83 |
- goto cleanup;
|
|
|
43fe83 |
- } else {
|
|
|
43fe83 |
- VIR_DEBUG("Got expected client cert fail");
|
|
|
43fe83 |
- }
|
|
|
43fe83 |
- } else {
|
|
|
43fe83 |
- if (data->expectClientFail) {
|
|
|
43fe83 |
- VIR_WARN("Expected client cert check fail");
|
|
|
43fe83 |
- goto cleanup;
|
|
|
43fe83 |
- } else {
|
|
|
43fe83 |
- VIR_DEBUG("Not unexpected client cert fail");
|
|
|
43fe83 |
- }
|
|
|
43fe83 |
- }
|
|
|
43fe83 |
-
|
|
|
43fe83 |
- ret = 0;
|
|
|
43fe83 |
-
|
|
|
43fe83 |
-cleanup:
|
|
|
43fe83 |
- virObjectUnref(serverCtxt);
|
|
|
43fe83 |
- virObjectUnref(clientCtxt);
|
|
|
43fe83 |
- virObjectUnref(serverSess);
|
|
|
43fe83 |
- virObjectUnref(clientSess);
|
|
|
43fe83 |
- gnutls_x509_crt_deinit(data->careq.crt);
|
|
|
43fe83 |
- if (data->othercareq.filename)
|
|
|
43fe83 |
- gnutls_x509_crt_deinit(data->othercareq.crt);
|
|
|
43fe83 |
- gnutls_x509_crt_deinit(data->clientreq.crt);
|
|
|
43fe83 |
- gnutls_x509_crt_deinit(data->serverreq.crt);
|
|
|
43fe83 |
- data->careq.crt = data->othercareq.crt = data->clientreq.crt = data->serverreq.crt = NULL;
|
|
|
43fe83 |
-
|
|
|
43fe83 |
- /* When troubleshooting this tests, we often want to leave the certs on disk */
|
|
|
43fe83 |
- if (getenv("VIRT_TEST_DEBUG_CERTS") == NULL) {
|
|
|
43fe83 |
- unlink(data->careq.filename);
|
|
|
43fe83 |
- if (data->othercareq.filename)
|
|
|
43fe83 |
- unlink(data->othercareq.filename);
|
|
|
43fe83 |
- unlink(data->clientreq.filename);
|
|
|
43fe83 |
- unlink(data->serverreq.filename);
|
|
|
43fe83 |
- }
|
|
|
43fe83 |
- VIR_FORCE_CLOSE(channel[0]);
|
|
|
43fe83 |
- VIR_FORCE_CLOSE(channel[1]);
|
|
|
43fe83 |
- return ret;
|
|
|
43fe83 |
-}
|
|
|
43fe83 |
-
|
|
|
43fe83 |
-
|
|
|
43fe83 |
static int
|
|
|
43fe83 |
mymain(void)
|
|
|
43fe83 |
{
|
|
|
43fe83 |
int ret = 0;
|
|
|
43fe83 |
- if (asn1_array2tree(pkix_asn1_tab, &pkix_asn1, NULL) != ASN1_SUCCESS)
|
|
|
43fe83 |
- abort();
|
|
|
43fe83 |
|
|
|
43fe83 |
- gnutls_global_init();
|
|
|
43fe83 |
-
|
|
|
43fe83 |
- privkey = testTLSLoadKey();
|
|
|
43fe83 |
-
|
|
|
43fe83 |
- if (virFileWriteStr(keyfile, PRIVATE_KEY, 0600) < 0)
|
|
|
43fe83 |
- return EXIT_FAILURE;
|
|
|
43fe83 |
+ testTLSInit();
|
|
|
43fe83 |
|
|
|
43fe83 |
# define DO_CTX_TEST(_isServer, _caReq, _certReq, _expectFail) \
|
|
|
43fe83 |
do { \
|
|
|
43fe83 |
@@ -763,40 +128,6 @@ mymain(void)
|
|
|
43fe83 |
ret = -1; \
|
|
|
43fe83 |
} while (0)
|
|
|
43fe83 |
|
|
|
43fe83 |
-# define DO_SESS_TEST(_caReq, _serverReq, _clientReq, _expectServerFail,\
|
|
|
43fe83 |
- _expectClientFail, _hostname, _wildcards) \
|
|
|
43fe83 |
- do { \
|
|
|
43fe83 |
- static struct testTLSSessionData data; \
|
|
|
43fe83 |
- static struct testTLSCertReq other; \
|
|
|
43fe83 |
- data.careq = _caReq; \
|
|
|
43fe83 |
- data.othercareq = other; \
|
|
|
43fe83 |
- data.serverreq = _serverReq; \
|
|
|
43fe83 |
- data.clientreq = _clientReq; \
|
|
|
43fe83 |
- data.expectServerFail = _expectServerFail; \
|
|
|
43fe83 |
- data.expectClientFail = _expectClientFail; \
|
|
|
43fe83 |
- data.hostname = _hostname; \
|
|
|
43fe83 |
- data.wildcards = _wildcards; \
|
|
|
43fe83 |
- if (virtTestRun("TLS Session", 1, testTLSSessionInit, &data) < 0) \
|
|
|
43fe83 |
- ret = -1; \
|
|
|
43fe83 |
- } while (0)
|
|
|
43fe83 |
-
|
|
|
43fe83 |
-# define DO_SESS_TEST_EXT(_caReq, _othercaReq, _serverReq, _clientReq, \
|
|
|
43fe83 |
- _expectServerFail, _expectClientFail, \
|
|
|
43fe83 |
- _hostname, _wildcards) \
|
|
|
43fe83 |
- do { \
|
|
|
43fe83 |
- static struct testTLSSessionData data; \
|
|
|
43fe83 |
- data.careq = _caReq; \
|
|
|
43fe83 |
- data.othercareq = _othercaReq; \
|
|
|
43fe83 |
- data.serverreq = _serverReq; \
|
|
|
43fe83 |
- data.clientreq = _clientReq; \
|
|
|
43fe83 |
- data.expectServerFail = _expectServerFail; \
|
|
|
43fe83 |
- data.expectClientFail = _expectClientFail; \
|
|
|
43fe83 |
- data.hostname = _hostname; \
|
|
|
43fe83 |
- data.wildcards = _wildcards; \
|
|
|
43fe83 |
- if (virtTestRun("TLS Session", 1, testTLSSessionInit, &data) < 0) \
|
|
|
43fe83 |
- ret = -1; \
|
|
|
43fe83 |
- } while (0)
|
|
|
43fe83 |
-
|
|
|
43fe83 |
/* A perfect CA, perfect client & perfect server */
|
|
|
43fe83 |
|
|
|
43fe83 |
/* Basic:CA:critical */
|
|
|
43fe83 |
@@ -850,24 +181,10 @@ mymain(void)
|
|
|
43fe83 |
false, false, NULL, NULL,
|
|
|
43fe83 |
0, 0,
|
|
|
43fe83 |
};
|
|
|
43fe83 |
- /* Basic:not-CA:not-critical */
|
|
|
43fe83 |
-# if 0
|
|
|
43fe83 |
- /* Default GNUTLS session config forbids use of CAs without
|
|
|
43fe83 |
- * basic constraints, so skip this otherwise valid test
|
|
|
43fe83 |
- */
|
|
|
43fe83 |
+ /* Key usage:cert-sign:critical */
|
|
|
43fe83 |
static struct testTLSCertReq cacert3req = {
|
|
|
43fe83 |
NULL, NULL, "cacert3.pem", "UK",
|
|
|
43fe83 |
"libvirt CA 3", NULL, NULL, NULL, NULL,
|
|
|
43fe83 |
- true, false, false,
|
|
|
43fe83 |
- false, false, 0,
|
|
|
43fe83 |
- false, false, NULL, NULL,
|
|
|
43fe83 |
- 0, 0,
|
|
|
43fe83 |
- };
|
|
|
43fe83 |
-# endif
|
|
|
43fe83 |
- /* Key usage:cert-sign:critical */
|
|
|
43fe83 |
- static struct testTLSCertReq cacert4req = {
|
|
|
43fe83 |
- NULL, NULL, "cacert4.pem", "UK",
|
|
|
43fe83 |
- "libvirt CA 4", NULL, NULL, NULL, NULL,
|
|
|
43fe83 |
true, true, true,
|
|
|
43fe83 |
true, true, GNUTLS_KEY_KEY_CERT_SIGN,
|
|
|
43fe83 |
false, false, NULL, NULL,
|
|
|
43fe83 |
@@ -876,35 +193,32 @@ mymain(void)
|
|
|
43fe83 |
|
|
|
43fe83 |
DO_CTX_TEST(true, cacert1req, servercertreq, false);
|
|
|
43fe83 |
DO_CTX_TEST(true, cacert2req, servercertreq, false);
|
|
|
43fe83 |
-# if 0
|
|
|
43fe83 |
DO_CTX_TEST(true, cacert3req, servercertreq, false);
|
|
|
43fe83 |
-# endif
|
|
|
43fe83 |
- DO_CTX_TEST(true, cacert4req, servercertreq, false);
|
|
|
43fe83 |
|
|
|
43fe83 |
/* Now some bad certs */
|
|
|
43fe83 |
|
|
|
43fe83 |
/* Key usage:dig-sig:not-critical */
|
|
|
43fe83 |
- static struct testTLSCertReq cacert5req = {
|
|
|
43fe83 |
- NULL, NULL, "cacert5.pem", "UK",
|
|
|
43fe83 |
- "libvirt CA 5", NULL, NULL, NULL, NULL,
|
|
|
43fe83 |
+ static struct testTLSCertReq cacert4req = {
|
|
|
43fe83 |
+ NULL, NULL, "cacert4.pem", "UK",
|
|
|
43fe83 |
+ "libvirt CA 4", NULL, NULL, NULL, NULL,
|
|
|
43fe83 |
true, true, true,
|
|
|
43fe83 |
true, false, GNUTLS_KEY_DIGITAL_SIGNATURE,
|
|
|
43fe83 |
false, false, NULL, NULL,
|
|
|
43fe83 |
0, 0,
|
|
|
43fe83 |
};
|
|
|
43fe83 |
/* no-basic */
|
|
|
43fe83 |
- static struct testTLSCertReq cacert6req = {
|
|
|
43fe83 |
- NULL, NULL, "cacert6.pem", "UK",
|
|
|
43fe83 |
- "libvirt CA 6", NULL, NULL, NULL, NULL,
|
|
|
43fe83 |
+ static struct testTLSCertReq cacert5req = {
|
|
|
43fe83 |
+ NULL, NULL, "cacert5.pem", "UK",
|
|
|
43fe83 |
+ "libvirt CA 5", NULL, NULL, NULL, NULL,
|
|
|
43fe83 |
false, false, false,
|
|
|
43fe83 |
false, false, 0,
|
|
|
43fe83 |
false, false, NULL, NULL,
|
|
|
43fe83 |
0, 0,
|
|
|
43fe83 |
};
|
|
|
43fe83 |
/* Key usage:dig-sig:critical */
|
|
|
43fe83 |
- static struct testTLSCertReq cacert7req = {
|
|
|
43fe83 |
- NULL, NULL, "cacert7.pem", "UK",
|
|
|
43fe83 |
- "libvirt CA 7", NULL, NULL, NULL, NULL,
|
|
|
43fe83 |
+ static struct testTLSCertReq cacert6req = {
|
|
|
43fe83 |
+ NULL, NULL, "cacert6.pem", "UK",
|
|
|
43fe83 |
+ "libvirt CA 6", NULL, NULL, NULL, NULL,
|
|
|
43fe83 |
true, true, true,
|
|
|
43fe83 |
true, true, GNUTLS_KEY_DIGITAL_SIGNATURE,
|
|
|
43fe83 |
false, false, NULL, NULL,
|
|
|
43fe83 |
@@ -916,9 +230,9 @@ mymain(void)
|
|
|
43fe83 |
* be rejected. GNUTLS < 3 does not reject it and
|
|
|
43fe83 |
* we don't anticipate them changing this behaviour
|
|
|
43fe83 |
*/
|
|
|
43fe83 |
- DO_CTX_TEST(true, cacert5req, servercertreq, GNUTLS_VERSION_MAJOR >= 3);
|
|
|
43fe83 |
+ DO_CTX_TEST(true, cacert4req, servercertreq, GNUTLS_VERSION_MAJOR >= 3);
|
|
|
43fe83 |
+ DO_CTX_TEST(true, cacert5req, servercertreq, true);
|
|
|
43fe83 |
DO_CTX_TEST(true, cacert6req, servercertreq, true);
|
|
|
43fe83 |
- DO_CTX_TEST(true, cacert7req, servercertreq, true);
|
|
|
43fe83 |
|
|
|
43fe83 |
|
|
|
43fe83 |
/* Various good servers */
|
|
|
43fe83 |
@@ -1200,74 +514,7 @@ mymain(void)
|
|
|
43fe83 |
DO_CTX_TEST(true, cacertreq, servercertnewreq, true);
|
|
|
43fe83 |
DO_CTX_TEST(false, cacertreq, clientcertnewreq, true);
|
|
|
43fe83 |
|
|
|
43fe83 |
-
|
|
|
43fe83 |
- DO_SESS_TEST(cacertreq, servercertreq, clientcertreq, false, false, "libvirt.org", NULL);
|
|
|
43fe83 |
- DO_SESS_TEST_EXT(cacertreq, cacert1req, servercertreq, clientcertreq, true, true, "libvirt.org", NULL);
|
|
|
43fe83 |
-
|
|
|
43fe83 |
- /* When an altname is set, the CN is ignored, so it must be duplicated
|
|
|
43fe83 |
- * as an altname for it to match */
|
|
|
43fe83 |
- static struct testTLSCertReq servercertalt1req = {
|
|
|
43fe83 |
- NULL, NULL, "servercert.pem", "UK",
|
|
|
43fe83 |
- "libvirt.org", "www.libvirt.org", "libvirt.org", "192.168.122.1", "fec0::dead:beaf",
|
|
|
43fe83 |
- true, true, false,
|
|
|
43fe83 |
- true, true, GNUTLS_KEY_DIGITAL_SIGNATURE | GNUTLS_KEY_KEY_ENCIPHERMENT,
|
|
|
43fe83 |
- true, true, GNUTLS_KP_TLS_WWW_SERVER, NULL,
|
|
|
43fe83 |
- 0, 0,
|
|
|
43fe83 |
- };
|
|
|
43fe83 |
- /* This intentionally doesn't replicate */
|
|
|
43fe83 |
- static struct testTLSCertReq servercertalt2req = {
|
|
|
43fe83 |
- NULL, NULL, "servercert.pem", "UK",
|
|
|
43fe83 |
- "libvirt.org", "www.libvirt.org", "wiki.libvirt.org", "192.168.122.1", "fec0::dead:beaf",
|
|
|
43fe83 |
- true, true, false,
|
|
|
43fe83 |
- true, true, GNUTLS_KEY_DIGITAL_SIGNATURE | GNUTLS_KEY_KEY_ENCIPHERMENT,
|
|
|
43fe83 |
- true, true, GNUTLS_KP_TLS_WWW_SERVER, NULL,
|
|
|
43fe83 |
- 0, 0,
|
|
|
43fe83 |
- };
|
|
|
43fe83 |
-
|
|
|
43fe83 |
- DO_SESS_TEST(cacertreq, servercertalt1req, clientcertreq, false, false, "libvirt.org", NULL);
|
|
|
43fe83 |
- DO_SESS_TEST(cacertreq, servercertalt1req, clientcertreq, false, false, "www.libvirt.org", NULL);
|
|
|
43fe83 |
- DO_SESS_TEST(cacertreq, servercertalt1req, clientcertreq, false, true, "wiki.libvirt.org", NULL);
|
|
|
43fe83 |
-
|
|
|
43fe83 |
- DO_SESS_TEST(cacertreq, servercertalt2req, clientcertreq, false, true, "libvirt.org", NULL);
|
|
|
43fe83 |
- DO_SESS_TEST(cacertreq, servercertalt2req, clientcertreq, false, false, "www.libvirt.org", NULL);
|
|
|
43fe83 |
- DO_SESS_TEST(cacertreq, servercertalt2req, clientcertreq, false, false, "wiki.libvirt.org", NULL);
|
|
|
43fe83 |
-
|
|
|
43fe83 |
- const char *const wildcards1[] = {
|
|
|
43fe83 |
- "C=UK,CN=dogfood",
|
|
|
43fe83 |
- NULL,
|
|
|
43fe83 |
- };
|
|
|
43fe83 |
- const char *const wildcards2[] = {
|
|
|
43fe83 |
- "C=UK,CN=libvirt",
|
|
|
43fe83 |
- NULL,
|
|
|
43fe83 |
- };
|
|
|
43fe83 |
- const char *const wildcards3[] = {
|
|
|
43fe83 |
- "C=UK,CN=dogfood",
|
|
|
43fe83 |
- "C=UK,CN=libvirt",
|
|
|
43fe83 |
- NULL,
|
|
|
43fe83 |
- };
|
|
|
43fe83 |
- const char *const wildcards4[] = {
|
|
|
43fe83 |
- "C=UK,CN=libvirtstuff",
|
|
|
43fe83 |
- NULL,
|
|
|
43fe83 |
- };
|
|
|
43fe83 |
- const char *const wildcards5[] = {
|
|
|
43fe83 |
- "C=UK,CN=libvirt*",
|
|
|
43fe83 |
- NULL,
|
|
|
43fe83 |
- };
|
|
|
43fe83 |
- const char *const wildcards6[] = {
|
|
|
43fe83 |
- "C=UK,CN=*virt*",
|
|
|
43fe83 |
- NULL,
|
|
|
43fe83 |
- };
|
|
|
43fe83 |
-
|
|
|
43fe83 |
- DO_SESS_TEST(cacertreq, servercertreq, clientcertreq, true, false, "libvirt.org", wildcards1);
|
|
|
43fe83 |
- DO_SESS_TEST(cacertreq, servercertreq, clientcertreq, false, false, "libvirt.org", wildcards2);
|
|
|
43fe83 |
- DO_SESS_TEST(cacertreq, servercertreq, clientcertreq, false, false, "libvirt.org", wildcards3);
|
|
|
43fe83 |
- DO_SESS_TEST(cacertreq, servercertreq, clientcertreq, true, false, "libvirt.org", wildcards4);
|
|
|
43fe83 |
- DO_SESS_TEST(cacertreq, servercertreq, clientcertreq, false, false, "libvirt.org", wildcards5);
|
|
|
43fe83 |
- DO_SESS_TEST(cacertreq, servercertreq, clientcertreq, false, false, "libvirt.org", wildcards6);
|
|
|
43fe83 |
-
|
|
|
43fe83 |
- unlink(keyfile);
|
|
|
43fe83 |
-
|
|
|
43fe83 |
- asn1_delete_structure(&pkix_asn1);
|
|
|
43fe83 |
+ testTLSCleanup();
|
|
|
43fe83 |
|
|
|
43fe83 |
return ret==0 ? EXIT_SUCCESS : EXIT_FAILURE;
|
|
|
43fe83 |
}
|
|
|
43fe83 |
diff --git a/tests/virnettlshelpers.c b/tests/virnettlshelpers.c
|
|
|
43fe83 |
new file mode 100644
|
|
|
43fe83 |
index 0000000..96b2f6e
|
|
|
43fe83 |
--- /dev/null
|
|
|
43fe83 |
+++ b/tests/virnettlshelpers.c
|
|
|
43fe83 |
@@ -0,0 +1,420 @@
|
|
|
43fe83 |
+/*
|
|
|
43fe83 |
+ * Copyright (C) 2011-2012 Red Hat, Inc.
|
|
|
43fe83 |
+ *
|
|
|
43fe83 |
+ * This library is free software; you can redistribute it and/or
|
|
|
43fe83 |
+ * modify it under the terms of the GNU Lesser General Public
|
|
|
43fe83 |
+ * License as published by the Free Software Foundation; either
|
|
|
43fe83 |
+ * version 2.1 of the License, or (at your option) any later version.
|
|
|
43fe83 |
+ *
|
|
|
43fe83 |
+ * This library is distributed in the hope that it will be useful,
|
|
|
43fe83 |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
43fe83 |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
43fe83 |
+ * Lesser General Public License for more details.
|
|
|
43fe83 |
+ *
|
|
|
43fe83 |
+ * You should have received a copy of the GNU Lesser General Public
|
|
|
43fe83 |
+ * License along with this library. If not, see
|
|
|
43fe83 |
+ * <http://www.gnu.org/licenses/>.
|
|
|
43fe83 |
+ *
|
|
|
43fe83 |
+ * Author: Daniel P. Berrange <berrange@redhat.com>
|
|
|
43fe83 |
+ */
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+#include <config.h>
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+#include <stdlib.h>
|
|
|
43fe83 |
+#include <fcntl.h>
|
|
|
43fe83 |
+#include <sys/socket.h>
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+#include "virnettlshelpers.h"
|
|
|
43fe83 |
+#include "viralloc.h"
|
|
|
43fe83 |
+#include "virlog.h"
|
|
|
43fe83 |
+#include "virfile.h"
|
|
|
43fe83 |
+#include "virsocketaddr.h"
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+#if !defined WIN32 && HAVE_LIBTASN1_H && LIBGNUTLS_VERSION_NUMBER >= 0x020600
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+# define VIR_FROM_THIS VIR_FROM_RPC
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+const char *keyfile = abs_builddir "/virnettlscontexttest-key.pem";
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+/*
|
|
|
43fe83 |
+ * These store some static data that is needed when
|
|
|
43fe83 |
+ * encoding extensions in the x509 certs
|
|
|
43fe83 |
+ */
|
|
|
43fe83 |
+ASN1_TYPE pkix_asn1;
|
|
|
43fe83 |
+extern const ASN1_ARRAY_TYPE pkix_asn1_tab[];
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+/*
|
|
|
43fe83 |
+ * To avoid consuming random entropy to generate keys,
|
|
|
43fe83 |
+ * here's one we prepared earlier :-)
|
|
|
43fe83 |
+ */
|
|
|
43fe83 |
+gnutls_x509_privkey_t privkey;
|
|
|
43fe83 |
+# define PRIVATE_KEY \
|
|
|
43fe83 |
+ "-----BEGIN PRIVATE KEY-----\n" \
|
|
|
43fe83 |
+ "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBALVcr\n" \
|
|
|
43fe83 |
+ "BL40Tm6yq88FBhJNw1aaoCjmtg0l4dWQZ/e9Fimx4ARxFpT+ji4FE\n" \
|
|
|
43fe83 |
+ "Cgl9s/SGqC+1nvlkm9ViSo0j7MKDbnDB+VRHDvMAzQhA2X7e8M0n9\n" \
|
|
|
43fe83 |
+ "rPolUY2lIVC83q0BBaOBkCj2RSmT2xTEbbC2xLukSrg2WP/ihVOxc\n" \
|
|
|
43fe83 |
+ "kXRuyFtzAgMBAAECgYB7slBexDwXrtItAMIH6m/U+LUpNe0Xx48OL\n" \
|
|
|
43fe83 |
+ "IOn4a4whNgO/o84uIwygUK27ZGFZT0kAGAk8CdF9hA6ArcbQ62s1H\n" \
|
|
|
43fe83 |
+ "myxrUbF9/mrLsQw1NEqpuUk9Ay2Tx5U/wPx35S3W/X2AvR/ZpTnCn\n" \
|
|
|
43fe83 |
+ "2q/7ym9fyiSoj86drD7BTvmKXlOnOwQJBAPOFMp4mMa9NGpGuEssO\n" \
|
|
|
43fe83 |
+ "m3Uwbp6lhcP0cA9MK+iOmeANpoKWfBdk5O34VbmeXnGYWEkrnX+9J\n" \
|
|
|
43fe83 |
+ "bM4wVhnnBWtgBMCQQC+qAEmvwcfhauERKYznMVUVksyeuhxhCe7EK\n" \
|
|
|
43fe83 |
+ "mPh+U2+g0WwdKvGDgO0PPt1gq0ILEjspMDeMHVdTwkaVBo/uMhAkA\n" \
|
|
|
43fe83 |
+ "Z5SsZyCP2aTOPFDypXRdI4eqRcjaEPOUBq27r3uYb/jeboVb2weLa\n" \
|
|
|
43fe83 |
+ "L1MmVuHiIHoa5clswPdWVI2y0em2IGoDAkBPSp/v9VKJEZabk9Frd\n" \
|
|
|
43fe83 |
+ "a+7u4fanrM9QrEjY3KhduslSilXZZSxrWjjAJPyPiqFb3M8XXA26W\n" \
|
|
|
43fe83 |
+ "nz1KYGnqYKhLcBAkB7dt57n9xfrhDpuyVEv+Uv1D3VVAhZlsaZ5Pp\n" \
|
|
|
43fe83 |
+ "dcrhrkJn2sa/+O8OKvdrPSeeu/N5WwYhJf61+CPoenMp7IFci\n" \
|
|
|
43fe83 |
+ "-----END PRIVATE KEY-----\n"
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+/*
|
|
|
43fe83 |
+ * This loads the private key we defined earlier
|
|
|
43fe83 |
+ */
|
|
|
43fe83 |
+static gnutls_x509_privkey_t testTLSLoadKey(void)
|
|
|
43fe83 |
+{
|
|
|
43fe83 |
+ gnutls_x509_privkey_t key;
|
|
|
43fe83 |
+ const gnutls_datum_t data = { (unsigned char *)PRIVATE_KEY, strlen(PRIVATE_KEY) };
|
|
|
43fe83 |
+ int err;
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+ if ((err = gnutls_x509_privkey_init(&key)) < 0) {
|
|
|
43fe83 |
+ VIR_WARN("Failed to init key %s", gnutls_strerror(err));
|
|
|
43fe83 |
+ abort();
|
|
|
43fe83 |
+ }
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+ if ((err = gnutls_x509_privkey_import(key, &data,
|
|
|
43fe83 |
+ GNUTLS_X509_FMT_PEM)) < 0) {
|
|
|
43fe83 |
+ if (err != GNUTLS_E_BASE64_UNEXPECTED_HEADER_ERROR) {
|
|
|
43fe83 |
+ VIR_WARN("Failed to import key %s", gnutls_strerror(err));
|
|
|
43fe83 |
+ abort();
|
|
|
43fe83 |
+ }
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+ if ((err = gnutls_x509_privkey_import_pkcs8(key, &data, GNUTLS_X509_FMT_PEM, NULL, 0)) < 0) {
|
|
|
43fe83 |
+ VIR_WARN("Failed to import PKCS8 key %s", gnutls_strerror(err));
|
|
|
43fe83 |
+ abort();
|
|
|
43fe83 |
+ }
|
|
|
43fe83 |
+ }
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+ return key;
|
|
|
43fe83 |
+}
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+void testTLSInit(void)
|
|
|
43fe83 |
+{
|
|
|
43fe83 |
+ gnutls_global_init();
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+ if (asn1_array2tree(pkix_asn1_tab, &pkix_asn1, NULL) != ASN1_SUCCESS)
|
|
|
43fe83 |
+ abort();
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+ privkey = testTLSLoadKey();
|
|
|
43fe83 |
+ if (virFileWriteStr(keyfile, PRIVATE_KEY, 0600) < 0)
|
|
|
43fe83 |
+ abort();
|
|
|
43fe83 |
+}
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+void testTLSCleanup(void)
|
|
|
43fe83 |
+{
|
|
|
43fe83 |
+ asn1_delete_structure(&pkix_asn1);
|
|
|
43fe83 |
+ unlink(keyfile);
|
|
|
43fe83 |
+}
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+/*
|
|
|
43fe83 |
+ * Turns an ASN1 object into a DER encoded byte array
|
|
|
43fe83 |
+ */
|
|
|
43fe83 |
+static void testTLSDerEncode(ASN1_TYPE src,
|
|
|
43fe83 |
+ const char *src_name,
|
|
|
43fe83 |
+ gnutls_datum_t * res)
|
|
|
43fe83 |
+{
|
|
|
43fe83 |
+ int size;
|
|
|
43fe83 |
+ char *data = NULL;
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+ size = 0;
|
|
|
43fe83 |
+ asn1_der_coding(src, src_name, NULL, &size, NULL);
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+ if (VIR_ALLOC_N(data, size) < 0)
|
|
|
43fe83 |
+ abort();
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+ asn1_der_coding(src, src_name, data, &size, NULL);
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+ res->data = (unsigned char *)data;
|
|
|
43fe83 |
+ res->size = size;
|
|
|
43fe83 |
+}
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+/*
|
|
|
43fe83 |
+ * This is a fairly lame x509 certificate generator.
|
|
|
43fe83 |
+ *
|
|
|
43fe83 |
+ * Do not copy/use this code for generating real certificates
|
|
|
43fe83 |
+ * since it leaves out many things that you would want in
|
|
|
43fe83 |
+ * certificates for real world usage.
|
|
|
43fe83 |
+ *
|
|
|
43fe83 |
+ * This is good enough only for doing tests of the libvirt
|
|
|
43fe83 |
+ * TLS certificate code
|
|
|
43fe83 |
+ */
|
|
|
43fe83 |
+void
|
|
|
43fe83 |
+testTLSGenerateCert(struct testTLSCertReq *req)
|
|
|
43fe83 |
+{
|
|
|
43fe83 |
+ gnutls_x509_crt_t crt;
|
|
|
43fe83 |
+ int err;
|
|
|
43fe83 |
+ static char buffer[1024*1024];
|
|
|
43fe83 |
+ size_t size = sizeof(buffer);
|
|
|
43fe83 |
+ char serial[5] = { 1, 2, 3, 4, 0 };
|
|
|
43fe83 |
+ gnutls_datum_t der;
|
|
|
43fe83 |
+ time_t start = time(NULL) + (60*60*req->start_offset);
|
|
|
43fe83 |
+ time_t expire = time(NULL) + (60*60*(req->expire_offset
|
|
|
43fe83 |
+ ? req->expire_offset : 24));
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+ /*
|
|
|
43fe83 |
+ * Prepare our new certificate object
|
|
|
43fe83 |
+ */
|
|
|
43fe83 |
+ if ((err = gnutls_x509_crt_init(&crt)) < 0) {
|
|
|
43fe83 |
+ VIR_WARN("Failed to initialize certificate %s", gnutls_strerror(err));
|
|
|
43fe83 |
+ abort();
|
|
|
43fe83 |
+ }
|
|
|
43fe83 |
+ if ((err = gnutls_x509_crt_set_key(crt, privkey)) < 0) {
|
|
|
43fe83 |
+ VIR_WARN("Failed to set certificate key %s", gnutls_strerror(err));
|
|
|
43fe83 |
+ abort();
|
|
|
43fe83 |
+ }
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+ /*
|
|
|
43fe83 |
+ * A v3 certificate is required in order to be able
|
|
|
43fe83 |
+ * set any of the basic constraints, key purpose and
|
|
|
43fe83 |
+ * key usage data
|
|
|
43fe83 |
+ */
|
|
|
43fe83 |
+ gnutls_x509_crt_set_version(crt, 3);
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+ if (req->country) {
|
|
|
43fe83 |
+ if ((err = gnutls_x509_crt_set_dn_by_oid(crt, GNUTLS_OID_X520_COUNTRY_NAME, 0,
|
|
|
43fe83 |
+ req->country, strlen(req->country))) < 0) {
|
|
|
43fe83 |
+ VIR_WARN("Failed to set certificate country name %s", gnutls_strerror(err));
|
|
|
43fe83 |
+ abort();
|
|
|
43fe83 |
+ }
|
|
|
43fe83 |
+ }
|
|
|
43fe83 |
+ if (req->cn) {
|
|
|
43fe83 |
+ if ((err = gnutls_x509_crt_set_dn_by_oid(crt, GNUTLS_OID_X520_COMMON_NAME, 0,
|
|
|
43fe83 |
+ req->cn, strlen(req->cn))) < 0) {
|
|
|
43fe83 |
+ VIR_WARN("Failed to set certificate common name %s", gnutls_strerror(err));
|
|
|
43fe83 |
+ abort();
|
|
|
43fe83 |
+ }
|
|
|
43fe83 |
+ }
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+ /*
|
|
|
43fe83 |
+ * Setup the subject altnames, which are used
|
|
|
43fe83 |
+ * for hostname checks in live sessions
|
|
|
43fe83 |
+ */
|
|
|
43fe83 |
+ if (req->altname1) {
|
|
|
43fe83 |
+ if ((err = gnutls_x509_crt_set_subject_alt_name(crt, GNUTLS_SAN_DNSNAME,
|
|
|
43fe83 |
+ req->altname1,
|
|
|
43fe83 |
+ strlen(req->altname1),
|
|
|
43fe83 |
+ GNUTLS_FSAN_APPEND))) {
|
|
|
43fe83 |
+ VIR_WARN("Failed to set certificate alt name %s", gnutls_strerror(err));
|
|
|
43fe83 |
+ abort();
|
|
|
43fe83 |
+ }
|
|
|
43fe83 |
+ }
|
|
|
43fe83 |
+ if (req->altname2) {
|
|
|
43fe83 |
+ if ((err = gnutls_x509_crt_set_subject_alt_name(crt, GNUTLS_SAN_DNSNAME,
|
|
|
43fe83 |
+ req->altname2,
|
|
|
43fe83 |
+ strlen(req->altname2),
|
|
|
43fe83 |
+ GNUTLS_FSAN_APPEND))) {
|
|
|
43fe83 |
+ VIR_WARN("Failed to set certificate %s alt name", gnutls_strerror(err));
|
|
|
43fe83 |
+ abort();
|
|
|
43fe83 |
+ }
|
|
|
43fe83 |
+ }
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+ /*
|
|
|
43fe83 |
+ * IP address need to be put into the cert in their
|
|
|
43fe83 |
+ * raw byte form, not strings, hence this is a little
|
|
|
43fe83 |
+ * more complicated
|
|
|
43fe83 |
+ */
|
|
|
43fe83 |
+ if (req->ipaddr1) {
|
|
|
43fe83 |
+ virSocketAddr addr;
|
|
|
43fe83 |
+ char *data;
|
|
|
43fe83 |
+ int len;
|
|
|
43fe83 |
+ if (virSocketAddrParse(&addr, req->ipaddr1, 0) < 0) {
|
|
|
43fe83 |
+ VIR_WARN("Cannot parse %s", req->ipaddr1);
|
|
|
43fe83 |
+ abort();
|
|
|
43fe83 |
+ }
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+ if (addr.data.sa.sa_family == AF_INET) {
|
|
|
43fe83 |
+ data = (char*)&addr.data.inet4.sin_addr;
|
|
|
43fe83 |
+ len = 4;
|
|
|
43fe83 |
+ } else {
|
|
|
43fe83 |
+ data = (char*)&addr.data.inet6.sin6_addr;
|
|
|
43fe83 |
+ len = 16;
|
|
|
43fe83 |
+ }
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+ if ((err = gnutls_x509_crt_set_subject_alt_name(crt, GNUTLS_SAN_IPADDRESS,
|
|
|
43fe83 |
+ data, len, GNUTLS_FSAN_APPEND))) {
|
|
|
43fe83 |
+ VIR_WARN("Failed to set certificate alt name %s", gnutls_strerror(err));
|
|
|
43fe83 |
+ abort();
|
|
|
43fe83 |
+ }
|
|
|
43fe83 |
+ }
|
|
|
43fe83 |
+ if (req->ipaddr2) {
|
|
|
43fe83 |
+ virSocketAddr addr;
|
|
|
43fe83 |
+ char *data;
|
|
|
43fe83 |
+ int len;
|
|
|
43fe83 |
+ if (virSocketAddrParse(&addr, req->ipaddr2, 0) < 0) {
|
|
|
43fe83 |
+ VIR_WARN("Cannot parse %s", req->ipaddr2);
|
|
|
43fe83 |
+ abort();
|
|
|
43fe83 |
+ }
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+ if (addr.data.sa.sa_family == AF_INET) {
|
|
|
43fe83 |
+ data = (char*)&addr.data.inet4.sin_addr;
|
|
|
43fe83 |
+ len = 4;
|
|
|
43fe83 |
+ } else {
|
|
|
43fe83 |
+ data = (char*)&addr.data.inet6.sin6_addr;
|
|
|
43fe83 |
+ len = 16;
|
|
|
43fe83 |
+ }
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+ if ((err = gnutls_x509_crt_set_subject_alt_name(crt, GNUTLS_SAN_IPADDRESS,
|
|
|
43fe83 |
+ data, len, GNUTLS_FSAN_APPEND))) {
|
|
|
43fe83 |
+ VIR_WARN("Failed to set certificate alt name %s", gnutls_strerror(err));
|
|
|
43fe83 |
+ abort();
|
|
|
43fe83 |
+ }
|
|
|
43fe83 |
+ }
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+ /*
|
|
|
43fe83 |
+ * Basic constraints are used to decide if the cert
|
|
|
43fe83 |
+ * is for a CA or not. We can't use the convenient
|
|
|
43fe83 |
+ * gnutls API for setting this, since it hardcodes
|
|
|
43fe83 |
+ * the 'critical' field which we want control over
|
|
|
43fe83 |
+ */
|
|
|
43fe83 |
+ if (req->basicConstraintsEnable) {
|
|
|
43fe83 |
+ ASN1_TYPE ext = ASN1_TYPE_EMPTY;
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+ asn1_create_element(pkix_asn1, "PKIX1.BasicConstraints", &ext;;
|
|
|
43fe83 |
+ asn1_write_value(ext, "cA", req->basicConstraintsIsCA ? "TRUE" : "FALSE", 1);
|
|
|
43fe83 |
+ asn1_write_value(ext, "pathLenConstraint", NULL, 0);
|
|
|
43fe83 |
+ testTLSDerEncode(ext, "", &der;;
|
|
|
43fe83 |
+ if ((err = gnutls_x509_crt_set_extension_by_oid(crt,
|
|
|
43fe83 |
+ "2.5.29.19",
|
|
|
43fe83 |
+ der.data,
|
|
|
43fe83 |
+ der.size,
|
|
|
43fe83 |
+ req->basicConstraintsCritical)) < 0) {
|
|
|
43fe83 |
+ VIR_WARN("Failed to set certificate basic constraints %s", gnutls_strerror(err));
|
|
|
43fe83 |
+ VIR_FREE(der.data);
|
|
|
43fe83 |
+ abort();
|
|
|
43fe83 |
+ }
|
|
|
43fe83 |
+ asn1_delete_structure(&ext;;
|
|
|
43fe83 |
+ VIR_FREE(der.data);
|
|
|
43fe83 |
+ }
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+ /*
|
|
|
43fe83 |
+ * Next up the key usage extension. Again we can't
|
|
|
43fe83 |
+ * use the gnutls API since it hardcodes the extension
|
|
|
43fe83 |
+ * to be 'critical'
|
|
|
43fe83 |
+ */
|
|
|
43fe83 |
+ if (req->keyUsageEnable) {
|
|
|
43fe83 |
+ ASN1_TYPE ext = ASN1_TYPE_EMPTY;
|
|
|
43fe83 |
+ char str[2];
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+ str[0] = req->keyUsageValue & 0xff;
|
|
|
43fe83 |
+ str[1] = (req->keyUsageValue >> 8) & 0xff;
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+ asn1_create_element(pkix_asn1, "PKIX1.KeyUsage", &ext;;
|
|
|
43fe83 |
+ asn1_write_value(ext, "", str, 9);
|
|
|
43fe83 |
+ testTLSDerEncode(ext, "", &der;;
|
|
|
43fe83 |
+ if ((err = gnutls_x509_crt_set_extension_by_oid(crt,
|
|
|
43fe83 |
+ "2.5.29.15",
|
|
|
43fe83 |
+ der.data,
|
|
|
43fe83 |
+ der.size,
|
|
|
43fe83 |
+ req->keyUsageCritical)) < 0) {
|
|
|
43fe83 |
+ VIR_WARN("Failed to set certificate key usage %s", gnutls_strerror(err));
|
|
|
43fe83 |
+ VIR_FREE(der.data);
|
|
|
43fe83 |
+ abort();
|
|
|
43fe83 |
+ }
|
|
|
43fe83 |
+ asn1_delete_structure(&ext;;
|
|
|
43fe83 |
+ VIR_FREE(der.data);
|
|
|
43fe83 |
+ }
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+ /*
|
|
|
43fe83 |
+ * Finally the key purpose extension. This time
|
|
|
43fe83 |
+ * gnutls has the opposite problem, always hardcoding
|
|
|
43fe83 |
+ * it to be non-critical. So once again we have to
|
|
|
43fe83 |
+ * set this the hard way building up ASN1 data ourselves
|
|
|
43fe83 |
+ */
|
|
|
43fe83 |
+ if (req->keyPurposeEnable) {
|
|
|
43fe83 |
+ ASN1_TYPE ext = ASN1_TYPE_EMPTY;
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+ asn1_create_element(pkix_asn1, "PKIX1.ExtKeyUsageSyntax", &ext;;
|
|
|
43fe83 |
+ if (req->keyPurposeOID1) {
|
|
|
43fe83 |
+ asn1_write_value(ext, "", "NEW", 1);
|
|
|
43fe83 |
+ asn1_write_value(ext, "?LAST", req->keyPurposeOID1, 1);
|
|
|
43fe83 |
+ }
|
|
|
43fe83 |
+ if (req->keyPurposeOID2) {
|
|
|
43fe83 |
+ asn1_write_value(ext, "", "NEW", 1);
|
|
|
43fe83 |
+ asn1_write_value(ext, "?LAST", req->keyPurposeOID2, 1);
|
|
|
43fe83 |
+ }
|
|
|
43fe83 |
+ testTLSDerEncode(ext, "", &der;;
|
|
|
43fe83 |
+ if ((err = gnutls_x509_crt_set_extension_by_oid(crt,
|
|
|
43fe83 |
+ "2.5.29.37",
|
|
|
43fe83 |
+ der.data,
|
|
|
43fe83 |
+ der.size,
|
|
|
43fe83 |
+ req->keyPurposeCritical)) < 0) {
|
|
|
43fe83 |
+ VIR_WARN("Failed to set certificate key purpose %s", gnutls_strerror(err));
|
|
|
43fe83 |
+ VIR_FREE(der.data);
|
|
|
43fe83 |
+ abort();
|
|
|
43fe83 |
+ }
|
|
|
43fe83 |
+ asn1_delete_structure(&ext;;
|
|
|
43fe83 |
+ VIR_FREE(der.data);
|
|
|
43fe83 |
+ }
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+ /*
|
|
|
43fe83 |
+ * Any old serial number will do, so lets pick 5
|
|
|
43fe83 |
+ */
|
|
|
43fe83 |
+ if ((err = gnutls_x509_crt_set_serial(crt, serial, 5)) < 0) {
|
|
|
43fe83 |
+ VIR_WARN("Failed to set certificate serial %s", gnutls_strerror(err));
|
|
|
43fe83 |
+ abort();
|
|
|
43fe83 |
+ }
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+ if ((err = gnutls_x509_crt_set_activation_time(crt, start)) < 0) {
|
|
|
43fe83 |
+ VIR_WARN("Failed to set certificate activation %s", gnutls_strerror(err));
|
|
|
43fe83 |
+ abort();
|
|
|
43fe83 |
+ }
|
|
|
43fe83 |
+ if ((err = gnutls_x509_crt_set_expiration_time(crt, expire)) < 0) {
|
|
|
43fe83 |
+ VIR_WARN("Failed to set certificate expiration %s", gnutls_strerror(err));
|
|
|
43fe83 |
+ abort();
|
|
|
43fe83 |
+ }
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+ /*
|
|
|
43fe83 |
+ * If no 'cart' is set then we are self signing
|
|
|
43fe83 |
+ * the cert. This is done for CA certs
|
|
|
43fe83 |
+ */
|
|
|
43fe83 |
+ if ((err = gnutls_x509_crt_sign(crt, req->cacrt ? req->cacrt : crt, privkey) < 0)) {
|
|
|
43fe83 |
+ VIR_WARN("Failed to sign certificate %s", gnutls_strerror(err));
|
|
|
43fe83 |
+ abort();
|
|
|
43fe83 |
+ }
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+ /*
|
|
|
43fe83 |
+ * Finally write the new cert out to disk
|
|
|
43fe83 |
+ */
|
|
|
43fe83 |
+ if ((err = gnutls_x509_crt_export(crt, GNUTLS_X509_FMT_PEM, buffer, &size) < 0)) {
|
|
|
43fe83 |
+ VIR_WARN("Failed to export certificate %s", gnutls_strerror(err));
|
|
|
43fe83 |
+ abort();
|
|
|
43fe83 |
+ }
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+ if (virFileWriteStr(req->filename, buffer, 0600) < 0) {
|
|
|
43fe83 |
+ VIR_WARN("Failed to write certificate %s %s", req->filename, gnutls_strerror(err));
|
|
|
43fe83 |
+ abort();
|
|
|
43fe83 |
+ }
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+ req->crt = crt;
|
|
|
43fe83 |
+ return;
|
|
|
43fe83 |
+}
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+void testTLSDiscardCert(struct testTLSCertReq *req)
|
|
|
43fe83 |
+{
|
|
|
43fe83 |
+ if (!req->crt)
|
|
|
43fe83 |
+ return;
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+ gnutls_x509_crt_deinit(req->crt);
|
|
|
43fe83 |
+ req->crt = NULL;
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+ if (getenv("VIRT_TEST_DEBUG_CERTS") == NULL)
|
|
|
43fe83 |
+ unlink(req->filename);
|
|
|
43fe83 |
+}
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+#endif
|
|
|
43fe83 |
diff --git a/tests/virnettlshelpers.h b/tests/virnettlshelpers.h
|
|
|
43fe83 |
new file mode 100644
|
|
|
43fe83 |
index 0000000..3ea9978
|
|
|
43fe83 |
--- /dev/null
|
|
|
43fe83 |
+++ b/tests/virnettlshelpers.h
|
|
|
43fe83 |
@@ -0,0 +1,79 @@
|
|
|
43fe83 |
+/*
|
|
|
43fe83 |
+ * Copyright (C) 2011-2013 Red Hat, Inc.
|
|
|
43fe83 |
+ *
|
|
|
43fe83 |
+ * This library is free software; you can redistribute it and/or
|
|
|
43fe83 |
+ * modify it under the terms of the GNU Lesser General Public
|
|
|
43fe83 |
+ * License as published by the Free Software Foundation; either
|
|
|
43fe83 |
+ * version 2.1 of the License, or (at your option) any later version.
|
|
|
43fe83 |
+ *
|
|
|
43fe83 |
+ * This library is distributed in the hope that it will be useful,
|
|
|
43fe83 |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
43fe83 |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
43fe83 |
+ * Lesser General Public License for more details.
|
|
|
43fe83 |
+ *
|
|
|
43fe83 |
+ * You should have received a copy of the GNU Lesser General Public
|
|
|
43fe83 |
+ * License along with this library. If not, see
|
|
|
43fe83 |
+ * <http://www.gnu.org/licenses/>.
|
|
|
43fe83 |
+ *
|
|
|
43fe83 |
+ * Author: Daniel P. Berrange <berrange@redhat.com>
|
|
|
43fe83 |
+ */
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+#include <gnutls/gnutls.h>
|
|
|
43fe83 |
+#include <gnutls/x509.h>
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+#if !defined WIN32 && HAVE_LIBTASN1_H && LIBGNUTLS_VERSION_NUMBER >= 0x020600
|
|
|
43fe83 |
+# include "gnutls_1_0_compat.h"
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+# include <libtasn1.h>
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+# include "rpc/virnettlscontext.h"
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+extern const char *keyfile;
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+/*
|
|
|
43fe83 |
+ * This contains parameter about how to generate
|
|
|
43fe83 |
+ * certificates.
|
|
|
43fe83 |
+ */
|
|
|
43fe83 |
+struct testTLSCertReq {
|
|
|
43fe83 |
+ gnutls_x509_crt_t crt;
|
|
|
43fe83 |
+ gnutls_x509_crt_t cacrt; /* If not set, then the cert will be self-signed */
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+ const char *filename;
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+ /* Identifying information */
|
|
|
43fe83 |
+ const char *country;
|
|
|
43fe83 |
+ const char *cn;
|
|
|
43fe83 |
+ const char *altname1;
|
|
|
43fe83 |
+ const char *altname2;
|
|
|
43fe83 |
+ const char *ipaddr1;
|
|
|
43fe83 |
+ const char *ipaddr2;
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+ /* Basic constraints */
|
|
|
43fe83 |
+ bool basicConstraintsEnable;
|
|
|
43fe83 |
+ bool basicConstraintsCritical;
|
|
|
43fe83 |
+ bool basicConstraintsIsCA;
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+ /* Key usage */
|
|
|
43fe83 |
+ bool keyUsageEnable;
|
|
|
43fe83 |
+ bool keyUsageCritical;
|
|
|
43fe83 |
+ int keyUsageValue;
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+ /* Key purpose (aka Extended key usage) */
|
|
|
43fe83 |
+ bool keyPurposeEnable;
|
|
|
43fe83 |
+ bool keyPurposeCritical;
|
|
|
43fe83 |
+ const char *keyPurposeOID1;
|
|
|
43fe83 |
+ const char *keyPurposeOID2;
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+ /* zero for current time, or non-zero for hours from now */
|
|
|
43fe83 |
+ int start_offset;
|
|
|
43fe83 |
+ /* zero for 24 hours from now, or non-zero for hours from now */
|
|
|
43fe83 |
+ int expire_offset;
|
|
|
43fe83 |
+};
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+void testTLSGenerateCert(struct testTLSCertReq *req);
|
|
|
43fe83 |
+void testTLSDiscardCert(struct testTLSCertReq *req);
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+void testTLSInit(void);
|
|
|
43fe83 |
+void testTLSCleanup(void);
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+#endif
|
|
|
43fe83 |
diff --git a/tests/virnettlssessiontest.c b/tests/virnettlssessiontest.c
|
|
|
43fe83 |
new file mode 100644
|
|
|
43fe83 |
index 0000000..9c5b3ca
|
|
|
43fe83 |
--- /dev/null
|
|
|
43fe83 |
+++ b/tests/virnettlssessiontest.c
|
|
|
43fe83 |
@@ -0,0 +1,414 @@
|
|
|
43fe83 |
+/*
|
|
|
43fe83 |
+ * Copyright (C) 2011-2012 Red Hat, Inc.
|
|
|
43fe83 |
+ *
|
|
|
43fe83 |
+ * This library is free software; you can redistribute it and/or
|
|
|
43fe83 |
+ * modify it under the terms of the GNU Lesser General Public
|
|
|
43fe83 |
+ * License as published by the Free Software Foundation; either
|
|
|
43fe83 |
+ * version 2.1 of the License, or (at your option) any later version.
|
|
|
43fe83 |
+ *
|
|
|
43fe83 |
+ * This library is distributed in the hope that it will be useful,
|
|
|
43fe83 |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
43fe83 |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
43fe83 |
+ * Lesser General Public License for more details.
|
|
|
43fe83 |
+ *
|
|
|
43fe83 |
+ * You should have received a copy of the GNU Lesser General Public
|
|
|
43fe83 |
+ * License along with this library. If not, see
|
|
|
43fe83 |
+ * <http://www.gnu.org/licenses/>.
|
|
|
43fe83 |
+ *
|
|
|
43fe83 |
+ * Author: Daniel P. Berrange <berrange@redhat.com>
|
|
|
43fe83 |
+ */
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+#include <config.h>
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+#include <stdlib.h>
|
|
|
43fe83 |
+#include <fcntl.h>
|
|
|
43fe83 |
+#include <sys/socket.h>
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+#include "testutils.h"
|
|
|
43fe83 |
+#include "virnettlshelpers.h"
|
|
|
43fe83 |
+#include "virutil.h"
|
|
|
43fe83 |
+#include "virerror.h"
|
|
|
43fe83 |
+#include "viralloc.h"
|
|
|
43fe83 |
+#include "virlog.h"
|
|
|
43fe83 |
+#include "virfile.h"
|
|
|
43fe83 |
+#include "vircommand.h"
|
|
|
43fe83 |
+#include "virsocketaddr.h"
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+#if !defined WIN32 && HAVE_LIBTASN1_H && LIBGNUTLS_VERSION_NUMBER >= 0x020600
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+# define VIR_FROM_THIS VIR_FROM_RPC
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+struct testTLSSessionData {
|
|
|
43fe83 |
+ struct testTLSCertReq careq;
|
|
|
43fe83 |
+ struct testTLSCertReq othercareq;
|
|
|
43fe83 |
+ struct testTLSCertReq serverreq;
|
|
|
43fe83 |
+ struct testTLSCertReq clientreq;
|
|
|
43fe83 |
+ bool expectServerFail;
|
|
|
43fe83 |
+ bool expectClientFail;
|
|
|
43fe83 |
+ const char *hostname;
|
|
|
43fe83 |
+ const char *const* wildcards;
|
|
|
43fe83 |
+};
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+static ssize_t testWrite(const char *buf, size_t len, void *opaque)
|
|
|
43fe83 |
+{
|
|
|
43fe83 |
+ int *fd = opaque;
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+ return write(*fd, buf, len);
|
|
|
43fe83 |
+}
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+static ssize_t testRead(char *buf, size_t len, void *opaque)
|
|
|
43fe83 |
+{
|
|
|
43fe83 |
+ int *fd = opaque;
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+ return read(*fd, buf, len);
|
|
|
43fe83 |
+}
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+/*
|
|
|
43fe83 |
+ * This tests validation checking of peer certificates
|
|
|
43fe83 |
+ *
|
|
|
43fe83 |
+ * This is replicating the checks that are done for an
|
|
|
43fe83 |
+ * active TLS session after handshake completes. To
|
|
|
43fe83 |
+ * simulate that we create our TLS contexts, skipping
|
|
|
43fe83 |
+ * sanity checks. When then get a socketpair, and
|
|
|
43fe83 |
+ * initiate a TLS session across them. Finally do
|
|
|
43fe83 |
+ * do actual cert validation tests
|
|
|
43fe83 |
+ */
|
|
|
43fe83 |
+static int testTLSSessionInit(const void *opaque)
|
|
|
43fe83 |
+{
|
|
|
43fe83 |
+ struct testTLSSessionData *data = (struct testTLSSessionData *)opaque;
|
|
|
43fe83 |
+ virNetTLSContextPtr clientCtxt = NULL;
|
|
|
43fe83 |
+ virNetTLSContextPtr serverCtxt = NULL;
|
|
|
43fe83 |
+ virNetTLSSessionPtr clientSess = NULL;
|
|
|
43fe83 |
+ virNetTLSSessionPtr serverSess = NULL;
|
|
|
43fe83 |
+ int ret = -1;
|
|
|
43fe83 |
+ int channel[2];
|
|
|
43fe83 |
+ bool clientShake = false;
|
|
|
43fe83 |
+ bool serverShake = false;
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+ /* We'll use this for our fake client-server connection */
|
|
|
43fe83 |
+ if (socketpair(AF_UNIX, SOCK_STREAM, 0, channel) < 0)
|
|
|
43fe83 |
+ abort();
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+ /*
|
|
|
43fe83 |
+ * We have an evil loop to do the handshake in a single
|
|
|
43fe83 |
+ * thread, so we need these non-blocking to avoid deadlock
|
|
|
43fe83 |
+ * of ourselves
|
|
|
43fe83 |
+ */
|
|
|
43fe83 |
+ ignore_value(virSetNonBlock(channel[0]));
|
|
|
43fe83 |
+ ignore_value(virSetNonBlock(channel[1]));
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+ /* Generate all the certs we need for this test */
|
|
|
43fe83 |
+ testTLSGenerateCert(&data->careq);
|
|
|
43fe83 |
+ data->serverreq.cacrt = data->careq.crt;
|
|
|
43fe83 |
+ testTLSGenerateCert(&data->serverreq);
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+ if (data->othercareq.filename) {
|
|
|
43fe83 |
+ testTLSGenerateCert(&data->othercareq);
|
|
|
43fe83 |
+ data->clientreq.cacrt = data->othercareq.crt;
|
|
|
43fe83 |
+ } else {
|
|
|
43fe83 |
+ data->clientreq.cacrt = data->careq.crt;
|
|
|
43fe83 |
+ }
|
|
|
43fe83 |
+ testTLSGenerateCert(&data->clientreq);
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+ /* We skip initial sanity checks here because we
|
|
|
43fe83 |
+ * want to make sure that problems are being
|
|
|
43fe83 |
+ * detected at the TLS session validation stage
|
|
|
43fe83 |
+ */
|
|
|
43fe83 |
+ serverCtxt = virNetTLSContextNewServer(data->careq.filename,
|
|
|
43fe83 |
+ NULL,
|
|
|
43fe83 |
+ data->serverreq.filename,
|
|
|
43fe83 |
+ keyfile,
|
|
|
43fe83 |
+ data->wildcards,
|
|
|
43fe83 |
+ false,
|
|
|
43fe83 |
+ true);
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+ clientCtxt = virNetTLSContextNewClient(data->othercareq.filename ?
|
|
|
43fe83 |
+ data->othercareq.filename :
|
|
|
43fe83 |
+ data->careq.filename,
|
|
|
43fe83 |
+ NULL,
|
|
|
43fe83 |
+ data->clientreq.filename,
|
|
|
43fe83 |
+ keyfile,
|
|
|
43fe83 |
+ false,
|
|
|
43fe83 |
+ true);
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+ if (!serverCtxt) {
|
|
|
43fe83 |
+ VIR_WARN("Unexpected failure loading %s against %s",
|
|
|
43fe83 |
+ data->careq.filename, data->serverreq.filename);
|
|
|
43fe83 |
+ goto cleanup;
|
|
|
43fe83 |
+ }
|
|
|
43fe83 |
+ if (!clientCtxt) {
|
|
|
43fe83 |
+ VIR_WARN("Unexpected failure loading %s against %s",
|
|
|
43fe83 |
+ data->othercareq.filename ? data->othercareq.filename :
|
|
|
43fe83 |
+ data->careq.filename, data->clientreq.filename);
|
|
|
43fe83 |
+ goto cleanup;
|
|
|
43fe83 |
+ }
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+ /* Now the real part of the test, setup the sessions */
|
|
|
43fe83 |
+ serverSess = virNetTLSSessionNew(serverCtxt, NULL);
|
|
|
43fe83 |
+ clientSess = virNetTLSSessionNew(clientCtxt, data->hostname);
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+ if (!serverSess) {
|
|
|
43fe83 |
+ VIR_WARN("Unexpected failure using %s against %s",
|
|
|
43fe83 |
+ data->careq.filename, data->serverreq.filename);
|
|
|
43fe83 |
+ goto cleanup;
|
|
|
43fe83 |
+ }
|
|
|
43fe83 |
+ if (!clientSess) {
|
|
|
43fe83 |
+ VIR_WARN("Unexpected failure using %s against %s",
|
|
|
43fe83 |
+ data->othercareq.filename ? data->othercareq.filename :
|
|
|
43fe83 |
+ data->careq.filename, data->clientreq.filename);
|
|
|
43fe83 |
+ goto cleanup;
|
|
|
43fe83 |
+ }
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+ /* For handshake to work, we need to set the I/O callbacks
|
|
|
43fe83 |
+ * to read/write over the socketpair
|
|
|
43fe83 |
+ */
|
|
|
43fe83 |
+ virNetTLSSessionSetIOCallbacks(serverSess, testWrite, testRead, &channel[0]);
|
|
|
43fe83 |
+ virNetTLSSessionSetIOCallbacks(clientSess, testWrite, testRead, &channel[1]);
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+ /*
|
|
|
43fe83 |
+ * Finally we loop around & around doing handshake on each
|
|
|
43fe83 |
+ * session until we get an error, or the handshake completes.
|
|
|
43fe83 |
+ * This relies on the socketpair being nonblocking to avoid
|
|
|
43fe83 |
+ * deadlocking ourselves upon handshake
|
|
|
43fe83 |
+ */
|
|
|
43fe83 |
+ do {
|
|
|
43fe83 |
+ int rv;
|
|
|
43fe83 |
+ if (!serverShake) {
|
|
|
43fe83 |
+ rv = virNetTLSSessionHandshake(serverSess);
|
|
|
43fe83 |
+ if (rv < 0)
|
|
|
43fe83 |
+ goto cleanup;
|
|
|
43fe83 |
+ if (rv == VIR_NET_TLS_HANDSHAKE_COMPLETE)
|
|
|
43fe83 |
+ serverShake = true;
|
|
|
43fe83 |
+ }
|
|
|
43fe83 |
+ if (!clientShake) {
|
|
|
43fe83 |
+ rv = virNetTLSSessionHandshake(clientSess);
|
|
|
43fe83 |
+ if (rv < 0)
|
|
|
43fe83 |
+ goto cleanup;
|
|
|
43fe83 |
+ if (rv == VIR_NET_TLS_HANDSHAKE_COMPLETE)
|
|
|
43fe83 |
+ clientShake = true;
|
|
|
43fe83 |
+ }
|
|
|
43fe83 |
+ } while (!clientShake && !serverShake);
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+ /* Finally make sure the server validation does what
|
|
|
43fe83 |
+ * we were expecting
|
|
|
43fe83 |
+ */
|
|
|
43fe83 |
+ if (virNetTLSContextCheckCertificate(serverCtxt,
|
|
|
43fe83 |
+ serverSess) < 0) {
|
|
|
43fe83 |
+ if (!data->expectServerFail) {
|
|
|
43fe83 |
+ VIR_WARN("Unexpected server cert check fail");
|
|
|
43fe83 |
+ goto cleanup;
|
|
|
43fe83 |
+ } else {
|
|
|
43fe83 |
+ VIR_DEBUG("Got expected server cert fail");
|
|
|
43fe83 |
+ }
|
|
|
43fe83 |
+ } else {
|
|
|
43fe83 |
+ if (data->expectServerFail) {
|
|
|
43fe83 |
+ VIR_WARN("Expected server cert check fail");
|
|
|
43fe83 |
+ goto cleanup;
|
|
|
43fe83 |
+ } else {
|
|
|
43fe83 |
+ VIR_DEBUG("Not unexpected server cert fail");
|
|
|
43fe83 |
+ }
|
|
|
43fe83 |
+ }
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+ /*
|
|
|
43fe83 |
+ * And the same for the client validation check
|
|
|
43fe83 |
+ */
|
|
|
43fe83 |
+ if (virNetTLSContextCheckCertificate(clientCtxt,
|
|
|
43fe83 |
+ clientSess) < 0) {
|
|
|
43fe83 |
+ if (!data->expectClientFail) {
|
|
|
43fe83 |
+ VIR_WARN("Unexpected client cert check fail");
|
|
|
43fe83 |
+ goto cleanup;
|
|
|
43fe83 |
+ } else {
|
|
|
43fe83 |
+ VIR_DEBUG("Got expected client cert fail");
|
|
|
43fe83 |
+ }
|
|
|
43fe83 |
+ } else {
|
|
|
43fe83 |
+ if (data->expectClientFail) {
|
|
|
43fe83 |
+ VIR_WARN("Expected client cert check fail");
|
|
|
43fe83 |
+ goto cleanup;
|
|
|
43fe83 |
+ } else {
|
|
|
43fe83 |
+ VIR_DEBUG("Not unexpected client cert fail");
|
|
|
43fe83 |
+ }
|
|
|
43fe83 |
+ }
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+ ret = 0;
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+cleanup:
|
|
|
43fe83 |
+ virObjectUnref(serverCtxt);
|
|
|
43fe83 |
+ virObjectUnref(clientCtxt);
|
|
|
43fe83 |
+ virObjectUnref(serverSess);
|
|
|
43fe83 |
+ virObjectUnref(clientSess);
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+ testTLSDiscardCert(&data->careq);
|
|
|
43fe83 |
+ if (data->othercareq.filename)
|
|
|
43fe83 |
+ testTLSDiscardCert(&data->othercareq);
|
|
|
43fe83 |
+ testTLSDiscardCert(&data->clientreq);
|
|
|
43fe83 |
+ testTLSDiscardCert(&data->serverreq);
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+ VIR_FORCE_CLOSE(channel[0]);
|
|
|
43fe83 |
+ VIR_FORCE_CLOSE(channel[1]);
|
|
|
43fe83 |
+ return ret;
|
|
|
43fe83 |
+}
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+static int
|
|
|
43fe83 |
+mymain(void)
|
|
|
43fe83 |
+{
|
|
|
43fe83 |
+ int ret = 0;
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+ testTLSInit();
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+# define DO_SESS_TEST(_caReq, _serverReq, _clientReq, _expectServerFail,\
|
|
|
43fe83 |
+ _expectClientFail, _hostname, _wildcards) \
|
|
|
43fe83 |
+ do { \
|
|
|
43fe83 |
+ static struct testTLSSessionData data; \
|
|
|
43fe83 |
+ static struct testTLSCertReq other; \
|
|
|
43fe83 |
+ data.careq = _caReq; \
|
|
|
43fe83 |
+ data.othercareq = other; \
|
|
|
43fe83 |
+ data.serverreq = _serverReq; \
|
|
|
43fe83 |
+ data.clientreq = _clientReq; \
|
|
|
43fe83 |
+ data.expectServerFail = _expectServerFail; \
|
|
|
43fe83 |
+ data.expectClientFail = _expectClientFail; \
|
|
|
43fe83 |
+ data.hostname = _hostname; \
|
|
|
43fe83 |
+ data.wildcards = _wildcards; \
|
|
|
43fe83 |
+ if (virtTestRun("TLS Session", 1, testTLSSessionInit, &data) < 0) \
|
|
|
43fe83 |
+ ret = -1; \
|
|
|
43fe83 |
+ } while (0)
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+# define DO_SESS_TEST_EXT(_caReq, _othercaReq, _serverReq, _clientReq, \
|
|
|
43fe83 |
+ _expectServerFail, _expectClientFail, \
|
|
|
43fe83 |
+ _hostname, _wildcards) \
|
|
|
43fe83 |
+ do { \
|
|
|
43fe83 |
+ static struct testTLSSessionData data; \
|
|
|
43fe83 |
+ data.careq = _caReq; \
|
|
|
43fe83 |
+ data.othercareq = _othercaReq; \
|
|
|
43fe83 |
+ data.serverreq = _serverReq; \
|
|
|
43fe83 |
+ data.clientreq = _clientReq; \
|
|
|
43fe83 |
+ data.expectServerFail = _expectServerFail; \
|
|
|
43fe83 |
+ data.expectClientFail = _expectClientFail; \
|
|
|
43fe83 |
+ data.hostname = _hostname; \
|
|
|
43fe83 |
+ data.wildcards = _wildcards; \
|
|
|
43fe83 |
+ if (virtTestRun("TLS Session", 1, testTLSSessionInit, &data) < 0) \
|
|
|
43fe83 |
+ ret = -1; \
|
|
|
43fe83 |
+ } while (0)
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+ /* A perfect CA, perfect client & perfect server */
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+ /* Basic:CA:critical */
|
|
|
43fe83 |
+ static struct testTLSCertReq cacertreq = {
|
|
|
43fe83 |
+ NULL, NULL, "cacert.pem", "UK",
|
|
|
43fe83 |
+ "libvirt CA", NULL, NULL, NULL, NULL,
|
|
|
43fe83 |
+ true, true, true,
|
|
|
43fe83 |
+ true, true, GNUTLS_KEY_KEY_CERT_SIGN,
|
|
|
43fe83 |
+ false, false, NULL, NULL,
|
|
|
43fe83 |
+ 0, 0,
|
|
|
43fe83 |
+ };
|
|
|
43fe83 |
+ static struct testTLSCertReq cacert1req = {
|
|
|
43fe83 |
+ NULL, NULL, "cacert1.pem", "UK",
|
|
|
43fe83 |
+ "libvirt CA 1", NULL, NULL, NULL, NULL,
|
|
|
43fe83 |
+ true, true, true,
|
|
|
43fe83 |
+ false, false, 0,
|
|
|
43fe83 |
+ false, false, NULL, NULL,
|
|
|
43fe83 |
+ 0, 0,
|
|
|
43fe83 |
+ };
|
|
|
43fe83 |
+ static struct testTLSCertReq servercertreq = {
|
|
|
43fe83 |
+ NULL, NULL, "servercert.pem", "UK",
|
|
|
43fe83 |
+ "libvirt.org", NULL, NULL, NULL, NULL,
|
|
|
43fe83 |
+ true, true, false,
|
|
|
43fe83 |
+ true, true, GNUTLS_KEY_DIGITAL_SIGNATURE | GNUTLS_KEY_KEY_ENCIPHERMENT,
|
|
|
43fe83 |
+ true, true, GNUTLS_KP_TLS_WWW_SERVER, NULL,
|
|
|
43fe83 |
+ 0, 0,
|
|
|
43fe83 |
+ };
|
|
|
43fe83 |
+ static struct testTLSCertReq clientcertreq = {
|
|
|
43fe83 |
+ NULL, NULL, "clientcert.pem", "UK",
|
|
|
43fe83 |
+ "libvirt", NULL, NULL, NULL, NULL,
|
|
|
43fe83 |
+ true, true, false,
|
|
|
43fe83 |
+ true, true, GNUTLS_KEY_DIGITAL_SIGNATURE | GNUTLS_KEY_KEY_ENCIPHERMENT,
|
|
|
43fe83 |
+ true, true, GNUTLS_KP_TLS_WWW_CLIENT, NULL,
|
|
|
43fe83 |
+ 0, 0,
|
|
|
43fe83 |
+ };
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+ DO_SESS_TEST(cacertreq, servercertreq, clientcertreq, false, false, "libvirt.org", NULL);
|
|
|
43fe83 |
+ DO_SESS_TEST_EXT(cacertreq, cacert1req, servercertreq, clientcertreq, true, true, "libvirt.org", NULL);
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+ /* When an altname is set, the CN is ignored, so it must be duplicated
|
|
|
43fe83 |
+ * as an altname for it to match */
|
|
|
43fe83 |
+ static struct testTLSCertReq servercertalt1req = {
|
|
|
43fe83 |
+ NULL, NULL, "servercert.pem", "UK",
|
|
|
43fe83 |
+ "libvirt.org", "www.libvirt.org", "libvirt.org", "192.168.122.1", "fec0::dead:beaf",
|
|
|
43fe83 |
+ true, true, false,
|
|
|
43fe83 |
+ true, true, GNUTLS_KEY_DIGITAL_SIGNATURE | GNUTLS_KEY_KEY_ENCIPHERMENT,
|
|
|
43fe83 |
+ true, true, GNUTLS_KP_TLS_WWW_SERVER, NULL,
|
|
|
43fe83 |
+ 0, 0,
|
|
|
43fe83 |
+ };
|
|
|
43fe83 |
+ /* This intentionally doesn't replicate */
|
|
|
43fe83 |
+ static struct testTLSCertReq servercertalt2req = {
|
|
|
43fe83 |
+ NULL, NULL, "servercert.pem", "UK",
|
|
|
43fe83 |
+ "libvirt.org", "www.libvirt.org", "wiki.libvirt.org", "192.168.122.1", "fec0::dead:beaf",
|
|
|
43fe83 |
+ true, true, false,
|
|
|
43fe83 |
+ true, true, GNUTLS_KEY_DIGITAL_SIGNATURE | GNUTLS_KEY_KEY_ENCIPHERMENT,
|
|
|
43fe83 |
+ true, true, GNUTLS_KP_TLS_WWW_SERVER, NULL,
|
|
|
43fe83 |
+ 0, 0,
|
|
|
43fe83 |
+ };
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+ DO_SESS_TEST(cacertreq, servercertalt1req, clientcertreq, false, false, "libvirt.org", NULL);
|
|
|
43fe83 |
+ DO_SESS_TEST(cacertreq, servercertalt1req, clientcertreq, false, false, "www.libvirt.org", NULL);
|
|
|
43fe83 |
+ DO_SESS_TEST(cacertreq, servercertalt1req, clientcertreq, false, true, "wiki.libvirt.org", NULL);
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+ DO_SESS_TEST(cacertreq, servercertalt2req, clientcertreq, false, true, "libvirt.org", NULL);
|
|
|
43fe83 |
+ DO_SESS_TEST(cacertreq, servercertalt2req, clientcertreq, false, false, "www.libvirt.org", NULL);
|
|
|
43fe83 |
+ DO_SESS_TEST(cacertreq, servercertalt2req, clientcertreq, false, false, "wiki.libvirt.org", NULL);
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+ const char *const wildcards1[] = {
|
|
|
43fe83 |
+ "C=UK,CN=dogfood",
|
|
|
43fe83 |
+ NULL,
|
|
|
43fe83 |
+ };
|
|
|
43fe83 |
+ const char *const wildcards2[] = {
|
|
|
43fe83 |
+ "C=UK,CN=libvirt",
|
|
|
43fe83 |
+ NULL,
|
|
|
43fe83 |
+ };
|
|
|
43fe83 |
+ const char *const wildcards3[] = {
|
|
|
43fe83 |
+ "C=UK,CN=dogfood",
|
|
|
43fe83 |
+ "C=UK,CN=libvirt",
|
|
|
43fe83 |
+ NULL,
|
|
|
43fe83 |
+ };
|
|
|
43fe83 |
+ const char *const wildcards4[] = {
|
|
|
43fe83 |
+ "C=UK,CN=libvirtstuff",
|
|
|
43fe83 |
+ NULL,
|
|
|
43fe83 |
+ };
|
|
|
43fe83 |
+ const char *const wildcards5[] = {
|
|
|
43fe83 |
+ "C=UK,CN=libvirt*",
|
|
|
43fe83 |
+ NULL,
|
|
|
43fe83 |
+ };
|
|
|
43fe83 |
+ const char *const wildcards6[] = {
|
|
|
43fe83 |
+ "C=UK,CN=*virt*",
|
|
|
43fe83 |
+ NULL,
|
|
|
43fe83 |
+ };
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+ DO_SESS_TEST(cacertreq, servercertreq, clientcertreq, true, false, "libvirt.org", wildcards1);
|
|
|
43fe83 |
+ DO_SESS_TEST(cacertreq, servercertreq, clientcertreq, false, false, "libvirt.org", wildcards2);
|
|
|
43fe83 |
+ DO_SESS_TEST(cacertreq, servercertreq, clientcertreq, false, false, "libvirt.org", wildcards3);
|
|
|
43fe83 |
+ DO_SESS_TEST(cacertreq, servercertreq, clientcertreq, true, false, "libvirt.org", wildcards4);
|
|
|
43fe83 |
+ DO_SESS_TEST(cacertreq, servercertreq, clientcertreq, false, false, "libvirt.org", wildcards5);
|
|
|
43fe83 |
+ DO_SESS_TEST(cacertreq, servercertreq, clientcertreq, false, false, "libvirt.org", wildcards6);
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+ testTLSCleanup();
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+ return ret==0 ? EXIT_SUCCESS : EXIT_FAILURE;
|
|
|
43fe83 |
+}
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+VIRT_TEST_MAIN(mymain)
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+#else
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+int
|
|
|
43fe83 |
+main(void)
|
|
|
43fe83 |
+{
|
|
|
43fe83 |
+ return EXIT_AM_SKIP;
|
|
|
43fe83 |
+}
|
|
|
43fe83 |
+
|
|
|
43fe83 |
+#endif
|
|
|
43fe83 |
--
|
|
|
43fe83 |
1.8.3.2
|
|
|
43fe83 |
|