test_hs.c 32 KB

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