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