test_dir_common.c 15 KB

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