test_dir_common.c 15 KB

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