test_routerkeys.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  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. tt_assert(kp1 == NULL);
  229. tt_assert(cert == NULL);
  230. /* Create the key if requested to do so. */
  231. kp1 = ed_key_init_from_file(fname1, INIT_ED_KEY_CREATE, LOG_INFO,
  232. NULL, now, 0, 7, &cert);
  233. tt_assert(kp1 != NULL);
  234. tt_assert(cert == NULL);
  235. tt_int_op(stat(get_fname("test_ed_key_1_cert"), &st), OP_LT, 0);
  236. tt_int_op(stat(get_fname("test_ed_key_1_secret_key"), &st), OP_EQ, 0);
  237. /* Fail to load if we say we need a cert */
  238. kp2 = ed_key_init_from_file(fname1, INIT_ED_KEY_NEEDCERT, LOG_INFO,
  239. NULL, now, 0, 7, &cert);
  240. tt_assert(kp2 == NULL);
  241. /* Fail to load if we say the wrong key type */
  242. kp2 = ed_key_init_from_file(fname1, 0, LOG_INFO,
  243. NULL, now, 0, 6, &cert);
  244. tt_assert(kp2 == NULL);
  245. /* Load successfully if we're not picky, whether we say "create" or not. */
  246. kp2 = ed_key_init_from_file(fname1, INIT_ED_KEY_CREATE, 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. kp2 = ed_key_init_from_file(fname1, 0, LOG_INFO,
  253. NULL, now, 0, 7, &cert);
  254. tt_assert(kp2 != NULL);
  255. tt_assert(cert == NULL);
  256. tt_mem_op(kp1, OP_EQ, kp2, sizeof(*kp1));
  257. ed25519_keypair_free(kp2); kp2 = NULL;
  258. /* Now create a key with a cert. */
  259. kp2 = ed_key_init_from_file(fname2, (INIT_ED_KEY_CREATE|
  260. INIT_ED_KEY_NEEDCERT),
  261. LOG_INFO, kp1, now, 7200, 7, &cert);
  262. tt_assert(kp2 != NULL);
  263. tt_assert(cert != NULL);
  264. tt_mem_op(kp1, OP_NE, kp2, sizeof(*kp1));
  265. tt_int_op(stat(get_fname("test_ed_key_2_cert"), &st), OP_EQ, 0);
  266. tt_int_op(stat(get_fname("test_ed_key_2_secret_key"), &st), OP_EQ, 0);
  267. tt_assert(cert->cert_valid == 1);
  268. tt_mem_op(&cert->signed_key, OP_EQ, &kp2->pubkey, 32);
  269. /* Now verify we can load the cert... */
  270. kp3 = ed_key_init_from_file(fname2, (INIT_ED_KEY_CREATE|
  271. INIT_ED_KEY_NEEDCERT),
  272. LOG_INFO, kp1, now, 7200, 7, &cert2);
  273. tt_mem_op(kp2, OP_EQ, kp3, sizeof(*kp2));
  274. tt_mem_op(cert2->encoded, OP_EQ, cert->encoded, cert->encoded_len);
  275. ed25519_keypair_free(kp3); kp3 = NULL;
  276. tor_cert_free(cert2); cert2 = NULL;
  277. /* ... even without create... */
  278. kp3 = ed_key_init_from_file(fname2, INIT_ED_KEY_NEEDCERT,
  279. LOG_INFO, kp1, now, 7200, 7, &cert2);
  280. tt_mem_op(kp2, OP_EQ, kp3, sizeof(*kp2));
  281. tt_mem_op(cert2->encoded, OP_EQ, cert->encoded, cert->encoded_len);
  282. ed25519_keypair_free(kp3); kp3 = NULL;
  283. tor_cert_free(cert2); cert2 = NULL;
  284. /* ... but that we don't crash or anything if we say we don't want it. */
  285. kp3 = ed_key_init_from_file(fname2, INIT_ED_KEY_NEEDCERT,
  286. LOG_INFO, kp1, now, 7200, 7, NULL);
  287. tt_mem_op(kp2, OP_EQ, kp3, sizeof(*kp2));
  288. ed25519_keypair_free(kp3); kp3 = NULL;
  289. /* Fail if we're told the wrong signing key */
  290. kp3 = ed_key_init_from_file(fname2, INIT_ED_KEY_NEEDCERT,
  291. LOG_INFO, kp2, now, 7200, 7, &cert2);
  292. tt_assert(kp3 == NULL);
  293. tt_assert(cert2 == NULL);
  294. done:
  295. ed25519_keypair_free(kp1);
  296. ed25519_keypair_free(kp2);
  297. ed25519_keypair_free(kp3);
  298. tor_cert_free(cert);
  299. tor_cert_free(cert2);
  300. tor_free(fname1);
  301. tor_free(fname2);
  302. }
  303. static void
  304. test_routerkeys_ed_key_init_split(void *arg)
  305. {
  306. (void) arg;
  307. tor_cert_t *cert = NULL;
  308. ed25519_keypair_t *kp1 = NULL, *kp2 = NULL;
  309. time_t now = time(NULL);
  310. char *fname1 = tor_strdup(get_fname("test_ed_key_3"));
  311. char *fname2 = tor_strdup(get_fname("test_ed_key_4"));
  312. struct stat st;
  313. const uint32_t flags = INIT_ED_KEY_SPLIT|INIT_ED_KEY_MISSING_SECRET_OK;
  314. unlink(fname1);
  315. unlink(fname2);
  316. /* Can't load key that isn't there. */
  317. kp1 = ed_key_init_from_file(fname1, flags, LOG_INFO, NULL, now, 0, 7, &cert);
  318. tt_assert(kp1 == NULL);
  319. tt_assert(cert == NULL);
  320. /* Create a split key */
  321. kp1 = ed_key_init_from_file(fname1, flags|INIT_ED_KEY_CREATE,
  322. LOG_INFO, NULL, now, 0, 7, &cert);
  323. tt_assert(kp1 != NULL);
  324. tt_assert(cert == NULL);
  325. tt_int_op(stat(get_fname("test_ed_key_3_cert"), &st), OP_LT, 0);
  326. tt_int_op(stat(get_fname("test_ed_key_3_secret_key"), &st), OP_EQ, 0);
  327. tt_int_op(stat(get_fname("test_ed_key_3_public_key"), &st), OP_EQ, 0);
  328. /* Load it. */
  329. kp2 = ed_key_init_from_file(fname1, flags|INIT_ED_KEY_CREATE,
  330. LOG_INFO, NULL, now, 0, 7, &cert);
  331. tt_assert(kp2 != NULL);
  332. tt_assert(cert == NULL);
  333. tt_mem_op(kp1, OP_EQ, kp2, sizeof(*kp2));
  334. ed25519_keypair_free(kp2); kp2 = NULL;
  335. /* Okay, try killing the secret key and loading it. */
  336. unlink(get_fname("test_ed_key_3_secret_key"));
  337. kp2 = ed_key_init_from_file(fname1, flags,
  338. LOG_INFO, NULL, now, 0, 7, &cert);
  339. tt_assert(kp2 != NULL);
  340. tt_assert(cert == NULL);
  341. tt_mem_op(&kp1->pubkey, OP_EQ, &kp2->pubkey, sizeof(kp2->pubkey));
  342. tt_assert(tor_mem_is_zero((char*)kp2->seckey.seckey,
  343. sizeof(kp2->seckey.seckey)));
  344. ed25519_keypair_free(kp2); kp2 = NULL;
  345. /* Even when we're told to "create", don't create if there's a public key */
  346. kp2 = ed_key_init_from_file(fname1, flags|INIT_ED_KEY_CREATE,
  347. LOG_INFO, NULL, now, 0, 7, &cert);
  348. tt_assert(kp2 != NULL);
  349. tt_assert(cert == NULL);
  350. tt_mem_op(&kp1->pubkey, OP_EQ, &kp2->pubkey, sizeof(kp2->pubkey));
  351. tt_assert(tor_mem_is_zero((char*)kp2->seckey.seckey,
  352. sizeof(kp2->seckey.seckey)));
  353. ed25519_keypair_free(kp2); kp2 = NULL;
  354. /* Make sure we fail on a tag mismatch, though */
  355. kp2 = ed_key_init_from_file(fname1, flags,
  356. LOG_INFO, NULL, now, 0, 99, &cert);
  357. tt_assert(kp2 == NULL);
  358. done:
  359. ed25519_keypair_free(kp1);
  360. ed25519_keypair_free(kp2);
  361. tor_cert_free(cert);
  362. tor_free(fname1);
  363. tor_free(fname2);
  364. }
  365. static void
  366. test_routerkeys_ed_keys_init_all(void *arg)
  367. {
  368. (void)arg;
  369. char *dir = tor_strdup(get_fname("test_ed_keys_init_all"));
  370. char *keydir = tor_strdup(get_fname("test_ed_keys_init_all/KEYS"));
  371. or_options_t *options = tor_malloc_zero(sizeof(or_options_t));
  372. time_t now = time(NULL);
  373. ed25519_public_key_t id;
  374. ed25519_keypair_t sign, auth;
  375. tor_cert_t *link_cert = NULL;
  376. get_options_mutable()->ORPort_set = 1;
  377. crypto_pk_t *rsa = pk_generate(0);
  378. set_server_identity_key(rsa);
  379. set_client_identity_key(rsa);
  380. router_initialize_tls_context();
  381. options->SigningKeyLifetime = 30*86400;
  382. options->TestingAuthKeyLifetime = 2*86400;
  383. options->TestingLinkCertLifetime = 2*86400;
  384. options->TestingSigningKeySlop = 2*86400;
  385. options->TestingAuthKeySlop = 2*3600;
  386. options->TestingLinkKeySlop = 2*3600;
  387. #ifdef _WIN32
  388. mkdir(dir);
  389. mkdir(keydir);
  390. #else
  391. mkdir(dir, 0700);
  392. mkdir(keydir, 0700);
  393. #endif /* defined(_WIN32) */
  394. options->DataDirectory = dir;
  395. options->KeyDirectory = keydir;
  396. tt_int_op(1, OP_EQ, load_ed_keys(options, now));
  397. tt_int_op(0, OP_EQ, generate_ed_link_cert(options, now, 0));
  398. tt_assert(get_master_identity_key());
  399. tt_assert(get_master_identity_key());
  400. tt_assert(get_master_signing_keypair());
  401. tt_assert(get_current_auth_keypair());
  402. tt_assert(get_master_signing_key_cert());
  403. tt_assert(get_current_link_cert_cert());
  404. tt_assert(get_current_auth_key_cert());
  405. memcpy(&id, get_master_identity_key(), sizeof(id));
  406. memcpy(&sign, get_master_signing_keypair(), sizeof(sign));
  407. memcpy(&auth, get_current_auth_keypair(), sizeof(auth));
  408. link_cert = tor_cert_dup(get_current_link_cert_cert());
  409. /* Call load_ed_keys again, but nothing has changed. */
  410. tt_int_op(0, 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_mem_op(&auth, OP_EQ, get_current_auth_keypair(), sizeof(auth));
  415. tt_assert(tor_cert_eq(link_cert, get_current_link_cert_cert()));
  416. /* Force a reload: we make new link/auth keys. */
  417. routerkeys_free_all();
  418. tt_int_op(1, OP_EQ, load_ed_keys(options, now));
  419. tt_int_op(0, OP_EQ, generate_ed_link_cert(options, now, 0));
  420. tt_mem_op(&id, OP_EQ, get_master_identity_key(), sizeof(id));
  421. tt_mem_op(&sign, OP_EQ, get_master_signing_keypair(), sizeof(sign));
  422. tt_assert(tor_cert_eq(link_cert, get_current_link_cert_cert()));
  423. tt_mem_op(&auth, OP_NE, get_current_auth_keypair(), sizeof(auth));
  424. tt_assert(get_master_signing_key_cert());
  425. tt_assert(get_current_link_cert_cert());
  426. tt_assert(get_current_auth_key_cert());
  427. tor_cert_free(link_cert);
  428. link_cert = tor_cert_dup(get_current_link_cert_cert());
  429. memcpy(&auth, get_current_auth_keypair(), sizeof(auth));
  430. /* Force a link/auth-key regeneration by advancing time. */
  431. tt_int_op(0, OP_EQ, load_ed_keys(options, now+3*86400));
  432. tt_int_op(0, OP_EQ, generate_ed_link_cert(options, now+3*86400, 0));
  433. tt_mem_op(&id, OP_EQ, get_master_identity_key(), sizeof(id));
  434. tt_mem_op(&sign, OP_EQ, get_master_signing_keypair(), sizeof(sign));
  435. tt_assert(! tor_cert_eq(link_cert, get_current_link_cert_cert()));
  436. tt_mem_op(&auth, OP_NE, get_current_auth_keypair(), sizeof(auth));
  437. tt_assert(get_master_signing_key_cert());
  438. tt_assert(get_current_link_cert_cert());
  439. tt_assert(get_current_auth_key_cert());
  440. tor_cert_free(link_cert);
  441. link_cert = tor_cert_dup(get_current_link_cert_cert());
  442. memcpy(&auth, get_current_auth_keypair(), sizeof(auth));
  443. /* Force a signing-key regeneration by advancing time. */
  444. tt_int_op(1, OP_EQ, load_ed_keys(options, now+100*86400));
  445. tt_int_op(0, OP_EQ, generate_ed_link_cert(options, now+100*86400, 0));
  446. tt_mem_op(&id, OP_EQ, get_master_identity_key(), sizeof(id));
  447. tt_mem_op(&sign, OP_NE, get_master_signing_keypair(), sizeof(sign));
  448. tt_assert(! tor_cert_eq(link_cert, get_current_link_cert_cert()));
  449. tt_mem_op(&auth, OP_NE, get_current_auth_keypair(), sizeof(auth));
  450. tt_assert(get_master_signing_key_cert());
  451. tt_assert(get_current_link_cert_cert());
  452. tt_assert(get_current_auth_key_cert());
  453. memcpy(&sign, get_master_signing_keypair(), sizeof(sign));
  454. tor_cert_free(link_cert);
  455. link_cert = tor_cert_dup(get_current_link_cert_cert());
  456. memcpy(&auth, get_current_auth_keypair(), sizeof(auth));
  457. /* Demonstrate that we can start up with no secret identity key */
  458. routerkeys_free_all();
  459. unlink(get_fname("test_ed_keys_init_all/KEYS/"
  460. "ed25519_master_id_secret_key"));
  461. tt_int_op(1, OP_EQ, load_ed_keys(options, now));
  462. tt_int_op(0, OP_EQ, generate_ed_link_cert(options, now, 0));
  463. tt_mem_op(&id, OP_EQ, get_master_identity_key(), sizeof(id));
  464. tt_mem_op(&sign, OP_EQ, get_master_signing_keypair(), sizeof(sign));
  465. tt_assert(! tor_cert_eq(link_cert, get_current_link_cert_cert()));
  466. tt_mem_op(&auth, OP_NE, get_current_auth_keypair(), sizeof(auth));
  467. tt_assert(get_master_signing_key_cert());
  468. tt_assert(get_current_link_cert_cert());
  469. tt_assert(get_current_auth_key_cert());
  470. /* But we're in trouble if we have no id key and our signing key has
  471. expired. */
  472. log_global_min_severity_ = LOG_ERR; /* Suppress warnings.
  473. * XXX (better way to do this)? */
  474. routerkeys_free_all();
  475. tt_int_op(-1, OP_EQ, load_ed_keys(options, now+200*86400));
  476. done:
  477. tor_free(dir);
  478. tor_free(keydir);
  479. tor_free(options);
  480. tor_cert_free(link_cert);
  481. routerkeys_free_all();
  482. }
  483. static void
  484. test_routerkeys_cross_certify_ntor(void *args)
  485. {
  486. (void) args;
  487. tor_cert_t *cert = NULL;
  488. curve25519_keypair_t onion_keys;
  489. ed25519_public_key_t master_key;
  490. ed25519_public_key_t onion_check_key;
  491. time_t now = time(NULL);
  492. int sign;
  493. tt_int_op(0, OP_EQ, ed25519_public_from_base64(&master_key,
  494. "IamwritingthesetestsOnARainyAfternoonin2014"));
  495. tt_int_op(0, OP_EQ, curve25519_keypair_generate(&onion_keys, 0));
  496. cert = make_ntor_onion_key_crosscert(&onion_keys,
  497. &master_key,
  498. now, 10000,
  499. &sign);
  500. tt_assert(cert);
  501. tt_assert(sign == 0 || sign == 1);
  502. tt_int_op(cert->cert_type, OP_EQ, CERT_TYPE_ONION_ID);
  503. tt_int_op(1, OP_EQ, ed25519_pubkey_eq(&cert->signed_key, &master_key));
  504. tt_int_op(0, OP_EQ, ed25519_public_key_from_curve25519_public_key(
  505. &onion_check_key, &onion_keys.pubkey, sign));
  506. tt_int_op(0, OP_EQ, tor_cert_checksig(cert, &onion_check_key, now));
  507. done:
  508. tor_cert_free(cert);
  509. }
  510. static void
  511. test_routerkeys_cross_certify_tap(void *args)
  512. {
  513. (void)args;
  514. uint8_t *cc = NULL;
  515. int cc_len;
  516. ed25519_public_key_t master_key;
  517. crypto_pk_t *onion_key = pk_generate(2), *id_key = pk_generate(1);
  518. char digest[20];
  519. char buf[128];
  520. int n;
  521. tt_int_op(0, OP_EQ, ed25519_public_from_base64(&master_key,
  522. "IAlreadyWroteTestsForRouterdescsUsingTheseX"));
  523. cc = make_tap_onion_key_crosscert(onion_key,
  524. &master_key,
  525. id_key, &cc_len);
  526. tt_assert(cc);
  527. tt_assert(cc_len);
  528. n = crypto_pk_public_checksig(onion_key, buf, sizeof(buf),
  529. (char*)cc, cc_len);
  530. tt_int_op(n,OP_GT,0);
  531. tt_int_op(n,OP_EQ,52);
  532. crypto_pk_get_digest(id_key, digest);
  533. tt_mem_op(buf,OP_EQ,digest,20);
  534. tt_mem_op(buf+20,OP_EQ,master_key.pubkey,32);
  535. tt_int_op(0, OP_EQ, check_tap_onion_key_crosscert(cc, cc_len,
  536. onion_key, &master_key, (uint8_t*)digest));
  537. done:
  538. tor_free(cc);
  539. crypto_pk_free(id_key);
  540. crypto_pk_free(onion_key);
  541. }
  542. static void
  543. test_routerkeys_rsa_ed_crosscert(void *arg)
  544. {
  545. (void)arg;
  546. ed25519_public_key_t ed;
  547. crypto_pk_t *rsa = pk_generate(2);
  548. uint8_t *cc = NULL;
  549. ssize_t cc_len;
  550. time_t expires_in = 1470846177;
  551. tt_int_op(0, OP_EQ, ed25519_public_from_base64(&ed,
  552. "ThisStringCanContainAnythingSoNoKeyHereNowX"));
  553. cc_len = tor_make_rsa_ed25519_crosscert(&ed, rsa, expires_in, &cc);
  554. tt_int_op(cc_len, OP_GT, 0);
  555. tt_int_op(cc_len, OP_GT, 37); /* key, expires, siglen */
  556. tt_mem_op(cc, OP_EQ, ed.pubkey, 32);
  557. time_t expires_out = 3600 * ntohl(get_uint32(cc+32));
  558. tt_int_op(expires_out, OP_GE, expires_in);
  559. tt_int_op(expires_out, OP_LE, expires_in + 3600);
  560. tt_int_op(cc_len, OP_EQ, 37 + get_uint8(cc+36));
  561. tt_int_op(0, OP_EQ, rsa_ed25519_crosscert_check(cc, cc_len, rsa, &ed,
  562. expires_in - 10));
  563. /* Now try after it has expired */
  564. tt_int_op(-4, OP_EQ, rsa_ed25519_crosscert_check(cc, cc_len, rsa, &ed,
  565. expires_out + 1));
  566. /* Truncated object */
  567. tt_int_op(-2, OP_EQ, rsa_ed25519_crosscert_check(cc, cc_len - 2, rsa, &ed,
  568. expires_in - 10));
  569. /* Key not as expected */
  570. cc[0] ^= 3;
  571. tt_int_op(-3, OP_EQ, rsa_ed25519_crosscert_check(cc, cc_len, rsa, &ed,
  572. expires_in - 10));
  573. cc[0] ^= 3;
  574. /* Bad signature */
  575. cc[40] ^= 3;
  576. tt_int_op(-5, OP_EQ, rsa_ed25519_crosscert_check(cc, cc_len, rsa, &ed,
  577. expires_in - 10));
  578. cc[40] ^= 3;
  579. /* Signature of wrong data */
  580. cc[0] ^= 3;
  581. ed.pubkey[0] ^= 3;
  582. tt_int_op(-6, OP_EQ, rsa_ed25519_crosscert_check(cc, cc_len, rsa, &ed,
  583. expires_in - 10));
  584. cc[0] ^= 3;
  585. ed.pubkey[0] ^= 3;
  586. done:
  587. crypto_pk_free(rsa);
  588. tor_free(cc);
  589. }
  590. #define TEST(name, flags) \
  591. { #name , test_routerkeys_ ## name, (flags), NULL, NULL }
  592. struct testcase_t routerkeys_tests[] = {
  593. TEST(write_fingerprint, TT_FORK),
  594. TEST(ed_certs, TT_FORK),
  595. TEST(ed_key_create, TT_FORK),
  596. TEST(ed_key_init_basic, TT_FORK),
  597. TEST(ed_key_init_split, TT_FORK),
  598. TEST(ed_keys_init_all, TT_FORK),
  599. TEST(cross_certify_ntor, 0),
  600. TEST(cross_certify_tap, 0),
  601. TEST(rsa_ed_crosscert, 0),
  602. END_OF_TESTCASES
  603. };