authcert.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209
  1. /* Copyright (c) 2001 Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  4. * Copyright (c) 2007-2019, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file authcert.c
  8. * \brief Code to maintain directory authorities' certificates.
  9. *
  10. * Authority certificates are signed with authority identity keys; they
  11. * are used to authenticate shorter-term authority signing keys. We
  12. * fetch them when we find a consensus or a vote that has been signed
  13. * with a signing key we don't recognize. We cache them on disk and
  14. * load them on startup. Authority operators generate them with the
  15. * "tor-gencert" utility.
  16. */
  17. #include "core/or/or.h"
  18. #include "app/config/config.h"
  19. #include "core/mainloop/connection.h"
  20. #include "core/mainloop/mainloop.h"
  21. #include "core/or/policies.h"
  22. #include "feature/client/bridges.h"
  23. #include "feature/dirauth/authmode.h"
  24. #include "feature/dirclient/dirclient.h"
  25. #include "feature/dirclient/dlstatus.h"
  26. #include "feature/dircommon/directory.h"
  27. #include "feature/dircommon/fp_pair.h"
  28. #include "feature/dirparse/authcert_parse.h"
  29. #include "feature/nodelist/authcert.h"
  30. #include "feature/nodelist/dirlist.h"
  31. #include "feature/nodelist/networkstatus.h"
  32. #include "feature/nodelist/node_select.h"
  33. #include "feature/nodelist/nodelist.h"
  34. #include "feature/nodelist/routerlist.h"
  35. #include "feature/relay/routermode.h"
  36. #include "core/or/connection_st.h"
  37. #include "feature/dirclient/dir_server_st.h"
  38. #include "feature/dircommon/dir_connection_st.h"
  39. #include "feature/nodelist/authority_cert_st.h"
  40. #include "feature/nodelist/document_signature_st.h"
  41. #include "feature/nodelist/networkstatus_st.h"
  42. #include "feature/nodelist/networkstatus_voter_info_st.h"
  43. #include "feature/nodelist/node_st.h"
  44. DECLARE_TYPED_DIGESTMAP_FNS(dsmap_, digest_ds_map_t, download_status_t)
  45. #define DSMAP_FOREACH(map, keyvar, valvar) \
  46. DIGESTMAP_FOREACH(dsmap_to_digestmap(map), keyvar, download_status_t *, \
  47. valvar)
  48. #define dsmap_free(map, fn) MAP_FREE_AND_NULL(dsmap, (map), (fn))
  49. /* Forward declaration for cert_list_t */
  50. typedef struct cert_list_t cert_list_t;
  51. static void download_status_reset_by_sk_in_cl(cert_list_t *cl,
  52. const char *digest);
  53. static int download_status_is_ready_by_sk_in_cl(cert_list_t *cl,
  54. const char *digest,
  55. time_t now);
  56. static void list_pending_fpsk_downloads(fp_pair_map_t *result);
  57. /** List of certificates for a single authority, and download status for
  58. * latest certificate.
  59. */
  60. struct cert_list_t {
  61. /*
  62. * The keys of download status map are cert->signing_key_digest for pending
  63. * downloads by (identity digest/signing key digest) pair; functions such
  64. * as authority_cert_get_by_digest() already assume these are unique.
  65. */
  66. struct digest_ds_map_t *dl_status_map;
  67. /* There is also a dlstatus for the download by identity key only */
  68. download_status_t dl_status_by_id;
  69. smartlist_t *certs;
  70. };
  71. /** Map from v3 identity key digest to cert_list_t. */
  72. static digestmap_t *trusted_dir_certs = NULL;
  73. /** True iff any key certificate in at least one member of
  74. * <b>trusted_dir_certs</b> has changed since we last flushed the
  75. * certificates to disk. */
  76. static int trusted_dir_servers_certs_changed = 0;
  77. /** Initialise schedule, want_authority, and increment_on in the download
  78. * status dlstatus, then call download_status_reset() on it.
  79. * It is safe to call this function or download_status_reset() multiple times
  80. * on a new dlstatus. But it should *not* be called after a dlstatus has been
  81. * used to count download attempts or failures. */
  82. static void
  83. download_status_cert_init(download_status_t *dlstatus)
  84. {
  85. dlstatus->schedule = DL_SCHED_CONSENSUS;
  86. dlstatus->want_authority = DL_WANT_ANY_DIRSERVER;
  87. dlstatus->increment_on = DL_SCHED_INCREMENT_FAILURE;
  88. dlstatus->last_backoff_position = 0;
  89. dlstatus->last_delay_used = 0;
  90. /* Use the new schedule to set next_attempt_at */
  91. download_status_reset(dlstatus);
  92. }
  93. /** Reset the download status of a specified element in a dsmap */
  94. static void
  95. download_status_reset_by_sk_in_cl(cert_list_t *cl, const char *digest)
  96. {
  97. download_status_t *dlstatus = NULL;
  98. tor_assert(cl);
  99. tor_assert(digest);
  100. /* Make sure we have a dsmap */
  101. if (!(cl->dl_status_map)) {
  102. cl->dl_status_map = dsmap_new();
  103. }
  104. /* Look for a download_status_t in the map with this digest */
  105. dlstatus = dsmap_get(cl->dl_status_map, digest);
  106. /* Got one? */
  107. if (!dlstatus) {
  108. /* Insert before we reset */
  109. dlstatus = tor_malloc_zero(sizeof(*dlstatus));
  110. dsmap_set(cl->dl_status_map, digest, dlstatus);
  111. download_status_cert_init(dlstatus);
  112. }
  113. tor_assert(dlstatus);
  114. /* Go ahead and reset it */
  115. download_status_reset(dlstatus);
  116. }
  117. /**
  118. * Return true if the download for this signing key digest in cl is ready
  119. * to be re-attempted.
  120. */
  121. static int
  122. download_status_is_ready_by_sk_in_cl(cert_list_t *cl,
  123. const char *digest,
  124. time_t now)
  125. {
  126. int rv = 0;
  127. download_status_t *dlstatus = NULL;
  128. tor_assert(cl);
  129. tor_assert(digest);
  130. /* Make sure we have a dsmap */
  131. if (!(cl->dl_status_map)) {
  132. cl->dl_status_map = dsmap_new();
  133. }
  134. /* Look for a download_status_t in the map with this digest */
  135. dlstatus = dsmap_get(cl->dl_status_map, digest);
  136. /* Got one? */
  137. if (dlstatus) {
  138. /* Use download_status_is_ready() */
  139. rv = download_status_is_ready(dlstatus, now);
  140. } else {
  141. /*
  142. * If we don't know anything about it, return 1, since we haven't
  143. * tried this one before. We need to create a new entry here,
  144. * too.
  145. */
  146. dlstatus = tor_malloc_zero(sizeof(*dlstatus));
  147. download_status_cert_init(dlstatus);
  148. dsmap_set(cl->dl_status_map, digest, dlstatus);
  149. rv = 1;
  150. }
  151. return rv;
  152. }
  153. /** Helper: Return the cert_list_t for an authority whose authority ID is
  154. * <b>id_digest</b>, allocating a new list if necessary. */
  155. static cert_list_t *
  156. get_cert_list(const char *id_digest)
  157. {
  158. cert_list_t *cl;
  159. if (!trusted_dir_certs)
  160. trusted_dir_certs = digestmap_new();
  161. cl = digestmap_get(trusted_dir_certs, id_digest);
  162. if (!cl) {
  163. cl = tor_malloc_zero(sizeof(cert_list_t));
  164. download_status_cert_init(&cl->dl_status_by_id);
  165. cl->certs = smartlist_new();
  166. cl->dl_status_map = dsmap_new();
  167. digestmap_set(trusted_dir_certs, id_digest, cl);
  168. }
  169. return cl;
  170. }
  171. /** Return a list of authority ID digests with potentially enumerable lists
  172. * of download_status_t objects; used by controller GETINFO queries.
  173. */
  174. MOCK_IMPL(smartlist_t *,
  175. list_authority_ids_with_downloads, (void))
  176. {
  177. smartlist_t *ids = smartlist_new();
  178. digestmap_iter_t *i;
  179. const char *digest;
  180. char *tmp;
  181. void *cl;
  182. if (trusted_dir_certs) {
  183. for (i = digestmap_iter_init(trusted_dir_certs);
  184. !(digestmap_iter_done(i));
  185. i = digestmap_iter_next(trusted_dir_certs, i)) {
  186. /*
  187. * We always have at least dl_status_by_id to query, so no need to
  188. * probe deeper than the existence of a cert_list_t.
  189. */
  190. digestmap_iter_get(i, &digest, &cl);
  191. tmp = tor_malloc(DIGEST_LEN);
  192. memcpy(tmp, digest, DIGEST_LEN);
  193. smartlist_add(ids, tmp);
  194. }
  195. }
  196. /* else definitely no downloads going since nothing even has a cert list */
  197. return ids;
  198. }
  199. /** Given an authority ID digest, return a pointer to the default download
  200. * status, or NULL if there is no such entry in trusted_dir_certs */
  201. MOCK_IMPL(download_status_t *,
  202. id_only_download_status_for_authority_id, (const char *digest))
  203. {
  204. download_status_t *dl = NULL;
  205. cert_list_t *cl;
  206. if (trusted_dir_certs) {
  207. cl = digestmap_get(trusted_dir_certs, digest);
  208. if (cl) {
  209. dl = &(cl->dl_status_by_id);
  210. }
  211. }
  212. return dl;
  213. }
  214. /** Given an authority ID digest, return a smartlist of signing key digests
  215. * for which download_status_t is potentially queryable, or NULL if no such
  216. * authority ID digest is known. */
  217. MOCK_IMPL(smartlist_t *,
  218. list_sk_digests_for_authority_id, (const char *digest))
  219. {
  220. smartlist_t *sks = NULL;
  221. cert_list_t *cl;
  222. dsmap_iter_t *i;
  223. const char *sk_digest;
  224. char *tmp;
  225. download_status_t *dl;
  226. if (trusted_dir_certs) {
  227. cl = digestmap_get(trusted_dir_certs, digest);
  228. if (cl) {
  229. sks = smartlist_new();
  230. if (cl->dl_status_map) {
  231. for (i = dsmap_iter_init(cl->dl_status_map);
  232. !(dsmap_iter_done(i));
  233. i = dsmap_iter_next(cl->dl_status_map, i)) {
  234. /* Pull the digest out and add it to the list */
  235. dsmap_iter_get(i, &sk_digest, &dl);
  236. tmp = tor_malloc(DIGEST_LEN);
  237. memcpy(tmp, sk_digest, DIGEST_LEN);
  238. smartlist_add(sks, tmp);
  239. }
  240. }
  241. }
  242. }
  243. return sks;
  244. }
  245. /** Given an authority ID digest and a signing key digest, return the
  246. * download_status_t or NULL if none exists. */
  247. MOCK_IMPL(download_status_t *,
  248. download_status_for_authority_id_and_sk,(const char *id_digest,
  249. const char *sk_digest))
  250. {
  251. download_status_t *dl = NULL;
  252. cert_list_t *cl = NULL;
  253. if (trusted_dir_certs) {
  254. cl = digestmap_get(trusted_dir_certs, id_digest);
  255. if (cl && cl->dl_status_map) {
  256. dl = dsmap_get(cl->dl_status_map, sk_digest);
  257. }
  258. }
  259. return dl;
  260. }
  261. #define cert_list_free(val) \
  262. FREE_AND_NULL(cert_list_t, cert_list_free_, (val))
  263. /** Release all space held by a cert_list_t */
  264. static void
  265. cert_list_free_(cert_list_t *cl)
  266. {
  267. if (!cl)
  268. return;
  269. SMARTLIST_FOREACH(cl->certs, authority_cert_t *, cert,
  270. authority_cert_free(cert));
  271. smartlist_free(cl->certs);
  272. dsmap_free(cl->dl_status_map, tor_free_);
  273. tor_free(cl);
  274. }
  275. /** Wrapper for cert_list_free so we can pass it to digestmap_free */
  276. static void
  277. cert_list_free_void(void *cl)
  278. {
  279. cert_list_free_(cl);
  280. }
  281. /** Reload the cached v3 key certificates from the cached-certs file in
  282. * the data directory. Return 0 on success, -1 on failure. */
  283. int
  284. trusted_dirs_reload_certs(void)
  285. {
  286. char *filename;
  287. char *contents;
  288. int r;
  289. filename = get_cachedir_fname("cached-certs");
  290. contents = read_file_to_str(filename, RFTS_IGNORE_MISSING, NULL);
  291. tor_free(filename);
  292. if (!contents)
  293. return 0;
  294. r = trusted_dirs_load_certs_from_string(
  295. contents,
  296. TRUSTED_DIRS_CERTS_SRC_FROM_STORE, 1, NULL);
  297. tor_free(contents);
  298. return r;
  299. }
  300. /** Helper: return true iff we already have loaded the exact cert
  301. * <b>cert</b>. */
  302. static inline int
  303. already_have_cert(authority_cert_t *cert)
  304. {
  305. cert_list_t *cl = get_cert_list(cert->cache_info.identity_digest);
  306. SMARTLIST_FOREACH(cl->certs, authority_cert_t *, c,
  307. {
  308. if (tor_memeq(c->cache_info.signed_descriptor_digest,
  309. cert->cache_info.signed_descriptor_digest,
  310. DIGEST_LEN))
  311. return 1;
  312. });
  313. return 0;
  314. }
  315. /** Load a bunch of new key certificates from the string <b>contents</b>. If
  316. * <b>source</b> is TRUSTED_DIRS_CERTS_SRC_FROM_STORE, the certificates are
  317. * from the cache, and we don't need to flush them to disk. If we are a
  318. * dirauth loading our own cert, source is TRUSTED_DIRS_CERTS_SRC_SELF.
  319. * Otherwise, source is download type: TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST
  320. * or TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_SK_DIGEST. If <b>flush</b> is true, we
  321. * need to flush any changed certificates to disk now. Return 0 on success,
  322. * -1 if any certs fail to parse.
  323. *
  324. * If source_dir is non-NULL, it's the identity digest for a directory that
  325. * we've just successfully retrieved certificates from, so try it first to
  326. * fetch any missing certificates.
  327. */
  328. int
  329. trusted_dirs_load_certs_from_string(const char *contents, int source,
  330. int flush, const char *source_dir)
  331. {
  332. dir_server_t *ds;
  333. const char *s, *eos;
  334. int failure_code = 0;
  335. int from_store = (source == TRUSTED_DIRS_CERTS_SRC_FROM_STORE);
  336. int added_trusted_cert = 0;
  337. for (s = contents; *s; s = eos) {
  338. authority_cert_t *cert = authority_cert_parse_from_string(s, strlen(s),
  339. &eos);
  340. cert_list_t *cl;
  341. if (!cert) {
  342. failure_code = -1;
  343. break;
  344. }
  345. ds = trusteddirserver_get_by_v3_auth_digest(
  346. cert->cache_info.identity_digest);
  347. log_debug(LD_DIR, "Parsed certificate for %s",
  348. ds ? ds->nickname : "unknown authority");
  349. if (already_have_cert(cert)) {
  350. /* we already have this one. continue. */
  351. log_info(LD_DIR, "Skipping %s certificate for %s that we "
  352. "already have.",
  353. from_store ? "cached" : "downloaded",
  354. ds ? ds->nickname : "an old or new authority");
  355. /*
  356. * A duplicate on download should be treated as a failure, so we call
  357. * authority_cert_dl_failed() to reset the download status to make sure
  358. * we can't try again. Since we've implemented the fp-sk mechanism
  359. * to download certs by signing key, this should be much rarer than it
  360. * was and is perhaps cause for concern.
  361. */
  362. if (!from_store) {
  363. if (authdir_mode(get_options())) {
  364. log_warn(LD_DIR,
  365. "Got a certificate for %s, but we already have it. "
  366. "Maybe they haven't updated it. Waiting for a while.",
  367. ds ? ds->nickname : "an old or new authority");
  368. } else {
  369. log_info(LD_DIR,
  370. "Got a certificate for %s, but we already have it. "
  371. "Maybe they haven't updated it. Waiting for a while.",
  372. ds ? ds->nickname : "an old or new authority");
  373. }
  374. /*
  375. * This is where we care about the source; authority_cert_dl_failed()
  376. * needs to know whether the download was by fp or (fp,sk) pair to
  377. * twiddle the right bit in the download map.
  378. */
  379. if (source == TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST) {
  380. authority_cert_dl_failed(cert->cache_info.identity_digest,
  381. NULL, 404);
  382. } else if (source == TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_SK_DIGEST) {
  383. authority_cert_dl_failed(cert->cache_info.identity_digest,
  384. cert->signing_key_digest, 404);
  385. }
  386. }
  387. authority_cert_free(cert);
  388. continue;
  389. }
  390. if (ds) {
  391. added_trusted_cert = 1;
  392. log_info(LD_DIR, "Adding %s certificate for directory authority %s with "
  393. "signing key %s", from_store ? "cached" : "downloaded",
  394. ds->nickname, hex_str(cert->signing_key_digest,DIGEST_LEN));
  395. } else {
  396. int adding = we_want_to_fetch_unknown_auth_certs(get_options());
  397. log_info(LD_DIR, "%s %s certificate for unrecognized directory "
  398. "authority with signing key %s",
  399. adding ? "Adding" : "Not adding",
  400. from_store ? "cached" : "downloaded",
  401. hex_str(cert->signing_key_digest,DIGEST_LEN));
  402. if (!adding) {
  403. authority_cert_free(cert);
  404. continue;
  405. }
  406. }
  407. cl = get_cert_list(cert->cache_info.identity_digest);
  408. smartlist_add(cl->certs, cert);
  409. if (ds && cert->cache_info.published_on > ds->addr_current_at) {
  410. /* Check to see whether we should update our view of the authority's
  411. * address. */
  412. if (cert->addr && cert->dir_port &&
  413. (ds->addr != cert->addr ||
  414. ds->dir_port != cert->dir_port)) {
  415. char *a = tor_dup_ip(cert->addr);
  416. log_notice(LD_DIR, "Updating address for directory authority %s "
  417. "from %s:%d to %s:%d based on certificate.",
  418. ds->nickname, ds->address, (int)ds->dir_port,
  419. a, cert->dir_port);
  420. tor_free(a);
  421. ds->addr = cert->addr;
  422. ds->dir_port = cert->dir_port;
  423. }
  424. ds->addr_current_at = cert->cache_info.published_on;
  425. }
  426. if (!from_store)
  427. trusted_dir_servers_certs_changed = 1;
  428. }
  429. if (flush)
  430. trusted_dirs_flush_certs_to_disk();
  431. /* call this even if failure_code is <0, since some certs might have
  432. * succeeded, but only pass source_dir if there were no failures,
  433. * and at least one more authority certificate was added to the store.
  434. * This avoids retrying a directory that's serving bad or entirely duplicate
  435. * certificates. */
  436. if (failure_code == 0 && added_trusted_cert) {
  437. networkstatus_note_certs_arrived(source_dir);
  438. } else {
  439. networkstatus_note_certs_arrived(NULL);
  440. }
  441. return failure_code;
  442. }
  443. /** Save all v3 key certificates to the cached-certs file. */
  444. void
  445. trusted_dirs_flush_certs_to_disk(void)
  446. {
  447. char *filename;
  448. smartlist_t *chunks;
  449. if (!trusted_dir_servers_certs_changed || !trusted_dir_certs)
  450. return;
  451. chunks = smartlist_new();
  452. DIGESTMAP_FOREACH(trusted_dir_certs, key, cert_list_t *, cl) {
  453. SMARTLIST_FOREACH(cl->certs, authority_cert_t *, cert,
  454. {
  455. sized_chunk_t *c = tor_malloc(sizeof(sized_chunk_t));
  456. c->bytes = cert->cache_info.signed_descriptor_body;
  457. c->len = cert->cache_info.signed_descriptor_len;
  458. smartlist_add(chunks, c);
  459. });
  460. } DIGESTMAP_FOREACH_END;
  461. filename = get_cachedir_fname("cached-certs");
  462. if (write_chunks_to_file(filename, chunks, 0, 0)) {
  463. log_warn(LD_FS, "Error writing certificates to disk.");
  464. }
  465. tor_free(filename);
  466. SMARTLIST_FOREACH(chunks, sized_chunk_t *, c, tor_free(c));
  467. smartlist_free(chunks);
  468. trusted_dir_servers_certs_changed = 0;
  469. }
  470. static int
  471. compare_certs_by_pubdates(const void **_a, const void **_b)
  472. {
  473. const authority_cert_t *cert1 = *_a, *cert2=*_b;
  474. if (cert1->cache_info.published_on < cert2->cache_info.published_on)
  475. return -1;
  476. else if (cert1->cache_info.published_on > cert2->cache_info.published_on)
  477. return 1;
  478. else
  479. return 0;
  480. }
  481. /** Remove all expired v3 authority certificates that have been superseded for
  482. * more than 48 hours or, if not expired, that were published more than 7 days
  483. * before being superseded. (If the most recent cert was published more than 48
  484. * hours ago, then we aren't going to get any consensuses signed with older
  485. * keys.) */
  486. void
  487. trusted_dirs_remove_old_certs(void)
  488. {
  489. time_t now = time(NULL);
  490. #define DEAD_CERT_LIFETIME (2*24*60*60)
  491. #define SUPERSEDED_CERT_LIFETIME (2*24*60*60)
  492. if (!trusted_dir_certs)
  493. return;
  494. DIGESTMAP_FOREACH(trusted_dir_certs, key, cert_list_t *, cl) {
  495. /* Sort the list from first-published to last-published */
  496. smartlist_sort(cl->certs, compare_certs_by_pubdates);
  497. SMARTLIST_FOREACH_BEGIN(cl->certs, authority_cert_t *, cert) {
  498. if (cert_sl_idx == smartlist_len(cl->certs) - 1) {
  499. /* This is the most recently published cert. Keep it. */
  500. continue;
  501. }
  502. authority_cert_t *next_cert = smartlist_get(cl->certs, cert_sl_idx+1);
  503. const time_t next_cert_published = next_cert->cache_info.published_on;
  504. if (next_cert_published > now) {
  505. /* All later certs are published in the future. Keep everything
  506. * we didn't discard. */
  507. break;
  508. }
  509. int should_remove = 0;
  510. if (cert->expires + DEAD_CERT_LIFETIME < now) {
  511. /* Certificate has been expired for at least DEAD_CERT_LIFETIME.
  512. * Remove it. */
  513. should_remove = 1;
  514. } else if (next_cert_published + SUPERSEDED_CERT_LIFETIME < now) {
  515. /* Certificate has been superseded for OLD_CERT_LIFETIME.
  516. * Remove it.
  517. */
  518. should_remove = 1;
  519. }
  520. if (should_remove) {
  521. SMARTLIST_DEL_CURRENT_KEEPORDER(cl->certs, cert);
  522. authority_cert_free(cert);
  523. trusted_dir_servers_certs_changed = 1;
  524. }
  525. } SMARTLIST_FOREACH_END(cert);
  526. } DIGESTMAP_FOREACH_END;
  527. #undef DEAD_CERT_LIFETIME
  528. #undef OLD_CERT_LIFETIME
  529. trusted_dirs_flush_certs_to_disk();
  530. }
  531. /** Return the newest v3 authority certificate whose v3 authority identity key
  532. * has digest <b>id_digest</b>. Return NULL if no such authority is known,
  533. * or it has no certificate. */
  534. authority_cert_t *
  535. authority_cert_get_newest_by_id(const char *id_digest)
  536. {
  537. cert_list_t *cl;
  538. authority_cert_t *best = NULL;
  539. if (!trusted_dir_certs ||
  540. !(cl = digestmap_get(trusted_dir_certs, id_digest)))
  541. return NULL;
  542. SMARTLIST_FOREACH(cl->certs, authority_cert_t *, cert,
  543. {
  544. if (!best || cert->cache_info.published_on > best->cache_info.published_on)
  545. best = cert;
  546. });
  547. return best;
  548. }
  549. /** Return the newest v3 authority certificate whose directory signing key has
  550. * digest <b>sk_digest</b>. Return NULL if no such certificate is known.
  551. */
  552. authority_cert_t *
  553. authority_cert_get_by_sk_digest(const char *sk_digest)
  554. {
  555. authority_cert_t *c;
  556. if (!trusted_dir_certs)
  557. return NULL;
  558. if ((c = get_my_v3_authority_cert()) &&
  559. tor_memeq(c->signing_key_digest, sk_digest, DIGEST_LEN))
  560. return c;
  561. if ((c = get_my_v3_legacy_cert()) &&
  562. tor_memeq(c->signing_key_digest, sk_digest, DIGEST_LEN))
  563. return c;
  564. DIGESTMAP_FOREACH(trusted_dir_certs, key, cert_list_t *, cl) {
  565. SMARTLIST_FOREACH(cl->certs, authority_cert_t *, cert,
  566. {
  567. if (tor_memeq(cert->signing_key_digest, sk_digest, DIGEST_LEN))
  568. return cert;
  569. });
  570. } DIGESTMAP_FOREACH_END;
  571. return NULL;
  572. }
  573. /** Return the v3 authority certificate with signing key matching
  574. * <b>sk_digest</b>, for the authority with identity digest <b>id_digest</b>.
  575. * Return NULL if no such authority is known. */
  576. authority_cert_t *
  577. authority_cert_get_by_digests(const char *id_digest,
  578. const char *sk_digest)
  579. {
  580. cert_list_t *cl;
  581. if (!trusted_dir_certs ||
  582. !(cl = digestmap_get(trusted_dir_certs, id_digest)))
  583. return NULL;
  584. SMARTLIST_FOREACH(cl->certs, authority_cert_t *, cert,
  585. if (tor_memeq(cert->signing_key_digest, sk_digest, DIGEST_LEN))
  586. return cert; );
  587. return NULL;
  588. }
  589. /** Add every known authority_cert_t to <b>certs_out</b>. */
  590. void
  591. authority_cert_get_all(smartlist_t *certs_out)
  592. {
  593. tor_assert(certs_out);
  594. if (!trusted_dir_certs)
  595. return;
  596. DIGESTMAP_FOREACH(trusted_dir_certs, key, cert_list_t *, cl) {
  597. SMARTLIST_FOREACH(cl->certs, authority_cert_t *, c,
  598. smartlist_add(certs_out, c));
  599. } DIGESTMAP_FOREACH_END;
  600. }
  601. /** Called when an attempt to download a certificate with the authority with
  602. * ID <b>id_digest</b> and, if not NULL, signed with key signing_key_digest
  603. * fails with HTTP response code <b>status</b>: remember the failure, so we
  604. * don't try again immediately. */
  605. void
  606. authority_cert_dl_failed(const char *id_digest,
  607. const char *signing_key_digest, int status)
  608. {
  609. cert_list_t *cl;
  610. download_status_t *dlstatus = NULL;
  611. char id_digest_str[2*DIGEST_LEN+1];
  612. char sk_digest_str[2*DIGEST_LEN+1];
  613. if (!trusted_dir_certs ||
  614. !(cl = digestmap_get(trusted_dir_certs, id_digest)))
  615. return;
  616. /*
  617. * Are we noting a failed download of the latest cert for the id digest,
  618. * or of a download by (id, signing key) digest pair?
  619. */
  620. if (!signing_key_digest) {
  621. /* Just by id digest */
  622. download_status_failed(&cl->dl_status_by_id, status);
  623. } else {
  624. /* Reset by (id, signing key) digest pair
  625. *
  626. * Look for a download_status_t in the map with this digest
  627. */
  628. dlstatus = dsmap_get(cl->dl_status_map, signing_key_digest);
  629. /* Got one? */
  630. if (dlstatus) {
  631. download_status_failed(dlstatus, status);
  632. } else {
  633. /*
  634. * Do this rather than hex_str(), since hex_str clobbers
  635. * old results and we call twice in the param list.
  636. */
  637. base16_encode(id_digest_str, sizeof(id_digest_str),
  638. id_digest, DIGEST_LEN);
  639. base16_encode(sk_digest_str, sizeof(sk_digest_str),
  640. signing_key_digest, DIGEST_LEN);
  641. log_warn(LD_BUG,
  642. "Got failure for cert fetch with (fp,sk) = (%s,%s), with "
  643. "status %d, but knew nothing about the download.",
  644. id_digest_str, sk_digest_str, status);
  645. }
  646. }
  647. }
  648. static const char *BAD_SIGNING_KEYS[] = {
  649. "09CD84F751FD6E955E0F8ADB497D5401470D697E", // Expires 2015-01-11 16:26:31
  650. "0E7E9C07F0969D0468AD741E172A6109DC289F3C", // Expires 2014-08-12 10:18:26
  651. "57B85409891D3FB32137F642FDEDF8B7F8CDFDCD", // Expires 2015-02-11 17:19:09
  652. "87326329007AF781F587AF5B594E540B2B6C7630", // Expires 2014-07-17 11:10:09
  653. "98CC82342DE8D298CF99D3F1A396475901E0D38E", // Expires 2014-11-10 13:18:56
  654. "9904B52336713A5ADCB13E4FB14DC919E0D45571", // Expires 2014-04-20 20:01:01
  655. "9DCD8E3F1DD1597E2AD476BBA28A1A89F3095227", // Expires 2015-01-16 03:52:30
  656. "A61682F34B9BB9694AC98491FE1ABBFE61923941", // Expires 2014-06-11 09:25:09
  657. "B59F6E99C575113650C99F1C425BA7B20A8C071D", // Expires 2014-07-31 13:22:10
  658. "D27178388FA75B96D37FA36E0B015227DDDBDA51", // Expires 2014-08-04 04:01:57
  659. NULL,
  660. };
  661. /** Return true iff <b>cert</b> authenticates some atuhority signing key
  662. * which, because of the old openssl heartbleed vulnerability, should
  663. * never be trusted. */
  664. int
  665. authority_cert_is_blacklisted(const authority_cert_t *cert)
  666. {
  667. char hex_digest[HEX_DIGEST_LEN+1];
  668. int i;
  669. base16_encode(hex_digest, sizeof(hex_digest),
  670. cert->signing_key_digest, sizeof(cert->signing_key_digest));
  671. for (i = 0; BAD_SIGNING_KEYS[i]; ++i) {
  672. if (!strcasecmp(hex_digest, BAD_SIGNING_KEYS[i])) {
  673. return 1;
  674. }
  675. }
  676. return 0;
  677. }
  678. /** Return true iff when we've been getting enough failures when trying to
  679. * download the certificate with ID digest <b>id_digest</b> that we're willing
  680. * to start bugging the user about it. */
  681. int
  682. authority_cert_dl_looks_uncertain(const char *id_digest)
  683. {
  684. #define N_AUTH_CERT_DL_FAILURES_TO_BUG_USER 2
  685. cert_list_t *cl;
  686. int n_failures;
  687. if (!trusted_dir_certs ||
  688. !(cl = digestmap_get(trusted_dir_certs, id_digest)))
  689. return 0;
  690. n_failures = download_status_get_n_failures(&cl->dl_status_by_id);
  691. return n_failures >= N_AUTH_CERT_DL_FAILURES_TO_BUG_USER;
  692. }
  693. /* Fetch the authority certificates specified in resource.
  694. * If we are a bridge client, and node is a configured bridge, fetch from node
  695. * using dir_hint as the fingerprint. Otherwise, if rs is not NULL, fetch from
  696. * rs. Otherwise, fetch from a random directory mirror. */
  697. static void
  698. authority_certs_fetch_resource_impl(const char *resource,
  699. const char *dir_hint,
  700. const node_t *node,
  701. const routerstatus_t *rs)
  702. {
  703. const or_options_t *options = get_options();
  704. int get_via_tor = purpose_needs_anonymity(DIR_PURPOSE_FETCH_CERTIFICATE, 0,
  705. resource);
  706. /* Make sure bridge clients never connect to anything but a bridge */
  707. if (options->UseBridges) {
  708. if (node && !node_is_a_configured_bridge(node)) {
  709. /* If we're using bridges, and node is not a bridge, use a 3-hop path. */
  710. get_via_tor = 1;
  711. } else if (!node) {
  712. /* If we're using bridges, and there's no node, use a 3-hop path. */
  713. get_via_tor = 1;
  714. }
  715. }
  716. const dir_indirection_t indirection = get_via_tor ? DIRIND_ANONYMOUS
  717. : DIRIND_ONEHOP;
  718. directory_request_t *req = NULL;
  719. /* If we've just downloaded a consensus from a bridge, re-use that
  720. * bridge */
  721. if (options->UseBridges && node && node->ri && !get_via_tor) {
  722. /* clients always make OR connections to bridges */
  723. tor_addr_port_t or_ap;
  724. /* we are willing to use a non-preferred address if we need to */
  725. fascist_firewall_choose_address_node(node, FIREWALL_OR_CONNECTION, 0,
  726. &or_ap);
  727. req = directory_request_new(DIR_PURPOSE_FETCH_CERTIFICATE);
  728. directory_request_set_or_addr_port(req, &or_ap);
  729. if (dir_hint)
  730. directory_request_set_directory_id_digest(req, dir_hint);
  731. } else if (rs) {
  732. /* And if we've just downloaded a consensus from a directory, re-use that
  733. * directory */
  734. req = directory_request_new(DIR_PURPOSE_FETCH_CERTIFICATE);
  735. directory_request_set_routerstatus(req, rs);
  736. }
  737. if (req) {
  738. /* We've set up a request object -- fill in the other request fields, and
  739. * send the request. */
  740. directory_request_set_indirection(req, indirection);
  741. directory_request_set_resource(req, resource);
  742. directory_initiate_request(req);
  743. directory_request_free(req);
  744. return;
  745. }
  746. /* Otherwise, we want certs from a random fallback or directory
  747. * mirror, because they will almost always succeed. */
  748. directory_get_from_dirserver(DIR_PURPOSE_FETCH_CERTIFICATE, 0,
  749. resource, PDS_RETRY_IF_NO_SERVERS,
  750. DL_WANT_ANY_DIRSERVER);
  751. }
  752. /** Try to download any v3 authority certificates that we may be missing. If
  753. * <b>status</b> is provided, try to get all the ones that were used to sign
  754. * <b>status</b>. Additionally, try to have a non-expired certificate for
  755. * every V3 authority in trusted_dir_servers. Don't fetch certificates we
  756. * already have.
  757. *
  758. * If dir_hint is non-NULL, it's the identity digest for a directory that
  759. * we've just successfully retrieved a consensus or certificates from, so try
  760. * it first to fetch any missing certificates.
  761. **/
  762. void
  763. authority_certs_fetch_missing(networkstatus_t *status, time_t now,
  764. const char *dir_hint)
  765. {
  766. /*
  767. * The pending_id digestmap tracks pending certificate downloads by
  768. * identity digest; the pending_cert digestmap tracks pending downloads
  769. * by (identity digest, signing key digest) pairs.
  770. */
  771. digestmap_t *pending_id;
  772. fp_pair_map_t *pending_cert;
  773. /*
  774. * The missing_id_digests smartlist will hold a list of id digests
  775. * we want to fetch the newest cert for; the missing_cert_digests
  776. * smartlist will hold a list of fp_pair_t with an identity and
  777. * signing key digest.
  778. */
  779. smartlist_t *missing_cert_digests, *missing_id_digests;
  780. char *resource = NULL;
  781. cert_list_t *cl;
  782. const or_options_t *options = get_options();
  783. const int keep_unknown = we_want_to_fetch_unknown_auth_certs(options);
  784. fp_pair_t *fp_tmp = NULL;
  785. char id_digest_str[2*DIGEST_LEN+1];
  786. char sk_digest_str[2*DIGEST_LEN+1];
  787. if (should_delay_dir_fetches(options, NULL))
  788. return;
  789. pending_cert = fp_pair_map_new();
  790. pending_id = digestmap_new();
  791. missing_cert_digests = smartlist_new();
  792. missing_id_digests = smartlist_new();
  793. /*
  794. * First, we get the lists of already pending downloads so we don't
  795. * duplicate effort.
  796. */
  797. list_pending_downloads(pending_id, NULL,
  798. DIR_PURPOSE_FETCH_CERTIFICATE, "fp/");
  799. list_pending_fpsk_downloads(pending_cert);
  800. /*
  801. * Now, we download any trusted authority certs we don't have by
  802. * identity digest only. This gets the latest cert for that authority.
  803. */
  804. SMARTLIST_FOREACH_BEGIN(router_get_trusted_dir_servers(),
  805. dir_server_t *, ds) {
  806. int found = 0;
  807. if (!(ds->type & V3_DIRINFO))
  808. continue;
  809. if (smartlist_contains_digest(missing_id_digests,
  810. ds->v3_identity_digest))
  811. continue;
  812. cl = get_cert_list(ds->v3_identity_digest);
  813. SMARTLIST_FOREACH_BEGIN(cl->certs, authority_cert_t *, cert) {
  814. if (now < cert->expires) {
  815. /* It's not expired, and we weren't looking for something to
  816. * verify a consensus with. Call it done. */
  817. download_status_reset(&(cl->dl_status_by_id));
  818. /* No sense trying to download it specifically by signing key hash */
  819. download_status_reset_by_sk_in_cl(cl, cert->signing_key_digest);
  820. found = 1;
  821. break;
  822. }
  823. } SMARTLIST_FOREACH_END(cert);
  824. if (!found &&
  825. download_status_is_ready(&(cl->dl_status_by_id), now) &&
  826. !digestmap_get(pending_id, ds->v3_identity_digest)) {
  827. log_info(LD_DIR,
  828. "No current certificate known for authority %s "
  829. "(ID digest %s); launching request.",
  830. ds->nickname, hex_str(ds->v3_identity_digest, DIGEST_LEN));
  831. smartlist_add(missing_id_digests, ds->v3_identity_digest);
  832. }
  833. } SMARTLIST_FOREACH_END(ds);
  834. /*
  835. * Next, if we have a consensus, scan through it and look for anything
  836. * signed with a key from a cert we don't have. Those get downloaded
  837. * by (fp,sk) pair, but if we don't know any certs at all for the fp
  838. * (identity digest), and it's one of the trusted dir server certs
  839. * we started off above or a pending download in pending_id, don't
  840. * try to get it yet. Most likely, the one we'll get for that will
  841. * have the right signing key too, and we'd just be downloading
  842. * redundantly.
  843. */
  844. if (status) {
  845. SMARTLIST_FOREACH_BEGIN(status->voters, networkstatus_voter_info_t *,
  846. voter) {
  847. if (!smartlist_len(voter->sigs))
  848. continue; /* This authority never signed this consensus, so don't
  849. * go looking for a cert with key digest 0000000000. */
  850. if (!keep_unknown &&
  851. !trusteddirserver_get_by_v3_auth_digest(voter->identity_digest))
  852. continue; /* We don't want unknown certs, and we don't know this
  853. * authority.*/
  854. /*
  855. * If we don't know *any* cert for this authority, and a download by ID
  856. * is pending or we added it to missing_id_digests above, skip this
  857. * one for now to avoid duplicate downloads.
  858. */
  859. cl = get_cert_list(voter->identity_digest);
  860. if (smartlist_len(cl->certs) == 0) {
  861. /* We have no certs at all for this one */
  862. /* Do we have a download of one pending? */
  863. if (digestmap_get(pending_id, voter->identity_digest))
  864. continue;
  865. /*
  866. * Are we about to launch a download of one due to the trusted
  867. * dir server check above?
  868. */
  869. if (smartlist_contains_digest(missing_id_digests,
  870. voter->identity_digest))
  871. continue;
  872. }
  873. SMARTLIST_FOREACH_BEGIN(voter->sigs, document_signature_t *, sig) {
  874. authority_cert_t *cert =
  875. authority_cert_get_by_digests(voter->identity_digest,
  876. sig->signing_key_digest);
  877. if (cert) {
  878. if (now < cert->expires)
  879. download_status_reset_by_sk_in_cl(cl, sig->signing_key_digest);
  880. continue;
  881. }
  882. if (download_status_is_ready_by_sk_in_cl(
  883. cl, sig->signing_key_digest, now) &&
  884. !fp_pair_map_get_by_digests(pending_cert,
  885. voter->identity_digest,
  886. sig->signing_key_digest)) {
  887. /*
  888. * Do this rather than hex_str(), since hex_str clobbers
  889. * old results and we call twice in the param list.
  890. */
  891. base16_encode(id_digest_str, sizeof(id_digest_str),
  892. voter->identity_digest, DIGEST_LEN);
  893. base16_encode(sk_digest_str, sizeof(sk_digest_str),
  894. sig->signing_key_digest, DIGEST_LEN);
  895. if (voter->nickname) {
  896. log_info(LD_DIR,
  897. "We're missing a certificate from authority %s "
  898. "(ID digest %s) with signing key %s: "
  899. "launching request.",
  900. voter->nickname, id_digest_str, sk_digest_str);
  901. } else {
  902. log_info(LD_DIR,
  903. "We're missing a certificate from authority ID digest "
  904. "%s with signing key %s: launching request.",
  905. id_digest_str, sk_digest_str);
  906. }
  907. /* Allocate a new fp_pair_t to append */
  908. fp_tmp = tor_malloc(sizeof(*fp_tmp));
  909. memcpy(fp_tmp->first, voter->identity_digest, sizeof(fp_tmp->first));
  910. memcpy(fp_tmp->second, sig->signing_key_digest,
  911. sizeof(fp_tmp->second));
  912. smartlist_add(missing_cert_digests, fp_tmp);
  913. }
  914. } SMARTLIST_FOREACH_END(sig);
  915. } SMARTLIST_FOREACH_END(voter);
  916. }
  917. /* Bridge clients look up the node for the dir_hint */
  918. const node_t *node = NULL;
  919. /* All clients, including bridge clients, look up the routerstatus for the
  920. * dir_hint */
  921. const routerstatus_t *rs = NULL;
  922. /* If we still need certificates, try the directory that just successfully
  923. * served us a consensus or certificates.
  924. * As soon as the directory fails to provide additional certificates, we try
  925. * another, randomly selected directory. This avoids continual retries.
  926. * (We only ever have one outstanding request per certificate.)
  927. */
  928. if (dir_hint) {
  929. if (options->UseBridges) {
  930. /* Bridge clients try the nodelist. If the dir_hint is from an authority,
  931. * or something else fetched over tor, we won't find the node here, but
  932. * we will find the rs. */
  933. node = node_get_by_id(dir_hint);
  934. }
  935. /* All clients try the consensus routerstatus, then the fallback
  936. * routerstatus */
  937. rs = router_get_consensus_status_by_id(dir_hint);
  938. if (!rs) {
  939. /* This will also find authorities */
  940. const dir_server_t *ds = router_get_fallback_dirserver_by_digest(
  941. dir_hint);
  942. if (ds) {
  943. rs = &ds->fake_status;
  944. }
  945. }
  946. if (!node && !rs) {
  947. log_warn(LD_BUG, "Directory %s delivered a consensus, but %s"
  948. "no routerstatus could be found for it.",
  949. options->UseBridges ? "no node and " : "",
  950. hex_str(dir_hint, DIGEST_LEN));
  951. }
  952. }
  953. /* Do downloads by identity digest */
  954. if (smartlist_len(missing_id_digests) > 0) {
  955. int need_plus = 0;
  956. smartlist_t *fps = smartlist_new();
  957. smartlist_add_strdup(fps, "fp/");
  958. SMARTLIST_FOREACH_BEGIN(missing_id_digests, const char *, d) {
  959. char *fp = NULL;
  960. if (digestmap_get(pending_id, d))
  961. continue;
  962. base16_encode(id_digest_str, sizeof(id_digest_str),
  963. d, DIGEST_LEN);
  964. if (need_plus) {
  965. tor_asprintf(&fp, "+%s", id_digest_str);
  966. } else {
  967. /* No need for tor_asprintf() in this case; first one gets no '+' */
  968. fp = tor_strdup(id_digest_str);
  969. need_plus = 1;
  970. }
  971. smartlist_add(fps, fp);
  972. } SMARTLIST_FOREACH_END(d);
  973. if (smartlist_len(fps) > 1) {
  974. resource = smartlist_join_strings(fps, "", 0, NULL);
  975. /* node and rs are directories that just gave us a consensus or
  976. * certificates */
  977. authority_certs_fetch_resource_impl(resource, dir_hint, node, rs);
  978. tor_free(resource);
  979. }
  980. /* else we didn't add any: they were all pending */
  981. SMARTLIST_FOREACH(fps, char *, cp, tor_free(cp));
  982. smartlist_free(fps);
  983. }
  984. /* Do downloads by identity digest/signing key pair */
  985. if (smartlist_len(missing_cert_digests) > 0) {
  986. int need_plus = 0;
  987. smartlist_t *fp_pairs = smartlist_new();
  988. smartlist_add_strdup(fp_pairs, "fp-sk/");
  989. SMARTLIST_FOREACH_BEGIN(missing_cert_digests, const fp_pair_t *, d) {
  990. char *fp_pair = NULL;
  991. if (fp_pair_map_get(pending_cert, d))
  992. continue;
  993. /* Construct string encodings of the digests */
  994. base16_encode(id_digest_str, sizeof(id_digest_str),
  995. d->first, DIGEST_LEN);
  996. base16_encode(sk_digest_str, sizeof(sk_digest_str),
  997. d->second, DIGEST_LEN);
  998. /* Now tor_asprintf() */
  999. if (need_plus) {
  1000. tor_asprintf(&fp_pair, "+%s-%s", id_digest_str, sk_digest_str);
  1001. } else {
  1002. /* First one in the list doesn't get a '+' */
  1003. tor_asprintf(&fp_pair, "%s-%s", id_digest_str, sk_digest_str);
  1004. need_plus = 1;
  1005. }
  1006. /* Add it to the list of pairs to request */
  1007. smartlist_add(fp_pairs, fp_pair);
  1008. } SMARTLIST_FOREACH_END(d);
  1009. if (smartlist_len(fp_pairs) > 1) {
  1010. resource = smartlist_join_strings(fp_pairs, "", 0, NULL);
  1011. /* node and rs are directories that just gave us a consensus or
  1012. * certificates */
  1013. authority_certs_fetch_resource_impl(resource, dir_hint, node, rs);
  1014. tor_free(resource);
  1015. }
  1016. /* else they were all pending */
  1017. SMARTLIST_FOREACH(fp_pairs, char *, p, tor_free(p));
  1018. smartlist_free(fp_pairs);
  1019. }
  1020. smartlist_free(missing_id_digests);
  1021. SMARTLIST_FOREACH(missing_cert_digests, fp_pair_t *, p, tor_free(p));
  1022. smartlist_free(missing_cert_digests);
  1023. digestmap_free(pending_id, NULL);
  1024. fp_pair_map_free(pending_cert, NULL);
  1025. }
  1026. void
  1027. authcert_free_all(void)
  1028. {
  1029. if (trusted_dir_certs) {
  1030. digestmap_free(trusted_dir_certs, cert_list_free_void);
  1031. trusted_dir_certs = NULL;
  1032. }
  1033. }
  1034. /** Free storage held in <b>cert</b>. */
  1035. void
  1036. authority_cert_free_(authority_cert_t *cert)
  1037. {
  1038. if (!cert)
  1039. return;
  1040. tor_free(cert->cache_info.signed_descriptor_body);
  1041. crypto_pk_free(cert->signing_key);
  1042. crypto_pk_free(cert->identity_key);
  1043. tor_free(cert);
  1044. }
  1045. /** For every certificate we are currently downloading by (identity digest,
  1046. * signing key digest) pair, set result[fp_pair] to (void *1).
  1047. */
  1048. static void
  1049. list_pending_fpsk_downloads(fp_pair_map_t *result)
  1050. {
  1051. const char *pfx = "fp-sk/";
  1052. smartlist_t *tmp;
  1053. smartlist_t *conns;
  1054. const char *resource;
  1055. tor_assert(result);
  1056. tmp = smartlist_new();
  1057. conns = get_connection_array();
  1058. SMARTLIST_FOREACH_BEGIN(conns, connection_t *, conn) {
  1059. if (conn->type == CONN_TYPE_DIR &&
  1060. conn->purpose == DIR_PURPOSE_FETCH_CERTIFICATE &&
  1061. !conn->marked_for_close) {
  1062. resource = TO_DIR_CONN(conn)->requested_resource;
  1063. if (!strcmpstart(resource, pfx))
  1064. dir_split_resource_into_fingerprint_pairs(resource + strlen(pfx),
  1065. tmp);
  1066. }
  1067. } SMARTLIST_FOREACH_END(conn);
  1068. SMARTLIST_FOREACH_BEGIN(tmp, fp_pair_t *, fp) {
  1069. fp_pair_map_set(result, fp, (void*)1);
  1070. tor_free(fp);
  1071. } SMARTLIST_FOREACH_END(fp);
  1072. smartlist_free(tmp);
  1073. }