2 * Copyright 2015-2020 The OpenSSL Project Authors. All Rights Reserved.
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
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>
27 #include "internal/nelem.h"
31 #define _UC(c) ((unsigned char)(c))
33 static const char *basedomain;
34 static const char *CAfile;
35 static const char *tlsafile;
38 * Forward declaration, of function that uses internal interfaces, from headers
39 * included at the end of this module.
41 static void store_ctx_dane_init(X509_STORE_CTX *, SSL *);
43 static int saved_errno;
45 static void save_errno(void)
50 static int restore_errno(void)
57 static int verify_chain(SSL *ssl, STACK_OF(X509) *chain)
59 X509_STORE_CTX *store_ctx = NULL;
60 SSL_CTX *ssl_ctx = NULL;
61 X509_STORE *store = NULL;
64 int store_ctx_idx = SSL_get_ex_data_X509_STORE_CTX_idx();
66 if (!TEST_ptr(store_ctx = X509_STORE_CTX_new())
67 || !TEST_ptr(ssl_ctx = SSL_get_SSL_CTX(ssl))
68 || !TEST_ptr(store = SSL_CTX_get_cert_store(ssl_ctx))
69 || !TEST_ptr(cert = sk_X509_value(chain, 0))
70 || !TEST_true(X509_STORE_CTX_init(store_ctx, store, cert, chain))
71 || !TEST_true(X509_STORE_CTX_set_ex_data(store_ctx, store_ctx_idx,
75 X509_STORE_CTX_set_default(store_ctx,
76 SSL_is_server(ssl) ? "ssl_client" : "ssl_server");
77 X509_VERIFY_PARAM_set1(X509_STORE_CTX_get0_param(store_ctx),
79 store_ctx_dane_init(store_ctx, ssl);
81 if (SSL_get_verify_callback(ssl) != NULL)
82 X509_STORE_CTX_set_verify_cb(store_ctx, SSL_get_verify_callback(ssl));
84 /* Mask "internal failures" (-1) from our return value. */
85 if (!TEST_int_ge(ret = X509_verify_cert(store_ctx), 0))
88 SSL_set_verify_result(ssl, X509_STORE_CTX_get_error(store_ctx));
89 X509_STORE_CTX_cleanup(store_ctx);
92 X509_STORE_CTX_free(store_ctx);
96 static STACK_OF(X509) *load_chain(BIO *fp, int nelem)
101 unsigned char *data = 0;
103 char *errtype = 0; /* if error: cert or pkey? */
104 STACK_OF(X509) *chain;
105 typedef X509 *(*d2i_X509_t)(X509 **, const unsigned char **, long);
107 if (!TEST_ptr(chain = sk_X509_new_null()))
111 count < nelem && errtype == 0
112 && PEM_read_bio(fp, &name, &header, &data, &len) == 1;
114 if (strcmp(name, PEM_STRING_X509) == 0
115 || strcmp(name, PEM_STRING_X509_TRUSTED) == 0
116 || strcmp(name, PEM_STRING_X509_OLD) == 0) {
117 d2i_X509_t d = strcmp(name, PEM_STRING_X509_TRUSTED) != 0
118 ? d2i_X509_AUX : d2i_X509;
120 const unsigned char *p = data;
122 if (!TEST_ptr(cert = d(0, &p, len))
123 || !TEST_long_eq(p - data, len)) {
124 TEST_info("Certificate parsing error");
128 if (!TEST_true(sk_X509_push(chain, cert)))
131 TEST_info("Unknown chain file object %s", name);
136 OPENSSL_free(header);
138 name = header = NULL;
142 if (count == nelem) {
149 OPENSSL_free(header);
151 sk_X509_pop_free(chain, X509_free);
155 static char *read_to_eol(BIO *f)
157 static char buf[1024];
160 if (!BIO_gets(f, buf, sizeof(buf)))
164 if (buf[n - 1] != '\n') {
165 if (n + 1 == sizeof(buf))
166 TEST_error("input too long");
168 TEST_error("EOF before newline");
172 /* Trim trailing whitespace */
173 while (n > 0 && isspace(_UC(buf[n - 1])))
180 * Hex decoder that tolerates optional whitespace
182 static ossl_ssize_t hexdecode(const char *in, void *result)
184 unsigned char **out = (unsigned char **)result;
190 if (!TEST_ptr(ret = OPENSSL_malloc(strlen(in) / 2)))
194 for (byte = 0; *in; ++in) {
197 if (isspace(_UC(*in)))
199 x = OPENSSL_hexchar2int(*in);
205 if ((nibble ^= 1) == 0) {
217 return cp - (*out = ret);
220 static ossl_ssize_t checked_uint8(const char *in, void *out)
222 uint8_t *result = (uint8_t *)out;
229 v = strtol(cp, &endp, 10);
232 if (((v == LONG_MIN || v == LONG_MAX) && e == ERANGE) ||
233 endp == cp || !isspace(_UC(*endp)) ||
234 v != (*(uint8_t *)result = (uint8_t) v)) {
237 for (cp = endp; isspace(_UC(*cp)); ++cp)
245 ossl_ssize_t (*parser)(const char *, void *);
248 static int tlsa_import_rr(SSL *ssl, const char *rrdata)
250 static uint8_t usage;
251 static uint8_t selector;
252 static uint8_t mtype;
253 static unsigned char *data = NULL;
254 static struct tlsa_field tlsa_fields[] = {
255 { &usage, "usage", checked_uint8 },
256 { &selector, "selector", checked_uint8 },
257 { &mtype, "mtype", checked_uint8 },
258 { &data, "data", hexdecode },
262 struct tlsa_field *f;
263 const char *cp = rrdata;
264 ossl_ssize_t len = 0;
266 for (f = tlsa_fields; f->var; ++f) {
267 if ((len = f->parser(cp += len, f->var)) <= 0) {
268 TEST_info("bad TLSA %s field in: %s", f->name, rrdata);
273 ret = SSL_dane_tlsa_add(ssl, usage, selector, mtype, data, len);
276 TEST_info("unusable TLSA rrdata: %s", rrdata);
280 TEST_info("error loading TLSA rrdata: %s", rrdata);
287 static int allws(const char *cp)
290 if (!isspace(_UC(*cp++)))
295 static int test_tlsafile(SSL_CTX *ctx, const char *base_name,
296 BIO *f, const char *path)
303 while (ret > 0 && (line = read_to_eol(f)) != NULL) {
304 STACK_OF(X509) *chain;
316 if (*line == '\0' || *line == '#')
320 if (sscanf(line, "%d %d %d %d %d%n",
321 &ntlsa, &ncert, &noncheck, &want, &want_depth, &off) != 5
322 || !allws(line + off)) {
323 TEST_error("Malformed line for test %d", testno);
327 if (!TEST_ptr(ssl = SSL_new(ctx)))
329 SSL_set_connect_state(ssl);
330 if (SSL_dane_enable(ssl, base_name) <= 0) {
335 SSL_dane_set_flags(ssl, DANE_FLAG_NO_DANE_EE_NAMECHECKS);
337 for (i = 0; i < ntlsa; ++i) {
338 if ((line = read_to_eol(f)) == NULL || !tlsa_import_rr(ssl, line)) {
344 /* Don't report old news */
346 if (!TEST_ptr(chain = load_chain(f, ncert))) {
351 ok = verify_chain(ssl, chain);
352 sk_X509_pop_free(chain, X509_free);
353 err = SSL_get_verify_result(ssl);
355 * Peek under the hood, normally TLSA match data is hidden when
356 * verification fails, we can obtain any suppressed data by setting the
357 * verification result to X509_V_OK before looking.
359 SSL_set_verify_result(ssl, X509_V_OK);
360 mdpth = SSL_get0_dane_authority(ssl, NULL, NULL);
361 /* Not needed any more, but lead by example and put the error back. */
362 SSL_set_verify_result(ssl, err);
365 if (!TEST_int_eq(err, want)) {
366 if (want == X509_V_OK)
367 TEST_info("Verification failure in test %d: %d=%s",
368 testno, err, X509_verify_cert_error_string(err));
370 TEST_info("Unexpected error in test %d", testno);
374 if (!TEST_false(want == 0 && ok == 0)) {
375 TEST_info("Verification failure in test %d: ok=0", testno);
379 if (!TEST_int_eq(mdpth, want_depth)) {
380 TEST_info("In test test %d", testno);
389 static int run_tlsatest(void)
395 if (!TEST_ptr(f = BIO_new_file(tlsafile, "r"))
396 || !TEST_ptr(ctx = SSL_CTX_new(TLS_client_method()))
397 || !TEST_int_gt(SSL_CTX_dane_enable(ctx), 0)
398 || !TEST_true(SSL_CTX_load_verify_file(ctx, CAfile))
399 || !TEST_int_gt(SSL_CTX_dane_mtype_set(ctx, EVP_sha512(), 2, 1),
401 || !TEST_int_gt(SSL_CTX_dane_mtype_set(ctx, EVP_sha256(), 1, 2),
403 || !TEST_int_gt(test_tlsafile(ctx, basedomain, f, tlsafile), 0))
414 OPT_TEST_DECLARE_USAGE("basedomain CAfile tlsafile\n")
416 int setup_tests(void)
418 if (!test_skip_common_options()) {
419 TEST_error("Error parsing test options\n");
423 if (!TEST_ptr(basedomain = test_get_argument(0))
424 || !TEST_ptr(CAfile = test_get_argument(1))
425 || !TEST_ptr(tlsafile = test_get_argument(2)))
428 ADD_TEST(run_tlsatest);
432 #include "internal/dane.h"
434 static void store_ctx_dane_init(X509_STORE_CTX *store_ctx, SSL *ssl)
436 X509_STORE_CTX_set0_dane(store_ctx, SSL_get0_dane(ssl));