test_tortls.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /* Copyright (c) 2010-2018, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #define TORTLS_PRIVATE
  4. #define TOR_X509_PRIVATE
  5. #define LOG_PRIVATE
  6. #include "orconfig.h"
  7. #ifdef _WIN32
  8. #include <winsock2.h>
  9. #endif
  10. #include <math.h>
  11. #include <stddef.h>
  12. #include "lib/cc/compat_compiler.h"
  13. #include "core/or/or.h"
  14. #include "lib/log/log.h"
  15. #include "app/config/config.h"
  16. #include "lib/crypt_ops/compat_openssl.h"
  17. #include "lib/tls/x509.h"
  18. #include "lib/tls/x509_internal.h"
  19. #include "lib/tls/tortls.h"
  20. #include "lib/tls/tortls_st.h"
  21. #include "lib/tls/tortls_internal.h"
  22. #include "app/config/or_state_st.h"
  23. #include "test/test.h"
  24. #include "test/log_test_helpers.h"
  25. #include "tinytest.h"
  26. static void
  27. test_tortls_errno_to_tls_error(void *data)
  28. {
  29. (void) data;
  30. tt_int_op(tor_errno_to_tls_error(SOCK_ERRNO(ECONNRESET)),OP_EQ,
  31. TOR_TLS_ERROR_CONNRESET);
  32. tt_int_op(tor_errno_to_tls_error(SOCK_ERRNO(ETIMEDOUT)),OP_EQ,
  33. TOR_TLS_ERROR_TIMEOUT);
  34. tt_int_op(tor_errno_to_tls_error(SOCK_ERRNO(EHOSTUNREACH)),OP_EQ,
  35. TOR_TLS_ERROR_NO_ROUTE);
  36. tt_int_op(tor_errno_to_tls_error(SOCK_ERRNO(ENETUNREACH)),OP_EQ,
  37. TOR_TLS_ERROR_NO_ROUTE);
  38. tt_int_op(tor_errno_to_tls_error(SOCK_ERRNO(ECONNREFUSED)),OP_EQ,
  39. TOR_TLS_ERROR_CONNREFUSED);
  40. tt_int_op(tor_errno_to_tls_error(0),OP_EQ,TOR_TLS_ERROR_MISC);
  41. done:
  42. (void)1;
  43. }
  44. static void
  45. test_tortls_err_to_string(void *data)
  46. {
  47. (void) data;
  48. tt_str_op(tor_tls_err_to_string(1),OP_EQ,"[Not an error.]");
  49. tt_str_op(tor_tls_err_to_string(TOR_TLS_ERROR_MISC),OP_EQ,"misc error");
  50. tt_str_op(tor_tls_err_to_string(TOR_TLS_ERROR_IO),OP_EQ,"unexpected close");
  51. tt_str_op(tor_tls_err_to_string(TOR_TLS_ERROR_CONNREFUSED),OP_EQ,
  52. "connection refused");
  53. tt_str_op(tor_tls_err_to_string(TOR_TLS_ERROR_CONNRESET),OP_EQ,
  54. "connection reset");
  55. tt_str_op(tor_tls_err_to_string(TOR_TLS_ERROR_NO_ROUTE),OP_EQ,
  56. "host unreachable");
  57. tt_str_op(tor_tls_err_to_string(TOR_TLS_ERROR_TIMEOUT),OP_EQ,
  58. "connection timed out");
  59. tt_str_op(tor_tls_err_to_string(TOR_TLS_CLOSE),OP_EQ,"closed");
  60. tt_str_op(tor_tls_err_to_string(TOR_TLS_WANTREAD),OP_EQ,"want to read");
  61. tt_str_op(tor_tls_err_to_string(TOR_TLS_WANTWRITE),OP_EQ,"want to write");
  62. tt_str_op(tor_tls_err_to_string(-100),OP_EQ,"(unknown error code)");
  63. done:
  64. (void)1;
  65. }
  66. #ifdef ENABLE_OPENSSL
  67. static int
  68. mock_tls_cert_matches_key(const tor_tls_t *tls, const tor_x509_cert_t *cert)
  69. {
  70. (void) tls;
  71. (void) cert; // XXXX look at this.
  72. return 1;
  73. }
  74. static void
  75. test_tortls_tor_tls_get_error(void *data)
  76. {
  77. (void) data;
  78. MOCK(tor_tls_cert_matches_key, mock_tls_cert_matches_key);
  79. crypto_pk_t *key1 = NULL, *key2 = NULL;
  80. key1 = pk_generate(2);
  81. key2 = pk_generate(3);
  82. tor_tls_t *tls = NULL;
  83. tt_int_op(tor_tls_context_init(TOR_TLS_CTX_IS_PUBLIC_SERVER,
  84. key1, key2, 86400), OP_EQ, 0);
  85. tls = tor_tls_new(-1, 0);
  86. setup_capture_of_logs(LOG_WARN);
  87. tor_tls_get_error(tls, 0, 0,
  88. (const char *)"in unit test", LOG_WARN, LD_GENERAL);
  89. expect_single_log_msg_containing("unexpected close while in unit test");
  90. done:
  91. UNMOCK(tor_tls_cert_matches_key);
  92. NS_UNMOCK(logv);
  93. crypto_pk_free(key1);
  94. crypto_pk_free(key2);
  95. tor_tls_free(tls);
  96. }
  97. #endif
  98. static void
  99. test_tortls_x509_cert_get_id_digests(void *ignored)
  100. {
  101. (void)ignored;
  102. tor_x509_cert_t *cert;
  103. common_digests_t *d;
  104. const common_digests_t *res;
  105. cert = tor_malloc_zero(sizeof(tor_x509_cert_t));
  106. d = tor_malloc_zero(sizeof(common_digests_t));
  107. d->d[0][0] = 42;
  108. res = tor_x509_cert_get_id_digests(cert);
  109. tt_assert(!res);
  110. cert->pkey_digests_set = 1;
  111. cert->pkey_digests = *d;
  112. res = tor_x509_cert_get_id_digests(cert);
  113. tt_int_op(res->d[0][0], OP_EQ, 42);
  114. done:
  115. tor_free(cert);
  116. tor_free(d);
  117. }
  118. static void
  119. test_tortls_get_my_certs(void *ignored)
  120. {
  121. (void)ignored;
  122. int ret;
  123. tor_tls_context_t *ctx;
  124. const tor_x509_cert_t *link_cert_out = NULL;
  125. const tor_x509_cert_t *id_cert_out = NULL;
  126. ctx = tor_malloc_zero(sizeof(tor_tls_context_t));
  127. client_tls_context = NULL;
  128. ret = tor_tls_get_my_certs(0, NULL, NULL);
  129. tt_int_op(ret, OP_EQ, -1);
  130. server_tls_context = NULL;
  131. ret = tor_tls_get_my_certs(1, NULL, NULL);
  132. tt_int_op(ret, OP_EQ, -1);
  133. client_tls_context = ctx;
  134. ret = tor_tls_get_my_certs(0, NULL, NULL);
  135. tt_int_op(ret, OP_EQ, 0);
  136. client_tls_context = ctx;
  137. ret = tor_tls_get_my_certs(0, &link_cert_out, &id_cert_out);
  138. tt_int_op(ret, OP_EQ, 0);
  139. server_tls_context = ctx;
  140. ret = tor_tls_get_my_certs(1, &link_cert_out, &id_cert_out);
  141. tt_int_op(ret, OP_EQ, 0);
  142. done:
  143. (void)1;
  144. }
  145. #ifdef ENABLE_OPENSSL
  146. static void
  147. test_tortls_get_forced_write_size(void *ignored)
  148. {
  149. (void)ignored;
  150. long ret;
  151. tor_tls_t *tls;
  152. tls = tor_malloc_zero(sizeof(tor_tls_t));
  153. tls->wantwrite_n = 43;
  154. ret = tor_tls_get_forced_write_size(tls);
  155. tt_int_op(ret, OP_EQ, 43);
  156. done:
  157. tor_free(tls);
  158. }
  159. static void
  160. test_tortls_used_v1_handshake(void *ignored)
  161. {
  162. (void)ignored;
  163. int ret;
  164. tor_tls_t *tls;
  165. tls = tor_malloc_zero(sizeof(tor_tls_t));
  166. // These tests assume both V2 handshake server and client are enabled
  167. tls->wasV2Handshake = 0;
  168. ret = tor_tls_used_v1_handshake(tls);
  169. tt_int_op(ret, OP_EQ, 1);
  170. tls->wasV2Handshake = 1;
  171. ret = tor_tls_used_v1_handshake(tls);
  172. tt_int_op(ret, OP_EQ, 0);
  173. done:
  174. tor_free(tls);
  175. }
  176. static void
  177. test_tortls_server_got_renegotiate(void *ignored)
  178. {
  179. (void)ignored;
  180. int ret;
  181. tor_tls_t *tls;
  182. tls = tor_malloc_zero(sizeof(tor_tls_t));
  183. tls->got_renegotiate = 1;
  184. ret = tor_tls_server_got_renegotiate(tls);
  185. tt_int_op(ret, OP_EQ, 1);
  186. done:
  187. tor_free(tls);
  188. }
  189. #endif
  190. static void
  191. test_tortls_evaluate_ecgroup_for_tls(void *ignored)
  192. {
  193. (void)ignored;
  194. int ret;
  195. ret = evaluate_ecgroup_for_tls(NULL);
  196. tt_int_op(ret, OP_EQ, 1);
  197. ret = evaluate_ecgroup_for_tls("foobar");
  198. tt_int_op(ret, OP_EQ, 0);
  199. ret = evaluate_ecgroup_for_tls("P256");
  200. tt_int_op(ret, OP_EQ, 1);
  201. ret = evaluate_ecgroup_for_tls("P224");
  202. // tt_int_op(ret, OP_EQ, 1); This varies between machines
  203. tt_assert(ret == 0 || ret == 1);
  204. done:
  205. (void)0;
  206. }
  207. static void
  208. test_tortls_double_init(void *arg)
  209. {
  210. (void) arg;
  211. /* If we call tor_tls_context_init() a second time, nothing should go
  212. * wrong.
  213. */
  214. crypto_pk_t *pk1 = NULL, *pk2 = NULL;
  215. pk1 = pk_generate(2);
  216. pk2 = pk_generate(0);
  217. int r = tor_tls_context_init(TOR_TLS_CTX_IS_PUBLIC_SERVER,
  218. pk1, pk2, 86400);
  219. tt_int_op(r, OP_EQ, 0);
  220. r = tor_tls_context_init(TOR_TLS_CTX_IS_PUBLIC_SERVER,
  221. pk2, pk1, 86400);
  222. tt_int_op(r, OP_EQ, 0);
  223. /* For a public server context, these are the same */
  224. tt_ptr_op(tor_tls_context_get(0), OP_EQ, tor_tls_context_get(1));
  225. done:
  226. crypto_pk_free(pk1);
  227. crypto_pk_free(pk2);
  228. }
  229. static void
  230. test_tortls_bridge_init(void *arg)
  231. {
  232. (void)arg;
  233. crypto_pk_t *pk1 = NULL, *pk2 = NULL;
  234. pk1 = pk_generate(2);
  235. pk2 = pk_generate(0);
  236. /* If we pass in a server identity key but not the
  237. TOR_TLS_CTX_IS_PUBLIC_SERVER flag, we should get a bridge-style
  238. configuration, with two distinct contexts. */
  239. int r = tor_tls_context_init(0 /* flags */, pk1, pk2, 86400);
  240. tt_int_op(r, OP_EQ, 0);
  241. tt_ptr_op(tor_tls_context_get(0), OP_NE, tor_tls_context_get(1));
  242. done:
  243. crypto_pk_free(pk1);
  244. crypto_pk_free(pk2);
  245. }
  246. static void
  247. test_tortls_address(void *arg)
  248. {
  249. (void)arg;
  250. tor_tls_t *tls = NULL;
  251. crypto_pk_t *pk1=NULL, *pk2=NULL;
  252. pk1 = pk_generate(2);
  253. pk2 = pk_generate(0);
  254. int r = tor_tls_context_init(TOR_TLS_CTX_IS_PUBLIC_SERVER,
  255. pk1, pk2, 86400);
  256. tt_int_op(r, OP_EQ, 0);
  257. tls = tor_tls_new(-1, 0);
  258. tls->state = TOR_TLS_ST_OPEN;
  259. tor_tls_set_logged_address(tls, "zombo.com");
  260. /* This write should fail, since the fd is -1. */
  261. setup_capture_of_logs(LOG_INFO);
  262. int n = tor_tls_write(tls, "welcome", 7);
  263. tt_int_op(n, OP_LT, 0);
  264. expect_log_msg_containing("with zombo.com");
  265. done:
  266. teardown_capture_of_logs();
  267. tor_tls_free(tls);
  268. crypto_pk_free(pk1);
  269. crypto_pk_free(pk2);
  270. }
  271. static void
  272. test_tortls_is_server(void *arg)
  273. {
  274. (void)arg;
  275. crypto_pk_t *pk1=NULL, *pk2=NULL;
  276. tor_tls_t *tls1=NULL, *tls2=NULL;
  277. pk1 = pk_generate(2);
  278. pk2 = pk_generate(0);
  279. int r = tor_tls_context_init(TOR_TLS_CTX_IS_PUBLIC_SERVER,
  280. pk1, pk2, 86400);
  281. tt_int_op(r, OP_EQ, 0);
  282. tls1 = tor_tls_new(-1, 0);
  283. tls2 = tor_tls_new(-1, 1);
  284. tt_assert(! tor_tls_is_server(tls1));
  285. tt_assert(tor_tls_is_server(tls2));
  286. done:
  287. tor_tls_free(tls1);
  288. tor_tls_free(tls2);
  289. crypto_pk_free(pk1);
  290. crypto_pk_free(pk2);
  291. }
  292. #define LOCAL_TEST_CASE(name, flags) \
  293. { #name, test_tortls_##name, (flags|TT_FORK), NULL, NULL }
  294. struct testcase_t tortls_tests[] = {
  295. LOCAL_TEST_CASE(errno_to_tls_error, 0),
  296. LOCAL_TEST_CASE(err_to_string, 0),
  297. LOCAL_TEST_CASE(x509_cert_get_id_digests, 0),
  298. LOCAL_TEST_CASE(get_my_certs, TT_FORK),
  299. #ifdef ENABLE_OPENSSL
  300. LOCAL_TEST_CASE(tor_tls_get_error, 0),
  301. LOCAL_TEST_CASE(get_forced_write_size, 0),
  302. LOCAL_TEST_CASE(used_v1_handshake, TT_FORK),
  303. LOCAL_TEST_CASE(server_got_renegotiate, 0),
  304. #endif
  305. LOCAL_TEST_CASE(evaluate_ecgroup_for_tls, 0),
  306. LOCAL_TEST_CASE(double_init, TT_FORK),
  307. LOCAL_TEST_CASE(address, TT_FORK),
  308. LOCAL_TEST_CASE(is_server, 0),
  309. LOCAL_TEST_CASE(bridge_init, TT_FORK),
  310. END_OF_TESTCASES
  311. };