test_routerkeys.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  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. char *keydir = tor_strdup(get_fname("test_ed_keys_init_all/KEYS"));
  365. or_options_t *options = tor_malloc_zero(sizeof(or_options_t));
  366. time_t now = time(NULL);
  367. ed25519_public_key_t id;
  368. ed25519_keypair_t sign, auth;
  369. tor_cert_t *link_cert = NULL;
  370. get_options_mutable()->ORPort_set = 1;
  371. crypto_pk_t *rsa = pk_generate(0);
  372. set_server_identity_key(rsa);
  373. set_client_identity_key(rsa);
  374. router_initialize_tls_context();
  375. options->SigningKeyLifetime = 30*86400;
  376. options->TestingAuthKeyLifetime = 2*86400;
  377. options->TestingLinkCertLifetime = 2*86400;
  378. options->TestingSigningKeySlop = 2*86400;
  379. options->TestingAuthKeySlop = 2*3600;
  380. options->TestingLinkKeySlop = 2*3600;
  381. #ifdef _WIN32
  382. mkdir(dir);
  383. mkdir(keydir);
  384. #else
  385. mkdir(dir, 0700);
  386. mkdir(keydir, 0700);
  387. #endif /* defined(_WIN32) */
  388. options->DataDirectory = dir;
  389. options->KeyDirectory = keydir;
  390. tt_int_op(1, OP_EQ, load_ed_keys(options, now));
  391. tt_int_op(0, OP_EQ, generate_ed_link_cert(options, now, 0));
  392. tt_assert(get_master_identity_key());
  393. tt_assert(get_master_identity_key());
  394. tt_assert(get_master_signing_keypair());
  395. tt_assert(get_current_auth_keypair());
  396. tt_assert(get_master_signing_key_cert());
  397. tt_assert(get_current_link_cert_cert());
  398. tt_assert(get_current_auth_key_cert());
  399. memcpy(&id, get_master_identity_key(), sizeof(id));
  400. memcpy(&sign, get_master_signing_keypair(), sizeof(sign));
  401. memcpy(&auth, get_current_auth_keypair(), sizeof(auth));
  402. link_cert = tor_cert_dup(get_current_link_cert_cert());
  403. /* Call load_ed_keys again, but nothing has changed. */
  404. tt_int_op(0, OP_EQ, load_ed_keys(options, now));
  405. tt_int_op(0, OP_EQ, generate_ed_link_cert(options, now, 0));
  406. tt_mem_op(&id, OP_EQ, get_master_identity_key(), sizeof(id));
  407. tt_mem_op(&sign, OP_EQ, get_master_signing_keypair(), sizeof(sign));
  408. tt_mem_op(&auth, OP_EQ, get_current_auth_keypair(), sizeof(auth));
  409. tt_assert(tor_cert_eq(link_cert, get_current_link_cert_cert()));
  410. /* Force a reload: we make new link/auth keys. */
  411. routerkeys_free_all();
  412. tt_int_op(1, OP_EQ, load_ed_keys(options, now));
  413. tt_int_op(0, OP_EQ, generate_ed_link_cert(options, now, 0));
  414. tt_mem_op(&id, OP_EQ, get_master_identity_key(), sizeof(id));
  415. tt_mem_op(&sign, OP_EQ, get_master_signing_keypair(), sizeof(sign));
  416. tt_assert(tor_cert_eq(link_cert, get_current_link_cert_cert()));
  417. tt_mem_op(&auth, OP_NE, get_current_auth_keypair(), sizeof(auth));
  418. tt_assert(get_master_signing_key_cert());
  419. tt_assert(get_current_link_cert_cert());
  420. tt_assert(get_current_auth_key_cert());
  421. tor_cert_free(link_cert);
  422. link_cert = tor_cert_dup(get_current_link_cert_cert());
  423. memcpy(&auth, get_current_auth_keypair(), sizeof(auth));
  424. /* Force a link/auth-key regeneration by advancing time. */
  425. tt_int_op(0, OP_EQ, load_ed_keys(options, now+3*86400));
  426. tt_int_op(0, OP_EQ, generate_ed_link_cert(options, now+3*86400, 0));
  427. tt_mem_op(&id, OP_EQ, get_master_identity_key(), sizeof(id));
  428. tt_mem_op(&sign, OP_EQ, get_master_signing_keypair(), sizeof(sign));
  429. tt_assert(! tor_cert_eq(link_cert, get_current_link_cert_cert()));
  430. tt_mem_op(&auth, OP_NE, get_current_auth_keypair(), sizeof(auth));
  431. tt_assert(get_master_signing_key_cert());
  432. tt_assert(get_current_link_cert_cert());
  433. tt_assert(get_current_auth_key_cert());
  434. tor_cert_free(link_cert);
  435. link_cert = tor_cert_dup(get_current_link_cert_cert());
  436. memcpy(&auth, get_current_auth_keypair(), sizeof(auth));
  437. /* Force a signing-key regeneration by advancing time. */
  438. tt_int_op(1, OP_EQ, load_ed_keys(options, now+100*86400));
  439. tt_int_op(0, OP_EQ, generate_ed_link_cert(options, now+100*86400, 0));
  440. tt_mem_op(&id, OP_EQ, get_master_identity_key(), sizeof(id));
  441. tt_mem_op(&sign, OP_NE, get_master_signing_keypair(), sizeof(sign));
  442. tt_assert(! tor_cert_eq(link_cert, get_current_link_cert_cert()));
  443. tt_mem_op(&auth, OP_NE, get_current_auth_keypair(), sizeof(auth));
  444. tt_assert(get_master_signing_key_cert());
  445. tt_assert(get_current_link_cert_cert());
  446. tt_assert(get_current_auth_key_cert());
  447. memcpy(&sign, get_master_signing_keypair(), sizeof(sign));
  448. tor_cert_free(link_cert);
  449. link_cert = tor_cert_dup(get_current_link_cert_cert());
  450. memcpy(&auth, get_current_auth_keypair(), sizeof(auth));
  451. /* Demonstrate that we can start up with no secret identity key */
  452. routerkeys_free_all();
  453. unlink(get_fname("test_ed_keys_init_all/KEYS/"
  454. "ed25519_master_id_secret_key"));
  455. tt_int_op(1, OP_EQ, load_ed_keys(options, now));
  456. tt_int_op(0, OP_EQ, generate_ed_link_cert(options, now, 0));
  457. tt_mem_op(&id, OP_EQ, get_master_identity_key(), sizeof(id));
  458. tt_mem_op(&sign, OP_EQ, get_master_signing_keypair(), sizeof(sign));
  459. tt_assert(! tor_cert_eq(link_cert, get_current_link_cert_cert()));
  460. tt_mem_op(&auth, OP_NE, get_current_auth_keypair(), sizeof(auth));
  461. tt_assert(get_master_signing_key_cert());
  462. tt_assert(get_current_link_cert_cert());
  463. tt_assert(get_current_auth_key_cert());
  464. /* But we're in trouble if we have no id key and our signing key has
  465. expired. */
  466. log_global_min_severity_ = LOG_ERR; /* Suppress warnings.
  467. * XXX (better way to do this)? */
  468. routerkeys_free_all();
  469. tt_int_op(-1, OP_EQ, load_ed_keys(options, now+200*86400));
  470. done:
  471. tor_free(dir);
  472. tor_free(keydir);
  473. tor_free(options);
  474. tor_cert_free(link_cert);
  475. routerkeys_free_all();
  476. }
  477. static void
  478. test_routerkeys_cross_certify_ntor(void *args)
  479. {
  480. (void) args;
  481. tor_cert_t *cert = NULL;
  482. curve25519_keypair_t onion_keys;
  483. ed25519_public_key_t master_key;
  484. ed25519_public_key_t onion_check_key;
  485. time_t now = time(NULL);
  486. int sign;
  487. tt_int_op(0, OP_EQ, ed25519_public_from_base64(&master_key,
  488. "IamwritingthesetestsOnARainyAfternoonin2014"));
  489. tt_int_op(0, OP_EQ, curve25519_keypair_generate(&onion_keys, 0));
  490. cert = make_ntor_onion_key_crosscert(&onion_keys,
  491. &master_key,
  492. now, 10000,
  493. &sign);
  494. tt_assert(cert);
  495. tt_assert(sign == 0 || sign == 1);
  496. tt_int_op(cert->cert_type, OP_EQ, CERT_TYPE_ONION_ID);
  497. tt_int_op(1, OP_EQ, ed25519_pubkey_eq(&cert->signed_key, &master_key));
  498. tt_int_op(0, OP_EQ, ed25519_public_key_from_curve25519_public_key(
  499. &onion_check_key, &onion_keys.pubkey, sign));
  500. tt_int_op(0, OP_EQ, tor_cert_checksig(cert, &onion_check_key, now));
  501. done:
  502. tor_cert_free(cert);
  503. }
  504. static void
  505. test_routerkeys_cross_certify_tap(void *args)
  506. {
  507. (void)args;
  508. uint8_t *cc = NULL;
  509. int cc_len;
  510. ed25519_public_key_t master_key;
  511. crypto_pk_t *onion_key = pk_generate(2), *id_key = pk_generate(1);
  512. char digest[20];
  513. char buf[128];
  514. int n;
  515. tt_int_op(0, OP_EQ, ed25519_public_from_base64(&master_key,
  516. "IAlreadyWroteTestsForRouterdescsUsingTheseX"));
  517. cc = make_tap_onion_key_crosscert(onion_key,
  518. &master_key,
  519. id_key, &cc_len);
  520. tt_assert(cc);
  521. tt_assert(cc_len);
  522. n = crypto_pk_public_checksig(onion_key, buf, sizeof(buf),
  523. (char*)cc, cc_len);
  524. tt_int_op(n,OP_GT,0);
  525. tt_int_op(n,OP_EQ,52);
  526. crypto_pk_get_digest(id_key, digest);
  527. tt_mem_op(buf,OP_EQ,digest,20);
  528. tt_mem_op(buf+20,OP_EQ,master_key.pubkey,32);
  529. tt_int_op(0, OP_EQ, check_tap_onion_key_crosscert(cc, cc_len,
  530. onion_key, &master_key, (uint8_t*)digest));
  531. done:
  532. tor_free(cc);
  533. crypto_pk_free(id_key);
  534. crypto_pk_free(onion_key);
  535. }
  536. static void
  537. test_routerkeys_rsa_ed_crosscert(void *arg)
  538. {
  539. (void)arg;
  540. ed25519_public_key_t ed;
  541. crypto_pk_t *rsa = pk_generate(2);
  542. uint8_t *cc = NULL;
  543. ssize_t cc_len;
  544. time_t expires_in = 1470846177;
  545. tt_int_op(0, OP_EQ, ed25519_public_from_base64(&ed,
  546. "ThisStringCanContainAnythingSoNoKeyHereNowX"));
  547. cc_len = tor_make_rsa_ed25519_crosscert(&ed, rsa, expires_in, &cc);
  548. tt_int_op(cc_len, OP_GT, 0);
  549. tt_int_op(cc_len, OP_GT, 37); /* key, expires, siglen */
  550. tt_mem_op(cc, OP_EQ, ed.pubkey, 32);
  551. time_t expires_out = 3600 * ntohl(get_uint32(cc+32));
  552. tt_int_op(expires_out, OP_GE, expires_in);
  553. tt_int_op(expires_out, OP_LE, expires_in + 3600);
  554. tt_int_op(cc_len, OP_EQ, 37 + get_uint8(cc+36));
  555. tt_int_op(0, OP_EQ, rsa_ed25519_crosscert_check(cc, cc_len, rsa, &ed,
  556. expires_in - 10));
  557. /* Now try after it has expired */
  558. tt_int_op(-4, OP_EQ, rsa_ed25519_crosscert_check(cc, cc_len, rsa, &ed,
  559. expires_out + 1));
  560. /* Truncated object */
  561. tt_int_op(-2, OP_EQ, rsa_ed25519_crosscert_check(cc, cc_len - 2, rsa, &ed,
  562. expires_in - 10));
  563. /* Key not as expected */
  564. cc[0] ^= 3;
  565. tt_int_op(-3, OP_EQ, rsa_ed25519_crosscert_check(cc, cc_len, rsa, &ed,
  566. expires_in - 10));
  567. cc[0] ^= 3;
  568. /* Bad signature */
  569. cc[40] ^= 3;
  570. tt_int_op(-5, OP_EQ, rsa_ed25519_crosscert_check(cc, cc_len, rsa, &ed,
  571. expires_in - 10));
  572. cc[40] ^= 3;
  573. /* Signature of wrong data */
  574. cc[0] ^= 3;
  575. ed.pubkey[0] ^= 3;
  576. tt_int_op(-6, OP_EQ, rsa_ed25519_crosscert_check(cc, cc_len, rsa, &ed,
  577. expires_in - 10));
  578. cc[0] ^= 3;
  579. ed.pubkey[0] ^= 3;
  580. done:
  581. crypto_pk_free(rsa);
  582. tor_free(cc);
  583. }
  584. #define TEST(name, flags) \
  585. { #name , test_routerkeys_ ## name, (flags), NULL, NULL }
  586. struct testcase_t routerkeys_tests[] = {
  587. TEST(write_fingerprint, TT_FORK),
  588. TEST(ed_certs, TT_FORK),
  589. TEST(ed_key_create, TT_FORK),
  590. TEST(ed_key_init_basic, TT_FORK),
  591. TEST(ed_key_init_split, TT_FORK),
  592. TEST(ed_keys_init_all, TT_FORK),
  593. TEST(cross_certify_ntor, 0),
  594. TEST(cross_certify_tap, 0),
  595. TEST(rsa_ed_crosscert, 0),
  596. END_OF_TESTCASES
  597. };