2 * Copyright 2015-2021 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"
29 #define _UC(c) ((unsigned char)(c))
31 static const char *basedomain;
32 static const char *CAfile;
33 static const char *tlsafile;
36 * Forward declaration, of function that uses internal interfaces, from headers
37 * included at the end of this module.
39 static void store_ctx_dane_init(X509_STORE_CTX *, SSL *);
41 static int saved_errno;
43 static void save_errno(void)
48 static int restore_errno(void)
55 static int verify_chain(SSL *ssl, STACK_OF(X509) *chain)
57 X509_STORE_CTX *store_ctx = NULL;
58 SSL_CTX *ssl_ctx = NULL;
59 X509_STORE *store = NULL;
61 int store_ctx_idx = SSL_get_ex_data_X509_STORE_CTX_idx();
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,
71 X509_STORE_CTX_set_default(store_ctx, SSL_is_server(ssl)
72 ? "ssl_client" : "ssl_server");
73 X509_VERIFY_PARAM_set1(X509_STORE_CTX_get0_param(store_ctx),
75 store_ctx_dane_init(store_ctx, ssl);
77 if (SSL_get_verify_callback(ssl) != NULL)
78 X509_STORE_CTX_set_verify_cb(store_ctx, SSL_get_verify_callback(ssl));
80 /* Mask "internal failures" (-1) from our return value. */
81 if (!TEST_int_ge(ret = X509_STORE_CTX_verify(store_ctx), 0))
84 SSL_set_verify_result(ssl, X509_STORE_CTX_get_error(store_ctx));
87 X509_STORE_CTX_free(store_ctx);
91 static STACK_OF(X509) *load_chain(BIO *fp, int nelem)
96 unsigned char *data = 0;
98 char *errtype = 0; /* if error: cert or pkey? */
99 STACK_OF(X509) *chain;
100 typedef X509 *(*d2i_X509_t)(X509 **, const unsigned char **, long);
102 if (!TEST_ptr(chain = sk_X509_new_null()))
106 count < nelem && errtype == 0
107 && PEM_read_bio(fp, &name, &header, &data, &len) == 1;
109 if (strcmp(name, PEM_STRING_X509) == 0
110 || strcmp(name, PEM_STRING_X509_TRUSTED) == 0
111 || strcmp(name, PEM_STRING_X509_OLD) == 0) {
112 d2i_X509_t d = strcmp(name, PEM_STRING_X509_TRUSTED) != 0
113 ? d2i_X509_AUX : d2i_X509;
115 const unsigned char *p = data;
117 if (!TEST_ptr(cert = d(0, &p, len))
118 || !TEST_long_eq(p - data, len)) {
119 TEST_info("Certificate parsing error");
123 if (!TEST_true(sk_X509_push(chain, cert)))
126 TEST_info("Unknown chain file object %s", name);
131 OPENSSL_free(header);
133 name = header = NULL;
137 if (count == nelem) {
144 OPENSSL_free(header);
146 OSSL_STACK_OF_X509_free(chain);
150 static char *read_to_eol(BIO *f)
152 static char buf[4096];
155 if (BIO_gets(f, buf, sizeof(buf)) <= 0)
159 if (buf[n - 1] != '\n') {
160 if (n + 1 == sizeof(buf))
161 TEST_error("input too long");
163 TEST_error("EOF before newline");
167 /* Trim trailing whitespace */
168 while (n > 0 && isspace(_UC(buf[n - 1])))
175 * Hex decoder that tolerates optional whitespace
177 static ossl_ssize_t hexdecode(const char *in, void *result)
179 unsigned char **out = (unsigned char **)result;
185 if (!TEST_ptr(ret = OPENSSL_malloc(strlen(in) / 2)))
189 for (byte = 0; *in; ++in) {
192 if (isspace(_UC(*in)))
194 x = OPENSSL_hexchar2int(*in);
200 if ((nibble ^= 1) == 0) {
212 return cp - (*out = ret);
215 static ossl_ssize_t checked_uint8(const char *in, void *out)
217 uint8_t *result = (uint8_t *)out;
224 v = strtol(cp, &endp, 10);
227 if (((v == LONG_MIN || v == LONG_MAX) && e == ERANGE) ||
228 endp == cp || !isspace(_UC(*endp)) ||
229 v != (*(uint8_t *)result = (uint8_t) v)) {
232 for (cp = endp; isspace(_UC(*cp)); ++cp)
240 ossl_ssize_t (*parser)(const char *, void *);
243 static int tlsa_import_rr(SSL *ssl, const char *rrdata)
245 static uint8_t usage;
246 static uint8_t selector;
247 static uint8_t mtype;
248 static unsigned char *data = NULL;
249 static struct tlsa_field tlsa_fields[] = {
250 { &usage, "usage", checked_uint8 },
251 { &selector, "selector", checked_uint8 },
252 { &mtype, "mtype", checked_uint8 },
253 { &data, "data", hexdecode },
257 struct tlsa_field *f;
258 const char *cp = rrdata;
259 ossl_ssize_t len = 0;
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);
268 ret = SSL_dane_tlsa_add(ssl, usage, selector, mtype, data, len);
271 TEST_info("unusable TLSA rrdata: %s", rrdata);
275 TEST_info("error loading TLSA rrdata: %s", rrdata);
282 static int allws(const char *cp)
285 if (!isspace(_UC(*cp++)))
290 static int test_tlsafile(SSL_CTX *ctx, const char *base_name,
291 BIO *f, const char *path)
298 while (ret > 0 && (line = read_to_eol(f)) != NULL) {
299 STACK_OF(X509) *chain;
311 if (*line == '\0' || *line == '#')
315 if (sscanf(line, "%d %d %d %d %d%n",
316 &ntlsa, &ncert, &noncheck, &want, &want_depth, &off) != 5
317 || !allws(line + off)) {
318 TEST_error("Malformed line for test %d", testno);
322 if (!TEST_ptr(ssl = SSL_new(ctx)))
324 SSL_set_connect_state(ssl);
325 if (SSL_dane_enable(ssl, base_name) <= 0) {
330 SSL_dane_set_flags(ssl, DANE_FLAG_NO_DANE_EE_NAMECHECKS);
332 for (i = 0; i < ntlsa; ++i) {
333 if ((line = read_to_eol(f)) == NULL || !tlsa_import_rr(ssl, line)) {
339 /* Don't report old news */
341 if (!TEST_ptr(chain = load_chain(f, ncert))) {
346 ok = verify_chain(ssl, chain);
347 OSSL_STACK_OF_X509_free(chain);
348 err = SSL_get_verify_result(ssl);
350 * Peek under the hood, normally TLSA match data is hidden when
351 * verification fails, we can obtain any suppressed data by setting the
352 * verification result to X509_V_OK before looking.
354 SSL_set_verify_result(ssl, X509_V_OK);
355 mdpth = SSL_get0_dane_authority(ssl, NULL, NULL);
356 /* Not needed any more, but lead by example and put the error back. */
357 SSL_set_verify_result(ssl, err);
360 if (!TEST_int_eq(err, want)) {
361 if (want == X509_V_OK)
362 TEST_info("Verification failure in test %d: %d=%s",
363 testno, err, X509_verify_cert_error_string(err));
365 TEST_info("Unexpected error in test %d", testno);
369 if (!TEST_false(want == 0 && ok == 0)) {
370 TEST_info("Verification failure in test %d: ok=0", testno);
374 if (!TEST_int_eq(mdpth, want_depth)) {
375 TEST_info("In test test %d", testno);
384 static int run_tlsatest(void)
390 if (!TEST_ptr(f = BIO_new_file(tlsafile, "r"))
391 || !TEST_ptr(ctx = SSL_CTX_new(TLS_client_method()))
392 || !TEST_int_gt(SSL_CTX_dane_enable(ctx), 0)
393 || !TEST_true(SSL_CTX_load_verify_file(ctx, CAfile))
394 || !TEST_int_gt(SSL_CTX_dane_mtype_set(ctx, EVP_sha512(), 2, 1), 0)
395 || !TEST_int_gt(SSL_CTX_dane_mtype_set(ctx, EVP_sha256(), 1, 2), 0)
396 || !TEST_int_gt(test_tlsafile(ctx, basedomain, f, tlsafile), 0))
407 OPT_TEST_DECLARE_USAGE("basedomain CAfile tlsafile\n")
409 int setup_tests(void)
411 if (!test_skip_common_options()) {
412 TEST_error("Error parsing test options\n");
416 if (!TEST_ptr(basedomain = test_get_argument(0))
417 || !TEST_ptr(CAfile = test_get_argument(1))
418 || !TEST_ptr(tlsafile = test_get_argument(2)))
421 ADD_TEST(run_tlsatest);
425 #include "internal/dane.h"
427 static void store_ctx_dane_init(X509_STORE_CTX *store_ctx, SSL *ssl)
429 X509_STORE_CTX_set0_dane(store_ctx, SSL_get0_dane(ssl));