test_routerkeys.c 24 KB

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