2 * Copyright 2015-2017 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;
62 int store_ctx_idx = SSL_get_ex_data_X509_STORE_CTX_idx();
64 if (!TEST_ptr(store_ctx = X509_STORE_CTX_new())
65 || !TEST_ptr(ssl_ctx = SSL_get_SSL_CTX(ssl))
66 || !TEST_ptr(store = SSL_CTX_get_cert_store(ssl_ctx))
67 || !TEST_ptr(cert = sk_X509_value(chain, 0))
68 || !TEST_true(X509_STORE_CTX_init(store_ctx, store, cert, chain))
69 || !TEST_true(X509_STORE_CTX_set_ex_data(store_ctx, store_ctx_idx,
73 X509_STORE_CTX_set_default(store_ctx,
74 SSL_is_server(ssl) ? "ssl_client" : "ssl_server");
75 X509_VERIFY_PARAM_set1(X509_STORE_CTX_get0_param(store_ctx),
77 store_ctx_dane_init(store_ctx, ssl);
79 if (SSL_get_verify_callback(ssl) != NULL)
80 X509_STORE_CTX_set_verify_cb(store_ctx, SSL_get_verify_callback(ssl));
82 /* Mask "internal failures" (-1) from our return value. */
83 if (!TEST_int_ge(ret = X509_verify_cert(store_ctx), 0))
86 SSL_set_verify_result(ssl, X509_STORE_CTX_get_error(store_ctx));
87 X509_STORE_CTX_cleanup(store_ctx);
90 X509_STORE_CTX_free(store_ctx);
94 static STACK_OF(X509) *load_chain(BIO *fp, int nelem)
99 unsigned char *data = 0;
101 char *errtype = 0; /* if error: cert or pkey? */
102 STACK_OF(X509) *chain;
103 typedef X509 *(*d2i_X509_t)(X509 **, const unsigned char **, long);
105 if (!TEST_ptr(chain = sk_X509_new_null()))
109 count < nelem && errtype == 0
110 && PEM_read_bio(fp, &name, &header, &data, &len) == 1;
112 if (strcmp(name, PEM_STRING_X509) == 0
113 || strcmp(name, PEM_STRING_X509_TRUSTED) == 0
114 || strcmp(name, PEM_STRING_X509_OLD) == 0) {
115 d2i_X509_t d = strcmp(name, PEM_STRING_X509_TRUSTED) != 0
116 ? d2i_X509_AUX : d2i_X509;
118 const unsigned char *p = data;
120 if (!TEST_ptr(cert = d(0, &p, len))
121 || !TEST_long_eq(p - data, len)) {
122 TEST_info("Certificate parsing error");
126 if (!TEST_true(sk_X509_push(chain, cert)))
129 TEST_info("Unknown chain file object %s", name);
134 OPENSSL_free(header);
136 name = header = NULL;
140 if (count == nelem) {
147 OPENSSL_free(header);
149 sk_X509_pop_free(chain, X509_free);
153 static char *read_to_eol(BIO *f)
155 static char buf[1024];
158 if (!BIO_gets(f, buf, sizeof(buf)))
162 if (buf[n - 1] != '\n') {
163 if (n + 1 == sizeof(buf))
164 TEST_error("input too long");
166 TEST_error("EOF before newline");
170 /* Trim trailing whitespace */
171 while (n > 0 && isspace(_UC(buf[n - 1])))
178 * Hex decoder that tolerates optional whitespace
180 static ossl_ssize_t hexdecode(const char *in, void *result)
182 unsigned char **out = (unsigned char **)result;
188 if (!TEST_ptr(ret = OPENSSL_malloc(strlen(in) / 2)))
192 for (byte = 0; *in; ++in) {
195 if (isspace(_UC(*in)))
197 x = OPENSSL_hexchar2int(*in);
203 if ((nibble ^= 1) == 0) {
215 return cp - (*out = ret);
218 static ossl_ssize_t checked_uint8(const char *in, void *out)
220 uint8_t *result = (uint8_t *)out;
227 v = strtol(cp, &endp, 10);
230 if (((v == LONG_MIN || v == LONG_MAX) && e == ERANGE) ||
231 endp == cp || !isspace(_UC(*endp)) ||
232 v != (*(uint8_t *)result = (uint8_t) v)) {
235 for (cp = endp; isspace(_UC(*cp)); ++cp)
243 ossl_ssize_t (*parser)(const char *, void *);
246 static int tlsa_import_rr(SSL *ssl, const char *rrdata)
248 static uint8_t usage;
249 static uint8_t selector;
250 static uint8_t mtype;
251 static unsigned char *data = NULL;
252 static struct tlsa_field tlsa_fields[] = {
253 { &usage, "usage", checked_uint8 },
254 { &selector, "selector", checked_uint8 },
255 { &mtype, "mtype", checked_uint8 },
256 { &data, "data", hexdecode },
260 struct tlsa_field *f;
261 const char *cp = rrdata;
262 ossl_ssize_t len = 0;
264 for (f = tlsa_fields; f->var; ++f) {
265 if ((len = f->parser(cp += len, f->var)) <= 0) {
266 TEST_info("bad TLSA %s field in: %s", f->name, rrdata);
271 ret = SSL_dane_tlsa_add(ssl, usage, selector, mtype, data, len);
274 TEST_info("unusable TLSA rrdata: %s", rrdata);
278 TEST_info("error loading TLSA rrdata: %s", rrdata);
285 static int allws(const char *cp)
288 if (!isspace(_UC(*cp++)))
293 static int test_tlsafile(SSL_CTX *ctx, const char *base_name,
294 BIO *f, const char *path)
301 while (ret > 0 && (line = read_to_eol(f)) != NULL) {
302 STACK_OF(X509) *chain;
314 if (*line == '\0' || *line == '#')
318 if (sscanf(line, "%d %d %d %d %d%n",
319 &ntlsa, &ncert, &noncheck, &want, &want_depth, &off) != 5
320 || !allws(line + off)) {
321 TEST_error("Malformed line for test %d", testno);
325 if (!TEST_ptr(ssl = SSL_new(ctx)))
327 SSL_set_connect_state(ssl);
328 if (SSL_dane_enable(ssl, base_name) <= 0) {
333 SSL_dane_set_flags(ssl, DANE_FLAG_NO_DANE_EE_NAMECHECKS);
335 for (i = 0; i < ntlsa; ++i) {
336 if ((line = read_to_eol(f)) == NULL || !tlsa_import_rr(ssl, line)) {
342 /* Don't report old news */
344 if (!TEST_ptr(chain = load_chain(f, ncert))) {
349 ok = verify_chain(ssl, chain);
350 sk_X509_pop_free(chain, X509_free);
351 err = SSL_get_verify_result(ssl);
353 * Peek under the hood, normally TLSA match data is hidden when
354 * verification fails, we can obtain any suppressed data by setting the
355 * verification result to X509_V_OK before looking.
357 SSL_set_verify_result(ssl, X509_V_OK);
358 mdpth = SSL_get0_dane_authority(ssl, NULL, NULL);
359 /* Not needed any more, but lead by example and put the error back. */
360 SSL_set_verify_result(ssl, err);
363 if (!TEST_int_eq(err, want)) {
364 if (want == X509_V_OK)
365 TEST_info("Verification failure in test %d: %d=%s",
366 testno, err, X509_verify_cert_error_string(err));
368 TEST_info("Unexpected error in test %d", testno);
372 if (!TEST_false(want == 0 && ok == 0)) {
373 TEST_info("Verification failure in test %d: ok=0", testno);
377 if (!TEST_int_eq(mdpth, want_depth)) {
378 TEST_info("In test test %d", testno);
387 static int run_tlsatest(void)
393 if (!TEST_ptr(f = BIO_new_file(tlsafile, "r"))
394 || !TEST_ptr(ctx = SSL_CTX_new(TLS_client_method()))
395 || !TEST_int_gt(SSL_CTX_dane_enable(ctx), 0)
396 || !TEST_true(SSL_CTX_load_verify_locations(ctx, CAfile, NULL))
397 || !TEST_int_gt(SSL_CTX_dane_mtype_set(ctx, EVP_sha512(), 2, 1),
399 || !TEST_int_gt(SSL_CTX_dane_mtype_set(ctx, EVP_sha256(), 1, 2),
401 || !TEST_int_gt(test_tlsafile(ctx, basedomain, f, tlsafile), 0))
412 int setup_tests(void)
414 if (!TEST_ptr(basedomain = test_get_argument(0))
415 || !TEST_ptr(CAfile = test_get_argument(1))
416 || !TEST_ptr(tlsafile = test_get_argument(2))) {
417 TEST_error("Usage error: danetest basedomain CAfile tlsafile");
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));