danetest.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. /*
  2. * Copyright 2015-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  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
  8. */
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <ctype.h>
  12. #include <limits.h>
  13. #include <errno.h>
  14. #include <openssl/crypto.h>
  15. #include <openssl/evp.h>
  16. #include <openssl/x509.h>
  17. #include <openssl/ssl.h>
  18. #include <openssl/err.h>
  19. #include <openssl/conf.h>
  20. #ifndef OPENSSL_NO_ENGINE
  21. #include <openssl/engine.h>
  22. #endif
  23. #include "../e_os.h"
  24. #define _UC(c) ((unsigned char)(c))
  25. static const char *progname;
  26. /*
  27. * Forward declaration, of function that uses internal interfaces, from headers
  28. * included at the end of this module.
  29. */
  30. static void store_ctx_dane_init(X509_STORE_CTX *, SSL *);
  31. static int saved_errno;
  32. static void save_errno(void)
  33. {
  34. saved_errno = errno;
  35. }
  36. static int restore_errno(void)
  37. {
  38. int ret = errno;
  39. errno = saved_errno;
  40. return ret;
  41. }
  42. static void test_usage(void)
  43. {
  44. fprintf(stderr, "usage: %s: danetest basedomain CAfile tlsafile\n", progname);
  45. }
  46. static void print_errors(void)
  47. {
  48. unsigned long err;
  49. char buffer[1024];
  50. const char *file;
  51. const char *data;
  52. int line;
  53. int flags;
  54. while ((err = ERR_get_error_line_data(&file, &line, &data, &flags)) != 0) {
  55. ERR_error_string_n(err, buffer, sizeof(buffer));
  56. if (flags & ERR_TXT_STRING)
  57. fprintf(stderr, "Error: %s:%s:%d:%s\n", buffer, file, line, data);
  58. else
  59. fprintf(stderr, "Error: %s:%s:%d\n", buffer, file, line);
  60. }
  61. }
  62. static int verify_chain(SSL *ssl, STACK_OF(X509) *chain)
  63. {
  64. int ret = -1;
  65. X509_STORE_CTX *store_ctx;
  66. SSL_CTX *ssl_ctx = SSL_get_SSL_CTX(ssl);
  67. X509_STORE *store = SSL_CTX_get_cert_store(ssl_ctx);
  68. int store_ctx_idx = SSL_get_ex_data_X509_STORE_CTX_idx();
  69. X509 *cert = sk_X509_value(chain, 0);
  70. if ((store_ctx = X509_STORE_CTX_new()) == NULL)
  71. return -1;
  72. if (!X509_STORE_CTX_init(store_ctx, store, cert, chain))
  73. goto end;
  74. if (!X509_STORE_CTX_set_ex_data(store_ctx, store_ctx_idx, ssl))
  75. goto end;
  76. X509_STORE_CTX_set_default(store_ctx,
  77. SSL_is_server(ssl) ? "ssl_client" : "ssl_server");
  78. X509_VERIFY_PARAM_set1(X509_STORE_CTX_get0_param(store_ctx),
  79. SSL_get0_param(ssl));
  80. store_ctx_dane_init(store_ctx, ssl);
  81. if (SSL_get_verify_callback(ssl))
  82. X509_STORE_CTX_set_verify_cb(store_ctx, SSL_get_verify_callback(ssl));
  83. ret = X509_verify_cert(store_ctx);
  84. SSL_set_verify_result(ssl, X509_STORE_CTX_get_error(store_ctx));
  85. X509_STORE_CTX_cleanup(store_ctx);
  86. end:
  87. X509_STORE_CTX_free(store_ctx);
  88. return (ret);
  89. }
  90. static STACK_OF(X509) *load_chain(BIO *fp, int nelem)
  91. {
  92. int count;
  93. char *name = 0;
  94. char *header = 0;
  95. unsigned char *data = 0;
  96. long len;
  97. char *errtype = 0; /* if error: cert or pkey? */
  98. STACK_OF(X509) *chain;
  99. typedef X509 *(*d2i_X509_t)(X509 **, const unsigned char **, long);
  100. if ((chain = sk_X509_new_null()) == 0) {
  101. perror("malloc");
  102. exit(1);
  103. }
  104. for (count = 0;
  105. count < nelem && errtype == 0
  106. && PEM_read_bio(fp, &name, &header, &data, &len);
  107. ++count) {
  108. const unsigned char *p = data;
  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) ?
  113. d2i_X509_AUX : d2i_X509;
  114. X509 *cert = d(0, &p, len);
  115. if (cert == 0 || (p - data) != len)
  116. errtype = "certificate";
  117. else if (sk_X509_push(chain, cert) == 0) {
  118. perror("malloc");
  119. goto err;
  120. }
  121. } else {
  122. fprintf(stderr, "unexpected chain file object: %s\n", name);
  123. goto err;
  124. }
  125. /*
  126. * If any of these were null, PEM_read() would have failed.
  127. */
  128. OPENSSL_free(name);
  129. OPENSSL_free(header);
  130. OPENSSL_free(data);
  131. }
  132. if (errtype) {
  133. fprintf(stderr, "error reading: malformed %s\n", errtype);
  134. goto err;
  135. }
  136. if (count == nelem) {
  137. ERR_clear_error();
  138. return chain;
  139. }
  140. err:
  141. /* Some other PEM read error */
  142. sk_X509_pop_free(chain, X509_free);
  143. print_errors();
  144. return NULL;
  145. }
  146. static char *read_to_eol(BIO *f)
  147. {
  148. static char buf[1024];
  149. int n;
  150. if (!BIO_gets(f, buf, sizeof(buf)))
  151. return NULL;
  152. n = strlen(buf);
  153. if (buf[n-1] != '\n') {
  154. if (n+1 == sizeof(buf)) {
  155. fprintf(stderr, "%s: warning: input too long\n", progname);
  156. } else {
  157. fprintf(stderr, "%s: warning: EOF before newline\n", progname);
  158. }
  159. return NULL;
  160. }
  161. /* Trim trailing whitespace */
  162. while (n > 0 && isspace(_UC(buf[n-1])))
  163. buf[--n] = '\0';
  164. return buf;
  165. }
  166. /*
  167. * Hex decoder that tolerates optional whitespace
  168. */
  169. static ossl_ssize_t hexdecode(const char *in, void *result)
  170. {
  171. unsigned char **out = (unsigned char **)result;
  172. unsigned char *ret = OPENSSL_malloc(strlen(in)/2);
  173. unsigned char *cp = ret;
  174. uint8_t byte;
  175. int nibble = 0;
  176. if (ret == NULL)
  177. return -1;
  178. for (byte = 0; *in; ++in) {
  179. int x;
  180. if (isspace(_UC(*in)))
  181. continue;
  182. x = OPENSSL_hexchar2int(*in);
  183. if (x < 0) {
  184. OPENSSL_free(ret);
  185. return 0;
  186. }
  187. byte |= (char)x;
  188. if ((nibble ^= 1) == 0) {
  189. *cp++ = byte;
  190. byte = 0;
  191. } else {
  192. byte <<= 4;
  193. }
  194. }
  195. if (nibble != 0) {
  196. OPENSSL_free(ret);
  197. return 0;
  198. }
  199. return cp - (*out = ret);
  200. }
  201. static ossl_ssize_t checked_uint8(const char *in, void *out)
  202. {
  203. uint8_t *result = (uint8_t *)out;
  204. const char *cp = in;
  205. char *endp;
  206. long v;
  207. int e;
  208. save_errno();
  209. v = strtol(cp, &endp, 10);
  210. e = restore_errno();
  211. if (((v == LONG_MIN || v == LONG_MAX) && e == ERANGE) ||
  212. endp == cp || !isspace(_UC(*endp)) ||
  213. v != (*(uint8_t *)result = (uint8_t) v)) {
  214. return -1;
  215. }
  216. for (cp = endp; isspace(_UC(*cp)); ++cp)
  217. continue;
  218. return cp - in;
  219. }
  220. struct tlsa_field {
  221. void *var;
  222. const char *name;
  223. ossl_ssize_t (*parser)(const char *, void *);
  224. };
  225. static int tlsa_import_rr(SSL *ssl, const char *rrdata)
  226. {
  227. static uint8_t usage;
  228. static uint8_t selector;
  229. static uint8_t mtype;
  230. static unsigned char *data = NULL;
  231. static struct tlsa_field tlsa_fields[] = {
  232. { &usage, "usage", checked_uint8 },
  233. { &selector, "selector", checked_uint8 },
  234. { &mtype, "mtype", checked_uint8 },
  235. { &data, "data", hexdecode },
  236. { NULL, }
  237. };
  238. int ret;
  239. struct tlsa_field *f;
  240. const char *cp = rrdata;
  241. ossl_ssize_t len = 0;
  242. for (f = tlsa_fields; f->var; ++f) {
  243. if ((len = f->parser(cp += len, f->var)) <= 0) {
  244. fprintf(stderr, "%s: warning: bad TLSA %s field in: %s\n",
  245. progname, f->name, rrdata);
  246. return 0;
  247. }
  248. }
  249. ret = SSL_dane_tlsa_add(ssl, usage, selector, mtype, data, len);
  250. OPENSSL_free(data);
  251. if (ret == 0) {
  252. print_errors();
  253. fprintf(stderr, "%s: warning: unusable TLSA rrdata: %s\n",
  254. progname, rrdata);
  255. return 0;
  256. }
  257. if (ret < 0) {
  258. fprintf(stderr, "%s: warning: error loading TLSA rrdata: %s\n",
  259. progname, rrdata);
  260. return 0;
  261. }
  262. return ret;
  263. }
  264. static int allws(const char *cp)
  265. {
  266. while (*cp)
  267. if (!isspace(_UC(*cp++)))
  268. return 0;
  269. return 1;
  270. }
  271. static int test_tlsafile(SSL_CTX *ctx, const char *basename,
  272. BIO *f, const char *path)
  273. {
  274. char *line;
  275. int testno = 0;
  276. int ret = 1;
  277. SSL *ssl;
  278. while (ret > 0 && (line = read_to_eol(f)) != NULL) {
  279. STACK_OF(X509) *chain;
  280. int ntlsa;
  281. int ncert;
  282. int want;
  283. int want_depth;
  284. int off;
  285. int i;
  286. int ok;
  287. int err;
  288. int mdpth;
  289. if (*line == '\0' || *line == '#')
  290. continue;
  291. ++testno;
  292. if (sscanf(line, "%d %d %d %d%n", &ntlsa, &ncert, &want, &want_depth, &off) != 4
  293. || !allws(line + off)) {
  294. fprintf(stderr, "Expected tlsa count, cert count and result"
  295. " at test %d of %s\n", testno, path);
  296. return 0;
  297. }
  298. if ((ssl = SSL_new(ctx)) == NULL)
  299. return -1;
  300. SSL_set_connect_state(ssl);
  301. if (SSL_dane_enable(ssl, basename) <= 0) {
  302. SSL_free(ssl);
  303. return -1;
  304. }
  305. for (i = 0; i < ntlsa; ++i) {
  306. if ((line = read_to_eol(f)) == NULL || !tlsa_import_rr(ssl, line)) {
  307. SSL_free(ssl);
  308. return 0;
  309. }
  310. }
  311. /* Don't report old news */
  312. ERR_clear_error();
  313. chain = load_chain(f, ncert);
  314. if (chain == NULL) {
  315. SSL_free(ssl);
  316. return -1;
  317. }
  318. ok = verify_chain(ssl, chain);
  319. sk_X509_pop_free(chain, X509_free);
  320. err = SSL_get_verify_result(ssl);
  321. /*
  322. * Peek under the hood, normally TLSA match data is hidden when
  323. * verification fails, we can obtain any suppressed data by setting the
  324. * verification result to X509_V_OK before looking.
  325. */
  326. SSL_set_verify_result(ssl, X509_V_OK);
  327. mdpth = SSL_get0_dane_authority(ssl, NULL, NULL);
  328. /* Not needed any more, but lead by example and put the error back. */
  329. SSL_set_verify_result(ssl, err);
  330. SSL_free(ssl);
  331. if (ok < 0) {
  332. ret = 0;
  333. fprintf(stderr, "verify_chain internal error in %s test %d\n",
  334. path, testno);
  335. print_errors();
  336. continue;
  337. }
  338. if (err != want || (want == 0 && !ok)) {
  339. ret = 0;
  340. if (err != want) {
  341. if (want == X509_V_OK)
  342. fprintf(stderr, "Verification failure in %s test %d: %d: %s\n",
  343. path, testno, err, X509_verify_cert_error_string(err));
  344. else
  345. fprintf(stderr, "Unexpected error in %s test %d: %d: wanted %d\n",
  346. path, testno, err, want);
  347. } else {
  348. fprintf(stderr, "Verification failure in %s test %d: ok=0\n",
  349. path, testno);
  350. }
  351. print_errors();
  352. continue;
  353. }
  354. if (mdpth != want_depth) {
  355. ret = 0;
  356. fprintf(stderr, "Wrong match depth, in %s test %d: wanted %d, got: %d\n",
  357. path, testno, want_depth, mdpth);
  358. }
  359. fprintf(stderr, "%s: test %d successful\n", path, testno);
  360. }
  361. ERR_clear_error();
  362. return ret;
  363. }
  364. int main(int argc, char *argv[])
  365. {
  366. BIO *f;
  367. BIO *bio_err;
  368. SSL_CTX *ctx = NULL;
  369. const char *basedomain;
  370. const char *CAfile;
  371. const char *tlsafile;
  372. const char *p;
  373. int ret = 1;
  374. progname = argv[0];
  375. if (argc != 4) {
  376. test_usage();
  377. EXIT(ret);
  378. }
  379. basedomain = argv[1];
  380. CAfile = argv[2];
  381. tlsafile = argv[3];
  382. bio_err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
  383. p = getenv("OPENSSL_DEBUG_MEMORY");
  384. if (p != NULL && strcmp(p, "on") == 0)
  385. CRYPTO_set_mem_debug(1);
  386. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
  387. f = BIO_new_file(tlsafile, "r");
  388. if (f == NULL) {
  389. fprintf(stderr, "%s: Error opening tlsa record file: '%s': %s\n",
  390. progname, tlsafile, strerror(errno));
  391. EXIT(ret);
  392. }
  393. ctx = SSL_CTX_new(TLS_client_method());
  394. if (SSL_CTX_dane_enable(ctx) <= 0) {
  395. print_errors();
  396. goto end;
  397. }
  398. if (!SSL_CTX_load_verify_locations(ctx, CAfile, NULL)) {
  399. print_errors();
  400. goto end;
  401. }
  402. if ((SSL_CTX_dane_mtype_set(ctx, EVP_sha512(), 2, 1)) <= 0) {
  403. print_errors();
  404. goto end;
  405. }
  406. if ((SSL_CTX_dane_mtype_set(ctx, EVP_sha256(), 1, 2)) <= 0) {
  407. print_errors();
  408. goto end;
  409. }
  410. if (test_tlsafile(ctx, basedomain, f, tlsafile) <= 0) {
  411. print_errors();
  412. goto end;
  413. }
  414. ret = 0;
  415. end:
  416. BIO_free(f);
  417. SSL_CTX_free(ctx);
  418. #ifndef OPENSSL_NO_CRYPTO_MDEBUG
  419. if (CRYPTO_mem_leaks(bio_err) <= 0)
  420. ret = 1;
  421. #endif
  422. BIO_free(bio_err);
  423. EXIT(ret);
  424. }
  425. #include <internal/dane.h>
  426. static void store_ctx_dane_init(X509_STORE_CTX *store_ctx, SSL *ssl)
  427. {
  428. X509_STORE_CTX_set0_dane(store_ctx, SSL_get0_dane(ssl));
  429. }