test_dir_common.c 15 KB

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