virnettlscontexttest.c 43.4 KB
Newer Older
1
/*
2
 * Copyright (C) 2011-2012 Red Hat, Inc.
3 4 5 6 7 8 9 10 11 12 13 14
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
O
Osier Yang 已提交
15 16
 * License along with this library;  If not, see
 * <http://www.gnu.org/licenses/>.
17 18 19 20 21 22 23 24 25
 *
 * Author: Daniel P. Berrange <berrange@redhat.com>
 */

#include <config.h>

#include <stdlib.h>
#include <fcntl.h>
#include <sys/socket.h>
M
Matthias Bolte 已提交
26 27
#include <gnutls/gnutls.h>
#include <gnutls/x509.h>
28 29 30 31 32 33 34 35

#include "testutils.h"
#include "util.h"
#include "virterror_internal.h"
#include "memory.h"
#include "logging.h"
#include "virfile.h"
#include "command.h"
36
#include "virsocketaddr.h"
M
Matthias Bolte 已提交
37
#include "gnutls_1_0_compat.h"
38

39
#if !defined WIN32 && HAVE_LIBTASN1_H && LIBGNUTLS_VERSION_NUMBER >= 0x020600
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
# include <libtasn1.h>

# include "rpc/virnettlscontext.h"

# define VIR_FROM_THIS VIR_FROM_RPC

const char *keyfile = abs_builddir "/virnettlscontexttest-key.pem";

/*
 * These store some static data that is needed when
 * encoding extensions in the x509 certs
 */
ASN1_TYPE pkix_asn1;
extern const ASN1_ARRAY_TYPE pkix_asn1_tab[];

/*
 * To avoid consuming random entropy to generate keys,
 * here's one we prepared earlier :-)
 */
gnutls_x509_privkey_t privkey;
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
# define PRIVATE_KEY                                              \
    "-----BEGIN PRIVATE KEY-----\n"                               \
    "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBALVcr\n"     \
    "BL40Tm6yq88FBhJNw1aaoCjmtg0l4dWQZ/e9Fimx4ARxFpT+ji4FE\n"     \
    "Cgl9s/SGqC+1nvlkm9ViSo0j7MKDbnDB+VRHDvMAzQhA2X7e8M0n9\n"     \
    "rPolUY2lIVC83q0BBaOBkCj2RSmT2xTEbbC2xLukSrg2WP/ihVOxc\n"     \
    "kXRuyFtzAgMBAAECgYB7slBexDwXrtItAMIH6m/U+LUpNe0Xx48OL\n"     \
    "IOn4a4whNgO/o84uIwygUK27ZGFZT0kAGAk8CdF9hA6ArcbQ62s1H\n"     \
    "myxrUbF9/mrLsQw1NEqpuUk9Ay2Tx5U/wPx35S3W/X2AvR/ZpTnCn\n"     \
    "2q/7ym9fyiSoj86drD7BTvmKXlOnOwQJBAPOFMp4mMa9NGpGuEssO\n"     \
    "m3Uwbp6lhcP0cA9MK+iOmeANpoKWfBdk5O34VbmeXnGYWEkrnX+9J\n"     \
    "bM4wVhnnBWtgBMCQQC+qAEmvwcfhauERKYznMVUVksyeuhxhCe7EK\n"     \
    "mPh+U2+g0WwdKvGDgO0PPt1gq0ILEjspMDeMHVdTwkaVBo/uMhAkA\n"     \
    "Z5SsZyCP2aTOPFDypXRdI4eqRcjaEPOUBq27r3uYb/jeboVb2weLa\n"     \
    "L1MmVuHiIHoa5clswPdWVI2y0em2IGoDAkBPSp/v9VKJEZabk9Frd\n"     \
    "a+7u4fanrM9QrEjY3KhduslSilXZZSxrWjjAJPyPiqFb3M8XXA26W\n"     \
    "nz1KYGnqYKhLcBAkB7dt57n9xfrhDpuyVEv+Uv1D3VVAhZlsaZ5Pp\n"     \
    "dcrhrkJn2sa/+O8OKvdrPSeeu/N5WwYhJf61+CPoenMp7IFci\n"         \
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
    "-----END PRIVATE KEY-----\n"


/*
 * This contains parameter about how to generate
 * certificates.
 */
struct testTLSCertReq {
    gnutls_x509_crt_t crt;
    gnutls_x509_crt_t cacrt; /* If not set, then the cert will be self-signed */

    const char *filename;

    /* Identifying information */
    const char *country;
    const char *cn;
    const char *altname1;
    const char *altname2;
    const char *ipaddr1;
    const char *ipaddr2;

    /* Basic constraints */
    bool basicConstraintsEnable;
    bool basicConstraintsCritical;
    bool basicConstraintsIsCA;

    /* Key usage */
    bool keyUsageEnable;
    bool keyUsageCritical;
    int keyUsageValue;

    /* Key purpose (aka Extended key usage) */
    bool keyPurposeEnable;
    bool keyPurposeCritical;
    const char *keyPurposeOID1;
    const char *keyPurposeOID2;

E
Eric Blake 已提交
115 116 117 118
    /* zero for current time, or non-zero for hours from now */
    int start_offset;
    /* zero for 24 hours from now, or non-zero for hours from now */
    int expire_offset;
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
};


/*
 * Turns an ASN1 object into a DER encoded byte array
 */
static void testTLSDerEncode(ASN1_TYPE src,
                             const char *src_name,
                             gnutls_datum_t * res)
{
  int size;
  char *data = NULL;

  size = 0;
  asn1_der_coding(src, src_name, NULL, &size, NULL);

  if (VIR_ALLOC_N(data, size) < 0)
      abort();

  asn1_der_coding(src, src_name, data, &size, NULL);

  res->data = (unsigned char *)data;
  res->size = size;
}


/*
 * This is a fairly lame x509 certificate generator.
 *
 * Do not copy/use this code for generating real certificates
 * since it leaves out many things that you would want in
 * certificates for real world usage.
 *
 * This is good enough only for doing tests of the libvirt
 * TLS certificate code
 */
