danetest.c 11 KB

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