test_dir_common.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2019, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #include "orconfig.h"
  6. #define DIRVOTE_PRIVATE
  7. #include "test/test.h"
  8. #include "core/or/or.h"
  9. #include "feature/dirauth/dirvote.h"
  10. #include "feature/nodelist/nodelist.h"
  11. #include "feature/nodelist/routerlist.h"
  12. #include "feature/dirparse/authcert_parse.h"
  13. #include "feature/dirparse/ns_parse.h"
  14. #include "test/test_dir_common.h"
  15. #include "feature/dircommon/voting_schedule.h"
  16. #include "feature/nodelist/authority_cert_st.h"
  17. #include "feature/nodelist/networkstatus_st.h"
  18. #include "feature/nodelist/networkstatus_voter_info_st.h"
  19. #include "feature/nodelist/routerinfo_st.h"
  20. #include "feature/dirauth/vote_microdesc_hash_st.h"
  21. #include "feature/nodelist/vote_routerstatus_st.h"
  22. void dir_common_setup_vote(networkstatus_t **vote, time_t now);
  23. networkstatus_t * dir_common_add_rs_and_parse(networkstatus_t *vote,
  24. networkstatus_t **vote_out,
  25. vote_routerstatus_t * (*vrs_gen)(int idx, time_t now),
  26. crypto_pk_t *sign_skey, int *n_vrs,
  27. time_t now, int clear_rl);
  28. /** Initialize and set auth certs and keys
  29. * Returns 0 on success, -1 on failure. Clean up handled by caller.
  30. */
  31. int
  32. dir_common_authority_pk_init(authority_cert_t **cert1,
  33. authority_cert_t **cert2,
  34. authority_cert_t **cert3,
  35. crypto_pk_t **sign_skey_1,
  36. crypto_pk_t **sign_skey_2,
  37. crypto_pk_t **sign_skey_3)
  38. {
  39. /* Parse certificates and keys. */
  40. authority_cert_t *cert;
  41. cert = authority_cert_parse_from_string(AUTHORITY_CERT_1,
  42. strlen(AUTHORITY_CERT_1),
  43. NULL);
  44. tt_assert(cert);
  45. tt_assert(cert->identity_key);
  46. *cert1 = cert;
  47. tt_assert(*cert1);
  48. *cert2 = authority_cert_parse_from_string(AUTHORITY_CERT_2,
  49. strlen(AUTHORITY_CERT_2),
  50. NULL);
  51. tt_assert(*cert2);
  52. *cert3 = authority_cert_parse_from_string(AUTHORITY_CERT_3,
  53. strlen(AUTHORITY_CERT_3),
  54. NULL);
  55. tt_assert(*cert3);
  56. *sign_skey_1 = crypto_pk_new();
  57. *sign_skey_2 = crypto_pk_new();
  58. *sign_skey_3 = crypto_pk_new();
  59. tt_assert(!crypto_pk_read_private_key_from_string(*sign_skey_1,
  60. AUTHORITY_SIGNKEY_1, -1));
  61. tt_assert(!crypto_pk_read_private_key_from_string(*sign_skey_2,
  62. AUTHORITY_SIGNKEY_2, -1));
  63. tt_assert(!crypto_pk_read_private_key_from_string(*sign_skey_3,
  64. AUTHORITY_SIGNKEY_3, -1));
  65. tt_assert(!crypto_pk_cmp_keys(*sign_skey_1, (*cert1)->signing_key));
  66. tt_assert(!crypto_pk_cmp_keys(*sign_skey_2, (*cert2)->signing_key));
  67. return 0;
  68. done:
  69. return -1;
  70. }
  71. /**
  72. * Generate a routerstatus for v3_networkstatus test.
  73. */
  74. vote_routerstatus_t *
  75. dir_common_gen_routerstatus_for_v3ns(int idx, time_t now)
  76. {
  77. vote_routerstatus_t *vrs=NULL;
  78. routerstatus_t *rs = NULL;
  79. tor_addr_t addr_ipv6;
  80. char *method_list = NULL;
  81. switch (idx) {
  82. case 0:
  83. /* Generate the first routerstatus. */
  84. vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
  85. rs = &vrs->status;
  86. vrs->version = tor_strdup("0.1.2.14");
  87. rs->published_on = now-1500;
  88. strlcpy(rs->nickname, "router2", sizeof(rs->nickname));
  89. memset(rs->identity_digest, TEST_DIR_ROUTER_ID_1, DIGEST_LEN);
  90. memset(rs->descriptor_digest, TEST_DIR_ROUTER_DD_1, DIGEST_LEN);
  91. rs->addr = 0x99008801;
  92. rs->or_port = 443;
  93. rs->dir_port = 8000;
  94. /* all flags but running and v2dir cleared */
  95. rs->is_flagged_running = 1;
  96. rs->is_v2_dir = 1;
  97. rs->is_valid = 1; /* xxxxx */
  98. break;
  99. case 1:
  100. /* Generate the second routerstatus. */
  101. vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
  102. rs = &vrs->status;
  103. vrs->version = tor_strdup("0.2.0.5");
  104. rs->published_on = now-1000;
  105. strlcpy(rs->nickname, "router1", sizeof(rs->nickname));
  106. memset(rs->identity_digest, TEST_DIR_ROUTER_ID_2, DIGEST_LEN);
  107. memset(rs->descriptor_digest, TEST_DIR_ROUTER_DD_2, DIGEST_LEN);
  108. rs->addr = 0x99009901;
  109. rs->or_port = 443;
  110. rs->dir_port = 0;
  111. tor_addr_parse(&addr_ipv6, "[1:2:3::4]");
  112. tor_addr_copy(&rs->ipv6_addr, &addr_ipv6);
  113. rs->ipv6_orport = 4711;
  114. rs->is_exit = rs->is_stable = rs->is_fast = rs->is_flagged_running =
  115. rs->is_valid = rs->is_possible_guard = rs->is_v2_dir = 1;
  116. break;
  117. case 2:
  118. /* Generate the third routerstatus. */
  119. vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
  120. rs = &vrs->status;
  121. vrs->version = tor_strdup("0.1.0.3");
  122. rs->published_on = now-1000;
  123. strlcpy(rs->nickname, "router3", sizeof(rs->nickname));
  124. memset(rs->identity_digest, TEST_DIR_ROUTER_ID_3, DIGEST_LEN);
  125. memset(rs->descriptor_digest, TEST_DIR_ROUTER_DD_3, DIGEST_LEN);
  126. rs->addr = 0xAA009901;
  127. rs->or_port = 400;
  128. rs->dir_port = 9999;
  129. rs->is_authority = rs->is_exit = rs->is_stable = rs->is_fast =
  130. rs->is_flagged_running = rs->is_valid = rs->is_v2_dir =
  131. rs->is_possible_guard = 1;
  132. break;
  133. case 3:
  134. /* Generate a fourth routerstatus that is not running. */
  135. vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
  136. rs = &vrs->status;
  137. vrs->version = tor_strdup("0.1.6.3");
  138. rs->published_on = now-1000;
  139. strlcpy(rs->nickname, "router4", sizeof(rs->nickname));
  140. memset(rs->identity_digest, TEST_DIR_ROUTER_ID_4, DIGEST_LEN);
  141. memset(rs->descriptor_digest, TEST_DIR_ROUTER_DD_4, DIGEST_LEN);
  142. rs->addr = 0xC0000203;
  143. rs->or_port = 500;
  144. rs->dir_port = 1999;
  145. rs->is_v2_dir = 1;
  146. /* Running flag (and others) cleared */
  147. break;
  148. case 4:
  149. /* No more for this test; return NULL */
  150. vrs = NULL;
  151. break;
  152. default:
  153. /* Shouldn't happen */
  154. tt_abort();
  155. }
  156. if (vrs) {
  157. vrs->microdesc = tor_malloc_zero(sizeof(vote_microdesc_hash_t));
  158. method_list = make_consensus_method_list(MIN_SUPPORTED_CONSENSUS_METHOD,
  159. MAX_SUPPORTED_CONSENSUS_METHOD,
  160. ",");
  161. tor_asprintf(&vrs->microdesc->microdesc_hash_line,
  162. "m %s "
  163. "sha256=xyzajkldsdsajdadlsdjaslsdksdjlsdjsdaskdaaa%d\n",
  164. method_list, idx);
  165. }
  166. done:
  167. tor_free(method_list);
  168. return vrs;
  169. }
  170. /** Initialize networkstatus vote object attributes. */
  171. void
  172. dir_common_setup_vote(networkstatus_t **vote, time_t now)
  173. {
  174. *vote = tor_malloc_zero(sizeof(networkstatus_t));
  175. (*vote)->type = NS_TYPE_VOTE;
  176. (*vote)->published = now;
  177. (*vote)->supported_methods = smartlist_new();
  178. (*vote)->known_flags = smartlist_new();
  179. (*vote)->net_params = smartlist_new();
  180. (*vote)->routerstatus_list = smartlist_new();
  181. (*vote)->voters = smartlist_new();
  182. }
  183. /** Helper: Make a new routerinfo containing the right information for a
  184. * given vote_routerstatus_t. */
  185. routerinfo_t *
  186. dir_common_generate_ri_from_rs(const vote_routerstatus_t *vrs)
  187. {
  188. routerinfo_t *r;
  189. const routerstatus_t *rs = &vrs->status;
  190. static time_t published = 0;
  191. r = tor_malloc_zero(sizeof(routerinfo_t));
  192. r->cert_expiration_time = TIME_MAX;
  193. memcpy(r->cache_info.identity_digest, rs->identity_digest, DIGEST_LEN);
  194. memcpy(r->cache_info.signed_descriptor_digest, rs->descriptor_digest,
  195. DIGEST_LEN);
  196. r->cache_info.do_not_cache = 1;
  197. r->cache_info.routerlist_index = -1;
  198. r->cache_info.signed_descriptor_body =
  199. tor_strdup("123456789012345678901234567890123");
  200. r->cache_info.signed_descriptor_len =
  201. strlen(r->cache_info.signed_descriptor_body);
  202. r->exit_policy = smartlist_new();
  203. r->cache_info.published_on = ++published + time(NULL);
  204. if (rs->has_bandwidth) {
  205. /*
  206. * Multiply by 1000 because the routerinfo_t and the routerstatus_t
  207. * seem to use different units (*sigh*) and because we seem stuck on
  208. * icky and perverse decimal kilobytes (*double sigh*) - see
  209. * router_get_advertised_bandwidth_capped() of routerlist.c and
  210. * routerstatus_format_entry() of dirserv.c.
  211. */
  212. r->bandwidthrate = rs->bandwidth_kb * 1000;
  213. r->bandwidthcapacity = rs->bandwidth_kb * 1000;
  214. }
  215. return r;
  216. }
  217. /** Create routerstatuses and signed vote.
  218. * Create routerstatuses using *vrs_gen* and add them to global routerlist.
  219. * Next, create signed vote using *sign_skey* and *vote*, which should have
  220. * predefined header fields.
  221. * Setting *clear_rl* clears the global routerlist before adding the new
  222. * routers.
  223. * Return the signed vote, same as *vote_out*. Save the number of routers added
  224. * in *n_vrs*.
  225. */
  226. networkstatus_t *
  227. dir_common_add_rs_and_parse(networkstatus_t *vote, networkstatus_t **vote_out,
  228. vote_routerstatus_t * (*vrs_gen)(int idx, time_t now),
  229. crypto_pk_t *sign_skey, int *n_vrs, time_t now,
  230. int clear_rl)
  231. {
  232. vote_routerstatus_t *vrs;
  233. char *v_text=NULL;
  234. const char *msg=NULL;
  235. int idx;
  236. was_router_added_t router_added = -1;
  237. *vote_out = NULL;
  238. if (clear_rl) {
  239. nodelist_free_all();
  240. routerlist_free_all();
  241. }
  242. idx = 0;
  243. do {
  244. vrs = vrs_gen(idx, now);
  245. if (vrs) {
  246. smartlist_add(vote->routerstatus_list, vrs);
  247. router_added =
  248. router_add_to_routerlist(dir_common_generate_ri_from_rs(vrs),
  249. &msg,0,0);
  250. tt_assert(router_added >= 0);
  251. ++idx;
  252. }
  253. } while (vrs);
  254. *n_vrs = idx;
  255. /* dump the vote and try to parse it. */
  256. v_text = format_networkstatus_vote(sign_skey, vote);
  257. tt_assert(v_text);
  258. *vote_out = networkstatus_parse_vote_from_string(v_text,
  259. strlen(v_text),
  260. NULL, NS_TYPE_VOTE);
  261. done:
  262. if (v_text)
  263. tor_free(v_text);
  264. return *vote_out;
  265. }
  266. /** Create a fake *vote* where *cert* describes the signer, *sign_skey*
  267. * is the signing key, and *vrs_gen* is the function we'll use to create the
  268. * routers on which we're voting.
  269. * We pass *vote_out*, *n_vrs*, and *clear_rl* directly to vrs_gen().
  270. * Return 0 on success, return -1 on failure.
  271. */
  272. int
  273. dir_common_construct_vote_1(networkstatus_t **vote, authority_cert_t *cert,
  274. crypto_pk_t *sign_skey,
  275. vote_routerstatus_t * (*vrs_gen)(int idx, time_t now),
  276. networkstatus_t **vote_out, int *n_vrs,
  277. time_t now, int clear_rl)
  278. {
  279. networkstatus_voter_info_t *voter;
  280. dir_common_setup_vote(vote, now);
  281. (*vote)->valid_after = now+1000;
  282. (*vote)->fresh_until = now+2000;
  283. (*vote)->valid_until = now+3000;
  284. (*vote)->vote_seconds = 100;
  285. (*vote)->dist_seconds = 200;
  286. smartlist_split_string((*vote)->supported_methods, "1 2 3", NULL, 0, -1);
  287. (*vote)->client_versions = tor_strdup("0.1.2.14,0.1.2.15");
  288. (*vote)->server_versions = tor_strdup("0.1.2.14,0.1.2.15,0.1.2.16");
  289. smartlist_split_string((*vote)->known_flags,
  290. "Authority Exit Fast Guard Running Stable V2Dir Valid",
  291. 0, SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  292. voter = tor_malloc_zero(sizeof(networkstatus_voter_info_t));
  293. voter->nickname = tor_strdup("Voter1");
  294. voter->address = tor_strdup("1.2.3.4");
  295. voter->addr = 0x01020304;
  296. voter->dir_port = 80;
  297. voter->or_port = 9000;
  298. voter->contact = tor_strdup("voter@example.com");
  299. crypto_pk_get_digest(cert->identity_key, voter->identity_digest);
  300. /*
  301. * Set up a vote; generate it; try to parse it.
  302. */
  303. smartlist_add((*vote)->voters, voter);
  304. (*vote)->cert = authority_cert_dup(cert);
  305. smartlist_split_string((*vote)->net_params, "circuitwindow=101 foo=990",
  306. NULL, 0, 0);
  307. *n_vrs = 0;
  308. /* add routerstatuses */
  309. if (!dir_common_add_rs_and_parse(*vote, vote_out, vrs_gen, sign_skey,
  310. n_vrs, now, clear_rl))
  311. return -1;
  312. return 0;
  313. }
  314. /** See dir_common_construct_vote_1.
  315. * Produces a vote with slightly different values.
  316. */
  317. int
  318. dir_common_construct_vote_2(networkstatus_t **vote, authority_cert_t *cert,
  319. crypto_pk_t *sign_skey,
  320. vote_routerstatus_t * (*vrs_gen)(int idx, time_t now),
  321. networkstatus_t **vote_out, int *n_vrs,
  322. time_t now, int clear_rl)
  323. {
  324. networkstatus_voter_info_t *voter;
  325. dir_common_setup_vote(vote, now);
  326. (*vote)->type = NS_TYPE_VOTE;
  327. (*vote)->published += 1;
  328. (*vote)->valid_after = now+1000;
  329. (*vote)->fresh_until = now+3005;
  330. (*vote)->valid_until = now+3000;
  331. (*vote)->vote_seconds = 100;
  332. (*vote)->dist_seconds = 300;
  333. smartlist_split_string((*vote)->supported_methods, "1 2 3", NULL, 0, -1);
  334. smartlist_split_string((*vote)->known_flags,
  335. "Authority Exit Fast Guard MadeOfCheese MadeOfTin "
  336. "Running Stable V2Dir Valid", 0,
  337. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  338. voter = tor_malloc_zero(sizeof(networkstatus_voter_info_t));
  339. voter->nickname = tor_strdup("Voter2");
  340. voter->address = tor_strdup("2.3.4.5");
  341. voter->addr = 0x02030405;
  342. voter->dir_port = 80;
  343. voter->or_port = 9000;
  344. voter->contact = tor_strdup("voter@example.com");
  345. crypto_pk_get_digest(cert->identity_key, voter->identity_digest);
  346. /*
  347. * Set up a vote; generate it; try to parse it.
  348. */
  349. smartlist_add((*vote)->voters, voter);
  350. (*vote)->cert = authority_cert_dup(cert);
  351. if (! (*vote)->net_params)
  352. (*vote)->net_params = smartlist_new();
  353. smartlist_split_string((*vote)->net_params,
  354. "bar=2000000000 circuitwindow=20",
  355. NULL, 0, 0);
  356. /* add routerstatuses */
  357. /* dump the vote and try to parse it. */
  358. dir_common_add_rs_and_parse(*vote, vote_out, vrs_gen, sign_skey,
  359. n_vrs, now, clear_rl);
  360. return 0;
  361. }
  362. /** See dir_common_construct_vote_1.
  363. * Produces a vote with slightly different values. Adds a legacy key.
  364. */
  365. int
  366. dir_common_construct_vote_3(networkstatus_t **vote, authority_cert_t *cert,
  367. crypto_pk_t *sign_skey,
  368. vote_routerstatus_t * (*vrs_gen)(int idx, time_t now),
  369. networkstatus_t **vote_out, int *n_vrs,
  370. time_t now, int clear_rl)
  371. {
  372. networkstatus_voter_info_t *voter;
  373. dir_common_setup_vote(vote, now);
  374. (*vote)->valid_after = now+1000;
  375. (*vote)->fresh_until = now+2003;
  376. (*vote)->valid_until = now+3000;
  377. (*vote)->vote_seconds = 100;
  378. (*vote)->dist_seconds = 250;
  379. smartlist_split_string((*vote)->supported_methods, "1 2 3 4", NULL, 0, -1);
  380. (*vote)->client_versions = tor_strdup("0.1.2.14,0.1.2.17");
  381. (*vote)->server_versions = tor_strdup("0.1.2.10,0.1.2.15,0.1.2.16");
  382. smartlist_split_string((*vote)->known_flags,
  383. "Authority Exit Fast Guard Running Stable V2Dir Valid",
  384. 0, SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  385. voter = tor_malloc_zero(sizeof(networkstatus_voter_info_t));
  386. voter->nickname = tor_strdup("Voter2");
  387. voter->address = tor_strdup("3.4.5.6");
  388. voter->addr = 0x03040506;
  389. voter->dir_port = 80;
  390. voter->or_port = 9000;
  391. voter->contact = tor_strdup("voter@example.com");
  392. crypto_pk_get_digest(cert->identity_key, voter->identity_digest);
  393. memset(voter->legacy_id_digest, (int)'A', DIGEST_LEN);
  394. /*
  395. * Set up a vote; generate it; try to parse it.
  396. */
  397. smartlist_add((*vote)->voters, voter);
  398. (*vote)->cert = authority_cert_dup(cert);
  399. smartlist_split_string((*vote)->net_params, "circuitwindow=80 foo=660",
  400. NULL, 0, 0);
  401. /* add routerstatuses */
  402. /* dump the vote and try to parse it. */
  403. dir_common_add_rs_and_parse(*vote, vote_out, vrs_gen, sign_skey,
  404. n_vrs, now, clear_rl);
  405. return 0;
  406. }