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