2 /* ====================================================================
3 * Copyright (c) 2015 The OpenSSL Project. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
17 * 3. All advertising materials mentioning features or use of this
18 * software must display the following acknowledgment:
19 * "This product includes software developed by the OpenSSL Project
20 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
22 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23 * endorse or promote products derived from this software without
24 * prior written permission. For written permission, please contact
25 * openssl-core@openssl.org.
27 * 5. Products derived from this software may not be called "OpenSSL"
28 * nor may "OpenSSL" appear in their names without prior written
29 * permission of the OpenSSL Project.
31 * 6. Redistributions of any form whatsoever must retain the following
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
36 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
40 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47 * OF THE POSSIBILITY OF SUCH DAMAGE.
48 * ====================================================================
57 #include <openssl/crypto.h>
58 #include <openssl/evp.h>
59 #include <openssl/x509.h>
60 #include <openssl/ssl.h>
61 #include <openssl/err.h>
62 #include <openssl/conf.h>
63 #ifndef OPENSSL_NO_ENGINE
64 #include <openssl/engine.h>
69 static const char *progname;
72 * Forward declaration, of function that uses internal interfaces, from headers
73 * included at the end of this module.
75 static void store_ctx_dane_init(X509_STORE_CTX *, SSL *);
77 static int saved_errno;
79 static void save_errno(void)
84 static int restore_errno(void)
91 static void usage(void)
93 fprintf(stderr, "usage: %s: danetest basedomain CAfile tlsafile\n", progname);
96 static void print_errors(void)
105 while ((err = ERR_get_error_line_data(&file, &line, &data, &flags)) != 0) {
106 ERR_error_string_n(err, buffer, sizeof(buffer));
107 if (flags & ERR_TXT_STRING)
108 fprintf(stderr, "Error: %s:%s:%d:%s\n", buffer, file, line, data);
110 fprintf(stderr, "Error: %s:%s:%d\n", buffer, file, line);
114 static int verify_chain(SSL *ssl, STACK_OF(X509) *chain)
117 X509_STORE_CTX *store_ctx;
118 SSL_CTX *ssl_ctx = SSL_get_SSL_CTX(ssl);
119 X509_STORE *store = SSL_CTX_get_cert_store(ssl_ctx);
120 int store_ctx_idx = SSL_get_ex_data_X509_STORE_CTX_idx();
121 X509 *cert = sk_X509_value(chain, 0);
123 if ((store_ctx = X509_STORE_CTX_new()) == NULL)
126 if (!X509_STORE_CTX_init(store_ctx, store, cert, chain))
128 X509_STORE_CTX_set_ex_data(store_ctx, store_ctx_idx, ssl);
130 X509_STORE_CTX_set_default(store_ctx,
131 SSL_is_server(ssl) ? "ssl_client" : "ssl_server");
132 X509_VERIFY_PARAM_set1(X509_STORE_CTX_get0_param(store_ctx),
133 SSL_get0_param(ssl));
134 store_ctx_dane_init(store_ctx, ssl);
136 if (SSL_get_verify_callback(ssl))
137 X509_STORE_CTX_set_verify_cb(store_ctx, SSL_get_verify_callback(ssl));
139 ret = X509_verify_cert(store_ctx);
141 SSL_set_verify_result(ssl, X509_STORE_CTX_get_error(store_ctx));
142 X509_STORE_CTX_cleanup(store_ctx);
143 X509_STORE_CTX_free(store_ctx);
148 static STACK_OF(X509) *load_chain(FILE *fp, int nelem)
153 unsigned char *data = 0;
155 char *errtype = 0; /* if error: cert or pkey? */
156 STACK_OF(X509) *chain;
157 typedef X509 *(*d2i_X509_t)(X509 **, const unsigned char **, long);
159 if ((chain = sk_X509_new_null()) == 0) {
165 count < nelem && errtype == 0
166 && PEM_read(fp, &name, &header, &data, &len);
168 const unsigned char *p = data;
170 if (strcmp(name, PEM_STRING_X509) == 0
171 || strcmp(name, PEM_STRING_X509_TRUSTED) == 0
172 || strcmp(name, PEM_STRING_X509_OLD) == 0) {
173 d2i_X509_t d = strcmp(name, PEM_STRING_X509_TRUSTED) ?
174 d2i_X509_AUX : d2i_X509;
175 X509 *cert = d(0, &p, len);
177 if (cert == 0 || (p - data) != len)
178 errtype = "certificate";
179 else if (sk_X509_push(chain, cert) == 0) {
184 fprintf(stderr, "unexpected chain file object: %s\n", name);
189 * If any of these were null, PEM_read() would have failed.
192 OPENSSL_free(header);
197 fprintf(stderr, "error reading: malformed %s\n", errtype);
201 if (count == nelem) {
207 /* Some other PEM read error */
208 sk_X509_pop_free(chain, X509_free);
213 static char *read_to_eol(FILE *f)
215 static char buf[1024];
218 if (fgets(buf, sizeof(buf), f)== NULL)
223 if (buf[n-1] != '\n') {
224 if (n+1 == sizeof(buf)) {
225 fprintf(stderr, "%s: warning: input too long\n", progname);
227 fprintf(stderr, "%s: warning: EOF before newline\n", progname);
232 /* Trim trailing whitespace */
233 while (n > 0 && isspace(buf[n-1]))
240 * Hex decoder that tolerates optional whitespace
242 static ossl_ssize_t hexdecode(const char *in, void *result)
244 unsigned char **out = (unsigned char **)result;
245 unsigned char *ret = OPENSSL_malloc(strlen(in)/2);
246 unsigned char *cp = ret;
253 for (byte = 0; *in; ++in) {
259 if ('0' <= c && c <= '9') {
261 } else if ('a' <= c && c <= 'f') {
262 byte |= c - 'a' + 10;
267 if ((nibble ^= 1) == 0) {
279 return cp - (*out = ret);
282 static ossl_ssize_t checked_uint8(const char *in, void *out)
284 uint8_t *result = (uint8_t *)out;
291 v = strtol(cp, &endp, 10);
294 if (((v == LONG_MIN || v == LONG_MAX) && e == ERANGE) ||
295 endp == cp || !isspace(*endp) ||
296 v != (*(uint8_t *)result = (uint8_t) v)) {
299 for (cp = endp; isspace(*cp); ++cp)
304 static int tlsa_import_rr(SSL *ssl, const char *rrdata)
310 unsigned char *data = NULL;
311 const char *cp = rrdata;
312 ossl_ssize_t len = 0;
316 ossl_ssize_t (*parser)(const char *, void *);
318 { &usage, "usage", checked_uint8 },
319 { &selector, "selector", checked_uint8 },
320 { &mtype, "mtype", checked_uint8 },
321 { &data, "data", hexdecode },
324 struct tlsa_field *f;
326 for (f = tlsa_fields; f->var; ++f) {
327 if ((len = f->parser(cp += len, f->var)) <= 0) {
328 fprintf(stderr, "%s: warning: bad TLSA %s field in: %s\n",
329 progname, f->name, rrdata);
333 ret = SSL_dane_tlsa_add(ssl, usage, selector, mtype, data, len);
338 fprintf(stderr, "%s: warning: unusable TLSA rrdata: %s\n",
343 fprintf(stderr, "%s: warning: error loading TLSA rrdata: %s\n",
350 static int allws(const char *cp)
358 static int test_tlsafile(SSL_CTX *ctx, const char *basename,
359 FILE *f, const char *path)
366 while (ret > 0 && (line = read_to_eol(f)) != NULL) {
367 STACK_OF(X509) *chain;
378 if (*line == '\0' || *line == '#')
382 if (sscanf(line, "%d %d %d %d%n", &ntlsa, &ncert, &want, &want_depth, &off) != 4
383 || !allws(line + off)) {
384 fprintf(stderr, "Expected tlsa count, cert count and result"
385 " at test %d of %s\n", testno, path);
389 if ((ssl = SSL_new(ctx)) == NULL)
391 SSL_set_connect_state(ssl);
392 if (SSL_dane_enable(ssl, basename) <= 0) {
397 for (i = 0; i < ntlsa; ++i) {
398 if ((line = read_to_eol(f)) == NULL || !tlsa_import_rr(ssl, line)) {
404 /* Don't report old news */
406 chain = load_chain(f, ncert);
412 ok = verify_chain(ssl, chain);
413 sk_X509_pop_free(chain, X509_free);
414 err = SSL_get_verify_result(ssl);
415 mdpth = SSL_get0_dane_authority(ssl, NULL, NULL);
420 fprintf(stderr, "verify_chain internal error in %s test %d\n",
425 if (err != want || (want == 0 && !ok)) {
428 if (want == X509_V_OK)
429 fprintf(stderr, "Verification failure in %s test %d: %d: %s\n",
430 path, testno, err, X509_verify_cert_error_string(err));
432 fprintf(stderr, "Unexpected error in %s test %d: %d: wanted %d\n",
433 path, testno, err, want);
435 fprintf(stderr, "Verification failure in %s test %d: ok=0\n",
441 if (mdpth != want_depth) {
443 fprintf(stderr, "Wrong match depth, in %s test %d: wanted %d, got: %d\n",
444 path, testno, want_depth, mdpth);
446 fprintf(stderr, "%s: test %d successful\n", path, testno);
453 int main(int argc, char *argv[])
459 const char *basedomain;
461 const char *tlsafile;
468 basedomain = argv[1];
472 f = fopen(tlsafile, "r");
474 fprintf(stderr, "%s: Error opening tlsa record file: '%s': %s\n",
475 progname, tlsafile, strerror(errno));
479 bio_err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
481 /* enable memory leak checking unless explicitly disabled */
482 if (!((getenv("OPENSSL_DEBUG_MEMORY") != NULL)
483 && (0 == strcmp(getenv("OPENSSL_DEBUG_MEMORY"), "off")))) {
484 CRYPTO_malloc_debug_init();
485 CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
487 /* OPENSSL_DEBUG_MEMORY=off */
488 CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0);
490 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
493 SSL_load_error_strings();
495 ctx = SSL_CTX_new(TLS_client_method());
496 if (SSL_CTX_dane_enable(ctx) <= 0) {
500 if (!SSL_CTX_load_verify_locations(ctx, CAfile, NULL)) {
504 if ((SSL_CTX_dane_mtype_set(ctx, EVP_sha512(), 2, 1)) <= 0) {
508 if ((SSL_CTX_dane_mtype_set(ctx, EVP_sha256(), 1, 2)) <= 0) {
513 if (test_tlsafile(ctx, argv[1], f, tlsafile) <= 0) {
525 #ifndef OPENSSL_NO_ENGINE
528 CONF_modules_unload(1);
529 CRYPTO_cleanup_all_ex_data();
531 ERR_remove_thread_state(NULL);
533 CRYPTO_mem_leaks(bio_err);
538 #include <internal/dane.h>
540 static void store_ctx_dane_init(X509_STORE_CTX *store_ctx, SSL *ssl)
542 X509_STORE_CTX_set0_dane(store_ctx, SSL_get0_dane(ssl));