static void
testTLSGenerateCert(struct testTLSCertReq *req)
{
    gnutls_x509_crt_t crt;
    int err;
    static char buffer[1024*1024];
    size_t size = sizeof(buffer);
    char serial[5] = { 1, 2, 3, 4, 0 };
    gnutls_datum_t der = { (unsigned char *)buffer, size };
E
Eric Blake 已提交
164 165 166
    time_t start = time(NULL) + (60*60*req->start_offset);
    time_t expire = time(NULL) + (60*60*(req->expire_offset
                                         ? req->expire_offset : 24));
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233

    /*
     * Prepare our new certificate object
     */
    if ((err = gnutls_x509_crt_init(&crt)) < 0) {
        VIR_WARN("Failed to initialize certificate %s", gnutls_strerror(err));
        abort();
    }
    if ((err = gnutls_x509_crt_set_key(crt, privkey)) < 0) {
        VIR_WARN("Failed to set certificate key %s", gnutls_strerror(err));
        abort();
    }

    /*
     * A v3 certificate is required in order to be able
     * set any of the basic constraints, key purpose and
     * key usage data
     */
    gnutls_x509_crt_set_version(crt, 3);

    if (req->country) {
        if ((err = gnutls_x509_crt_set_dn_by_oid(crt, GNUTLS_OID_X520_COUNTRY_NAME, 0,
                                                 req->country, strlen(req->country))) < 0) {
            VIR_WARN("Failed to set certificate country name %s", gnutls_strerror(err));
            abort();
        }
    }
    if (req->cn) {
        if ((err = gnutls_x509_crt_set_dn_by_oid(crt, GNUTLS_OID_X520_COMMON_NAME, 0,
                                                 req->cn, strlen(req->cn))) < 0) {
            VIR_WARN("Failed to set certificate common name %s", gnutls_strerror(err));
            abort();
        }
    }

    /*
     * Setup the subject altnames, which are used
     * for hostname checks in live sessions
     */
    if (req->altname1) {
        if ((err = gnutls_x509_crt_set_subject_alt_name(crt, GNUTLS_SAN_DNSNAME,
                                                        req->altname1,
                                                        strlen(req->altname1),
                                                        GNUTLS_FSAN_APPEND))) {
            VIR_WARN("Failed to set certificate alt name %s", gnutls_strerror(err));
            abort();
        }
    }
    if (req->altname2) {
        if ((err = gnutls_x509_crt_set_subject_alt_name(crt, GNUTLS_SAN_DNSNAME,
                                                        req->altname2,
                                                        strlen(req->altname2),
                                                        GNUTLS_FSAN_APPEND))) {
            VIR_WARN("Failed to set certificate %s alt name", gnutls_strerror(err));
            abort();
        }
    }

    /*
     * IP address need to be put into the cert in their
     * raw byte form, not strings, hence this is a little
     * more complicated
     */
    if (req->ipaddr1) {
        virSocketAddr addr;
        char *data;
        int len;
234
        if (virSocketAddrParse(&addr, req->ipaddr1, 0) < 0) {
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
            VIR_WARN("Cannot parse %s", req->ipaddr1);
            abort();
        }

        if (addr.data.sa.sa_family == AF_INET) {
            data = (char*)&addr.data.inet4.sin_addr;
            len = 4;
        } else {
            data = (char*)&addr.data.inet6.sin6_addr;
            len = 16;
        }

        if ((err = gnutls_x509_crt_set_subject_alt_name(crt, GNUTLS_SAN_IPADDRESS,
                                                        data, len, GNUTLS_FSAN_APPEND))) {
            VIR_WARN("Failed to set certificate alt name %s", gnutls_strerror(err));
            abort();
        }
    }
    if (req->ipaddr2) {
        virSocketAddr addr;
        char *data;
        int len;
257
        if (virSocketAddrParse(&addr, req->ipaddr2, 0) < 0) {
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
            VIR_WARN("Cannot parse %s", req->ipaddr2);
            abort();
        }

        if (addr.data.sa.sa_family == AF_INET) {
            data = (char*)&addr.data.inet4.sin_addr;
            len = 4;
        } else {
            data = (char*)&addr.data.inet6.sin6_addr;
            len = 16;
        }

        if ((err = gnutls_x509_crt_set_subject_alt_name(crt, GNUTLS_SAN_IPADDRESS,
                                                        data, len, GNUTLS_FSAN_APPEND))) {
            VIR_WARN("Failed to set certificate alt name %s", gnutls_strerror(err));
            abort();
        }
    }


    /*
     * Basic constraints are used to decide if the cert
     * is for a CA or not. We can't use the convenient
     * gnutls API for setting this, since it hardcodes
     * the 'critical' field which we want control over
     */
    if (req->basicConstraintsEnable) {
        ASN1_TYPE ext = ASN1_TYPE_EMPTY;

        asn1_create_element(pkix_asn1, "PKIX1.BasicConstraints", &ext);
        asn1_write_value(ext, "cA", req->basicConstraintsIsCA ? "TRUE" : "FALSE", 1);
        asn1_write_value(ext, "pathLenConstraint", NULL, 0);
        testTLSDerEncode(ext, "", &der);
        if ((err = gnutls_x509_crt_set_extension_by_oid(crt,
                                                        "2.5.29.19",
                                                        der.data,
                                                        der.size,
                                                        req->basicConstraintsCritical)) < 0) {
            VIR_WARN("Failed to set certificate basic constraints %s", gnutls_strerror(err));
            abort();
        }
        asn1_delete_structure(&ext);
        VIR_FREE(der.data);
    }

    /*
     * Next up the key usage extension. Again we can't
     * use the gnutls API since it hardcodes the extension
     * to be 'critical'
     */
    if (req->keyUsageEnable) {
        ASN1_TYPE ext = ASN1_TYPE_EMPTY;
        char str[2];

        str[0] = req->keyUsageValue & 0xff;
        str[1] = (req->keyUsageValue >> 8) & 0xff;

        asn1_create_element(pkix_asn1, "PKIX1.KeyUsage", &ext);
        asn1_write_value(ext, "", str, 9);
        testTLSDerEncode(ext, "", &der);
        if ((err = gnutls_x509_crt_set_extension_by_oid(crt,
                                                        "2.5.29.15",
                                                        der.data,
                                                        der.size,
                                                        req->keyUsageCritical)) < 0) {
            VIR_WARN("Failed to set certificate key usage %s", gnutls_strerror(err));
            abort();
        }
        asn1_delete_structure(&ext);
        VIR_FREE(der.data);
    }

    /*
     * Finally the key purpose extension. This time
     * gnutls has the opposite problem, always hardcoding
     * it to be non-critical. So once again we have to
     * set this the hard way building up ASN1 data ourselves
     */
    if (req->keyPurposeEnable) {
        ASN1_TYPE ext = ASN1_TYPE_EMPTY;

        asn1_create_element(pkix_asn1, "PKIX1.ExtKeyUsageSyntax", &ext);
        if (req->keyPurposeOID1) {
            asn1_write_value(ext, "", "NEW", 1);
            asn1_write_value(ext, "?LAST", req->keyPurposeOID1, 1);
        }
        if (req->keyPurposeOID2) {
            asn1_write_value(ext, "", "NEW", 1);
            asn1_write_value(ext, "?LAST", req->keyPurposeOID2, 1);
        }
        testTLSDerEncode(ext, "", &der);
        if ((err = gnutls_x509_crt_set_extension_by_oid(crt,
                                                        "2.5.29.37",
                                                        der.data,
                                                        der.size,
                                                        req->keyPurposeCritical)) < 0) {
            VIR_WARN("Failed to set certificate key purpose %s", gnutls_strerror(err));
            abort();
        }
        asn1_delete_structure(&ext);
        VIR_FREE(der.data);
    }

    /*
     * Any old serial number will do, so lets pick 5
     */
    if ((err = gnutls_x509_crt_set_serial(crt, serial, 5)) < 0) {
        VIR_WARN("Failed to set certificate serial %s", gnutls_strerror(err));
        abort();
    }

    if ((err = gnutls_x509_crt_set_activation_time(crt, start)) < 0) {
        VIR_WARN("Failed to set certificate activation %s", gnutls_strerror(err));
        abort();
    }
    if ((err = gnutls_x509_crt_set_expiration_time(crt, expire)) < 0) {
        VIR_WARN("Failed to set certificate expiration %s", gnutls_strerror(err));
        abort();
    }


    /*
     * If no 'cart' is set then we are self signing
     * the cert. This is done for CA certs
     */
    if ((err = gnutls_x509_crt_sign(crt, req->cacrt ? req->cacrt : crt, privkey) < 0)) {
        VIR_WARN("Failed to sign certificate %s", gnutls_strerror(err));
        abort();
    }

    /*
     * Finally write the new cert out to disk
     */
    if ((err = gnutls_x509_crt_export(crt, GNUTLS_X509_FMT_PEM, buffer, &size) < 0)) {
        VIR_WARN("Failed to export certificate %s", gnutls_strerror(err));
        abort();
    }

    if (virFileWriteStr(req->filename, buffer, 0600) < 0) {
        VIR_WARN("Failed to write certificate %s %s", req->filename, gnutls_strerror(err));
        abort();
    }

    req->crt = crt;
    return;
}


