test_routerkeys.c 24 KB

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