Home | History | Annotate | Line # | Download | only in test
      1 /*
      2  * Copyright 2015-2021 The OpenSSL Project Authors. All Rights Reserved.
      3  *
      4  * Licensed under the Apache License 2.0 (the "License").  You may not use
      5  * this file except in compliance with the License.  You can obtain a copy
      6  * in the file LICENSE in the source distribution or at
      7  * https://www.openssl.org/source/license.html
      8  */
      9 
     10 #include <stdio.h>
     11 #include <string.h>
     12 #include <ctype.h>
     13 #include <limits.h>
     14 #include <errno.h>
     15 
     16 #include <openssl/crypto.h>
     17 #include <openssl/evp.h>
     18 #include <openssl/x509.h>
     19 #include <openssl/ssl.h>
     20 #include <openssl/err.h>
     21 #include <openssl/conf.h>
     22 #ifndef OPENSSL_NO_ENGINE
     23 #include <openssl/engine.h>
     24 #endif
     25 #include "testutil.h"
     26 
     27 #include "internal/nelem.h"
     28 
     29 #define _UC(c) ((unsigned char)(c))
     30 
     31 static const char *basedomain;
     32 static const char *CAfile;
     33 static const char *tlsafile;
     34 
     35 /*
     36  * Forward declaration, of function that uses internal interfaces, from headers
     37  * included at the end of this module.
     38  */
     39 static void store_ctx_dane_init(X509_STORE_CTX *, SSL *);
     40 
     41 static int saved_errno;
     42 
     43 static void save_errno(void)
     44 {
     45     saved_errno = errno;
     46 }
     47 
     48 static int restore_errno(void)
     49 {
     50     int ret = errno;
     51     errno = saved_errno;
     52     return ret;
     53 }
     54 
     55 static int verify_chain(SSL *ssl, STACK_OF(X509) *chain)
     56 {
     57     X509_STORE_CTX *store_ctx = NULL;
     58     SSL_CTX *ssl_ctx = NULL;
     59     X509_STORE *store = NULL;
     60     int ret = 0;
     61     int store_ctx_idx = SSL_get_ex_data_X509_STORE_CTX_idx();
     62 
     63     if (!TEST_ptr(store_ctx = X509_STORE_CTX_new())
     64         || !TEST_ptr(ssl_ctx = SSL_get_SSL_CTX(ssl))
     65         || !TEST_ptr(store = SSL_CTX_get_cert_store(ssl_ctx))
     66         || !TEST_true(X509_STORE_CTX_init(store_ctx, store, NULL, chain))
     67         || !TEST_true(X509_STORE_CTX_set_ex_data(store_ctx, store_ctx_idx,
     68             ssl)))
     69         goto end;
     70 
     71     X509_STORE_CTX_set_default(store_ctx, SSL_is_server(ssl) ? "ssl_client" : "ssl_server");
     72     X509_VERIFY_PARAM_set1(X509_STORE_CTX_get0_param(store_ctx),
     73         SSL_get0_param(ssl));
     74     store_ctx_dane_init(store_ctx, ssl);
     75 
     76     if (SSL_get_verify_callback(ssl) != NULL)
     77         X509_STORE_CTX_set_verify_cb(store_ctx, SSL_get_verify_callback(ssl));
     78 
     79     /* Mask "internal failures" (-1) from our return value. */
     80     if (!TEST_int_ge(ret = X509_STORE_CTX_verify(store_ctx), 0))
     81         ret = 0;
     82 
     83     SSL_set_verify_result(ssl, X509_STORE_CTX_get_error(store_ctx));
     84 
     85 end:
     86     X509_STORE_CTX_free(store_ctx);
     87     return ret;
     88 }
     89 
     90 static STACK_OF(X509) *load_chain(BIO *fp, int nelem)
     91 {
     92     int count;
     93     char *name = 0;
     94     char *header = 0;
     95     unsigned char *data = 0;
     96     long len;
     97     char *errtype = 0; /* if error: cert or pkey? */
     98     STACK_OF(X509) *chain;
     99     typedef X509 *(*d2i_X509_t)(X509 **, const unsigned char **, long);
    100 
    101     if (!TEST_ptr(chain = sk_X509_new_null()))
    102         goto err;
    103 
    104     for (count = 0;
    105         count < nelem && errtype == 0
    106         && PEM_read_bio(fp, &name, &header, &data, &len) == 1;
    107         ++count) {
    108         if (strcmp(name, PEM_STRING_X509) == 0
    109             || strcmp(name, PEM_STRING_X509_TRUSTED) == 0
    110             || strcmp(name, PEM_STRING_X509_OLD) == 0) {
    111             d2i_X509_t d = strcmp(name, PEM_STRING_X509_TRUSTED) != 0
    112                 ? d2i_X509_AUX
    113                 : d2i_X509;
    114             X509 *cert;
    115             const unsigned char *p = data;
    116 
    117             if (!TEST_ptr(cert = d(0, &p, len))
    118                 || !TEST_long_eq(p - data, len)) {
    119                 TEST_info("Certificate parsing error");
    120                 goto err;
    121             }
    122 
    123             if (!TEST_true(sk_X509_push(chain, cert)))
    124                 goto err;
    125         } else {
    126             TEST_info("Unknown chain file object %s", name);
    127             goto err;
    128         }
    129 
    130         OPENSSL_free(name);
    131         OPENSSL_free(header);
    132         OPENSSL_free(data);
    133         name = header = NULL;
    134         data = NULL;
    135     }
    136 
    137     if (count == nelem) {
    138         ERR_clear_error();
    139         return chain;
    140     }
    141 
    142 err:
    143     OPENSSL_free(name);
    144     OPENSSL_free(header);
    145     OPENSSL_free(data);
    146     OSSL_STACK_OF_X509_free(chain);
    147     return NULL;
    148 }
    149 
    150 static char *read_to_eol(BIO *f)
    151 {
    152     static char buf[4096];
    153     int n;
    154 
    155     if (BIO_gets(f, buf, sizeof(buf)) <= 0)
    156         return NULL;
    157 
    158     n = strlen(buf);
    159     if (buf[n - 1] != '\n') {
    160         if (n + 1 == sizeof(buf))
    161             TEST_error("input too long");
    162         else
    163             TEST_error("EOF before newline");
    164         return NULL;
    165     }
    166 
    167     /* Trim trailing whitespace */
    168     while (n > 0 && isspace(_UC(buf[n - 1])))
    169         buf[--n] = '\0';
    170 
    171     return buf;
    172 }
    173 
    174 /*
    175  * Hex decoder that tolerates optional whitespace
    176  */
    177 static ossl_ssize_t hexdecode(const char *in, void *result)
    178 {
    179     unsigned char **out = (unsigned char **)result;
    180     unsigned char *ret;
    181     unsigned char *cp;
    182     uint8_t byte;
    183     int nibble = 0;
    184 
    185     if (!TEST_ptr(ret = OPENSSL_malloc(strlen(in) / 2)))
    186         return -1;
    187     cp = ret;
    188 
    189     for (byte = 0; *in; ++in) {
    190         int x;
    191 
    192         if (isspace(_UC(*in)))
    193             continue;
    194         x = OPENSSL_hexchar2int(*in);
    195         if (x < 0) {
    196             OPENSSL_free(ret);
    197             return 0;
    198         }
    199         byte |= (char)x;
    200         if ((nibble ^= 1) == 0) {
    201             *cp++ = byte;
    202             byte = 0;
    203         } else {
    204             byte <<= 4;
    205         }
    206     }
    207     if (nibble != 0) {
    208         OPENSSL_free(ret);
    209         return 0;
    210     }
    211 
    212     return cp - (*out = ret);
    213 }
    214 
    215 static ossl_ssize_t checked_uint8(const char *in, void *out)
    216 {
    217     uint8_t *result = (uint8_t *)out;
    218     const char *cp = in;
    219     char *endp;
    220     long v;
    221     int e;
    222 
    223     save_errno();
    224     v = strtol(cp, &endp, 10);
    225     e = restore_errno();
    226 
    227     if (((v == LONG_MIN || v == LONG_MAX) && e == ERANGE) || endp == cp || !isspace(_UC(*endp)) || v != (*(uint8_t *)result = (uint8_t)v)) {
    228         return -1;
    229     }
    230     for (cp = endp; isspace(_UC(*cp)); ++cp)
    231         continue;
    232     return cp - in;
    233 }
    234 
    235 struct tlsa_field {
    236     void *var;
    237     const char *name;
    238     ossl_ssize_t (*parser)(const char *, void *);
    239 };
    240 
    241 static int tlsa_import_rr(SSL *ssl, const char *rrdata)
    242 {
    243     static uint8_t usage;
    244     static uint8_t selector;
    245     static uint8_t mtype;
    246     static unsigned char *data = NULL;
    247     static struct tlsa_field tlsa_fields[] = {
    248         { &usage, "usage", checked_uint8 },
    249         { &selector, "selector", checked_uint8 },
    250         { &mtype, "mtype", checked_uint8 },
    251         { &data, "data", hexdecode },
    252         {
    253             NULL,
    254         }
    255     };
    256     int ret;
    257     struct tlsa_field *f;
    258     const char *cp = rrdata;
    259     ossl_ssize_t len = 0;
    260 
    261     for (f = tlsa_fields; f->var; ++f) {
    262         if ((len = f->parser(cp += len, f->var)) <= 0) {
    263             TEST_info("bad TLSA %s field in: %s", f->name, rrdata);
    264             return 0;
    265         }
    266     }
    267 
    268     ret = SSL_dane_tlsa_add(ssl, usage, selector, mtype, data, len);
    269     OPENSSL_free(data);
    270     if (ret == 0) {
    271         TEST_info("unusable TLSA rrdata: %s", rrdata);
    272         return 0;
    273     }
    274     if (ret < 0) {
    275         TEST_info("error loading TLSA rrdata: %s", rrdata);
    276         return 0;
    277     }
    278 
    279     return ret;
    280 }
    281 
    282 static int allws(const char *cp)
    283 {
    284     while (*cp)
    285         if (!isspace(_UC(*cp++)))
    286             return 0;
    287     return 1;
    288 }
    289 
    290 static int test_tlsafile(SSL_CTX *ctx, const char *base_name,
    291     BIO *f, const char *path)
    292 {
    293     char *line;
    294     int testno = 0;
    295     int ret = 1;
    296     SSL *ssl;
    297 
    298     while (ret > 0 && (line = read_to_eol(f)) != NULL) {
    299         STACK_OF(X509) *chain;
    300         int ntlsa;
    301         int ncert;
    302         int noncheck;
    303         int want;
    304         int want_depth;
    305         int off;
    306         int i;
    307         int ok;
    308         int err;
    309         int mdpth;
    310 
    311         if (*line == '\0' || *line == '#')
    312             continue;
    313 
    314         ++testno;
    315         if (sscanf(line, "%d %d %d %d %d%n",
    316                 &ntlsa, &ncert, &noncheck, &want, &want_depth, &off)
    317                 != 5
    318             || !allws(line + off)) {
    319             TEST_error("Malformed line for test %d", testno);
    320             return 0;
    321         }
    322 
    323         if (!TEST_ptr(ssl = SSL_new(ctx)))
    324             return 0;
    325         SSL_set_connect_state(ssl);
    326         if (SSL_dane_enable(ssl, base_name) <= 0) {
    327             SSL_free(ssl);
    328             return 0;
    329         }
    330         if (noncheck)
    331             SSL_dane_set_flags(ssl, DANE_FLAG_NO_DANE_EE_NAMECHECKS);
    332 
    333         for (i = 0; i < ntlsa; ++i) {
    334             if ((line = read_to_eol(f)) == NULL || !tlsa_import_rr(ssl, line)) {
    335                 SSL_free(ssl);
    336                 return 0;
    337             }
    338         }
    339 
    340         /* Don't report old news */
    341         ERR_clear_error();
    342         if (!TEST_ptr(chain = load_chain(f, ncert))) {
    343             SSL_free(ssl);
    344             return 0;
    345         }
    346 
    347         ok = verify_chain(ssl, chain);
    348         OSSL_STACK_OF_X509_free(chain);
    349         err = SSL_get_verify_result(ssl);
    350         /*
    351          * Peek under the hood, normally TLSA match data is hidden when
    352          * verification fails, we can obtain any suppressed data by setting the
    353          * verification result to X509_V_OK before looking.
    354          */
    355         SSL_set_verify_result(ssl, X509_V_OK);
    356         mdpth = SSL_get0_dane_authority(ssl, NULL, NULL);
    357         /* Not needed any more, but lead by example and put the error back. */
    358         SSL_set_verify_result(ssl, err);
    359         SSL_free(ssl);
    360 
    361         if (!TEST_int_eq(err, want)) {
    362             if (want == X509_V_OK)
    363                 TEST_info("Verification failure in test %d: %d=%s",
    364                     testno, err, X509_verify_cert_error_string(err));
    365             else
    366                 TEST_info("Unexpected error in test %d", testno);
    367             ret = 0;
    368             continue;
    369         }
    370         if (!TEST_false(want == 0 && ok == 0)) {
    371             TEST_info("Verification failure in test %d: ok=0", testno);
    372             ret = 0;
    373             continue;
    374         }
    375         if (!TEST_int_eq(mdpth, want_depth)) {
    376             TEST_info("In test test %d", testno);
    377             ret = 0;
    378         }
    379     }
    380     ERR_clear_error();
    381 
    382     return ret;
    383 }
    384 
    385 static int run_tlsatest(void)
    386 {
    387     SSL_CTX *ctx = NULL;
    388     BIO *f = NULL;
    389     int ret = 0;
    390 
    391     if (!TEST_ptr(f = BIO_new_file(tlsafile, "r"))
    392         || !TEST_ptr(ctx = SSL_CTX_new(TLS_client_method()))
    393         || !TEST_int_gt(SSL_CTX_dane_enable(ctx), 0)
    394         || !TEST_true(SSL_CTX_load_verify_file(ctx, CAfile))
    395         || !TEST_int_gt(SSL_CTX_dane_mtype_set(ctx, EVP_sha512(), 2, 1), 0)
    396         || !TEST_int_gt(SSL_CTX_dane_mtype_set(ctx, EVP_sha256(), 1, 2), 0)
    397         || !TEST_int_gt(test_tlsafile(ctx, basedomain, f, tlsafile), 0))
    398         goto end;
    399     ret = 1;
    400 
    401 end:
    402     BIO_free(f);
    403     SSL_CTX_free(ctx);
    404 
    405     return ret;
    406 }
    407 
    408 OPT_TEST_DECLARE_USAGE("basedomain CAfile tlsafile\n")
    409 
    410 int setup_tests(void)
    411 {
    412     if (!test_skip_common_options()) {
    413         TEST_error("Error parsing test options\n");
    414         return 0;
    415     }
    416 
    417     if (!TEST_ptr(basedomain = test_get_argument(0))
    418         || !TEST_ptr(CAfile = test_get_argument(1))
    419         || !TEST_ptr(tlsafile = test_get_argument(2)))
    420         return 0;
    421 
    422     ADD_TEST(run_tlsatest);
    423     return 1;
    424 }
    425 
    426 #include "internal/dane.h"
    427 
    428 static void store_ctx_dane_init(X509_STORE_CTX *store_ctx, SSL *ssl)
    429 {
    430     X509_STORE_CTX_set0_dane(store_ctx, SSL_get0_dane(ssl));
    431 }
    432