/*
 * This loads the private key we defined earlier
 */
static gnutls_x509_privkey_t testTLSLoadKey(void)
{
    gnutls_x509_privkey_t key;
    const gnutls_datum_t data = { (unsigned char *)PRIVATE_KEY, strlen(PRIVATE_KEY) };
E
Eric Blake 已提交
413
    int err;
414

E
Eric Blake 已提交
415 416 417 418
    if ((err = gnutls_x509_privkey_init(&key)) < 0) {
        VIR_WARN("Failed to init key %s", gnutls_strerror(err));
        abort();
    }
419

E
Eric Blake 已提交
420 421
    if ((err = gnutls_x509_privkey_import(key, &data,
                                          GNUTLS_X509_FMT_PEM)) < 0) {
422 423 424 425 426 427 428 429 430
        if (err != GNUTLS_E_BASE64_UNEXPECTED_HEADER_ERROR) {
            VIR_WARN("Failed to import key %s", gnutls_strerror(err));
            abort();
        }

        if ((err = gnutls_x509_privkey_import_pkcs8(key, &data, GNUTLS_X509_FMT_PEM, NULL, 0)) < 0) {
            VIR_WARN("Failed to import PKCS8 key %s", gnutls_strerror(err));
            abort();
        }
E
Eric Blake 已提交
431
    }
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714

    return key;
}


struct testTLSContextData {
    bool isServer;
    struct testTLSCertReq careq;
    struct testTLSCertReq certreq;
    bool expectFail;
};


/*
 * This tests sanity checking of our own certificates
 *
 * This code is done when libvirtd starts up, or before
 * a libvirt client connects. The test is ensuring that
 * the creation of virNetTLSContextPtr fails if we
 * give bogus certs, or suceeds for good certs
 */
static int testTLSContextInit(const void *opaque)
{
    struct testTLSContextData *data = (struct testTLSContextData *)opaque;
    virNetTLSContextPtr ctxt = NULL;
    int ret = -1;

    testTLSGenerateCert(&data->careq);
    data->certreq.cacrt = data->careq.crt;
    testTLSGenerateCert(&data->certreq);

    if (data->isServer) {
        ctxt = virNetTLSContextNewServer(data->careq.filename,
                                         NULL,
                                         data->certreq.filename,
                                         keyfile,
                                         NULL,
                                         true,
                                         true);
    } else {
        ctxt = virNetTLSContextNewClient(data->careq.filename,
                                         NULL,
                                         data->certreq.filename,
                                         keyfile,
                                         true,
                                         true);
    }

    if (ctxt) {
        if (data->expectFail) {
            VIR_WARN("Expected failure %s against %s",
                     data->careq.filename, data->certreq.filename);
            goto cleanup;
        }
    } else {
        virErrorPtr err = virGetLastError();
        if (!data->expectFail) {
            VIR_WARN("Unexpected failure %s against %s",
                     data->careq.filename, data->certreq.filename);
            goto cleanup;
        }
        VIR_DEBUG("Got error %s", err ? err->message : "<unknown>");
    }

    ret = 0;

cleanup:
    virNetTLSContextFree(ctxt);
    gnutls_x509_crt_deinit(data->careq.crt);
    gnutls_x509_crt_deinit(data->certreq.crt);
    data->careq.crt = data->certreq.crt = NULL;
    /* When troubleshooting this tests, we often want to leave the certs on disk */
    if (getenv("VIRT_TEST_DEBUG_CERTS") == NULL) {
        unlink(data->careq.filename);
        unlink(data->certreq.filename);
    }
    return ret;
}



struct testTLSSessionData {
    struct testTLSCertReq careq;
    struct testTLSCertReq othercareq;
    struct testTLSCertReq serverreq;
    struct testTLSCertReq clientreq;
    bool expectServerFail;
    bool expectClientFail;
    const char *hostname;
    const char *const* wildcards;
};


static ssize_t testWrite(const char *buf, size_t len, void *opaque)
{
    int *fd = opaque;

    return write(*fd, buf, len);
}

static ssize_t testRead(char *buf, size_t len, void *opaque)
{
    int *fd = opaque;

    return read(*fd, buf, len);
}

/*
 * This tests validation checking of peer certificates
 *
 * This is replicating the checks that are done for an
 * active TLS session after handshake completes. To
 * simulate that we create our TLS contexts, skipping
 * sanity checks. When then get a socketpair, and
 * initiate a TLS session across them. Finally do
 * do actual cert validation tests
 */
