2 * Copyright 2015-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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>
28 #define _UC(c) ((unsigned char)(c))
30 static const char *progname;
33 * Forward declaration, of function that uses internal interfaces, from headers
34 * included at the end of this module.
36 static void store_ctx_dane_init(X509_STORE_CTX *, SSL *);
38 static int saved_errno;
40 static void save_errno(void)
45 static int restore_errno(void)
52 static void test_usage(void)
54 fprintf(stderr, "usage: %s: danetest basedomain CAfile tlsafile\n", progname);
57 static void print_errors(void)
66 while ((err = ERR_get_error_line_data(&file, &line, &data, &flags)) != 0) {
67 ERR_error_string_n(err, buffer, sizeof(buffer));
68 if (flags & ERR_TXT_STRING)
69 fprintf(stderr, "Error: %s:%s:%d:%s\n", buffer, file, line, data);
71 fprintf(stderr, "Error: %s:%s:%d\n", buffer, file, line);
75 static int verify_chain(SSL *ssl, STACK_OF(X509) *chain)
78 X509_STORE_CTX *store_ctx;
79 SSL_CTX *ssl_ctx = SSL_get_SSL_CTX(ssl);
80 X509_STORE *store = SSL_CTX_get_cert_store(ssl_ctx);
81 int store_ctx_idx = SSL_get_ex_data_X509_STORE_CTX_idx();
82 X509 *cert = sk_X509_value(chain, 0);
84 if ((store_ctx = X509_STORE_CTX_new()) == NULL)
87 if (!X509_STORE_CTX_init(store_ctx, store, cert, chain))
89 X509_STORE_CTX_set_ex_data(store_ctx, store_ctx_idx, ssl);
91 X509_STORE_CTX_set_default(store_ctx,
92 SSL_is_server(ssl) ? "ssl_client" : "ssl_server");
93 X509_VERIFY_PARAM_set1(X509_STORE_CTX_get0_param(store_ctx),
95 store_ctx_dane_init(store_ctx, ssl);
97 if (SSL_get_verify_callback(ssl))
98 X509_STORE_CTX_set_verify_cb(store_ctx, SSL_get_verify_callback(ssl));
100 ret = X509_verify_cert(store_ctx);
102 SSL_set_verify_result(ssl, X509_STORE_CTX_get_error(store_ctx));
103 X509_STORE_CTX_cleanup(store_ctx);
104 X509_STORE_CTX_free(store_ctx);
109 static STACK_OF(X509) *load_chain(BIO *fp, int nelem)
114 unsigned char *data = 0;
116 char *errtype = 0; /* if error: cert or pkey? */
117 STACK_OF(X509) *chain;
118 typedef X509 *(*d2i_X509_t)(X509 **, const unsigned char **, long);
120 if ((chain = sk_X509_new_null()) == 0) {
126 count < nelem && errtype == 0
127 && PEM_read_bio(fp, &name, &header, &data, &len);
129 const unsigned char *p = data;
131 if (strcmp(name, PEM_STRING_X509) == 0
132 || strcmp(name, PEM_STRING_X509_TRUSTED) == 0
133 || strcmp(name, PEM_STRING_X509_OLD) == 0) {
134 d2i_X509_t d = strcmp(name, PEM_STRING_X509_TRUSTED) ?
135 d2i_X509_AUX : d2i_X509;
136 X509 *cert = d(0, &p, len);
138 if (cert == 0 || (p - data) != len)
139 errtype = "certificate";
140 else if (sk_X509_push(chain, cert) == 0) {
145 fprintf(stderr, "unexpected chain file object: %s\n", name);
150 * If any of these were null, PEM_read() would have failed.
153 OPENSSL_free(header);
158 fprintf(stderr, "error reading: malformed %s\n", errtype);
162 if (count == nelem) {
168 /* Some other PEM read error */
169 sk_X509_pop_free(chain, X509_free);
174 static char *read_to_eol(BIO *f)
176 static char buf[1024];
179 if (!BIO_gets(f, buf, sizeof(buf)))
184 if (buf[n-1] != '\n') {
185 if (n+1 == sizeof(buf)) {
186 fprintf(stderr, "%s: warning: input too long\n", progname);
188 fprintf(stderr, "%s: warning: EOF before newline\n", progname);
193 /* Trim trailing whitespace */
194 while (n > 0 && isspace(_UC(buf[n-1])))
201 * Hex decoder that tolerates optional whitespace
203 static ossl_ssize_t hexdecode(const char *in, void *result)
205 unsigned char **out = (unsigned char **)result;
206 unsigned char *ret = OPENSSL_malloc(strlen(in)/2);
207 unsigned char *cp = ret;
214 for (byte = 0; *in; ++in) {
217 if (isspace(_UC(*in)))
219 x = OPENSSL_hexchar2int(*in);
225 if ((nibble ^= 1) == 0) {
237 return cp - (*out = ret);
240 static ossl_ssize_t checked_uint8(const char *in, void *out)
242 uint8_t *result = (uint8_t *)out;
249 v = strtol(cp, &endp, 10);
252 if (((v == LONG_MIN || v == LONG_MAX) && e == ERANGE) ||
253 endp == cp || !isspace(_UC(*endp)) ||
254 v != (*(uint8_t *)result = (uint8_t) v)) {
257 for (cp = endp; isspace(_UC(*cp)); ++cp)
265 ossl_ssize_t (*parser)(const char *, void *);
268 static int tlsa_import_rr(SSL *ssl, const char *rrdata)
270 static uint8_t usage;
271 static uint8_t selector;
272 static uint8_t mtype;
273 static unsigned char *data = NULL;
274 static struct tlsa_field tlsa_fields[] = {
275 { &usage, "usage", checked_uint8 },
276 { &selector, "selector", checked_uint8 },
277 { &mtype, "mtype", checked_uint8 },
278 { &data, "data", hexdecode },
282 struct tlsa_field *f;
283 const char *cp = rrdata;
284 ossl_ssize_t len = 0;
286 for (f = tlsa_fields; f->var; ++f) {
287 if ((len = f->parser(cp += len, f->var)) <= 0) {
288 fprintf(stderr, "%s: warning: bad TLSA %s field in: %s\n",
289 progname, f->name, rrdata);
293 ret = SSL_dane_tlsa_add(ssl, usage, selector, mtype, data, len);
298 fprintf(stderr, "%s: warning: unusable TLSA rrdata: %s\n",
303 fprintf(stderr, "%s: warning: error loading TLSA rrdata: %s\n",
310 static int allws(const char *cp)
313 if (!isspace(_UC(*cp++)))
318 static int test_tlsafile(SSL_CTX *ctx, const char *basename,
319 BIO *f, const char *path)
326 while (ret > 0 && (line = read_to_eol(f)) != NULL) {
327 STACK_OF(X509) *chain;
338 if (*line == '\0' || *line == '#')
342 if (sscanf(line, "%d %d %d %d%n", &ntlsa, &ncert, &want, &want_depth, &off) != 4
343 || !allws(line + off)) {
344 fprintf(stderr, "Expected tlsa count, cert count and result"
345 " at test %d of %s\n", testno, path);
349 if ((ssl = SSL_new(ctx)) == NULL)
351 SSL_set_connect_state(ssl);
352 if (SSL_dane_enable(ssl, basename) <= 0) {
357 for (i = 0; i < ntlsa; ++i) {
358 if ((line = read_to_eol(f)) == NULL || !tlsa_import_rr(ssl, line)) {
364 /* Don't report old news */
366 chain = load_chain(f, ncert);
372 ok = verify_chain(ssl, chain);
373 sk_X509_pop_free(chain, X509_free);
374 err = SSL_get_verify_result(ssl);
376 * Peek under the hood, normally TLSA match data is hidden when
377 * verification fails, we can obtain any suppressed data by setting the
378 * verification result to X509_V_OK before looking.
380 SSL_set_verify_result(ssl, X509_V_OK);
381 mdpth = SSL_get0_dane_authority(ssl, NULL, NULL);
382 /* Not needed any more, but lead by example and put the error back. */
383 SSL_set_verify_result(ssl, err);
388 fprintf(stderr, "verify_chain internal error in %s test %d\n",
393 if (err != want || (want == 0 && !ok)) {
396 if (want == X509_V_OK)
397 fprintf(stderr, "Verification failure in %s test %d: %d: %s\n",
398 path, testno, err, X509_verify_cert_error_string(err));
400 fprintf(stderr, "Unexpected error in %s test %d: %d: wanted %d\n",
401 path, testno, err, want);
403 fprintf(stderr, "Verification failure in %s test %d: ok=0\n",
409 if (mdpth != want_depth) {
411 fprintf(stderr, "Wrong match depth, in %s test %d: wanted %d, got: %d\n",
412 path, testno, want_depth, mdpth);
414 fprintf(stderr, "%s: test %d successful\n", path, testno);
421 int main(int argc, char *argv[])
426 const char *basedomain;
428 const char *tlsafile;
437 basedomain = argv[1];
441 bio_err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
443 p = getenv("OPENSSL_DEBUG_MEMORY");
444 if (p != NULL && strcmp(p, "on") == 0)
445 CRYPTO_set_mem_debug(1);
446 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
448 f = BIO_new_file(tlsafile, "r");
450 fprintf(stderr, "%s: Error opening tlsa record file: '%s': %s\n",
451 progname, tlsafile, strerror(errno));
455 ctx = SSL_CTX_new(TLS_client_method());
456 if (SSL_CTX_dane_enable(ctx) <= 0) {
460 if (!SSL_CTX_load_verify_locations(ctx, CAfile, NULL)) {
464 if ((SSL_CTX_dane_mtype_set(ctx, EVP_sha512(), 2, 1)) <= 0) {
468 if ((SSL_CTX_dane_mtype_set(ctx, EVP_sha256(), 1, 2)) <= 0) {
473 if (test_tlsafile(ctx, basedomain, f, tlsafile) <= 0) {
485 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
486 if (CRYPTO_mem_leaks(bio_err) <= 0)
493 #include <internal/dane.h>
495 static void store_ctx_dane_init(X509_STORE_CTX *store_ctx, SSL *ssl)
497 X509_STORE_CTX_set0_dane(store_ctx, SSL_get0_dane(ssl));