test_dir_common.c 15 KB

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