static int testTLSSessionInit(const void *opaque)
{
    struct testTLSSessionData *data = (struct testTLSSessionData *)opaque;
    virNetTLSContextPtr clientCtxt = NULL;
    virNetTLSContextPtr serverCtxt = NULL;
    virNetTLSSessionPtr clientSess = NULL;
    virNetTLSSessionPtr serverSess = NULL;
    int ret = -1;
    int channel[2];
    bool clientShake = false;
    bool serverShake = false;


    /* We'll use this for our fake client-server connection */
    if (socketpair(AF_UNIX, SOCK_STREAM, 0, channel) < 0)
        abort();

    /*
     * We have an evil loop to do the handshake in a single
     * thread, so we need these non-blocking to avoid deadlock
     * of ourselves
     */
    ignore_value(virSetNonBlock(channel[0]));
    ignore_value(virSetNonBlock(channel[1]));


    /* Generate all the certs we need for this test */
    testTLSGenerateCert(&data->careq);
    data->serverreq.cacrt = data->careq.crt;
    testTLSGenerateCert(&data->serverreq);

    if (data->othercareq.filename) {
        testTLSGenerateCert(&data->othercareq);
        data->clientreq.cacrt = data->othercareq.crt;
    } else {
        data->clientreq.cacrt = data->careq.crt;
    }
    testTLSGenerateCert(&data->clientreq);


    /* We skip initial sanity checks here because we
     * want to make sure that problems are being
     * detected at the TLS session validation stage
     */
    serverCtxt = virNetTLSContextNewServer(data->careq.filename,
                                           NULL,
                                           data->serverreq.filename,
                                           keyfile,
                                           data->wildcards,
                                           false,
                                           true);

    clientCtxt = virNetTLSContextNewClient(data->othercareq.filename ?
                                           data->othercareq.filename :
                                           data->careq.filename,
                                           NULL,
                                           data->clientreq.filename,
                                           keyfile,
                                           false,
                                           true);

    if (!serverCtxt) {
        VIR_WARN("Unexpected failure loading %s against %s",
                 data->careq.filename, data->serverreq.filename);
        goto cleanup;
    }
    if (!clientCtxt) {
        VIR_WARN("Unexpected failure loading %s against %s",
                 data->othercareq.filename ? data->othercareq.filename :
                 data->careq.filename, data->clientreq.filename);
        goto cleanup;
    }


    /* Now the real part of the test, setup the sessions */
    serverSess = virNetTLSSessionNew(serverCtxt, NULL);
    clientSess = virNetTLSSessionNew(clientCtxt, data->hostname);

    if (!serverSess) {
        VIR_WARN("Unexpected failure using %s against %s",
                 data->careq.filename, data->serverreq.filename);
        goto cleanup;
    }
    if (!clientSess) {
        VIR_WARN("Unexpected failure using %s against %s",
                 data->othercareq.filename ? data->othercareq.filename :
                 data->careq.filename, data->clientreq.filename);
        goto cleanup;
    }

    /* For handshake to work, we need to set the I/O callbacks
     * to read/write over the socketpair
     */
    virNetTLSSessionSetIOCallbacks(serverSess, testWrite, testRead, &channel[0]);
    virNetTLSSessionSetIOCallbacks(clientSess, testWrite, testRead, &channel[1]);

    /*
     * Finally we loop around & around doing handshake on each
     * session until we get an error, or the handshake completes.
     * This relies on the socketpair being nonblocking to avoid
     * deadlocking ourselves upon handshake
     */
    do {
        int rv;
        if (!serverShake) {
            rv = virNetTLSSessionHandshake(serverSess);
            if (rv < 0)
                goto cleanup;
            if (rv == VIR_NET_TLS_HANDSHAKE_COMPLETE)
                serverShake = true;
        }
        if (!clientShake) {
            rv = virNetTLSSessionHandshake(clientSess);
            if (rv < 0)
                goto cleanup;
            if (rv == VIR_NET_TLS_HANDSHAKE_COMPLETE)
                serverShake = true;
        }
    } while (!clientShake && !serverShake);


    /* Finally make sure the server validation does what
     * we were expecting
     */
    if (virNetTLSContextCheckCertificate(serverCtxt,
                                         serverSess) < 0) {
        if (!data->expectServerFail) {
            VIR_WARN("Unexpected server cert check fail");
            goto cleanup;
        } else {
            VIR_DEBUG("Got expected server cert fail");
        }
    } else {
        if (data->expectServerFail) {
            VIR_WARN("Expected server cert check fail");
            goto cleanup;
        } else {
            VIR_DEBUG("Not unexpected server cert fail");
        }
    }

    /*
     * And the same for the client validation check
     */
    if (virNetTLSContextCheckCertificate(clientCtxt,
                                         clientSess) < 0) {
        if (!data->expectClientFail) {
            VIR_WARN("Unexpected client cert check fail");
            goto cleanup;
        } else {
            VIR_DEBUG("Got expected client cert fail");
        }
    } else {
        if (data->expectClientFail) {
            VIR_WARN("Expected client cert check fail");
            goto cleanup;
        } else {
            VIR_DEBUG("Not unexpected client cert fail");
        }
    }

    ret = 0;

cleanup:
    virNetTLSContextFree(serverCtxt);
    virNetTLSContextFree(clientCtxt);
715 716
    virNetTLSSessionFree(serverSess);
    virNetTLSSessionFree(clientSess);
717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751
    gnutls_x509_crt_deinit(data->careq.crt);
    if (data->othercareq.filename)
        gnutls_x509_crt_deinit(data->othercareq.crt);
    gnutls_x509_crt_deinit(data->clientreq.crt);
    gnutls_x509_crt_deinit(data->serverreq.crt);
    data->careq.crt = data->othercareq.crt = data->clientreq.crt = data->serverreq.crt = NULL;

    /* When troubleshooting this tests, we often want to leave the certs on disk */
    if (getenv("VIRT_TEST_DEBUG_CERTS") == NULL) {
        unlink(data->careq.filename);
        if (data->othercareq.filename)
            unlink(data->othercareq.filename);
        unlink(data->clientreq.filename);
        unlink(data->serverreq.filename);
    }
    VIR_FORCE_CLOSE(channel[0]);
    VIR_FORCE_CLOSE(channel[1]);
    return ret;
}


