test_routerkeys.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  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 ROUTER_PRIVATE
  7. #include "or.h"
  8. #include "config.h"
  9. #include "router.h"
  10. #include "routerkeys.h"
  11. #include "util.h"
  12. #include "crypto.h"
  13. #include "torcert.h"
  14. #include "test.h"
  15. #ifdef _WIN32
  16. /* For mkdir() */
  17. #include <direct.h>
  18. #endif
  19. static void
  20. test_routerkeys_write_fingerprint(void *arg)
  21. {
  22. crypto_pk_t *key = pk_generate(2);
  23. or_options_t *options = get_options_mutable();
  24. const char *ddir = get_fname("write_fingerprint");
  25. char *cp = NULL, *cp2 = NULL;
  26. char fp[FINGERPRINT_LEN+1];
  27. (void)arg;
  28. tt_assert(key);
  29. options->ORPort_set = 1; /* So that we can get the server ID key */
  30. tor_free(options->DataDirectory);
  31. options->DataDirectory = tor_strdup(ddir);
  32. options->Nickname = tor_strdup("haflinger");
  33. set_server_identity_key(key);
  34. set_client_identity_key(crypto_pk_dup_key(key));
  35. tt_int_op(0, OP_EQ, check_private_dir(ddir, CPD_CREATE, NULL));
  36. tt_int_op(crypto_pk_cmp_keys(get_server_identity_key(),key),OP_EQ,0);
  37. /* Write fingerprint file */
  38. tt_int_op(0, OP_EQ, router_write_fingerprint(0));
  39. cp = read_file_to_str(get_fname("write_fingerprint/fingerprint"),
  40. 0, NULL);
  41. crypto_pk_get_fingerprint(key, fp, 0);
  42. tor_asprintf(&cp2, "haflinger %s\n", fp);
  43. tt_str_op(cp, OP_EQ, cp2);
  44. tor_free(cp);
  45. tor_free(cp2);
  46. /* Write hashed-fingerprint file */
  47. tt_int_op(0, OP_EQ, router_write_fingerprint(1));
  48. cp = read_file_to_str(get_fname("write_fingerprint/hashed-fingerprint"),
  49. 0, NULL);
  50. crypto_pk_get_hashed_fingerprint(key, fp);
  51. tor_asprintf(&cp2, "haflinger %s\n", fp);
  52. tt_str_op(cp, OP_EQ, cp2);
  53. tor_free(cp);
  54. tor_free(cp2);
  55. /* Replace outdated file */
  56. write_str_to_file(get_fname("write_fingerprint/hashed-fingerprint"),
  57. "junk goes here", 0);
  58. tt_int_op(0, OP_EQ, router_write_fingerprint(1));
  59. cp = read_file_to_str(get_fname("write_fingerprint/hashed-fingerprint"),
  60. 0, NULL);
  61. crypto_pk_get_hashed_fingerprint(key, fp);
  62. tor_asprintf(&cp2, "haflinger %s\n", fp);
  63. tt_str_op(cp, OP_EQ, cp2);
  64. tor_free(cp);
  65. tor_free(cp2);
  66. done:
  67. crypto_pk_free(key);
  68. set_client_identity_key(NULL);
  69. tor_free(cp);
  70. tor_free(cp2);
  71. }
  72. static void
  73. test_routerkeys_ed_certs(void *args)
  74. {
  75. (void)args;
  76. ed25519_keypair_t kp1, kp2;
  77. tor_cert_t *cert[2] = {NULL, NULL}, *nocert = NULL;
  78. tor_cert_t *parsed_cert[2] = {NULL, NULL};
  79. time_t now = 1412094534;
  80. uint8_t *junk = NULL;
  81. char *base64 = NULL;
  82. tt_int_op(0,OP_EQ,ed25519_keypair_generate(&kp1, 0));
  83. tt_int_op(0,OP_EQ,ed25519_keypair_generate(&kp2, 0));
  84. for (int i = 0; i <= 1; ++i) {
  85. uint32_t flags = i ? CERT_FLAG_INCLUDE_SIGNING_KEY : 0;
  86. cert[i] = tor_cert_create(&kp1, 5, &kp2.pubkey, now, 10000, flags);
  87. tt_assert(cert[i]);
  88. tt_uint_op(cert[i]->sig_bad, OP_EQ, 0);
  89. tt_uint_op(cert[i]->sig_ok, OP_EQ, 1);
  90. tt_uint_op(cert[i]->cert_expired, OP_EQ, 0);
  91. tt_uint_op(cert[i]->cert_valid, OP_EQ, 1);
  92. tt_int_op(cert[i]->cert_type, OP_EQ, 5);
  93. tt_mem_op(cert[i]->signed_key.pubkey, OP_EQ, &kp2.pubkey.pubkey, 32);
  94. tt_mem_op(cert[i]->signing_key.pubkey, OP_EQ, &kp1.pubkey.pubkey, 32);
  95. tt_int_op(cert[i]->signing_key_included, OP_EQ, i);
  96. tt_assert(cert[i]->encoded);
  97. tt_int_op(cert[i]->encoded_len, OP_EQ, 104 + 36 * i);
  98. tt_int_op(cert[i]->encoded[0], OP_EQ, 1);
  99. tt_int_op(cert[i]->encoded[1], OP_EQ, 5);
  100. parsed_cert[i] = tor_cert_parse(cert[i]->encoded, cert[i]->encoded_len);
  101. tt_assert(parsed_cert[i]);
  102. tt_int_op(cert[i]->encoded_len, OP_EQ, parsed_cert[i]->encoded_len);
  103. tt_mem_op(cert[i]->encoded, OP_EQ, parsed_cert[i]->encoded,
  104. cert[i]->encoded_len);
  105. tt_uint_op(parsed_cert[i]->sig_bad, OP_EQ, 0);
  106. tt_uint_op(parsed_cert[i]->sig_ok, OP_EQ, 0);
  107. tt_uint_op(parsed_cert[i]->cert_expired, OP_EQ, 0);
  108. tt_uint_op(parsed_cert[i]->cert_valid, OP_EQ, 0);
  109. /* Expired */
  110. tt_int_op(tor_cert_checksig(parsed_cert[i], &kp1.pubkey, now + 30000),
  111. OP_LT, 0);
  112. tt_uint_op(parsed_cert[i]->cert_expired, OP_EQ, 1);
  113. parsed_cert[i]->cert_expired = 0;
  114. /* Wrong key */
  115. tt_int_op(tor_cert_checksig(parsed_cert[i], &kp2.pubkey, now), OP_LT, 0);
  116. tt_uint_op(parsed_cert[i]->sig_bad, OP_EQ, 1);
  117. parsed_cert[i]->sig_bad = 0;
  118. /* Missing key */
  119. int ok = tor_cert_checksig(parsed_cert[i], NULL, now);
  120. tt_int_op(ok < 0, OP_EQ, i == 0);
  121. tt_uint_op(parsed_cert[i]->sig_bad, OP_EQ, 0);
  122. tt_assert(parsed_cert[i]->sig_ok == (i != 0));
  123. tt_assert(parsed_cert[i]->cert_valid == (i != 0));
  124. parsed_cert[i]->sig_bad = 0;
  125. parsed_cert[i]->sig_ok = 0;
  126. parsed_cert[i]->cert_valid = 0;
  127. /* Right key */
  128. tt_int_op(tor_cert_checksig(parsed_cert[i], &kp1.pubkey, now), OP_EQ, 0);
  129. tt_uint_op(parsed_cert[i]->sig_bad, OP_EQ, 0);
  130. tt_uint_op(parsed_cert[i]->sig_ok, OP_EQ, 1);
  131. tt_uint_op(parsed_cert[i]->cert_expired, OP_EQ, 0);
  132. tt_uint_op(parsed_cert[i]->cert_valid, OP_EQ, 1);
  133. }
  134. /* Now try some junky certs. */
  135. /* - Truncated */
  136. nocert = tor_cert_parse(cert[0]->encoded, cert[0]->encoded_len-1);
  137. tt_ptr_op(NULL, OP_EQ, nocert);
  138. /* - First byte modified */
  139. cert[0]->encoded[0] = 99;
  140. nocert = tor_cert_parse(cert[0]->encoded, cert[0]->encoded_len);
  141. tt_ptr_op(NULL, OP_EQ, nocert);
  142. cert[0]->encoded[0] = 1;
  143. /* - Extra byte at the end*/
  144. junk = tor_malloc_zero(cert[0]->encoded_len + 1);
  145. memcpy(junk, cert[0]->encoded, cert[0]->encoded_len);
  146. nocert = tor_cert_parse(junk, cert[0]->encoded_len+1);
  147. tt_ptr_op(NULL, OP_EQ, nocert);
  148. /* - Multiple signing key instances */
  149. tor_free(junk);
  150. junk = tor_malloc_zero(104 + 36 * 2);
  151. junk[0] = 1; /* version */
  152. junk[1] = 5; /* cert type */
  153. junk[6] = 1; /* key type */
  154. junk[39] = 2; /* n_extensions */
  155. junk[41] = 32; /* extlen */
  156. junk[42] = 4; /* exttype */
  157. junk[77] = 32; /* extlen */
  158. junk[78] = 4; /* exttype */
  159. nocert = tor_cert_parse(junk, 104 + 36 * 2);
  160. tt_ptr_op(NULL, OP_EQ, nocert);
  161. done:
  162. tor_cert_free(cert[0]);
  163. tor_cert_free(cert[1]);
  164. tor_cert_free(parsed_cert[0]);
  165. tor_cert_free(parsed_cert[1]);
  166. tor_cert_free(nocert);
  167. tor_free(junk);
  168. tor_free(base64);
  169. }
  170. static void
  171. test_routerkeys_ed_key_create(void *arg)
  172. {
  173. (void)arg;
  174. tor_cert_t *cert = NULL;
  175. ed25519_keypair_t *kp1 = NULL, *kp2 = NULL;
  176. time_t now = time(NULL);
  177. /* This is a simple alias for 'make a new keypair' */
  178. kp1 = ed_key_new(NULL, 0, 0, 0, 0, &cert);
  179. tt_assert(kp1);
  180. /* Create a new certificate signed by kp1. */
  181. kp2 = ed_key_new(kp1, INIT_ED_KEY_NEEDCERT, now, 3600, 4, &cert);
  182. tt_assert(kp2);
  183. tt_assert(cert);
  184. tt_mem_op(&cert->signed_key, OP_EQ, &kp2->pubkey,
  185. sizeof(ed25519_public_key_t));
  186. tt_assert(! cert->signing_key_included);
  187. tt_int_op(cert->valid_until, OP_GE, now);
  188. tt_int_op(cert->valid_until, OP_LE, now+7200);
  189. /* Create a new key-including certificate signed by kp1 */
  190. ed25519_keypair_free(kp2);
  191. tor_cert_free(cert);
  192. cert = NULL; kp2 = NULL;
  193. kp2 = ed_key_new(kp1, (INIT_ED_KEY_NEEDCERT|
  194. INIT_ED_KEY_INCLUDE_SIGNING_KEY_IN_CERT),
  195. now, 3600, 4, &cert);
  196. tt_assert(kp2);
  197. tt_assert(cert);
  198. tt_assert(cert->signing_key_included);
  199. tt_mem_op(&cert->signed_key, OP_EQ, &kp2->pubkey,
  200. sizeof(ed25519_public_key_t));
  201. tt_mem_op(&cert->signing_key, OP_EQ, &kp1->pubkey,
  202. sizeof(ed25519_public_key_t));
  203. done:
  204. ed25519_keypair_free(kp1);
  205. ed25519_keypair_free(kp2);
  206. tor_cert_free(cert);
  207. }
  208. static void
  209. test_routerkeys_ed_key_init_basic(void *arg)
  210. {
  211. (void) arg;
  212. tor_cert_t *cert = NULL, *cert2 = NULL;
  213. ed25519_keypair_t *kp1 = NULL, *kp2 = NULL, *kp3 = NULL;
  214. time_t now = time(NULL);
  215. char *fname1 = tor_strdup(get_fname("test_ed_key_1"));
  216. char *fname2 = tor_strdup(get_fname("test_ed_key_2"));
  217. struct stat st;
  218. unlink(fname1);
  219. unlink(fname2);
  220. /* Fail to load a key that isn't there. */
  221. kp1 = ed_key_init_from_file(fname1, 0, LOG_INFO, NULL, now, 0, 7, &cert);
  222. tt_assert(kp1 == NULL);
  223. tt_assert(cert == NULL);
  224. /* Create the key if requested to do so. */
  225. kp1 = ed_key_init_from_file(fname1, INIT_ED_KEY_CREATE, LOG_INFO,
  226. NULL, now, 0, 7, &cert);
  227. tt_assert(kp1 != NULL);
  228. tt_assert(cert == NULL);
  229. tt_int_op(stat(get_fname("test_ed_key_1_cert"), &st), OP_LT, 0);
  230. tt_int_op(stat(get_fname("test_ed_key_1_secret_key"), &st), OP_EQ, 0);
  231. /* Fail to load if we say we need a cert */
  232. kp2 = ed_key_init_from_file(fname1, INIT_ED_KEY_NEEDCERT, LOG_INFO,
  233. NULL, now, 0, 7, &cert);
  234. tt_assert(kp2 == NULL);
  235. /* Fail to load if we say the wrong key type */
  236. kp2 = ed_key_init_from_file(fname1, 0, LOG_INFO,
  237. NULL, now, 0, 6, &cert);
  238. tt_assert(kp2 == NULL);
  239. /* Load successfully if we're not picky, whether we say "create" or not. */
  240. kp2 = ed_key_init_from_file(fname1, INIT_ED_KEY_CREATE, LOG_INFO,
  241. NULL, now, 0, 7, &cert);
  242. tt_assert(kp2 != NULL);
  243. tt_assert(cert == NULL);
  244. tt_mem_op(kp1, OP_EQ, kp2, sizeof(*kp1));
  245. ed25519_keypair_free(kp2); kp2 = NULL;
  246. kp2 = ed_key_init_from_file(fname1, 0, LOG_INFO,
  247. NULL, now, 0, 7, &cert);
  248. tt_assert(kp2 != NULL);
  249. tt_assert(cert == NULL);
  250. tt_mem_op(kp1, OP_EQ, kp2, sizeof(*kp1));
  251. ed25519_keypair_free(kp2); kp2 = NULL;
  252. /* Now create a key with a cert. */
  253. kp2 = ed_key_init_from_file(fname2, (INIT_ED_KEY_CREATE|
  254. INIT_ED_KEY_NEEDCERT),
  255. LOG_INFO, kp1, now, 7200, 7, &cert);
  256. tt_assert(kp2 != NULL);
  257. tt_assert(cert != NULL);
  258. tt_mem_op(kp1, OP_NE, kp2, sizeof(*kp1));
  259. tt_int_op(stat(get_fname("test_ed_key_2_cert"), &st), OP_EQ, 0);
  260. tt_int_op(stat(get_fname("test_ed_key_2_secret_key"), &st), OP_EQ, 0);
  261. tt_assert(cert->cert_valid == 1);
  262. tt_mem_op(&cert->signed_key, OP_EQ, &kp2->pubkey, 32);
  263. /* Now verify we can load the cert... */
  264. kp3 = ed_key_init_from_file(fname2, (INIT_ED_KEY_CREATE|
  265. INIT_ED_KEY_NEEDCERT),
  266. LOG_INFO, kp1, now, 7200, 7, &cert2);
  267. tt_mem_op(kp2, OP_EQ, kp3, sizeof(*kp2));
  268. tt_mem_op(cert2->encoded, OP_EQ, cert->encoded, cert->encoded_len);
  269. ed25519_keypair_free(kp3); kp3 = NULL;
  270. tor_cert_free(cert2); cert2 = NULL;
  271. /* ... even without create... */
  272. kp3 = ed_key_init_from_file(fname2, INIT_ED_KEY_NEEDCERT,
  273. LOG_INFO, kp1, now, 7200, 7, &cert2);
  274. tt_mem_op(kp2, OP_EQ, kp3, sizeof(*kp2));
  275. tt_mem_op(cert2->encoded, OP_EQ, cert->encoded, cert->encoded_len);
  276. ed25519_keypair_free(kp3); kp3 = NULL;
  277. tor_cert_free(cert2); cert2 = NULL;
  278. /* ... but that we don't crash or anything if we say we don't want it. */
  279. kp3 = ed_key_init_from_file(fname2, INIT_ED_KEY_NEEDCERT,
  280. LOG_INFO, kp1, now, 7200, 7, NULL);
  281. tt_mem_op(kp2, OP_EQ, kp3, sizeof(*kp2));
  282. ed25519_keypair_free(kp3); kp3 = NULL;
  283. /* Fail if we're told the wrong signing key */
  284. kp3 = ed_key_init_from_file(fname2, INIT_ED_KEY_NEEDCERT,
  285. LOG_INFO, kp2, now, 7200, 7, &cert2);
  286. tt_assert(kp3 == NULL);
  287. tt_assert(cert2 == NULL);
  288. done:
  289. ed25519_keypair_free(kp1);
  290. ed25519_keypair_free(kp2);
  291. ed25519_keypair_free(kp3);
  292. tor_cert_free(cert);
  293. tor_cert_free(cert2);
  294. tor_free(fname1);
  295. tor_free(fname2);
  296. }
  297. static void
  298. test_routerkeys_ed_key_init_split(void *arg)
  299. {
  300. (void) arg;
  301. tor_cert_t *cert = NULL;
  302. ed25519_keypair_t *kp1 = NULL, *kp2 = NULL;
  303. time_t now = time(NULL);
  304. char *fname1 = tor_strdup(get_fname("test_ed_key_3"));
  305. char *fname2 = tor_strdup(get_fname("test_ed_key_4"));
  306. struct stat st;
  307. const uint32_t flags = INIT_ED_KEY_SPLIT|INIT_ED_KEY_MISSING_SECRET_OK;
  308. unlink(fname1);
  309. unlink(fname2);
  310. /* Can't load key that isn't there. */
  311. kp1 = ed_key_init_from_file(fname1, flags, LOG_INFO, NULL, now, 0, 7, &cert);
  312. tt_assert(kp1 == NULL);
  313. tt_assert(cert == NULL);
  314. /* Create a split key */
  315. kp1 = ed_key_init_from_file(fname1, flags|INIT_ED_KEY_CREATE,
  316. LOG_INFO, NULL, now, 0, 7, &cert);
  317. tt_assert(kp1 != NULL);
  318. tt_assert(cert == NULL);
  319. tt_int_op(stat(get_fname("test_ed_key_3_cert"), &st), OP_LT, 0);
  320. tt_int_op(stat(get_fname("test_ed_key_3_secret_key"), &st), OP_EQ, 0);
  321. tt_int_op(stat(get_fname("test_ed_key_3_public_key"), &st), OP_EQ, 0);
  322. /* Load it. */
  323. kp2 = ed_key_init_from_file(fname1, flags|INIT_ED_KEY_CREATE,
  324. LOG_INFO, NULL, now, 0, 7, &cert);
  325. tt_assert(kp2 != NULL);
  326. tt_assert(cert == NULL);
  327. tt_mem_op(kp1, OP_EQ, kp2, sizeof(*kp2));
  328. ed25519_keypair_free(kp2); kp2 = NULL;
  329. /* Okay, try killing the secret key and loading it. */
  330. unlink(get_fname("test_ed_key_3_secret_key"));
  331. kp2 = ed_key_init_from_file(fname1, flags,
  332. LOG_INFO, NULL, now, 0, 7, &cert);
  333. tt_assert(kp2 != NULL);
  334. tt_assert(cert == NULL);
  335. tt_mem_op(&kp1->pubkey, OP_EQ, &kp2->pubkey, sizeof(kp2->pubkey));
  336. tt_assert(tor_mem_is_zero((char*)kp2->seckey.seckey,
  337. sizeof(kp2->seckey.seckey)));
  338. ed25519_keypair_free(kp2); kp2 = NULL;
  339. /* Even when we're told to "create", don't create if there's a public key */
  340. kp2 = ed_key_init_from_file(fname1, flags|INIT_ED_KEY_CREATE,
  341. LOG_INFO, NULL, now, 0, 7, &cert);
  342. tt_assert(kp2 != NULL);
  343. tt_assert(cert == NULL);
  344. tt_mem_op(&kp1->pubkey, OP_EQ, &kp2->pubkey, sizeof(kp2->pubkey));
  345. tt_assert(tor_mem_is_zero((char*)kp2->seckey.seckey,
  346. sizeof(kp2->seckey.seckey)));
  347. ed25519_keypair_free(kp2); kp2 = NULL;
  348. /* Make sure we fail on a tag mismatch, though */
  349. kp2 = ed_key_init_from_file(fname1, flags,
  350. LOG_INFO, NULL, now, 0, 99, &cert);
  351. tt_assert(kp2 == NULL);
  352. done:
  353. ed25519_keypair_free(kp1);
  354. ed25519_keypair_free(kp2);
  355. tor_cert_free(cert);
  356. tor_free(fname1);
  357. tor_free(fname2);
  358. }
  359. static void
  360. test_routerkeys_ed_keys_init_all(void *arg)
  361. {
  362. (void)arg;
  363. char *dir = tor_strdup(get_fname("test_ed_keys_init_all"));
  364. or_options_t *options = tor_malloc_zero(sizeof(or_options_t));
  365. time_t now = time(NULL);
  366. ed25519_public_key_t id;
  367. ed25519_keypair_t sign, auth;
  368. tor_cert_t *link_cert = NULL;
  369. get_options_mutable()->ORPort_set = 1;
  370. crypto_pk_t *rsa = pk_generate(0);
  371. set_server_identity_key(rsa);
  372. set_client_identity_key(rsa);
  373. router_initialize_tls_context();
  374. options->SigningKeyLifetime = 30*86400;
  375. options->TestingAuthKeyLifetime = 2*86400;
  376. options->TestingLinkCertLifetime = 2*86400;
  377. options->TestingSigningKeySlop = 2*86400;
  378. options->TestingAuthKeySlop = 2*3600;
  379. options->TestingLinkKeySlop = 2*3600;
  380. #ifdef _WIN32
  381. mkdir(dir);
  382. mkdir(get_fname("test_ed_keys_init_all/keys"));
  383. #else
  384. mkdir(dir, 0700);
  385. mkdir(get_fname("test_ed_keys_init_all/keys"), 0700);
  386. #endif /* defined(_WIN32) */
  387. options->DataDirectory = dir;
  388. tt_int_op(1, OP_EQ, load_ed_keys(options, now));
  389. tt_int_op(0, OP_EQ, generate_ed_link_cert(options, now, 0));
  390. tt_assert(get_master_identity_key());
  391. tt_assert(get_master_identity_key());
  392. tt_assert(get_master_signing_keypair());
  393. tt_assert(get_current_auth_keypair());
  394. tt_assert(get_master_signing_key_cert());
  395. tt_assert(get_current_link_cert_cert());
  396. tt_assert(get_current_auth_key_cert());
  397. memcpy(&id, get_master_identity_key(), sizeof(id));
  398. memcpy(&sign, get_master_signing_keypair(), sizeof(sign));
  399. memcpy(&auth, get_current_auth_keypair(), sizeof(auth));
  400. link_cert = tor_cert_dup(get_current_link_cert_cert());
  401. /* Call load_ed_keys again, but nothing has changed. */
  402. tt_int_op(0, OP_EQ, load_ed_keys(options, now));
  403. tt_int_op(0, OP_EQ, generate_ed_link_cert(options, now, 0));
  404. tt_mem_op(&id, OP_EQ, get_master_identity_key(), sizeof(id));
  405. tt_mem_op(&sign, OP_EQ, get_master_signing_keypair(), sizeof(sign));
  406. tt_mem_op(&auth, OP_EQ, get_current_auth_keypair(), sizeof(auth));
  407. tt_assert(tor_cert_eq(link_cert, get_current_link_cert_cert()));
  408. /* Force a reload: we make new link/auth keys. */
  409. routerkeys_free_all();
  410. tt_int_op(1, OP_EQ, load_ed_keys(options, now));
  411. tt_int_op(0, OP_EQ, generate_ed_link_cert(options, now, 0));
  412. tt_mem_op(&id, OP_EQ, get_master_identity_key(), sizeof(id));
  413. tt_mem_op(&sign, OP_EQ, get_master_signing_keypair(), sizeof(sign));
  414. tt_assert(tor_cert_eq(link_cert, get_current_link_cert_cert()));
  415. tt_mem_op(&auth, OP_NE, get_current_auth_keypair(), sizeof(auth));
  416. tt_assert(get_master_signing_key_cert());
  417. tt_assert(get_current_link_cert_cert());
  418. tt_assert(get_current_auth_key_cert());
  419. tor_cert_free(link_cert);
  420. link_cert = tor_cert_dup(get_current_link_cert_cert());
  421. memcpy(&auth, get_current_auth_keypair(), sizeof(auth));
  422. /* Force a link/auth-key regeneration by advancing time. */
  423. tt_int_op(0, OP_EQ, load_ed_keys(options, now+3*86400));
  424. tt_int_op(0, OP_EQ, generate_ed_link_cert(options, now+3*86400, 0));
  425. tt_mem_op(&id, OP_EQ, get_master_identity_key(), sizeof(id));
  426. tt_mem_op(&sign, OP_EQ, get_master_signing_keypair(), sizeof(sign));
  427. tt_assert(! tor_cert_eq(link_cert, get_current_link_cert_cert()));
  428. tt_mem_op(&auth, OP_NE, get_current_auth_keypair(), sizeof(auth));
  429. tt_assert(get_master_signing_key_cert());
  430. tt_assert(get_current_link_cert_cert());
  431. tt_assert(get_current_auth_key_cert());
  432. tor_cert_free(link_cert);
  433. link_cert = tor_cert_dup(get_current_link_cert_cert());
  434. memcpy(&auth, get_current_auth_keypair(), sizeof(auth));
  435. /* Force a signing-key regeneration by advancing time. */
  436. tt_int_op(1, OP_EQ, load_ed_keys(options, now+100*86400));
  437. tt_int_op(0, OP_EQ, generate_ed_link_cert(options, now+100*86400, 0));
  438. tt_mem_op(&id, OP_EQ, get_master_identity_key(), sizeof(id));
  439. tt_mem_op(&sign, OP_NE, get_master_signing_keypair(), sizeof(sign));
  440. tt_assert(! tor_cert_eq(link_cert, get_current_link_cert_cert()));
  441. tt_mem_op(&auth, OP_NE, get_current_auth_keypair(), sizeof(auth));
  442. tt_assert(get_master_signing_key_cert());
  443. tt_assert(get_current_link_cert_cert());
  444. tt_assert(get_current_auth_key_cert());
  445. memcpy(&sign, get_master_signing_keypair(), sizeof(sign));
  446. tor_cert_free(link_cert);
  447. link_cert = tor_cert_dup(get_current_link_cert_cert());
  448. memcpy(&auth, get_current_auth_keypair(), sizeof(auth));
  449. /* Demonstrate that we can start up with no secret identity key */
  450. routerkeys_free_all();
  451. unlink(get_fname("test_ed_keys_init_all/keys/"
  452. "ed25519_master_id_secret_key"));
  453. tt_int_op(1, OP_EQ, load_ed_keys(options, now));
  454. tt_int_op(0, OP_EQ, generate_ed_link_cert(options, now, 0));
  455. tt_mem_op(&id, OP_EQ, get_master_identity_key(), sizeof(id));
  456. tt_mem_op(&sign, OP_EQ, get_master_signing_keypair(), sizeof(sign));
  457. tt_assert(! tor_cert_eq(link_cert, get_current_link_cert_cert()));
  458. tt_mem_op(&auth, OP_NE, get_current_auth_keypair(), sizeof(auth));
  459. tt_assert(get_master_signing_key_cert());
  460. tt_assert(get_current_link_cert_cert());
  461. tt_assert(get_current_auth_key_cert());
  462. /* But we're in trouble if we have no id key and our signing key has
  463. expired. */
  464. log_global_min_severity_ = LOG_ERR; /* Suppress warnings.
  465. * XXX (better way to do this)? */
  466. routerkeys_free_all();
  467. tt_int_op(-1, OP_EQ, load_ed_keys(options, now+200*86400));
  468. done:
  469. tor_free(dir);
  470. tor_free(options);
  471. tor_cert_free(link_cert);
  472. routerkeys_free_all();
  473. }
  474. static void
  475. test_routerkeys_cross_certify_ntor(void *args)
  476. {
  477. (void) args;
  478. tor_cert_t *cert = NULL;
  479. curve25519_keypair_t onion_keys;
  480. ed25519_public_key_t master_key;
  481. ed25519_public_key_t onion_check_key;
  482. time_t now = time(NULL);
  483. int sign;
  484. tt_int_op(0, OP_EQ, ed25519_public_from_base64(&master_key,
  485. "IamwritingthesetestsOnARainyAfternoonin2014"));
  486. tt_int_op(0, OP_EQ, curve25519_keypair_generate(&onion_keys, 0));
  487. cert = make_ntor_onion_key_crosscert(&onion_keys,
  488. &master_key,
  489. now, 10000,
  490. &sign);
  491. tt_assert(cert);
  492. tt_assert(sign == 0 || sign == 1);
  493. tt_int_op(cert->cert_type, OP_EQ, CERT_TYPE_ONION_ID);
  494. tt_int_op(1, OP_EQ, ed25519_pubkey_eq(&cert->signed_key, &master_key));
  495. tt_int_op(0, OP_EQ, ed25519_public_key_from_curve25519_public_key(
  496. &onion_check_key, &onion_keys.pubkey, sign));
  497. tt_int_op(0, OP_EQ, tor_cert_checksig(cert, &onion_check_key, now));
  498. done:
  499. tor_cert_free(cert);
  500. }
  501. static void
  502. test_routerkeys_cross_certify_tap(void *args)
  503. {
  504. (void)args;
  505. uint8_t *cc = NULL;
  506. int cc_len;
  507. ed25519_public_key_t master_key;
  508. crypto_pk_t *onion_key = pk_generate(2), *id_key = pk_generate(1);
  509. char digest[20];
  510. char buf[128];
  511. int n;
  512. tt_int_op(0, OP_EQ, ed25519_public_from_base64(&master_key,
  513. "IAlreadyWroteTestsForRouterdescsUsingTheseX"));
  514. cc = make_tap_onion_key_crosscert(onion_key,
  515. &master_key,
  516. id_key, &cc_len);
  517. tt_assert(cc);
  518. tt_assert(cc_len);
  519. n = crypto_pk_public_checksig(onion_key, buf, sizeof(buf),
  520. (char*)cc, cc_len);
  521. tt_int_op(n,OP_GT,0);
  522. tt_int_op(n,OP_EQ,52);
  523. crypto_pk_get_digest(id_key, digest);
  524. tt_mem_op(buf,OP_EQ,digest,20);
  525. tt_mem_op(buf+20,OP_EQ,master_key.pubkey,32);
  526. tt_int_op(0, OP_EQ, check_tap_onion_key_crosscert(cc, cc_len,
  527. onion_key, &master_key, (uint8_t*)digest));
  528. done:
  529. tor_free(cc);
  530. crypto_pk_free(id_key);
  531. crypto_pk_free(onion_key);
  532. }
  533. static void
  534. test_routerkeys_rsa_ed_crosscert(void *arg)
  535. {
  536. (void)arg;
  537. ed25519_public_key_t ed;
  538. crypto_pk_t *rsa = pk_generate(2);
  539. uint8_t *cc = NULL;
  540. ssize_t cc_len;
  541. time_t expires_in = 1470846177;
  542. tt_int_op(0, OP_EQ, ed25519_public_from_base64(&ed,
  543. "ThisStringCanContainAnythingSoNoKeyHereNowX"));
  544. cc_len = tor_make_rsa_ed25519_crosscert(&ed, rsa, expires_in, &cc);
  545. tt_int_op(cc_len, OP_GT, 0);
  546. tt_int_op(cc_len, OP_GT, 37); /* key, expires, siglen */
  547. tt_mem_op(cc, OP_EQ, ed.pubkey, 32);
  548. time_t expires_out = 3600 * ntohl(get_uint32(cc+32));
  549. tt_int_op(expires_out, OP_GE, expires_in);
  550. tt_int_op(expires_out, OP_LE, expires_in + 3600);
  551. tt_int_op(cc_len, OP_EQ, 37 + get_uint8(cc+36));
  552. tt_int_op(0, OP_EQ, rsa_ed25519_crosscert_check(cc, cc_len, rsa, &ed,
  553. expires_in - 10));
  554. /* Now try after it has expired */
  555. tt_int_op(-4, OP_EQ, rsa_ed25519_crosscert_check(cc, cc_len, rsa, &ed,
  556. expires_out + 1));
  557. /* Truncated object */
  558. tt_int_op(-2, OP_EQ, rsa_ed25519_crosscert_check(cc, cc_len - 2, rsa, &ed,
  559. expires_in - 10));
  560. /* Key not as expected */
  561. cc[0] ^= 3;
  562. tt_int_op(-3, OP_EQ, rsa_ed25519_crosscert_check(cc, cc_len, rsa, &ed,
  563. expires_in - 10));
  564. cc[0] ^= 3;
  565. /* Bad signature */
  566. cc[40] ^= 3;
  567. tt_int_op(-5, OP_EQ, rsa_ed25519_crosscert_check(cc, cc_len, rsa, &ed,
  568. expires_in - 10));
  569. cc[40] ^= 3;
  570. /* Signature of wrong data */
  571. cc[0] ^= 3;
  572. ed.pubkey[0] ^= 3;
  573. tt_int_op(-6, OP_EQ, rsa_ed25519_crosscert_check(cc, cc_len, rsa, &ed,
  574. expires_in - 10));
  575. cc[0] ^= 3;
  576. ed.pubkey[0] ^= 3;
  577. done:
  578. crypto_pk_free(rsa);
  579. tor_free(cc);
  580. }
  581. #define TEST(name, flags) \
  582. { #name , test_routerkeys_ ## name, (flags), NULL, NULL }
  583. struct testcase_t routerkeys_tests[] = {
  584. TEST(write_fingerprint, TT_FORK),
  585. TEST(ed_certs, TT_FORK),
  586. TEST(ed_key_create, TT_FORK),
  587. TEST(ed_key_init_basic, TT_FORK),
  588. TEST(ed_key_init_split, TT_FORK),
  589. TEST(ed_keys_init_all, TT_FORK),
  590. TEST(cross_certify_ntor, 0),
  591. TEST(cross_certify_tap, 0),
  592. TEST(rsa_ed_crosscert, 0),
  593. END_OF_TESTCASES
  594. };