test_extorport.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. /* Copyright (c) 2013, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #define CONNECTION_PRIVATE
  4. #define EXT_ORPORT_PRIVATE
  5. #define MAIN_PRIVATE
  6. #include "or.h"
  7. #include "buffers.h"
  8. #include "connection.h"
  9. #include "config.h"
  10. #include "control.h"
  11. #include "ext_orport.h"
  12. #include "main.h"
  13. #include "test.h"
  14. /* Test connection_or_remove_from_ext_or_id_map and
  15. * connection_or_set_ext_or_identifier */
  16. static void
  17. test_ext_or_id_map(void *arg)
  18. {
  19. or_connection_t *c1 = NULL, *c2 = NULL, *c3 = NULL;
  20. char *idp = NULL, *idp2 = NULL;
  21. (void)arg;
  22. /* pre-initialization */
  23. tt_ptr_op(NULL, ==, connection_or_get_by_ext_or_id("xxxxxxxxxxxxxxxxxxxx"));
  24. c1 = or_connection_new(CONN_TYPE_EXT_OR, AF_INET);
  25. c2 = or_connection_new(CONN_TYPE_EXT_OR, AF_INET);
  26. c3 = or_connection_new(CONN_TYPE_OR, AF_INET);
  27. tt_ptr_op(c1->ext_or_conn_id, !=, NULL);
  28. tt_ptr_op(c2->ext_or_conn_id, !=, NULL);
  29. tt_ptr_op(c3->ext_or_conn_id, ==, NULL);
  30. tt_ptr_op(c1, ==, connection_or_get_by_ext_or_id(c1->ext_or_conn_id));
  31. tt_ptr_op(c2, ==, connection_or_get_by_ext_or_id(c2->ext_or_conn_id));
  32. tt_ptr_op(NULL, ==, connection_or_get_by_ext_or_id("xxxxxxxxxxxxxxxxxxxx"));
  33. idp = tor_memdup(c2->ext_or_conn_id, EXT_OR_CONN_ID_LEN);
  34. /* Give c2 a new ID. */
  35. connection_or_set_ext_or_identifier(c2);
  36. test_mem_op(idp, !=, c2->ext_or_conn_id, EXT_OR_CONN_ID_LEN);
  37. idp2 = tor_memdup(c2->ext_or_conn_id, EXT_OR_CONN_ID_LEN);
  38. tt_assert(!tor_digest_is_zero(idp2));
  39. tt_ptr_op(NULL, ==, connection_or_get_by_ext_or_id(idp));
  40. tt_ptr_op(c2, ==, connection_or_get_by_ext_or_id(idp2));
  41. /* Now remove it. */
  42. connection_or_remove_from_ext_or_id_map(c2);
  43. tt_ptr_op(NULL, ==, connection_or_get_by_ext_or_id(idp));
  44. tt_ptr_op(NULL, ==, connection_or_get_by_ext_or_id(idp2));
  45. done:
  46. if (c1)
  47. connection_free_(TO_CONN(c1));
  48. if (c2)
  49. connection_free_(TO_CONN(c2));
  50. if (c3)
  51. connection_free_(TO_CONN(c3));
  52. tor_free(idp);
  53. tor_free(idp2);
  54. connection_or_clear_ext_or_id_map();
  55. }
  56. /* Simple connection_write_to_buf_impl_ replacement that unconditionally
  57. * writes to outbuf. */
  58. static void
  59. connection_write_to_buf_impl_replacement(const char *string, size_t len,
  60. connection_t *conn, int zlib)
  61. {
  62. (void) zlib;
  63. tor_assert(string);
  64. tor_assert(conn);
  65. write_to_buf(string, len, conn->outbuf);
  66. }
  67. static char *
  68. buf_get_contents(buf_t *buf, size_t *sz_out)
  69. {
  70. char *out;
  71. *sz_out = buf_datalen(buf);
  72. if (*sz_out >= ULONG_MAX)
  73. return NULL; /* C'mon, really? */
  74. out = tor_malloc(*sz_out + 1);
  75. if (fetch_from_buf(out, (unsigned long)*sz_out, buf) != 0) {
  76. tor_free(out);
  77. return NULL;
  78. }
  79. out[*sz_out] = '\0'; /* Hopefully gratuitous. */
  80. return out;
  81. }
  82. static void
  83. test_ext_or_write_command(void *arg)
  84. {
  85. or_connection_t *c1;
  86. char *cp = NULL;
  87. char *buf = NULL;
  88. size_t sz;
  89. (void) arg;
  90. MOCK(connection_write_to_buf_impl_,
  91. connection_write_to_buf_impl_replacement);
  92. c1 = or_connection_new(CONN_TYPE_EXT_OR, AF_INET);
  93. tt_assert(c1);
  94. /* Length too long */
  95. tt_int_op(connection_write_ext_or_command(TO_CONN(c1), 100, "X", 100000),
  96. <, 0);
  97. /* Empty command */
  98. tt_int_op(connection_write_ext_or_command(TO_CONN(c1), 0x99, NULL, 0),
  99. ==, 0);
  100. cp = buf_get_contents(TO_CONN(c1)->outbuf, &sz);
  101. tt_int_op(sz, ==, 4);
  102. test_mem_op(cp, ==, "\x00\x99\x00\x00", 4);
  103. tor_free(cp);
  104. /* Medium command. */
  105. tt_int_op(connection_write_ext_or_command(TO_CONN(c1), 0x99,
  106. "Wai\0Hello", 9), ==, 0);
  107. cp = buf_get_contents(TO_CONN(c1)->outbuf, &sz);
  108. tt_int_op(sz, ==, 13);
  109. test_mem_op(cp, ==, "\x00\x99\x00\x09Wai\x00Hello", 13);
  110. tor_free(cp);
  111. /* Long command */
  112. buf = tor_malloc(65535);
  113. memset(buf, 'x', 65535);
  114. tt_int_op(connection_write_ext_or_command(TO_CONN(c1), 0xf00d,
  115. buf, 65535), ==, 0);
  116. cp = buf_get_contents(TO_CONN(c1)->outbuf, &sz);
  117. tt_int_op(sz, ==, 65539);
  118. test_mem_op(cp, ==, "\xf0\x0d\xff\xff", 4);
  119. test_mem_op(cp+4, ==, buf, 65535);
  120. tor_free(cp);
  121. done:
  122. if (c1)
  123. connection_free_(TO_CONN(c1));
  124. tor_free(cp);
  125. tor_free(buf);
  126. UNMOCK(connection_write_to_buf_impl_);
  127. }
  128. static void
  129. test_ext_or_init_auth(void *arg)
  130. {
  131. or_options_t *options = get_options_mutable();
  132. const char *fn;
  133. char *cp = NULL;
  134. struct stat st;
  135. char cookie0[32];
  136. (void)arg;
  137. /* Check default filename location */
  138. options->DataDirectory = tor_strdup("foo");
  139. cp = get_ext_or_auth_cookie_file_name();
  140. tt_str_op(cp, ==, "foo"PATH_SEPARATOR"extended_orport_auth_cookie");
  141. tor_free(cp);
  142. /* Shouldn't be initialized already, or our tests will be a bit
  143. * meaningless */
  144. test_assert(tor_mem_is_zero(ext_or_auth_cookie, 32));
  145. /* Now make sure we use a temporary file */
  146. fn = get_fname("ext_cookie_file");
  147. options->ExtORPortCookieAuthFile = tor_strdup(fn);
  148. cp = get_ext_or_auth_cookie_file_name();
  149. tt_str_op(cp, ==, fn);
  150. tor_free(cp);
  151. tt_int_op(0, ==, init_ext_or_cookie_authentication(1));
  152. tt_int_op(ext_or_auth_cookie_is_set, ==, 1);
  153. cp = read_file_to_str(fn, RFTS_BIN, &st);
  154. tt_ptr_op(cp, !=, NULL);
  155. tt_int_op(st.st_size, ==, 64);
  156. test_memeq(cp, "! Extended ORPort Auth Cookie !\x0a", 32);
  157. test_memeq(cp+32, ext_or_auth_cookie, 32);
  158. memcpy(cookie0, ext_or_auth_cookie, 32);
  159. test_assert(!tor_mem_is_zero(ext_or_auth_cookie, 32));
  160. /* Operation should be idempotent. */
  161. tt_int_op(0, ==, init_ext_or_cookie_authentication(1));
  162. test_memeq(cookie0, ext_or_auth_cookie, 32);
  163. done:
  164. tor_free(cp);
  165. }
  166. static void
  167. test_ext_or_cookie_auth(void *arg)
  168. {
  169. char *reply=NULL, *reply2=NULL, *client_hash=NULL, *client_hash2=NULL;
  170. size_t reply_len=0;
  171. char hmac1[32], hmac2[32];
  172. const char client_nonce[32] =
  173. "Who is the third who walks alway";
  174. char server_hash_input[] =
  175. "ExtORPort authentication server-to-client hash"
  176. "Who is the third who walks alway"
  177. "................................";
  178. char client_hash_input[] =
  179. "ExtORPort authentication client-to-server hash"
  180. "Who is the third who walks alway"
  181. "................................";
  182. (void)arg;
  183. tt_int_op(strlen(client_hash_input), ==, 46+32+32);
  184. tt_int_op(strlen(server_hash_input), ==, 46+32+32);
  185. memcpy(ext_or_auth_cookie, "s beside you? When I count, ther", 32);
  186. ext_or_auth_cookie_is_set = 1;
  187. /* For this authentication, the client sends 32 random bytes (ClientNonce)
  188. * The server replies with 32 byte ServerHash and 32 byte ServerNonce,
  189. * where ServerHash is:
  190. * HMAC-SHA256(CookieString,
  191. * "ExtORPort authentication server-to-client hash" | ClientNonce |
  192. * ServerNonce)"
  193. * The client must reply with 32-byte ClientHash, which we compute as:
  194. * ClientHash is computed as:
  195. * HMAC-SHA256(CookieString,
  196. * "ExtORPort authentication client-to-server hash" | ClientNonce |
  197. * ServerNonce)
  198. */
  199. /* Wrong length */
  200. tt_int_op(-1, ==,
  201. handle_client_auth_nonce(client_nonce, 33, &client_hash, &reply,
  202. &reply_len));
  203. tt_int_op(-1, ==,
  204. handle_client_auth_nonce(client_nonce, 31, &client_hash, &reply,
  205. &reply_len));
  206. /* Now let's try this for real! */
  207. tt_int_op(0, ==,
  208. handle_client_auth_nonce(client_nonce, 32, &client_hash, &reply,
  209. &reply_len));
  210. tt_int_op(reply_len, ==, 64);
  211. tt_ptr_op(reply, !=, NULL);
  212. tt_ptr_op(client_hash, !=, NULL);
  213. /* Fill in the server nonce into the hash inputs... */
  214. memcpy(server_hash_input+46+32, reply+32, 32);
  215. memcpy(client_hash_input+46+32, reply+32, 32);
  216. /* Check the HMACs are correct... */
  217. crypto_hmac_sha256(hmac1, ext_or_auth_cookie, 32, server_hash_input,
  218. 46+32+32);
  219. crypto_hmac_sha256(hmac2, ext_or_auth_cookie, 32, client_hash_input,
  220. 46+32+32);
  221. test_memeq(hmac1, reply, 32);
  222. test_memeq(hmac2, client_hash, 32);
  223. /* Now do it again and make sure that the results are *different* */
  224. tt_int_op(0, ==,
  225. handle_client_auth_nonce(client_nonce, 32, &client_hash2, &reply2,
  226. &reply_len));
  227. test_memneq(reply2, reply, reply_len);
  228. test_memneq(client_hash2, client_hash, 32);
  229. /* But that this one checks out too. */
  230. memcpy(server_hash_input+46+32, reply2+32, 32);
  231. memcpy(client_hash_input+46+32, reply2+32, 32);
  232. /* Check the HMACs are correct... */
  233. crypto_hmac_sha256(hmac1, ext_or_auth_cookie, 32, server_hash_input,
  234. 46+32+32);
  235. crypto_hmac_sha256(hmac2, ext_or_auth_cookie, 32, client_hash_input,
  236. 46+32+32);
  237. test_memeq(hmac1, reply2, 32);
  238. test_memeq(hmac2, client_hash2, 32);
  239. done:
  240. tor_free(reply);
  241. tor_free(client_hash);
  242. tor_free(reply2);
  243. tor_free(client_hash2);
  244. }
  245. static int
  246. crypto_rand_return_tse_str(char *to, size_t n)
  247. {
  248. if (n != 32) {
  249. TT_FAIL(("Asked for %d bytes, not 32", (int)n));
  250. return -1;
  251. }
  252. memcpy(to, "te road There is always another ", 32);
  253. return 0;
  254. }
  255. static void
  256. test_ext_or_cookie_auth_testvec(void *arg)
  257. {
  258. char *reply=NULL, *client_hash=NULL;
  259. size_t reply_len;
  260. char *mem_op_hex_tmp=NULL;
  261. const char client_nonce[] = "But when I look ahead up the whi";
  262. (void)arg;
  263. memcpy(ext_or_auth_cookie, "Gliding wrapt in a brown mantle," , 32);
  264. ext_or_auth_cookie_is_set = 1;
  265. MOCK(crypto_rand, crypto_rand_return_tse_str);
  266. tt_int_op(0, ==,
  267. handle_client_auth_nonce(client_nonce, 32, &client_hash, &reply,
  268. &reply_len));
  269. tt_ptr_op(reply, !=, NULL );
  270. tt_ptr_op(reply_len, ==, 64);
  271. test_memeq(reply+32, "te road There is always another ", 32);
  272. /* HMACSHA256("Gliding wrapt in a brown mantle,"
  273. * "ExtORPort authentication server-to-client hash"
  274. * "But when I look ahead up the write road There is always another ");
  275. */
  276. test_memeq_hex(reply,
  277. "ec80ed6e546d3b36fdfc22fe1315416b"
  278. "029f1ade7610d910878b62eeb7403821");
  279. /* HMACSHA256("Gliding wrapt in a brown mantle,"
  280. * "ExtORPort authentication client-to-server hash"
  281. * "But when I look ahead up the write road There is always another ");
  282. * (Both values computed using Python CLI.)
  283. */
  284. test_memeq_hex(client_hash,
  285. "ab391732dd2ed968cd40c087d1b1f25b"
  286. "33b3cd77ff79bd80c2074bbf438119a2");
  287. done:
  288. UNMOCK(crypto_rand);
  289. tor_free(reply);
  290. tor_free(client_hash);
  291. tor_free(mem_op_hex_tmp);
  292. }
  293. static void
  294. ignore_bootstrap_problem(const char *warn, int reason)
  295. {
  296. (void)warn;
  297. (void)reason;
  298. }
  299. static void
  300. test_ext_or_handshake(void *arg)
  301. {
  302. or_connection_t *conn=NULL;
  303. char b[256];
  304. #define WRITE(s,n) \
  305. do { \
  306. write_to_buf((s), (n), TO_CONN(conn)->inbuf); \
  307. } while (0)
  308. #define CONTAINS(s,n) \
  309. do { \
  310. tt_int_op((n), <=, sizeof(b)); \
  311. tt_int_op(buf_datalen(TO_CONN(conn)->outbuf), ==, (n)); \
  312. if ((n)) { \
  313. fetch_from_buf(b, (n), TO_CONN(conn)->outbuf); \
  314. test_memeq(b, (s), (n)); \
  315. } \
  316. } while (0)
  317. (void) arg;
  318. MOCK(connection_write_to_buf_impl_,
  319. connection_write_to_buf_impl_replacement);
  320. /* Use same authenticators as for test_ext_or_cookie_auth_testvec */
  321. memcpy(ext_or_auth_cookie, "Gliding wrapt in a brown mantle," , 32);
  322. ext_or_auth_cookie_is_set = 1;
  323. init_connection_lists();
  324. conn = or_connection_new(CONN_TYPE_EXT_OR, AF_INET);
  325. tt_int_op(0, ==, connection_ext_or_start_auth(conn));
  326. /* The server starts by telling us about the one supported authtype. */
  327. CONTAINS("\x01\x00", 2);
  328. /* Say the client hasn't responded yet. */
  329. tt_int_op(0, ==, connection_ext_or_process_inbuf(conn));
  330. /* Let's say the client replies badly. */
  331. WRITE("\x99", 1);
  332. tt_int_op(-1, ==, connection_ext_or_process_inbuf(conn));
  333. CONTAINS("", 0);
  334. tt_assert(TO_CONN(conn)->marked_for_close);
  335. close_closeable_connections();
  336. conn = NULL;
  337. /* Okay, try again. */
  338. conn = or_connection_new(CONN_TYPE_EXT_OR, AF_INET);
  339. tt_int_op(0, ==, connection_ext_or_start_auth(conn));
  340. CONTAINS("\x01\x00", 2);
  341. /* Let's say the client replies sensibly this time. "Yes, AUTHTYPE_COOKIE
  342. * sounds delicious. Let's have some of that!" */
  343. WRITE("\x01", 1);
  344. /* Let's say that the client also sends part of a nonce. */
  345. WRITE("But when I look ", 16);
  346. tt_int_op(0, ==, connection_ext_or_process_inbuf(conn));
  347. CONTAINS("", 0);
  348. tt_int_op(TO_CONN(conn)->state, ==,
  349. EXT_OR_CONN_STATE_AUTH_WAIT_CLIENT_NONCE);
  350. /* Pump it again. Nothing should happen. */
  351. tt_int_op(0, ==, connection_ext_or_process_inbuf(conn));
  352. /* send the rest of the nonce. */
  353. WRITE("ahead up the whi", 16);
  354. MOCK(crypto_rand, crypto_rand_return_tse_str);
  355. tt_int_op(0, ==, connection_ext_or_process_inbuf(conn));
  356. UNMOCK(crypto_rand);
  357. /* We should get the right reply from the server. */
  358. CONTAINS("\xec\x80\xed\x6e\x54\x6d\x3b\x36\xfd\xfc\x22\xfe\x13\x15\x41\x6b"
  359. "\x02\x9f\x1a\xde\x76\x10\xd9\x10\x87\x8b\x62\xee\xb7\x40\x38\x21"
  360. "te road There is always another ", 64);
  361. /* Send the wrong response. */
  362. WRITE("not with a bang but a whimper...", 32);
  363. MOCK(control_event_bootstrap_problem, ignore_bootstrap_problem);
  364. tt_int_op(-1, ==, connection_ext_or_process_inbuf(conn));
  365. CONTAINS("\x00", 1);
  366. tt_assert(TO_CONN(conn)->marked_for_close);
  367. /* XXXX Hold-open-until-flushed. */
  368. close_closeable_connections();
  369. conn = NULL;
  370. UNMOCK(control_event_bootstrap_problem);
  371. /* Okay, this time let's succeed. */
  372. conn = or_connection_new(CONN_TYPE_EXT_OR, AF_INET);
  373. tt_int_op(0, ==, connection_ext_or_start_auth(conn));
  374. CONTAINS("\x01\x00", 2);
  375. WRITE("\x01", 1);
  376. WRITE("But when I look ahead up the whi", 32);
  377. MOCK(crypto_rand, crypto_rand_return_tse_str);
  378. tt_int_op(0, ==, connection_ext_or_process_inbuf(conn));
  379. UNMOCK(crypto_rand);
  380. tt_int_op(TO_CONN(conn)->state, ==, EXT_OR_CONN_STATE_AUTH_WAIT_CLIENT_HASH);
  381. CONTAINS("\xec\x80\xed\x6e\x54\x6d\x3b\x36\xfd\xfc\x22\xfe\x13\x15\x41\x6b"
  382. "\x02\x9f\x1a\xde\x76\x10\xd9\x10\x87\x8b\x62\xee\xb7\x40\x38\x21"
  383. "te road There is always another ", 64);
  384. /* Send the right response this time. */
  385. WRITE("\xab\x39\x17\x32\xdd\x2e\xd9\x68\xcd\x40\xc0\x87\xd1\xb1\xf2\x5b"
  386. "\x33\xb3\xcd\x77\xff\x79\xbd\x80\xc2\x07\x4b\xbf\x43\x81\x19\xa2",
  387. 32);
  388. tt_int_op(0, ==, connection_ext_or_process_inbuf(conn));
  389. CONTAINS("\x01", 1);
  390. tt_assert(! TO_CONN(conn)->marked_for_close);
  391. tt_int_op(TO_CONN(conn)->state, ==, EXT_OR_CONN_STATE_OPEN);
  392. done:
  393. UNMOCK(connection_write_to_buf_impl_);
  394. UNMOCK(crypto_rand);
  395. if (conn)
  396. connection_free_(TO_CONN(conn));
  397. #undef WRITE
  398. }
  399. struct testcase_t extorport_tests[] = {
  400. { "id_map", test_ext_or_id_map, TT_FORK, NULL, NULL },
  401. { "write_command", test_ext_or_write_command, TT_FORK, NULL, NULL },
  402. { "init_auth", test_ext_or_init_auth, TT_FORK, NULL, NULL },
  403. { "cookie_auth", test_ext_or_cookie_auth, TT_FORK, NULL, NULL },
  404. { "cookie_auth_testvec", test_ext_or_cookie_auth_testvec, TT_FORK,
  405. NULL, NULL },
  406. { "handshake", test_ext_or_handshake, TT_FORK, NULL, NULL },
  407. END_OF_TESTCASES
  408. };