static int
mymain(void)
{
    int ret = 0;
    if (asn1_array2tree(pkix_asn1_tab, &pkix_asn1, NULL) != ASN1_SUCCESS)
        abort();

    gnutls_global_init();

    privkey = testTLSLoadKey();

    if (virFileWriteStr(keyfile, PRIVATE_KEY, 0600) < 0)
        return EXIT_FAILURE;

752
# define DO_CTX_TEST(_isServer, _caReq, _certReq, _expectFail)          \
753
    do {                                                                \
754 755 756 757 758
        static struct testTLSContextData data;                          \
        data.isServer = _isServer;                                      \
        data.careq = _caReq;                                            \
        data.certreq = _certReq;                                        \
        data.expectFail = _expectFail;                                  \
759 760 761 762
        if (virtTestRun("TLS Context", 1, testTLSContextInit, &data) < 0) \
            ret = -1;                                                   \
    } while (0)

763 764
# define DO_SESS_TEST(_caReq, _serverReq, _clientReq, _expectServerFail,\
                      _expectClientFail, _hostname, _wildcards)         \
765
    do {                                                                \
766 767 768 769 770 771 772 773 774 775
        static struct testTLSSessionData data;                          \
        static struct testTLSCertReq other;                             \
        data.careq = _caReq;                                            \
        data.othercareq = other;                                        \
        data.serverreq = _serverReq;                                    \
        data.clientreq = _clientReq;                                    \
        data.expectServerFail = _expectServerFail;                      \
        data.expectClientFail = _expectClientFail;                      \
        data.hostname = _hostname;                                      \
        data.wildcards = _wildcards;                                    \
776 777 778 779
        if (virtTestRun("TLS Session", 1, testTLSSessionInit, &data) < 0) \
            ret = -1;                                                   \
    } while (0)

780 781 782
# define DO_SESS_TEST_EXT(_caReq, _othercaReq, _serverReq, _clientReq,  \
                          _expectServerFail, _expectClientFail,         \
                          _hostname, _wildcards)                        \
783
    do {                                                                \
784 785 786 787 788 789 790 791 792
        static struct testTLSSessionData data;                          \
        data.careq = _caReq;                                            \
        data.othercareq = _othercaReq;                                  \
        data.serverreq = _serverReq;                                    \
        data.clientreq = _clientReq;                                    \
        data.expectServerFail = _expectServerFail;                      \
        data.expectClientFail = _expectClientFail;                      \
        data.hostname = _hostname;                                      \
        data.wildcards = _wildcards;                                    \
793 794 795 796 797 798 799
        if (virtTestRun("TLS Session", 1, testTLSSessionInit, &data) < 0) \
            ret = -1;                                                   \
    } while (0)

    /* A perfect CA, perfect client & perfect server */

    /* Basic:CA:critical */
E
Eric Blake 已提交
800
    static struct testTLSCertReq cacertreq = {
801 802 803 804 805 806 807
        NULL, NULL, "cacert.pem", "UK",
        "libvirt CA", NULL, NULL, NULL, NULL,
        true, true, true,
        true, true, GNUTLS_KEY_KEY_CERT_SIGN,
        false, false, NULL, NULL,
        0, 0,
    };
E
Eric Blake 已提交
808
    static struct testTLSCertReq servercertreq = {
809 810 811 812 813 814 815
        NULL, NULL, "servercert.pem", "UK",
        "libvirt.org", NULL, NULL, NULL, NULL,
        true, true, false,
        true, true, GNUTLS_KEY_DIGITAL_SIGNATURE | GNUTLS_KEY_KEY_ENCIPHERMENT,
        true, true, GNUTLS_KP_TLS_WWW_SERVER, NULL,
        0, 0,
    };
E
Eric Blake 已提交
816
    static struct testTLSCertReq clientcertreq = {
817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832
        NULL, NULL, "clientcert.pem", "UK",
        "libvirt", NULL, NULL, NULL, NULL,
        true, true, false,
        true, true, GNUTLS_KEY_DIGITAL_SIGNATURE | GNUTLS_KEY_KEY_ENCIPHERMENT,
        true, true, GNUTLS_KP_TLS_WWW_CLIENT, NULL,
        0, 0,
    };


    DO_CTX_TEST(true, cacertreq, servercertreq, false);
    DO_CTX_TEST(false, cacertreq, clientcertreq, false);


    /* Some other CAs which are good */

    /* Basic:CA:critical */
E
Eric Blake 已提交
833
    static struct testTLSCertReq cacert1req = {
834 835 836 837 838 839 840 841
        NULL, NULL, "cacert1.pem", "UK",
        "libvirt CA 1", NULL, NULL, NULL, NULL,
        true, true, true,
        false, false, 0,
        false, false, NULL, NULL,
        0, 0,
    };
    /* Basic:CA:not-critical */
E
Eric Blake 已提交
842
    static struct testTLSCertReq cacert2req = {
843 844 845 846 847 848 849 850 851 852 853 854
        NULL, NULL, "cacert2.pem", "UK",
        "libvirt CA 2", NULL, NULL, NULL, NULL,
        true, false, true,
        false, false, 0,
        false, false, NULL, NULL,
        0, 0,
    };
    /* Basic:not-CA:not-critical */
# if 0
    /* Default GNUTLS session config forbids use of CAs without
     * basic constraints, so skip this otherwise valid test
     */
E
Eric Blake 已提交
855
    static struct testTLSCertReq cacert3req = {
856 857 858 859 860 861 862 863 864
        NULL, NULL, "cacert3.pem", "UK",
        "libvirt CA 3", NULL, NULL, NULL, NULL,
        true, false, false,
        false, false, 0,
        false, false, NULL, NULL,
        0, 0,
    };
# endif
    /* Key usage:cert-sign:critical */
E
Eric Blake 已提交
865
    static struct testTLSCertReq cacert4req = {
866 867 868 869 870 871 872 873
        NULL, NULL, "cacert4.pem", "UK",
        "libvirt CA 4", NULL, NULL, NULL, NULL,
        true, true, true,
        true, true, GNUTLS_KEY_KEY_CERT_SIGN,
        false, false, NULL, NULL,
        0, 0,
    };
    /* Key usage:dig-sig:not-critical */
E
Eric Blake 已提交
874
    static struct testTLSCertReq cacert5req = {
875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893
        NULL, NULL, "cacert5.pem", "UK",
        "libvirt CA 5", NULL, NULL, NULL, NULL,
        true, true, true,
        true, false, GNUTLS_KEY_DIGITAL_SIGNATURE,
        false, false, NULL, NULL,
        0, 0,
    };

    DO_CTX_TEST(true, cacert1req, servercertreq, false);
    DO_CTX_TEST(true, cacert2req, servercertreq, false);
# if 0
    DO_CTX_TEST(true, cacert3req, servercertreq, false);
