test_hs.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  1. /* Copyright (c) 2007-2016, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file test_hs.c
  5. * \brief Unit tests for hidden service.
  6. **/
  7. #define CONTROL_PRIVATE
  8. #define CIRCUITBUILD_PRIVATE
  9. #define RENDSERVICE_PRIVATE
  10. #include "or.h"
  11. #include "test.h"
  12. #include "control.h"
  13. #include "config.h"
  14. #include "rendcommon.h"
  15. #include "rendservice.h"
  16. #include "routerset.h"
  17. #include "circuitbuild.h"
  18. #include "test_helpers.h"
  19. /* mock ID digest and longname for node that's in nodelist */
  20. #define HSDIR_EXIST_ID "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA" \
  21. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  22. #define STR_HSDIR_EXIST_LONGNAME \
  23. "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=TestDir"
  24. /* mock ID digest and longname for node that's not in nodelist */
  25. #define HSDIR_NONE_EXIST_ID "\xBB\xBB\xBB\xBB\xBB\xBB\xBB\xBB\xBB\xBB" \
  26. "\xBB\xBB\xBB\xBB\xBB\xBB\xBB\xBB\xBB\xBB"
  27. #define STR_HSDIR_NONE_EXIST_LONGNAME \
  28. "$BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
  29. /* DuckDuckGo descriptor as an example. */
  30. static const char *hs_desc_content = "\
  31. rendezvous-service-descriptor g5ojobzupf275beh5ra72uyhb3dkpxwg\r\n\
  32. version 2\r\n\
  33. permanent-key\r\n\
  34. -----BEGIN RSA PUBLIC KEY-----\r\n\
  35. MIGJAoGBAJ/SzzgrXPxTlFrKVhXh3buCWv2QfcNgncUpDpKouLn3AtPH5Ocys0jE\r\n\
  36. aZSKdvaiQ62md2gOwj4x61cFNdi05tdQjS+2thHKEm/KsB9BGLSLBNJYY356bupg\r\n\
  37. I5gQozM65ENelfxYlysBjJ52xSDBd8C4f/p9umdzaaaCmzXG/nhzAgMBAAE=\r\n\
  38. -----END RSA PUBLIC KEY-----\r\n\
  39. secret-id-part anmjoxxwiupreyajjt5yasimfmwcnxlf\r\n\
  40. publication-time 2015-03-11 19:00:00\r\n\
  41. protocol-versions 2,3\r\n\
  42. introduction-points\r\n\
  43. -----BEGIN MESSAGE-----\r\n\
  44. aW50cm9kdWN0aW9uLXBvaW50IDd1bnd4cmg2dG5kNGh6eWt1Z3EzaGZzdHduc2ll\r\n\
  45. c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Whzd3cyVwpjejlHMXhx\r\n\
  74. MmN0Nkd6VWkrNnVkTDlITTRVOUdHTi9BbW8wRG9GV1hKWHpBQkFXd2YyMVdsd1lW\r\n\
  75. eFJQMHRydi9WCkN6UDkzcHc5OG5vSmdGUGRUZ05iMjdKYmVUZENLVFBrTEtscXFt\r\n\
  76. b3NveUN2RitRa25vUS9BZ01CQUFFPQotLS0tLUVORCBSU0EgUFVCTElDIEtFWS0t\r\n\
  77. LS0tCnNlcnZpY2Uta2V5Ci0tLS0tQkVHSU4gUlNBIFBVQkxJQyBLRVktLS0tLQpN\r\n\
  78. SUdKQW9HQkFMVjNKSmtWN3lTNU9jc1lHMHNFYzFQOTVRclFRR3ZzbGJ6Wi9zRGxl\r\n\
  79. RlpKYXFSOUYvYjRUVERNClNGcFMxcU1GbldkZDgxVmRGMEdYRmN2WVpLamRJdHU2\r\n\
  80. SndBaTRJeEhxeXZtdTRKdUxrcXNaTEFLaXRLVkx4eGsKeERlMjlDNzRWMmJrOTRJ\r\n\
  81. MEgybTNKS2tzTHVwc3VxWWRVUmhOVXN0SElKZmgyZmNIalF0bEFnTUJBQUU9Ci0t\r\n\
  82. LS0tRU5EIFJTQSBQVUJMSUMgS0VZLS0tLS0KCg==\r\n\
  83. -----END MESSAGE-----\r\n\
  84. signature\r\n\
  85. -----BEGIN SIGNATURE-----\r\n\
  86. d4OuCE5OLAOnRB6cQN6WyMEmg/BHem144Vec+eYgeWoKwx3MxXFplUjFxgnMlmwN\r\n\
  87. PcftsZf2ztN0sbNCtPgDL3d0PqvxY3iHTQAI8EbaGq/IAJUZ8U4y963dD5+Bn6JQ\r\n\
  88. myE3ctmh0vy5+QxSiRjmQBkuEpCyks7LvWvHYrhnmcg=\r\n\
  89. -----END SIGNATURE-----";
  90. /* Helper global variable for hidden service descriptor event test.
  91. * It's used as a pointer to dynamically created message buffer in
  92. * send_control_event_string_replacement function, which mocks
  93. * send_control_event_string function.
  94. *
  95. * Always free it after use! */
  96. static char *received_msg = NULL;
  97. /** Mock function for send_control_event_string
  98. */
  99. static void
  100. queue_control_event_string_replacement(uint16_t event, char *msg)
  101. {
  102. (void) event;
  103. tor_free(received_msg);
  104. received_msg = msg;
  105. }
  106. /** Mock function for node_describe_longname_by_id, it returns either
  107. * STR_HSDIR_EXIST_LONGNAME or STR_HSDIR_NONE_EXIST_LONGNAME
  108. */
  109. static const char *
  110. node_describe_longname_by_id_replacement(const char *id_digest)
  111. {
  112. if (!strcmp(id_digest, HSDIR_EXIST_ID)) {
  113. return STR_HSDIR_EXIST_LONGNAME;
  114. } else {
  115. return STR_HSDIR_NONE_EXIST_LONGNAME;
  116. }
  117. }
  118. /** Make sure each hidden service descriptor async event generation
  119. *
  120. * function generates the message in expected format.
  121. */
  122. static void
  123. test_hs_desc_event(void *arg)
  124. {
  125. #define STR_HS_ADDR "ajhb7kljbiru65qo"
  126. #define STR_HS_CONTENT_DESC_ID "g5ojobzupf275beh5ra72uyhb3dkpxwg"
  127. #define STR_DESC_ID_BASE32 "hba3gmcgpfivzfhx5rtfqkfdhv65yrj3"
  128. int ret;
  129. rend_data_t rend_query;
  130. const char *expected_msg;
  131. char desc_id_base32[REND_DESC_ID_V2_LEN_BASE32 + 1];
  132. (void) arg;
  133. MOCK(queue_control_event_string,
  134. queue_control_event_string_replacement);
  135. MOCK(node_describe_longname_by_id,
  136. node_describe_longname_by_id_replacement);
  137. /* setup rend_query struct */
  138. memset(&rend_query, 0, sizeof(rend_query));
  139. strncpy(rend_query.onion_address, STR_HS_ADDR,
  140. REND_SERVICE_ID_LEN_BASE32+1);
  141. rend_query.auth_type = REND_NO_AUTH;
  142. rend_query.hsdirs_fp = smartlist_new();
  143. smartlist_add(rend_query.hsdirs_fp, tor_memdup(HSDIR_EXIST_ID,
  144. DIGEST_LEN));
  145. /* Compute descriptor ID for replica 0, should be STR_DESC_ID_BASE32. */
  146. ret = rend_compute_v2_desc_id(rend_query.descriptor_id[0],
  147. rend_query.onion_address,
  148. NULL, 0, 0);
  149. tt_int_op(ret, ==, 0);
  150. base32_encode(desc_id_base32, sizeof(desc_id_base32),
  151. rend_query.descriptor_id[0], DIGEST_LEN);
  152. /* Make sure rend_compute_v2_desc_id works properly. */
  153. tt_mem_op(desc_id_base32, OP_EQ, STR_DESC_ID_BASE32,
  154. sizeof(desc_id_base32));
  155. /* test request event */
  156. control_event_hs_descriptor_requested(&rend_query, HSDIR_EXIST_ID,
  157. STR_DESC_ID_BASE32);
  158. expected_msg = "650 HS_DESC REQUESTED "STR_HS_ADDR" NO_AUTH "\
  159. STR_HSDIR_EXIST_LONGNAME " " STR_DESC_ID_BASE32 "\r\n";
  160. tt_assert(received_msg);
  161. tt_str_op(received_msg,OP_EQ, expected_msg);
  162. tor_free(received_msg);
  163. /* test received event */
  164. rend_query.auth_type = REND_BASIC_AUTH;
  165. control_event_hs_descriptor_received(rend_query.onion_address,
  166. &rend_query, HSDIR_EXIST_ID);
  167. expected_msg = "650 HS_DESC RECEIVED "STR_HS_ADDR" BASIC_AUTH "\
  168. STR_HSDIR_EXIST_LONGNAME " " STR_DESC_ID_BASE32"\r\n";
  169. tt_assert(received_msg);
  170. tt_str_op(received_msg,OP_EQ, expected_msg);
  171. tor_free(received_msg);
  172. /* test failed event */
  173. rend_query.auth_type = REND_STEALTH_AUTH;
  174. control_event_hs_descriptor_failed(&rend_query,
  175. HSDIR_NONE_EXIST_ID,
  176. "QUERY_REJECTED");
  177. expected_msg = "650 HS_DESC FAILED "STR_HS_ADDR" STEALTH_AUTH "\
  178. STR_HSDIR_NONE_EXIST_LONGNAME" REASON=QUERY_REJECTED\r\n";
  179. tt_assert(received_msg);
  180. tt_str_op(received_msg,OP_EQ, expected_msg);
  181. tor_free(received_msg);
  182. /* test invalid auth type */
  183. rend_query.auth_type = 999;
  184. control_event_hs_descriptor_failed(&rend_query,
  185. HSDIR_EXIST_ID,
  186. "QUERY_REJECTED");
  187. expected_msg = "650 HS_DESC FAILED "STR_HS_ADDR" UNKNOWN "\
  188. STR_HSDIR_EXIST_LONGNAME " " STR_DESC_ID_BASE32\
  189. " REASON=QUERY_REJECTED\r\n";
  190. tt_assert(received_msg);
  191. tt_str_op(received_msg,OP_EQ, expected_msg);
  192. tor_free(received_msg);
  193. /* test valid content. */
  194. char *exp_msg;
  195. control_event_hs_descriptor_content(rend_query.onion_address,
  196. STR_HS_CONTENT_DESC_ID, HSDIR_EXIST_ID,
  197. hs_desc_content);
  198. tor_asprintf(&exp_msg, "650+HS_DESC_CONTENT " STR_HS_ADDR " "\
  199. STR_HS_CONTENT_DESC_ID " " STR_HSDIR_EXIST_LONGNAME\
  200. "\r\n%s\r\n.\r\n650 OK\r\n", hs_desc_content);
  201. tt_assert(received_msg);
  202. tt_str_op(received_msg, OP_EQ, exp_msg);
  203. tor_free(received_msg);
  204. tor_free(exp_msg);
  205. SMARTLIST_FOREACH(rend_query.hsdirs_fp, char *, d, tor_free(d));
  206. smartlist_free(rend_query.hsdirs_fp);
  207. done:
  208. UNMOCK(queue_control_event_string);
  209. UNMOCK(node_describe_longname_by_id);
  210. tor_free(received_msg);
  211. }
  212. /* Make sure we always pick the right RP, given a well formatted
  213. * Tor2webRendezvousPoints value. */
  214. static void
  215. test_pick_tor2web_rendezvous_node(void *arg)
  216. {
  217. or_options_t *options = get_options_mutable();
  218. const node_t *chosen_rp = NULL;
  219. router_crn_flags_t flags = CRN_NEED_DESC;
  220. int retval, i;
  221. const char *tor2web_rendezvous_str = "test003r";
  222. (void) arg;
  223. /* Setup fake routerlist. */
  224. helper_setup_fake_routerlist();
  225. /* Parse Tor2webRendezvousPoints as a routerset. */
  226. options->Tor2webRendezvousPoints = routerset_new();
  227. retval = routerset_parse(options->Tor2webRendezvousPoints,
  228. tor2web_rendezvous_str,
  229. "test_tor2web_rp");
  230. tt_int_op(retval, >=, 0);
  231. /* Pick rendezvous point. Make sure the correct one is
  232. picked. Repeat many times to make sure it works properly. */
  233. for (i = 0; i < 50 ; i++) {
  234. chosen_rp = pick_tor2web_rendezvous_node(flags, options);
  235. tt_assert(chosen_rp);
  236. tt_str_op(chosen_rp->ri->nickname, ==, tor2web_rendezvous_str);
  237. }
  238. done:
  239. routerset_free(options->Tor2webRendezvousPoints);
  240. }
  241. /* Make sure we never pick an RP if Tor2webRendezvousPoints doesn't
  242. * correspond to an actual node. */
  243. static void
  244. test_pick_bad_tor2web_rendezvous_node(void *arg)
  245. {
  246. or_options_t *options = get_options_mutable();
  247. const node_t *chosen_rp = NULL;
  248. router_crn_flags_t flags = CRN_NEED_DESC;
  249. int retval, i;
  250. const char *tor2web_rendezvous_str = "dummy";
  251. (void) arg;
  252. /* Setup fake routerlist. */
  253. helper_setup_fake_routerlist();
  254. /* Parse Tor2webRendezvousPoints as a routerset. */
  255. options->Tor2webRendezvousPoints = routerset_new();
  256. retval = routerset_parse(options->Tor2webRendezvousPoints,
  257. tor2web_rendezvous_str,
  258. "test_tor2web_rp");
  259. tt_int_op(retval, >=, 0);
  260. /* Pick rendezvous point. Since Tor2webRendezvousPoints was set to a
  261. dummy value, we shouldn't find any eligible RPs. */
  262. for (i = 0; i < 50 ; i++) {
  263. chosen_rp = pick_tor2web_rendezvous_node(flags, options);
  264. tt_assert(!chosen_rp);
  265. }
  266. done:
  267. routerset_free(options->Tor2webRendezvousPoints);
  268. }
  269. /* Make sure rend_data_t is valid at creation, destruction and when
  270. * duplicated. */
  271. static void
  272. test_hs_rend_data(void *arg)
  273. {
  274. int rep;
  275. rend_data_t *client = NULL, *client_dup = NULL;
  276. /* Binary format of a descriptor ID. */
  277. char desc_id[DIGEST_LEN];
  278. char client_cookie[REND_DESC_COOKIE_LEN];
  279. time_t now = time(NULL);
  280. rend_data_t *service_dup = NULL;
  281. rend_data_t *service = NULL;
  282. (void)arg;
  283. base32_decode(desc_id, sizeof(desc_id), STR_DESC_ID_BASE32,
  284. REND_DESC_ID_V2_LEN_BASE32);
  285. memset(client_cookie, 'e', sizeof(client_cookie));
  286. client = rend_data_client_create(STR_HS_ADDR, desc_id, client_cookie,
  287. REND_NO_AUTH);
  288. tt_assert(client);
  289. tt_int_op(client->auth_type, ==, REND_NO_AUTH);
  290. tt_str_op(client->onion_address, OP_EQ, STR_HS_ADDR);
  291. tt_mem_op(client->desc_id_fetch, OP_EQ, desc_id, sizeof(desc_id));
  292. tt_mem_op(client->descriptor_cookie, OP_EQ, client_cookie,
  293. sizeof(client_cookie));
  294. tt_assert(client->hsdirs_fp);
  295. tt_int_op(smartlist_len(client->hsdirs_fp), ==, 0);
  296. for (rep = 0; rep < REND_NUMBER_OF_NON_CONSECUTIVE_REPLICAS; rep++) {
  297. int ret = rend_compute_v2_desc_id(desc_id, client->onion_address,
  298. client->descriptor_cookie, now, rep);
  299. /* That shouldn't never fail. */
  300. tt_int_op(ret, ==, 0);
  301. tt_mem_op(client->descriptor_id[rep], OP_EQ, desc_id, sizeof(desc_id));
  302. }
  303. /* The rest should be zeroed because this is a client request. */
  304. tt_int_op(tor_digest_is_zero(client->rend_pk_digest), ==, 1);
  305. tt_int_op(tor_digest_is_zero(client->rend_cookie), ==, 1);
  306. /* Test dup(). */
  307. client_dup = rend_data_dup(client);
  308. tt_assert(client_dup);
  309. tt_int_op(client_dup->auth_type, ==, client->auth_type);
  310. tt_str_op(client_dup->onion_address, OP_EQ, client->onion_address);
  311. tt_mem_op(client_dup->desc_id_fetch, OP_EQ, client->desc_id_fetch,
  312. sizeof(client_dup->desc_id_fetch));
  313. tt_mem_op(client_dup->descriptor_cookie, OP_EQ, client->descriptor_cookie,
  314. sizeof(client_dup->descriptor_cookie));
  315. tt_assert(client_dup->hsdirs_fp);
  316. tt_int_op(smartlist_len(client_dup->hsdirs_fp), ==, 0);
  317. for (rep = 0; rep < REND_NUMBER_OF_NON_CONSECUTIVE_REPLICAS; rep++) {
  318. tt_mem_op(client_dup->descriptor_id[rep], OP_EQ,
  319. client->descriptor_id[rep], DIGEST_LEN);
  320. }
  321. /* The rest should be zeroed because this is a client request. */
  322. tt_int_op(tor_digest_is_zero(client_dup->rend_pk_digest), ==, 1);
  323. tt_int_op(tor_digest_is_zero(client_dup->rend_cookie), ==, 1);
  324. rend_data_free(client);
  325. client = NULL;
  326. rend_data_free(client_dup);
  327. client_dup = NULL;
  328. /* Reset state. */
  329. base32_decode(desc_id, sizeof(desc_id), STR_DESC_ID_BASE32,
  330. REND_DESC_ID_V2_LEN_BASE32);
  331. memset(client_cookie, 'e', sizeof(client_cookie));
  332. /* Try with different parameters here for which some content should be
  333. * zeroed out. */
  334. client = rend_data_client_create(NULL, desc_id, NULL, REND_BASIC_AUTH);
  335. tt_assert(client);
  336. tt_int_op(client->auth_type, ==, REND_BASIC_AUTH);
  337. tt_int_op(strlen(client->onion_address), ==, 0);
  338. tt_mem_op(client->desc_id_fetch, OP_EQ, desc_id, sizeof(desc_id));
  339. tt_int_op(tor_mem_is_zero(client->descriptor_cookie,
  340. sizeof(client->descriptor_cookie)), ==, 1);
  341. tt_assert(client->hsdirs_fp);
  342. tt_int_op(smartlist_len(client->hsdirs_fp), ==, 0);
  343. for (rep = 0; rep < REND_NUMBER_OF_NON_CONSECUTIVE_REPLICAS; rep++) {
  344. tt_int_op(tor_digest_is_zero(client->descriptor_id[rep]), ==, 1);
  345. }
  346. /* The rest should be zeroed because this is a client request. */
  347. tt_int_op(tor_digest_is_zero(client->rend_pk_digest), ==, 1);
  348. tt_int_op(tor_digest_is_zero(client->rend_cookie), ==, 1);
  349. rend_data_free(client);
  350. client = NULL;
  351. /* Let's test the service object now. */
  352. char rend_pk_digest[DIGEST_LEN];
  353. uint8_t rend_cookie[DIGEST_LEN];
  354. memset(rend_pk_digest, 'f', sizeof(rend_pk_digest));
  355. memset(rend_cookie, 'g', sizeof(rend_cookie));
  356. service = rend_data_service_create(STR_HS_ADDR, rend_pk_digest,
  357. rend_cookie, REND_NO_AUTH);
  358. tt_assert(service);
  359. tt_int_op(service->auth_type, ==, REND_NO_AUTH);
  360. tt_str_op(service->onion_address, OP_EQ, STR_HS_ADDR);
  361. tt_mem_op(service->rend_pk_digest, OP_EQ, rend_pk_digest,
  362. sizeof(rend_pk_digest));
  363. tt_mem_op(service->rend_cookie, OP_EQ, rend_cookie, sizeof(rend_cookie));
  364. tt_assert(service->hsdirs_fp);
  365. tt_int_op(smartlist_len(service->hsdirs_fp), ==, 0);
  366. for (rep = 0; rep < REND_NUMBER_OF_NON_CONSECUTIVE_REPLICAS; rep++) {
  367. tt_int_op(tor_digest_is_zero(service->descriptor_id[rep]), ==, 1);
  368. }
  369. /* The rest should be zeroed because this is a service request. */
  370. tt_int_op(tor_digest_is_zero(service->descriptor_cookie), ==, 1);
  371. tt_int_op(tor_digest_is_zero(service->desc_id_fetch), ==, 1);
  372. /* Test dup(). */
  373. service_dup = rend_data_dup(service);
  374. tt_assert(service_dup);
  375. tt_int_op(service_dup->auth_type, ==, service->auth_type);
  376. tt_str_op(service_dup->onion_address, OP_EQ, service->onion_address);
  377. tt_mem_op(service_dup->rend_pk_digest, OP_EQ, service->rend_pk_digest,
  378. sizeof(service_dup->rend_pk_digest));
  379. tt_mem_op(service_dup->rend_cookie, OP_EQ, service->rend_cookie,
  380. sizeof(service_dup->rend_cookie));
  381. tt_assert(service_dup->hsdirs_fp);
  382. tt_int_op(smartlist_len(service_dup->hsdirs_fp), ==, 0);
  383. for (rep = 0; rep < REND_NUMBER_OF_NON_CONSECUTIVE_REPLICAS; rep++) {
  384. tt_int_op(tor_digest_is_zero(service_dup->descriptor_id[rep]), ==, 1);
  385. }
  386. /* The rest should be zeroed because this is a service request. */
  387. tt_int_op(tor_digest_is_zero(service_dup->descriptor_cookie), ==, 1);
  388. tt_int_op(tor_digest_is_zero(service_dup->desc_id_fetch), ==, 1);
  389. done:
  390. rend_data_free(service);
  391. rend_data_free(service_dup);
  392. rend_data_free(client);
  393. rend_data_free(client_dup);
  394. }
  395. /* Test encoding and decoding service authorization cookies */
  396. static void
  397. test_hs_auth_cookies(void *arg)
  398. {
  399. #define TEST_COOKIE_RAW ((const uint8_t *) "abcdefghijklmnop")
  400. #define TEST_COOKIE_ENCODED "YWJjZGVmZ2hpamtsbW5vcA"
  401. #define TEST_COOKIE_ENCODED_STEALTH "YWJjZGVmZ2hpamtsbW5vcB"
  402. #define TEST_COOKIE_ENCODED_INVALID "YWJjZGVmZ2hpamtsbW5vcD"
  403. char *encoded_cookie;
  404. uint8_t raw_cookie[REND_DESC_COOKIE_LEN];
  405. rend_auth_type_t auth_type;
  406. char *err_msg;
  407. int re;
  408. (void)arg;
  409. /* Test that encoding gives the expected result */
  410. encoded_cookie = rend_auth_encode_cookie(TEST_COOKIE_RAW, REND_BASIC_AUTH);
  411. tt_str_op(encoded_cookie, OP_EQ, TEST_COOKIE_ENCODED);
  412. tor_free(encoded_cookie);
  413. encoded_cookie = rend_auth_encode_cookie(TEST_COOKIE_RAW, REND_STEALTH_AUTH);
  414. tt_str_op(encoded_cookie, OP_EQ, TEST_COOKIE_ENCODED_STEALTH);
  415. tor_free(encoded_cookie);
  416. /* Decoding should give the original value */
  417. re = rend_auth_decode_cookie(TEST_COOKIE_ENCODED, raw_cookie, &auth_type,
  418. &err_msg);
  419. tt_assert(!re);
  420. tt_assert(!err_msg);
  421. tt_mem_op(raw_cookie, OP_EQ, TEST_COOKIE_RAW, REND_DESC_COOKIE_LEN);
  422. tt_int_op(auth_type, OP_EQ, REND_BASIC_AUTH);
  423. memset(raw_cookie, 0, sizeof(raw_cookie));
  424. re = rend_auth_decode_cookie(TEST_COOKIE_ENCODED_STEALTH, raw_cookie,
  425. &auth_type, &err_msg);
  426. tt_assert(!re);
  427. tt_assert(!err_msg);
  428. tt_mem_op(raw_cookie, OP_EQ, TEST_COOKIE_RAW, REND_DESC_COOKIE_LEN);
  429. tt_int_op(auth_type, OP_EQ, REND_STEALTH_AUTH);
  430. memset(raw_cookie, 0, sizeof(raw_cookie));
  431. /* Decoding with padding characters should also work */
  432. re = rend_auth_decode_cookie(TEST_COOKIE_ENCODED "==", raw_cookie, NULL,
  433. &err_msg);
  434. tt_assert(!re);
  435. tt_assert(!err_msg);
  436. tt_mem_op(raw_cookie, OP_EQ, TEST_COOKIE_RAW, REND_DESC_COOKIE_LEN);
  437. /* Decoding with an unknown type should fail */
  438. re = rend_auth_decode_cookie(TEST_COOKIE_ENCODED_INVALID, raw_cookie,
  439. &auth_type, &err_msg);
  440. tt_int_op(re, OP_LT, 0);
  441. tt_assert(err_msg);
  442. tor_free(err_msg);
  443. done:
  444. return;
  445. }
  446. static int mock_get_options_calls = 0;
  447. static or_options_t *mock_options = NULL;
  448. static void
  449. reset_options(or_options_t *options, int *get_options_calls)
  450. {
  451. memset(options, 0, sizeof(or_options_t));
  452. options->TestingTorNetwork = 1;
  453. *get_options_calls = 0;
  454. }
  455. static const or_options_t *
  456. mock_get_options(void)
  457. {
  458. ++mock_get_options_calls;
  459. tor_assert(mock_options);
  460. return mock_options;
  461. }
  462. /* arg can't be 0 (the test fails) or 2 (the test is skipped) */
  463. #define CREATE_HS_DIR_NONE ((intptr_t)0x04)
  464. #define CREATE_HS_DIR1 ((intptr_t)0x08)
  465. #define CREATE_HS_DIR2 ((intptr_t)0x10)
  466. /* Test that single onion poisoning works. */
  467. static void
  468. test_single_onion_poisoning(void *arg)
  469. {
  470. or_options_t opt;
  471. mock_options = &opt;
  472. reset_options(mock_options, &mock_get_options_calls);
  473. MOCK(get_options, mock_get_options);
  474. int ret = -1;
  475. intptr_t create_dir_mask = (intptr_t)arg;
  476. /* Get directories with a random suffix so we can repeat the tests */
  477. mock_options->DataDirectory = tor_strdup(get_fname_rnd("test_data_dir"));
  478. rend_service_t *service_1 = tor_malloc_zero(sizeof(rend_service_t));
  479. char *dir1 = tor_strdup(get_fname_rnd("test_hs_dir1"));
  480. rend_service_t *service_2 = tor_malloc_zero(sizeof(rend_service_t));
  481. char *dir2 = tor_strdup(get_fname_rnd("test_hs_dir2"));
  482. smartlist_t *services = smartlist_new();
  483. /* No services, no service to verify, no problem! */
  484. mock_options->HiddenServiceSingleHopMode = 0;
  485. mock_options->HiddenServiceNonAnonymousMode = 0;
  486. ret = rend_config_services(mock_options, 1);
  487. tt_assert(ret == 0);
  488. /* Either way, no problem. */
  489. mock_options->HiddenServiceSingleHopMode = 1;
  490. mock_options->HiddenServiceNonAnonymousMode = 1;
  491. ret = rend_config_services(mock_options, 1);
  492. tt_assert(ret == 0);
  493. /* Create the data directory, and, if the correct bit in arg is set,
  494. * create a directory for that service.
  495. * The data directory is required for the lockfile, which is used when
  496. * loading keys. */
  497. ret = check_private_dir(mock_options->DataDirectory, CPD_CREATE, NULL);
  498. tt_assert(ret == 0);
  499. if (create_dir_mask & CREATE_HS_DIR1) {
  500. ret = check_private_dir(dir1, CPD_CREATE, NULL);
  501. tt_assert(ret == 0);
  502. }
  503. if (create_dir_mask & CREATE_HS_DIR2) {
  504. ret = check_private_dir(dir2, CPD_CREATE, NULL);
  505. tt_assert(ret == 0);
  506. }
  507. service_1->directory = dir1;
  508. service_2->directory = dir2;
  509. /* The services own the directory pointers now */
  510. dir1 = dir2 = NULL;
  511. /* Add port to service 1 */
  512. service_1->ports = smartlist_new();
  513. service_2->ports = smartlist_new();
  514. char *err_msg = NULL;
  515. rend_service_port_config_t *port1 = rend_service_parse_port_config("80", " ",
  516. &err_msg);
  517. tt_assert(port1);
  518. tt_assert(!err_msg);
  519. smartlist_add(service_1->ports, port1);
  520. rend_service_port_config_t *port2 = rend_service_parse_port_config("90", " ",
  521. &err_msg);
  522. /* Add port to service 2 */
  523. tt_assert(port2);
  524. tt_assert(!err_msg);
  525. smartlist_add(service_2->ports, port2);
  526. /* No services, a service to verify, no problem! */
  527. mock_options->HiddenServiceSingleHopMode = 0;
  528. mock_options->HiddenServiceNonAnonymousMode = 0;
  529. ret = rend_service_verify_single_onion_poison(service_1, mock_options);
  530. tt_assert(ret == 0);
  531. ret = rend_service_verify_single_onion_poison(service_2, mock_options);
  532. tt_assert(ret == 0);
  533. /* Either way, no problem. */
  534. mock_options->HiddenServiceSingleHopMode = 1;
  535. mock_options->HiddenServiceNonAnonymousMode = 1;
  536. ret = rend_service_verify_single_onion_poison(service_1, mock_options);
  537. tt_assert(ret == 0);
  538. ret = rend_service_verify_single_onion_poison(service_2, mock_options);
  539. tt_assert(ret == 0);
  540. /* Add the first service */
  541. ret = rend_service_check_dir_and_add(services, mock_options, service_1, 0);
  542. tt_assert(ret == 0);
  543. /* But don't add the second service yet. */
  544. /* Service directories, but no previous keys, no problem! */
  545. mock_options->HiddenServiceSingleHopMode = 0;
  546. mock_options->HiddenServiceNonAnonymousMode = 0;
  547. ret = rend_service_verify_single_onion_poison(service_1, mock_options);
  548. tt_assert(ret == 0);
  549. ret = rend_service_verify_single_onion_poison(service_2, mock_options);
  550. tt_assert(ret == 0);
  551. /* Either way, no problem. */
  552. mock_options->HiddenServiceSingleHopMode = 1;
  553. mock_options->HiddenServiceNonAnonymousMode = 1;
  554. ret = rend_service_verify_single_onion_poison(service_1, mock_options);
  555. tt_assert(ret == 0);
  556. ret = rend_service_verify_single_onion_poison(service_2, mock_options);
  557. tt_assert(ret == 0);
  558. /* Poison! Poison! Poison!
  559. * This can only be done in HiddenServiceSingleHopMode. */
  560. mock_options->HiddenServiceSingleHopMode = 1;
  561. mock_options->HiddenServiceNonAnonymousMode = 1;
  562. ret = rend_service_poison_new_single_onion_dir(service_1, mock_options);
  563. tt_assert(ret == 0);
  564. /* Poisoning twice is a no-op. */
  565. ret = rend_service_poison_new_single_onion_dir(service_1, mock_options);
  566. tt_assert(ret == 0);
  567. /* Poisoned service directories, but no previous keys, no problem! */
  568. mock_options->HiddenServiceSingleHopMode = 0;
  569. mock_options->HiddenServiceNonAnonymousMode = 0;
  570. ret = rend_service_verify_single_onion_poison(service_1, mock_options);
  571. tt_assert(ret == 0);
  572. ret = rend_service_verify_single_onion_poison(service_2, mock_options);
  573. tt_assert(ret == 0);
  574. /* Either way, no problem. */
  575. mock_options->HiddenServiceSingleHopMode = 1;
  576. mock_options->HiddenServiceNonAnonymousMode = 1;
  577. ret = rend_service_verify_single_onion_poison(service_1, mock_options);
  578. tt_assert(ret == 0);
  579. ret = rend_service_verify_single_onion_poison(service_2, mock_options);
  580. tt_assert(ret == 0);
  581. /* Now add some keys, and we'll have a problem. */
  582. ret = rend_service_load_all_keys(services);
  583. tt_assert(ret == 0);
  584. /* Poisoned service directories with previous keys are not allowed. */
  585. mock_options->HiddenServiceSingleHopMode = 0;
  586. mock_options->HiddenServiceNonAnonymousMode = 0;
  587. ret = rend_service_verify_single_onion_poison(service_1, mock_options);
  588. tt_assert(ret < 0);
  589. ret = rend_service_verify_single_onion_poison(service_2, mock_options);
  590. tt_assert(ret == 0);
  591. /* But they are allowed if we're in non-anonymous mode. */
  592. mock_options->HiddenServiceSingleHopMode = 1;
  593. mock_options->HiddenServiceNonAnonymousMode = 1;
  594. ret = rend_service_verify_single_onion_poison(service_1, mock_options);
  595. tt_assert(ret == 0);
  596. ret = rend_service_verify_single_onion_poison(service_2, mock_options);
  597. tt_assert(ret == 0);
  598. /* Re-poisoning directories with existing keys is a no-op, because
  599. * directories with existing keys are ignored. */
  600. mock_options->HiddenServiceSingleHopMode = 1;
  601. mock_options->HiddenServiceNonAnonymousMode = 1;
  602. ret = rend_service_poison_new_single_onion_dir(service_1, mock_options);
  603. tt_assert(ret == 0);
  604. /* And it keeps the poison. */
  605. ret = rend_service_verify_single_onion_poison(service_1, mock_options);
  606. tt_assert(ret == 0);
  607. ret = rend_service_verify_single_onion_poison(service_2, mock_options);
  608. tt_assert(ret == 0);
  609. /* Now add the second service: it has no key and no poison file */
  610. ret = rend_service_check_dir_and_add(services, mock_options, service_2, 0);
  611. /* A new service, and an existing poisoned service. Not ok. */
  612. mock_options->HiddenServiceSingleHopMode = 0;
  613. mock_options->HiddenServiceNonAnonymousMode = 0;
  614. ret = rend_service_verify_single_onion_poison(service_1, mock_options);
  615. tt_assert(ret < 0);
  616. ret = rend_service_verify_single_onion_poison(service_2, mock_options);
  617. tt_assert(ret == 0);
  618. /* But ok to add in non-anonymous mode. */
  619. mock_options->HiddenServiceSingleHopMode = 1;
  620. mock_options->HiddenServiceNonAnonymousMode = 1;
  621. ret = rend_service_verify_single_onion_poison(service_1, mock_options);
  622. tt_assert(ret == 0);
  623. ret = rend_service_verify_single_onion_poison(service_2, mock_options);
  624. tt_assert(ret == 0);
  625. /* Now remove the poisoning from the first service, and we have the opposite
  626. * problem. */
  627. char *poison_path = rend_service_sos_poison_path(service_1);
  628. ret = unlink(poison_path);
  629. tor_free(poison_path);
  630. tt_assert(ret == 0);
  631. /* Unpoisoned service directories with previous keys are ok, as are empty
  632. * directories. */
  633. mock_options->HiddenServiceSingleHopMode = 0;
  634. mock_options->HiddenServiceNonAnonymousMode = 0;
  635. ret = rend_service_verify_single_onion_poison(service_1, mock_options);
  636. tt_assert(ret == 0);
  637. ret = rend_service_verify_single_onion_poison(service_2, mock_options);
  638. tt_assert(ret == 0);
  639. /* But the existing unpoisoned key is not ok in non-anonymous mode, even if
  640. * there is an empty service. */
  641. mock_options->HiddenServiceSingleHopMode = 1;
  642. mock_options->HiddenServiceNonAnonymousMode = 1;
  643. ret = rend_service_verify_single_onion_poison(service_1, mock_options);
  644. tt_assert(ret < 0);
  645. ret = rend_service_verify_single_onion_poison(service_2, mock_options);
  646. tt_assert(ret == 0);
  647. /* Poisoning directories with existing keys is a no-op, because directories
  648. * with existing keys are ignored. But the new directory should poison. */
  649. mock_options->HiddenServiceSingleHopMode = 1;
  650. mock_options->HiddenServiceNonAnonymousMode = 1;
  651. ret = rend_service_poison_new_single_onion_dir(service_1, mock_options);
  652. tt_assert(ret == 0);
  653. ret = rend_service_poison_new_single_onion_dir(service_2, mock_options);
  654. tt_assert(ret == 0);
  655. /* And the old directory remains unpoisoned. */
  656. ret = rend_service_verify_single_onion_poison(service_1, mock_options);
  657. tt_assert(ret < 0);
  658. ret = rend_service_verify_single_onion_poison(service_2, mock_options);
  659. tt_assert(ret == 0);
  660. /* And the new directory should be ignored, because it has no key. */
  661. mock_options->HiddenServiceSingleHopMode = 0;
  662. mock_options->HiddenServiceNonAnonymousMode = 0;
  663. ret = rend_service_verify_single_onion_poison(service_1, mock_options);
  664. tt_assert(ret == 0);
  665. ret = rend_service_verify_single_onion_poison(service_2, mock_options);
  666. tt_assert(ret == 0);
  667. /* Re-poisoning directories without existing keys is a no-op. */
  668. mock_options->HiddenServiceSingleHopMode = 1;
  669. mock_options->HiddenServiceNonAnonymousMode = 1;
  670. ret = rend_service_poison_new_single_onion_dir(service_1, mock_options);
  671. tt_assert(ret == 0);
  672. ret = rend_service_poison_new_single_onion_dir(service_2, mock_options);
  673. tt_assert(ret == 0);
  674. /* And the old directory remains unpoisoned. */
  675. ret = rend_service_verify_single_onion_poison(service_1, mock_options);
  676. tt_assert(ret < 0);
  677. ret = rend_service_verify_single_onion_poison(service_2, mock_options);
  678. tt_assert(ret == 0);
  679. done:
  680. tor_free(dir1);
  681. tor_free(dir2);
  682. /* The test harness deletes the directories at exit */
  683. smartlist_free(services);
  684. rend_service_free(service_1);
  685. rend_service_free(service_2);
  686. UNMOCK(get_options);
  687. tor_free(mock_options->DataDirectory);
  688. }
  689. struct testcase_t hs_tests[] = {
  690. { "hs_rend_data", test_hs_rend_data, TT_FORK,
  691. NULL, NULL },
  692. { "hs_desc_event", test_hs_desc_event, TT_FORK,
  693. NULL, NULL },
  694. { "pick_tor2web_rendezvous_node", test_pick_tor2web_rendezvous_node, TT_FORK,
  695. NULL, NULL },
  696. { "pick_bad_tor2web_rendezvous_node",
  697. test_pick_bad_tor2web_rendezvous_node, TT_FORK,
  698. NULL, NULL },
  699. { "hs_auth_cookies", test_hs_auth_cookies, TT_FORK,
  700. NULL, NULL },
  701. { "single_onion_poisoning_create_dir_none", test_single_onion_poisoning,
  702. TT_FORK, &passthrough_setup, (void*)(CREATE_HS_DIR_NONE) },
  703. { "single_onion_poisoning_create_dir1", test_single_onion_poisoning,
  704. TT_FORK, &passthrough_setup, (void*)(CREATE_HS_DIR1) },
  705. { "single_onion_poisoning_create_dir2", test_single_onion_poisoning,
  706. TT_FORK, &passthrough_setup, (void*)(CREATE_HS_DIR2) },
  707. { "single_onion_poisoning_create_dir_both", test_single_onion_poisoning,
  708. TT_FORK, &passthrough_setup, (void*)(CREATE_HS_DIR1 | CREATE_HS_DIR2) },
  709. END_OF_TESTCASES
  710. };