2
0

danetest.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /*
  2. * Copyright 2015-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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 "internal/nelem.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. name = header = NULL;
  115. data = NULL;
  116. }
  117. if (count == nelem) {
  118. ERR_clear_error();
  119. return chain;
  120. }
  121. err:
  122. OPENSSL_free(name);
  123. OPENSSL_free(header);
  124. OPENSSL_free(data);
  125. sk_X509_pop_free(chain, X509_free);
  126. return NULL;
  127. }
  128. static char *read_to_eol(BIO *f)
  129. {
  130. static char buf[1024];
  131. int n;
  132. if (!BIO_gets(f, buf, sizeof(buf)))
  133. return NULL;
  134. n = strlen(buf);
  135. if (buf[n - 1] != '\n') {
  136. if (n + 1 == sizeof(buf))
  137. TEST_error("input too long");
  138. else
  139. TEST_error("EOF before newline");
  140. return NULL;
  141. }
  142. /* Trim trailing whitespace */
  143. while (n > 0 && isspace(_UC(buf[n - 1])))
  144. buf[--n] = '\0';
  145. return buf;
  146. }
  147. /*
  148. * Hex decoder that tolerates optional whitespace
  149. */
  150. static ossl_ssize_t hexdecode(const char *in, void *result)
  151. {
  152. unsigned char **out = (unsigned char **)result;
  153. unsigned char *ret;
  154. unsigned char *cp;
  155. uint8_t byte;
  156. int nibble = 0;
  157. if (!TEST_ptr(ret = OPENSSL_malloc(strlen(in) / 2)))
  158. return -1;
  159. cp = ret;
  160. for (byte = 0; *in; ++in) {
  161. int x;
  162. if (isspace(_UC(*in)))
  163. continue;
  164. x = OPENSSL_hexchar2int(*in);
  165. if (x < 0) {
  166. OPENSSL_free(ret);
  167. return 0;
  168. }
  169. byte |= (char)x;
  170. if ((nibble ^= 1) == 0) {
  171. *cp++ = byte;
  172. byte = 0;
  173. } else {
  174. byte <<= 4;
  175. }
  176. }
  177. if (nibble != 0) {
  178. OPENSSL_free(ret);
  179. return 0;
  180. }
  181. return cp - (*out = ret);
  182. }
  183. static ossl_ssize_t checked_uint8(const char *in, void *out)
  184. {
  185. uint8_t *result = (uint8_t *)out;
  186. const char *cp = in;
  187. char *endp;
  188. long v;
  189. int e;
  190. save_errno();
  191. v = strtol(cp, &endp, 10);
  192. e = restore_errno();
  193. if (((v == LONG_MIN || v == LONG_MAX) && e == ERANGE) ||
  194. endp == cp || !isspace(_UC(*endp)) ||
  195. v != (*(uint8_t *)result = (uint8_t) v)) {
  196. return -1;
  197. }
  198. for (cp = endp; isspace(_UC(*cp)); ++cp)
  199. continue;
  200. return cp - in;
  201. }
  202. struct tlsa_field {
  203. void *var;
  204. const char *name;
  205. ossl_ssize_t (*parser)(const char *, void *);
  206. };
  207. static int tlsa_import_rr(SSL *ssl, const char *rrdata)
  208. {
  209. static uint8_t usage;
  210. static uint8_t selector;
  211. static uint8_t mtype;
  212. static unsigned char *data = NULL;
  213. static struct tlsa_field tlsa_fields[] = {
  214. { &usage, "usage", checked_uint8 },
  215. { &selector, "selector", checked_uint8 },
  216. { &mtype, "mtype", checked_uint8 },
  217. { &data, "data", hexdecode },
  218. { NULL, }
  219. };
  220. int ret;
  221. struct tlsa_field *f;
  222. const char *cp = rrdata;
  223. ossl_ssize_t len = 0;
  224. for (f = tlsa_fields; f->var; ++f) {
  225. if ((len = f->parser(cp += len, f->var)) <= 0) {
  226. TEST_info("bad TLSA %s field in: %s", f->name, rrdata);
  227. return 0;
  228. }
  229. }
  230. ret = SSL_dane_tlsa_add(ssl, usage, selector, mtype, data, len);
  231. OPENSSL_free(data);
  232. if (ret == 0) {
  233. TEST_info("unusable TLSA rrdata: %s", rrdata);
  234. return 0;
  235. }
  236. if (ret < 0) {
  237. TEST_info("error loading TLSA rrdata: %s", rrdata);
  238. return 0;
  239. }
  240. return ret;
  241. }
  242. static int allws(const char *cp)
  243. {
  244. while (*cp)
  245. if (!isspace(_UC(*cp++)))
  246. return 0;
  247. return 1;
  248. }
  249. static int test_tlsafile(SSL_CTX *ctx, const char *base_name,
  250. BIO *f, const char *path)
  251. {
  252. char *line;
  253. int testno = 0;
  254. int ret = 1;
  255. SSL *ssl;
  256. while (ret > 0 && (line = read_to_eol(f)) != NULL) {
  257. STACK_OF(X509) *chain;
  258. int ntlsa;
  259. int ncert;
  260. int noncheck;
  261. int want;
  262. int want_depth;
  263. int off;
  264. int i;
  265. int ok;
  266. int err;
  267. int mdpth;
  268. if (*line == '\0' || *line == '#')
  269. continue;
  270. ++testno;
  271. if (sscanf(line, "%d %d %d %d %d%n",
  272. &ntlsa, &ncert, &noncheck, &want, &want_depth, &off) != 5
  273. || !allws(line + off)) {
  274. TEST_error("Malformed line for test %d", testno);
  275. return 0;
  276. }
  277. if (!TEST_ptr(ssl = SSL_new(ctx)))
  278. return 0;
  279. SSL_set_connect_state(ssl);
  280. if (SSL_dane_enable(ssl, base_name) <= 0) {
  281. SSL_free(ssl);
  282. return 0;
  283. }
  284. if (noncheck)
  285. SSL_dane_set_flags(ssl, DANE_FLAG_NO_DANE_EE_NAMECHECKS);
  286. for (i = 0; i < ntlsa; ++i) {
  287. if ((line = read_to_eol(f)) == NULL || !tlsa_import_rr(ssl, line)) {
  288. SSL_free(ssl);
  289. return 0;
  290. }
  291. }
  292. /* Don't report old news */
  293. ERR_clear_error();
  294. if (!TEST_ptr(chain = load_chain(f, ncert))) {
  295. SSL_free(ssl);
  296. return 0;
  297. }
  298. ok = verify_chain(ssl, chain);
  299. sk_X509_pop_free(chain, X509_free);
  300. err = SSL_get_verify_result(ssl);
  301. /*
  302. * Peek under the hood, normally TLSA match data is hidden when
  303. * verification fails, we can obtain any suppressed data by setting the
  304. * verification result to X509_V_OK before looking.
  305. */
  306. SSL_set_verify_result(ssl, X509_V_OK);
  307. mdpth = SSL_get0_dane_authority(ssl, NULL, NULL);
  308. /* Not needed any more, but lead by example and put the error back. */
  309. SSL_set_verify_result(ssl, err);
  310. SSL_free(ssl);
  311. if (!TEST_int_eq(err, want)) {
  312. if (want == X509_V_OK)
  313. TEST_info("Verification failure in test %d: %d=%s",
  314. testno, err, X509_verify_cert_error_string(err));
  315. else
  316. TEST_info("Unexpected error in test %d", testno);
  317. ret = 0;
  318. continue;
  319. }
  320. if (!TEST_false(want == 0 && ok == 0)) {
  321. TEST_info("Verification failure in test %d: ok=0", testno);
  322. ret = 0;
  323. continue;
  324. }
  325. if (!TEST_int_eq(mdpth, want_depth)) {
  326. TEST_info("In test test %d", testno);
  327. ret = 0;
  328. }
  329. }
  330. ERR_clear_error();
  331. return ret;
  332. }
  333. static int run_tlsatest(void)
  334. {
  335. SSL_CTX *ctx = NULL;
  336. BIO *f = NULL;
  337. int ret = 0;
  338. if (!TEST_ptr(f = BIO_new_file(tlsafile, "r"))
  339. || !TEST_ptr(ctx = SSL_CTX_new(TLS_client_method()))
  340. || !TEST_int_gt(SSL_CTX_dane_enable(ctx), 0)
  341. || !TEST_true(SSL_CTX_load_verify_file(ctx, CAfile))
  342. || !TEST_int_gt(SSL_CTX_dane_mtype_set(ctx, EVP_sha512(), 2, 1),
  343. 0)
  344. || !TEST_int_gt(SSL_CTX_dane_mtype_set(ctx, EVP_sha256(), 1, 2),
  345. 0)
  346. || !TEST_int_gt(test_tlsafile(ctx, basedomain, f, tlsafile), 0))
  347. goto end;
  348. ret = 1;
  349. end:
  350. BIO_free(f);
  351. SSL_CTX_free(ctx);
  352. return ret;
  353. }
  354. OPT_TEST_DECLARE_USAGE("basedomain CAfile tlsafile\n")
  355. int setup_tests(void)
  356. {
  357. if (!test_skip_common_options()) {
  358. TEST_error("Error parsing test options\n");
  359. return 0;
  360. }
  361. if (!TEST_ptr(basedomain = test_get_argument(0))
  362. || !TEST_ptr(CAfile = test_get_argument(1))
  363. || !TEST_ptr(tlsafile = test_get_argument(2)))
  364. return 0;
  365. ADD_TEST(run_tlsatest);
  366. return 1;
  367. }
  368. #include "internal/dane.h"
  369. static void store_ctx_dane_init(X509_STORE_CTX *store_ctx, SSL *ssl)
  370. {
  371. X509_STORE_CTX_set0_dane(store_ctx, SSL_get0_dane(ssl));
  372. }