# endif
    DO_CTX_TEST(true, cacert4req, servercertreq, false);
    DO_CTX_TEST(true, cacert5req, servercertreq, false);

    /* Now some bad certs */

    /* no-basic */
E
Eric Blake 已提交
894
    static struct testTLSCertReq cacert6req = {
895 896 897 898 899 900 901 902
        NULL, NULL, "cacert6.pem", "UK",
        "libvirt CA 6", NULL, NULL, NULL, NULL,
        false, false, false,
        false, false, 0,
        false, false, NULL, NULL,
        0, 0,
    };
    /* Key usage:dig-sig:critical */
E
Eric Blake 已提交
903
    static struct testTLSCertReq cacert7req = {
904 905 906 907 908 909 910 911 912 913 914 915 916 917
        NULL, NULL, "cacert7.pem", "UK",
        "libvirt CA 7", NULL, NULL, NULL, NULL,
        true, true, true,
        true, true, GNUTLS_KEY_DIGITAL_SIGNATURE,
        false, false, NULL, NULL,
        0, 0,
    };

    DO_CTX_TEST(true, cacert6req, servercertreq, true);
    DO_CTX_TEST(true, cacert7req, servercertreq, true);


    /* Various good servers */
    /* no usage or purpose */
E
Eric Blake 已提交
918
    static struct testTLSCertReq servercert1req = {
919 920 921 922 923 924 925 926
        NULL, NULL, "servercert1.pem", "UK",
        "libvirt", NULL, NULL, NULL, NULL,
        true, true, false,
        false, false, 0,
        false, false, NULL, NULL,
        0, 0,
    };
    /* usage:cert-sign+dig-sig+encipher:critical */
E
Eric Blake 已提交
927
    static struct testTLSCertReq servercert2req = {
928 929 930 931 932 933 934 935
        NULL, NULL, "servercert2.pem", "UK",
        "libvirt", NULL, NULL, NULL, NULL,
        true, true, false,
        true, true, GNUTLS_KEY_DIGITAL_SIGNATURE | GNUTLS_KEY_KEY_ENCIPHERMENT | GNUTLS_KEY_KEY_CERT_SIGN,
        false, false, NULL, NULL,
        0, 0,
    };
    /* usage:cert-sign:not-critical */
E
Eric Blake 已提交
936
    static struct testTLSCertReq servercert3req = {
937 938 939 940 941 942 943 944
        NULL, NULL, "servercert3.pem", "UK",
        "libvirt", NULL, NULL, NULL, NULL,
        true, true, false,
        true, false, GNUTLS_KEY_KEY_CERT_SIGN,
        false, false, NULL, NULL,
        0, 0,
    };
    /* purpose:server:critical */
E
Eric Blake 已提交
945
    static struct testTLSCertReq servercert4req = {
946 947 948 949 950 951 952 953
        NULL, NULL, "servercert4.pem", "UK",
        "libvirt", NULL, NULL, NULL, NULL,
        true, true, false,
        false, false, 0,
        true, true, GNUTLS_KP_TLS_WWW_SERVER, NULL,
        0, 0,
    };
    /* purpose:server:not-critical */
E
Eric Blake 已提交
954
    static struct testTLSCertReq servercert5req = {
955 956 957 958 959 960 961 962
        NULL, NULL, "servercert5.pem", "UK",
        "libvirt", NULL, NULL, NULL, NULL,
        true, true, false,
        false, false, 0,
        true, false, GNUTLS_KP_TLS_WWW_SERVER, NULL,
        0, 0,
    };
    /* purpose:client+server:critical */
E
Eric Blake 已提交
963
    static struct testTLSCertReq servercert6req = {
964 965 966 967 968 969 970 971
        NULL, NULL, "servercert6.pem", "UK",
        "libvirt", NULL, NULL, NULL, NULL,
        true, true, false,
        false, false, 0,
        true, true, GNUTLS_KP_TLS_WWW_CLIENT, GNUTLS_KP_TLS_WWW_SERVER,
        0, 0,
    };
    /* purpose:client+server:not-critical */
E
Eric Blake 已提交
972
    static struct testTLSCertReq servercert7req = {
973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990
        NULL, NULL, "servercert7.pem", "UK",
        "libvirt", NULL, NULL, NULL, NULL,
        true, true, false,
        false, false, 0,
        true, false, GNUTLS_KP_TLS_WWW_CLIENT, GNUTLS_KP_TLS_WWW_SERVER,
        0, 0,
    };

    DO_CTX_TEST(true, cacertreq, servercert1req, false);
    DO_CTX_TEST(true, cacertreq, servercert2req, false);
    DO_CTX_TEST(true, cacertreq, servercert3req, false);
    DO_CTX_TEST(true, cacertreq, servercert4req, false);
    DO_CTX_TEST(true, cacertreq, servercert5req, false);
    DO_CTX_TEST(true, cacertreq, servercert6req, false);
    DO_CTX_TEST(true, cacertreq, servercert7req, false);
    /* Bad servers */

    /* usage:cert-sign:critical */
E
Eric Blake 已提交
991
    static struct testTLSCertReq servercert8req = {
992 993 994 995 996 997 998 999
        NULL, NULL, "servercert8.pem", "UK",
        "libvirt", NULL, NULL, NULL, NULL,
        true, true, false,
        true, true, GNUTLS_KEY_KEY_CERT_SIGN,
        false, false, NULL, NULL,
        0, 0,
    };
    /* purpose:client:critical */
E
Eric Blake 已提交
1000
    static struct testTLSCertReq servercert9req = {
1001 1002 1003 1004 1005 1006 1007 1008
        NULL, NULL, "servercert9.pem", "UK",
        "libvirt", NULL, NULL, NULL, NULL,
        true, true, false,
        false, false, 0,
        true, true, GNUTLS_KP_TLS_WWW_CLIENT, NULL,
        0, 0,
    };
    /* usage: none:critical */
E
Eric Blake 已提交
1009
    static struct testTLSCertReq servercert10req = {
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
        NULL, NULL, "servercert10.pem", "UK",
        "libvirt", NULL, NULL, NULL, NULL,
        true, true, false,
        true, true, 0,
        false, false, NULL, NULL,
        0, 0,
    };

    DO_CTX_TEST(true, cacertreq, servercert8req, true);
    DO_CTX_TEST(true, cacertreq, servercert9req, true);
    DO_CTX_TEST(true, cacertreq, servercert10req, true);



    /* Various good clients */
    /* no usage or purpose */
