2
0

danetest.c 11 KB

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