danetest.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. /* ====================================================================
  2. * Copyright (c) 2015 The OpenSSL Project. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in
  13. * the documentation and/or other materials provided with the
  14. * distribution.
  15. *
  16. * 3. All advertising materials mentioning features or use of this
  17. * software must display the following acknowledgment:
  18. * "This product includes software developed by the OpenSSL Project
  19. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  20. *
  21. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  22. * endorse or promote products derived from this software without
  23. * prior written permission. For written permission, please contact
  24. * openssl-core@openssl.org.
  25. *
  26. * 5. Products derived from this software may not be called "OpenSSL"
  27. * nor may "OpenSSL" appear in their names without prior written
  28. * permission of the OpenSSL Project.
  29. *
  30. * 6. Redistributions of any form whatsoever must retain the following
  31. * acknowledgment:
  32. * "This product includes software developed by the OpenSSL Project
  33. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  34. *
  35. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  36. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  37. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  38. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  41. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  42. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  43. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  44. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  45. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  46. * OF THE POSSIBILITY OF SUCH DAMAGE.
  47. * ====================================================================
  48. */
  49. #include <stdio.h>
  50. #include <string.h>
  51. #include <ctype.h>
  52. #include <limits.h>
  53. #include <errno.h>
  54. #include <openssl/crypto.h>
  55. #include <openssl/evp.h>
  56. #include <openssl/x509.h>
  57. #include <openssl/ssl.h>
  58. #include <openssl/err.h>
  59. #include <openssl/conf.h>
  60. #ifndef OPENSSL_NO_ENGINE
  61. #include <openssl/engine.h>
  62. #endif
  63. #include "../e_os.h"
  64. static const char *progname;
  65. /*
  66. * Forward declaration, of function that uses internal interfaces, from headers
  67. * included at the end of this module.
  68. */
  69. static void store_ctx_dane_init(X509_STORE_CTX *, SSL *);
  70. static int saved_errno;
  71. static void save_errno(void)
  72. {
  73. saved_errno = errno;
  74. }
  75. static int restore_errno(void)
  76. {
  77. int ret = errno;
  78. errno = saved_errno;
  79. return ret;
  80. }
  81. static void test_usage(void)
  82. {
  83. fprintf(stderr, "usage: %s: danetest basedomain CAfile tlsafile\n", progname);
  84. }
  85. static void print_errors(void)
  86. {
  87. unsigned long err;
  88. char buffer[1024];
  89. const char *file;
  90. const char *data;
  91. int line;
  92. int flags;
  93. while ((err = ERR_get_error_line_data(&file, &line, &data, &flags)) != 0) {
  94. ERR_error_string_n(err, buffer, sizeof(buffer));
  95. if (flags & ERR_TXT_STRING)
  96. fprintf(stderr, "Error: %s:%s:%d:%s\n", buffer, file, line, data);
  97. else
  98. fprintf(stderr, "Error: %s:%s:%d\n", buffer, file, line);
  99. }
  100. }
  101. static int verify_chain(SSL *ssl, STACK_OF(X509) *chain)
  102. {
  103. int ret;
  104. X509_STORE_CTX *store_ctx;
  105. SSL_CTX *ssl_ctx = SSL_get_SSL_CTX(ssl);
  106. X509_STORE *store = SSL_CTX_get_cert_store(ssl_ctx);
  107. int store_ctx_idx = SSL_get_ex_data_X509_STORE_CTX_idx();
  108. X509 *cert = sk_X509_value(chain, 0);
  109. if ((store_ctx = X509_STORE_CTX_new()) == NULL)
  110. return -1;
  111. if (!X509_STORE_CTX_init(store_ctx, store, cert, chain))
  112. return 0;
  113. X509_STORE_CTX_set_ex_data(store_ctx, store_ctx_idx, ssl);
  114. X509_STORE_CTX_set_default(store_ctx,
  115. SSL_is_server(ssl) ? "ssl_client" : "ssl_server");
  116. X509_VERIFY_PARAM_set1(X509_STORE_CTX_get0_param(store_ctx),
  117. SSL_get0_param(ssl));
  118. store_ctx_dane_init(store_ctx, ssl);
  119. if (SSL_get_verify_callback(ssl))
  120. X509_STORE_CTX_set_verify_cb(store_ctx, SSL_get_verify_callback(ssl));
  121. ret = X509_verify_cert(store_ctx);
  122. SSL_set_verify_result(ssl, X509_STORE_CTX_get_error(store_ctx));
  123. X509_STORE_CTX_cleanup(store_ctx);
  124. X509_STORE_CTX_free(store_ctx);
  125. return (ret);
  126. }
  127. static STACK_OF(X509) *load_chain(FILE *fp, int nelem)
  128. {
  129. int count;
  130. char *name = 0;
  131. char *header = 0;
  132. unsigned char *data = 0;
  133. long len;
  134. char *errtype = 0; /* if error: cert or pkey? */
  135. STACK_OF(X509) *chain;
  136. typedef X509 *(*d2i_X509_t)(X509 **, const unsigned char **, long);
  137. if ((chain = sk_X509_new_null()) == 0) {
  138. perror("malloc");
  139. exit(1);
  140. }
  141. for (count = 0;
  142. count < nelem && errtype == 0
  143. && PEM_read(fp, &name, &header, &data, &len);
  144. ++count) {
  145. const unsigned char *p = data;
  146. if (strcmp(name, PEM_STRING_X509) == 0
  147. || strcmp(name, PEM_STRING_X509_TRUSTED) == 0
  148. || strcmp(name, PEM_STRING_X509_OLD) == 0) {
  149. d2i_X509_t d = strcmp(name, PEM_STRING_X509_TRUSTED) ?
  150. d2i_X509_AUX : d2i_X509;
  151. X509 *cert = d(0, &p, len);
  152. if (cert == 0 || (p - data) != len)
  153. errtype = "certificate";
  154. else if (sk_X509_push(chain, cert) == 0) {
  155. perror("malloc");
  156. goto err;
  157. }
  158. } else {
  159. fprintf(stderr, "unexpected chain file object: %s\n", name);
  160. goto err;
  161. }
  162. /*
  163. * If any of these were null, PEM_read() would have failed.
  164. */
  165. OPENSSL_free(name);
  166. OPENSSL_free(header);
  167. OPENSSL_free(data);
  168. }
  169. if (errtype) {
  170. fprintf(stderr, "error reading: malformed %s\n", errtype);
  171. goto err;
  172. }
  173. if (count == nelem) {
  174. ERR_clear_error();
  175. return chain;
  176. }
  177. err:
  178. /* Some other PEM read error */
  179. sk_X509_pop_free(chain, X509_free);
  180. print_errors();
  181. return NULL;
  182. }
  183. static char *read_to_eol(FILE *f)
  184. {
  185. static char buf[1024];
  186. int n;
  187. if (fgets(buf, sizeof(buf), f)== NULL)
  188. return NULL;
  189. n = strlen(buf);
  190. if (buf[n-1] != '\n') {
  191. if (n+1 == sizeof(buf)) {
  192. fprintf(stderr, "%s: warning: input too long\n", progname);
  193. } else {
  194. fprintf(stderr, "%s: warning: EOF before newline\n", progname);
  195. }
  196. return NULL;
  197. }
  198. /* Trim trailing whitespace */
  199. while (n > 0 && isspace(buf[n-1]))
  200. buf[--n] = '\0';
  201. return buf;
  202. }
  203. /*
  204. * Hex decoder that tolerates optional whitespace
  205. */
  206. static ossl_ssize_t hexdecode(const char *in, void *result)
  207. {
  208. unsigned char **out = (unsigned char **)result;
  209. unsigned char *ret = OPENSSL_malloc(strlen(in)/2);
  210. unsigned char *cp = ret;
  211. uint8_t byte;
  212. int nibble = 0;
  213. if (ret == NULL)
  214. return -1;
  215. for (byte = 0; *in; ++in) {
  216. char c;
  217. if (isspace(*in))
  218. continue;
  219. c = tolower(*in);
  220. if ('0' <= c && c <= '9') {
  221. byte |= c - '0';
  222. } else if ('a' <= c && c <= 'f') {
  223. byte |= c - 'a' + 10;
  224. } else {
  225. OPENSSL_free(ret);
  226. return 0;
  227. }
  228. if ((nibble ^= 1) == 0) {
  229. *cp++ = byte;
  230. byte = 0;
  231. } else {
  232. byte <<= 4;
  233. }
  234. }
  235. if (nibble != 0) {
  236. OPENSSL_free(ret);
  237. return 0;
  238. }
  239. return cp - (*out = ret);
  240. }
  241. static ossl_ssize_t checked_uint8(const char *in, void *out)
  242. {
  243. uint8_t *result = (uint8_t *)out;
  244. const char *cp = in;
  245. char *endp;
  246. long v;
  247. int e;
  248. save_errno();
  249. v = strtol(cp, &endp, 10);
  250. e = restore_errno();
  251. if (((v == LONG_MIN || v == LONG_MAX) && e == ERANGE) ||
  252. endp == cp || !isspace(*endp) ||
  253. v != (*(uint8_t *)result = (uint8_t) v)) {
  254. return -1;
  255. }
  256. for (cp = endp; isspace(*cp); ++cp)
  257. continue;
  258. return cp - in;
  259. }
  260. struct tlsa_field {
  261. void *var;
  262. const char *name;
  263. ossl_ssize_t (*parser)(const char *, void *);
  264. };
  265. static int tlsa_import_rr(SSL *ssl, const char *rrdata)
  266. {
  267. static uint8_t usage;
  268. static uint8_t selector;
  269. static uint8_t mtype;
  270. static unsigned char *data = NULL;
  271. static struct tlsa_field tlsa_fields[] = {
  272. { &usage, "usage", checked_uint8 },
  273. { &selector, "selector", checked_uint8 },
  274. { &mtype, "mtype", checked_uint8 },
  275. { &data, "data", hexdecode },
  276. { NULL, }
  277. };
  278. int ret;
  279. struct tlsa_field *f;
  280. const char *cp = rrdata;
  281. ossl_ssize_t len = 0;
  282. for (f = tlsa_fields; f->var; ++f) {
  283. if ((len = f->parser(cp += len, f->var)) <= 0) {
  284. fprintf(stderr, "%s: warning: bad TLSA %s field in: %s\n",
  285. progname, f->name, rrdata);
  286. return 0;
  287. }
  288. }
  289. ret = SSL_dane_tlsa_add(ssl, usage, selector, mtype, data, len);
  290. OPENSSL_free(data);
  291. if (ret == 0) {
  292. print_errors();
  293. fprintf(stderr, "%s: warning: unusable TLSA rrdata: %s\n",
  294. progname, rrdata);
  295. return 0;
  296. }
  297. if (ret < 0) {
  298. fprintf(stderr, "%s: warning: error loading TLSA rrdata: %s\n",
  299. progname, rrdata);
  300. return 0;
  301. }
  302. return ret;
  303. }
  304. static int allws(const char *cp)
  305. {
  306. while (*cp)
  307. if (!isspace(*cp++))
  308. return 0;
  309. return 1;
  310. }
  311. static int test_tlsafile(SSL_CTX *ctx, const char *basename,
  312. FILE *f, const char *path)
  313. {
  314. char *line;
  315. int testno = 0;
  316. int ret = 1;
  317. SSL *ssl;
  318. while (ret > 0 && (line = read_to_eol(f)) != NULL) {
  319. STACK_OF(X509) *chain;
  320. int ntlsa;
  321. int ncert;
  322. int want;
  323. int want_depth;
  324. int off;
  325. int i;
  326. int ok;
  327. int err;
  328. int mdpth;
  329. if (*line == '\0' || *line == '#')
  330. continue;
  331. ++testno;
  332. if (sscanf(line, "%d %d %d %d%n", &ntlsa, &ncert, &want, &want_depth, &off) != 4
  333. || !allws(line + off)) {
  334. fprintf(stderr, "Expected tlsa count, cert count and result"
  335. " at test %d of %s\n", testno, path);
  336. return 0;
  337. }
  338. if ((ssl = SSL_new(ctx)) == NULL)
  339. return -1;
  340. SSL_set_connect_state(ssl);
  341. if (SSL_dane_enable(ssl, basename) <= 0) {
  342. SSL_free(ssl);
  343. return -1;
  344. }
  345. for (i = 0; i < ntlsa; ++i) {
  346. if ((line = read_to_eol(f)) == NULL || !tlsa_import_rr(ssl, line)) {
  347. SSL_free(ssl);
  348. return 0;
  349. }
  350. }
  351. /* Don't report old news */
  352. ERR_clear_error();
  353. chain = load_chain(f, ncert);
  354. if (chain == NULL) {
  355. SSL_free(ssl);
  356. return -1;
  357. }
  358. ok = verify_chain(ssl, chain);
  359. sk_X509_pop_free(chain, X509_free);
  360. err = SSL_get_verify_result(ssl);
  361. /*
  362. * Peek under the hood, normally TLSA match data is hidden when
  363. * verification fails, we can obtain any suppressed data by setting the
  364. * verification result to X509_V_OK before looking.
  365. */
  366. SSL_set_verify_result(ssl, X509_V_OK);
  367. mdpth = SSL_get0_dane_authority(ssl, NULL, NULL);
  368. /* Not needed any more, but lead by example and put the error back. */
  369. SSL_set_verify_result(ssl, err);
  370. SSL_free(ssl);
  371. if (ok < 0) {
  372. ret = 0;
  373. fprintf(stderr, "verify_chain internal error in %s test %d\n",
  374. path, testno);
  375. print_errors();
  376. continue;
  377. }
  378. if (err != want || (want == 0 && !ok)) {
  379. ret = 0;
  380. if (err != want) {
  381. if (want == X509_V_OK)
  382. fprintf(stderr, "Verification failure in %s test %d: %d: %s\n",
  383. path, testno, err, X509_verify_cert_error_string(err));
  384. else
  385. fprintf(stderr, "Unexpected error in %s test %d: %d: wanted %d\n",
  386. path, testno, err, want);
  387. } else {
  388. fprintf(stderr, "Verification failure in %s test %d: ok=0\n",
  389. path, testno);
  390. }
  391. print_errors();
  392. continue;
  393. }
  394. if (mdpth != want_depth) {
  395. ret = 0;
  396. fprintf(stderr, "Wrong match depth, in %s test %d: wanted %d, got: %d\n",
  397. path, testno, want_depth, mdpth);
  398. }
  399. fprintf(stderr, "%s: test %d successful\n", path, testno);
  400. }
  401. ERR_clear_error();
  402. return ret;
  403. }
  404. int main(int argc, char *argv[])
  405. {
  406. FILE *f;
  407. BIO *bio_err;
  408. SSL_CTX *ctx = NULL;
  409. const char *basedomain;
  410. const char *CAfile;
  411. const char *tlsafile;
  412. const char *p;
  413. int ret = 1;
  414. progname = argv[0];
  415. if (argc != 4) {
  416. test_usage();
  417. EXIT(1);
  418. }
  419. basedomain = argv[1];
  420. CAfile = argv[2];
  421. tlsafile = argv[3];
  422. bio_err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
  423. p = getenv("OPENSSL_DEBUG_MEMORY");
  424. if (p != NULL && strcmp(p, "on") == 0)
  425. CRYPTO_set_mem_debug(1);
  426. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
  427. f = fopen(tlsafile, "r");
  428. if (f == NULL) {
  429. fprintf(stderr, "%s: Error opening tlsa record file: '%s': %s\n",
  430. progname, tlsafile, strerror(errno));
  431. return 0;
  432. }
  433. ctx = SSL_CTX_new(TLS_client_method());
  434. if (SSL_CTX_dane_enable(ctx) <= 0) {
  435. print_errors();
  436. goto end;
  437. }
  438. if (!SSL_CTX_load_verify_locations(ctx, CAfile, NULL)) {
  439. print_errors();
  440. goto end;
  441. }
  442. if ((SSL_CTX_dane_mtype_set(ctx, EVP_sha512(), 2, 1)) <= 0) {
  443. print_errors();
  444. goto end;
  445. }
  446. if ((SSL_CTX_dane_mtype_set(ctx, EVP_sha256(), 1, 2)) <= 0) {
  447. print_errors();
  448. goto end;
  449. }
  450. if (test_tlsafile(ctx, basedomain, f, tlsafile) <= 0) {
  451. print_errors();
  452. goto end;
  453. }
  454. ret = 0;
  455. end:
  456. (void) fclose(f);
  457. SSL_CTX_free(ctx);
  458. #ifndef OPENSSL_NO_CRYPTO_MDEBUG
  459. if (CRYPTO_mem_leaks(bio_err) <= 0)
  460. ret = 1;
  461. #endif
  462. BIO_free(bio_err);
  463. EXIT(ret);
  464. }
  465. #include <internal/dane.h>
  466. static void store_ctx_dane_init(X509_STORE_CTX *store_ctx, SSL *ssl)
  467. {
  468. X509_STORE_CTX_set0_dane(store_ctx, SSL_get0_dane(ssl));
  469. }