E
Eric Blake 已提交
1026
    static struct testTLSCertReq clientcert1req = {
1027 1028 1029 1030 1031 1032 1033 1034
        NULL, NULL, "clientcert1.pem", "UK",
        "libvirt", NULL, NULL, NULL, NULL,
        true, true, false,
        false, false, 0,
        false, false, NULL, NULL,
        0, 0,
    };
    /* usage:cert-sign+dig-sig+encipher:critical */
E
Eric Blake 已提交
1035
    static struct testTLSCertReq clientcert2req = {
1036 1037 1038 1039 1040 1041 1042 1043
        NULL, NULL, "clientcert2.pem", "UK",
        "libvirt", NULL, NULL, NULL, NULL,
        true, true, false,
        true, true, GNUTLS_KEY_DIGITAL_SIGNATURE | GNUTLS_KEY_KEY_ENCIPHERMENT | GNUTLS_KEY_KEY_CERT_SIGN,
        false, false, NULL, NULL,
        0, 0,
    };
    /* usage:cert-sign:not-critical */
E
Eric Blake 已提交
1044
    static struct testTLSCertReq clientcert3req = {
1045 1046 1047 1048 1049 1050 1051 1052
        NULL, NULL, "clientcert3.pem", "UK",
        "libvirt", NULL, NULL, NULL, NULL,
        true, true, false,
        true, false, GNUTLS_KEY_KEY_CERT_SIGN,
        false, false, NULL, NULL,
        0, 0,
    };
    /* purpose:client:critical */
E
Eric Blake 已提交
1053
    static struct testTLSCertReq clientcert4req = {
1054 1055 1056 1057 1058 1059 1060 1061
        NULL, NULL, "clientcert4.pem", "UK",
        "libvirt", NULL, NULL, NULL, NULL,
        true, true, false,
        false, false, 0,
        true, true, GNUTLS_KP_TLS_WWW_CLIENT, NULL,
        0, 0,
    };
    /* purpose:client:not-critical */
E
Eric Blake 已提交
1062
    static struct testTLSCertReq clientcert5req = {
1063 1064 1065 1066 1067 1068 1069 1070
        NULL, NULL, "clientcert5.pem", "UK",
        "libvirt", NULL, NULL, NULL, NULL,
        true, true, false,
        false, false, 0,
        true, false, GNUTLS_KP_TLS_WWW_CLIENT, NULL,
        0, 0,
    };
    /* purpose:client+client:critical */
E
Eric Blake 已提交
1071
    static struct testTLSCertReq clientcert6req = {
1072 1073 1074 1075 1076 1077 1078 1079
        NULL, NULL, "clientcert6.pem", "UK",
        "libvirt", NULL, NULL, NULL, NULL,
        true, true, false,
        false, false, 0,
        true, true, GNUTLS_KP_TLS_WWW_CLIENT, GNUTLS_KP_TLS_WWW_SERVER,
        0, 0,
    };
    /* purpose:client+client:not-critical */
E
Eric Blake 已提交
1080
    static struct testTLSCertReq clientcert7req = {
1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098
        NULL, NULL, "clientcert7.pem", "UK",
        "libvirt", NULL, NULL, NULL, NULL,
        true, true, false,
        false, false, 0,
        true, false, GNUTLS_KP_TLS_WWW_CLIENT, GNUTLS_KP_TLS_WWW_SERVER,
        0, 0,
    };

    DO_CTX_TEST(false, cacertreq, clientcert1req, false);
    DO_CTX_TEST(false, cacertreq, clientcert2req, false);
    DO_CTX_TEST(false, cacertreq, clientcert3req, false);
    DO_CTX_TEST(false, cacertreq, clientcert4req, false);
    DO_CTX_TEST(false, cacertreq, clientcert5req, false);
    DO_CTX_TEST(false, cacertreq, clientcert6req, false);
    DO_CTX_TEST(false, cacertreq, clientcert7req, false);
    /* Bad clients */

    /* usage:cert-sign:critical */
E
Eric Blake 已提交
1099
    static struct testTLSCertReq clientcert8req = {
1100 1101 1102 1103 1104 1105 1106 1107
        NULL, NULL, "clientcert8.pem", "UK",
        "libvirt", NULL, NULL, NULL, NULL,
        true, true, false,
        true, true, GNUTLS_KEY_KEY_CERT_SIGN,
        false, false, NULL, NULL,
        0, 0,
    };
    /* purpose:client:critical */
E
Eric Blake 已提交
1108
    static struct testTLSCertReq clientcert9req = {
1109 1110 1111 1112 1113 1114 1115 1116
        NULL, NULL, "clientcert9.pem", "UK",
        "libvirt", NULL, NULL, NULL, NULL,
        true, true, false,
        false, false, 0,
        true, true, GNUTLS_KP_TLS_WWW_SERVER, NULL,
        0, 0,
    };
    /* usage: none:critical */
E
Eric Blake 已提交
1117
    static struct testTLSCertReq clientcert10req = {
1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133
        NULL, NULL, "clientcert10.pem", "UK",
        "libvirt", NULL, NULL, NULL, NULL,
        true, true, false,
        true, true, 0,
        false, false, NULL, NULL,
        0, 0,
    };

    DO_CTX_TEST(false, cacertreq, clientcert8req, true);
    DO_CTX_TEST(false, cacertreq, clientcert9req, true);
    DO_CTX_TEST(false, cacertreq, clientcert10req, true);



    /* Expired stuff */

E
Eric Blake 已提交
1134
    static struct testTLSCertReq cacertexpreq = {
1135 1136 1137 1138 1139
        NULL, NULL, "cacert.pem", "UK",
        "libvirt", NULL, NULL, NULL, NULL,
        true, true, true,
        true, true, GNUTLS_KEY_KEY_CERT_SIGN,
        false, false, NULL, NULL,
1140
        0, -1,
1141
    };
E
Eric Blake 已提交
1142
    static struct testTLSCertReq servercertexpreq = {
1143 1144 1145 1146 1147
        NULL, NULL, "servercert.pem", "UK",
        "libvirt", NULL, NULL, NULL, NULL,
        true, true, false,
        true, true, GNUTLS_KEY_DIGITAL_SIGNATURE | GNUTLS_KEY_KEY_ENCIPHERMENT,
        true, true, GNUTLS_KP_TLS_WWW_SERVER, NULL,
1148
        0, -1,
1149
    };
