danetest.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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 "testutil.h"
  24. #include "e_os.h"
  25. #define _UC(c) ((unsigned char)(c))
  26. static const char *basedomain;
  27. static const char *CAfile;
  28. static const char *tlsafile;
  29. /*
  30. * Forward declaration, of function that uses internal interfaces, from headers
  31. * included at the end of this module.
  32. */
  33. static void store_ctx_dane_init(X509_STORE_CTX *, SSL *);
  34. static int saved_errno;
  35. static void save_errno(void)
  36. {
  37. saved_errno = errno;
  38. }
  39. static int restore_errno(void)
  40. {
  41. int ret = errno;
  42. errno = saved_errno;
  43. return ret;
  44. }
  45. static int verify_chain(SSL *ssl, STACK_OF(X509) *chain)
  46. {
  47. X509_STORE_CTX *store_ctx = NULL;
  48. SSL_CTX *ssl_ctx = NULL;
  49. X509_STORE *store = NULL;
  50. X509 *cert = NULL;
  51. int ret = 0;
  52. int store_ctx_idx = SSL_get_ex_data_X509_STORE_CTX_idx();
  53. if (!TEST_ptr(store_ctx = X509_STORE_CTX_new())
  54. || !TEST_ptr(ssl_ctx = SSL_get_SSL_CTX(ssl))
  55. || !TEST_ptr(store = SSL_CTX_get_cert_store(ssl_ctx))
  56. || !TEST_ptr(cert = sk_X509_value(chain, 0))
  57. || !TEST_true(X509_STORE_CTX_init(store_ctx, store, cert, chain))
  58. || !TEST_true(X509_STORE_CTX_set_ex_data(store_ctx, store_ctx_idx,
  59. ssl)))
  60. goto end;
  61. X509_STORE_CTX_set_default(store_ctx,
  62. SSL_is_server(ssl) ? "ssl_client" : "ssl_server");
  63. X509_VERIFY_PARAM_set1(X509_STORE_CTX_get0_param(store_ctx),
  64. SSL_get0_param(ssl));
  65. store_ctx_dane_init(store_ctx, ssl);
  66. if (SSL_get_verify_callback(ssl) != NULL)
  67. X509_STORE_CTX_set_verify_cb(store_ctx, SSL_get_verify_callback(ssl));
  68. /* Mask "internal failures" (-1) from our return value. */
  69. if (!TEST_int_ge(ret = X509_verify_cert(store_ctx), 0))
  70. ret = 0;
  71. SSL_set_verify_result(ssl, X509_STORE_CTX_get_error(store_ctx));
  72. X509_STORE_CTX_cleanup(store_ctx);
  73. end:
  74. X509_STORE_CTX_free(store_ctx);
  75. return ret;
  76. }
  77. static STACK_OF(X509) *load_chain(BIO *fp, int nelem)
  78. {
  79. int count;
  80. char *name = 0;
  81. char *header = 0;
  82. unsigned char *data = 0;
  83. long len;
  84. char *errtype = 0; /* if error: cert or pkey? */
  85. STACK_OF(X509) *chain;
  86. typedef X509 *(*d2i_X509_t)(X509 **, const unsigned char **, long);
  87. if (!TEST_ptr(chain = sk_X509_new_null()))
  88. goto err;
  89. for (count = 0;
  90. count < nelem && errtype == 0
  91. && PEM_read_bio(fp, &name, &header, &data, &len) == 1;
  92. ++count) {
  93. if (strcmp(name, PEM_STRING_X509) == 0
  94. || strcmp(name, PEM_STRING_X509_TRUSTED) == 0
  95. || strcmp(name, PEM_STRING_X509_OLD) == 0) {
  96. d2i_X509_t d = strcmp(name, PEM_STRING_X509_TRUSTED) != 0
  97. ? d2i_X509_AUX : d2i_X509;
  98. X509 *cert;
  99. const unsigned char *p = data;
  100. if (!TEST_ptr(cert = d(0, &p, len))
  101. || !TEST_long_eq(p - data, len)) {
  102. TEST_info("Certificate parsing error");
  103. goto err;
  104. }
  105. if (!TEST_true(sk_X509_push(chain, cert)))
  106. goto err;
  107. } else {
  108. TEST_info("Unknown chain file object %s", name);
  109. goto err;
  110. }
  111. OPENSSL_free(name);
  112. OPENSSL_free(header);
  113. OPENSSL_free(data);
  114. }
  115. if (count == nelem) {
  116. ERR_clear_error();
  117. return chain;
  118. }
  119. err:
  120. OPENSSL_free(name);
  121. OPENSSL_free(header);
  122. OPENSSL_free(data);
  123. sk_X509_pop_free(chain, X509_free);
  124. return NULL;
  125. }
  126. static char *read_to_eol(BIO *f)
  127. {
  128. static char buf[1024];
  129. int n;
  130. if (!BIO_gets(f, buf, sizeof(buf)))
  131. return NULL;
  132. n = strlen(buf);
  133. if (buf[n - 1] != '\n') {
  134. if (n + 1 == sizeof(buf))
  135. TEST_error("input too long");
  136. else
  137. TEST_error("EOF before newline");
  138. return NULL;
  139. }
  140. /* Trim trailing whitespace */
  141. while (n > 0 && isspace(_UC(buf[n - 1])))
  142. buf[--n] = '\0';
  143. return buf;
  144. }
  145. /*
  146. * Hex decoder that tolerates optional whitespace
  147. */
  148. static ossl_ssize_t hexdecode(const char *in, void *result)
  149. {
  150. unsigned char **out = (unsigned char **)result;
  151. unsigned char *ret;
  152. unsigned char *cp;
  153. uint8_t byte;
  154. int nibble = 0;
  155. if (!TEST_ptr(ret = OPENSSL_malloc(strlen(in) / 2)))
  156. return -1;
  157. cp = ret;
  158. for (byte = 0; *in; ++in) {
  159. int x;
  160. if (isspace(_UC(*in)))
  161. continue;
  162. x = OPENSSL_hexchar2int(*in);
  163. if (x < 0) {
  164. OPENSSL_free(ret);
  165. return 0;
  166. }
  167. byte |= (char)x;
  168. if ((nibble ^= 1) == 0) {
  169. *cp++ = byte;
  170. byte = 0;
  171. } else {
  172. byte <<= 4;
  173. }
  174. }
  175. if (nibble != 0) {
  176. OPENSSL_free(ret);
  177. return 0;
  178. }
  179. return cp - (*out = ret);
  180. }
  181. static ossl_ssize_t checked_uint8(const char *in, void *out)
  182. {
  183. uint8_t *result = (uint8_t *)out;
  184. const char *cp = in;
  185. char *endp;
  186. long v;
  187. int e;
  188. save_errno();
  189. v = strtol(cp, &endp, 10);
  190. e = restore_errno();
  191. if (((v == LONG_MIN || v == LONG_MAX) && e == ERANGE) ||
  192. endp == cp || !isspace(_UC(*endp)) ||
  193. v != (*(uint8_t *)result = (uint8_t) v)) {
  194. return -1;
  195. }
  196. for (cp = endp; isspace(_UC(*cp)); ++cp)
  197. continue;
  198. return cp - in;
  199. }
  200. struct tlsa_field {
  201. void *var;
  202. const char *name;
  203. ossl_ssize_t (*parser)(const char *, void *);
  204. };
  205. static int tlsa_import_rr(SSL *ssl, const char *rrdata)
  206. {
  207. static uint8_t usage;
  208. static uint8_t selector;
  209. static uint8_t mtype;
  210. static unsigned char *data = NULL;
  211. static struct tlsa_field tlsa_fields[] = {
  212. { &usage, "usage", checked_uint8 },
  213. { &selector, "selector", checked_uint8 },
  214. { &mtype, "mtype", checked_uint8 },
  215. { &data, "data", hexdecode },
  216. { NULL, }
  217. };
  218. int ret;
  219. struct tlsa_field *f;
  220. const char *cp = rrdata;
  221. ossl_ssize_t len = 0;
  222. for (f = tlsa_fields; f->var; ++f) {
  223. if ((len = f->parser(cp += len, f->var)) <= 0) {
  224. TEST_info("bad TLSA %s field in: %s", f->name, rrdata);
  225. return 0;
  226. }
  227. }
  228. ret = SSL_dane_tlsa_add(ssl, usage, selector, mtype, data, len);
  229. OPENSSL_free(data);
  230. if (ret == 0) {
  231. TEST_info("unusable TLSA rrdata: %s", rrdata);
  232. return 0;
  233. }
  234. if (ret < 0) {
  235. TEST_info("error loading TLSA rrdata: %s", rrdata);
  236. return 0;
  237. }
  238. return ret;
  239. }
  240. static int allws(const char *cp)
  241. {
  242. while (*cp)
  243. if (!isspace(_UC(*cp++)))
  244. return 0;
  245. return 1;
  246. }
  247. static int test_tlsafile(SSL_CTX *ctx, const char *base_name,
  248. BIO *f, const char *path)
  249. {
  250. char *line;
  251. int testno = 0;
  252. int ret = 1;
  253. SSL *ssl;
  254. while (ret > 0 && (line = read_to_eol(f)) != NULL) {
  255. STACK_OF(X509) *chain;
  256. int ntlsa;
  257. int ncert;
  258. int noncheck;
  259. int want;
  260. int want_depth;
  261. int off;
  262. int i;
  263. int ok;
  264. int err;
  265. int mdpth;
  266. if (*line == '\0' || *line == '#')
  267. continue;
  268. ++testno;
  269. if (sscanf(line, "%d %d %d %d %d%n",
  270. &ntlsa, &ncert, &noncheck, &want, &want_depth, &off) != 5
  271. || !allws(line + off)) {
  272. TEST_error("Malformed line for test %d", testno);
  273. return 0;
  274. }
  275. if (!TEST_ptr(ssl = SSL_new(ctx)))
  276. return 0;
  277. SSL_set_connect_state(ssl);
  278. if (SSL_dane_enable(ssl, base_name) <= 0) {
  279. SSL_free(ssl);
  280. return 0;
  281. }
  282. if (noncheck)
  283. SSL_dane_set_flags(ssl, DANE_FLAG_NO_DANE_EE_NAMECHECKS);
  284. for (i = 0; i < ntlsa; ++i) {
  285. if ((line = read_to_eol(f)) == NULL || !tlsa_import_rr(ssl, line)) {
  286. SSL_free(ssl);
  287. return 0;
  288. }
  289. }
  290. /* Don't report old news */
  291. ERR_clear_error();
  292. if (!TEST_ptr(chain = load_chain(f, ncert))) {
  293. SSL_free(ssl);
  294. return 0;
  295. }
  296. ok = verify_chain(ssl, chain);
  297. sk_X509_pop_free(chain, X509_free);
  298. err = SSL_get_verify_result(ssl);
  299. /*
  300. * Peek under the hood, normally TLSA match data is hidden when
  301. * verification fails, we can obtain any suppressed data by setting the
  302. * verification result to X509_V_OK before looking.
  303. */
  304. SSL_set_verify_result(ssl, X509_V_OK);
  305. mdpth = SSL_get0_dane_authority(ssl, NULL, NULL);
  306. /* Not needed any more, but lead by example and put the error back. */
  307. SSL_set_verify_result(ssl, err);
  308. SSL_free(ssl);
  309. if (!TEST_int_eq(err, want)) {
  310. if (want == X509_V_OK)
  311. TEST_info("Verification failure in test %d: %d=%s",
  312. testno, err, X509_verify_cert_error_string(err));
  313. else
  314. TEST_info("Unexpected error in test %d", testno);
  315. ret = 0;
  316. continue;
  317. }
  318. if (!TEST_false(want == 0 && ok == 0)) {
  319. TEST_info("Verification failure in test %d: ok=0", testno);
  320. ret = 0;
  321. continue;
  322. }
  323. if (!TEST_int_eq(mdpth, want_depth)) {
  324. TEST_info("In test test %d", testno);
  325. ret = 0;
  326. }
  327. }
  328. ERR_clear_error();
  329. return ret;
  330. }
  331. static int run_tlsatest()
  332. {
  333. SSL_CTX *ctx = NULL;
  334. BIO *f = NULL;
  335. int ret = 0;
  336. if (!TEST_ptr(f = BIO_new_file(tlsafile, "r"))
  337. || !TEST_ptr(ctx = SSL_CTX_new(TLS_client_method()))
  338. || !TEST_int_gt(SSL_CTX_dane_enable(ctx), 0)
  339. || !TEST_true(SSL_CTX_load_verify_locations(ctx, CAfile, NULL))
  340. || !TEST_int_gt(SSL_CTX_dane_mtype_set(ctx, EVP_sha512(), 2, 1),
  341. 0)
  342. || !TEST_int_gt(SSL_CTX_dane_mtype_set(ctx, EVP_sha256(), 1, 2),
  343. 0)
  344. || !TEST_int_gt(test_tlsafile(ctx, basedomain, f, tlsafile), 0))
  345. goto end;
  346. ret = 1;
  347. end:
  348. BIO_free(f);
  349. SSL_CTX_free(ctx);
  350. return ret;
  351. }
  352. int test_main(int argc, char *argv[])
  353. {
  354. int ret = 0;
  355. if (argc != 4) {
  356. TEST_error("Usage error: danetest basedomain CAfile tlsafile");
  357. return 0;
  358. }
  359. basedomain = argv[1];
  360. CAfile = argv[2];
  361. tlsafile = argv[3];
  362. ADD_TEST(run_tlsatest);
  363. ret = run_tests(argv[0]);
  364. return ret;
  365. }
  366. #include <internal/dane.h>
  367. static void store_ctx_dane_init(X509_STORE_CTX *store_ctx, SSL *ssl)
  368. {
  369. X509_STORE_CTX_set0_dane(store_ctx, SSL_get0_dane(ssl));
  370. }