E
Eric Blake 已提交
1150
    static struct testTLSCertReq clientcertexpreq = {
1151 1152 1153 1154 1155
        NULL, NULL, "clientcert.pem", "UK",
        "libvirt", NULL, NULL, NULL, NULL,
        true, true, false,
        true, true, GNUTLS_KEY_DIGITAL_SIGNATURE | GNUTLS_KEY_KEY_ENCIPHERMENT,
        true, true, GNUTLS_KP_TLS_WWW_CLIENT, NULL,
1156
        0, -1,
1157 1158 1159 1160 1161 1162 1163 1164 1165
    };

    DO_CTX_TEST(true, cacertexpreq, servercertreq, true);
    DO_CTX_TEST(true, cacertreq, servercertexpreq, true);
    DO_CTX_TEST(false, cacertreq, clientcertexpreq, true);


    /* Not activated stuff */

E
Eric Blake 已提交
1166
    static struct testTLSCertReq cacertnewreq = {
1167 1168 1169 1170 1171
        NULL, NULL, "cacert.pem", "UK",
        "libvirt", NULL, NULL, NULL, NULL,
        true, true, true,
        true, true, GNUTLS_KEY_KEY_CERT_SIGN,
        false, false, NULL, NULL,
E
Eric Blake 已提交
1172
        1, 2,
1173
    };
E
Eric Blake 已提交
1174
    static struct testTLSCertReq servercertnewreq = {
1175 1176 1177 1178 1179
        NULL, NULL, "servercert.pem", "UK",
        "libvirt", NULL, NULL, NULL, NULL,
        true, true, false,
        true, true, GNUTLS_KEY_DIGITAL_SIGNATURE | GNUTLS_KEY_KEY_ENCIPHERMENT,
        true, true, GNUTLS_KP_TLS_WWW_SERVER, NULL,
E
Eric Blake 已提交
1180
        1, 2,
1181
    };
E
Eric Blake 已提交
1182
    static struct testTLSCertReq clientcertnewreq = {
1183 1184 1185 1186 1187
        NULL, NULL, "clientcert.pem", "UK",
        "libvirt", NULL, NULL, NULL, NULL,
        true, true, false,
        true, true, GNUTLS_KEY_DIGITAL_SIGNATURE | GNUTLS_KEY_KEY_ENCIPHERMENT,
        true, true, GNUTLS_KP_TLS_WWW_CLIENT, NULL,
E
Eric Blake 已提交
1188
        1, 2,
1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200
    };

    DO_CTX_TEST(true, cacertnewreq, servercertreq, true);
    DO_CTX_TEST(true, cacertreq, servercertnewreq, true);
    DO_CTX_TEST(false, cacertreq, clientcertnewreq, true);


    DO_SESS_TEST(cacertreq, servercertreq, clientcertreq, false, false, "libvirt.org", NULL);
    DO_SESS_TEST_EXT(cacertreq, cacert1req, servercertreq, clientcertreq, true, true, "libvirt.org", NULL);

    /* When an altname is set, the CN is ignored, so it must be duplicated
     * as an altname for it to match */
E
Eric Blake 已提交
1201
    static struct testTLSCertReq servercertalt1req = {
1202 1203 1204 1205 1206 1207 1208 1209
        NULL, NULL, "servercert.pem", "UK",
        "libvirt.org", "www.libvirt.org", "libvirt.org", "192.168.122.1", "fec0::dead:beaf",
        true, true, false,
        true, true, GNUTLS_KEY_DIGITAL_SIGNATURE | GNUTLS_KEY_KEY_ENCIPHERMENT,
        true, true, GNUTLS_KP_TLS_WWW_SERVER, NULL,
        0, 0,
    };
    /* This intentionally doesn't replicate */
E
Eric Blake 已提交
1210
    static struct testTLSCertReq servercertalt2req = {
1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263
        NULL, NULL, "servercert.pem", "UK",
        "libvirt.org", "www.libvirt.org", "wiki.libvirt.org", "192.168.122.1", "fec0::dead:beaf",
        true, true, false,
        true, true, GNUTLS_KEY_DIGITAL_SIGNATURE | GNUTLS_KEY_KEY_ENCIPHERMENT,
        true, true, GNUTLS_KP_TLS_WWW_SERVER, NULL,
        0, 0,
    };

    DO_SESS_TEST(cacertreq, servercertalt1req, clientcertreq, false, false, "libvirt.org", NULL);
    DO_SESS_TEST(cacertreq, servercertalt1req, clientcertreq, false, false, "www.libvirt.org", NULL);
    DO_SESS_TEST(cacertreq, servercertalt1req, clientcertreq, false, true, "wiki.libvirt.org", NULL);

    DO_SESS_TEST(cacertreq, servercertalt2req, clientcertreq, false, true, "libvirt.org", NULL);
    DO_SESS_TEST(cacertreq, servercertalt2req, clientcertreq, false, false, "www.libvirt.org", NULL);
    DO_SESS_TEST(cacertreq, servercertalt2req, clientcertreq, false, false, "wiki.libvirt.org", NULL);

    const char *const wildcards1[] = {
        "C=UK,CN=dogfood",
        NULL,
    };
    const char *const wildcards2[] = {
        "C=UK,CN=libvirt",
        NULL,
    };
    const char *const wildcards3[] = {
        "C=UK,CN=dogfood",
        "C=UK,CN=libvirt",
        NULL,
    };
    const char *const wildcards4[] = {
        "C=UK,CN=libvirtstuff",
        NULL,
    };
    const char *const wildcards5[] = {
        "C=UK,CN=libvirt*",
        NULL,
    };
    const char *const wildcards6[] = {
        "C=UK,CN=*virt*",
        NULL,
    };

    DO_SESS_TEST(cacertreq, servercertreq, clientcertreq, true, false, "libvirt.org", wildcards1);
    DO_SESS_TEST(cacertreq, servercertreq, clientcertreq, false, false, "libvirt.org", wildcards2);
    DO_SESS_TEST(cacertreq, servercertreq, clientcertreq, false, false, "libvirt.org", wildcards3);
    DO_SESS_TEST(cacertreq, servercertreq, clientcertreq, true, false, "libvirt.org", wildcards4);
    DO_SESS_TEST(cacertreq, servercertreq, clientcertreq, false, false, "libvirt.org", wildcards5);
    DO_SESS_TEST(cacertreq, servercertreq, clientcertreq, false, false, "libvirt.org", wildcards6);

    unlink(keyfile);

    asn1_delete_structure(&pkix_asn1);

1264
    return ret==0 ? EXIT_SUCCESS : EXIT_FAILURE;
1265 1266
}

E
Eric Blake 已提交
1267 1268
VIRT_TEST_MAIN(mymain)

1269
#else
1270

E
Eric Blake 已提交
1271
int
1272
main(void)
1273
{
1274
    return EXIT_AM_SKIP;
1275
}
1276

1277
#endif